MDL-21695 adding help string
[moodle.git] / mod / lesson / backuplib.php
blobb9fc75c365099397762a014ced189bcb0a46cc29
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Lesson's backup routine
21 * @package lesson
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 **/
26 // This is the "graphical" structure of the lesson mod:
28 // lesson ----------------------------|--------------------------|--------------------------|
29 // (CL,pk->id) | | |
30 // | | | |
31 // | lesson_grades lesson_high_scores lesson_timer
32 // | (UL, pk->id,fk->lessonid) (UL, pk->id,fk->lessonid) (UL, pk->id,fk->lessonid)
33 // |
34 // |
35 // lesson_pages---------------------------|
36 // (CL,pk->id,fk->lessonid) |
37 // | |
38 // | lesson_branch
39 // | (UL, pk->id,fk->pageid)
40 // lesson_answers
41 // (CL,pk->id,fk->pageid)
42 // |
43 // |
44 // |
45 // lesson_attempts
46 // (UL,pk->id,fk->answerid)
48 // Meaning: pk->primary key field of the table
49 // fk->foreign key to link with parent
50 // nt->nested field (recursive data)
51 // CL->course level info
52 // UL->user level info
53 // files->table may have files)
55 //-----------------------------------------------------------
57 /**
58 * This function executes all the backup procedure about this mod
60 function lesson_backup_mods($bf, $preferences) {
62 global $CFG, $DB;
64 $status = true;
66 //Iterate over lesson table
67 $lessons = $DB->get_records("lesson", array ("course" => $preferences->backup_course), "id");
68 if ($lessons) {
69 foreach ($lessons as $lesson) {
70 if (backup_mod_selected($preferences,'lesson',$lesson->id)) {
71 $status = lesson_backup_one_mod($bf,$preferences,$lesson);
75 return $status;
78 function lesson_backup_one_mod($bf,$preferences,$lesson) {
80 global $CFG, $DB;
82 if (is_numeric($lesson)) {
83 $lesson = $DB->get_record('lesson',array ('id' => $lesson));
86 $status = true;
88 //Start mod
89 fwrite ($bf,start_tag("MOD",3,true));
90 //Print lesson data
91 fwrite ($bf,full_tag("ID",4,false,$lesson->id));
92 fwrite ($bf,full_tag("MODTYPE",4,false,"lesson"));
93 fwrite ($bf,full_tag("NAME",4,false,$lesson->name));
94 fwrite ($bf,full_tag("PRACTICE",4,false,$lesson->practice));
95 fwrite ($bf,full_tag("MODATTEMPTS",4,false,$lesson->modattempts));
96 fwrite ($bf,full_tag("USEPASSWORD",4,false,$lesson->usepassword));
97 fwrite ($bf,full_tag("PASSWORD",4,false,$lesson->password));
98 fwrite ($bf,full_tag("DEPENDENCY",4,false,$lesson->dependency));
99 fwrite ($bf,full_tag("CONDITIONS",4,false,$lesson->conditions));
100 fwrite ($bf,full_tag("GRADE",4,false,$lesson->grade));
101 fwrite ($bf,full_tag("CUSTOM",4,false,$lesson->custom));
102 fwrite ($bf,full_tag("ONGOING",4,false,$lesson->ongoing));
103 fwrite ($bf,full_tag("USEMAXGRADE",4,false,$lesson->usemaxgrade));
104 fwrite ($bf,full_tag("MAXANSWERS",4,false,$lesson->maxanswers));
105 fwrite ($bf,full_tag("MAXATTEMPTS",4,false,$lesson->maxattempts));
106 fwrite ($bf,full_tag("REVIEW",4,false,$lesson->review));
107 fwrite ($bf,full_tag("NEXTPAGEDEFAULT",4,false,$lesson->nextpagedefault));
108 fwrite ($bf,full_tag("FEEDBACK",4,false,$lesson->feedback));
109 fwrite ($bf,full_tag("MINQUESTIONS",4,false,$lesson->minquestions));
110 fwrite ($bf,full_tag("MAXPAGES",4,false,$lesson->maxpages));
111 fwrite ($bf,full_tag("TIMED",4,false,$lesson->timed));
112 fwrite ($bf,full_tag("MAXTIME",4,false,$lesson->maxtime));
113 fwrite ($bf,full_tag("RETAKE",4,false,$lesson->retake));
114 fwrite ($bf,full_tag("ACTIVITYLINK",4,false,$lesson->activitylink));
115 fwrite ($bf,full_tag("MEDIAFILE",4,false,$lesson->mediafile));
116 fwrite ($bf,full_tag("MEDIAHEIGHT",4,false,$lesson->mediaheight));
117 fwrite ($bf,full_tag("MEDIAWIDTH",4,false,$lesson->mediawidth));
118 fwrite ($bf,full_tag("MEDIACLOSE",4,false,$lesson->mediaclose));
119 fwrite ($bf,full_tag("SLIDESHOW",4,false,$lesson->slideshow));
120 fwrite ($bf,full_tag("WIDTH",4,false,$lesson->width));
121 fwrite ($bf,full_tag("HEIGHT",4,false,$lesson->height));
122 fwrite ($bf,full_tag("BGCOLOR",4,false,$lesson->bgcolor));
123 fwrite ($bf,full_tag("DISPLAYLEFT",4,false,$lesson->displayleft));
124 fwrite ($bf,full_tag("DISPLAYLEFTIF",4,false,$lesson->displayleftif));
125 fwrite ($bf,full_tag("PROGRESSBAR",4,false,$lesson->progressbar));
126 fwrite ($bf,full_tag("SHOWHIGHSCORES",4,false,$lesson->highscores));
127 fwrite ($bf,full_tag("MAXHIGHSCORES",4,false,$lesson->maxhighscores));
128 fwrite ($bf,full_tag("AVAILABLE",4,false,$lesson->available));
129 fwrite ($bf,full_tag("DEADLINE",4,false,$lesson->deadline));
130 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$lesson->timemodified));
132 //Now we backup lesson pages
133 $status = backup_lesson_pages($bf,$preferences,$lesson->id);
134 //if we've selected to backup users info, then backup grades, high scores, and timer info
135 if ($status) {
136 if (backup_userdata_selected($preferences,'lesson',$lesson->id)) {
137 if(!backup_lesson_grades($bf, $preferences, $lesson->id)) {
138 return false;
140 if (!backup_lesson_high_scores($bf, $preferences, $lesson->id)) {
141 return false;
143 if (!backup_lesson_timer($bf, $preferences, $lesson->id)) {
144 return false;
147 //End mod
148 $status =fwrite ($bf,end_tag("MOD",3,true));
151 return $status;
154 //Backup lesson_pages contents (executed from lesson_backup_mods)
155 function backup_lesson_pages ($bf, $preferences, $lessonid) {
157 global $CFG, $DB;
159 $status = true;
161 // run through the pages in their logical order, get the first page
162 $params = array ("lessonid" => $lessonid, "prevpageid" => 0);
163 if ($page = $DB->get_record_select("lesson_pages", "lessonid = :lessonid AND prevpageid = :prevpageid", $params)) {
164 //Write start tag
165 $status =fwrite ($bf,start_tag("PAGES",4,true));
166 //Iterate over each page
167 while (true) {
168 //Start of page
169 $status =fwrite ($bf,start_tag("PAGE",5,true));
170 //Print page contents (prevpageid and nextpageid not needed)
171 fwrite ($bf,full_tag("PAGEID",6,false,$page->id)); // needed to fix (absolute) jumps
172 fwrite ($bf,full_tag("QTYPE",6,false,$page->qtype));
173 fwrite ($bf,full_tag("QOPTION",6,false,$page->qoption));
174 fwrite ($bf,full_tag("LAYOUT",6,false,$page->layout));
175 fwrite ($bf,full_tag("DISPLAY",6,false,$page->display));
176 fwrite ($bf,full_tag("TIMECREATED",6,false,$page->timecreated));
177 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$page->timemodified));
178 fwrite ($bf,full_tag("TITLE",6,false,$page->title));
179 fwrite ($bf,full_tag("CONTENTS",6,false,$page->contents));
180 //Now we backup lesson answers for this page
181 $status = backup_lesson_answers($bf, $preferences, $page->id);
182 // backup branch table info for branch tables.
183 if ($status && backup_userdata_selected($preferences,'lesson',$lessonid)) {
184 if (!backup_lesson_branch($bf, $preferences, $page->id)) {
185 return false;
188 //End of page
189 $status =fwrite ($bf,end_tag("PAGE",5,true));
190 // move to the next (logical) page
191 if ($page->nextpageid) {
192 if (!$page = $DB->get_record("lesson_pages", array ("id" => $page->nextpageid))) {
193 print_error('cannotfindnextpage', 'lesson');
195 } else {
196 // last page reached
197 break;
201 //Write end tag
202 $status =fwrite ($bf,end_tag("PAGES",4,true));
204 return $status;
207 //Backup lesson_answers contents (executed from backup_lesson_pages)
208 function backup_lesson_answers($bf,$preferences,$pageno) {
210 global $CFG, $DB;
212 $status = true;
214 // get the answers in a set order, the id order
215 $lesson_answers = $DB->get_records("lesson_answers", array("pageid" => $pageno), "id");
217 //If there is lesson_answers
218 if ($lesson_answers) {
219 //Write start tag
220 $status =fwrite ($bf,start_tag("ANSWERS",6,true));
221 //Iterate over each element
222 foreach ($lesson_answers as $answer) {
223 //Start answer
224 $status =fwrite ($bf,start_tag("ANSWER",7,true));
225 //Print answer contents
226 fwrite ($bf,full_tag("ID",8,false,$answer->id));
227 fwrite ($bf,full_tag("JUMPTO",8,false,$answer->jumpto));
228 fwrite ($bf,full_tag("GRADE",8,false,$answer->grade));
229 fwrite ($bf,full_tag("SCORE",8,false,$answer->score));
230 fwrite ($bf,full_tag("FLAGS",8,false,$answer->flags));
231 fwrite ($bf,full_tag("TIMECREATED",8,false,$answer->timecreated));
232 fwrite ($bf,full_tag("TIMEMODIFIED",8,false,$answer->timemodified));
233 fwrite ($bf,full_tag("ANSWERTEXT",8,false,$answer->answer));
234 fwrite ($bf,full_tag("RESPONSE",8,false,$answer->response));
235 //Now we backup any lesson attempts (if student data required)
236 if (backup_userdata_selected($preferences,'lesson',$answer->lessonid)) {
237 $status = backup_lesson_attempts($bf,$preferences,$answer->id);
239 //End rubric
240 $status =fwrite ($bf,end_tag("ANSWER",7,true));
242 //Write end tag
243 $status =fwrite ($bf,end_tag("ANSWERS",6,true));
245 return $status;
248 //Backup lesson_attempts contents (executed from lesson_backup_answers)
249 function backup_lesson_attempts ($bf,$preferences,$answerid) {
251 global $CFG, $DB;
253 $status = true;
255 $lesson_attempts = $DB->get_records("lesson_attempts", array("answerid" => $answerid));
256 //If there are attempts
257 if ($lesson_attempts) {
258 //Write start tag
259 $status =fwrite ($bf,start_tag("ATTEMPTS",8,true));
260 //Iterate over each attempt
261 foreach ($lesson_attempts as $attempt) {
262 //Start Attempt
263 $status =fwrite ($bf,start_tag("ATTEMPT",9,true));
264 //Print attempt contents
265 fwrite ($bf,full_tag("USERID",10,false,$attempt->userid));
266 fwrite ($bf,full_tag("RETRY",10,false,$attempt->retry));
267 fwrite ($bf,full_tag("CORRECT",10,false,$attempt->correct));
268 fwrite ($bf,full_tag("USERANSWER",10,false,$attempt->useranswer));
269 fwrite ($bf,full_tag("TIMESEEN",10,false,$attempt->timeseen));
270 //End attempt
271 $status =fwrite ($bf,end_tag("ATTEMPT",9,true));
273 //Write end tag
274 $status =fwrite ($bf,end_tag("ATTEMPTS",8,true));
276 return $status;
280 //Backup lesson_grades contents (executed from backup_lesson_mods)
281 function backup_lesson_grades ($bf,$preferences,$lessonid) {
283 global $CFG, $DB;
285 $status = true;
287 $grades = $DB->get_records("lesson_grades", array("lessonid" => $lessonid));
289 //If there is grades
290 if ($grades) {
291 //Write start tag
292 $status =fwrite ($bf,start_tag("GRADES",4,true));
293 //Iterate over each grade
294 foreach ($grades as $grade) {
295 //Start grade
296 $status =fwrite ($bf,start_tag("GRADE",5,true));
297 //Print grade contents
298 fwrite ($bf,full_tag("USERID",6,false,$grade->userid));
299 fwrite ($bf,full_tag("GRADE_VALUE",6,false,$grade->grade));
300 fwrite ($bf,full_tag("LATE",6,false,$grade->late));
301 fwrite ($bf,full_tag("COMPLETED",6,false,$grade->completed));
302 //End grade
303 $status =fwrite ($bf,end_tag("GRADE",5,true));
305 //Write end tag
306 $status =fwrite ($bf,end_tag("GRADES",4,true));
308 return $status;
311 //Backup lesson_branch contents (executed from backup_lesson_pages)
312 function backup_lesson_branch($bf,$preferences,$pageno) {
314 global $CFG, $DB;
316 $status = true;
318 // get the branches in a set order, the id order
319 $lesson_branch = $DB->get_records("lesson_branch", array("pageid" => $pageno), "id");
321 //If there is lesson_branch
322 if ($lesson_branch) {
323 //Write start tag
324 $status =fwrite ($bf,start_tag("BRANCHES",6,true));
325 //Iterate over each element
326 foreach ($lesson_branch as $branch) {
327 //Start branch
328 $status =fwrite ($bf,start_tag("BRANCH",7,true));
329 //Print branch contents
330 fwrite ($bf,full_tag("USERID",8,false,$branch->userid));
331 fwrite ($bf,full_tag("RETRY",8,false,$branch->retry));
332 fwrite ($bf,full_tag("FLAG",8,false,$branch->flag));
333 fwrite ($bf,full_tag("TIMESEEN",8,false,$branch->timeseen));
334 // END BRANCH
335 $status =fwrite ($bf,end_tag("BRANCH",7,true));
337 //Write end tag
338 $status =fwrite ($bf,end_tag("BRANCHES",6,true));
340 return $status;
343 //Backup lesson_timer contents (executed from backup_lesson_mods)
344 function backup_lesson_timer ($bf,$preferences,$lessonid) {
346 global $CFG, $DB;
348 $status = true;
350 $times = $DB->get_records("lesson_timer", array("lessonid" => $lessonid));
352 //If there is times
353 if ($times) {
354 //Write start tag
355 $status =fwrite ($bf,start_tag("TIMES",4,true));
356 //Iterate over each time
357 foreach ($times as $time) {
358 //Start time
359 $status =fwrite ($bf,start_tag("TIME",5,true));
360 //Print time contents
361 fwrite ($bf,full_tag("USERID",6,false,$time->userid));
362 fwrite ($bf,full_tag("STARTTIME",6,false,$time->starttime));
363 fwrite ($bf,full_tag("LESSONTIME",6,false,$time->lessontime));
364 //End time
365 $status =fwrite ($bf,end_tag("TIME",5,true));
367 //Write end tag
368 $status =fwrite ($bf,end_tag("TIMES",4,true));
370 return $status;
373 // backup lesson_high_score contents (executed from backup_lesson_mods)
374 function backup_lesson_high_scores($bf, $preferences, $lessonid) {
375 global $CFG, $DB;
377 $status = true;
379 $highscores = $DB->get_records("lesson_high_scores", array("lessonid" => $lessonid));
381 //If there is highscores
382 if ($highscores) {
383 //Write start tag
384 $status =fwrite ($bf,start_tag("HIGHSCORES",4,true));
385 //Iterate over each highscore
386 foreach ($highscores as $highscore) {
387 //Start highscore
388 $status =fwrite ($bf,start_tag("HIGHSCORE",5,true));
389 //Print highscore contents
390 fwrite ($bf,full_tag("USERID",6,false,$highscore->userid));
391 fwrite ($bf,full_tag("GRADEID",6,false,$highscore->gradeid));
392 fwrite ($bf,full_tag("NICKNAME",6,false,$highscore->nickname));
393 //End highscore
394 $status =fwrite ($bf,end_tag("HIGHSCORE",5,true));
396 //Write end tag
397 $status =fwrite ($bf,end_tag("HIGHSCORES",4,true));
399 return $status;
402 //Return an array of info (name,value)
403 function lesson_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
404 if (!empty($instances) && is_array($instances) && count($instances)) {
405 $info = array();
406 foreach ($instances as $id => $instance) {
407 $info += lesson_check_backup_mods_instances($instance,$backup_unique_code);
409 return $info;
411 //First the course data
412 $info[0][0] = get_string("modulenameplural","lesson");
413 if ($ids = lesson_ids($course)) {
414 $info[0][1] = count($ids);
415 } else {
416 $info[0][1] = 0;
419 //Now, if requested, the user_data
420 if ($user_data) {
421 $info[1][0] = get_string("attempts","lesson");
422 if ($ids = lesson_attempts_ids_by_course ($course)) {
423 $info[1][1] = count($ids);
424 } else {
425 $info[1][1] = 0;
428 return $info;
431 //Return an array of info (name,value)
432 function lesson_check_backup_mods_instances($instance,$backup_unique_code) {
433 //First the course data
434 $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
435 $info[$instance->id.'0'][1] = '';
437 //Now, if requested, the user_data
438 if (!empty($instance->userdata)) {
439 $info[$instance->id.'1'][0] = get_string("attempts","lesson");
440 if ($ids = lesson_attempts_ids_by_instance ($instance->id)) {
441 $info[$instance->id.'1'][1] = count($ids);
442 } else {
443 $info[$instance->id.'1'][1] = 0;
446 return $info;
449 //Return a content encoded to support interactivities linking. Every module
450 //should have its own. They are called automatically from the backup procedure.
451 function lesson_encode_content_links ($content,$preferences) {
453 global $CFG;
455 $base = preg_quote($CFG->wwwroot,"/");
457 //Link to the list of lessons
458 $buscar="/(".$base."\/mod\/lesson\/index.php\?id\=)([0-9]+)/";
459 $result= preg_replace($buscar,'$@LESSONINDEX*$2@$',$content);
461 //Link to lesson view by moduleid
462 $buscar="/(".$base."\/mod\/lesson\/view.php\?id\=)([0-9]+)/";
463 $result= preg_replace($buscar,'$@LESSONVIEWBYID*$2@$',$result);
465 return $result;
468 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
470 //Returns an array of lesson id
471 function lesson_ids ($course) {
473 global $CFG, $DB;
475 $params = array ("course" => $course);
476 return $DB->get_records_sql ("SELECT l.id, l.course
477 FROM {lesson} l
478 WHERE l.course = :course", $params);
481 //Returns an array of lesson_submissions id
482 function lesson_attempts_ids_by_course ($course) {
484 global $CFG, $DB;
486 $params = array ("course" => $course);
487 return $DB->get_records_sql ("SELECT a.id , a.lessonid
488 FROM {lesson_attempts} a,
489 {lesson} l
490 WHERE l.course = :course AND
491 a.lessonid = l.id", $params);
494 //Returns an array of lesson_submissions id
495 function lesson_attempts_ids_by_instance ($instanceid) {
497 global $CFG, $DB;
499 $params = array ("lessonid" => $instanceid);
500 return $DB->get_records_sql ("SELECT a.id , a.lessonid
501 FROM {lesson_attempts} a
502 WHERE a.lessonid = :lessonid", $params);