MDL-14945
[moodle-linuxchix.git] / mod / workshop / backuplib.php
blobed4b37b7f822e3ed9dd00f4d4831ff33fc9e7bac
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 backup procedure about this mod
39 function workshop_backup_mods($bf,$preferences) {
41 global $CFG;
43 $status = true;
45 //Iterate over workshop table
46 $workshops = get_records ("workshop","course",$preferences->backup_course,"id");
47 if ($workshops) {
48 foreach ($workshops as $workshop) {
49 if (backup_mod_selected($preferences,'workshop',$workshop->id)) {
50 $status = workshop_backup_one_mod($bf,$preferences,$workshop);
55 return $status;
58 function workshop_backup_one_mod($bf,$preferences,$workshop) {
60 $status = true;
62 if (is_numeric($workshop)) {
63 $workshop = get_record('workshop','id',$workshop);
65 $instanceid = $workshop->id;
67 //Start mod
68 fwrite ($bf,start_tag("MOD",3,true));
69 //Print workshop data
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)) {
106 $ws = array();
107 $status = backup_workshop_submissions($bf,$preferences,$workshop->id,$ws);
108 $status = backup_workshop_files_instance($bf,$preferences,$workshop->id,$ws);
111 //End mod
112 $status =fwrite ($bf,end_tag("MOD",3,true));
114 return $status;
117 //Backup workshop_elements contents (executed from workshop_backup_mods)
118 function backup_workshop_elements ($bf,$preferences,$workshop) {
120 global $CFG;
122 $status = true;
124 $workshop_elements = get_records("workshop_elements","workshopid",$workshop,"id");
125 //If there is workshop_elements
126 if ($workshop_elements) {
127 //Write start tag
128 $status =fwrite ($bf,start_tag("ELEMENTS",4,true));
129 //Iterate over each element
130 foreach ($workshop_elements as $wor_ele) {
131 //Start element
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);
145 //End element
146 $status =fwrite ($bf,end_tag("ELEMENT",5,true));
148 //Write end tag
149 $status =fwrite ($bf,end_tag("ELEMENTS",4,true));
151 return $status;
154 //Backup workshop_rubrics contents (executed from backup_workshop_elements)
155 function backup_workshop_rubrics ($bf,$preferences,$workshop,$elementno) {
157 global $CFG;
159 $status = true;
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) {
167 //Write start tag
168 $status =fwrite ($bf,start_tag("RUBRICS",6,true));
169 //Iterate over each element
170 foreach ($workshop_rubrics as $wor_rub) {
171 //Start rubric
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));
176 //End rubric
177 $status =fwrite ($bf,end_tag("RUBRIC",7,true));
179 //Write end tag
180 $status =fwrite ($bf,end_tag("RUBRICS",6,true));
182 return $status;
185 //Backup workshop_stockcomments contents (executed from backup_workshop_elements)
186 function backup_workshop_stockcomments ($bf,$preferences,$workshop,$elementno) {
188 global $CFG;
190 $status = true;
192 $workshop_stockcomments = get_records_sql("SELECT * from {$CFG->prefix}workshop_stockcomments c
193 WHERE c.workshopid = '$workshop' and c.elementno = '$elementno'
194 ORDER BY c.id");
196 //If there is workshop_stockcomments
197 if ($workshop_stockcomments) {
198 //Write start tag
199 $status =fwrite ($bf,start_tag("STOCKCOMMENTS",6,true));
200 //Iterate over each comment
201 foreach ($workshop_stockcomments as $wor_com) {
202 //Start comment
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));
206 //End comment
207 $status =fwrite ($bf,end_tag("STOCKCOMMENT",7,true));
209 //Write end tag
210 $status =fwrite ($bf,end_tag("STOCKCOMMENTS",6,true));
212 return $status;
215 //Backup workshop_submissions contents (executed from workshop_backup_mods)
216 function backup_workshop_submissions ($bf,$preferences,$workshop,&$workshop_submissions) {
218 global $CFG;
220 $status = true;
222 $workshop_submissions = get_records("workshop_submissions","workshopid",$workshop,"id");
223 //If there is submissions
224 if ($workshop_submissions) {
225 //Write start tag
226 $status =fwrite ($bf,start_tag("SUBMISSIONS",4,true));
227 //Iterate over each submission
228 foreach ($workshop_submissions as $wor_sub) {
229 //Start submission
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);
244 //End submission
245 $status =fwrite ($bf,end_tag("SUBMISSION",5,true));
247 //Write end tag
248 $status =fwrite ($bf,end_tag("SUBMISSIONS",4,true));
250 return $status;
253 //Backup workshop_assessments contents (executed from backup_workshop_submissions)
254 function backup_workshop_assessments ($bf,$preferences,$workshop,$submission) {
256 global $CFG;
258 $status = true;
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'
263 ORDER BY a.id");
265 //If there is workshop_assessments
266 if ($workshop_assessments) {
267 //Write start tag
268 $status =fwrite ($bf,start_tag("ASSESSMENTS",6,true));
269 //Iterate over each assessment
270 foreach ($workshop_assessments as $wor_ass) {
271 //Start assessment
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);
291 //End assessment
292 $status =fwrite ($bf,end_tag("ASSESSMENT",7,true));
294 //Write end tag
295 $status =fwrite ($bf,end_tag("ASSESSMENTS",6,true));
297 return $status;
300 //Backup workshop_comments contents (executed from backup_workshop_assessments)
301 function backup_workshop_comments ($bf,$preferences,$workshop,$assessmentid) {
303 global $CFG;
305 $status = true;
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'
310 ORDER BY c.id");
312 //If there is workshop_comments
313 if ($workshop_comments) {
314 //Write start tag
315 $status =fwrite ($bf,start_tag("COMMENTS",8,true));
316 //Iterate over each comment
317 foreach ($workshop_comments as $wor_com) {
318 //Start comment
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));
325 //End comment
326 $status =fwrite ($bf,end_tag("COMMENT",9,true));
328 //Write end tag
329 $status =fwrite ($bf,end_tag("COMMENTS",8,true));
331 return $status;
334 //Backup workshop_grades contents (executed from backup_workshop_assessments)
335 function backup_workshop_grades ($bf,$preferences,$workshop,$assessmentid) {
337 global $CFG;
339 $status = true;
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) {
348 //Write start tag
349 $status =fwrite ($bf,start_tag("GRADES",8,true));
350 //Iterate over each grade
351 foreach ($workshop_grades as $wor_gra) {
352 //Start grade
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));
358 //End comment
359 $status =fwrite ($bf,end_tag("GRADE",9,true));
361 //Write end tag
362 $status =fwrite ($bf,end_tag("GRADES",8,true));
364 return $status;
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) {
372 global $CFG;
374 $status = true;
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
380 if ($status) {
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/");
388 return $status;
392 function backup_workshop_files_instance($bf,$preferences,$instanceid,$ws) {
393 global $CFG;
395 $status = true;
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
402 if ($status) {
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);
412 return $status;
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);
424 } else {
425 $info[$instance->id.'1'][1] = 0;
428 return $info;
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)) {
435 $info = array();
436 foreach ($instances as $id => $instance) {
437 $info += workshop_check_backup_mods_instances($instance,$backup_unique_code);
439 return $info;
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);
445 } else {
446 $info[0][1] = 0;
449 //Now, if requested, the user_data
450 if ($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);
454 } else {
455 $info[1][1] = 0;
458 return $info;
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) {
465 global $CFG;
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);
477 return $result;
480 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
482 //Returns an array of workshop id
483 function workshop_ids ($course) {
485 global $CFG;
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) {
495 global $CFG;
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) {
506 global $CFG;
508 return get_records_sql ("SELECT s.id , s.workshopid
509 FROM {$CFG->prefix}workshop_submissions s
510 WHERE s.workshopid = $instanceid");