mirrored from git://git.moodle.org/moodle.git
/
view.php
1015 lines (938 loc) · 52.3 KB
/
view.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php // $Id$
/**
* This page prints a particular instance of lesson
*
* @version $Id$
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package lesson
**/
require_once('../../config.php');
require_once('locallib.php');
require_once('lib.php');
$id = required_param('id', PARAM_INT); // Course Module ID
$pageid = optional_param('pageid', NULL, PARAM_INT); // Lesson Page ID
list($cm, $course, $lesson) = lesson_get_basics($id);
require_login($course->id, false, $cm);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
/// Check these for students only TODO: Find a better method for doing this!
/// Check lesson availability
/// Check for password
/// Check dependencies
/// Check for high scores
if (!has_capability('mod/lesson:manage', $context)) {
if (time() < $lesson->available) {
lesson_print_header($cm, $course, $lesson);
print_simple_box_start('center');
echo '<div align="center">';
echo get_string('lessonopen', 'lesson', userdate($lesson->available)).'<br />';
echo '<div class="lessonbutton standardbutton" style="padding: 5px;"><a href="../../course/view.php?id='. $course->id .'">'. get_string('returnmainmenu', 'lesson') .'</a></div>';
echo '</div>';
print_simple_box_end();
print_footer($course);
exit();
} else if (time() > $lesson->deadline) {
lesson_print_header($cm, $course, $lesson);
print_simple_box_start('center');
echo '<div align="center">';
echo get_string('lessonclosed', 'lesson', userdate($lesson->deadline)) .'<br />';
echo '<div class="lessonbutton standardbutton" style="padding: 5px;"><a href="../../course/view.php?id='. $course->id. '">'. get_string('returnmainmenu', 'lesson') .'</a></div>';
echo '</div>';
print_simple_box_end();
print_footer($course);
exit();
} else if ($lesson->usepassword and empty($USER->lessonloggedin[$lesson->id])) { // Password protected lesson code
$correctpass = false;
if ($password = optional_param('userpassword', '', PARAM_CLEAN)) {
if ($lesson->password == md5(trim($password))) {
$USER->lessonloggedin[$lesson->id] = true;
$correctpass = true;
if ($lesson->highscores) {
// Logged in - redirect so we go through all of these checks before starting the lesson.
redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id");
}
}
}
if (!$correctpass) {
lesson_print_header($cm, $course, $lesson);
echo "<div class=\"password-form\">\n";
print_simple_box_start('center');
echo '<form name="password" method="post" action="'.$CFG->wwwroot.'/mod/lesson/view.php">' . "\n";
echo '<input type="hidden" name="id" value="'. $cm->id .'" />' . "\n";
if (optional_param('userpassword', 0, PARAM_CLEAN)) {
notify(get_string('loginfail', 'lesson'));
}
echo get_string('passwordprotectedlesson', 'lesson', format_string($lesson->name))."<br /><br />\n".
get_string('enterpassword', 'lesson')." <input type=\"password\" name=\"userpassword\" /><br /><br />\n<center>".
'<span class="lessonbutton standardbutton"><a href="'.$CFG->wwwroot.'/course/view.php?id='. $course->id .'">'. get_string('cancel', 'lesson') .'</a></span> ';
lesson_print_submit_link(get_string('continue', 'lesson'), 'password', 'center', 'standardbutton submitbutton');
echo '</center>';
print_simple_box_end();
echo "</div>\n";
print_footer($course);
exit();
}
} else if ($lesson->dependency) { // check for dependencies
if ($dependentlesson = get_record('lesson', 'id', $lesson->dependency)) {
// lesson exists, so we can proceed
$conditions = unserialize($lesson->conditions);
// assume false for all
$timespent = false;
$completed = false;
$gradebetterthan = false;
// check for the timespent condition
if ($conditions->timespent) {
if ($attempttimes = get_records_select('lesson_timer', "userid = $USER->id AND lessonid = $dependentlesson->id")) {
// go through all the times and test to see if any of them satisfy the condition
foreach($attempttimes as $attempttime) {
$duration = $attempttime->lessontime - $attempttime->starttime;
if ($conditions->timespent < $duration/60) {
$timespent = true;
}
}
}
} else {
$timespent = true; // there isn't one set
}
// check for the gradebetterthan condition
if($conditions->gradebetterthan) {
if ($studentgrades = get_records_select('lesson_grades', "userid = $USER->id AND lessonid = $dependentlesson->id")) {
// go through all the grades and test to see if any of them satisfy the condition
foreach($studentgrades as $studentgrade) {
if ($studentgrade->grade >= $conditions->gradebetterthan) {
$gradebetterthan = true;
}
}
}
} else {
$gradebetterthan = true; // there isn't one set
}
// check for the completed condition
if ($conditions->completed) {
if (count_records('lesson_grades', 'userid', $USER->id, 'lessonid', $dependentlesson->id)) {
$completed = true;
}
} else {
$completed = true; // not set
}
$errors = array();
// collect all of our error statements
if (!$timespent) {
$errors[] = get_string('timespenterror', 'lesson', $conditions->timespent);
}
if (!$completed) {
$errors[] = get_string('completederror', 'lesson');
}
if (!$gradebetterthan) {
$errors[] = get_string('gradebetterthanerror', 'lesson', $conditions->gradebetterthan);
}
if (!empty($errors)) { // print out the errors if any
lesson_print_header($cm, $course, $lesson);
echo '<p>';
print_simple_box_start('center');
print_string('completethefollowingconditions', 'lesson', $dependentlesson->name);
echo '<p align="center">'.implode('<br />'.get_string('and', 'lesson').'<br />', $errors).'</p>';
print_simple_box_end();
echo '</p>';
print_footer($course);
exit();
}
}
} else if ($lesson->highscores and !$lesson->practice and !optional_param('viewed', 0) and empty($pageid)) {
// Display high scores before starting lesson
redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id");
}
}
// set up some general variables
$path = $CFG->wwwroot .'/course';
// this is called if a student leaves during a lesson
if($pageid == LESSON_UNSEENBRANCHPAGE) {
$pageid = lesson_unseen_question_jump($lesson->id, $USER->id, $pageid);
}
// display individual pages and their sets of answers
// if pageid is EOL then the end of the lesson has been reached
// for flow, changed to simple echo for flow styles, michaelp, moved lesson name and page title down
$attemptflag = false;
if (empty($pageid)) {
// make sure there are pages to view
if (!get_field('lesson_pages', 'id', 'lessonid', $lesson->id, 'prevpageid', 0)) {
if (!has_capability('mod/lesson:manage', $context)) {
lesson_set_message(get_string('lessonnotready', 'lesson', $course->teacher)); // a nice message to the student
} else {
if (!count_records('lesson_pages', 'lessonid', $lesson->id)) {
redirect("$CFG->wwwroot/mod/lesson/lesson.php?id=$cm->id&action=addpage&pageid=0"); // no pages - redirect to add pages
} else {
lesson_set_message(get_string('lessonpagelinkingbroken', 'lesson')); // ok, bad mojo
}
}
}
add_to_log($course->id, 'lesson', 'start', 'view.php?id='. $cm->id, $lesson->id, $cm->id);
// if no pageid given see if the lesson has been started
if ($grades = get_records_select('lesson_grades', 'lessonid = '. $lesson->id .' AND userid = '. $USER->id,
'grade DESC')) {
$retries = count($grades);
} else {
$retries = 0;
}
if ($retries) {
$attemptflag = true;
}
if (isset($USER->modattempts[$lesson->id])) {
unset($USER->modattempts[$lesson->id]); // if no pageid, then student is NOT reviewing
}
// if there are any questions have been answered correctly in this attempt
if ($attempts = get_records_select('lesson_attempts',
"lessonid = $lesson->id AND userid = $USER->id AND retry = $retries AND
correct = 1", 'timeseen DESC')) {
foreach ($attempts as $attempt) {
$jumpto = get_field('lesson_answers', 'jumpto', 'id', $attempt->answerid);
// convert the jumpto to a proper page id
if ($jumpto == 0) { // unlikely value!
$lastpageseen = $attempt->pageid;
} elseif ($jumpto == LESSON_NEXTPAGE) {
if (!$lastpageseen = get_field('lesson_pages', 'nextpageid', 'id',
$attempt->pageid)) {
// no nextpage go to end of lesson
$lastpageseen = LESSON_EOL;
}
} else {
$lastpageseen = $jumpto;
}
break; // only look at the latest correct attempt
}
} else {
$attempts = NULL;
}
if ($branchtables = get_records_select('lesson_branch',
"lessonid = $lesson->id AND userid = $USER->id AND retry = $retries", 'timeseen DESC')) {
// in here, user has viewed a branch table
$lastbranchtable = current($branchtables);
if ($attempts != NULL) {
foreach($attempts as $attempt) {
if ($lastbranchtable->timeseen > $attempt->timeseen) {
// branch table was viewed later than the last attempt
$lastpageseen = $lastbranchtable->pageid;
}
break;
}
} else {
// hasnt answered any questions but has viewed a branch table
$lastpageseen = $lastbranchtable->pageid;
}
}
//if ($lastpageseen != $firstpageid) {
if (isset($lastpageseen) and count_records('lesson_attempts', 'lessonid', $lesson->id, 'userid', $USER->id, 'retry', $retries) > 0) {
// get the first page
if (!$firstpageid = get_field('lesson_pages', 'id', 'lessonid', $lesson->id,
'prevpageid', 0)) {
error('Navigation: first page not found');
}
lesson_print_header($cm, $course, $lesson);
if ($lesson->timed) {
if ($lesson->retake) {
print_simple_box('<p align="center">'. get_string('leftduringtimed', 'lesson') .'</p>', 'center');
echo '<div align="center" class="lessonbutton standardbutton">'.
'<a href="view.php?id='.$cm->id.'&pageid='.$firstpageid.'&startlastseen=no">'.
get_string('continue', 'lesson').'</a></div>';
} else {
print_simple_box_start('center');
echo '<div align="center">';
echo get_string('leftduringtimednoretake', 'lesson');
echo '<br /><br /><div class="lessonbutton standardbutton"><a href="../../course/view.php?id='. $course->id .'">'. get_string('returntocourse', 'lesson') .'</a></div>';
echo '</div>';
print_simple_box_end();
}
} else {
print_simple_box("<p align=\"center\">".get_string('youhaveseen','lesson').'</p>',
"center");
echo '<div align="center">';
echo '<span class="lessonbutton standardbutton">'.
'<a href="view.php?id='.$cm->id.'&pageid='.$lastpageseen.'&startlastseen=yes">'.
get_string('yes').'</a></span> ';
echo '<span class="lessonbutton standardbutton">'.
'<a href="view.php?id='.$cm->id.'&pageid='.$firstpageid.'&startlastseen=no">'.
get_string('no').'</a></div>';
echo '</span>';
}
print_footer($course);
exit();
}
if ($grades) {
foreach ($grades as $grade) {
$bestgrade = $grade->grade;
break;
}
if (!$lesson->retake) {
lesson_print_header($cm, $course, $lesson, 'view');
print_simple_box_start('center');
echo "<div align=\"center\">";
echo get_string("noretake", "lesson");
echo "<br /><br /><div class=\"lessonbutton standardbutton\"><a href=\"../../course/view.php?id=$course->id\">".get_string('returntocourse', 'lesson').'</a></div>';
echo "</div>";
print_simple_box_end();
print_footer($course);
exit();
//redirect("../../course/view.php?id=$course->id", get_string("alreadytaken", "lesson"));
// allow student to retake course even if they have the maximum grade
// } elseif ($bestgrade == 100) {
// redirect("../../course/view.php?id=$course->id", get_string("maximumgradeachieved",
// "lesson"));
}
}
// start at the first page
if (!$pageid = get_field('lesson_pages', 'id', 'lessonid', $lesson->id, 'prevpageid', 0)) {
error('Navigation: first page not found');
}
/// This is the code for starting a timed test
if(!isset($USER->startlesson[$lesson->id]) && !has_capability('mod/lesson:manage', $context)) {
$USER->startlesson[$lesson->id] = true;
$startlesson = new stdClass;
$startlesson->lessonid = $lesson->id;
$startlesson->userid = $USER->id;
$startlesson->starttime = time();
$startlesson->lessontime = time();
if (!insert_record('lesson_timer', $startlesson)) {
error('Error: could not insert row into lesson_timer table');
}
if ($lesson->timed) {
lesson_set_message(get_string('maxtimewarning', 'lesson', $lesson->maxtime), 'center');
}
}
}
if ($pageid != LESSON_EOL) {
/// This is the code updates the lessontime for a timed test
if ($startlastseen = optional_param('startlastseen', '', PARAM_ALPHA)) { /// this deletes old records not totally sure if this is necessary anymore
if ($startlastseen == 'no') {
if ($grades = get_records_select('lesson_grades', "lessonid = $lesson->id AND userid = $USER->id",
'grade DESC')) {
$retries = count($grades);
} else {
$retries = 0;
}
if (!delete_records('lesson_attempts', 'userid', $USER->id, 'lessonid', $lesson->id, 'retry', $retries)) {
error('Error: could not delete old attempts');
}
if (!delete_records('lesson_branch', 'userid', $USER->id, 'lessonid', $lesson->id, 'retry', $retries)) {
error('Error: could not delete old seen branches');
}
}
}
add_to_log($course->id, 'lesson', 'view', 'view.php?id='. $cm->id, $pageid, $cm->id);
if (!$page = get_record('lesson_pages', 'id', $pageid)) {
error('Navigation: the page record not found');
}
if ($page->qtype == LESSON_CLUSTER) { //this only gets called when a user starts up a new lesson and the first page is a cluster page
if (!has_capability('mod/lesson:manage', $context)) {
// get new id
$pageid = lesson_cluster_jump($lesson->id, $USER->id, $pageid);
// get new page info
if (!$page = get_record('lesson_pages', 'id', $pageid)) {
error('Navigation: the page record not found');
}
add_to_log($course->id, 'lesson', 'view', 'view.php?id='. $cm->id, $pageid, $cm->id);
} else {
// get the next page
$pageid = $page->nextpageid;
if (!$page = get_record('lesson_pages', 'id', $pageid)) {
error('Navigation: the page record not found');
}
}
} elseif ($page->qtype == LESSON_ENDOFCLUSTER) {
if ($page->nextpageid == 0) {
$nextpageid = LESSON_EOL;
} else {
$nextpageid = $page->nextpageid;
}
redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&pageid=$nextpageid");
}
// check to see if the user can see the left menu
if (!has_capability('mod/lesson:manage', $context)) {
$lesson->displayleft = lesson_displayleftif($lesson);
}
// This is where several messages (usually warnings) are displayed
// all of this is displayed above the actual page
if (!empty($lesson->mediafile)) {
$url = '/mod/lesson/mediafile.php?id='.$cm->id;
$options = 'menubar=0,location=0,left=5,top=5,scrollbars,resizable,width='. $lesson->mediawidth .',height='. $lesson->mediaheight;
$name = 'lessonmediafile';
$medialink = link_to_popup_window ($url, $name, get_string('mediafilepopup', 'lesson'), '', '', get_string('mediafilepopup', 'lesson'), $options, true);
$medialink .= helpbutton("mediafilestudent", get_string("mediafile", "lesson"), "lesson", true, false, '', true);
} else {
$medialink = '';
}
// clock code
// get time information for this user
if(!has_capability('mod/lesson:manage', $context)) {
if (!$timer = get_records_select('lesson_timer', "lessonid = $lesson->id AND userid = $USER->id", 'starttime')) {
error('Error: could not find records');
} else {
$timer = array_pop($timer); // this will get the latest start time record
}
}
$startlastseen = optional_param('startlastseen', '', PARAM_ALPHA);
if ($startlastseen == 'yes') { // continue a previous test, need to update the clock (think this option is disabled atm)
$timer->starttime = time() - ($timer->lessontime - $timer->starttime);
$timer->lessontime = time();
} else if ($startlastseen == 'no') { // starting over
// starting over, so reset the clock
$timer->starttime = time();
$timer->lessontime = time();
}
// for timed lessons, display clock
if ($lesson->timed) {
if(has_capability('mod/lesson:manage', $context)) {
lesson_set_message(get_string('teachertimerwarning', 'lesson'));
} else {
if ((($timer->starttime + $lesson->maxtime * 60) - time()) <= 0) {
lesson_set_message(get_string('eolstudentoutoftime', 'lesson'));
redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&pageid=".LESSON_EOL."&outoftime=normal", get_string("outoftime", "lesson"));
}
// update clock when viewing a new page... no special treatment
if ((($timer->starttime + $lesson->maxtime * 60) - time()) < 60) {
lesson_set_message(get_string('studentoneminwarning', 'lesson'));
}
}
}
// update the clock
if (!has_capability('mod/lesson:manage', $context)) {
$timer->lessontime = time();
if (!update_record('lesson_timer', $timer)) {
error('Error: could not update lesson_timer table');
}
}
// before we output everything check to see if the page is a EOB, if so jump directly
// to it's associated branch table
if ($page->qtype == LESSON_ENDOFBRANCH) {
if ($answers = get_records('lesson_answers', 'pageid', $page->id, 'id')) {
// print_heading(get_string('endofbranch', 'lesson'));
foreach ($answers as $answer) {
// just need the first answer
if ($answer->jumpto == LESSON_RANDOMBRANCH) {
$answer->jumpto = lesson_unseen_branch_jump($lesson->id, $USER->id);
} elseif ($answer->jumpto == LESSON_CLUSTERJUMP) {
if (!has_capability('mod/lesson:manage', $context)) {
$answer->jumpto = lesson_cluster_jump($lesson->id, $USER->id, $pageid);
} else {
if ($page->nextpageid == 0) {
$answer->jumpto = LESSON_EOL;
} else {
$answer->jumpto = $page->nextpageid;
}
}
} else if ($answer->jumpto == LESSON_NEXTPAGE) {
if ($page->nextpageid == 0) {
$answer->jumpto = LESSON_EOL;
} else {
$answer->jumpto = $page->nextpageid;
}
} else if ($answer->jumpto == 0) {
$answer->jumpto = $page->id;
} else if ($answer->jumpto == LESSON_PREVIOUSPAGE) {
$answer->jumpto = $page->prevpageid;
}
redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&pageid=$answer->jumpto");
break;
}
} else {
error('Navigation: No answers on EOB');
}
}
/// This is the warning msg for teachers to inform them that cluster and unseen does not work while logged in as a teacher
if(has_capability('mod/lesson:manage', $context)) {
if (lesson_display_teacher_warning($lesson->id)) {
$warningvars->cluster = get_string('clusterjump', 'lesson');
$warningvars->unseen = get_string('unseenpageinbranch', 'lesson');
lesson_set_message(get_string('teacherjumpwarning', 'lesson', $warningvars));
}
}
if ($page->qtype == LESSON_BRANCHTABLE) {
if ($lesson->minquestions and !has_capability('mod/lesson:manage', $context)) {
// tell student how many questions they have seen, how many are required and their grade
$ntries = count_records("lesson_grades", "lessonid", $lesson->id, "userid", $USER->id);
$gradeinfo = lesson_grade($lesson, $ntries);
if ($gradeinfo->attempts) {
if ($gradeinfo->nquestions < $lesson->minquestions) {
$a = new stdClass;
$a->nquestions = $gradeinfo->nquestions;
$a->minquestions = $lesson->minquestions;
lesson_set_message(get_string('numberofpagesviewednotice', 'lesson', $a));
}
lesson_set_message(get_string("numberofcorrectanswers", "lesson", $gradeinfo->earned), 'notify');
$a = new stdClass;
$a->grade = number_format($gradeinfo->grade * $lesson->grade / 100, 1);
$a->total = $lesson->grade;
lesson_set_message(get_string('yourcurrentgradeisoutof', 'lesson', $a), 'notify');
}
}
}
lesson_print_header($cm, $course, $lesson, 'view');
require($CFG->dirroot.'/mod/lesson/viewstart.html');
// now starting to print the page's contents
if ($page->qtype == LESSON_BRANCHTABLE) {
print_heading(format_string($page->title));
} else {
$lesson->slideshow = false; // turn off slide show for all pages other than LESSON_BRANTCHTABLE
}
if (!$lesson->slideshow) {
$options = new stdClass;
$options->noclean = true;
print_simple_box('<div class="contents">'.
format_text($page->contents, FORMAT_MOODLE, $options).
'</div>', 'center');
}
// this is for modattempts option. Find the users previous answer to this page,
// and then display it below in answer processing
if (isset($USER->modattempts[$lesson->id])) {
$retries = count_records('lesson_grades', "lessonid", $lesson->id, "userid", $USER->id);
$retries--;
if (! $attempts = get_records_select("lesson_attempts", "lessonid = $lesson->id AND userid = $USER->id AND pageid = $page->id AND retry = $retries", "timeseen")) {
error("Previous attempt record could not be found!");
}
$attempt = end($attempts);
}
// get the answers in a set order, the id order
if ($answers = get_records("lesson_answers", "pageid", $page->id, "id")) {
echo "<form name=\"answerform\" method =\"post\" action=\"lesson.php\">";
echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
echo "<input type=\"hidden\" name=\"action\" value=\"continue\" />";
echo "<input type=\"hidden\" name=\"pageid\" value=\"$pageid\" />";
echo "<input type=\"hidden\" name=\"sesskey\" value=\"".$USER->sesskey."\" />";
if (!$lesson->slideshow) {
if ($page->qtype != LESSON_BRANCHTABLE) {
print_simple_box_start("center");
}
echo '<table width="100%">';
}
// default format text options
$options = new stdClass;
$options->para = false; // no <p></p>
$options->noclean = true;
switch ($page->qtype) {
case LESSON_SHORTANSWER :
case LESSON_NUMERICAL :
if (isset($USER->modattempts[$lesson->id])) {
$value = "value=\"$attempt->useranswer\"";
} else {
$value = "";
}
echo '<tr><td align="center"><label for="answer">'.get_string('youranswer', 'lesson').'</label>'.
": <input type=\"text\" id=\"answer\" name=\"answer\" size=\"50\" maxlength=\"200\" $value />\n";
echo '</table>';
print_simple_box_end();
lesson_print_submit_link(get_string('pleaseenteryouranswerinthebox', 'lesson'), 'answerform');
break;
case LESSON_TRUEFALSE :
shuffle($answers);
$i = 0;
foreach ($answers as $answer) {
echo '<tr><td valign="top">';
if (isset($USER->modattempts[$lesson->id]) && $answer->id == $attempt->answerid) {
$checked = 'checked="checked"';
} else {
$checked = '';
}
echo "<input type=\"radio\" id=\"answerid$i\" name=\"answerid\" value=\"{$answer->id}\" $checked />";
echo "</td><td>";
echo "<label for=\"answerid$i\">".format_text(trim($answer->answer), FORMAT_MOODLE, $options).'</label>';
echo '</td></tr>';
if ($answer != end($answers)) {
echo "<tr><td><br></td></tr>";
}
$i++;
}
echo '</table>';
print_simple_box_end();
lesson_print_submit_link(get_string('pleasecheckoneanswer', 'lesson'), 'answerform');
break;
case LESSON_MULTICHOICE :
$i = 0;
shuffle($answers);
foreach ($answers as $answer) {
echo '<tr><td valign="top">';
if ($page->qoption) {
$checked = '';
if (isset($USER->modattempts[$lesson->id])) {
$answerids = explode(",", $attempt->useranswer);
if (in_array($answer->id, $answerids)) {
$checked = ' checked="checked"';
} else {
$checked = '';
}
}
// more than one answer allowed
echo "<input type=\"checkbox\" id=\"answerid$i\" name=\"answer[$i]\" value=\"{$answer->id}\"$checked />";
} else {
if (isset($USER->modattempts[$lesson->id]) && $answer->id == $attempt->answerid) {
$checked = ' checked="checked"';
} else {
$checked = '';
}
// only one answer allowed
echo "<input type=\"radio\" id=\"answerid$i\" name=\"answerid\" value=\"{$answer->id}\"$checked />";
}
echo '</td><td>';
echo "<label for=\"answerid$i\" >".format_text(trim($answer->answer), FORMAT_MOODLE, $options).'</label>';
echo '</td></tr>';
if ($answer != end($answers)) {
echo '<tr><td><br></td></tr>';
}
$i++;
}
echo '</table>';
print_simple_box_end();
if ($page->qoption) {
$linkname = get_string('pleasecheckoneormoreanswers', 'lesson');
} else {
$linkname = get_string('pleasecheckoneanswer', 'lesson');
}
lesson_print_submit_link($linkname, 'answerform');
break;
case LESSON_MATCHING :
echo '<tr><td><table width="100%">';
// don't suffle answers (could be an option??)
foreach ($answers as $answer) {
// get all the response
if ($answer->response != NULL) {
$responses[] = trim($answer->response);
}
}
shuffle($responses);
$responses = array_unique($responses);
$responseoptions = array();
foreach ($responses as $response) {
$responseoptions[htmlspecialchars(trim($response))] = $response;
}
if (isset($USER->modattempts[$lesson->id])) {
$useranswers = explode(',', $attempt->useranswer);
$t = 0;
}
foreach ($answers as $answer) {
if ($answer->response != NULL) {
echo '<tr><td align="right">';
echo "<b><label for=\"menuresponse[$answer->id]\">".
format_text($answer->answer,FORMAT_MOODLE,$options).
'</label>: </b></td><td valign="bottom">';
if (isset($USER->modattempts[$lesson->id])) {
$selected = htmlspecialchars(trim($answers[$useranswers[$t]]->response)); // gets the user's previous answer
choose_from_menu ($responseoptions, "response[$answer->id]", $selected);
$t++;
} else {
choose_from_menu ($responseoptions, "response[$answer->id]");
}
echo '</td></tr>';
if ($answer != end($answers)) {
echo '<tr><td><br /></td></tr>';
}
}
}
echo '</table></table>';
print_simple_box_end();
lesson_print_submit_link(get_string('pleasematchtheabovepairs', 'lesson'), 'answerform');
break;
case LESSON_BRANCHTABLE :
$options = new stdClass;
$options->para = false;
$buttons = array('next' => array(), 'prev' => array(), 'other' => array());
/// seperate out next and previous jumps from the other jumps
foreach ($answers as $answer) {
if ($answer->jumpto == LESSON_NEXTPAGE) {
$type = 'next';
} else if ($answer->jumpto == LESSON_PREVIOUSPAGE) {
$type = 'prev';
} else {
$type = 'other';
}
$buttons[$type][] = '<a href="javascript:document.answerform.jumpto.value='.$answer->jumpto.';document.answerform.submit();">'.
strip_tags(format_text($answer->answer, FORMAT_MOODLE, $options)).'</a>';
}
/// set the order and orientation (order is very important for the divs to work for horizontal!)
if ($page->layout) {
$orientation = 'horizontal';
$a = 'a';
$b = 'b';
$c = 'c';
$implode = ' ';
$implode2 = "\n ";
if (empty($buttons['other'])) {
$buttons['other'][] = ' '; // very critical! If nothing is in the middle,
// then the div style float left/right will not
// render properly with next/previous buttons
}
} else {
$orientation = 'vertical';
$a = 'c';
$b = 'a';
$c = 'b';
$implode = '<br /><br />';
$implode2 = "<br /><br />\n ";
}
$buttonsarranged = array();
$buttonsarranged[$a] = '<span class="lessonbutton prevbutton prev'.$orientation.'">'.implode($implode, $buttons['prev']).'</span>';
$buttonsarranged[$b] = '<span class="lessonbutton nextbutton next'.$orientation.'">'.implode($implode, $buttons['next']).'</span>';
$buttonsarranged[$c] = '<span class="lessonbutton standardbutton standard'.$orientation.'">'.implode($implode, $buttons['other']).'</span>';
ksort($buttonsarranged); // sort by key
$fullbuttonhtml = "\n<div class=\"branchbuttoncontainer\">\n " . implode($implode2, $buttonsarranged). "\n</div>\n";
if ($lesson->slideshow) {
echo '<div class="branchslidetop">' . $fullbuttonhtml . '</div>';
$options = new stdClass;
$options->noclean = true;
echo '<div class="contents">'.format_text($page->contents, FORMAT_MOODLE, $options)."</div>\n";
echo '</div><!--end slideshow div-->';
echo '<div class="branchslidebottom">' . $fullbuttonhtml . '</div>';
} else {
echo '<tr><td>';
print_simple_box($fullbuttonhtml, 'center');
echo '</td></tr></table>'; // ends the answers table
}
echo '<input type="hidden" name="jumpto" />';
break;
case LESSON_ESSAY :
if (isset($USER->modattempts[$lesson->id])) {
$essayinfo = unserialize($attempt->useranswer);
$value = $essayinfo->answer;
} else {
$value = "";
}
echo '<tr><td align="center" valign="top" nowrap><label for="answer">'.get_string("youranswer", "lesson").'</label>:</td><td>'.
'<textarea id="answer" name="answer" rows="15" cols="60">'.$value."</textarea>\n";
echo '</td></tr></table>';
print_simple_box_end();
lesson_print_submit_link(get_string('pleaseenteryouranswerinthebox', 'lesson'), 'answerform');
break;
}
echo "</form>\n";
} else {
// a page without answers - find the next (logical) page
echo "<form name=\"pageform\" method =\"post\" action=\"$CFG->wwwroot/mod/lesson/view.php\">\n";
echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
if ($lesson->nextpagedefault) {
// in Flash Card mode...
// ...first get number of retakes
$nretakes = count_records("lesson_grades", "lessonid", $lesson->id, "userid", $USER->id);
// ...then get the page ids (lessonid the 5th param is needed to make get_records play)
$allpages = get_records("lesson_pages", "lessonid", $lesson->id, "id", "id,lessonid");
shuffle ($allpages);
$found = false;
if ($lesson->nextpagedefault == LESSON_UNSEENPAGE) {
foreach ($allpages as $thispage) {
if (!count_records("lesson_attempts", "pageid", $thispage->id, "userid",
$USER->id, "retry", $nretakes)) {
$found = true;
break;
}
}
} elseif ($lesson->nextpagedefault == LESSON_UNANSWEREDPAGE) {
foreach ($allpages as $thispage) {
if (!count_records_select("lesson_attempts", "pageid = $thispage->id AND
userid = $USER->id AND correct = 1 AND retry = $nretakes")) {
$found = true;
break;
}
}
}
if ($found) {
$newpageid = $thispage->id;
if ($lesson->maxpages) {
// check number of pages viewed (in the lesson)
if (count_records("lesson_attempts", "lessonid", $lesson->id, "userid", $USER->id,
"retry", $nretakes) >= $lesson->maxpages) {
$newpageid = LESSON_EOL;
}
}
} else {
$newpageid = LESSON_EOL;
}
} else {
// in normal lesson mode...
if (!$newpageid = get_field("lesson_pages", "nextpageid", "id", $pageid)) {
// this is the last page - flag end of lesson
$newpageid = LESSON_EOL;
}
}
echo "<input type=\"hidden\" name=\"pageid\" value=\"$newpageid\" />\n";
lesson_print_submit_link(get_string('continue', 'lesson'), 'pageform');
echo "</form>\n";
}
// Finish of the page
lesson_print_progress_bar($lesson, $course);
require($CFG->dirroot.'/mod/lesson/viewend.html');
} else {
// end of lesson reached work out grade
// Used to check to see if the student ran out of time
$outoftime = optional_param('outoftime', '', PARAM_ALPHA);
// Update the clock / get time information for this user
if (!has_capability('mod/lesson:manage', $context)) {
unset($USER->startlesson[$lesson->id]);
if (!$timer = get_records_select('lesson_timer', "lessonid = $lesson->id AND userid = $USER->id", 'starttime')) {
error('Error: could not find records');
} else {
$timer = array_pop($timer); // this will get the latest start time record
}
$timer->lessontime = time();
if (!update_record("lesson_timer", $timer)) {
error("Error: could not update lesson_timer table");
}
}
add_to_log($course->id, "lesson", "end", "view.php?id=$cm->id", "$lesson->id", $cm->id);
lesson_print_header($cm, $course, $lesson, 'view');
print_heading(get_string("congratulations", "lesson"));
print_simple_box_start("center");
$ntries = count_records("lesson_grades", "lessonid", $lesson->id, "userid", $USER->id);
if (isset($USER->modattempts[$lesson->id])) {
$ntries--; // need to look at the old attempts :)
}
if (!has_capability('mod/lesson:manage', $context)) {
$gradeinfo = lesson_grade($lesson, $ntries);
if ($gradeinfo->attempts) {
if (!$lesson->custom) {
echo "<p align=\"center\">".get_string("numberofpagesviewed", "lesson", $gradeinfo->nquestions).
"</p>\n";
if ($lesson->minquestions) {
if ($gradeinfo->nquestions < $lesson->minquestions) {
// print a warning and set nviewed to minquestions
echo "<p align=\"center\">".get_string("youshouldview", "lesson",
$lesson->minquestions)."</p>\n";
}
}
echo "<p align=\"center\">".get_string("numberofcorrectanswers", "lesson", $gradeinfo->earned).
"</p>\n";
}
$a = new stdClass;
$a->score = $gradeinfo->earned;
$a->grade = $gradeinfo->total;
if ($gradeinfo->nmanual) {
$a->tempmaxgrade = $gradeinfo->total - $gradeinfo->manualpoints;
$a->essayquestions = $gradeinfo->nmanual;
echo "<div align=\"center\">".get_string("displayscorewithessays", "lesson", $a)."</div>";
} else {
echo "<div align=\"center\">".get_string("displayscorewithoutessays", "lesson", $a)."</div>";
}
$a = new stdClass;
$a->grade = number_format($gradeinfo->grade * $lesson->grade / 100, 1);
$a->total = $lesson->grade;
echo "<p align=\"center\">".get_string('yourcurrentgradeisoutof', 'lesson', $a)."</p>\n";
$grade->lessonid = $lesson->id;
$grade->userid = $USER->id;
$grade->grade = $gradeinfo->grade;
$grade->completed = time();
if (!$lesson->practice) {
if (isset($USER->modattempts[$lesson->id])) { // if reviewing, make sure update old grade record
if (!$grades = get_records_select("lesson_grades", "lessonid = $lesson->id and userid = $USER->id", "completed")) {
error("Could not find Grade Records");
}
$oldgrade = end($grades);
$grade->id = $oldgrade->id;
if (!$update = update_record("lesson_grades", $grade)) {
error("Navigation: grade not updated");
}
} else {
if (!$newgradeid = insert_record("lesson_grades", $grade)) {
error("Navigation: grade not inserted");
}
}
} else {
if (!delete_records("lesson_attempts", "lessonid", $lesson->id, "userid", $USER->id, "retry", $ntries)) {
error("Could not delete lesson attempts");
}
}
} else {
if ($lesson->timed) {
if ($outoftime == 'normal') {
$grade = new stdClass;
$grade->lessonid = $lesson->id;
$grade->userid = $USER->id;
$grade->grade = 0;
$grade->completed = time();
if (!$lesson->practice) {
if (!$newgradeid = insert_record("lesson_grades", $grade)) {
error("Navigation: grade not inserted");
}
}
echo get_string("eolstudentoutoftimenoanswers", "lesson");
}
} else {
echo get_string("welldone", "lesson");
}
}
} else {
// display for teacher
echo "<p align=\"center\">".get_string("displayofgrade", "lesson")."</p>\n";
}
print_simple_box_end(); //End of Lesson button to Continue.
// after all the grade processing, check to see if "Show Grades" is off for the course
// if yes, redirect to the course page
if (!$course->showgrades) {
redirect($CFG->wwwroot.'/course/view.php?id='.$course->id);
}
// high scores code
if ($lesson->highscores && !has_capability('mod/lesson:manage', $context) && !$lesson->practice) {
echo "<div align=\"center\"><br>";
if ($grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
$madeit = false;
if ($highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) {
// get all the high scores into an array
$topscores = array();
$uniquescores = array();
foreach ($highscores as $highscore) {
$grade = $grades[$highscore->gradeid]->grade;
$topscores[] = $grade;
$uniquescores[$grade] = 1;
}
// sort to find the lowest score
sort($topscores);
$lowscore = $topscores[0];
if ($gradeinfo->grade >= $lowscore || count($uniquescores) <= $lesson->maxhighscores) {
$madeit = true;
}
}
if (!$highscores or $madeit) {
echo '<p>'.get_string("youmadehighscore", "lesson", $lesson->maxhighscores).
'</p><p>
<form method="post" name="highscores" action="'.$CFG->wwwroot.'/mod/lesson/highscores.php">
<input type="hidden" name="mode" value="add" />
<input type="hidden" name="id" value="'.$cm->id.'" />
<input type="hidden" name="sesskey" value="'.sesskey().'" />
<p>';
lesson_print_submit_link(get_string('clicktopost', 'lesson'), 'highscores');
echo '</p>
</form>';
} else {
echo get_string("nothighscore", "lesson", $lesson->maxhighscores)."<br>";
}
}
echo "<br /><div style=\"padding: 5px;\" class=\"lessonbutton standardbutton\"><a href=\"$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id&link=1\">".get_string("viewhighscores", "lesson").'</a></div>';
echo "</div>";
}
if ($lesson->modattempts && !has_capability('mod/lesson:manage', $context)) {
// make sure if the student is reviewing, that he/she sees the same pages/page path that he/she saw the first time
// look at the attempt records to find the first QUESTION page that the user answered, then use that page id
// to pass to view again. This is slick cause it wont call the empty($pageid) code
// $ntries is decremented above
if (!$attempts = get_records_select("lesson_attempts", "lessonid = $lesson->id AND userid = $USER->id AND retry = $ntries", "timeseen")) {
$attempts = array();
}
$firstattempt = current($attempts);
$pageid = $firstattempt->pageid;
// IF the student wishes to review, need to know the last question page that the student answered. This will help to make
// sure that the student can leave the lesson via pushing the continue button.
$lastattempt = end($attempts);
$USER->modattempts[$lesson->id] = $lastattempt->pageid;
echo "<div align=\"center\" style=\"padding: 5px;\" class=\"lessonbutton standardbutton\"><a href=\"view.php?id=$cm->id&pageid=$pageid\">".get_string("reviewlesson", "lesson")."</a></div>\n";
} elseif ($lesson->modattempts && has_capability('mod/lesson:manage', $context)) {
echo "<p align=\"center\">".get_string("modattemptsnoteacher", "lesson")."</p>";
}
if ($lesson->activitylink) {
if ($module = get_record('course_modules', 'id', $lesson->activitylink)) {
if ($modname = get_field('modules', 'name', 'id', $module->module))
if ($instance = get_record($modname, 'id', $module->instance)) {