MDL-10234
[moodle-linuxchix.git] / mod / assignment / restorelib.php
blob6c7648e5e4e1120117d8a4624a318b8bc6168bb1
1 <?php //$Id$
2 //This php script contains all the stuff to backup/restore
3 //assignment mods
5 //This is the "graphical" structure of the assignment mod:
6 //
7 // assignment
8 // (CL,pk->id)
9 // |
10 // |
11 // |
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) {
27 global $CFG;
29 $status = true;
31 //Get record from backup_ids
32 $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
34 if ($data) {
35 //Now get completed xmlized object
36 $info = $data->info;
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));
69 if ($scale) {
70 $assignment->grade = -($scale->new_id);
74 if (empty($assignment->assignmenttype)) { /// Pre 1.5 assignment
75 if ($assignment->type == 1) {
76 $assignment->assignmenttype = 'uploadsingle';
77 } else {
78 $assignment->assignmenttype = 'offline';
82 //The structure is equal to the db, so insert the assignment
83 $newid = insert_record ("assignment",$assignment);
85 //Do some output
86 if (!defined('RESTORE_SILENTLY')) {
87 echo "<li>".get_string("modulename","assignment")." \"".format_string(stripslashes($assignment->name),true)."\"</li>";
89 backup_flush(300);
91 if ($newid) {
92 //We have the newid, update backup_ids
93 backup_putid($restore->backup_unique_code,$mod->modtype,
94 $mod->id, $newid);
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);
100 } else {
101 $status = false;
103 } else {
104 $status = false;
107 return $status;
110 //This function restores the assignment_submissions
111 function assignment_submissions_restore_mods($old_assignment_id, $new_assignment_id,$info,$restore) {
113 global $CFG;
115 $status = true;
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']['#']);
142 } else {
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);
152 if ($user) {
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);
158 if ($user) {
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);
165 //Do some output
166 if (($i+1) % 50 == 0) {
167 if (!defined('RESTORE_SILENTLY')) {
168 echo ".";
169 if (($i+1) % 1000 == 0) {
170 echo "<br />";
173 backup_flush(300);
176 if ($newid) {
177 //We have the newid, update backup_ids
178 backup_putid($restore->backup_unique_code,"assignment_submission",$oldid,
179 $newid);
181 //Now copy moddata associated files
182 $status = assignment_restore_files ($old_assignment_id, $new_assignment_id,
183 $olduserid, $submission->userid, $restore);
185 } else {
186 $status = false;
190 return $status;
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) {
197 global $CFG;
199 $status = true;
200 $todo = false;
201 $moddata_path = "";
202 $assignment_path = "";
203 $temp_path = "";
205 //First, we check to "course_id" exists and create is as necessary
206 //in CFG->dataroot
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
217 if ($status) {
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
224 if ($status) {
225 $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
226 "/moddata/assignment/".$oldassid."/".$olduserid;
227 //Check it exists
228 if (is_dir($temp_path)) {
229 $todo = true;
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);
238 //Now this user id
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);
244 return $status;
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) {
253 global $CFG;
255 $result = $content;
257 //Link to the list of assignments
259 $searchstring='/\$@(ASSIGNMENTINDEX)\*([0-9]+)@\$/';
260 //We look for it
261 preg_match_all($searchstring,$content,$foundset);
262 //If found, then we are going to look for its new id (in backup tables)
263 if ($foundset[0]) {
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
272 if($rec->new_id) {
273 //Now replace it
274 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/assignment/index.php?id='.$rec->new_id,$result);
275 } else {
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]+)@\$/';
285 //We look for it
286 preg_match_all($searchstring,$result,$foundset);
287 //If found, then we are going to look for its new id (in backup tables)
288 if ($foundset[0]) {
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
297 if($rec->new_id) {
298 //Now replace it
299 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/assignment/view.php?id='.$rec->new_id,$result);
300 } else {
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);
307 return $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) {
316 global $CFG;
317 $status = true;
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) {
325 //Increment counter
326 $i++;
327 $content = $assignment->description;
328 $result = restore_decode_content_links_worker($content,$restore);
329 if ($result != $content) {
330 //Update record
331 $assignment->description = addslashes($result);
332 $status = update_record("assignment",$assignment);
333 if (debugging()) {
334 if (!defined('RESTORE_SILENTLY')) {
335 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
339 //Do some output
340 if (($i+1) % 5 == 0) {
341 if (!defined('RESTORE_SILENTLY')) {
342 echo ".";
343 if (($i+1) % 100 == 0) {
344 echo "<br />";
347 backup_flush(300);
351 return $status;
354 //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
355 //some texts in the module
356 function assignment_restore_wiki2markdown ($restore) {
358 global $CFG;
360 $status = true;
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
370 b.new_id = a.id")) {
371 foreach ($records as $record) {
372 //Rebuild wiki links
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));
379 //Do some output
380 $i++;
381 if (($i+1) % 1 == 0) {
382 if (!defined('RESTORE_SILENTLY')) {
383 echo ".";
384 if (($i+1) % 20 == 0) {
385 echo "<br />";
388 backup_flush(300);
393 return $status;
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) {
400 $status = false;
402 //Depending of the action, we recode different things
403 switch ($log->action) {
404 case "add":
405 if ($log->cmid) {
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);
408 if ($mod) {
409 $log->url = "view.php?id=".$log->cmid;
410 $log->info = $mod->new_id;
411 $status = true;
414 break;
415 case "update":
416 if ($log->cmid) {
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);
419 if ($mod) {
420 $log->url = "view.php?id=".$log->cmid;
421 $log->info = $mod->new_id;
422 $status = true;
425 break;
426 case "view":
427 if ($log->cmid) {
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);
430 if ($mod) {
431 $log->url = "view.php?id=".$log->cmid;
432 $log->info = $mod->new_id;
433 $status = true;
436 break;
437 case "view all":
438 $log->url = "index.php?id=".$log->course;
439 $status = true;
440 break;
441 case "upload":
442 if ($log->cmid) {
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);
445 if ($mod) {
446 $log->url = "view.php?a=".$mod->new_id;
447 $log->info = $mod->new_id;
448 $status = true;
451 break;
452 case "view submission":
453 if ($log->cmid) {
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);
456 if ($mod) {
457 $log->url = "submissions.php?id=".$mod->new_id;
458 $log->info = $mod->new_id;
459 $status = true;
462 break;
463 case "update grades":
464 if ($log->cmid) {
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);
469 if ($mod) {
470 $log->url = "submissions.php?id=".$mod->new_id;
471 $status = true;
474 break;
475 default:
476 if (!defined('RESTORE_SILENTLY')) {
477 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
479 break;
482 if ($status) {
483 $status = $log;
485 return $status;