2 //This php script contains all the stuff to backup/restore
5 //This is the "graphical" structure of the workshop mod:
12 // |------------------------------|-----------------------------------------------------|
16 // | workshop_submissions
17 // | (UL,pk->id,fk->workshopid,files)
19 // | |-------------------------------------| |----------------------| |
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 ) |
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 backup procedure about this mod
39 function workshop_backup_mods($bf,$preferences) {
45 //Iterate over workshop table
46 $workshops = get_records ("workshop","course",$preferences->backup_course
,"id");
48 foreach ($workshops as $workshop) {
49 if (backup_mod_selected($preferences,'workshop',$workshop->id
)) {
50 $status = workshop_backup_one_mod($bf,$preferences,$workshop);
58 function workshop_backup_one_mod($bf,$preferences,$workshop) {
62 if (is_numeric($workshop)) {
63 $workshop = get_record('workshop','id',$workshop);
65 $instanceid = $workshop->id
;
68 fwrite ($bf,start_tag("MOD",3,true));
70 fwrite ($bf,full_tag("ID",4,false,$workshop->id
));
71 fwrite ($bf,full_tag("MODTYPE",4,false,"workshop"));
72 fwrite ($bf,full_tag("NAME",4,false,$workshop->name
));
73 fwrite ($bf,full_tag("DESCRIPTION",4,false,$workshop->description
));
74 fwrite ($bf,full_tag("WTYPE",4,false,$workshop->wtype
));
75 fwrite ($bf,full_tag("NELEMENTS",4,false,$workshop->nelements
));
76 fwrite ($bf,full_tag("NATTACHMENTS",4,false,$workshop->nattachments
));
77 fwrite ($bf,full_tag("FORMAT",4,false,$workshop->format
));
78 fwrite ($bf,full_tag("GRADINGSTRATEGY",4,false,$workshop->gradingstrategy
));
79 fwrite ($bf,full_tag("RESUBMIT",4,false,$workshop->resubmit
));
80 fwrite ($bf,full_tag("AGREEASSESSMENTS",4,false,$workshop->agreeassessments
));
81 fwrite ($bf,full_tag("HIDEGRADES",4,false,$workshop->hidegrades
));
82 fwrite ($bf,full_tag("ANONYMOUS",4,false,$workshop->anonymous
));
83 fwrite ($bf,full_tag("INCLUDESELF",4,false,$workshop->includeself
));
84 fwrite ($bf,full_tag("MAXBYTES",4,false,$workshop->maxbytes
));
85 fwrite ($bf,full_tag("SUBMISSIONSTART",4,false,$workshop->submissionstart
));
86 fwrite ($bf,full_tag("ASSESSMENTSTART",4,false,$workshop->assessmentstart
));
87 fwrite ($bf,full_tag("SUBMISSIONEND",4,false,$workshop->submissionend
));
88 fwrite ($bf,full_tag("ASSESSMENTEND",4,false,$workshop->assessmentend
));
89 fwrite ($bf,full_tag("RELEASEGRADES",4,false,$workshop->releasegrades
));
90 fwrite ($bf,full_tag("GRADE",4,false,$workshop->grade
));
91 fwrite ($bf,full_tag("GRADINGGRADE",4,false,$workshop->gradinggrade
));
92 fwrite ($bf,full_tag("NTASSESSMENTS",4,false,$workshop->ntassessments
));
93 fwrite ($bf,full_tag("ASSESSMENTCOMPS",4,false,$workshop->assessmentcomps
));
94 fwrite ($bf,full_tag("NSASSESSMENTS",4,false,$workshop->nsassessments
));
95 fwrite ($bf,full_tag("OVERALLOCATION",4,false,$workshop->overallocation
));
96 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$workshop->timemodified
));
97 fwrite ($bf,full_tag("TEACHERWEIGHT",4,false,$workshop->teacherweight
));
98 fwrite ($bf,full_tag("SHOWLEAGUETABLE",4,false,$workshop->showleaguetable
));
99 fwrite ($bf,full_tag("USEPASSWORD",4,false,$workshop->usepassword
));
100 fwrite ($bf,full_tag("PASSWORD",4,false,$workshop->password
));
101 //Now we backup workshop elements
102 $status = backup_workshop_elements($bf,$preferences,$workshop->id
);
104 //if we've selected to backup users info, then execute backup_workshop_submisions
105 if (backup_userdata_selected($preferences,'workshop',$workshop->id
)) {
107 $status = backup_workshop_submissions($bf,$preferences,$workshop->id
,$ws);
108 $status = backup_workshop_files_instance($bf,$preferences,$workshop->id
,$ws);
112 $status =fwrite ($bf,end_tag("MOD",3,true));
117 //Backup workshop_elements contents (executed from workshop_backup_mods)
118 function backup_workshop_elements ($bf,$preferences,$workshop) {
124 $workshop_elements = get_records("workshop_elements","workshopid",$workshop,"id");
125 //If there is workshop_elements
126 if ($workshop_elements) {
128 $status =fwrite ($bf,start_tag("ELEMENTS",4,true));
129 //Iterate over each element
130 foreach ($workshop_elements as $wor_ele) {
132 $status =fwrite ($bf,start_tag("ELEMENT",5,true));
133 //Print element contents
134 fwrite ($bf,full_tag("ELEMENTNO",6,false,$wor_ele->elementno
));
135 fwrite ($bf,full_tag("DESCRIPTION",6,false,$wor_ele->description
));
136 fwrite ($bf,full_tag("SCALE",6,false,$wor_ele->scale
));
137 fwrite ($bf,full_tag("MAXSCORE",6,false,$wor_ele->maxscore
));
138 fwrite ($bf,full_tag("WEIGHT",6,false,$wor_ele->weight
));
139 fwrite ($bf,full_tag("STDDEV",6,false,$wor_ele->stddev
));
140 fwrite ($bf,full_tag("TOTALASSESSMENTS",6,false,$wor_ele->totalassessments
));
141 //Now we backup workshop rubrics
142 $status = backup_workshop_rubrics($bf,$preferences,$workshop,$wor_ele->elementno
);
143 //Now we backup element's stock comments
144 $status = backup_workshop_stockcomments($bf,$preferences,$workshop,$wor_ele->elementno
);
146 $status =fwrite ($bf,end_tag("ELEMENT",5,true));
149 $status =fwrite ($bf,end_tag("ELEMENTS",4,true));
154 //Backup workshop_rubrics contents (executed from backup_workshop_elements)
155 function backup_workshop_rubrics ($bf,$preferences,$workshop,$elementno) {
161 $workshop_rubrics = get_records_sql("SELECT * from {$CFG->prefix}workshop_rubrics r
162 WHERE r.workshopid = '$workshop' and r.elementno = '$elementno'
163 ORDER BY r.elementno");
165 //If there is workshop_rubrics
166 if ($workshop_rubrics) {
168 $status =fwrite ($bf,start_tag("RUBRICS",6,true));
169 //Iterate over each element
170 foreach ($workshop_rubrics as $wor_rub) {
172 $status =fwrite ($bf,start_tag("RUBRIC",7,true));
173 //Print rubric contents
174 fwrite ($bf,full_tag("RUBRICNO",8,false,$wor_rub->rubricno
));
175 fwrite ($bf,full_tag("DESCRIPTION",8,false,$wor_rub->description
));
177 $status =fwrite ($bf,end_tag("RUBRIC",7,true));
180 $status =fwrite ($bf,end_tag("RUBRICS",6,true));
185 //Backup workshop_stockcomments contents (executed from backup_workshop_elements)
186 function backup_workshop_stockcomments ($bf,$preferences,$workshop,$elementno) {
192 $workshop_stockcomments = get_records_sql("SELECT * from {$CFG->prefix}workshop_stockcomments c
193 WHERE c.workshopid = '$workshop' and c.elementno = '$elementno'
196 //If there is workshop_stockcomments
197 if ($workshop_stockcomments) {
199 $status =fwrite ($bf,start_tag("STOCKCOMMENTS",6,true));
200 //Iterate over each comment
201 foreach ($workshop_stockcomments as $wor_com) {
203 $status =fwrite ($bf,start_tag("STOCKCOMMENT",7,true));
204 //Print comment contents
205 fwrite ($bf,full_tag("COMMENT_TEXT",8,false,$wor_com->comments
));
207 $status =fwrite ($bf,end_tag("STOCKCOMMENT",7,true));
210 $status =fwrite ($bf,end_tag("STOCKCOMMENTS",6,true));
215 //Backup workshop_submissions contents (executed from workshop_backup_mods)
216 function backup_workshop_submissions ($bf,$preferences,$workshop,&$workshop_submissions) {
222 $workshop_submissions = get_records("workshop_submissions","workshopid",$workshop,"id");
223 //If there is submissions
224 if ($workshop_submissions) {
226 $status =fwrite ($bf,start_tag("SUBMISSIONS",4,true));
227 //Iterate over each submission
228 foreach ($workshop_submissions as $wor_sub) {
230 $status =fwrite ($bf,start_tag("SUBMISSION",5,true));
231 //Print submission contents
232 fwrite ($bf,full_tag("ID",6,false,$wor_sub->id
));
233 fwrite ($bf,full_tag("USERID",6,false,$wor_sub->userid
));
234 fwrite ($bf,full_tag("TITLE",6,false,$wor_sub->title
));
235 fwrite ($bf,full_tag("TIMECREATED",6,false,$wor_sub->timecreated
));
236 fwrite ($bf,full_tag("MAILED",6,false,$wor_sub->mailed
));
237 fwrite ($bf,full_tag("DESCRIPTION",6,false,$wor_sub->description
));
238 fwrite ($bf,full_tag("GRADINGGRADE",6,false,$wor_sub->gradinggrade
));
239 fwrite ($bf,full_tag("FINALGRADE",6,false,$wor_sub->finalgrade
));
240 fwrite ($bf,full_tag("LATE",6,false,$wor_sub->late
));
241 fwrite ($bf,full_tag("NASSESSMENTS",6,false,$wor_sub->nassessments
));
242 //Now we backup workshop assessments
243 $status = backup_workshop_assessments($bf,$preferences,$workshop,$wor_sub->id
);
245 $status =fwrite ($bf,end_tag("SUBMISSION",5,true));
248 $status =fwrite ($bf,end_tag("SUBMISSIONS",4,true));
253 //Backup workshop_assessments contents (executed from backup_workshop_submissions)
254 function backup_workshop_assessments ($bf,$preferences,$workshop,$submission) {
260 //NOTE: I think that the workshopid can go out (submissionid is a good unique fk), but mantain it, as is in db !!
261 $workshop_assessments = get_records_sql("SELECT * from {$CFG->prefix}workshop_assessments a
262 WHERE a.workshopid = '$workshop' and a.submissionid = '$submission'
265 //If there is workshop_assessments
266 if ($workshop_assessments) {
268 $status =fwrite ($bf,start_tag("ASSESSMENTS",6,true));
269 //Iterate over each assessment
270 foreach ($workshop_assessments as $wor_ass) {
272 $status =fwrite ($bf,start_tag("ASSESSMENT",7,true));
273 //Print assessment contents
274 fwrite ($bf,full_tag("ID",8,false,$wor_ass->id
));
275 fwrite ($bf,full_tag("USERID",8,false,$wor_ass->userid
));
276 fwrite ($bf,full_tag("TIMECREATED",8,false,$wor_ass->timecreated
));
277 fwrite ($bf,full_tag("TIMEGRADED",8,false,$wor_ass->timegraded
));
278 fwrite ($bf,full_tag("TIMEAGREED",8,false,$wor_ass->timeagreed
));
279 fwrite ($bf,full_tag("GRADE",8,false,$wor_ass->grade
));
280 fwrite ($bf,full_tag("GRADINGGRADE",8,false,$wor_ass->gradinggrade
));
281 fwrite ($bf,full_tag("TEACHERGRADED",8,false,$wor_ass->teachergraded
));
282 fwrite ($bf,full_tag("MAILED",8,false,$wor_ass->mailed
));
283 fwrite ($bf,full_tag("RESUBMISSION",8,false,$wor_ass->resubmission
));
284 fwrite ($bf,full_tag("DONOTUSE",8,false,$wor_ass->donotuse
));
285 fwrite ($bf,full_tag("GENERALCOMMENT",8,false,$wor_ass->generalcomment
));
286 fwrite ($bf,full_tag("TEACHERCOMMENT",8,false,$wor_ass->teachercomment
));
287 //Now we backup workshop comments
288 $status = backup_workshop_comments($bf,$preferences,$workshop,$wor_ass->id
);
289 //Now we backup workshop grades
290 $status = backup_workshop_grades($bf,$preferences,$workshop,$wor_ass->id
);
292 $status =fwrite ($bf,end_tag("ASSESSMENT",7,true));
295 $status =fwrite ($bf,end_tag("ASSESSMENTS",6,true));
300 //Backup workshop_comments contents (executed from backup_workshop_assessments)
301 function backup_workshop_comments ($bf,$preferences,$workshop,$assessmentid) {
307 //NOTE: I think that the workshopid can go out (assessmentid is a good unique fk), but mantain it, as is in db !!
308 $workshop_comments = get_records_sql("SELECT * from {$CFG->prefix}workshop_comments c
309 WHERE c.workshopid = '$workshop' and c.assessmentid = '$assessmentid'
312 //If there is workshop_comments
313 if ($workshop_comments) {
315 $status =fwrite ($bf,start_tag("COMMENTS",8,true));
316 //Iterate over each comment
317 foreach ($workshop_comments as $wor_com) {
319 $status =fwrite ($bf,start_tag("COMMENT",9,true));
320 //Print comment contents
321 fwrite ($bf,full_tag("USERID",10,false,$wor_com->userid
));
322 fwrite ($bf,full_tag("TIMECREATED",10,false,$wor_com->timecreated
));
323 fwrite ($bf,full_tag("MAILED",10,false,$wor_com->mailed
));
324 fwrite ($bf,full_tag("COMMENT_TEXT",10,false,$wor_com->comments
));
326 $status =fwrite ($bf,end_tag("COMMENT",9,true));
329 $status =fwrite ($bf,end_tag("COMMENTS",8,true));
334 //Backup workshop_grades contents (executed from backup_workshop_assessments)
335 function backup_workshop_grades ($bf,$preferences,$workshop,$assessmentid) {
341 //NOTE: I think that the workshopid can go out (assessmentid is a good unique fk), but mantain it, as is in db !!
342 $workshop_grades = get_records_sql("SELECT * from {$CFG->prefix}workshop_grades g
343 WHERE g.workshopid = '$workshop' and g.assessmentid = '$assessmentid'
344 ORDER BY g.elementno");
346 //If there is workshop_grades
347 if ($workshop_grades) {
349 $status =fwrite ($bf,start_tag("GRADES",8,true));
350 //Iterate over each grade
351 foreach ($workshop_grades as $wor_gra) {
353 $status =fwrite ($bf,start_tag("GRADE",9,true));
354 //Print grade contents
355 fwrite ($bf,full_tag("ELEMENTNO",10,false,$wor_gra->elementno
));
356 fwrite ($bf,full_tag("FEEDBACK",10,false,$wor_gra->feedback
));
357 fwrite ($bf,full_tag("GRADE_VALUE",10,false,$wor_gra->grade
));
359 $status =fwrite ($bf,end_tag("GRADE",9,true));
362 $status =fwrite ($bf,end_tag("GRADES",8,true));
368 //Backup workshop files because we've selected to backup user info
369 //and files are user info's level
370 function backup_workshop_files($bf,$preferences) {
376 //First we check to moddata exists and create it as necessary
377 //in temp/backup/$backup_code dir
378 $status = check_and_create_moddata_dir($preferences->backup_unique_code
);
379 //Now copy the workshop dir
381 //Only if it exists !! Thanks to Daniel Miksik.
382 if (is_dir($CFG->dataroot
."/".$preferences->backup_course
."/".$CFG->moddata
."/workshop")) {
383 $status = backup_copy_file($CFG->dataroot
."/".$preferences->backup_course
."/".$CFG->moddata
."/workshop/",
384 $CFG->dataroot
."/temp/backup/".$preferences->backup_unique_code
."/moddata/workshop/");
392 function backup_workshop_files_instance($bf,$preferences,$instanceid,$ws) {
397 //First we check to moddata exists and create it as necessary
398 //in temp/backup/$backup_code dir
399 $status = check_and_create_moddata_dir($preferences->backup_unique_code
);
400 $status = check_dir_exists($CFG->dataroot
."/temp/backup/".$preferences->backup_unique_code
."/moddata/workshop/",true);
401 //Now copy the forum dir
403 foreach ($ws as $submission) {
404 //Only if it exists !! Thanks to Daniel Miksik.
405 if (is_dir($CFG->dataroot
."/".$preferences->backup_course
."/".$CFG->moddata
."/workshop/".$submission->id
)) {
406 $status = backup_copy_file($CFG->dataroot
."/".$preferences->backup_course
."/".$CFG->moddata
."/workshop/".$submission->id
,
407 $CFG->dataroot
."/temp/backup/".$preferences->backup_unique_code
."/moddata/workshop/".$submission->id
);
415 function workshop_check_backup_mods_instances($instance,$backup_unique_code) {
416 //First the course data
417 $info[$instance->id
.'0'][0] = $instance->name
;
418 $info[$instance->id
.'0'][1] = '';
419 //Now, if requested, the user_data
420 if (!empty($instance->userdata
)) {
421 $info[$instance->id
.'1'][0] = get_string("submissions","workshop");
422 if ($ids = workshop_submission_ids_by_instance ($instance->id
)) {
423 $info[$instance->id
.'1'][1] = count($ids);
425 $info[$instance->id
.'1'][1] = 0;
432 //Return an array of info (name,value)
433 function workshop_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
434 if (!empty($instances) && is_array($instances) && count($instances)) {
436 foreach ($instances as $id => $instance) {
437 $info +
= workshop_check_backup_mods_instances($instance,$backup_unique_code);
441 //First the course data
442 $info[0][0] = get_string("modulenameplural","workshop");
443 if ($ids = workshop_ids ($course)) {
444 $info[0][1] = count($ids);
449 //Now, if requested, the user_data
451 $info[1][0] = get_string("submissions","workshop");
452 if ($ids = workshop_submission_ids_by_course ($course)) {
453 $info[1][1] = count($ids);
461 //Return a content encoded to support interactivities linking. Every module
462 //should have its own. They are called automatically from the backup procedure.
463 function workshop_encode_content_links ($content,$preferences) {
467 $base = preg_quote($CFG->wwwroot
,"/");
469 //Link to the list of workshops
470 $buscar="/(".$base."\/mod\/workshop\/index.php\?id\=)([0-9]+)/";
471 $result= preg_replace($buscar,'$@WORKSHOPINDEX*$2@$',$content);
473 //Link to workshop view by moduleid
474 $buscar="/(".$base."\/mod\/workshop\/view.php\?id\=)([0-9]+)/";
475 $result= preg_replace($buscar,'$@WORKSHOPVIEWBYID*$2@$',$result);
480 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
482 //Returns an array of workshop id
483 function workshop_ids ($course) {
487 return get_records_sql ("SELECT w.id, w.course
488 FROM {$CFG->prefix}workshop w
489 WHERE w.course = '$course'");
492 //Returns an array of workshop_submissions id
493 function workshop_submission_ids_by_course ($course) {
497 return get_records_sql ("SELECT s.id , s.workshopid
498 FROM {$CFG->prefix}workshop_submissions s,
499 {$CFG->prefix}workshop w
500 WHERE w.course = '$course' AND
501 s.workshopid = w.id");
504 function workshop_submission_ids_by_instance ($instanceid) {
508 return get_records_sql ("SELECT s.id , s.workshopid
509 FROM {$CFG->prefix}workshop_submissions s
510 WHERE s.workshopid = $instanceid");