File: restorelib.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (964 lines) | stat: -rw-r--r-- 43,354 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
<?php //$Id: restorelib.php,v 1.26 2006/04/11 16:23:29 stronk7 Exp $
    //This php script contains all the stuff to backup/restore
    //workshop mods

    //This is the "graphical" structure of the workshop mod:
    //
    //                                          workshop
    //                                         (CL,pk->id)
    //                                             |
    //                                             |
    //                                             |
    //              |------------------------------|-----------------------------------------------------|
    //              |                                                                                    |
    //              |                                                                                    |
    //              |                                                                                    |
    //              |                                                                            workshop_submissions
    //              |                                                                        (UL,pk->id,fk->workshopid,files)
    //              |                                                                                    |
    //              |        |-------------------------------------|      |----------------------|       |
    //              |        |                                     |      |                      |       |
    //             workshop_elements                           workshop_grades                  workshop_assessments
    //         (CL,pk->id,fk->workshopid)                (UL,pk->id,fk->assessmentid)       (UL,pk->id,fk->submissionid)
    //              |                  |                 (          fk->elementno   )                    |
    //              |                  |                                                                 |
    //              |                  |                                                                 |
    //      workshop_rubrics          workshop_stockcomments                                        workshop_comments
    // (CL,pk->id,fk->elementno)   (CL, pk->id, fk->elementno)                             (UL,pk->id,fk->assessmentid)
    //
    // 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 workshop_restore_mods($mod,$restore) {

        global $CFG;

        $status = true;

        //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;
            //traverse_xmlize($info);                                                                     //Debug
            //print_object ($GLOBALS['traverse_array']);                                                  //Debug
            //$GLOBALS['traverse_array']="";                                                              //Debug

            //Now, build the WORKSHOP record structure
            $workshop->course = $restore->course_id;
            $workshop->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
            $workshop->description = backup_todb($info['MOD']['#']['DESCRIPTION']['0']['#']);
            $workshop->wtype = backup_todb($info['MOD']['#']['WTYPE']['0']['#']);
            $workshop->nelements = backup_todb($info['MOD']['#']['NELEMENTS']['0']['#']);
            $workshop->nattachments = backup_todb($info['MOD']['#']['NATTACHMENTS']['0']['#']);
            $workshop->phase = backup_todb($info['MOD']['#']['PHASE']['0']['#']);
            $workshop->format = backup_todb($info['MOD']['#']['FORMAT']['0']['#']);
            $workshop->gradingstrategy = backup_todb($info['MOD']['#']['GRADINGSTRATEGY']['0']['#']);
            $workshop->resubmit = backup_todb($info['MOD']['#']['RESUBMIT']['0']['#']);
            $workshop->agreeassessments = backup_todb($info['MOD']['#']['AGREEASSESSMENTS']['0']['#']);
            $workshop->hidegrades = backup_todb($info['MOD']['#']['HIDEGRADES']['0']['#']);
            $workshop->anonymous = backup_todb($info['MOD']['#']['ANONYMOUS']['0']['#']);
            $workshop->includeself = backup_todb($info['MOD']['#']['INCLUDESELF']['0']['#']);
            $workshop->maxbytes = backup_todb($info['MOD']['#']['MAXBYTES']['0']['#']);
            $workshop->submissionstart = backup_todb($info['MOD']['#']['SUBMISSIONSTART']['0']['#']);
            $workshop->assessmentstart = backup_todb($info['MOD']['#']['ASSESSMENTSTART']['0']['#']);
            $workshop->deadline = backup_todb($info['MOD']['#']['DEADLINE']['0']['#']);
            $workshop->submissionend = backup_todb($info['MOD']['#']['SUBMISSIONEND']['0']['#']);
            $workshop->assessmentend = backup_todb($info['MOD']['#']['ASSESSMENTEND']['0']['#']);
            $workshop->releasegrades = backup_todb($info['MOD']['#']['RELEASEGRADES']['0']['#']);
            $workshop->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
            $workshop->gradinggrade = backup_todb($info['MOD']['#']['GRADINGGRADE']['0']['#']);
            $workshop->ntassessments = backup_todb($info['MOD']['#']['NTASSESSMENTS']['0']['#']);
            $workshop->assessmentcomps = backup_todb($info['MOD']['#']['ASSESSMENTCOMPS']['0']['#']);
            $workshop->nsassessments = backup_todb($info['MOD']['#']['NSASSESSMENTS']['0']['#']);
            $workshop->overallocation = backup_todb($info['MOD']['#']['OVERALLOCATION']['0']['#']);
            $workshop->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
            $workshop->teacherweight = backup_todb($info['MOD']['#']['TEACHERWEIGHT']['0']['#']);
            $workshop->showleaguetable = backup_todb($info['MOD']['#']['SHOWLEAGUETABLE']['0']['#']);
            $workshop->usepassword = backup_todb($info['MOD']['#']['USEPASSWORD']['0']['#']);
            $workshop->password = backup_todb($info['MOD']['#']['PASSWORD']['0']['#']);

            //If we have retrieved workshop->phase, it's a pre 1.5 backup, so we have to do
            //some conversions before inserting to DB. Upwards compatibility :-)
            if ( isset($info['MOD']['#']['PHASE']['0']['#'])) { //It's a pre-15 backup file

                //Adjust the wtype field (mimetised from the upgrade script)
                $workshop->wtype = 0;
                if ($workshop->includeself || $workshop->ntassessments) {
                    $workshop->wtype = 1;    // 3 phases with grading grades
                } else if ($workshop->nsassessments) {
                    $workshop->wtype = 2;   // 5 phases with grading grades
                }

                //Now, adjust phases time limits (mimetised from the upgrade script too)
                $early = 0;
                $late = 0;
                $now = time();
                if ($now < $workshop->deadline) {
                    $late = $workshop->deadline;
                } else {
                    $early = $workshop->deadline;
                }
                if ($workshop->phase > 1) {
                    $workshop->submissionstart = $early;
                } else {
                    $workshop->submissionstart = $late;
                }
                if ($workshop->phase > 2) {
                    $workshop->assessmentstart = $early;
                } else {
                    $workshop->assessmentstart = $late;
                }
                if ($workshop->phase > 3) {
                    $workshop->submissionend = $early;
                } else {
                    $workshop->submissionend = $late;
                }
                if ($workshop->phase > 4) {
                    $workshop->assessmentend = $early;
                } else {
                    $workshop->assessmentend = $late;
                }
                if ($workshop->phase > 5) {
                    $workshop->releasegrades = $now;
                } else {
                    $workshop->releasegrades = $now + (4 * 7 * 24 * 60 * 60); //Grades will be available in 4 weeks
                }
            }

            //The structure is equal to the db, so insert the workshop
            $newid = insert_record ("workshop",$workshop);

            //Do some output     
            if (!defined('RESTORE_SILENTLY')) {
                echo "<li>".get_string("modulename","workshop")." \"".format_string(stripslashes($workshop->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);
                //We have to restore the workshop_elements table now (course level table)
                $status = workshop_elements_restore_mods($newid,$info,$restore);
                //Now check if want to restore user data and do it.
                if (restore_userdata_selected($restore,'workshop',$mod->id)) {
                    //Restore workshop_submissions
                    $status = workshop_submissions_restore_mods ($mod->id, $newid,$info,$restore);
                }
            } else {
                $status = false;
            }
        } else {
            $status = false;
        }

        return $status;
    }

    //This function restores the workshop_elements
    function workshop_elements_restore_mods($workshop_id,$info,$restore) {

        global $CFG;

        $status = true;

        //Get the workshop_elements array
        $elements = $info['MOD']['#']['ELEMENTS']['0']['#']['ELEMENT'];

        //Iterate over workshop_elements
        for($i = 0; $i < sizeof($elements); $i++) {
            $ele_info = $elements[$i];
            //traverse_xmlize($ele_info);                                                                 //Debug
            //print_object ($GLOBALS['traverse_array']);                                                  //Debug
            //$GLOBALS['traverse_array']="";                                                              //Debug

            //Now, build the WORKSHOP_ELEMENTS record structure
            $element->workshopid = $workshop_id;
            $element->elementno = backup_todb($ele_info['#']['ELEMENTNO']['0']['#']);
            $element->description = backup_todb($ele_info['#']['DESCRIPTION']['0']['#']);
            $element->scale = backup_todb($ele_info['#']['SCALE']['0']['#']);
            $element->maxscore = backup_todb($ele_info['#']['MAXSCORE']['0']['#']);
            $element->weight = backup_todb($ele_info['#']['WEIGHT']['0']['#']);
            $element->stddev = backup_todb($ele_info['#']['STDDEV']['0']['#']);
            $element->totalassessments = backup_todb($ele_info['#']['TOTALASSESSMENTS']['0']['#']);

            //The structure is equal to the db, so insert the workshop_elements
            $newid = insert_record ("workshop_elements",$element);

            //Do some output
            if (($i+1) % 10 == 0) {
                if (!defined('RESTORE_SILENTLY')) {
                    echo ".";
                    if (($i+1) % 200 == 0) {
                        echo "<br />";
                    }
                }
                backup_flush(300);
            }

            if ($newid) {
                //We have to restore the workshop_rubrics table now (course level table)
                $status = workshop_rubrics_restore_mods($workshop_id,$element->elementno,$ele_info,$restore);
                //We have to restore the workshop_stockcomment table now (course level table)
                $status = workshop_stockcomments_restore_mods($workshop_id,$element->elementno,$ele_info,$restore);
            } else {
                $status = false;
            }
        }

        return $status;
    }


    //This function restores the workshop_rubrics
    function workshop_rubrics_restore_mods($workshop_id,$elementno,$info,$restore) {

        global $CFG;

        $status = true;

        //Get the workshop_rubrics array (optional)
        if (isset($info['#']['RUBRICS']['0']['#']['RUBRIC'])) {
            $rubrics = $info['#']['RUBRICS']['0']['#']['RUBRIC'];

            //Iterate over workshop_rubrics
            for($i = 0; $i < sizeof($rubrics); $i++) {
                $rub_info = $rubrics[$i];
                //traverse_xmlize($rub_info);                             //Debug
                //print_object ($GLOBALS['traverse_array']);              //Debug
                //$GLOBALS['traverse_array']="";                          //Debug

                //Now, build the WORKSHOP_RUBRICS record structure
                $rubric->workshopid = $workshop_id;
                $rubric->elementno = $elementno;
                $rubric->rubricno = backup_todb($rub_info['#']['RUBRICNO']['0']['#']);
                $rubric->description = backup_todb($rub_info['#']['DESCRIPTION']['0']['#']);

                //The structure is equal to the db, so insert the workshop_rubrics
                $newid = insert_record ("workshop_rubrics",$rubric);

                //Do some output
                if (($i+1) % 10 == 0) {
                    if (!defined('RESTORE_SILENTLY')) {
                        echo ".";
                        if (($i+1) % 200 == 0) {
                            echo "<br />";
                        }
                    }
                    backup_flush(300);
                }

                if (!$newid) {
                    $status = false;
                }
            }
        }
        return $status;
    }


    //This function restores the workshop_stockcomments
    function workshop_stockcomments_restore_mods($new_workshop_id, $elementno, $info, $restore) {

        global $CFG;

        $status = true;

        //Get the stockcomments array (optional)
        if (isset($info['#']['STOCKCOMMENTS']['0']['#']['STOCKCOMMENT'])) {
            $stockcomments = $info['#']['STOCKCOMMENTS']['0']['#']['STOCKCOMMENT'];

            //Iterate over stock comments
            for($i = 0; $i < sizeof($stockcomments); $i++) {
                $com_info = $stockcomments[$i];
                //traverse_xmlize($com_info);                            //Debug
                //print_object ($GLOBALS['traverse_array']);             //Debug
                //$GLOBALS['traverse_array']="";                         //Debug

                //Now, build the WORKSHOP_STOCKCOMMENTS record structure
                $stockcomment->workshopid = $new_workshop_id;
                $stockcomment->elementno = $elementno;
                $stockcomment->comments = backup_todb($com_info['#']['COMMENT_TEXT']['0']['#']);

                //The structure is equal to the db, so insert the workshop_comment
                $newid = insert_record ("workshop_stockcomments",$stockcomment);

                //Do some output
                if (($i+1) % 50 == 0) {
                    if (!defined('RESTORE_SILENTLY')) {
                        echo ".";
                        if (($i+1) % 1000 == 0) {
                            echo "<br />";
                        }
                    }
                    backup_flush(300);
                }

                if (!$newid) {
                    $status = false;
                }
            }
        }

        return $status;
    }

    //This function restores the workshop_submissions
    function workshop_submissions_restore_mods($old_workshop_id, $new_workshop_id,$info,$restore) {

        global $CFG;

        $status = true;

        //Get the submissions array 
        $submissions = $info['MOD']['#']['SUBMISSIONS']['0']['#']['SUBMISSION'];

        //Iterate over submissions
        for($i = 0; $i < sizeof($submissions); $i++) {
            $sub_info = $submissions[$i];
            //traverse_xmlize($sub_info);                                     //Debug
            //print_object ($GLOBALS['traverse_array']);                      //Debug
            //$GLOBALS['traverse_array']="";                                  //Debug

            //We'll need this later!!
            $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
            $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);

            //Now, build the WORKSHOP_SUBMISSIONS record structure
            $submission->workshopid = $new_workshop_id;
            $submission->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
            $submission->title = backup_todb($sub_info['#']['TITLE']['0']['#']);
            $submission->timecreated = backup_todb($sub_info['#']['TIMECREATED']['0']['#']);
            $submission->mailed = backup_todb($sub_info['#']['MAILED']['0']['#']);
            $submission->description = backup_todb($sub_info['#']['DESCRIPTION']['0']['#']);
            $submission->gradinggrade = backup_todb($sub_info['#']['GRADINGGRADE']['0']['#']);
            $submission->finalgrade = backup_todb($sub_info['#']['FINALGRADE']['0']['#']);
            $submission->late = backup_todb($sub_info['#']['LATE']['0']['#']);
            $submission->nassessments = backup_todb($sub_info['#']['NASSESSMENTS']['0']['#']);

            //We have to recode the userid field
            $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
            if ($user) {
                $submission->userid = $user->new_id;
            }

            //The structure is equal to the db, so insert the workshop_submission
            $newid = insert_record ("workshop_submissions",$submission);

            //Do some output
            if (($i+1) % 50 == 0) {
                if (!defined('RESTORE_SILENTLY')) {
                    echo ".";
                    if (($i+1) % 1000 == 0) {
                        echo "<br />";
                    }
                }
                backup_flush(300);
            }

            if ($newid) {
                //We have the newid, update backup_ids
                backup_putid($restore->backup_unique_code,"workshop_submissions",$oldid,
                             $newid);

                //Now copy moddata associated files
                $status = workshop_restore_files ($oldid, $newid,$restore); 
                //Now we need to restore workshop_assessments (user level table)
                if ($status) {
                    $status = workshop_assessments_restore_mods ($new_workshop_id, $newid,$sub_info,$restore);
                }
            } else {
                $status = false;
            }
        }

        return $status;
    }

    //This function restores the workshop_assessments       
    function workshop_assessments_restore_mods($new_workshop_id, $new_submission_id,$info,$restore) {

        global $CFG;

        $status = true;

        //Get the assessments array (if any)
        if (isset($info['#']['ASSESSMENTS']['0']['#']['ASSESSMENT'])) {
            $assessments = $info['#']['ASSESSMENTS']['0']['#']['ASSESSMENT'];

            //Iterate over assessments
            for($i = 0; $i < sizeof($assessments); $i++) {
                $ass_info = $assessments[$i];
                //traverse_xmlize($ass_info);                                                                 //Debug
                //print_object ($GLOBALS['traverse_array']);                                                  //Debug
                //$GLOBALS['traverse_array']="";                                                              //Debug

                //We'll need this later!!
                $oldid = backup_todb($ass_info['#']['ID']['0']['#']);
                $olduserid = backup_todb($ass_info['#']['USERID']['0']['#']);

                //Now, build the WORKSHOP_ASSESSMENTS record structure
                $assessment->workshopid = $new_workshop_id;
                $assessment->submissionid = $new_submission_id;
                $assessment->userid = backup_todb($ass_info['#']['USERID']['0']['#']);
                $assessment->timecreated = backup_todb($ass_info['#']['TIMECREATED']['0']['#']);
                $assessment->timegraded = backup_todb($ass_info['#']['TIMEGRADED']['0']['#']);
                $assessment->timeagreed = backup_todb($ass_info['#']['TIMEAGREED']['0']['#']);
                $assessment->grade = backup_todb($ass_info['#']['GRADE']['0']['#']);
                $assessment->gradinggrade = backup_todb($ass_info['#']['GRADINGGRADE']['0']['#']);
                $assessment->mailed = backup_todb($ass_info['#']['MAILED']['0']['#']);
                $assessment->resubmission = backup_todb($ass_info['#']['RESUBMISSION']['0']['#']);
                $assessment->donotuse = backup_todb($ass_info['#']['DONOTUSE']['0']['#']);
                $assessment->generalcomment = backup_todb($ass_info['#']['GENERALCOMMENT']['0']['#']);
                $assessment->teachercomment = backup_todb($ass_info['#']['TEACHERCOMMENT']['0']['#']);

                //We have to recode the userid field
                $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
                if ($user) {
                    $assessment->userid = $user->new_id;
                }

                //The structure is equal to the db, so insert the workshop_assessment
                $newid = insert_record ("workshop_assessments",$assessment);

                //Do some output
                if (($i+1) % 50 == 0) {
                    if (!defined('RESTORE_SILENTLY')) {
                        echo ".";
                        if (($i+1) % 1000 == 0) {
                            echo "<br />";
                        }
                    }
                    backup_flush(300);
                }

                if ($newid) {
                    //We have the newid, update backup_ids
                    backup_putid($restore->backup_unique_code,"workshop_assessments",$oldid,
                            $newid);

                    //Now we need to restore workshop_comments (user level table)
                    if ($status) {
                        $status = workshop_comments_restore_mods ($new_workshop_id, $newid,$ass_info,$restore);
                    }
                    //Now we need to restore workshop_grades (user level table)   
                    if ($status) {
                        $status = workshop_grades_restore_mods ($new_workshop_id, $newid,$ass_info,$restore);   
                    }
                } else {
                    $status = false;
                }
            }
        }

        return $status;
    }

    //This function restores the workshop_comments
    function workshop_comments_restore_mods($new_workshop_id, $new_assessment_id,$info,$restore) {

        global $CFG;

        $status = true;

        //Get the comments array (optional)
        if (isset($info['#']['COMMENTS']['0']['#']['COMMENT'])) {
            $comments = $info['#']['COMMENTS']['0']['#']['COMMENT'];

            //Iterate over comments
            for($i = 0; $i < sizeof($comments); $i++) {
                $com_info = $comments[$i];
                //traverse_xmlize($com_info);                            //Debug
                //print_object ($GLOBALS['traverse_array']);             //Debug
                //$GLOBALS['traverse_array']="";                         //Debug

                //We'll need this later!!
                $olduserid = backup_todb($com_info['#']['USERID']['0']['#']);

                //Now, build the WORKSHOP_COMMENTS record structure
                $comment->workshopid = $new_workshop_id;
                $comment->assessmentid = $new_assessment_id;
                $comment->userid = backup_todb($com_info['#']['USERID']['0']['#']);
                $comment->timecreated = backup_todb($com_info['#']['TIMECREATED']['0']['#']);
                $comment->mailed = backup_todb($com_info['#']['MAILED']['0']['#']);
                $comment->comments = backup_todb($com_info['#']['COMMENT_TEXT']['0']['#']);

                //We have to recode the userid field
                $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
                if ($user) {
                    $comment->userid = $user->new_id;
                }

                //The structure is equal to the db, so insert the workshop_comment
                $newid = insert_record ("workshop_comments",$comment);

                //Do some output
                if (($i+1) % 50 == 0) {
                    if (!defined('RESTORE_SILENTLY')) {
                        echo ".";
                        if (($i+1) % 1000 == 0) {
                            echo "<br />";
                        }
                    }
                    backup_flush(300);
                }

                if (!$newid) {
                    $status = false;
                }
            }
        }

        return $status;
    }

    //This function restores the workshop_grades
    function workshop_grades_restore_mods($new_workshop_id, $new_assessment_id,$info,$restore) {

        global $CFG;

        $status = true;

        //Get the grades array (optional)
        if (isset($info['#']['GRADES']['0']['#']['GRADE'])) {
            $grades = $info['#']['GRADES']['0']['#']['GRADE'];

            //Iterate over grades
            for($i = 0; $i < sizeof($grades); $i++) {
                $gra_info = $grades[$i];
                //traverse_xmlize($gra_info);                             //Debug
                //print_object ($GLOBALS['traverse_array']);              //Debug
                //$GLOBALS['traverse_array']="";                          //Debug

                //Now, build the WORKSHOP_GRADES record structure
                $grade->workshopid = $new_workshop_id;
                $grade->assessmentid = $new_assessment_id;
                $grade->elementno = backup_todb($gra_info['#']['ELEMENTNO']['0']['#']);
                $grade->feedback = backup_todb($gra_info['#']['FEEDBACK']['0']['#']);
                $grade->grade = backup_todb($gra_info['#']['GRADE_VALUE']['0']['#']);

                //The structure is equal to the db, so insert the workshop_grade
                $newid = insert_record ("workshop_grades",$grade);

                //Do some output
                if (($i+1) % 50 == 0) {
                    if (!defined('RESTORE_SILENTLY')) {
                        echo ".";
                        if (($i+1) % 1000 == 0) {
                            echo "<br />";
                        }
                    }
                    backup_flush(300);
                }

                if (!$newid) {
                    $status = false;
                }
            }
        }

        return $status;
    }

    //This function copies the workshop related info from backup temp dir to course moddata folder,
    //creating it if needed and recoding everything (submission_id) 
    function workshop_restore_files ($oldsubmiss, $newsubmiss, $restore) {

        global $CFG;

        $status = true;
        $todo = false;
        $moddata_path = "";
        $workshop_path = "";
        $temp_path = "";

        //First, we check to "course_id" exists and create is as necessary
        //in CFG->dataroot
        $dest_dir = $CFG->dataroot."/".$restore->course_id;
        $status = check_dir_exists($dest_dir,true);

        //Now, locate course's moddata directory
        $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
   
        //Check it exists and create it
        $status = check_dir_exists($moddata_path,true);

        //Now, locate workshop directory
        if ($status) {
            $workshop_path = $moddata_path."/workshop";
            //Check it exists and create it
            $status = check_dir_exists($workshop_path,true);
        }

        //Now locate the temp dir we are gong to restore
        if ($status) {
            $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
                         "/moddata/workshop/".$oldsubmiss;
            //Check it exists
            if (is_dir($temp_path)) {
                $todo = true;
            }
        }

        //If todo, we create the neccesary dirs in course moddata/workshop
        if ($status and $todo) {
            //First this workshop id
            $this_workshop_path = $workshop_path."/".$newsubmiss;
            $status = check_dir_exists($this_workshop_path,true);
            //And now, copy temp_path to this_workshop_path
            $status = backup_copy_file($temp_path, $this_workshop_path); 
        }
       
        return $status;
    }

    //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
    //some texts in the module
    function workshop_restore_wiki2markdown ($restore) {

        global $CFG;

        $status = true;

        //Convert workshop->description
        if ($records = get_records_sql ("SELECT w.id, w.description, w.format
                                         FROM {$CFG->prefix}workshop w,
                                              {$CFG->prefix}backup_ids b
                                         WHERE w.course = $restore->course_id AND
                                               format = ".FORMAT_WIKI. " AND
                                               b.backup_code = $restore->backup_unique_code AND
                                               b.table_name = 'workshop' AND
                                               b.new_id = w.id")) {
            foreach ($records as $record) {
                //Rebuild wiki links
                $record->description = restore_decode_wiki_content($record->description, $restore);
                //Convert to Markdown
                $wtm = new WikiToMarkdown();
                $record->description = $wtm->convert($record->description, $restore->course_id);
                $record->format = FORMAT_MARKDOWN;
                $status = update_record('workshop', addslashes_object($record));
                //Do some output
                $i++;
                if (($i+1) % 1 == 0) {
                    if (!defined('RESTORE_SILENTLY')) {
                        echo ".";
                        if (($i+1) % 20 == 0) {
                            echo "<br />";
                        }
                    }
                    backup_flush(300);
                }
            }

        }
        return $status;
    }

    //Return a content decoded to support interactivities linking. Every module
    //should have its own. They are called automatically from
    //workshop_decode_content_links_caller() function in each module
    //in the restore process
    function workshop_decode_content_links ($content,$restore) {

        global $CFG;

        $result = $content;

        //Link to the list of workshops

        $searchstring='/\$@(WORKSHOPINDEX)\*([0-9]+)@\$/';
        //We look for it
        preg_match_all($searchstring,$content,$foundset);
        //If found, then we are going to look for its new id (in backup tables)
        if ($foundset[0]) {
            //print_object($foundset);                                     //Debug
            //Iterate over foundset[2]. They are the old_ids
            foreach($foundset[2] as $old_id) {
                //We get the needed variables here (course id)
                $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
                //Personalize the searchstring
                $searchstring='/\$@(WORKSHOPINDEX)\*('.$old_id.')@\$/';
                //If it is a link to this course, update the link to its new location
                if($rec->new_id) {
                    //Now replace it
                    $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/workshop/index.php?id='.$rec->new_id,$result);
                } else {
                    //It's a foreign link so leave it as original
                    $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/workshop/index.php?id='.$old_id,$result);
                }
            }
        }

        //Link to workshop view by moduleid

        $searchstring='/\$@(WORKSHOPVIEWBYID)\*([0-9]+)@\$/';
        //We look for it
        preg_match_all($searchstring,$result,$foundset);
        //If found, then we are going to look for its new id (in backup tables)
        if ($foundset[0]) {
            //print_object($foundset);                                     //Debug
            //Iterate over foundset[2]. They are the old_ids
            foreach($foundset[2] as $old_id) {
                //We get the needed variables here (course_modules id)
                $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
                //Personalize the searchstring
                $searchstring='/\$@(WORKSHOPVIEWBYID)\*('.$old_id.')@\$/';
                //If it is a link to this course, update the link to its new location
                if($rec->new_id) {
                    //Now replace it
                    $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/workshop/view.php?id='.$rec->new_id,$result);
                } else {
                    //It's a foreign link so leave it as original
                    $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/workshop/view.php?id='.$old_id,$result);
                }
            }
        }

        return $result;
    }

    //This function makes all the necessary calls to xxxx_decode_content_links()
    //function in each module, passing them the desired contents to be decoded
    //from backup format to destination site/course in order to mantain inter-activities
    //working in the backup/restore process. It's called from restore_decode_content_links()
    //function in restore process
    function workshop_decode_content_links_caller($restore) {
        global $CFG;
        $status = true;

        //Process every WORKSHOP (description) in the course
        if ($workshops = get_records_sql ("SELECT w.id, w.description
                                           FROM {$CFG->prefix}workshop w
                                           WHERE w.course = $restore->course_id")) {
            //Iterate over each workshop->description
            $i = 0;   //Counter to send some output to the browser to avoid timeouts
            foreach ($workshops as $workshop) {
                //Increment counter
                $i++;
                $content = $workshop->description;
                $result = restore_decode_content_links_worker($content,$restore);
                if ($result != $content) {
                    //Update record
                    $workshop->description = addslashes($result);
                    $status = update_record("workshop",$workshop);
                    if ($CFG->debug>7) {
                        if (!defined('RESTORE_SILENTLY')) {
                            echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
                        }
                    }
                }
                //Do some output
                if (($i+1) % 5 == 0) {
                    if (!defined('RESTORE_SILENTLY')) {
                        echo ".";
                        if (($i+1) % 100 == 0) {
                            echo "<br />";
                        }
                    }
                    backup_flush(300);
                }
            }
        }

        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 workshop_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 "submit":
            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 "resubmit":
            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 "league table":
            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 "submissions":
            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 "allow both":
            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 "set up":
            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 "assessments onl":
            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 "close":
            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 "display grades":
            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 "over allocation":
            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 "assess":
            if ($log->cmid) {
                //Get the new_id of the workshop assessments
                $ass = backup_getid($restore->backup_unique_code,"workshop_assessments",$log->info);
                if ($ass) {
                    $log->url = "assessments.php?action=viewassessment&id=".$log->cmid."&aid=".$ass->new_id;
                    $log->info = $ass->new_id;
                    $status = true;
                }
            }
            break;
        case "grade":
            if ($log->cmid) {
                //Get the new_id of the workshop assessments
                $ass = backup_getid($restore->backup_unique_code,"workshop_assessments",$log->info);
                if ($ass) {
                    $log->url = "assessments.php?action=viewassessment&id=".$log->cmid."&aid=".$ass->new_id;
                    $log->info = $ass->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;
    }
?>