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