From: Mike G. v. a. <we...@ma...> - 2008-08-24 01:32:02
|
Log Message: ----------- Added changes that allow backup and restore Modified Files: -------------- wwmoodle/wwassignment4/moodle/mod/wwassignment: backuplib.php lib.php restorelib.php wwmoodle/wwassignment4/moodle/mod/wwassignment/db: install.xml upgrade.php Revision Data ------------- Index: restorelib.php =================================================================== RCS file: /webwork/cvs/system/wwmoodle/wwassignment4/moodle/mod/wwassignment/restorelib.php,v retrieving revision 1.1 retrieving revision 1.2 diff -Lwwassignment4/moodle/mod/wwassignment/restorelib.php -Lwwassignment4/moodle/mod/wwassignment/restorelib.php -u -r1.1 -r1.2 --- wwassignment4/moodle/mod/wwassignment/restorelib.php +++ wwassignment4/moodle/mod/wwassignment/restorelib.php @@ -0,0 +1,178 @@ +<?php //$Id$ + //This php script contains all the stuff to backup/restore + //assignment mods + + //This is the "graphical" structure of the assignment mod: + // + // assignment + // (CL,pk->id) + // | + // | + // | + // assignment_submisions + // (UL,pk->id, fk->assignment,files) + // + // Meaning: pk->primary key field of the table + // fk->foreign key to link with parent + // nt->nested field (recursive data) + // CL->course level info + // UL->user level info + // files->table may have files) + // + //----------------------------------------------------------- + + //This function executes all the restore procedure about this mod + function wwassignment_restore_mods($mod,$restore) { + + global $CFG; + + $status = true; + error_log("mod id ".$mod->id); + if ($mod->id == "66666") { + $wwlinkdata = backup_getid($restore->backup_unique_code,"wwassignment_bridge","wwassignment_bridge"); + error_log("wwlink data ".print_r($wwlinkdata, true )); + return $status; + } + + //Get record from backup_ids + + $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id); + + if ($data) { + //Now get completed xmlized object + $info = $data->info; + //if necessary, write to restorelog and adjust date/time fields + if ($restore->course_startdateoffset) { + //restore_log_date_changes('Wwassignment', $restore, $info['MOD']['#'], array('TIMEDUE', 'TIMEAVAILABLE')); + } + //traverse_xmlize($info); //Debug + //print_object ($GLOBALS['traverse_array']); //Debug + //$GLOBALS['traverse_array']=""; //Debug + + //Now, build the ASSIGNMENT record structure + $wwassignment->course = $restore->course_id; + $wwassignment->name = backup_todb($info['MOD']['#']['NAME']['0']['#']); + $wwassignment->description = backup_todb($info['MOD']['#']['DESCRIPTION']['0']['#']); + $wwassignment->webwork_set = backup_todb($info['MOD']['#']['WEBWORK_SET']['0']['#']); + $wwassignment->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']); + + + + //The structure is equal to the db, so insert the assignment + $newid = insert_record ("wwassignment",$wwassignment); + + //Do some output + if (!defined('RESTORE_SILENTLY')) { + echo "<li>".get_string("modulename","wwassignment")." \"".format_string(stripslashes($wwassignment->name),true)."\"</li>"; + } + backup_flush(300); + + if ($newid) { + //We have the newid, update backup_ids + backup_putid($restore->backup_unique_code,$mod->modtype, + $mod->id, $newid); + + } else { + $status = false; + } + } else { + $status = false; + } + error_log("mod id is ".print_r($mod,true)); + return $status; + } + + + + //This function returns a log record with all the necessay transformations + //done. It's used by restore_log_module() to restore modules log. + function wwassignment_restore_logs($restore,$log) { + + $status = false; + + //Depending of the action, we recode different things + switch ($log->action) { + case "add": + if ($log->cmid) { + //Get the new_id of the module (to recode the info field) + $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info); + if ($mod) { + $log->url = "view.php?id=".$log->cmid; + $log->info = $mod->new_id; + $status = true; + } + } + break; + case "update": + if ($log->cmid) { + //Get the new_id of the module (to recode the info field) + $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info); + if ($mod) { + $log->url = "view.php?id=".$log->cmid; + $log->info = $mod->new_id; + $status = true; + } + } + break; + case "view": + if ($log->cmid) { + //Get the new_id of the module (to recode the info field) + $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info); + if ($mod) { + $log->url = "view.php?id=".$log->cmid; + $log->info = $mod->new_id; + $status = true; + } + } + break; + case "view all": + $log->url = "index.php?id=".$log->course; + $status = true; + break; + case "upload": + if ($log->cmid) { + //Get the new_id of the module (to recode the info field) + $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info); + if ($mod) { + $log->url = "view.php?a=".$mod->new_id; + $log->info = $mod->new_id; + $status = true; + } + } + break; + case "view submission": + if ($log->cmid) { + //Get the new_id of the module (to recode the info field) + $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info); + if ($mod) { + $log->url = "submissions.php?id=".$mod->new_id; + $log->info = $mod->new_id; + $status = true; + } + } + break; + case "update grades": + if ($log->cmid) { + //Extract the assignment id from the url field + $assid = substr(strrchr($log->url,"="),1); + //Get the new_id of the module (to recode the info field) + $mod = backup_getid($restore->backup_unique_code,$log->module,$assid); + if ($mod) { + $log->url = "submissions.php?id=".$mod->new_id; + $status = true; + } + } + break; + default: + if (!defined('RESTORE_SILENTLY')) { + echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug + } + break; + } + + if ($status) { + $status = $log; + } + return $status; + } +?> Index: lib.php =================================================================== RCS file: /webwork/cvs/system/wwmoodle/wwassignment4/moodle/mod/wwassignment/lib.php,v retrieving revision 1.3 retrieving revision 1.4 diff -Lwwassignment4/moodle/mod/wwassignment/lib.php -Lwwassignment4/moodle/mod/wwassignment/lib.php -u -r1.3 -r1.4 --- wwassignment4/moodle/mod/wwassignment/lib.php +++ wwassignment4/moodle/mod/wwassignment/lib.php @@ -175,6 +175,35 @@ * @param int $userid optional user id, 0 means all users * @return array array of grades, false if none */ +// function assignment_get_user_grades($assignment, $userid=0) { +// global $CFG; +// +// $user = $userid ? "AND u.id = $userid" : ""; +// +// $sql = "SELECT u.id, u.id AS userid, s.grade AS rawgrade, s.submissioncomment AS feedback, s.format AS feedbackformat, +// s.teacher AS usermodified, s.timemarked AS dategraded, s.timemodified AS datesubmitted +// FROM {$CFG->prefix}user u, {$CFG->prefix}assignment_submissions s +// WHERE u.id = s.userid AND s.assignment = $assignment->id +// $user"; +// +// return get_records_sql($sql); +// } +// object returned looks like an array of standard objects +// ( +// [22] => stdClass Object +// ( +// [userid] => 22 +// [rawgrade] => -1 +// [feedback] => 23 +// [feedbackformat] => 0 +// [usermodified] => 2 +// [dategraded] => 1211200838 +// [datesubmitted] => 1211199392 +// [id] => 22 +// ) +// +// ) + function wwassignment_get_user_grades($wwassignment,$userid=0) { debugLog("Begin wwassignment_get_user_grades"); //debugLog("inputs -- wwassignment" . print_r($wwassignment,true)); @@ -437,12 +466,7 @@ * @return array Representing time, info pairing. */ function wwassignment_user_complete($course, $user, $mod, $wwassignment) { -$aLogs = get_logs("l.userid=$user AND l.course=$course AND l.cmid={$wwassignment->id}"); - if( count($aLogs) > 0 ) { - $return->time = $aLogs[0]->time; - $return->info = $aLogs[0]->info; - } - return $return; + return true; } @@ -489,7 +513,7 @@ */ function wwassignment_get_participants($wwassignmentid) { - error_log("wwassignment_get_participants -- legacy??"); + error_log("wwassignment_get_participants"); $wwassignment = get_record('wwassignment', 'id', $wwassignmentid); if(!isset($wwassignment)) { return array(); Index: backuplib.php =================================================================== RCS file: /webwork/cvs/system/wwmoodle/wwassignment4/moodle/mod/wwassignment/backuplib.php,v retrieving revision 1.1 retrieving revision 1.2 diff -Lwwassignment4/moodle/mod/wwassignment/backuplib.php -Lwwassignment4/moodle/mod/wwassignment/backuplib.php -u -r1.1 -r1.2 --- wwassignment4/moodle/mod/wwassignment/backuplib.php +++ wwassignment4/moodle/mod/wwassignment/backuplib.php @@ -0,0 +1,185 @@ +<?php //$Id$ + //This php script contains all the stuff to backup + //wwassignment mods + + //This is the "graphical" structure of the assignment mod: + // + // assignment + // (CL,pk->id) + // | + // | + // | + // assignment_submisions + // (UL,pk->id, fk->assignment,files) + // + // Meaning: pk->primary key field of the table + // fk->foreign key to link with parent + // nt->nested field (recursive data) + // CL->course level info + // UL->user level info + // files->table may have files) + // + //----------------------------------------------------------- + + //This function executes all the backup procedure about this mod + + + function wwassignment_backup_mods($bf,$preferences) { + error_log("wwassignment_backup_mods"); + //error_log("preferences ".print_r($preferences,true)); + global $CFG; + + $status = true; + + //Iterate over assignment table + $wwassignments = get_records ("wwassignment","course",$preferences->backup_course); + if ($wwassignments) { + foreach ($wwassignments as $wwassignment) { + //error_log("backing up wwassignment ".$wwassignment->id); + if (backup_mod_selected($preferences,'wwassignment',$wwassignment->id)) { + $status = wwassignment_backup_one_mod($bf,$preferences,$wwassignment); + // backup files happens in backup_one_mod now too. + } + } + } + + + + error_log("end wwassignment_backup_mods"); + return $status; + } + + function wwassignment_backup_one_mod($bf,$preferences,$wwassignment) { + error_log("wwassignment_backup_one_mod"); + //error_log("preferences ".print_r($preferences,true)); + global $CFG; + global $BaCkUpWWassignment_bridge ; // hack to back up wwassignment_bridge table just once + + if (is_numeric($wwassignment)) { + $wwassignment = get_record('wwassignment','id',$wwassignment); + } + + $status = true; +// function full_tag($tag,$level=0,$endline=true,$content,$attributes=null) { +// //Start mod + fwrite ($bf,start_tag("MOD",3,true)); + //Print wwassignment data + fwrite ($bf,full_tag("ID",4,false,$wwassignment->id)); + fwrite ($bf,full_tag("MODTYPE",4,false,"wwassignment")); + fwrite ($bf,full_tag("NAME",4,false,$wwassignment->name)); + fwrite ($bf,full_tag("DESCRIPTION",4,false,$wwassignment->description)); + fwrite ($bf,full_tag("WEBWORK_SET",4,false,$wwassignment->webwork_set)); + fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$wwassignment->timemodified)); + //if we've selected to backup users info, then execute backup_assignment_submisions and + //backup_assignment_files_instance + + //End mod + $status =fwrite ($bf,end_tag("MOD",3,true)); + + + error_log("end wwassignment_one_backup_mod"); + error_log("preferences ".print_r($preferences,true)); + if ($status and !$BaCkUpWWassignment_bridge) { + $BaCkUpWWassignment_bridge = 1 ; // don't back it up again + $wwassignment_bridge =get_record("wwassignment_bridge","course",$preferences->backup_course); + error_log("writing wwassignment_bridge"); + fwrite ($bf,start_tag("MOD",3,true)); + //Print wwassignment_bridge data + fwrite ($bf,full_tag("ID",4,false,"66666")); + fwrite ($bf,full_tag("MODTYPE",4,false,"wwassignment")); + fwrite ($bf,full_tag("WEBWORK_COURSE",4,false,$wwassignment_bridge->webwork_course)); + fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$wwassignment_bridge->timemodified)); + $status =fwrite ($bf,end_tag("MOD",3,true)); + error_log("wwassignment_bridge backed up"); + } + + + return $status; + } + + + //Return an array of info (name,value) + function wwassignment_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) { + //error_log("wwassignment_check_backup_mods "); + //error_log("user_data ".print_r($user_data,true) ); + //error_log("backup code ".print_r($backup_unique_code,true)); + //error_log("instance ".print_r($instance,true)); + $info=array(); + if (!empty($instances) && is_array($instances) && count($instances)) { + foreach ($instances as $id => $instance) { + $info += wwassignment_check_backup_mods_instances($instance,$backup_unique_code); + } + return $info; + } + //First the course data + $info[0][0] = get_string("modulenameplural","wwassignment"); + if ($ids = wwassignment_ids ($course)) { + $info[0][1] = count($ids); + } else { + $info[0][1] = 0; + } + + //error_log("end wwassignment_check_backup_mods"); + + return $info; + } + + //Return an array of info (name,value) + function wwassignment_check_backup_mods_instances($instance,$backup_unique_code) { + //error_log("wwassignment_check_backup_mods_instances "); + //error_log("content ".print_r($content,true)); + //error_log("preferences ".print_r($preferences,true)); + + $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>'; + $info[$instance->id.'0'][1] = ''; +// if (!empty($instance->userdata)) { +// $info[$instance->id.'1'][0] = get_string("submissions","assignment"); +// if ($ids = assignment_submission_ids_by_instance ($instance->id)) { +// $info[$instance->id.'1'][1] = count($ids); +// } else { +// $info[$instance->id.'1'][1] = 0; +// } +// } + //error_log("end wwassignment_check_backup_mods_instances"); + + return $info; + } +// +// //Return a content encoded to support interactivities linking. Every module +// //should have its own. They are called automatically from the backup procedure. +// function assignment_encode_content_links ($content,$preferences) { +// +// global $CFG; +// +// $base = preg_quote($CFG->wwwroot,"/"); +// +// //Link to the list of assignments +// $buscar="/(".$base."\/mod\/assignment\/index.php\?id\=)([0-9]+)/"; +// $result= preg_replace($buscar,'$@ASSIGNMENTINDEX*$2@$',$content); +// +// //Link to assignment view by moduleid +// $buscar="/(".$base."\/mod\/assignment\/view.php\?id\=)([0-9]+)/"; +// $result= preg_replace($buscar,'$@ASSIGNMENTVIEWBYID*$2@$',$result); +// +// return $result; +// } + + // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE + + //Returns an array of assignments id + function wwassignment_ids ($course) { + //error_log("wwassignment_ids "); + //error_log("course ".print_r($course,true)); + + global $CFG; + + + //error_log("end wwassignment_ids"); + + return get_records_sql ("SELECT a.id, a.course + FROM {$CFG->prefix}wwassignment a + WHERE a.course = '$course'"); + } + + +?> Index: upgrade.php =================================================================== RCS file: /webwork/cvs/system/wwmoodle/wwassignment4/moodle/mod/wwassignment/db/upgrade.php,v retrieving revision 1.3 retrieving revision 1.4 diff -Lwwassignment4/moodle/mod/wwassignment/db/upgrade.php -Lwwassignment4/moodle/mod/wwassignment/db/upgrade.php -u -r1.3 -r1.4 --- wwassignment4/moodle/mod/wwassignment/db/upgrade.php +++ wwassignment4/moodle/mod/wwassignment/db/upgrade.php @@ -36,11 +36,17 @@ notify("running 1.9 upgrade"); if ($result && $oldversion < 2008042072) { + + /// Define field grade to be added to wwassignment //$table = new XMLDBTable('wwassignment'); //$field = new XMLDBField('grade'); //$field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'webwork_set'); + // can't do this until you are connected to webwork -- which you won't be on initial upgrades + // add this either to wwlink block or to configuration + + /// Launch add field grade //$result = $result && add_field($table, $field); @@ -49,11 +55,18 @@ $field = new XMLDBField('timemodified'); $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'webwork_set'); - /// Launch add field timemodified + /// Launch add field timemodified to wwassignment_bridge $result = $result && add_field($table, $field); - // can't do this until you are connected to webwork -- which you won't be on initial upgrades - // add this either to wwlink block or to configuration + + /// Define field timemodified to be added to wwassignment + $table = new XMLDBTable('wwassignment_bridge'); + $field = new XMLDBField('timemodified'); + $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'webwork_course'); + + /// Launch add field timemodified to wwassignment_bridge + $result = $result && add_field($table, $field); + //notify('Processing assignment grades, this may take a while if there are many assignments...', 'notifysuccess'); // change grade typo to text if no grades MDL-13920 Index: install.xml =================================================================== RCS file: /webwork/cvs/system/wwmoodle/wwassignment4/moodle/mod/wwassignment/db/install.xml,v retrieving revision 1.1 retrieving revision 1.2 diff -Lwwassignment4/moodle/mod/wwassignment/db/install.xml -Lwwassignment4/moodle/mod/wwassignment/db/install.xml -u -r1.1 -r1.2 --- wwassignment4/moodle/mod/wwassignment/db/install.xml +++ wwassignment4/moodle/mod/wwassignment/db/install.xml @@ -18,8 +18,8 @@ <FIELDS> <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" ENUM="false" NEXT="course"/> <FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="webwork_course"/> - <FIELD NAME="webwork_course" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="course"/> + <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="webwork_set"/> </FIELDS> <KEYS> <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for data"/> |