Incorrect variable name used for parameter.
[moodle-linuxchix.git] / mod / lesson / backuplib.php
blob317ad30f82cf583b56df5c9b55792eb31e0315b1
1 <?PHP //$Id$
3 //This php script contains all the stuff to backup/restore
4 //lesson mods
6 //This is the "graphical" structure of the lesson mod:
7 //
8 // lesson_default lesson ----------------------------|--------------------------|--------------------------|
9 // (UL, pk->id,fk->courseid) (CL,pk->id) | | |
10 // | | | |
11 // | lesson_grades lesson_high_scores lesson_timer
12 // | (UL, pk->id,fk->lessonid) (UL, pk->id,fk->lessonid) (UL, pk->id,fk->lessonid)
13 // |
14 // |
15 // lesson_pages---------------------------|
16 // (CL,pk->id,fk->lessonid) |
17 // | |
18 // | lesson_branch
19 // | (UL, pk->id,fk->pageid)
20 // lesson_answers
21 // (CL,pk->id,fk->pageid)
22 // |
23 // |
24 // |
25 // lesson_attempts
26 // (UL,pk->id,fk->answerid)
28 // Meaning: pk->primary key field of the table
29 // fk->foreign key to link with parent
30 // nt->nested field (recursive data)
31 // CL->course level info
32 // UL->user level info
33 // files->table may have files)
35 //-----------------------------------------------------------
37 //This function executes all the backup procedure about this mod
38 function lesson_backup_mods($bf, $preferences) {
40 global $CFG;
42 $status = true;
44 //Iterate over lesson table
45 $lessons = get_records("lesson", "course", $preferences->backup_course, "id");
46 if ($lessons) {
47 foreach ($lessons as $lesson) {
48 //Start mod
49 fwrite ($bf,start_tag("MOD",3,true));
50 //Print lesson data
51 fwrite ($bf,full_tag("ID",4,false,$lesson->id));
52 fwrite ($bf,full_tag("MODTYPE",4,false,"lesson"));
53 fwrite ($bf,full_tag("NAME",4,false,$lesson->name));
54 fwrite ($bf,full_tag("PRACTICE",4,false,$lesson->practice));
55 fwrite ($bf,full_tag("MODATTEMPTS",4,false,$lesson->modattempts));
56 fwrite ($bf,full_tag("PASSWORD",4,false,$lesson->password));
57 fwrite ($bf,full_tag("USEPASSWORD",4,false,$lesson->usepassword));
58 fwrite ($bf,full_tag("GRADE",4,false,$lesson->grade));
59 fwrite ($bf,full_tag("CUSTOM",4,false,$lesson->custom));
60 fwrite ($bf,full_tag("ONGOING",4,false,$lesson->ongoing));
61 fwrite ($bf,full_tag("USEMAXGRADE",4,false,$lesson->usemaxgrade));
62 fwrite ($bf,full_tag("MAXANSWERS",4,false,$lesson->maxanswers));
63 fwrite ($bf,full_tag("MAXATTEMPTS",4,false,$lesson->maxattempts));
64 fwrite ($bf,full_tag("REVIEW",4,false,$lesson->review));
65 fwrite ($bf,full_tag("NEXTPAGEDEFAULT",4,false,$lesson->nextpagedefault));
66 fwrite ($bf,full_tag("MINQUESTIONS",4,false,$lesson->minquestions));
67 fwrite ($bf,full_tag("MAXPAGES",4,false,$lesson->maxpages));
68 fwrite ($bf,full_tag("TIMED",4,false,$lesson->timed));
69 fwrite ($bf,full_tag("MAXTIME",4,false,$lesson->maxtime));
70 fwrite ($bf,full_tag("RETAKE",4,false,$lesson->retake));
71 fwrite ($bf,full_tag("TREE",4,false,$lesson->tree));
72 fwrite ($bf,full_tag("SLIDESHOW",4,false,$lesson->slideshow));
73 fwrite ($bf,full_tag("WIDTH",4,false,$lesson->width));
74 fwrite ($bf,full_tag("HEIGHT",4,false,$lesson->height));
75 fwrite ($bf,full_tag("BGCOLOR",4,false,$lesson->bgcolor));
76 fwrite ($bf,full_tag("DISPLAYLEFT",4,false,$lesson->displayleft));
77 fwrite ($bf,full_tag("SHOWHIGHSCORES",4,false,$lesson->highscores));
78 fwrite ($bf,full_tag("MAXHIGHSCORES",4,false,$lesson->maxhighscores));
79 fwrite ($bf,full_tag("AVAILABLE",4,false,$lesson->available));
80 fwrite ($bf,full_tag("DEADLINE",4,false,$lesson->deadline));
81 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$lesson->timemodified));
83 //Now we backup lesson pages
84 $status = backup_lesson_pages($bf,$preferences,$lesson->id);
85 //if we've selected to backup users info, then backup grades, high scores, and timer info
86 if ($status) {
87 if ($preferences->mods["lesson"]->userinfo) {
88 if(!backup_lesson_grades($bf, $preferences, $lesson->id)) {
89 return false;
91 if (!backup_lesson_high_scores($bf, $preferences, $lesson->id)) {
92 return false;
94 if (!backup_lesson_timer($bf, $preferences, $lesson->id)) {
95 return false;
98 // back up the default for the course. There might not be one, but if there
99 // is, there will only be one.
100 $status = backup_lesson_default($bf,$preferences);
101 //End mod
102 if ($status) {
103 $status =fwrite ($bf,end_tag("MOD",3,true));
108 return $status;
111 //Backup lesson_pages contents (executed from lesson_backup_mods)
112 function backup_lesson_pages ($bf, $preferences, $lessonid) {
114 global $CFG;
116 $status = true;
118 // run through the pages in their logical order, get the first page
119 if ($page = get_record_select("lesson_pages", "lessonid = $lessonid AND prevpageid = 0")) {
120 //Write start tag
121 $status =fwrite ($bf,start_tag("PAGES",4,true));
122 //Iterate over each page
123 while (true) {
124 //Start of page
125 $status =fwrite ($bf,start_tag("PAGE",5,true));
126 //Print page contents (prevpageid and nextpageid not needed)
127 fwrite ($bf,full_tag("PAGEID",6,false,$page->id)); // needed to fix (absolute) jumps
128 fwrite ($bf,full_tag("QTYPE",6,false,$page->qtype));
129 fwrite ($bf,full_tag("QOPTION",6,false,$page->qoption));
130 fwrite ($bf,full_tag("LAYOUT",6,false,$page->layout));
131 fwrite ($bf,full_tag("DISPLAY",6,false,$page->display));
132 fwrite ($bf,full_tag("TIMECREATED",6,false,$page->timecreated));
133 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$page->timemodified));
134 fwrite ($bf,full_tag("TITLE",6,false,$page->title));
135 fwrite ($bf,full_tag("CONTENTS",6,false,$page->contents));
136 //Now we backup lesson answers for this page
137 $status = backup_lesson_answers($bf, $preferences, $page->id);
138 // backup branch table info for branch tables.
139 if ($status && $preferences->mods["lesson"]->userinfo) {
140 if (!backup_lesson_branch($bf, $preferences, $page->id)) {
141 return false;
144 //End of page
145 $status =fwrite ($bf,end_tag("PAGE",5,true));
146 // move to the next (logical) page
147 if ($page->nextpageid) {
148 if (!$page = get_record("lesson_pages", "id", $page->nextpageid)) {
149 error("Lesson Backup: Next page not found!");
151 } else {
152 // last page reached
153 break;
157 //Write end tag
158 $status =fwrite ($bf,end_tag("PAGES",4,true));
160 return $status;
163 //Backup lesson_answers contents (executed from backup_lesson_pages)
164 function backup_lesson_answers($bf,$preferences,$pageno) {
166 global $CFG;
168 $status = true;
170 // get the answers in a set order, the id order
171 $lesson_answers = get_records("lesson_answers", "pageid", $pageno, "id");
173 //If there is lesson_answers
174 if ($lesson_answers) {
175 //Write start tag
176 $status =fwrite ($bf,start_tag("ANSWERS",6,true));
177 //Iterate over each element
178 foreach ($lesson_answers as $answer) {
179 //Start answer
180 $status =fwrite ($bf,start_tag("ANSWER",7,true));
181 //Print answer contents
182 fwrite ($bf,full_tag("ID",8,false,$answer->id));
183 fwrite ($bf,full_tag("JUMPTO",8,false,$answer->jumpto));
184 fwrite ($bf,full_tag("GRADE",8,false,$answer->grade));
185 fwrite ($bf,full_tag("SCORE",8,false,$answer->score));
186 fwrite ($bf,full_tag("FLAGS",8,false,$answer->flags));
187 fwrite ($bf,full_tag("TIMECREATED",8,false,$answer->timecreated));
188 fwrite ($bf,full_tag("TIMEMODIFIED",8,false,$answer->timemodified));
189 fwrite ($bf,full_tag("ANSWERTEXT",8,false,$answer->answer));
190 fwrite ($bf,full_tag("RESPONSE",8,false,$answer->response));
191 //Now we backup any lesson attempts (if student data required)
192 if ($preferences->mods["lesson"]->userinfo) {
193 $status = backup_lesson_attempts($bf,$preferences,$answer->id);
195 //End rubric
196 $status =fwrite ($bf,end_tag("ANSWER",7,true));
198 //Write end tag
199 $status =fwrite ($bf,end_tag("ANSWERS",6,true));
201 return $status;
204 //Backup lesson_attempts contents (executed from lesson_backup_answers)
205 function backup_lesson_attempts ($bf,$preferences,$answerid) {
207 global $CFG;
209 $status = true;
211 $lesson_attempts = get_records("lesson_attempts","answerid", $answerid);
212 //If there are attempts
213 if ($lesson_attempts) {
214 //Write start tag
215 $status =fwrite ($bf,start_tag("ATTEMPTS",8,true));
216 //Iterate over each attempt
217 foreach ($lesson_attempts as $attempt) {
218 //Start Attempt
219 $status =fwrite ($bf,start_tag("ATTEMPT",9,true));
220 //Print attempt contents
221 fwrite ($bf,full_tag("USERID",10,false,$attempt->userid));
222 fwrite ($bf,full_tag("RETRY",10,false,$attempt->retry));
223 fwrite ($bf,full_tag("CORRECT",10,false,$attempt->correct));
224 fwrite ($bf,full_tag("USERANSWER",10,false,$attempt->useranswer));
225 fwrite ($bf,full_tag("TIMESEEN",10,false,$attempt->timeseen));
226 //End attempt
227 $status =fwrite ($bf,end_tag("ATTEMPT",9,true));
229 //Write end tag
230 $status =fwrite ($bf,end_tag("ATTEMPTS",8,true));
232 return $status;
236 //Backup lesson_grades contents (executed from backup_lesson_mods)
237 function backup_lesson_grades ($bf,$preferences,$lessonid) {
239 global $CFG;
241 $status = true;
243 $grades = get_records("lesson_grades", "lessonid", $lessonid);
245 //If there is grades
246 if ($grades) {
247 //Write start tag
248 $status =fwrite ($bf,start_tag("GRADES",4,true));
249 //Iterate over each grade
250 foreach ($grades as $grade) {
251 //Start grade
252 $status =fwrite ($bf,start_tag("GRADE",5,true));
253 //Print grade contents
254 fwrite ($bf,full_tag("USERID",6,false,$grade->userid));
255 fwrite ($bf,full_tag("GRADE_VALUE",6,false,$grade->grade));
256 fwrite ($bf,full_tag("LATE",6,false,$grade->late));
257 fwrite ($bf,full_tag("COMPLETED",6,false,$grade->completed));
258 //End grade
259 $status =fwrite ($bf,end_tag("GRADE",5,true));
261 //Write end tag
262 $status =fwrite ($bf,end_tag("GRADES",4,true));
264 return $status;
267 //Backup lesson_branch contents (executed from backup_lesson_pages)
268 function backup_lesson_branch($bf,$preferences,$pageno) {
270 global $CFG;
272 $status = true;
274 // get the branches in a set order, the id order
275 $lesson_branch = get_records("lesson_branch", "pageid", $pageno, "id");
277 //If there is lesson_branch
278 if ($lesson_branch) {
279 //Write start tag
280 $status =fwrite ($bf,start_tag("BRANCHES",6,true));
281 //Iterate over each element
282 foreach ($lesson_branch as $branch) {
283 //Start branch
284 $status =fwrite ($bf,start_tag("BRANCH",7,true));
285 //Print branch contents
286 fwrite ($bf,full_tag("USERID",8,false,$branch->userid));
287 fwrite ($bf,full_tag("RETRY",8,false,$branch->retry));
288 fwrite ($bf,full_tag("FLAG",8,false,$branch->flag));
289 fwrite ($bf,full_tag("TIMESEEN",8,false,$branch->timeseen));
290 // END BRANCH
291 $status =fwrite ($bf,end_tag("BRANCH",7,true));
293 //Write end tag
294 $status =fwrite ($bf,end_tag("BRANCHES",6,true));
296 return $status;
299 //Backup lesson_timer contents (executed from backup_lesson_mods)
300 function backup_lesson_timer ($bf,$preferences,$lessonid) {
302 global $CFG;
304 $status = true;
306 $times = get_records("lesson_timer", "lessonid", $lessonid);
308 //If there is times
309 if ($times) {
310 //Write start tag
311 $status =fwrite ($bf,start_tag("TIMES",4,true));
312 //Iterate over each time
313 foreach ($times as $time) {
314 //Start time
315 $status =fwrite ($bf,start_tag("TIME",5,true));
316 //Print time contents
317 fwrite ($bf,full_tag("USERID",6,false,$time->userid));
318 fwrite ($bf,full_tag("STARTTIME",6,false,$time->starttime));
319 fwrite ($bf,full_tag("LESSONTIME",6,false,$time->lessontime));
320 //End time
321 $status =fwrite ($bf,end_tag("TIME",5,true));
323 //Write end tag
324 $status =fwrite ($bf,end_tag("TIMES",4,true));
326 return $status;
329 // backup lesson_high_score contents (executed from backup_lesson_mods)
330 function backup_lesson_high_scores($bf, $preferences, $lessonid) {
331 global $CFG;
333 $status = true;
335 $highscores = get_records("lesson_high_scores", "lessonid", $lessonid);
337 //If there is highscores
338 if ($highscores) {
339 //Write start tag
340 $status =fwrite ($bf,start_tag("HIGHSCORES",4,true));
341 //Iterate over each highscore
342 foreach ($highscores as $highscore) {
343 //Start highscore
344 $status =fwrite ($bf,start_tag("HIGHSCORE",5,true));
345 //Print highscore contents
346 fwrite ($bf,full_tag("USERID",6,false,$highscore->userid));
347 fwrite ($bf,full_tag("GRADEID",6,false,$highscore->gradeid));
348 fwrite ($bf,full_tag("NICKNAME",6,false,$highscore->nickname));
349 //End highscore
350 $status =fwrite ($bf,end_tag("HIGHSCORE",5,true));
352 //Write end tag
353 $status =fwrite ($bf,end_tag("HIGHSCORES",4,true));
355 return $status;
358 // backup lesson_default contents (executed from backup_lesson_mods)
359 function backup_lesson_default ($bf,$preferences) {
360 global $CFG;
362 $status = true;
364 //only one default record per course
365 $default = get_record("lesson_default", "course", $preferences->backup_course);
366 if ($default) {
367 //Start mod
368 $status =fwrite ($bf,start_tag("DEFAULTS",4,true));
369 //Print default data
370 fwrite ($bf,full_tag("PRACTICE",5,false,$default->practice));
371 fwrite ($bf,full_tag("MODATTEMPTS",5,false,$default->modattempts));
372 fwrite ($bf,full_tag("PASSWORD",5,false,$default->password));
373 fwrite ($bf,full_tag("USEPASSWORD",5,false,$default->usepassword));
374 fwrite ($bf,full_tag("GRADE",5,false,$default->grade));
375 fwrite ($bf,full_tag("CUSTOM",5,false,$default->custom));
376 fwrite ($bf,full_tag("ONGOING",5,false,$default->ongoing));
377 fwrite ($bf,full_tag("USEMAXGRADE",5,false,$default->usemaxgrade));
378 fwrite ($bf,full_tag("MAXANSWERS",5,false,$default->maxanswers));
379 fwrite ($bf,full_tag("MAXATTEMPTS",5,false,$default->maxattempts));
380 fwrite ($bf,full_tag("REVIEW",5,false,$default->review));
381 fwrite ($bf,full_tag("NEXTPAGEDEFAULT",5,false,$default->nextpagedefault));
382 fwrite ($bf,full_tag("MINQUESTIONS",5,false,$default->minquestions));
383 fwrite ($bf,full_tag("MAXPAGES",5,false,$default->maxpages));
384 fwrite ($bf,full_tag("TIMED",5,false,$default->timed));
385 fwrite ($bf,full_tag("MAXTIME",5,false,$default->maxtime));
386 fwrite ($bf,full_tag("RETAKE",5,false,$default->retake));
387 fwrite ($bf,full_tag("TREE",5,false,$default->tree));
388 fwrite ($bf,full_tag("SLIDESHOW",5,false,$default->slideshow));
389 fwrite ($bf,full_tag("WIDTH",5,false,$default->width));
390 fwrite ($bf,full_tag("HEIGHT",5,false,$default->height));
391 fwrite ($bf,full_tag("BGCOLOR",5,false,$default->bgcolor));
392 fwrite ($bf,full_tag("DISPLAYLEFT",5,false,$default->displayleft));
393 fwrite ($bf,full_tag("HIGHSCORES",5,false,$default->highscores));
394 fwrite ($bf,full_tag("MAXHIGHSCORES",5,false,$default->maxhighscores));
395 $status =fwrite ($bf,end_tag("DEFAULTS",4,true));
397 return $status;
400 //Return an array of info (name,value)
401 function lesson_check_backup_mods($course,$user_data=false,$backup_unique_code) {
402 //First the course data
403 $info[0][0] = get_string("modulenameplural","lesson");
404 if ($ids = lesson_ids($course)) {
405 $info[0][1] = count($ids);
406 } else {
407 $info[0][1] = 0;
410 //Now, if requested, the user_data
411 if ($user_data) {
412 $info[1][0] = get_string("attempts","lesson");
413 if ($ids = lesson_attempts_ids_by_course ($course)) {
414 $info[1][1] = count($ids);
415 } else {
416 $info[1][1] = 0;
419 return $info;
424 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
426 //Returns an array of lesson id
427 function lesson_ids ($course) {
429 global $CFG;
431 return get_records_sql ("SELECT l.id, l.course
432 FROM {$CFG->prefix}lesson l
433 WHERE l.course = '$course'");
436 //Returns an array of lesson_submissions id
437 function lesson_attempts_ids_by_course ($course) {
439 global $CFG;
441 return get_records_sql ("SELECT a.id , a.lessonid
442 FROM {$CFG->prefix}lesson_attempts a,
443 {$CFG->prefix}lesson l
444 WHERE l.course = '$course' AND
445 a.lessonid = l.id");