MDL-14945
[moodle-linuxchix.git] / mod / workshop / restorelib.php
blobbc9a12bc3b19aa60eaf6b570501b6418b6608a01
1 <?php //$Id$
2 //This php script contains all the stuff to backup/restore
3 //workshop mods
5 //This is the "graphical" structure of the workshop mod:
6 //
7 // workshop
8 // (CL,pk->id)
9 // |
10 // |
11 // |
12 // |------------------------------|-----------------------------------------------------|
13 // | |
14 // | |
15 // | |
16 // | workshop_submissions
17 // | (UL,pk->id,fk->workshopid,files)
18 // | |
19 // | |-------------------------------------| |----------------------| |
20 // | | | | | |
21 // workshop_elements workshop_grades workshop_assessments
22 // (CL,pk->id,fk->workshopid) (UL,pk->id,fk->assessmentid) (UL,pk->id,fk->submissionid)
23 // | | ( fk->elementno ) |
24 // | | |
25 // | | |
26 // workshop_rubrics workshop_stockcomments workshop_comments
27 // (CL,pk->id,fk->elementno) (CL, pk->id, fk->elementno) (UL,pk->id,fk->assessmentid)
29 // Meaning: pk->primary key field of the table
30 // fk->foreign key to link with parent
31 // nt->nested field (recursive data)
32 // CL->course level info
33 // UL->user level info
34 // files->table may have files)
36 //-----------------------------------------------------------
38 //This function executes all the restore procedure about this mod
39 function workshop_restore_mods($mod,$restore) {
41 global $CFG;
43 $status = true;
45 //Get record from backup_ids
46 $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
48 if ($data) {
49 //Now get completed xmlized object
50 $info = $data->info;
51 //if necessary, write to restorelog and adjust date/time fields
52 if ($restore->course_startdateoffset) {
53 restore_log_date_changes('Workshop', $restore, $info['MOD']['#'], array('SUBMISSIONSTART','ASSESSMENTSTART', 'SUBMISSIONEND', 'ASSESSMENTEND', 'RELEASEGRADES'));
55 //traverse_xmlize($info); //Debug
56 //print_object ($GLOBALS['traverse_array']); //Debug
57 //$GLOBALS['traverse_array']=""; //Debug
59 //Now, build the WORKSHOP record structure
60 $workshop->course = $restore->course_id;
61 $workshop->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
62 $workshop->description = backup_todb($info['MOD']['#']['DESCRIPTION']['0']['#']);
63 $workshop->wtype = backup_todb($info['MOD']['#']['WTYPE']['0']['#']);
64 $workshop->nelements = backup_todb($info['MOD']['#']['NELEMENTS']['0']['#']);
65 $workshop->nattachments = backup_todb($info['MOD']['#']['NATTACHMENTS']['0']['#']);
66 $workshop->phase = backup_todb($info['MOD']['#']['PHASE']['0']['#']);
67 $workshop->format = backup_todb($info['MOD']['#']['FORMAT']['0']['#']);
68 $workshop->gradingstrategy = backup_todb($info['MOD']['#']['GRADINGSTRATEGY']['0']['#']);
69 $workshop->resubmit = backup_todb($info['MOD']['#']['RESUBMIT']['0']['#']);
70 $workshop->agreeassessments = backup_todb($info['MOD']['#']['AGREEASSESSMENTS']['0']['#']);
71 $workshop->hidegrades = backup_todb($info['MOD']['#']['HIDEGRADES']['0']['#']);
72 $workshop->anonymous = backup_todb($info['MOD']['#']['ANONYMOUS']['0']['#']);
73 $workshop->includeself = backup_todb($info['MOD']['#']['INCLUDESELF']['0']['#']);
74 $workshop->maxbytes = backup_todb($info['MOD']['#']['MAXBYTES']['0']['#']);
75 $workshop->submissionstart = backup_todb($info['MOD']['#']['SUBMISSIONSTART']['0']['#']);
76 $workshop->assessmentstart = backup_todb($info['MOD']['#']['ASSESSMENTSTART']['0']['#']);
77 $workshop->deadline = backup_todb($info['MOD']['#']['DEADLINE']['0']['#']);
78 $workshop->submissionend = backup_todb($info['MOD']['#']['SUBMISSIONEND']['0']['#']);
79 $workshop->assessmentend = backup_todb($info['MOD']['#']['ASSESSMENTEND']['0']['#']);
80 $workshop->releasegrades = backup_todb($info['MOD']['#']['RELEASEGRADES']['0']['#']);
81 $workshop->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
82 $workshop->gradinggrade = backup_todb($info['MOD']['#']['GRADINGGRADE']['0']['#']);
83 $workshop->ntassessments = backup_todb($info['MOD']['#']['NTASSESSMENTS']['0']['#']);
84 $workshop->assessmentcomps = backup_todb($info['MOD']['#']['ASSESSMENTCOMPS']['0']['#']);
85 $workshop->nsassessments = backup_todb($info['MOD']['#']['NSASSESSMENTS']['0']['#']);
86 $workshop->overallocation = backup_todb($info['MOD']['#']['OVERALLOCATION']['0']['#']);
87 $workshop->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
88 $workshop->teacherweight = backup_todb($info['MOD']['#']['TEACHERWEIGHT']['0']['#']);
89 $workshop->showleaguetable = backup_todb($info['MOD']['#']['SHOWLEAGUETABLE']['0']['#']);
90 $workshop->usepassword = backup_todb($info['MOD']['#']['USEPASSWORD']['0']['#']);
91 $workshop->password = backup_todb($info['MOD']['#']['PASSWORD']['0']['#']);
93 //If we have retrieved workshop->phase, it's a pre 1.5 backup, so we have to do
94 //some conversions before inserting to DB. Upwards compatibility :-)
95 if ( isset($info['MOD']['#']['PHASE']['0']['#'])) { //It's a pre-15 backup file
97 //Adjust the wtype field (mimetised from the upgrade script)
98 $workshop->wtype = 0;
99 if ($workshop->includeself || $workshop->ntassessments) {
100 $workshop->wtype = 1; // 3 phases with grading grades
101 } else if ($workshop->nsassessments) {
102 $workshop->wtype = 2; // 5 phases with grading grades
105 //Now, adjust phases time limits (mimetised from the upgrade script too)
106 $early = 0;
107 $late = 0;
108 $now = time();
109 if ($now < $workshop->deadline) {
110 $late = $workshop->deadline;
111 } else {
112 $early = $workshop->deadline;
114 if ($workshop->phase > 1) {
115 $workshop->submissionstart = $early;
116 } else {
117 $workshop->submissionstart = $late;
119 if ($workshop->phase > 2) {
120 $workshop->assessmentstart = $early;
121 } else {
122 $workshop->assessmentstart = $late;
124 if ($workshop->phase > 3) {
125 $workshop->submissionend = $early;
126 } else {
127 $workshop->submissionend = $late;
129 if ($workshop->phase > 4) {
130 $workshop->assessmentend = $early;
131 } else {
132 $workshop->assessmentend = $late;
134 if ($workshop->phase > 5) {
135 $workshop->releasegrades = $now;
136 } else {
137 $workshop->releasegrades = $now + (4 * 7 * 24 * 60 * 60); //Grades will be available in 4 weeks
141 //The structure is equal to the db, so insert the workshop
142 $newid = insert_record ("workshop",$workshop);
144 //Do some output
145 if (!defined('RESTORE_SILENTLY')) {
146 echo "<li>".get_string("modulename","workshop")." \"".format_string(stripslashes($workshop->name),true)."\"</li>";
148 backup_flush(300);
150 if ($newid) {
151 //We have the newid, update backup_ids
152 backup_putid($restore->backup_unique_code,$mod->modtype,
153 $mod->id, $newid);
154 //We have to restore the workshop_elements table now (course level table)
155 $status = workshop_elements_restore_mods($newid,$info,$restore);
156 //Now check if want to restore user data and do it.
157 if (restore_userdata_selected($restore,'workshop',$mod->id)) {
158 //Restore workshop_submissions
159 $status = workshop_submissions_restore_mods ($mod->id, $newid,$info,$restore);
161 } else {
162 $status = false;
164 } else {
165 $status = false;
168 return $status;
171 //This function restores the workshop_elements
172 function workshop_elements_restore_mods($workshop_id,$info,$restore) {
174 global $CFG;
176 $status = true;
178 //Get the workshop_elements array
179 $elements = $info['MOD']['#']['ELEMENTS']['0']['#']['ELEMENT'];
181 //Iterate over workshop_elements
182 for($i = 0; $i < sizeof($elements); $i++) {
183 $ele_info = $elements[$i];
184 //traverse_xmlize($ele_info); //Debug
185 //print_object ($GLOBALS['traverse_array']); //Debug
186 //$GLOBALS['traverse_array']=""; //Debug
188 //Now, build the WORKSHOP_ELEMENTS record structure
189 $element->workshopid = $workshop_id;
190 $element->elementno = backup_todb($ele_info['#']['ELEMENTNO']['0']['#']);
191 $element->description = backup_todb($ele_info['#']['DESCRIPTION']['0']['#']);
192 $element->scale = backup_todb($ele_info['#']['SCALE']['0']['#']);
193 $element->maxscore = backup_todb($ele_info['#']['MAXSCORE']['0']['#']);
194 $element->weight = backup_todb($ele_info['#']['WEIGHT']['0']['#']);
195 $element->stddev = backup_todb($ele_info['#']['STDDEV']['0']['#']);
196 $element->totalassessments = backup_todb($ele_info['#']['TOTALASSESSMENTS']['0']['#']);
198 //The structure is equal to the db, so insert the workshop_elements
199 $newid = insert_record ("workshop_elements",$element);
201 //Do some output
202 if (($i+1) % 10 == 0) {
203 if (!defined('RESTORE_SILENTLY')) {
204 echo ".";
205 if (($i+1) % 200 == 0) {
206 echo "<br />";
209 backup_flush(300);
212 if ($newid) {
213 //We have to restore the workshop_rubrics table now (course level table)
214 $status = workshop_rubrics_restore_mods($workshop_id,$element->elementno,$ele_info,$restore);
215 //We have to restore the workshop_stockcomment table now (course level table)
216 $status = workshop_stockcomments_restore_mods($workshop_id,$element->elementno,$ele_info,$restore);
217 } else {
218 $status = false;
222 return $status;
226 //This function restores the workshop_rubrics
227 function workshop_rubrics_restore_mods($workshop_id,$elementno,$info,$restore) {
229 global $CFG;
231 $status = true;
233 //Get the workshop_rubrics array (optional)
234 if (isset($info['#']['RUBRICS']['0']['#']['RUBRIC'])) {
235 $rubrics = $info['#']['RUBRICS']['0']['#']['RUBRIC'];
237 //Iterate over workshop_rubrics
238 for($i = 0; $i < sizeof($rubrics); $i++) {
239 $rub_info = $rubrics[$i];
240 //traverse_xmlize($rub_info); //Debug
241 //print_object ($GLOBALS['traverse_array']); //Debug
242 //$GLOBALS['traverse_array']=""; //Debug
244 //Now, build the WORKSHOP_RUBRICS record structure
245 $rubric->workshopid = $workshop_id;
246 $rubric->elementno = $elementno;
247 $rubric->rubricno = backup_todb($rub_info['#']['RUBRICNO']['0']['#']);
248 $rubric->description = backup_todb($rub_info['#']['DESCRIPTION']['0']['#']);
250 //The structure is equal to the db, so insert the workshop_rubrics
251 $newid = insert_record ("workshop_rubrics",$rubric);
253 //Do some output
254 if (($i+1) % 10 == 0) {
255 if (!defined('RESTORE_SILENTLY')) {
256 echo ".";
257 if (($i+1) % 200 == 0) {
258 echo "<br />";
261 backup_flush(300);
264 if (!$newid) {
265 $status = false;
269 return $status;
273 //This function restores the workshop_stockcomments
274 function workshop_stockcomments_restore_mods($new_workshop_id, $elementno, $info, $restore) {
276 global $CFG;
278 $status = true;
280 //Get the stockcomments array (optional)
281 if (isset($info['#']['STOCKCOMMENTS']['0']['#']['STOCKCOMMENT'])) {
282 $stockcomments = $info['#']['STOCKCOMMENTS']['0']['#']['STOCKCOMMENT'];
284 //Iterate over stock comments
285 for($i = 0; $i < sizeof($stockcomments); $i++) {
286 $com_info = $stockcomments[$i];
287 //traverse_xmlize($com_info); //Debug
288 //print_object ($GLOBALS['traverse_array']); //Debug
289 //$GLOBALS['traverse_array']=""; //Debug
291 //Now, build the WORKSHOP_STOCKCOMMENTS record structure
292 $stockcomment->workshopid = $new_workshop_id;
293 $stockcomment->elementno = $elementno;
294 $stockcomment->comments = backup_todb($com_info['#']['COMMENT_TEXT']['0']['#']);
296 //The structure is equal to the db, so insert the workshop_comment
297 $newid = insert_record ("workshop_stockcomments",$stockcomment);
299 //Do some output
300 if (($i+1) % 50 == 0) {
301 if (!defined('RESTORE_SILENTLY')) {
302 echo ".";
303 if (($i+1) % 1000 == 0) {
304 echo "<br />";
307 backup_flush(300);
310 if (!$newid) {
311 $status = false;
316 return $status;
319 //This function restores the workshop_submissions
320 function workshop_submissions_restore_mods($old_workshop_id, $new_workshop_id,$info,$restore) {
322 global $CFG;
324 $status = true;
326 //Get the submissions array
327 $submissions = $info['MOD']['#']['SUBMISSIONS']['0']['#']['SUBMISSION'];
329 //Iterate over submissions
330 for($i = 0; $i < sizeof($submissions); $i++) {
331 $sub_info = $submissions[$i];
332 //traverse_xmlize($sub_info); //Debug
333 //print_object ($GLOBALS['traverse_array']); //Debug
334 //$GLOBALS['traverse_array']=""; //Debug
336 //We'll need this later!!
337 $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
338 $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
340 //Now, build the WORKSHOP_SUBMISSIONS record structure
341 $submission->workshopid = $new_workshop_id;
342 $submission->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
343 $submission->title = backup_todb($sub_info['#']['TITLE']['0']['#']);
344 $submission->timecreated = backup_todb($sub_info['#']['TIMECREATED']['0']['#']);
345 $submission->timecreated += $restore->course_startdateoffset;
346 $submission->mailed = backup_todb($sub_info['#']['MAILED']['0']['#']);
347 $submission->description = backup_todb($sub_info['#']['DESCRIPTION']['0']['#']);
348 $submission->gradinggrade = backup_todb($sub_info['#']['GRADINGGRADE']['0']['#']);
349 $submission->finalgrade = backup_todb($sub_info['#']['FINALGRADE']['0']['#']);
350 $submission->late = backup_todb($sub_info['#']['LATE']['0']['#']);
351 $submission->nassessments = backup_todb($sub_info['#']['NASSESSMENTS']['0']['#']);
353 //We have to recode the userid field
354 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
355 if ($user) {
356 $submission->userid = $user->new_id;
359 //The structure is equal to the db, so insert the workshop_submission
360 $newid = insert_record ("workshop_submissions",$submission);
362 //Do some output
363 if (($i+1) % 50 == 0) {
364 if (!defined('RESTORE_SILENTLY')) {
365 echo ".";
366 if (($i+1) % 1000 == 0) {
367 echo "<br />";
370 backup_flush(300);
373 if ($newid) {
374 //We have the newid, update backup_ids
375 backup_putid($restore->backup_unique_code,"workshop_submissions",$oldid,
376 $newid);
378 //Now copy moddata associated files
379 $status = workshop_restore_files ($oldid, $newid,$restore);
380 //Now we need to restore workshop_assessments (user level table)
381 if ($status) {
382 $status = workshop_assessments_restore_mods ($new_workshop_id, $newid,$sub_info,$restore);
384 } else {
385 $status = false;
389 return $status;
392 //This function restores the workshop_assessments
393 function workshop_assessments_restore_mods($new_workshop_id, $new_submission_id,$info,$restore) {
395 global $CFG;
397 $status = true;
399 //Get the assessments array (if any)
400 if (isset($info['#']['ASSESSMENTS']['0']['#']['ASSESSMENT'])) {
401 $assessments = $info['#']['ASSESSMENTS']['0']['#']['ASSESSMENT'];
403 //Iterate over assessments
404 for($i = 0; $i < sizeof($assessments); $i++) {
405 $ass_info = $assessments[$i];
406 //traverse_xmlize($ass_info); //Debug
407 //print_object ($GLOBALS['traverse_array']); //Debug
408 //$GLOBALS['traverse_array']=""; //Debug
410 //We'll need this later!!
411 $oldid = backup_todb($ass_info['#']['ID']['0']['#']);
412 $olduserid = backup_todb($ass_info['#']['USERID']['0']['#']);
414 //Now, build the WORKSHOP_ASSESSMENTS record structure
415 $assessment->workshopid = $new_workshop_id;
416 $assessment->submissionid = $new_submission_id;
417 $assessment->userid = backup_todb($ass_info['#']['USERID']['0']['#']);
418 $assessment->timecreated = backup_todb($ass_info['#']['TIMECREATED']['0']['#']);
419 $assessment->timegraded = backup_todb($ass_info['#']['TIMEGRADED']['0']['#']);
420 $assessment->timeagreed = backup_todb($ass_info['#']['TIMEAGREED']['0']['#']);
421 $assessment->grade = backup_todb($ass_info['#']['GRADE']['0']['#']);
422 $assessment->gradinggrade = backup_todb($ass_info['#']['GRADINGGRADE']['0']['#']);
423 $assessment->mailed = backup_todb($ass_info['#']['MAILED']['0']['#']);
424 $assessment->resubmission = backup_todb($ass_info['#']['RESUBMISSION']['0']['#']);
425 $assessment->donotuse = backup_todb($ass_info['#']['DONOTUSE']['0']['#']);
426 $assessment->generalcomment = backup_todb($ass_info['#']['GENERALCOMMENT']['0']['#']);
427 $assessment->teachercomment = backup_todb($ass_info['#']['TEACHERCOMMENT']['0']['#']);
429 //We have to recode the userid field
430 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
431 if ($user) {
432 $assessment->userid = $user->new_id;
435 //The structure is equal to the db, so insert the workshop_assessment
436 $newid = insert_record ("workshop_assessments",$assessment);
438 //Do some output
439 if (($i+1) % 50 == 0) {
440 if (!defined('RESTORE_SILENTLY')) {
441 echo ".";
442 if (($i+1) % 1000 == 0) {
443 echo "<br />";
446 backup_flush(300);
449 if ($newid) {
450 //We have the newid, update backup_ids
451 backup_putid($restore->backup_unique_code,"workshop_assessments",$oldid,
452 $newid);
454 //Now we need to restore workshop_comments (user level table)
455 if ($status) {
456 $status = workshop_comments_restore_mods ($new_workshop_id, $newid,$ass_info,$restore);
458 //Now we need to restore workshop_grades (user level table)
459 if ($status) {
460 $status = workshop_grades_restore_mods ($new_workshop_id, $newid,$ass_info,$restore);
462 } else {
463 $status = false;
468 return $status;
471 //This function restores the workshop_comments
472 function workshop_comments_restore_mods($new_workshop_id, $new_assessment_id,$info,$restore) {
474 global $CFG;
476 $status = true;
478 //Get the comments array (optional)
479 if (isset($info['#']['COMMENTS']['0']['#']['COMMENT'])) {
480 $comments = $info['#']['COMMENTS']['0']['#']['COMMENT'];
482 //Iterate over comments
483 for($i = 0; $i < sizeof($comments); $i++) {
484 $com_info = $comments[$i];
485 //traverse_xmlize($com_info); //Debug
486 //print_object ($GLOBALS['traverse_array']); //Debug
487 //$GLOBALS['traverse_array']=""; //Debug
489 //We'll need this later!!
490 $olduserid = backup_todb($com_info['#']['USERID']['0']['#']);
492 //Now, build the WORKSHOP_COMMENTS record structure
493 $comment->workshopid = $new_workshop_id;
494 $comment->assessmentid = $new_assessment_id;
495 $comment->userid = backup_todb($com_info['#']['USERID']['0']['#']);
496 $comment->timecreated = backup_todb($com_info['#']['TIMECREATED']['0']['#']);
497 $comment->mailed = backup_todb($com_info['#']['MAILED']['0']['#']);
498 $comment->comments = backup_todb($com_info['#']['COMMENT_TEXT']['0']['#']);
500 //We have to recode the userid field
501 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
502 if ($user) {
503 $comment->userid = $user->new_id;
506 //The structure is equal to the db, so insert the workshop_comment
507 $newid = insert_record ("workshop_comments",$comment);
509 //Do some output
510 if (($i+1) % 50 == 0) {
511 if (!defined('RESTORE_SILENTLY')) {
512 echo ".";
513 if (($i+1) % 1000 == 0) {
514 echo "<br />";
517 backup_flush(300);
520 if (!$newid) {
521 $status = false;
526 return $status;
529 //This function restores the workshop_grades
530 function workshop_grades_restore_mods($new_workshop_id, $new_assessment_id,$info,$restore) {
532 global $CFG;
534 $status = true;
536 //Get the grades array (optional)
537 if (isset($info['#']['GRADES']['0']['#']['GRADE'])) {
538 $grades = $info['#']['GRADES']['0']['#']['GRADE'];
540 //Iterate over grades
541 for($i = 0; $i < sizeof($grades); $i++) {
542 $gra_info = $grades[$i];
543 //traverse_xmlize($gra_info); //Debug
544 //print_object ($GLOBALS['traverse_array']); //Debug
545 //$GLOBALS['traverse_array']=""; //Debug
547 //Now, build the WORKSHOP_GRADES record structure
548 $grade->workshopid = $new_workshop_id;
549 $grade->assessmentid = $new_assessment_id;
550 $grade->elementno = backup_todb($gra_info['#']['ELEMENTNO']['0']['#']);
551 $grade->feedback = backup_todb($gra_info['#']['FEEDBACK']['0']['#']);
552 $grade->grade = backup_todb($gra_info['#']['GRADE_VALUE']['0']['#']);
554 //The structure is equal to the db, so insert the workshop_grade
555 $newid = insert_record ("workshop_grades",$grade);
557 //Do some output
558 if (($i+1) % 50 == 0) {
559 if (!defined('RESTORE_SILENTLY')) {
560 echo ".";
561 if (($i+1) % 1000 == 0) {
562 echo "<br />";
565 backup_flush(300);
568 if (!$newid) {
569 $status = false;
574 return $status;
577 //This function copies the workshop related info from backup temp dir to course moddata folder,
578 //creating it if needed and recoding everything (submission_id)
579 function workshop_restore_files ($oldsubmiss, $newsubmiss, $restore) {
581 global $CFG;
583 $status = true;
584 $todo = false;
585 $moddata_path = "";
586 $workshop_path = "";
587 $temp_path = "";
589 //First, we check to "course_id" exists and create is as necessary
590 //in CFG->dataroot
591 $dest_dir = $CFG->dataroot."/".$restore->course_id;
592 $status = check_dir_exists($dest_dir,true);
594 //Now, locate course's moddata directory
595 $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
597 //Check it exists and create it
598 $status = check_dir_exists($moddata_path,true);
600 //Now, locate workshop directory
601 if ($status) {
602 $workshop_path = $moddata_path."/workshop";
603 //Check it exists and create it
604 $status = check_dir_exists($workshop_path,true);
607 //Now locate the temp dir we are gong to restore
608 if ($status) {
609 $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
610 "/moddata/workshop/".$oldsubmiss;
611 //Check it exists
612 if (is_dir($temp_path)) {
613 $todo = true;
617 //If todo, we create the neccesary dirs in course moddata/workshop
618 if ($status and $todo) {
619 //First this workshop id
620 $this_workshop_path = $workshop_path."/".$newsubmiss;
621 $status = check_dir_exists($this_workshop_path,true);
622 //And now, copy temp_path to this_workshop_path
623 $status = backup_copy_file($temp_path, $this_workshop_path);
626 return $status;
629 //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
630 //some texts in the module
631 function workshop_restore_wiki2markdown ($restore) {
633 global $CFG;
635 $status = true;
637 //Convert workshop->description
638 if ($records = get_records_sql ("SELECT w.id, w.description, w.format
639 FROM {$CFG->prefix}workshop w,
640 {$CFG->prefix}backup_ids b
641 WHERE w.course = $restore->course_id AND
642 format = ".FORMAT_WIKI. " AND
643 b.backup_code = $restore->backup_unique_code AND
644 b.table_name = 'workshop' AND
645 b.new_id = w.id")) {
646 foreach ($records as $record) {
647 //Rebuild wiki links
648 $record->description = restore_decode_wiki_content($record->description, $restore);
649 //Convert to Markdown
650 $wtm = new WikiToMarkdown();
651 $record->description = $wtm->convert($record->description, $restore->course_id);
652 $record->format = FORMAT_MARKDOWN;
653 $status = update_record('workshop', addslashes_object($record));
654 //Do some output
655 $i++;
656 if (($i+1) % 1 == 0) {
657 if (!defined('RESTORE_SILENTLY')) {
658 echo ".";
659 if (($i+1) % 20 == 0) {
660 echo "<br />";
663 backup_flush(300);
668 return $status;
671 //Return a content decoded to support interactivities linking. Every module
672 //should have its own. They are called automatically from
673 //workshop_decode_content_links_caller() function in each module
674 //in the restore process
675 function workshop_decode_content_links ($content,$restore) {
677 global $CFG;
679 $result = $content;
681 //Link to the list of workshops
683 $searchstring='/\$@(WORKSHOPINDEX)\*([0-9]+)@\$/';
684 //We look for it
685 preg_match_all($searchstring,$content,$foundset);
686 //If found, then we are going to look for its new id (in backup tables)
687 if ($foundset[0]) {
688 //print_object($foundset); //Debug
689 //Iterate over foundset[2]. They are the old_ids
690 foreach($foundset[2] as $old_id) {
691 //We get the needed variables here (course id)
692 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
693 //Personalize the searchstring
694 $searchstring='/\$@(WORKSHOPINDEX)\*('.$old_id.')@\$/';
695 //If it is a link to this course, update the link to its new location
696 if($rec->new_id) {
697 //Now replace it
698 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/workshop/index.php?id='.$rec->new_id,$result);
699 } else {
700 //It's a foreign link so leave it as original
701 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/workshop/index.php?id='.$old_id,$result);
706 //Link to workshop view by moduleid
708 $searchstring='/\$@(WORKSHOPVIEWBYID)\*([0-9]+)@\$/';
709 //We look for it
710 preg_match_all($searchstring,$result,$foundset);
711 //If found, then we are going to look for its new id (in backup tables)
712 if ($foundset[0]) {
713 //print_object($foundset); //Debug
714 //Iterate over foundset[2]. They are the old_ids
715 foreach($foundset[2] as $old_id) {
716 //We get the needed variables here (course_modules id)
717 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
718 //Personalize the searchstring
719 $searchstring='/\$@(WORKSHOPVIEWBYID)\*('.$old_id.')@\$/';
720 //If it is a link to this course, update the link to its new location
721 if($rec->new_id) {
722 //Now replace it
723 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/workshop/view.php?id='.$rec->new_id,$result);
724 } else {
725 //It's a foreign link so leave it as original
726 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/workshop/view.php?id='.$old_id,$result);
731 return $result;
734 //This function makes all the necessary calls to xxxx_decode_content_links()
735 //function in each module, passing them the desired contents to be decoded
736 //from backup format to destination site/course in order to mantain inter-activities
737 //working in the backup/restore process. It's called from restore_decode_content_links()
738 //function in restore process
739 function workshop_decode_content_links_caller($restore) {
740 global $CFG;
741 $status = true;
743 //Process every WORKSHOP (description) in the course
744 if ($workshops = get_records_sql ("SELECT w.id, w.description
745 FROM {$CFG->prefix}workshop w
746 WHERE w.course = $restore->course_id")) {
747 //Iterate over each workshop->description
748 $i = 0; //Counter to send some output to the browser to avoid timeouts
749 foreach ($workshops as $workshop) {
750 //Increment counter
751 $i++;
752 $content = $workshop->description;
753 $result = restore_decode_content_links_worker($content,$restore);
754 if ($result != $content) {
755 //Update record
756 $workshop->description = addslashes($result);
757 $status = update_record("workshop",$workshop);
758 if (debugging()) {
759 if (!defined('RESTORE_SILENTLY')) {
760 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
764 //Do some output
765 if (($i+1) % 5 == 0) {
766 if (!defined('RESTORE_SILENTLY')) {
767 echo ".";
768 if (($i+1) % 100 == 0) {
769 echo "<br />";
772 backup_flush(300);
777 return $status;
780 //This function returns a log record with all the necessay transformations
781 //done. It's used by restore_log_module() to restore modules log.
782 function workshop_restore_logs($restore,$log) {
784 $status = false;
786 //Depending of the action, we recode different things
787 switch ($log->action) {
788 case "add":
789 if ($log->cmid) {
790 //Get the new_id of the module (to recode the info field)
791 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
792 if ($mod) {
793 $log->url = "view.php?id=".$log->cmid;
794 $log->info = $mod->new_id;
795 $status = true;
798 break;
799 case "update":
800 if ($log->cmid) {
801 //Get the new_id of the module (to recode the info field)
802 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
803 if ($mod) {
804 $log->url = "view.php?id=".$log->cmid;
805 $log->info = $mod->new_id;
806 $status = true;
809 break;
810 case "view":
811 if ($log->cmid) {
812 //Get the new_id of the module (to recode the info field)
813 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
814 if ($mod) {
815 $log->url = "view.php?id=".$log->cmid;
816 $log->info = $mod->new_id;
817 $status = true;
820 break;
821 case "view all":
822 $log->url = "index.php?id=".$log->course;
823 $status = true;
824 break;
825 case "submit":
826 if ($log->cmid) {
827 //Get the new_id of the module (to recode the info field)
828 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
829 if ($mod) {
830 $log->url = "view.php?id=".$log->cmid;
831 $log->info = $mod->new_id;
832 $status = true;
835 break;
836 case "resubmit":
837 if ($log->cmid) {
838 //Get the new_id of the module (to recode the info field)
839 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
840 if ($mod) {
841 $log->url = "view.php?id=".$log->cmid;
842 $log->info = $mod->new_id;
843 $status = true;
846 break;
847 case "league table":
848 if ($log->cmid) {
849 //Get the new_id of the module (to recode the info field)
850 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
851 if ($mod) {
852 $log->url = "view.php?id=".$log->cmid;
853 $log->info = $mod->new_id;
854 $status = true;
857 break;
858 case "submissions":
859 if ($log->cmid) {
860 //Get the new_id of the module (to recode the info field)
861 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
862 if ($mod) {
863 $log->url = "view.php?id=".$log->cmid;
864 $log->info = $mod->new_id;
865 $status = true;
868 break;
869 case "allow both":
870 if ($log->cmid) {
871 //Get the new_id of the module (to recode the info field)
872 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
873 if ($mod) {
874 $log->url = "view.php?id=".$log->cmid;
875 $log->info = $mod->new_id;
876 $status = true;
879 break;
880 case "set up":
881 if ($log->cmid) {
882 //Get the new_id of the module (to recode the info field)
883 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
884 if ($mod) {
885 $log->url = "view.php?id=".$log->cmid;
886 $log->info = $mod->new_id;
887 $status = true;
890 break;
891 case "assessments onl":
892 if ($log->cmid) {
893 //Get the new_id of the module (to recode the info field)
894 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
895 if ($mod) {
896 $log->url = "view.php?id=".$log->cmid;
897 $log->info = $mod->new_id;
898 $status = true;
901 break;
902 case "close":
903 if ($log->cmid) {
904 //Get the new_id of the module (to recode the info field)
905 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
906 if ($mod) {
907 $log->url = "view.php?id=".$log->cmid;
908 $log->info = $mod->new_id;
909 $status = true;
912 break;
913 case "display grades":
914 if ($log->cmid) {
915 //Get the new_id of the module (to recode the info field)
916 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
917 if ($mod) {
918 $log->url = "view.php?id=".$log->cmid;
919 $log->info = $mod->new_id;
920 $status = true;
923 break;
924 case "over allocation":
925 if ($log->cmid) {
926 //Get the new_id of the module (to recode the info field)
927 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
928 if ($mod) {
929 $log->url = "view.php?id=".$log->cmid;
930 $log->info = $mod->new_id;
931 $status = true;
934 break;
935 case "assess":
936 if ($log->cmid) {
937 //Get the new_id of the workshop assessments
938 $ass = backup_getid($restore->backup_unique_code,"workshop_assessments",$log->info);
939 if ($ass) {
940 $log->url = "assessments.php?action=viewassessment&id=".$log->cmid."&aid=".$ass->new_id;
941 $log->info = $ass->new_id;
942 $status = true;
945 break;
946 case "grade":
947 if ($log->cmid) {
948 //Get the new_id of the workshop assessments
949 $ass = backup_getid($restore->backup_unique_code,"workshop_assessments",$log->info);
950 if ($ass) {
951 $log->url = "assessments.php?action=viewassessment&id=".$log->cmid."&aid=".$ass->new_id;
952 $log->info = $ass->new_id;
953 $status = true;
956 break;
957 default:
958 if (!defined('RESTORE_SILENTLY')) {
959 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
961 break;
964 if ($status) {
965 $status = $log;
967 return $status;