mirrored from git://git.moodle.org/moodle.git
/
renderer.php
1475 lines (1320 loc) · 57.8 KB
/
renderer.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
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\output;
use cm_info;
use coding_exception;
use context;
use context_module;
use html_table;
use html_table_cell;
use html_writer;
use mod_quiz\access_manager;
use mod_quiz\form\preflight_check_form;
use mod_quiz\question\display_options;
use mod_quiz\quiz_attempt;
use moodle_url;
use plugin_renderer_base;
use popup_action;
use question_display_options;
use mod_quiz\quiz_settings;
use renderable;
use single_button;
use stdClass;
/**
* The main renderer for the quiz module.
*
* @package mod_quiz
* @category output
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends plugin_renderer_base {
/**
* Builds the review page
*
* @param quiz_attempt $attemptobj an instance of quiz_attempt.
* @param array $slots of slots to be displayed.
* @param int $page the current page number
* @param bool $showall whether to show entire attempt on one page.
* @param bool $lastpage if true the current page is the last page.
* @param display_options $displayoptions instance of display_options.
* @param array $summarydata contains all table data
* @return string HTML to display.
*/
public function review_page(quiz_attempt $attemptobj, $slots, $page, $showall,
$lastpage, display_options $displayoptions, $summarydata) {
$output = '';
$output .= $this->header();
$output .= $this->review_summary_table($summarydata, $page);
$output .= $this->review_form($page, $showall, $displayoptions,
$this->questions($attemptobj, true, $slots, $page, $showall, $displayoptions),
$attemptobj);
$output .= $this->review_next_navigation($attemptobj, $page, $lastpage, $showall);
$output .= $this->footer();
return $output;
}
/**
* Renders the review question pop-up.
*
* @param quiz_attempt $attemptobj an instance of quiz_attempt.
* @param int $slot which question to display.
* @param int $seq which step of the question attempt to show. null = latest.
* @param display_options $displayoptions instance of display_options.
* @param array $summarydata contains all table data
* @return string HTML to display.
*/
public function review_question_page(quiz_attempt $attemptobj, $slot, $seq,
display_options $displayoptions, $summarydata) {
$output = '';
$output .= $this->header();
$output .= $this->review_summary_table($summarydata, 0);
if (!is_null($seq)) {
$output .= $attemptobj->render_question_at_step($slot, $seq, true, $this);
} else {
$output .= $attemptobj->render_question($slot, true, $this);
}
$output .= $this->close_window_button();
$output .= $this->footer();
return $output;
}
/**
* Renders the review question pop-up.
*
* @param quiz_attempt $attemptobj an instance of quiz_attempt.
* @param string $message Why the review is not allowed.
* @return string html to output.
*/
public function review_question_not_allowed(quiz_attempt $attemptobj, $message) {
$output = '';
$output .= $this->header();
$output .= $this->heading(format_string($attemptobj->get_quiz_name(), true,
["context" => $attemptobj->get_quizobj()->get_context()]));
$output .= $this->notification($message);
$output .= $this->close_window_button();
$output .= $this->footer();
return $output;
}
/**
* Filters the summarydata array.
*
* @param array $summarydata contains row data for table
* @param int $page the current page number
* @return array updated version of the $summarydata array.
*/
protected function filter_review_summary_table($summarydata, $page) {
if ($page == 0) {
return $summarydata;
}
// Only show some of summary table on subsequent pages.
foreach ($summarydata as $key => $rowdata) {
if (!in_array($key, ['user', 'attemptlist'])) {
unset($summarydata[$key]);
}
}
return $summarydata;
}
/**
* Outputs the table containing data from summary data array
*
* @param array $summarydata contains row data for table
* @param int $page contains the current page number
* @return string HTML to display.
*/
public function review_summary_table($summarydata, $page) {
$summarydata = $this->filter_review_summary_table($summarydata, $page);
if (empty($summarydata)) {
return '';
}
$output = '';
$output .= html_writer::start_tag('table', [
'class' => 'generaltable generalbox quizreviewsummary']);
$output .= html_writer::start_tag('tbody');
foreach ($summarydata as $rowdata) {
if ($rowdata['title'] instanceof renderable) {
$title = $this->render($rowdata['title']);
} else {
$title = $rowdata['title'];
}
if ($rowdata['content'] instanceof renderable) {
$content = $this->render($rowdata['content']);
} else {
$content = $rowdata['content'];
}
$output .= html_writer::tag('tr',
html_writer::tag('th', $title, ['class' => 'cell', 'scope' => 'row']) .
html_writer::tag('td', $content, ['class' => 'cell'])
);
}
$output .= html_writer::end_tag('tbody');
$output .= html_writer::end_tag('table');
return $output;
}
/**
* Renders each question
*
* @param quiz_attempt $attemptobj instance of quiz_attempt
* @param bool $reviewing
* @param array $slots array of integers relating to questions
* @param int $page current page number
* @param bool $showall if true shows attempt on single page
* @param display_options $displayoptions instance of display_options
*/
public function questions(quiz_attempt $attemptobj, $reviewing, $slots, $page, $showall,
display_options $displayoptions) {
$output = '';
foreach ($slots as $slot) {
$output .= $attemptobj->render_question($slot, $reviewing, $this,
$attemptobj->review_url($slot, $page, $showall));
}
return $output;
}
/**
* Renders the main bit of the review page.
*
* @param int $page current page number
* @param bool $showall if true display attempt on one page
* @param display_options $displayoptions instance of display_options
* @param string $content the rendered display of each question
* @param quiz_attempt $attemptobj instance of quiz_attempt
* @return string HTML to display.
*/
public function review_form($page, $showall, $displayoptions, $content, $attemptobj) {
if ($displayoptions->flags != question_display_options::EDITABLE) {
return $content;
}
$this->page->requires->js_init_call('M.mod_quiz.init_review_form', null, false,
quiz_get_js_module());
$output = '';
$output .= html_writer::start_tag('form', ['action' => $attemptobj->review_url(null,
$page, $showall), 'method' => 'post', 'class' => 'questionflagsaveform']);
$output .= html_writer::start_tag('div');
$output .= $content;
$output .= html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'sesskey',
'value' => sesskey()]);
$output .= html_writer::start_tag('div', ['class' => 'submitbtns']);
$output .= html_writer::empty_tag('input', ['type' => 'submit',
'class' => 'questionflagsavebutton btn btn-secondary', 'name' => 'savingflags',
'value' => get_string('saveflags', 'question')]);
$output .= html_writer::end_tag('div');
$output .= html_writer::end_tag('div');
$output .= html_writer::end_tag('form');
return $output;
}
/**
* Returns either a link or button.
*
* @param quiz_attempt $attemptobj instance of quiz_attempt
*/
public function finish_review_link(quiz_attempt $attemptobj) {
$url = $attemptobj->view_url();
if ($attemptobj->get_access_manager(time())->attempt_must_be_in_popup()) {
$this->page->requires->js_init_call('M.mod_quiz.secure_window.init_close_button',
[$url->out(false)], false, quiz_get_js_module());
return html_writer::empty_tag('input', ['type' => 'button',
'value' => get_string('finishreview', 'quiz'),
'id' => 'secureclosebutton',
'class' => 'mod_quiz-next-nav btn btn-primary']);
} else {
return html_writer::link($url, get_string('finishreview', 'quiz'),
['class' => 'mod_quiz-next-nav']);
}
}
/**
* Creates the navigation links/buttons at the bottom of the review attempt page.
*
* Note, the name of this function is no longer accurate, but when the design
* changed, it was decided to keep the old name for backwards compatibility.
*
* @param quiz_attempt $attemptobj instance of quiz_attempt
* @param int $page the current page
* @param bool $lastpage if true current page is the last page
* @param bool|null $showall if true, the URL will be to review the entire attempt on one page,
* and $page will be ignored. If null, a sensible default will be chosen.
*
* @return string HTML fragment.
*/
public function review_next_navigation(quiz_attempt $attemptobj, $page, $lastpage, $showall = null) {
$nav = '';
if ($page > 0) {
$nav .= link_arrow_left(get_string('navigateprevious', 'quiz'),
$attemptobj->review_url(null, $page - 1, $showall), false, 'mod_quiz-prev-nav');
}
if ($lastpage) {
$nav .= $this->finish_review_link($attemptobj);
} else {
$nav .= link_arrow_right(get_string('navigatenext', 'quiz'),
$attemptobj->review_url(null, $page + 1, $showall), false, 'mod_quiz-next-nav');
}
return html_writer::tag('div', $nav, ['class' => 'submitbtns']);
}
/**
* Return the HTML of the quiz timer.
*
* @param quiz_attempt $attemptobj instance of quiz_attempt
* @param int $timenow timestamp to use as 'now'.
* @return string HTML content.
*/
public function countdown_timer(quiz_attempt $attemptobj, $timenow) {
$timeleft = $attemptobj->get_time_left_display($timenow);
if ($timeleft !== false) {
$ispreview = $attemptobj->is_preview();
$timerstartvalue = $timeleft;
if (!$ispreview) {
// Make sure the timer starts just above zero. If $timeleft was <= 0, then
// this will just have the effect of causing the quiz to be submitted immediately.
$timerstartvalue = max($timerstartvalue, 1);
}
$this->initialise_timer($timerstartvalue, $ispreview);
}
return $this->output->render_from_template('mod_quiz/timer', (object) []);
}
/**
* Create a preview link
*
* @param moodle_url $url URL to restart the attempt.
*/
public function restart_preview_button($url) {
return $this->single_button($url, get_string('startnewpreview', 'quiz'));
}
/**
* Outputs the navigation block panel
*
* @param navigation_panel_base $panel
*/
public function navigation_panel(navigation_panel_base $panel) {
$output = '';
$userpicture = $panel->user_picture();
if ($userpicture) {
$fullname = fullname($userpicture->user);
if ($userpicture->size) {
$fullname = html_writer::div($fullname);
}
$output .= html_writer::tag('div', $this->render($userpicture) . $fullname,
['id' => 'user-picture', 'class' => 'clearfix']);
}
$output .= $panel->render_before_button_bits($this);
$bcc = $panel->get_button_container_class();
$output .= html_writer::start_tag('div', ['class' => "qn_buttons clearfix $bcc"]);
foreach ($panel->get_question_buttons() as $button) {
$output .= $this->render($button);
}
$output .= html_writer::end_tag('div');
$output .= html_writer::tag('div', $panel->render_end_bits($this),
['class' => 'othernav']);
$this->page->requires->js_init_call('M.mod_quiz.nav.init', null, false,
quiz_get_js_module());
return $output;
}
/**
* Display a quiz navigation button.
*
* @param navigation_question_button $button
* @return string HTML fragment.
*/
protected function render_navigation_question_button(navigation_question_button $button) {
$classes = ['qnbutton', $button->stateclass, $button->navmethod, 'btn'];
$extrainfo = [];
if ($button->currentpage) {
$classes[] = 'thispage';
$extrainfo[] = get_string('onthispage', 'quiz');
}
// Flagged?
if ($button->flagged) {
$classes[] = 'flagged';
$flaglabel = get_string('flagged', 'question');
} else {
$flaglabel = '';
}
$extrainfo[] = html_writer::tag('span', $flaglabel, ['class' => 'flagstate']);
if ($button->isrealquestion) {
$qnostring = 'questionnonav';
} else {
$qnostring = 'questionnonavinfo';
}
$tooltip = get_string('questionx', 'question', s($button->number)) . ' - ' . $button->statestring;
$a = new stdClass();
$a->number = s($button->number);
$a->attributes = implode(' ', $extrainfo);
$tagcontents = html_writer::tag('span', '', ['class' => 'thispageholder']) .
html_writer::tag('span', '', ['class' => 'trafficlight']) .
get_string($qnostring, 'quiz', $a);
$tagattributes = ['class' => implode(' ', $classes), 'id' => $button->id,
'title' => $tooltip, 'data-quiz-page' => $button->page];
if ($button->url) {
return html_writer::link($button->url, $tagcontents, $tagattributes);
} else {
return html_writer::tag('span', $tagcontents, $tagattributes);
}
}
/**
* Display a quiz navigation heading.
*
* @param navigation_section_heading $heading the heading.
* @return string HTML fragment.
*/
protected function render_navigation_section_heading(navigation_section_heading $heading) {
if (empty($heading->heading)) {
$headingtext = get_string('sectionnoname', 'quiz');
$class = ' dimmed_text';
} else {
$headingtext = $heading->heading;
$class = '';
}
return $this->heading($headingtext, 3, 'mod_quiz-section-heading' . $class);
}
/**
* Renders a list of links the other attempts.
*
* @param links_to_other_attempts $links
* @return string HTML fragment.
*/
protected function render_links_to_other_attempts(
links_to_other_attempts $links) {
$attemptlinks = [];
foreach ($links->links as $attempt => $url) {
if (!$url) {
$attemptlinks[] = html_writer::tag('strong', $attempt);
} else {
if ($url instanceof renderable) {
$attemptlinks[] = $this->render($url);
} else {
$attemptlinks[] = html_writer::link($url, $attempt);
}
}
}
return implode(', ', $attemptlinks);
}
/**
* Render the 'start attempt' page.
*
* The student gets here if their interaction with the preflight check
* from fails in some way (e.g. they typed the wrong password).
*
* @param \mod_quiz\quiz_settings $quizobj
* @param preflight_check_form $mform
* @return string
*/
public function start_attempt_page(quiz_settings $quizobj, preflight_check_form $mform) {
$output = '';
$output .= $this->header();
$output .= $this->during_attempt_tertiary_nav($quizobj->view_url());
$output .= $this->heading(format_string($quizobj->get_quiz_name(), true,
["context" => $quizobj->get_context()]));
$output .= $this->quiz_intro($quizobj->get_quiz(), $quizobj->get_cm());
$output .= $mform->render();
$output .= $this->footer();
return $output;
}
/**
* Attempt Page
*
* @param quiz_attempt $attemptobj Instance of quiz_attempt
* @param int $page Current page number
* @param access_manager $accessmanager Instance of access_manager
* @param array $messages An array of messages
* @param array $slots Contains an array of integers that relate to questions
* @param int $id The ID of an attempt
* @param int $nextpage The number of the next page
* @return string HTML to output.
*/
public function attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id,
$nextpage) {
$output = '';
$output .= $this->header();
$output .= $this->during_attempt_tertiary_nav($attemptobj->view_url());
$output .= $this->quiz_notices($messages);
$output .= $this->countdown_timer($attemptobj, time());
$output .= $this->attempt_form($attemptobj, $page, $slots, $id, $nextpage);
$output .= $this->footer();
return $output;
}
/**
* Render the tertiary navigation for pages during the attempt.
*
* @param string|moodle_url $quizviewurl url of the view.php page for this quiz.
* @return string HTML to output.
*/
public function during_attempt_tertiary_nav($quizviewurl): string {
$output = '';
$output .= html_writer::start_div('container-fluid tertiary-navigation');
$output .= html_writer::start_div('row');
$output .= html_writer::start_div('navitem');
$output .= html_writer::link($quizviewurl, get_string('back'),
['class' => 'btn btn-secondary']);
$output .= html_writer::end_div();
$output .= html_writer::end_div();
$output .= html_writer::end_div();
return $output;
}
/**
* Returns any notices.
*
* @param array $messages
*/
public function quiz_notices($messages) {
if (!$messages) {
return '';
}
return $this->notification(
html_writer::tag('p', get_string('accessnoticesheader', 'quiz')) . $this->access_messages($messages),
'warning',
false
);
}
/**
* Outputs the form for making an attempt
*
* @param quiz_attempt $attemptobj
* @param int $page Current page number
* @param array $slots Array of integers relating to questions
* @param int $id ID of the attempt
* @param int $nextpage Next page number
*/
public function attempt_form($attemptobj, $page, $slots, $id, $nextpage) {
$output = '';
// Start the form.
$output .= html_writer::start_tag('form',
['action' => new moodle_url($attemptobj->processattempt_url(),
['cmid' => $attemptobj->get_cmid()]), 'method' => 'post',
'enctype' => 'multipart/form-data', 'accept-charset' => 'utf-8',
'id' => 'responseform']);
$output .= html_writer::start_tag('div');
// Print all the questions.
foreach ($slots as $slot) {
$output .= $attemptobj->render_question($slot, false, $this,
$attemptobj->attempt_url($slot, $page));
}
$navmethod = $attemptobj->get_quiz()->navmethod;
$output .= $this->attempt_navigation_buttons($page, $attemptobj->is_last_page($page), $navmethod);
// Some hidden fields to track what is going on.
$output .= html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'attempt',
'value' => $attemptobj->get_attemptid()]);
$output .= html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'thispage',
'value' => $page, 'id' => 'followingpage']);
$output .= html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'nextpage',
'value' => $nextpage]);
$output .= html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'timeup',
'value' => '0', 'id' => 'timeup']);
$output .= html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'sesskey',
'value' => sesskey()]);
$output .= html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'mdlscrollto',
'value' => '', 'id' => 'mdlscrollto']);
// Add a hidden field with questionids. Do this at the end of the form, so
// if you navigate before the form has finished loading, it does not wipe all
// the student's answers.
$output .= html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'slots',
'value' => implode(',', $attemptobj->get_active_slots($page))]);
// Finish the form.
$output .= html_writer::end_tag('div');
$output .= html_writer::end_tag('form');
$output .= $this->connection_warning();
return $output;
}
/**
* Display the prev/next buttons that go at the bottom of each page of the attempt.
*
* @param int $page the page number. Starts at 0 for the first page.
* @param bool $lastpage is this the last page in the quiz?
* @param string $navmethod Optional quiz attribute, 'free' (default) or 'sequential'
* @return string HTML fragment.
*/
protected function attempt_navigation_buttons($page, $lastpage, $navmethod = 'free') {
$output = '';
$output .= html_writer::start_tag('div', ['class' => 'submitbtns']);
if ($page > 0 && $navmethod == 'free') {
$output .= html_writer::empty_tag('input', ['type' => 'submit', 'name' => 'previous',
'value' => get_string('navigateprevious', 'quiz'), 'class' => 'mod_quiz-prev-nav btn btn-secondary',
'id' => 'mod_quiz-prev-nav']);
$this->page->requires->js_call_amd('core_form/submit', 'init', ['mod_quiz-prev-nav']);
}
if ($lastpage) {
$nextlabel = get_string('endtest', 'quiz');
} else {
$nextlabel = get_string('navigatenext', 'quiz');
}
$output .= html_writer::empty_tag('input', ['type' => 'submit', 'name' => 'next',
'value' => $nextlabel, 'class' => 'mod_quiz-next-nav btn btn-primary', 'id' => 'mod_quiz-next-nav']);
$output .= html_writer::end_tag('div');
$this->page->requires->js_call_amd('core_form/submit', 'init', ['mod_quiz-next-nav']);
return $output;
}
/**
* Render a button which allows students to redo a question in the attempt.
*
* @param int $slot the number of the slot to generate the button for.
* @param bool $disabled if true, output the button disabled.
* @return string HTML fragment.
*/
public function redo_question_button($slot, $disabled) {
$attributes = ['type' => 'submit', 'name' => 'redoslot' . $slot,
'value' => get_string('redoquestion', 'quiz'),
'class' => 'mod_quiz-redo_question_button btn btn-secondary',
'id' => 'redoslot' . $slot . '-submit',
'data-savescrollposition' => 'true',
];
if ($disabled) {
$attributes['disabled'] = 'disabled';
} else {
$this->page->requires->js_call_amd('core_question/question_engine', 'initSubmitButton', [$attributes['id']]);
}
return html_writer::div(html_writer::empty_tag('input', $attributes));
}
/**
* Initialise the JavaScript required to initialise the countdown timer.
*
* @param int $timerstartvalue time remaining, in seconds.
* @param bool $ispreview true if this is a preview attempt.
*/
public function initialise_timer($timerstartvalue, $ispreview) {
$options = [$timerstartvalue, (bool) $ispreview];
$this->page->requires->js_init_call('M.mod_quiz.timer.init', $options, false, quiz_get_js_module());
}
/**
* Output a page with an optional message, and JavaScript code to close the
* current window and redirect the parent window to a new URL.
*
* @param moodle_url $url the URL to redirect the parent window to.
* @param string $message message to display before closing the window. (optional)
* @return string HTML to output.
*/
public function close_attempt_popup($url, $message = '') {
$output = '';
$output .= $this->header();
$output .= $this->box_start();
if ($message) {
$output .= html_writer::tag('p', $message);
$output .= html_writer::tag('p', get_string('windowclosing', 'quiz'));
$delay = 5;
} else {
$output .= html_writer::tag('p', get_string('pleaseclose', 'quiz'));
$delay = 0;
}
$this->page->requires->js_init_call('M.mod_quiz.secure_window.close',
[$url, $delay], false, quiz_get_js_module());
$output .= $this->box_end();
$output .= $this->footer();
return $output;
}
/**
* Print each message in an array, surrounded by <p>, </p> tags.
*
* @param array $messages the array of message strings.
* @return string HTML to output.
*/
public function access_messages($messages) {
$output = '';
foreach ($messages as $message) {
$output .= html_writer::tag('p', $message, ['class' => 'text-left']);
}
return $output;
}
/*
* Summary Page
*/
/**
* Create the summary page
*
* @param quiz_attempt $attemptobj
* @param display_options $displayoptions
*/
public function summary_page($attemptobj, $displayoptions) {
$output = '';
$output .= $this->header();
$output .= $this->during_attempt_tertiary_nav($attemptobj->view_url());
$output .= $this->heading(format_string($attemptobj->get_quiz_name()));
$output .= $this->heading(get_string('summaryofattempt', 'quiz'), 3);
$output .= $this->summary_table($attemptobj, $displayoptions);
$output .= $this->summary_page_controls($attemptobj);
$output .= $this->footer();
return $output;
}
/**
* Generates the table of summarydata
*
* @param quiz_attempt $attemptobj
* @param display_options $displayoptions
*/
public function summary_table($attemptobj, $displayoptions) {
// Prepare the summary table header.
$table = new html_table();
$table->attributes['class'] = 'generaltable quizsummaryofattempt boxaligncenter';
$table->head = [get_string('question', 'quiz'), get_string('status', 'quiz')];
$table->align = ['left', 'left'];
$table->size = ['', ''];
$markscolumn = $displayoptions->marks >= question_display_options::MARK_AND_MAX;
if ($markscolumn) {
$table->head[] = get_string('marks', 'quiz');
$table->align[] = 'left';
$table->size[] = '';
}
$tablewidth = count($table->align);
$table->data = [];
// Get the summary info for each question.
$slots = $attemptobj->get_slots();
foreach ($slots as $slot) {
// Add a section headings if we need one here.
$heading = $attemptobj->get_heading_before_slot($slot);
if ($heading !== null) {
// There is a heading here.
$rowclasses = 'quizsummaryheading';
if ($heading) {
$heading = format_string($heading);
} else {
if (count($attemptobj->get_quizobj()->get_sections()) > 1) {
// If this is the start of an unnamed section, and the quiz has more
// than one section, then add a default heading.
$heading = get_string('sectionnoname', 'quiz');
$rowclasses .= ' dimmed_text';
}
}
$cell = new html_table_cell(format_string($heading));
$cell->header = true;
$cell->colspan = $tablewidth;
$table->data[] = [$cell];
$table->rowclasses[] = $rowclasses;
}
// Don't display information items.
if (!$attemptobj->is_real_question($slot)) {
continue;
}
// Real question, show it.
$flag = '';
if ($attemptobj->is_question_flagged($slot)) {
// Quiz has custom JS manipulating these image tags - so we can't use the pix_icon method here.
$flag = html_writer::empty_tag('img', ['src' => $this->image_url('i/flagged'),
'alt' => get_string('flagged', 'question'), 'class' => 'questionflag icon-post']);
}
if ($attemptobj->can_navigate_to($slot)) {
$row = [html_writer::link($attemptobj->attempt_url($slot),
$attemptobj->get_question_number($slot) . $flag),
$attemptobj->get_question_status($slot, $displayoptions->correctness)];
} else {
$row = [$attemptobj->get_question_number($slot) . $flag,
$attemptobj->get_question_status($slot, $displayoptions->correctness)];
}
if ($markscolumn) {
$row[] = $attemptobj->get_question_mark($slot);
}
$table->data[] = $row;
$table->rowclasses[] = 'quizsummary' . $slot . ' ' . $attemptobj->get_question_state_class(
$slot, $displayoptions->correctness);
}
// Print the summary table.
return html_writer::table($table);
}
/**
* Creates any controls the page should have.
*
* @param quiz_attempt $attemptobj
*/
public function summary_page_controls($attemptobj) {
$output = '';
// Return to place button.
if ($attemptobj->get_state() == quiz_attempt::IN_PROGRESS) {
$button = new single_button(
new moodle_url($attemptobj->attempt_url(null, $attemptobj->get_currentpage())),
get_string('returnattempt', 'quiz'));
$output .= $this->container($this->container($this->render($button),
'controls'), 'submitbtns mdl-align');
}
// Finish attempt button.
$options = [
'attempt' => $attemptobj->get_attemptid(),
'finishattempt' => 1,
'timeup' => 0,
'slots' => '',
'cmid' => $attemptobj->get_cmid(),
'sesskey' => sesskey(),
];
$button = new single_button(
new moodle_url($attemptobj->processattempt_url(), $options),
get_string('submitallandfinish', 'quiz'));
$button->class = 'btn-finishattempt';
$button->formid = 'frm-finishattempt';
if ($attemptobj->get_state() == quiz_attempt::IN_PROGRESS) {
$totalunanswered = 0;
if ($attemptobj->get_quiz()->navmethod == 'free') {
// Only count the unanswered question if the navigation method is set to free.
$totalunanswered = $attemptobj->get_number_of_unanswered_questions();
}
$this->page->requires->js_call_amd('mod_quiz/submission_confirmation', 'init', [$totalunanswered]);
}
$button->type = \single_button::BUTTON_PRIMARY;
$duedate = $attemptobj->get_due_date();
$message = '';
if ($attemptobj->get_state() == quiz_attempt::OVERDUE) {
$message = get_string('overduemustbesubmittedby', 'quiz', userdate($duedate));
} else {
if ($duedate) {
$message = get_string('mustbesubmittedby', 'quiz', userdate($duedate));
}
}
$output .= $this->countdown_timer($attemptobj, time());
$output .= $this->container($message . $this->container(
$this->render($button), 'controls'), 'submitbtns mdl-align');
return $output;
}
/*
* View Page
*/
/**
* Generates the view page
*
* @param stdClass $course the course settings row from the database.
* @param stdClass $quiz the quiz settings row from the database.
* @param stdClass $cm the course_module settings row from the database.
* @param context_module $context the quiz context.
* @param view_page $viewobj
* @return string HTML to display
*/
public function view_page($course, $quiz, $cm, $context, $viewobj) {
$output = '';
$output .= $this->view_page_tertiary_nav($viewobj);
$output .= $this->view_information($quiz, $cm, $context, $viewobj->infomessages);
$output .= $this->view_table($quiz, $context, $viewobj);
$output .= $this->view_result_info($quiz, $context, $cm, $viewobj);
$output .= $this->box($this->view_page_buttons($viewobj), 'quizattempt');
return $output;
}
/**
* Render the tertiary navigation for the view page.
*
* @param view_page $viewobj the information required to display the view page.
* @return string HTML to output.
*/
public function view_page_tertiary_nav(view_page $viewobj): string {
$content = '';
if ($viewobj->buttontext) {
$attemptbtn = $this->start_attempt_button($viewobj->buttontext,
$viewobj->startattempturl, $viewobj->preflightcheckform,
$viewobj->popuprequired, $viewobj->popupoptions);
$content .= $attemptbtn;
}
if ($viewobj->canedit && !$viewobj->quizhasquestions) {
$content .= html_writer::link($viewobj->editurl, get_string('addquestion', 'quiz'),
['class' => 'btn btn-secondary']);
}
if ($content) {
return html_writer::div(html_writer::div($content, 'row'), 'container-fluid tertiary-navigation');
} else {
return '';
}
}
/**
* Work out, and render, whatever buttons, and surrounding info, should appear
* at the end of the review page.
*
* @param view_page $viewobj the information required to display the view page.
* @return string HTML to output.
*/
public function view_page_buttons(view_page $viewobj) {
$output = '';
if (!$viewobj->quizhasquestions) {
$output .= html_writer::div(
$this->notification(get_string('noquestions', 'quiz'), 'warning', false),
'text-left mb-3');
}
$output .= $this->access_messages($viewobj->preventmessages);
if ($viewobj->showbacktocourse) {
$output .= $this->single_button($viewobj->backtocourseurl,
get_string('backtocourse', 'quiz'), 'get',
['class' => 'continuebutton']);
}
return $output;
}
/**
* Generates the view attempt button
*
* @param string $buttontext the label to display on the button.
* @param moodle_url $url The URL to POST to in order to start the attempt.
* @param preflight_check_form|null $preflightcheckform deprecated.
* @param bool $popuprequired whether the attempt needs to be opened in a pop-up.
* @param array $popupoptions the options to use if we are opening a popup.
* @return string HTML fragment.
*/
public function start_attempt_button($buttontext, moodle_url $url,
preflight_check_form $preflightcheckform = null,
$popuprequired = false, $popupoptions = null) {
$button = new single_button($url, $buttontext, 'post', single_button::BUTTON_PRIMARY);
$button->class .= ' quizstartbuttondiv';
if ($popuprequired) {
$button->class .= ' quizsecuremoderequired';
}
$popupjsoptions = null;
if ($popuprequired && $popupoptions) {
$action = new popup_action('click', $url, 'popup', $popupoptions);
$popupjsoptions = $action->get_js_options();
}
$this->page->requires->js_call_amd('mod_quiz/preflightcheck', 'init',
['.quizstartbuttondiv [type=submit]', get_string('startattempt', 'quiz'),
'#mod_quiz_preflight_form', $popupjsoptions]);
return $this->render($button) . ($preflightcheckform ? $preflightcheckform->render() : '');
}
/**
* Generate a message saying that this quiz has no questions, with a button to
* go to the edit page, if the user has the right capability.
*
* @param bool $canedit can the current user edit the quiz?
* @param moodle_url $editurl URL of the edit quiz page.
* @return string HTML to output.
*
* @deprecated since Moodle 4.0 MDL-71915 - please do not use this function any more.
*/
public function no_questions_message($canedit, $editurl) {
debugging('no_questions_message() is deprecated, please use generate_no_questions_message() instead.', DEBUG_DEVELOPER);
$output = html_writer::start_tag('div', ['class' => 'card text-center mb-3']);
$output .= html_writer::start_tag('div', ['class' => 'card-body']);
$output .= $this->notification(get_string('noquestions', 'quiz'), 'warning', false);
if ($canedit) {
$output .= $this->single_button($editurl, get_string('editquiz', 'quiz'), 'get');
}
$output .= html_writer::end_tag('div');
$output .= html_writer::end_tag('div');
return $output;
}
/**
* Outputs an error message for any guests accessing the quiz
*
* @param stdClass $course the course settings row from the database.
* @param stdClass $quiz the quiz settings row from the database.
* @param stdClass $cm the course_module settings row from the database.
* @param context_module $context the quiz context.
* @param array $messages Array containing any messages
* @param view_page $viewobj
*/
public function view_page_guest($course, $quiz, $cm, $context, $messages, $viewobj) {