2 //This php script contains all the stuff to backup/restore
5 //This is the "graphical" structure of the assignment mod:
12 // assignment_submisions
13 // (UL,pk->id, fk->assignment,files)
15 // Meaning: pk->primary key field of the table
16 // fk->foreign key to link with parent
17 // nt->nested field (recursive data)
18 // CL->course level info
19 // UL->user level info
20 // files->table may have files)
22 //-----------------------------------------------------------
24 //This function executes all the restore procedure about this mod
25 function assignment_restore_mods($mod,$restore) {
31 //Get record from backup_ids
32 $data = backup_getid($restore->backup_unique_code
,$mod->modtype
,$mod->id
);
35 //Now get completed xmlized object
37 //if necessary, write to restorelog and adjust date/time fields
38 if ($restore->course_startdateoffset
) {
39 restore_log_date_changes('Assignment', $restore, $info['MOD']['#'], array('TIMEDUE', 'TIMEAVAILABLE'));
41 //traverse_xmlize($info); //Debug
42 //print_object ($GLOBALS['traverse_array']); //Debug
43 //$GLOBALS['traverse_array']=""; //Debug
45 //Now, build the ASSIGNMENT record structure
46 $assignment->course
= $restore->course_id
;
47 $assignment->name
= backup_todb($info['MOD']['#']['NAME']['0']['#']);
48 $assignment->description
= backup_todb($info['MOD']['#']['DESCRIPTION']['0']['#']);
49 $assignment->format
= backup_todb($info['MOD']['#']['FORMAT']['0']['#']);
50 $assignment->resubmit
= backup_todb($info['MOD']['#']['RESUBMIT']['0']['#']);
51 $assignment->preventlate
= backup_todb($info['MOD']['#']['PREVENTLATE']['0']['#']);
52 $assignment->emailteachers
= backup_todb($info['MOD']['#']['EMAILTEACHERS']['0']['#']);
53 $assignment->var1
= backup_todb($info['MOD']['#']['VAR1']['0']['#']);
54 $assignment->var2
= backup_todb($info['MOD']['#']['VAR2']['0']['#']);
55 $assignment->var3
= backup_todb($info['MOD']['#']['VAR3']['0']['#']);
56 $assignment->var4
= backup_todb($info['MOD']['#']['VAR4']['0']['#']);
57 $assignment->var5
= backup_todb($info['MOD']['#']['VAR5']['0']['#']);
58 $assignment->type
= isset($info['MOD']['#']['TYPE']['0']['#'])?
backup_todb($info['MOD']['#']['TYPE']['0']['#']):'';
59 $assignment->assignmenttype
= backup_todb($info['MOD']['#']['ASSIGNMENTTYPE']['0']['#']);
60 $assignment->maxbytes
= backup_todb($info['MOD']['#']['MAXBYTES']['0']['#']);
61 $assignment->timedue
= backup_todb($info['MOD']['#']['TIMEDUE']['0']['#']);
62 $assignment->timeavailable
= backup_todb($info['MOD']['#']['TIMEAVAILABLE']['0']['#']);
63 $assignment->grade
= backup_todb($info['MOD']['#']['GRADE']['0']['#']);
64 $assignment->timemodified
= backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
66 //We have to recode the grade field if it is <0 (scale)
67 if ($assignment->grade
< 0) {
68 $scale = backup_getid($restore->backup_unique_code
,"scale",abs($assignment->grade
));
70 $assignment->grade
= -($scale->new_id
);
74 if (empty($assignment->assignmenttype
)) { /// Pre 1.5 assignment
75 if ($assignment->type
== 1) {
76 $assignment->assignmenttype
= 'uploadsingle';
78 $assignment->assignmenttype
= 'offline';
82 //The structure is equal to the db, so insert the assignment
83 $newid = insert_record ("assignment",$assignment);
86 if (!defined('RESTORE_SILENTLY')) {
87 echo "<li>".get_string("modulename","assignment")." \"".format_string(stripslashes($assignment->name
),true)."\"</li>";
92 //We have the newid, update backup_ids
93 backup_putid($restore->backup_unique_code
,$mod->modtype
,
95 //Now check if want to restore user data and do it.
96 if (restore_userdata_selected($restore,'assignment',$mod->id
)) {
97 //Restore assignmet_submissions
98 $status = assignment_submissions_restore_mods ($mod->id
, $newid,$info,$restore);
110 //This function restores the assignment_submissions
111 function assignment_submissions_restore_mods($old_assignment_id, $new_assignment_id,$info,$restore) {
117 //Get the submissions array
118 $submissions = $info['MOD']['#']['SUBMISSIONS']['0']['#']['SUBMISSION'];
120 //Iterate over submissions
121 for($i = 0; $i < sizeof($submissions); $i++
) {
122 $sub_info = $submissions[$i];
123 //traverse_xmlize($sub_info); //Debug
124 //print_object ($GLOBALS['traverse_array']); //Debug
125 //$GLOBALS['traverse_array']=""; //Debug
127 //We'll need this later!!
128 $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
129 $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
131 //Now, build the ASSIGNMENT_SUBMISSIONS record structure
132 $submission->assignment
= $new_assignment_id;
133 $submission->userid
= backup_todb($sub_info['#']['USERID']['0']['#']);
134 $submission->timecreated
= backup_todb($sub_info['#']['TIMECREATED']['0']['#']);
135 $submission->timemodified
= backup_todb($sub_info['#']['TIMEMODIFIED']['0']['#']);
136 $submission->numfiles
= backup_todb($sub_info['#']['NUMFILES']['0']['#']);
137 $submission->data1
= backup_todb($sub_info['#']['DATA1']['0']['#']);
138 $submission->data2
= backup_todb($sub_info['#']['DATA2']['0']['#']);
139 $submission->grade
= backup_todb($sub_info['#']['GRADE']['0']['#']);
140 if (isset($sub_info['#']['COMMENT']['0']['#'])) {
141 $submission->submissioncomment
= backup_todb($sub_info['#']['COMMENT']['0']['#']);
143 $submission->submissioncomment
= backup_todb($sub_info['#']['SUBMISSIONCOMMENT']['0']['#']);
145 $submission->format
= backup_todb($sub_info['#']['FORMAT']['0']['#']);
146 $submission->teacher
= backup_todb($sub_info['#']['TEACHER']['0']['#']);
147 $submission->timemarked
= backup_todb($sub_info['#']['TIMEMARKED']['0']['#']);
148 $submission->mailed
= backup_todb($sub_info['#']['MAILED']['0']['#']);
150 //We have to recode the userid field
151 $user = backup_getid($restore->backup_unique_code
,"user",$submission->userid
);
153 $submission->userid
= $user->new_id
;
156 //We have to recode the teacher field
157 $user = backup_getid($restore->backup_unique_code
,"user",$submission->teacher
);
159 $submission->teacher
= $user->new_id
;
162 //The structure is equal to the db, so insert the assignment_submission
163 $newid = insert_record ("assignment_submissions",$submission);
166 if (($i+
1) %
50 == 0) {
167 if (!defined('RESTORE_SILENTLY')) {
169 if (($i+
1) %
1000 == 0) {
177 //We have the newid, update backup_ids
178 backup_putid($restore->backup_unique_code
,"assignment_submission",$oldid,
181 //Now copy moddata associated files
182 $status = assignment_restore_files ($old_assignment_id, $new_assignment_id,
183 $olduserid, $submission->userid
, $restore);
193 //This function copies the assignment related info from backup temp dir to course moddata folder,
194 //creating it if needed and recoding everything (assignment id and user id)
195 function assignment_restore_files ($oldassid, $newassid, $olduserid, $newuserid, $restore) {
202 $assignment_path = "";
205 //First, we check to "course_id" exists and create is as necessary
207 $dest_dir = $CFG->dataroot
."/".$restore->course_id
;
208 $status = check_dir_exists($dest_dir,true);
210 //Now, locate course's moddata directory
211 $moddata_path = $CFG->dataroot
."/".$restore->course_id
."/".$CFG->moddata
;
213 //Check it exists and create it
214 $status = check_dir_exists($moddata_path,true);
216 //Now, locate assignment directory
218 $assignment_path = $moddata_path."/assignment";
219 //Check it exists and create it
220 $status = check_dir_exists($assignment_path,true);
223 //Now locate the temp dir we are gong to restore
225 $temp_path = $CFG->dataroot
."/temp/backup/".$restore->backup_unique_code
.
226 "/moddata/assignment/".$oldassid."/".$olduserid;
228 if (is_dir($temp_path)) {
233 //If todo, we create the neccesary dirs in course moddata/assignment
234 if ($status and $todo) {
235 //First this assignment id
236 $this_assignment_path = $assignment_path."/".$newassid;
237 $status = check_dir_exists($this_assignment_path,true);
239 $user_assignment_path = $this_assignment_path."/".$newuserid;
240 //And now, copy temp_path to user_assignment_path
241 $status = backup_copy_file($temp_path, $user_assignment_path);
247 //Return a content decoded to support interactivities linking. Every module
248 //should have its own. They are called automatically from
249 //assignment_decode_content_links_caller() function in each module
250 //in the restore process
251 function assignment_decode_content_links ($content,$restore) {
257 //Link to the list of assignments
259 $searchstring='/\$@(ASSIGNMENTINDEX)\*([0-9]+)@\$/';
261 preg_match_all($searchstring,$content,$foundset);
262 //If found, then we are going to look for its new id (in backup tables)
264 //print_object($foundset); //Debug
265 //Iterate over foundset[2]. They are the old_ids
266 foreach($foundset[2] as $old_id) {
267 //We get the needed variables here (course id)
268 $rec = backup_getid($restore->backup_unique_code
,"course",$old_id);
269 //Personalize the searchstring
270 $searchstring='/\$@(ASSIGNMENTINDEX)\*('.$old_id.')@\$/';
271 //If it is a link to this course, update the link to its new location
274 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/assignment/index.php?id='.$rec->new_id
,$result);
276 //It's a foreign link so leave it as original
277 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/assignment/index.php?id='.$old_id,$result);
282 //Link to assignment view by moduleid
284 $searchstring='/\$@(ASSIGNMENTVIEWBYID)\*([0-9]+)@\$/';
286 preg_match_all($searchstring,$result,$foundset);
287 //If found, then we are going to look for its new id (in backup tables)
289 //print_object($foundset); //Debug
290 //Iterate over foundset[2]. They are the old_ids
291 foreach($foundset[2] as $old_id) {
292 //We get the needed variables here (course_modules id)
293 $rec = backup_getid($restore->backup_unique_code
,"course_modules",$old_id);
294 //Personalize the searchstring
295 $searchstring='/\$@(ASSIGNMENTVIEWBYID)\*('.$old_id.')@\$/';
296 //If it is a link to this course, update the link to its new location
299 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/assignment/view.php?id='.$rec->new_id
,$result);
301 //It's a foreign link so leave it as original
302 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/assignment/view.php?id='.$old_id,$result);
310 //This function makes all the necessary calls to xxxx_decode_content_links()
311 //function in each module, passing them the desired contents to be decoded
312 //from backup format to destination site/course in order to mantain inter-activities
313 //working in the backup/restore process. It's called from restore_decode_content_links()
314 //function in restore process
315 function assignment_decode_content_links_caller($restore) {
319 if ($assignments = get_records_sql ("SELECT a.id, a.description
320 FROM {$CFG->prefix}assignment a
321 WHERE a.course = $restore->course_id")) {
322 //Iterate over each assignment->description
323 $i = 0; //Counter to send some output to the browser to avoid timeouts
324 foreach ($assignments as $assignment) {
327 $content = $assignment->description
;
328 $result = restore_decode_content_links_worker($content,$restore);
329 if ($result != $content) {
331 $assignment->description
= addslashes($result);
332 $status = update_record("assignment",$assignment);
334 if (!defined('RESTORE_SILENTLY')) {
335 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
340 if (($i+
1) %
5 == 0) {
341 if (!defined('RESTORE_SILENTLY')) {
343 if (($i+
1) %
100 == 0) {
354 //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
355 //some texts in the module
356 function assignment_restore_wiki2markdown ($restore) {
362 //Convert assignment->description
363 if ($records = get_records_sql ("SELECT a.id, a.description, a.format
364 FROM {$CFG->prefix}assignment a,
365 {$CFG->prefix}backup_ids b
366 WHERE a.course = $restore->course_id AND
367 a.format = ".FORMAT_WIKI
. " AND
368 b.backup_code = $restore->backup_unique_code AND
369 b.table_name = 'assignment' AND
371 foreach ($records as $record) {
373 $record->description
= restore_decode_wiki_content($record->description
, $restore);
374 //Convert to Markdown
375 $wtm = new WikiToMarkdown();
376 $record->description
= $wtm->convert($record->description
, $restore->course_id
);
377 $record->format
= FORMAT_MARKDOWN
;
378 $status = update_record('assignment', addslashes_object($record));
381 if (($i+
1) %
1 == 0) {
382 if (!defined('RESTORE_SILENTLY')) {
384 if (($i+
1) %
20 == 0) {
396 //This function returns a log record with all the necessay transformations
397 //done. It's used by restore_log_module() to restore modules log.
398 function assignment_restore_logs($restore,$log) {
402 //Depending of the action, we recode different things
403 switch ($log->action
) {
406 //Get the new_id of the module (to recode the info field)
407 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
409 $log->url
= "view.php?id=".$log->cmid
;
410 $log->info
= $mod->new_id
;
417 //Get the new_id of the module (to recode the info field)
418 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
420 $log->url
= "view.php?id=".$log->cmid
;
421 $log->info
= $mod->new_id
;
428 //Get the new_id of the module (to recode the info field)
429 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
431 $log->url
= "view.php?id=".$log->cmid
;
432 $log->info
= $mod->new_id
;
438 $log->url
= "index.php?id=".$log->course
;
443 //Get the new_id of the module (to recode the info field)
444 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
446 $log->url
= "view.php?a=".$mod->new_id
;
447 $log->info
= $mod->new_id
;
452 case "view submission":
454 //Get the new_id of the module (to recode the info field)
455 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
457 $log->url
= "submissions.php?id=".$mod->new_id
;
458 $log->info
= $mod->new_id
;
463 case "update grades":
465 //Extract the assignment id from the url field
466 $assid = substr(strrchr($log->url
,"="),1);
467 //Get the new_id of the module (to recode the info field)
468 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$assid);
470 $log->url
= "submissions.php?id=".$mod->new_id
;
476 if (!defined('RESTORE_SILENTLY')) {
477 echo "action (".$log->module
."-".$log->action
.") unknown. Not restored<br />"; //Debug