Added a lot of new features to lesson mod. Check them out :)
[moodle.git] / mod / lesson / locallib.php
CommitLineData
5e7856af 1<?php
2/// mnielsen @ CDC
3/// locallib.php is the new lib file for lesson module.
4/// including locallib.php is the same as including the old lib.php
5
6if (!defined("LESSON_UNSEENPAGE")) {
7 define("LESSON_UNSEENPAGE", 1); // Next page -> any page not seen before
8 }
9if (!defined("LESSON_UNANSWEREDPAGE")) {
10 define("LESSON_UNANSWEREDPAGE", 2); // Next page -> any page not answered correctly
11 }
12
13$LESSON_NEXTPAGE_ACTION = array (0 => get_string("normal", "lesson"),
14 LESSON_UNSEENPAGE => get_string("showanunseenpage", "lesson"),
15 LESSON_UNANSWEREDPAGE => get_string("showanunansweredpage", "lesson") );
16
17
18if (!defined("LESSON_NEXTPAGE")) {
19 define("LESSON_NEXTPAGE", -1); // Next page
20 }
21if (!defined("LESSON_EOL")) {
22 define("LESSON_EOL", -9); // End of Lesson
23 }
24/// CDC-FLAG 6/14/04 ///
25if (!defined("LESSON_UNSEENBRANCHPAGE")) {
26 define("LESSON_UNSEENBRANCHPAGE", -50); // Unseen branch page
27 }
28if (!defined("LESSON_PREVIOUSPAGE")) {
29 define("LESSON_PREVIOUSPAGE", -40); // previous page
30 }
31if (!defined("LESSON_RANDOMPAGE")) {
32 define("LESSON_RANDOMPAGE", -60); // random branch page
33 }
34if (!defined("LESSON_RANDOMBRANCH")) {
35 define("LESSON_RANDOMBRANCH", -70); // random branch
36 }
37if (!defined("LESSON_CLUSTERJUMP")) {
38 define("LESSON_CLUSTERJUMP", -80); // random within a cluster
39 }
40/// CDC-FLAG ///
41if (!defined("LESSON_UNDEFINED")) {
42 define("LESSON_UNDEFINED", -99); // undefined
43 }
44
45if (!defined("LESSON_SHORTANSWER")) {
46 define("LESSON_SHORTANSWER", "1");
47}
48if (!defined("LESSON_TRUEFALSE")) {
49 define("LESSON_TRUEFALSE", "2");
50}
51if (!defined("LESSON_MULTICHOICE")) {
52 define("LESSON_MULTICHOICE", "3");
53}
54if (!defined("LESSON_RANDOM")) {
55 define("LESSON_RANDOM", "4");
56}
57if (!defined("LESSON_MATCHING")) {
58 define("LESSON_MATCHING", "5");
59}
60if (!defined("LESSON_RANDOMSAMATCH")) {
61 define("LESSON_RANDOMSAMATCH", "6");
62}
63if (!defined("LESSON_DESCRIPTION")) {
64 define("LESSON_DESCRIPTION", "7");
65}
66if (!defined("LESSON_NUMERICAL")) {
67 define("LESSON_NUMERICAL", "8");
68}
69if (!defined("LESSON_MULTIANSWER")) {
70 define("LESSON_MULTIANSWER", "9");
71}
72/// CDC-FLAG /// 6/16/04
73if (!defined("LESSON_ESSAY")) {
74 define("LESSON_ESSAY", "10");
75}
76if (!defined("LESSON_CLUSTER")) {
77 define("LESSON_CLUSTER", "30");
78}
79if (!defined("LESSON_ENDOFCLUSTER")) {
80 define("LESSON_ENDOFCLUSTER", "31");
81}
82/// CDC-FLAG ///
83
84$LESSON_QUESTION_TYPE = array ( LESSON_MULTICHOICE => get_string("multichoice", "quiz"),
85 LESSON_TRUEFALSE => get_string("truefalse", "quiz"),
86 LESSON_SHORTANSWER => get_string("shortanswer", "quiz"),
87 LESSON_NUMERICAL => get_string("numerical", "quiz"),
88 LESSON_MATCHING => get_string("match", "quiz"),
89 LESSON_ESSAY => get_string("essay", "lesson") /// CDC-FLAG 6/16/04
90// LESSON_DESCRIPTION => get_string("description", "quiz"),
91// LESSON_RANDOM => get_string("random", "quiz"),
92// LESSON_RANDOMSAMATCH => get_string("randomsamatch", "quiz"),
93// LESSON_MULTIANSWER => get_string("multianswer", "quiz"),
94 );
95
96if (!defined("LESSON_BRANCHTABLE")) {
97 define("LESSON_BRANCHTABLE", "20");
98}
99if (!defined("LESSON_ENDOFBRANCH")) {
100 define("LESSON_ENDOFBRANCH", "21");
101}
102
103if (!defined("LESSON_ANSWER_EDITOR")) {
104 define("LESSON_ANSWER_EDITOR", "1");
105}
106if (!defined("LESSON_RESPONSE_EDITOR")) {
107 define("LESSON_RESPONSE_EDITOR", "2");
108}
109
110//////////////////////////////////////////////////////////////////////////////////////
111/// Any other lesson functions go here. Each of them must have a name that
112/// starts with lesson_
113
114/*******************************************************************/
115function lesson_save_question_options($question) {
116/// Given some question info and some data about the the answers
117/// this function parses, organises and saves the question
118/// This is only used when IMPORTING questions and is only called
119/// from format.php
120/// Lifted from mod/quiz/lib.php -
121/// 1. all reference to oldanswers removed
122/// 2. all reference to quiz_multichoice table removed
123/// 3. In SHORTANSWER questions usecase is store in the qoption field
124/// 4. In NUMERIC questions store the range as two answers
125/// 5. TRUEFALSE options are ignored
126/// 6. For MULTICHOICE questions with more than one answer the qoption field is true
127///
128/// Returns $result->error or $result->notice
129
130 $timenow = time();
131 switch ($question->qtype) {
132 case LESSON_SHORTANSWER:
133
134 $answers = array();
135 $maxfraction = -1;
136
137 // Insert all the new answers
138 foreach ($question->answer as $key => $dataanswer) {
139 if ($dataanswer != "") {
140 unset($answer);
141 $answer->lessonid = $question->lessonid;
142 $answer->pageid = $question->id;
143 if ($question->fraction[$key] >=0.5) {
144 $answer->jumpto = LESSON_NEXTPAGE;
145 }
146 $answer->timecreated = $timenow;
147 $answer->grade = $question->fraction[$key] * 100;
148 $answer->answer = $dataanswer;
149 $answer->feedback = $question->feedback[$key];
150 if (!$answer->id = insert_record("lesson_answers", $answer)) {
151 $result->error = "Could not insert shortanswer quiz answer!";
152 return $result;
153 }
154 $answers[] = $answer->id;
155 if ($question->fraction[$key] > $maxfraction) {
156 $maxfraction = $question->fraction[$key];
157 }
158 }
159 }
160
161
162 /// Perform sanity checks on fractional grades
163 if ($maxfraction != 1) {
164 $maxfraction = $maxfraction * 100;
165 $result->notice = get_string("fractionsnomax", "quiz", $maxfraction);
166 return $result;
167 }
168 break;
169
170 case LESSON_NUMERICAL: // Note similarities to SHORTANSWER
171
172 $answers = array();
173 $maxfraction = -1;
174
175
176 // for each answer store the pair of min and max values even if they are the same
177 foreach ($question->answer as $key => $dataanswer) {
178 if ($dataanswer != "") {
179 unset($answer);
180 $answer->lessonid = $question->lessonid;
181 $answer->pageid = $question->id;
182 $answer->jumpto = LESSON_NEXTPAGE;
183 $answer->timecreated = $timenow;
184 $answer->grade = $question->fraction[$key] * 100;
185 $answer->answer = $question->min[$key].":".$question->max[$key];
186 $answer->response = $question->feedback[$key];
187 if (!$answer->id = insert_record("lesson_answers", $answer)) {
188 $result->error = "Could not insert numerical quiz answer!";
189 return $result;
190 }
191
192 $answers[] = $answer->id;
193 if ($question->fraction[$key] > $maxfraction) {
194 $maxfraction = $question->fraction[$key];
195 }
196 }
197 }
198
199 /// Perform sanity checks on fractional grades
200 if ($maxfraction != 1) {
201 $maxfraction = $maxfraction * 100;
202 $result->notice = get_string("fractionsnomax", "quiz", $maxfraction);
203 return $result;
204 }
205 break;
206
207
208 case LESSON_TRUEFALSE:
209
210 // the truth
211 $answer->lessonid = $question->lessonid;
212 $answer->pageid = $question->id;
213 $answer->timecreated = $timenow;
214 $answer->answer = get_string("true", "quiz");
215 $answer->grade = $question->answer * 100;
216 if ($answer->grade > 50 ) {
217 $answer->jumpto = LESSON_NEXTPAGE;
218 }
219 if (isset($question->feedbacktrue)) {
220 $answer->response = $question->feedbacktrue;
221 }
222 if (!$true->id = insert_record("lesson_answers", $answer)) {
223 $result->error = "Could not insert quiz answer \"true\")!";
224 return $result;
225 }
226
227 // the lie
228 unset($answer);
229 $answer->lessonid = $question->lessonid;
230 $answer->pageid = $question->id;
231 $answer->timecreated = $timenow;
232 $answer->answer = get_string("false", "quiz");
233 $answer->grade = (1 - (int)$question->answer) * 100;
234 if ($answer->grade > 50 ) {
235 $answer->jumpto = LESSON_NEXTPAGE;
236 }
237 if (isset($question->feedbackfalse)) {
238 $answer->response = $question->feedbackfalse;
239 }
240 if (!$false->id = insert_record("lesson_answers", $answer)) {
241 $result->error = "Could not insert quiz answer \"false\")!";
242 return $result;
243 }
244
245 break;
246
247
248 case LESSON_MULTICHOICE:
249
250 $totalfraction = 0;
251 $maxfraction = -1;
252
253 $answers = array();
254
255 // Insert all the new answers
256 foreach ($question->answer as $key => $dataanswer) {
257 if ($dataanswer != "") {
258 unset($answer);
259 $answer->lessonid = $question->lessonid;
260 $answer->pageid = $question->id;
261 $answer->timecreated = $timenow;
262 $answer->grade = $question->fraction[$key] * 100;
263 /// CDC-FLAG changed some defaults
264 /* Original Code
265 if ($answer->grade > 50 ) {
266 $answer->jumpto = LESSON_NEXTPAGE;
267 }
268 Replaced with: */
269 if ($answer->grade > 50 ) {
270 $answer->jumpto = LESSON_NEXTPAGE;
271 $answer->score = 1;
272 }
273 // end Replace
274 $answer->answer = $dataanswer;
275 $answer->response = $question->feedback[$key];
276 if (!$answer->id = insert_record("lesson_answers", $answer)) {
277 $result->error = "Could not insert multichoice quiz answer! ";
278 return $result;
279 }
280 // for Sanity checks
281 if ($question->fraction[$key] > 0) {
282 $totalfraction += $question->fraction[$key];
283 }
284 if ($question->fraction[$key] > $maxfraction) {
285 $maxfraction = $question->fraction[$key];
286 }
287 }
288 }
289
290 /// Perform sanity checks on fractional grades
291 if ($question->single) {
292 if ($maxfraction != 1) {
293 $maxfraction = $maxfraction * 100;
294 $result->notice = get_string("fractionsnomax", "quiz", $maxfraction);
295 return $result;
296 }
297 } else {
298 $totalfraction = round($totalfraction,2);
299 if ($totalfraction != 1) {
300 $totalfraction = $totalfraction * 100;
301 $result->notice = get_string("fractionsaddwrong", "quiz", $totalfraction);
302 return $result;
303 }
304 }
305 break;
306
307 case LESSON_MATCHING:
308
309 $subquestions = array();
310
311 $i = 0;
312 // Insert all the new question+answer pairs
313 foreach ($question->subquestions as $key => $questiontext) {
314 $answertext = $question->subanswers[$key]; echo $answertext; echo "<br>"; exit;
315 if (!empty($questiontext) and !empty($answertext)) {
316 unset($answer);
317 $answer->lessonid = $question->lessonid;
318 $answer->pageid = $question->id;
319 $answer->timecreated = $timenow;
320 $answer->answer = $questiontext;
321 $answer->response = $answertext;
322 if ($i == 0) {
323 // first answer contains the correct answer jump
324 $answer->jumpto = LESSON_NEXTPAGE;
325 }
326 if (!$subquestion->id = insert_record("lesson_answers", $answer)) {
327 $result->error = "Could not insert quiz match subquestion!";
328 return $result;
329 }
330 $subquestions[] = $subquestion->id;
331 $i++;
332 }
333 }
334
335 if (count($subquestions) < 3) {
336 $result->notice = get_string("notenoughsubquestions", "quiz");
337 return $result;
338 }
339
340 break;
341
342
343 case LESSON_RANDOMSAMATCH:
344 $options->question = $question->id;
345 $options->choose = $question->choose;
346 if ($existing = get_record("quiz_randomsamatch", "question", $options->question)) {
347 $options->id = $existing->id;
348 if (!update_record("quiz_randomsamatch", $options)) {
349 $result->error = "Could not update quiz randomsamatch options!";
350 return $result;
351 }
352 } else {
353 if (!insert_record("quiz_randomsamatch", $options)) {
354 $result->error = "Could not insert quiz randomsamatch options!";
355 return $result;
356 }
357 }
358 break;
359
360 case LESSON_MULTIANSWER:
361 if (!$oldmultianswers = get_records("quiz_multianswers", "question", $question->id, "id ASC")) {
362 $oldmultianswers = array();
363 }
364
365 // Insert all the new multi answers
366 foreach ($question->answers as $dataanswer) {
367 if ($oldmultianswer = array_shift($oldmultianswers)) { // Existing answer, so reuse it
368 $multianswer = $oldmultianswer;
369 $multianswer->positionkey = $dataanswer->positionkey;
370 $multianswer->norm = $dataanswer->norm;
371 $multianswer->answertype = $dataanswer->answertype;
372
373 if (! $multianswer->answers = quiz_save_multianswer_alternatives
374 ($question->id, $dataanswer->answertype,
375 $dataanswer->alternatives, $oldmultianswer->answers))
376 {
377 $result->error = "Could not update multianswer alternatives! (id=$multianswer->id)";
378 return $result;
379 }
380 if (!update_record("quiz_multianswers", $multianswer)) {
381 $result->error = "Could not update quiz multianswer! (id=$multianswer->id)";
382 return $result;
383 }
384 } else { // This is a completely new answer
385 unset($multianswer);
386 $multianswer->question = $question->id;
387 $multianswer->positionkey = $dataanswer->positionkey;
388 $multianswer->norm = $dataanswer->norm;
389 $multianswer->answertype = $dataanswer->answertype;
390
391 if (! $multianswer->answers = quiz_save_multianswer_alternatives
392 ($question->id, $dataanswer->answertype,
393 $dataanswer->alternatives))
394 {
395 $result->error = "Could not insert multianswer alternatives! (questionid=$question->id)";
396 return $result;
397 }
398 if (!insert_record("quiz_multianswers", $multianswer)) {
399 $result->error = "Could not insert quiz multianswer!";
400 return $result;
401 }
402 }
403 }
404 break;
405
406 case LESSON_RANDOM:
407 break;
408
409 case LESSON_DESCRIPTION:
410 break;
411
412 default:
413 $result->error = "Unsupported question type ($question->qtype)!";
414 return $result;
415 break;
416 }
417 return true;
418}
419/*******************************************************************/
420function lesson_choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", $nothingvalue="0", $return=false) {
421/// Given an array of value, creates a popup menu to be part of a form
422/// $options["value"]["label"]
423
424 if ($nothing == "choose") {
425 $nothing = get_string("choose")."...";
426 }
427
428 if ($script) {
429 $javascript = "onChange=\"$script\"";
430 } else {
431 $javascript = "";
432 }
433
434 $output = "<label for=$name class=hidden-label>$name</label><SELECT id=$name NAME=$name $javascript>\n"; //CDC hidden label added.
435 if ($nothing) {
436 $output .= " <OPTION VALUE=\"$nothingvalue\"\n";
437 if ($nothingvalue == $selected) {
438 $output .= " SELECTED";
439 }
440 $output .= ">$nothing</OPTION>\n";
441 }
442 if (!empty($options)) {
443 foreach ($options as $value => $label) {
444 $output .= " <OPTION VALUE=\"$value\"";
445 if ($value == $selected) {
446 $output .= " SELECTED";
447 }
448 // stop zero label being replaced by array index value
449 // if ($label) {
450 // $output .= ">$label</OPTION>\n";
451 // } else {
452 // $output .= ">$value</OPTION>\n";
453 // }
454 $output .= ">$label</OPTION>\n";
455
456 }
457 }
458 $output .= "</SELECT>\n";
459
460 if ($return) {
461 return $output;
462 } else {
463 echo $output;
464 }
465}
466
467/*******************************************************************/
468function lesson_iscorrect($pageid, $jumpto) {
469 // returns true is jumpto page is (logically) after the pageid page, other returns false
470
471 // first test the special values
472 if (!$jumpto) {
473 // same page
474 return false;
475 } elseif ($jumpto == LESSON_NEXTPAGE) {
476 return true;
477 /// CDC-FLAG 6/21/04 ///
478 } elseif ($jumpto == LESSON_UNSEENBRANCHPAGE) {
479 return true;
480 } elseif ($jumpto == LESSON_RANDOMPAGE) {
481 return true;
482 } elseif ($jumpto == LESSON_CLUSTERJUMP) {
483 return true;
484 /// CDC-FLAG ///
485 } elseif ($jumpto == LESSON_EOL) {
486 return true;
487 }
488 // we have to run through the pages from pageid looking for jumpid
489 $apageid = get_field("lesson_pages", "nextpageid", "id", $pageid);
490 while (true) {
491 if ($jumpto == $apageid) {
492 return true;
493 }
494 if ($apageid) {
495 $apageid = get_field("lesson_pages", "nextpageid", "id", $apageid);
496 } else {
497 return false;
498 }
499 }
500 return false; // should never be reached
501}
502
503/// CDC-FLAG ///
504/*******************************************************************/
505function lesson_display_branch_jumps($lesson_id, $pageid) {
506// this fucntion checks to see if a page is a branch or is
507// a page that is enclosed by a branch table and an endofbranch/eol
508
509 // NoticeFix ... this may cause problems... not sure
510 if($pageid == 0) {
511 // first page
512 return false;
513 }
514 // get all of the lesson pages
515 if (!$lessonpages = get_records_select("lesson_pages", "lessonid = $lesson_id")) {
516 // adding first page
517 return false;
518 }
519
520 if ($lessonpages[$pageid]->qtype == LESSON_BRANCHTABLE) {
521 return true;
522 }
523
524 return lesson_is_page_in_branch($lessonpages, $pageid);
525}
526
527/*******************************************************************/
528function lesson_display_cluster_jump($lesson_id, $pageid) {
529// this fucntion checks to see if a page is a cluster page or is
530// a page that is enclosed by a cluster page and an endofcluster/eol
531
532 // NoticeFix ... this may cause problems... not sure
533 if($pageid == 0) {
534 // first page
535 return false;
536 }
537 // get all of the lesson pages
538 if (!$lessonpages = get_records_select("lesson_pages", "lessonid = $lesson_id")) {
539 // adding first page
540 return false;
541 }
542
543 if ($lessonpages[$pageid]->qtype == LESSON_CLUSTER) {
544 return true;
545 }
546
547 return lesson_is_page_in_cluster($lessonpages, $pageid);
548
549}
550
551// 6/21/04
552/*******************************************************************/
553function execute_teacherwarning($lesson) {
554// this function checks to see if a LESSON_CLUSTERJUMP or
555// a LESSON_UNSEENBRANCHPAGE is used in a lesson.
556// This function is only executed when a teacher is
557// checking the navigation for a lesson.
558
559 // get all of the lesson answers
560 if (!$lessonanswers = get_records_select("lesson_answers", "lessonid = $lesson")) {
561 // no answers, then not useing cluster or unseen
562 return false;
563 }
564 // just check for the first one that fulfills the requirements
565 foreach ($lessonanswers as $lessonanswer) {
566 if ($lessonanswer->jumpto == LESSON_CLUSTERJUMP || $lessonanswer->jumpto == LESSON_UNSEENBRANCHPAGE) {
567 return true;
568 }
569 }
570
571 // if no answers use either of the two jumps
572 return false;
573}
574
575
576// 6/18/04
577/*******************************************************************/
578function lesson_cluster_jump($lesson, $user, $pageid) {
579// this fucntion interprets LESSON_CLUSTERJUMP
580// it will select a page randomly
581// and the page selected will be inbetween a cluster page and endofcluter/eol
582// and the page selected will be a page that has not been viewed already
583// and if any pages are within a branchtable/endofbranch then only 1 page within
584// the branchtable/endofbranch will be randomly selected (sub clustering)
585
586 // get the number of retakes
587 if (!$retakes = count_records("lesson_grades", "lessonid", $lesson, "userid", $user)) {
588 $retakes = 0;
589 }
590
591 // get all the lesson_attempts aka what the user has seen
592 if ($seen = get_records_select("lesson_attempts", "lessonid = $lesson AND userid = $user AND retry = $retakes", "timeseen DESC")) {
593 foreach ($seen as $value) { // load it into an array that I can more easily use
594 $seenpages[$value->pageid] = $value->pageid;
595 }
596 } else {
597 $seenpages = array();
598 }
599
600 // get the lesson pages
601 if (!$lessonpages = get_records_select("lesson_pages", "lessonid = $lesson")) {
602 error("Error: could not find records in lesson_pages table");
603 }
604 // find the start of the cluster
605 while ($pageid != 0) { // this condition should not be satisfied... should be a cluster page
606 if ($lessonpages[$pageid]->qtype == LESSON_CLUSTER) {
607 break;
608 }
609 $pageid = $lessonpages[$pageid]->prevpageid;
610 }
611
612 $pageid = $lessonpages[$pageid]->nextpageid; // move down from the cluster page
613
614 while (true) { // now load all the pages into the cluster that are not already inside of a branch table.
615 if ($lessonpages[$pageid]->qtype == LESSON_ENDOFCLUSTER) {
616 // store the endofcluster page's jump
617 $exitjump = get_field("lesson_answers", "jumpto", "pageid", $pages[$count][0], "lessonid", $lesson);
618 if ($exitjump == LESSON_NEXTPAGE) {
619 $exitjump = $pages[$count][2];
620 }
621 if ($exitjump == 0) {
622 $exitjump = LESSON_EOL;
623 }
624 break;
625 } elseif (!lesson_is_page_in_branch($lessonpages, $pageid) && $lessonpages[$pageid]->qtype != LESSON_ENDOFBRANCH) {
626 // load page into array when it is not in a branch table and when it is not an endofbranch
627 $clusterpages[] = $lessonpages[$pageid];
628 }
629 if ($lessonpages[$pageid]->nextpageid == 0) {
630 // shouldn't ever get here... should be using endofcluster
631 $exitjump = LESSON_EOL;
632 break;
633 } else {
634 $pageid = $lessonpages[$pageid]->nextpageid;
635 }
636 }
637
638 // filter out the ones we have seen
639 foreach ($clusterpages as $clusterpage) {
640 if ($clusterpage->qtype == LESSON_BRANCHTABLE) { // if branchtable, check to see if any pages inside have been viewed
641 $branchpages = lesson_pages_in_branch($lessonpages, $clusterpage->id); // get the pages in the branchtable
642 $flag = true;
643 foreach ($branchpages as $branchpage) {
644 if (array_key_exists($branchpage->id, $seenpages)) { // check if any of the pages have been viewed
645 $flag = false;
646 }
647 }
648 if ($flag && count($branchpages) > 0) {
649 // add branch table
650 $unseen[] = $clusterpage;
651 }
652 } else {
653 // add any other type of page that has not already been viewed
654 if (!array_key_exists($clusterpage->id, $seenpages)) {
655 $unseen[] = $clusterpage;
656 }
657 }
658 }
659
660 if (isset($unseen)) { // if not set, then use exitjump, otherwise find out next page/branch
661 $nextpage = $unseen[rand(0, count($unseen)-1)];
662 } else {
663 return $exitjump; // seen all there is to see, leave the cluster
664 }
665
666 if ($nextpage->qtype == LESSON_BRANCHTABLE) { // if branch table, then pick a random page inside of it
667 $branchpages = lesson_pages_in_branch($lessonpages, $nextpage->id);
668 return $branchpages[rand(0, count($branchpages)-1)]->id;
669 } else { // otherwise, return the page's id
670 return $nextpage->id;
671 }
672}
673
674/*******************************************************************/
675function lesson_pages_in_branch($lessonpages, $branchid) {
676// returns pages that are within a branch
677
678 $pageid = $lessonpages[$branchid]->nextpageid; // move to the first page after the branch table
679 $pagesinbranch = array();
680
681 while (true) {
682 if ($pageid == 0) { // EOL
683 break;
684 } elseif ($lessonpages[$pageid]->qtype == LESSON_BRANCHTABLE) {
685 break;
686 } elseif ($lessonpages[$pageid]->qtype == LESSON_ENDOFBRANCH) {
687 break;
688 }
689 $pagesinbranch[] = $lessonpages[$pageid];
690 $pageid = $lessonpages[$pageid]->nextpageid;
691 }
692
693 return $pagesinbranch;
694}
695
696/*******************************************************************/
697function lesson_unseen_question_jump($lesson, $user, $pageid) {
698// This function interprets the LESSON_UNSEENBRANCHPAGE jump.
699// will return the pageid of a random unseen page that is within a branch
700
701 // get the number of retakes
702 if (!$retakes = count_records("lesson_grades", "lessonid", $lesson, "userid", $user)) {
703 $retakes = 0;
704 }
705
706 // get all the lesson_attempts aka what the user has seen
707 if ($viewedpages = get_records_select("lesson_attempts", "lessonid = $lesson AND userid = $user AND retry = $retakes", "timeseen DESC")) {
708 foreach($viewedpages as $viewed) {
709 $seenpages[] = $viewed->pageid;
710 }
711 } else {
712 $seenpages = array();
713 }
714
715 // get the lesson pages
716 if (!$lessonpages = get_records_select("lesson_pages", "lessonid = $lesson")) {
717 error("Error: could not find records in lesson_pages table");
718 }
719
720 if ($pageid == LESSON_UNSEENBRANCHPAGE) { // this only happens when a student leaves in the middle of an unseen question within a branch series
721 $pageid = $seenpages[0]; // just change the pageid to the last page viewed inside the branch table
722 }
723
724 // go up the pages till branch table
725 while ($pageid != 0) { // this condition should never be satisfied... only happens if there are no branch tables above this page
726 if ($lessonpages[$pageid]->qtype == LESSON_BRANCHTABLE) {
727 break;
728 }
729 $pageid = $lessonpages[$pageid]->prevpageid;
730 }
731
732 $pagesinbranch = lesson_pages_in_branch($lessonpages, $pageid);
733
734 // this foreach loop stores all the pages that are within the branch table but are not in the $seenpages array
735 foreach($pagesinbranch as $page) {
736 if (!in_array($page->id, $seenpages)) {
737 $unseen[] = $page->id;
738 }
739 }
740
741 if(!isset($unseen)) {
742 if(isset($pagesinbranch)) {
743 $temp = end($pagesinbranch);
744 $nextpage = $temp->nextpageid; // they have seen all the pages in the branch, so go to EOB/next branch table/EOL
745 } else {
746 // there are no pages inside the branch, so return the next page
747 $nextpage = $lessonpages[$pageid]->nextpageid;
748 }
749 if ($nextpage == 0) {
750 return LESSON_EOL;
751 } else {
752 return $nextpage;
753 }
754 } else {
755 return $unseen[rand(0, count($unseen)-1)]; // returns a random page id for the next page
756 }
757}
758
759// 6/15/04
760/*******************************************************************/
761function lesson_unseen_branch_jump($lesson, $user) {
762// This will return a random unseen branch table
763
764 if (!$retakes = count_records("lesson_grades", "lessonid", $lesson, "userid", $user)) {
765 $retakes = 0;
766 }
767
768 if (!$seenbranches = get_records_select("lesson_branch", "lessonid = $lesson AND userid = $user AND retry = $retakes",
769 "timeseen DESC")) {
770 error("Error: could not find records in lesson_branch table");
771 }
772
773 // get the lesson pages
774 if (!$lessonpages = get_records_select("lesson_pages", "lessonid = $lesson")) {
775 error("Error: could not find records in lesson_pages table");
776 }
777
778 // this loads all the viewed branch tables into $seen untill it finds the branch table with the flag
779 // which is the branch table that starts the unseenbranch function
780 $seen = array();
781 foreach ($seenbranches as $seenbranch) {
782 if (!$seenbranch->flag) {
783 $seen[$seenbranch->pageid] = $seenbranch->pageid;
784 } else {
785 $start = $seenbranch->pageid;
786 break;
787 }
788 }
789 // this function searches through the lesson pages to find all the branch tables
790 // that follow the flagged branch table
791 $pageid = $lessonpages[$start]->nextpageid; // move down from the flagged branch table
792 while ($pageid != 0) { // grab all of the branch table till eol
793 if ($lessonpages[$pageid]->qtype == LESSON_BRANCHTABLE) {
794 $branchtables[] = $lessonpages[$pageid]->id;
795 }
796 $pageid = $lessonpages[$pageid]->nextpageid;
797 }
798
799 foreach ($branchtables as $branchtable) {
800 // load all of the unseen branch tables into unseen
801 if (!array_key_exists($branchtable, $seen)) {
802 $unseen[] = $branchtable;
803 }
804 }
805 if (isset($unseen)) {
806 return $unseen[rand(0, count($unseen)-1)]; // returns a random page id for the next page
807 } else {
808 return LESSON_EOL; // has viewed all of the branch tables
809 }
810}
811
812/*******************************************************************/
813function lesson_random_question_jump($lesson, $pageid) {
814// This function will return the pageid of a random page
815// that is within a branch table
816
817 // get the lesson pages
818 if (!$lessonpages = get_records_select("lesson_pages", "lessonid = $lesson")) {
819 error("Error: could not find records in lesson_pages table");
820 }
821
822 // go up the pages till branch table
823 while ($pageid != 0) { // this condition should never be satisfied... only happens if there are no branch tables above this page
824
825 if ($lessonpages[$pageid]->qtype == LESSON_BRANCHTABLE) {
826 break;
827 }
828 $pageid = $lessonpages[$pageid]->prevpageid;
829 }
830
831 // get the pages within the branch
832 $pagesinbranch = lesson_pages_in_branch($lessonpages, $pageid);
833
834 if(!isset($pagesinbranch)) {
835 // there are no pages inside the branch, so return the next page
836 return $lessonpages[$pageid]->nextpageid;
837 } else {
838 return $pagesinbranch[rand(0, count($pagesinbranch)-1)]->id; // returns a random page id for the next page
839 }
840}
841
842// 6/15/04
843/*******************************************************************/
844function lesson_is_page_in_branch($pages, $pageid) {
845// This function's purpose is to check if a page is within a branch or not
846
847 $pageid = $pages[$pageid]->prevpageid; // move up one
848
849 // go up the pages till branch table
850 while (true) {
851 if ($pageid == 0) { // ran into the beginning of the lesson
852 return false;
853 } elseif ($pages[$pageid]->qtype == LESSON_ENDOFBRANCH) { // ran into the end of another branch table
854 return false;
855 } elseif ($pages[$pageid]->qtype == LESSON_CLUSTER) { // do not look beyond a cluster
856 return false;
857 } elseif ($pages[$pageid]->qtype == LESSON_BRANCHTABLE) { // hit a branch table
858 return true;
859 }
860 $pageid = $pages[$pageid]->prevpageid;
861 }
862
863}
864
865/*******************************************************************/
866function lesson_is_page_in_cluster($pages, $pageid) {
867// This function checks to see if a page is within a cluster or not
868
869 $pageid = $pages[$pageid]->prevpageid; // move up one
870
871 // go up the pages till branch table
872 while (true) {
873 if ($pageid == 0) { // ran into the beginning of the lesson
874 return false;
875 } elseif ($pages[$pageid]->qtype == LESSON_ENDOFCLUSTER) { // ran into the end of another branch table
876 return false;
877 } elseif ($pages[$pageid]->qtype == LESSON_CLUSTER) { // hit a branch table
878 return true;
879 }
880 $pageid = $pages[$pageid]->prevpageid;
881 }
882}
883
884/*******************************************************************/
885function lesson_print_tree_menu($lessonid, $pageid, $id) {
886// prints the contents of the left menu
887
888 if(!$pages = get_records_select("lesson_pages", "lessonid = $lessonid")) {
889 error("Error: could not find lesson pages");
890 }
891 while ($pageid != 0) {
892 lesson_print_tree_link_menu($pages[$pageid], $id);
893 $pageid = $pages[$pageid]->nextpageid;
894 }
895}
896
897/*******************************************************************/
898function lesson_print_tree_link_menu($page, $id) {
899// prints the actual link for the left menu
900
901 if ($page->qtype == LESSON_BRANCHTABLE && !$page->display) {
902 return false;
903 }
904
905 // set up some variables NoticeFix changed whole function
906 $output = "";
907 $close = false;
908 $title=$page->title; //CDC Chris Berri took out parsing of title in left menu on 6/11
909 $link="id=$id&action=navigation&pageid=".$page->id;
910
911 if($_SERVER['QUERY_STRING']==$link) {
912 $close=true;
913 $output.="<div class='active'><em>";
914 }
915 if (($page->qtype!=LESSON_BRANCHTABLE)||($page->qtype==LESSON_ENDOFBRANCH)||($page->qtype==21)) {
916 $output .= "";
917 } else {
918 $output .= "<li><a href=\"view.php?id=$id&action=navigation&pageid=$page->id\">".$title."</a></li>\n";
919 }
920 if($close) {
921 $output.="</em></div>";
922 }
923 echo $output;
924
925}
926
927/*******************************************************************/
928function lesson_print_tree($pageid, $lessonid, $cmid, $pixpath) {
929// this function prints out the tree view list
930
931 if(!$pages = get_records_select("lesson_pages", "lessonid = $lessonid")) {
932 error("Error: could not find lesson pages");
933 }
934 echo "<table>";
935 while ($pageid != 0) {
936 echo "<tr><td>";
937 if(($pages[$pageid]->qtype != LESSON_BRANCHTABLE) && ($pages[$pageid]->qtype != LESSON_ENDOFBRANCH)) {
938 $output = "<a style='color:#DF041E;' href=\"view.php?id=$cmid&display=".$pages[$pageid]->id."\">".$pages[$pageid]->title."</a>\n";
939 } else {
940 $output = "<a href=\"view.php?id=$cmid&display=".$pages[$pageid]->id."\">".$pages[$pageid]->title."</a>\n";
941
942 if($answers = get_records_select("lesson_answers", "lessonid = $lessonid and pageid = $pageid")) {
943 $output .= "Jumps to: ";
944 $end = end($answers);
945 foreach ($answers as $answer) {
946 if ($answer->jumpto == 0) {
947 $output .= get_string("thispage", "lesson");
948 } elseif ($answer->jumpto == LESSON_NEXTPAGE) {
949 $output .= get_string("nextpage", "lesson");
950 } elseif ($answer->jumpto == LESSON_EOL) {
951 $output .= get_string("endoflesson", "lesson");
952 } elseif ($answer->jumpto == LESSON_UNSEENBRANCHPAGE) {
953 $output .= get_string("unseenpageinbranch", "lesson");
954 } elseif ($answer->jumpto == LESSON_PREVIOUSPAGE) {
955 $output .= get_string("previouspage", "lesson");
956 } elseif ($answer->jumpto == LESSON_RANDOMPAGE) {
957 $output .= get_string("randompageinbranch", "lesson");
958 } elseif ($answer->jumpto == LESSON_RANDOMBRANCH) {
959 $output .= get_string("randombranch", "lesson");
960 } elseif ($answer->jumpto == LESSON_CLUSTERJUMP) {
961 $output .= get_string("clusterjump", "lesson");
962 } else {
963 $output .= $pages[$answer->jumpto]->title;
964 }
965 if ($answer->id != $end->id) {
966 $output .= ", ";
967 }
968 }
969 }
970 }
971
972 echo $output;
973 if (count($pages) > 1) {
974 echo "<a title=\"move\" href=\"lesson.php?id=$cmid&action=move&pageid=".$pages[$pageid]->id."\">\n".
975 "<img src=\"$pixpath/t/move.gif\" hspace=\"2\" height=11 width=11 alt=\"move\" border=0></a>\n"; //CDC alt text added.
976 }
977 echo "<a title=\"update\" href=\"lesson.php?id=$cmid&action=editpage&pageid=".$pages[$pageid]->id."\">\n".
978 "<img src=\"$pixpath/t/edit.gif\" hspace=\"2\" height=11 width=11 alt=\"edit\" border=0></a>\n".
979 "<a title=\"delete\" href=\"lesson.php?id=$cmid&action=confirmdelete&pageid=".$pages[$pageid]->id."\">\n".
980 "<img src=\"$pixpath/t/delete.gif\" hspace=\"2\" height=11 width=11 alt=\"delete\" border=0></a>\n"; //CDC alt text added.
981
982 echo "</tr></td>";
983 $pageid = $pages[$pageid]->nextpageid;
984 }
985 echo "</table>";
986}
987
988/*******************************************************************/
989function lesson_calculate_ongoing_score($lesson, $USER) {
990// this calculates and prints the ongoing score for students
991
992 // get the number of retries
993 if (!$retries = count_records("lesson_grades", "lessonid", $lesson->id, "userid", $USER->id)) {
994 $retries = 0;
995 }
996
997 if (!$lesson->custom) {
998 $ncorrect = 0;
999 if ($pagesanswered = get_records_select("lesson_attempts", "lessonid = $lesson->id AND
1000 userid = $USER->id AND retry = $retries order by timeseen")) {
1001
1002 foreach ($pagesanswered as $pageanswered) {
1003 if (@!array_key_exists($pageanswered->pageid, $temp)) {
1004 $temp[$pageanswered->pageid] = array($pageanswered->correct, 1);
1005 } else {
1006 if ($temp[$pageanswered->pageid][1] < $lesson->maxattempts) {
1007 $n = $temp[$pageanswered->pageid][1] + 1;
1008 $temp[$pageanswered->pageid] = array($pageanswered->correct, $n);
1009 }
1010 }
1011 }
1012 foreach ($temp as $value => $key) {
1013 if ($key[0] == 1) {
1014 $ncorrect += 1;
1015 }
1016 }
1017 }
1018 $nviewed = count($temp); // this counts number of Questions the user viewed
1019
1020 $output->correct = $ncorrect;
1021 $output->viewed = $nviewed;
1022 print_simple_box(get_string("ongoingnormal", "lesson", $output), "center");
1023
1024 } else {
1025 $score = 0;
1026 $currenthigh = 0;
1027 if ($useranswers = get_records_select("lesson_attempts", "lessonid = $lesson->id AND
1028 userid = $USER->id AND retry = $retries", "timeseen")) {
1029
1030 foreach ($useranswers as $useranswer) {
1031 if (@!array_key_exists($useranswer->pageid, $temp)) {
1032 $temp[$useranswer->pageid] = array($useranswer->answerid, 1);
1033 } else {
1034 if ($temp[$useranswer->pageid][1] < $lesson->maxattempts) {
1035 $n = $temp[$useranswer->pageid][1] + 1;
1036 $temp[$useranswer->pageid] = array($useranswer->answerid, $n);
1037 }
1038 }
1039 }
1040 if ($answervalues = get_records_select("lesson_answers", "lessonid = $lesson->id")) {
1041 if ($pages = get_records_select("lesson_pages", "lessonid = $lesson->id")) {
1042 foreach ($pages as $page) {
1043 $questions[$page->id] = $page->qtype;
1044 }
1045 } else {
1046 $questions = array();
1047 }
1048 $currenthighscore = array();
1049 foreach ($answervalues as $answervalue) {
1050 if (array_key_exists($answervalue->pageid, $temp)) {
1051 if ($temp[$answervalue->pageid][0] == $answervalue->id && $questions[$answervalue->pageid] != LESSON_ESSAY) {
1052 $score = $score + $answervalue->score;
1053 if (isset($currenthighscore[$answervalue->pageid])) {
1054 if ($currenthighscore[$answervalue->pageid] < $answervalue->score) {
1055 $currenthighscore[$answervalue->pageid] = $answervalue->score;
1056 }
1057 } else {
1058 $currenthighscore[$answervalue->pageid] = $answervalue->score;
1059 }
1060 } elseif ($questions[$answervalue->pageid] != LESSON_ESSAY) {
1061 if (isset($currenthighscore[$answervalue->pageid])) {
1062 if ($currenthighscore[$answervalue->pageid] < $answervalue->score) {
1063 $currenthighscore[$answervalue->pageid] = $answervalue->score;
1064 }
1065 } else {
1066 $currenthighscore[$answervalue->pageid] = $answervalue->score;
1067 }
1068 }
1069 }
1070 }
1071 // add up the current high score
1072 foreach ($currenthighscore as $value) {
1073 $currenthigh += $value;
1074 }
1075 } else {
1076 error("Error: Could not find answers!");
1077 }
1078 }
1079 if ($score > $lesson->grade) {
1080 $score = $lesson->grade;
1081 } elseif ($score < 0) {
1082 $score = 0;
1083 }
1084
1085 $ongoingoutput->grade = $lesson->grade;
1086 $ongoingoutput->score = $score;
1087 $ongoingoutput->currenthigh = $currenthigh;
1088 print_simple_box(get_string("ongoingcustom", "lesson", $ongoingoutput), "center");
1089 }
1090}
1091
1092/*******************************************************************/
1093function lesson_qtype_menu($qtypes, $selected="", $link="", $onclick="") {
1094// prints the question types for when editing and adding a page
1095
1096 $output = "";
1097 foreach ($qtypes as $value => $label) {
1098 if ($value == $selected) {
1099 $output .= "<b>$label</b>";
1100 $output .= "<input type=\"hidden\" name=\"qtype\" value=\"$value\"> \n";
1101 } else {
1102 $output .= "<a onClick=\"$onclick\" href=\"$link"."&qtype=$value\">$label</a>";
1103 }
1104 if ($label != end($qtypes)) {
1105 $output .= " | ";
1106 }
1107 }
1108 echo $output;
1109}
1110
1111/*******************************************************************/
1112function lesson_check_nickname($name) {
1113// used to check high score nicknames.
1114// checks nickname agains a list of "bad words" in filter.php
1115
1116 if ($name == NULL) {
1117 return false;
1118 }
1119
1120 require_once('filter.php');
1121
1122 foreach ($filterwords as $filterword) {
1123 if (strstr($name, $filterword)) {
1124 return false;
1125 }
1126 }
1127 return true;
1128}
1129/// CDC-FLAG ///
1130
1131?>