mirrored from git://git.moodle.org/moodle.git
/
quiz_attempt.php
2387 lines (2120 loc) · 88.6 KB
/
quiz_attempt.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;
use action_link;
use block_contents;
use cm_info;
use coding_exception;
use context_module;
use Exception;
use html_writer;
use mod_quiz\output\links_to_other_attempts;
use mod_quiz\output\renderer;
use mod_quiz\question\bank\qbank_helper;
use mod_quiz\question\display_options;
use moodle_exception;
use moodle_url;
use popup_action;
use qtype_description_question;
use question_attempt;
use question_bank;
use question_display_options;
use question_engine;
use question_out_of_sequence_exception;
use question_state;
use question_usage_by_activity;
use stdClass;
/**
* This class represents one user's attempt at a particular quiz.
*
* @package mod_quiz
* @copyright 2008 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quiz_attempt {
/** @var string to identify the in progress state. */
const IN_PROGRESS = 'inprogress';
/** @var string to identify the overdue state. */
const OVERDUE = 'overdue';
/** @var string to identify the finished state. */
const FINISHED = 'finished';
/** @var string to identify the abandoned state. */
const ABANDONED = 'abandoned';
/** @var int maximum number of slots in the quiz for the review page to default to show all. */
const MAX_SLOTS_FOR_DEFAULT_REVIEW_SHOW_ALL = 50;
/** @var int amount of time considered 'immedately after the attempt', in seconds. */
const IMMEDIATELY_AFTER_PERIOD = 2 * MINSECS;
/** @var quiz_settings object containing the quiz settings. */
protected $quizobj;
/** @var stdClass the quiz_attempts row. */
protected $attempt;
/** @var question_usage_by_activity the question usage for this quiz attempt. */
protected $quba;
/**
* @var array of slot information. These objects contain ->slot (int),
* ->requireprevious (bool), ->questionids (int) the original question for random questions,
* ->firstinsection (bool), ->section (stdClass from $this->sections).
* This does not contain page - get that from {@see get_question_page()} -
* or maxmark - get that from $this->quba.
*/
protected $slots;
/** @var array of quiz_sections rows, with a ->lastslot field added. */
protected $sections;
/** @var array page no => array of slot numbers on the page in order. */
protected $pagelayout;
/** @var array slot => displayed question number for this slot. (E.g. 1, 2, 3 or 'i'.) */
protected $questionnumbers;
/** @var array slot => page number for this slot. */
protected $questionpages;
/** @var display_options cache for the appropriate review options. */
protected $reviewoptions = null;
// Constructor =============================================================.
/**
* Constructor assuming we already have the necessary data loaded.
*
* @param stdClass $attempt the row of the quiz_attempts table.
* @param stdClass $quiz the quiz object for this attempt and user.
* @param stdClass|cm_info $cm the course_module object for this quiz.
* @param stdClass $course the row from the course table for the course we belong to.
* @param bool $loadquestions (optional) if true, the default, load all the details
* of the state of each question. Else just set up the basic details of the attempt.
*/
public function __construct($attempt, $quiz, $cm, $course, $loadquestions = true) {
$this->attempt = $attempt;
$this->quizobj = new quiz_settings($quiz, $cm, $course);
if ($loadquestions) {
$this->load_questions();
}
}
/**
* Used by {create()} and {create_from_usage_id()}.
*
* @param array $conditions passed to $DB->get_record('quiz_attempts', $conditions).
* @return quiz_attempt the desired instance of this class.
*/
protected static function create_helper($conditions) {
global $DB;
$attempt = $DB->get_record('quiz_attempts', $conditions, '*', MUST_EXIST);
$quiz = access_manager::load_quiz_and_settings($attempt->quiz);
$course = get_course($quiz->course);
$cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id, false, MUST_EXIST);
// Update quiz with override information.
$quiz = quiz_update_effective_access($quiz, $attempt->userid);
return new quiz_attempt($attempt, $quiz, $cm, $course);
}
/**
* Static function to create a new quiz_attempt object given an attemptid.
*
* @param int $attemptid the attempt id.
* @return quiz_attempt the new quiz_attempt object
*/
public static function create($attemptid) {
return self::create_helper(['id' => $attemptid]);
}
/**
* Static function to create a new quiz_attempt object given a usage id.
*
* @param int $usageid the attempt usage id.
* @return quiz_attempt the new quiz_attempt object
*/
public static function create_from_usage_id($usageid) {
return self::create_helper(['uniqueid' => $usageid]);
}
/**
* Get a human-readable name for one of the quiz attempt states.
*
* @param string $state one of the state constants like IN_PROGRESS.
* @return string the human-readable state name.
*/
public static function state_name($state) {
return quiz_attempt_state_name($state);
}
/**
* This method can be called later if the object was constructed with $loadquestions = false.
*/
public function load_questions() {
global $DB;
if (isset($this->quba)) {
throw new coding_exception('This quiz attempt has already had the questions loaded.');
}
$this->quba = question_engine::load_questions_usage_by_activity($this->attempt->uniqueid);
$this->slots = $DB->get_records('quiz_slots',
['quizid' => $this->get_quizid()], 'slot', 'slot, id, requireprevious, displaynumber');
$this->sections = array_values($DB->get_records('quiz_sections',
['quizid' => $this->get_quizid()], 'firstslot'));
$this->link_sections_and_slots();
$this->determine_layout();
$this->number_questions();
}
/**
* Preload all attempt step users to show in Response history.
*/
public function preload_all_attempt_step_users(): void {
$this->quba->preload_all_step_users();
}
/**
* Let each slot know which section it is part of.
*/
protected function link_sections_and_slots() {
foreach ($this->sections as $i => $section) {
if (isset($this->sections[$i + 1])) {
$section->lastslot = $this->sections[$i + 1]->firstslot - 1;
} else {
$section->lastslot = count($this->slots);
}
for ($slot = $section->firstslot; $slot <= $section->lastslot; $slot += 1) {
$this->slots[$slot]->section = $section;
}
}
}
/**
* Parse attempt->layout to populate the other arrays that represent the layout.
*/
protected function determine_layout() {
// Break up the layout string into pages.
$pagelayouts = explode(',0', $this->attempt->layout);
// Strip off any empty last page (normally there is one).
if (end($pagelayouts) == '') {
array_pop($pagelayouts);
}
// File the ids into the arrays.
// Tracking which is the first slot in each section in this attempt is
// trickier than you might guess, since the slots in this section
// may be shuffled, so $section->firstslot (the lowest numbered slot in
// the section) may not be the first one.
$unseensections = $this->sections;
$this->pagelayout = [];
foreach ($pagelayouts as $page => $pagelayout) {
$pagelayout = trim($pagelayout, ',');
if ($pagelayout == '') {
continue;
}
$this->pagelayout[$page] = explode(',', $pagelayout);
foreach ($this->pagelayout[$page] as $slot) {
$sectionkey = array_search($this->slots[$slot]->section, $unseensections);
if ($sectionkey !== false) {
$this->slots[$slot]->firstinsection = true;
unset($unseensections[$sectionkey]);
} else {
$this->slots[$slot]->firstinsection = false;
}
}
}
}
/**
* Work out the number to display for each question/slot.
*/
protected function number_questions() {
$number = 1;
foreach ($this->pagelayout as $page => $slots) {
foreach ($slots as $slot) {
if ($length = $this->is_real_question($slot)) {
// Whether question numbering is customised or is numeric and automatically incremented.
if ($this->slots[$slot]->displaynumber !== null && $this->slots[$slot]->displaynumber !== '' &&
!$this->slots[$slot]->section->shufflequestions) {
$this->questionnumbers[$slot] = $this->slots[$slot]->displaynumber;
} else {
$this->questionnumbers[$slot] = (string) $number;
}
$number += $length;
} else {
$this->questionnumbers[$slot] = get_string('infoshort', 'quiz');
}
$this->questionpages[$slot] = $page;
}
}
}
/**
* If the given page number is out of range (before the first page, or after
* the last page, change it to be within range).
*
* @param int $page the requested page number.
* @return int a safe page number to use.
*/
public function force_page_number_into_range($page) {
return min(max($page, 0), count($this->pagelayout) - 1);
}
// Simple getters ==========================================================.
/**
* Get the raw quiz settings object.
*
* @return stdClass
*/
public function get_quiz() {
return $this->quizobj->get_quiz();
}
/**
* Get the {@see seb_quiz_settings} object for this quiz.
*
* @return quiz_settings
*/
public function get_quizobj() {
return $this->quizobj;
}
/**
* Git the id of the course this quiz belongs to.
*
* @return int the course id.
*/
public function get_courseid() {
return $this->quizobj->get_courseid();
}
/**
* Get the course settings object.
*
* @return stdClass the course settings object.
*/
public function get_course() {
return $this->quizobj->get_course();
}
/**
* Get the quiz id.
*
* @return int the quiz id.
*/
public function get_quizid() {
return $this->quizobj->get_quizid();
}
/**
* Get the name of this quiz.
*
* @return string Quiz name, directly from the database (format_string must be called before output).
*/
public function get_quiz_name() {
return $this->quizobj->get_quiz_name();
}
/**
* Get the quiz navigation method.
*
* @return int QUIZ_NAVMETHOD_FREE or QUIZ_NAVMETHOD_SEQ.
*/
public function get_navigation_method() {
return $this->quizobj->get_navigation_method();
}
/**
* Get the course_module for this quiz.
*
* @return stdClass|cm_info the course_module object.
*/
public function get_cm() {
return $this->quizobj->get_cm();
}
/**
* Get the course-module id.
*
* @return int the course_module id.
*/
public function get_cmid() {
return $this->quizobj->get_cmid();
}
/**
* Get the quiz context.
*
* @return context_module the context of the quiz this attempt belongs to.
*/
public function get_context(): context_module {
return $this->quizobj->get_context();
}
/**
* Is the current user is someone who previews the quiz, rather than attempting it?
*
* @return bool true user is a preview user. False, if they can do real attempts.
*/
public function is_preview_user() {
return $this->quizobj->is_preview_user();
}
/**
* Get the number of attempts the user is allowed at this quiz.
*
* @return int the number of attempts allowed at this quiz (0 = infinite).
*/
public function get_num_attempts_allowed() {
return $this->quizobj->get_num_attempts_allowed();
}
/**
* Get the number of quizzes in the quiz attempt.
*
* @return int number pages.
*/
public function get_num_pages() {
return count($this->pagelayout);
}
/**
* Get the access_manager for this quiz attempt.
*
* @param int $timenow the current time as a unix timestamp.
* @return access_manager and instance of the access_manager class
* for this quiz at this time.
*/
public function get_access_manager($timenow) {
return $this->quizobj->get_access_manager($timenow);
}
/**
* Get the id of this attempt.
*
* @return int the attempt id.
*/
public function get_attemptid() {
return $this->attempt->id;
}
/**
* Get the question-usage id corresponding to this quiz attempt.
*
* @return int the attempt unique id.
*/
public function get_uniqueid() {
return $this->attempt->uniqueid;
}
/**
* Get the raw quiz attempt object.
*
* @return stdClass the row from the quiz_attempts table.
*/
public function get_attempt() {
return $this->attempt;
}
/**
* Get the attempt number.
*
* @return int the number of this attempt (is it this user's first, second, ... attempt).
*/
public function get_attempt_number() {
return $this->attempt->attempt;
}
/**
* Get the state of this attempt.
*
* @return string {@see IN_PROGRESS}, {@see FINISHED}, {@see OVERDUE} or {@see ABANDONED}.
*/
public function get_state() {
return $this->attempt->state;
}
/**
* Get the id of the user this attempt belongs to.
* @return int user id.
*/
public function get_userid() {
return $this->attempt->userid;
}
/**
* Get the current page of the attempt
* @return int page number.
*/
public function get_currentpage() {
return $this->attempt->currentpage;
}
/**
* Get the total number of marks that the user had scored on all the questions.
*
* @return float
*/
public function get_sum_marks() {
return $this->attempt->sumgrades;
}
/**
* Has this attempt been finished?
*
* States {@see FINISHED} and {@see ABANDONED} are both considered finished in this state.
* Other states are not.
*
* @return bool
*/
public function is_finished() {
return $this->attempt->state == self::FINISHED || $this->attempt->state == self::ABANDONED;
}
/**
* Is this attempt a preview?
*
* @return bool true if it is.
*/
public function is_preview() {
return $this->attempt->preview;
}
/**
* Does this attempt belong to the current user?
*
* @return bool true => own attempt/preview. false => reviewing someone else's.
*/
public function is_own_attempt() {
global $USER;
return $this->attempt->userid == $USER->id;
}
/**
* Is this attempt is a preview belonging to the current user.
*
* @return bool true if it is.
*/
public function is_own_preview() {
return $this->is_own_attempt() &&
$this->is_preview_user() && $this->attempt->preview;
}
/**
* Is the current user allowed to review this attempt. This applies when
* {@see is_own_attempt()} returns false.
*
* @return bool whether the review should be allowed.
*/
public function is_review_allowed() {
if (!$this->has_capability('mod/quiz:viewreports')) {
return false;
}
$cm = $this->get_cm();
if ($this->has_capability('moodle/site:accessallgroups') ||
groups_get_activity_groupmode($cm) != SEPARATEGROUPS) {
return true;
}
// Check the users have at least one group in common.
$teachersgroups = groups_get_activity_allowed_groups($cm);
$studentsgroups = groups_get_all_groups(
$cm->course, $this->attempt->userid, $cm->groupingid);
return $teachersgroups && $studentsgroups &&
array_intersect(array_keys($teachersgroups), array_keys($studentsgroups));
}
/**
* Has the student, in this attempt, engaged with the quiz in a non-trivial way?
*
* That is, is there any question worth a non-zero number of marks, where
* the student has made some response that we have saved?
*
* @return bool true if we have saved a response for at least one graded question.
*/
public function has_response_to_at_least_one_graded_question() {
foreach ($this->quba->get_attempt_iterator() as $qa) {
if ($qa->get_max_mark() == 0) {
continue;
}
if ($qa->get_num_steps() > 1) {
return true;
}
}
return false;
}
/**
* Do any questions in this attempt need to be graded manually?
*
* @return bool True if we have at least one question still needs manual grading.
*/
public function requires_manual_grading(): bool {
return $this->quba->get_total_mark() === null;
}
/**
* Get extra summary information about this attempt.
*
* Some behaviours may be able to provide interesting summary information
* about the attempt as a whole, and this method provides access to that data.
* To see how this works, try setting a quiz to one of the CBM behaviours,
* and then look at the extra information displayed at the top of the quiz
* review page once you have submitted an attempt.
*
* In the return value, the array keys are identifiers of the form
* qbehaviour_behaviourname_meaningfullkey. For qbehaviour_deferredcbm_highsummary.
* The values are arrays with two items, title and content. Each of these
* will be either a string, or a renderable.
*
* @param question_display_options $options the display options for this quiz attempt at this time.
* @return array as described above.
*/
public function get_additional_summary_data(question_display_options $options) {
return $this->quba->get_summary_information($options);
}
/**
* Get the overall feedback corresponding to a particular mark.
*
* @param number $grade a particular grade.
* @return string the feedback.
*/
public function get_overall_feedback($grade) {
return quiz_feedback_for_grade($grade, $this->get_quiz(),
$this->quizobj->get_context());
}
/**
* Wrapper round the has_capability function that automatically passes in the quiz context.
*
* @param string $capability the name of the capability to check. For example mod/forum:view.
* @param int|null $userid A user id. If null checks the permissions of the current user.
* @param bool $doanything If false, ignore effect of admin role assignment.
* @return boolean true if the user has this capability, otherwise false.
*/
public function has_capability($capability, $userid = null, $doanything = true) {
return $this->quizobj->has_capability($capability, $userid, $doanything);
}
/**
* Wrapper round the require_capability function that automatically passes in the quiz context.
*
* @param string $capability the name of the capability to check. For example mod/forum:view.
* @param int|null $userid A user id. If null checks the permissions of the current user.
* @param bool $doanything If false, ignore effect of admin role assignment.
*/
public function require_capability($capability, $userid = null, $doanything = true) {
$this->quizobj->require_capability($capability, $userid, $doanything);
}
/**
* Check the appropriate capability to see whether this user may review their own attempt.
* If not, prints an error.
*/
public function check_review_capability() {
if ($this->get_attempt_state() == display_options::IMMEDIATELY_AFTER) {
$capability = 'mod/quiz:attempt';
} else {
$capability = 'mod/quiz:reviewmyattempts';
}
// These next tests are in a slightly funny order. The point is that the
// common and most performance-critical case is students attempting a quiz,
// so we want to check that permission first.
if ($this->has_capability($capability)) {
// User has the permission that lets you do the quiz as a student. Fine.
return;
}
if ($this->has_capability('mod/quiz:viewreports') ||
$this->has_capability('mod/quiz:preview')) {
// User has the permission that lets teachers review. Fine.
return;
}
// They should not be here. Trigger the standard no-permission error
// but using the name of the student capability.
// We know this will fail. We just want the standard exception thrown.
$this->require_capability($capability);
}
/**
* Checks whether a user may navigate to a particular slot.
*
* @param int $slot the target slot (currently does not affect the answer).
* @return bool true if the navigation should be allowed.
*/
public function can_navigate_to($slot) {
if ($this->attempt->state == self::OVERDUE) {
// When the attempt is overdue, students can only see the
// attempt summary page and cannot navigate anywhere else.
return false;
}
return $this->get_navigation_method() == QUIZ_NAVMETHOD_FREE;
}
/**
* Get where we are time-wise in relation to this attempt and the quiz settings.
*
* @return int one of {@see display_options::DURING}, {@see display_options::IMMEDIATELY_AFTER},
* {@see display_options::LATER_WHILE_OPEN} or {@see display_options::AFTER_CLOSE}.
*/
public function get_attempt_state() {
return quiz_attempt_state($this->get_quiz(), $this->attempt);
}
/**
* Wrapper that the correct display_options for this quiz at the
* moment.
*
* @param bool $reviewing true for options when reviewing, false for when attempting.
* @return question_display_options the render options for this user on this attempt.
*/
public function get_display_options($reviewing) {
if ($reviewing) {
if (is_null($this->reviewoptions)) {
$this->reviewoptions = quiz_get_review_options($this->get_quiz(),
$this->attempt, $this->quizobj->get_context());
if ($this->is_own_preview()) {
// It should always be possible for a teacher to review their
// own preview irrespective of the review options settings.
$this->reviewoptions->attempt = true;
}
}
return $this->reviewoptions;
} else {
$options = display_options::make_from_quiz($this->get_quiz(),
display_options::DURING);
$options->flags = quiz_get_flag_option($this->attempt, $this->quizobj->get_context());
return $options;
}
}
/**
* Wrapper that the correct display_options for this quiz at the
* moment.
*
* @param bool $reviewing true for review page, else attempt page.
* @param int $slot which question is being displayed.
* @param moodle_url $thispageurl to return to after the editing form is
* submitted or cancelled. If null, no edit link will be generated.
*
* @return question_display_options the render options for this user on this
* attempt, with extra info to generate an edit link, if applicable.
*/
public function get_display_options_with_edit_link($reviewing, $slot, $thispageurl) {
$options = clone($this->get_display_options($reviewing));
if (!$thispageurl) {
return $options;
}
if (!($reviewing || $this->is_preview())) {
return $options;
}
$question = $this->quba->get_question($slot, false);
if (!question_has_capability_on($question, 'edit', $question->category)) {
return $options;
}
$options->editquestionparams['cmid'] = $this->get_cmid();
$options->editquestionparams['returnurl'] = $thispageurl;
return $options;
}
/**
* Is a particular page the last one in the quiz?
*
* @param int $page a page number
* @return bool true if that is the last page of the quiz.
*/
public function is_last_page($page) {
return $page == count($this->pagelayout) - 1;
}
/**
* Return the list of slot numbers for either a given page of the quiz, or for the
* whole quiz.
*
* @param mixed $page string 'all' or integer page number.
* @return array the requested list of slot numbers.
*/
public function get_slots($page = 'all') {
if ($page === 'all') {
$numbers = [];
foreach ($this->pagelayout as $numbersonpage) {
$numbers = array_merge($numbers, $numbersonpage);
}
return $numbers;
} else {
return $this->pagelayout[$page];
}
}
/**
* Return the list of slot numbers for either a given page of the quiz, or for the
* whole quiz.
*
* @param mixed $page string 'all' or integer page number.
* @return array the requested list of slot numbers.
*/
public function get_active_slots($page = 'all') {
$activeslots = [];
foreach ($this->get_slots($page) as $slot) {
if (!$this->is_blocked_by_previous_question($slot)) {
$activeslots[] = $slot;
}
}
return $activeslots;
}
/**
* Helper method for unit tests. Get the underlying question usage object.
*
* @return question_usage_by_activity the usage.
*/
public function get_question_usage() {
if (!(PHPUNIT_TEST || defined('BEHAT_TEST'))) {
throw new coding_exception('get_question_usage is only for use in unit tests. ' .
'For other operations, use the quiz_attempt api, or extend it properly.');
}
return $this->quba;
}
/**
* Get the question_attempt object for a particular question in this attempt.
*
* @param int $slot the number used to identify this question within this attempt.
* @return question_attempt the requested question_attempt.
*/
public function get_question_attempt($slot) {
return $this->quba->get_question_attempt($slot);
}
/**
* Get all the question_attempt objects that have ever appeared in a given slot.
*
* This relates to the 'Try another question like this one' feature.
*
* @param int $slot the number used to identify this question within this attempt.
* @return question_attempt[] the attempts.
*/
public function all_question_attempts_originally_in_slot($slot) {
$qas = [];
foreach ($this->quba->get_attempt_iterator() as $qa) {
if ($qa->get_metadata('originalslot') == $slot) {
$qas[] = $qa;
}
}
$qas[] = $this->quba->get_question_attempt($slot);
return $qas;
}
/**
* Is a particular question in this attempt a real question, or something like a description.
*
* @param int $slot the number used to identify this question within this attempt.
* @return int whether that question is a real question. Actually returns the
* question length, which could theoretically be greater than one.
*/
public function is_real_question($slot) {
return $this->quba->get_question($slot, false)->length;
}
/**
* Is a particular question in this attempt a real question, or something like a description.
*
* @param int $slot the number used to identify this question within this attempt.
* @return bool whether that question is a real question.
*/
public function is_question_flagged($slot) {
return $this->quba->get_question_attempt($slot)->is_flagged();
}
/**
* Checks whether the question in this slot requires the previous
* question to have been completed.
*
* @param int $slot the number used to identify this question within this attempt.
* @return bool whether the previous question must have been completed before
* this one can be seen.
*/
public function is_blocked_by_previous_question($slot) {
return $slot > 1 && isset($this->slots[$slot]) && $this->slots[$slot]->requireprevious &&
!$this->slots[$slot]->section->shufflequestions &&
!$this->slots[$slot - 1]->section->shufflequestions &&
$this->get_navigation_method() != QUIZ_NAVMETHOD_SEQ &&
!$this->get_question_state($slot - 1)->is_finished() &&
$this->quba->can_question_finish_during_attempt($slot - 1);
}
/**
* Is it possible for this question to be re-started within this attempt?
*
* @param int $slot the number used to identify this question within this attempt.
* @return bool whether the student should be given the option to restart this question now.
*/
public function can_question_be_redone_now($slot) {
return $this->get_quiz()->canredoquestions && !$this->is_finished() &&
$this->get_question_state($slot)->is_finished();
}
/**
* Given a slot in this attempt, which may or not be a redone question, return the original slot.
*
* @param int $slot identifies a particular question in this attempt.
* @return int the slot where this question was originally.
*/
public function get_original_slot($slot) {
$originalslot = $this->quba->get_question_attempt_metadata($slot, 'originalslot');
if ($originalslot) {
return $originalslot;
} else {
return $slot;
}
}
/**
* Get the displayed question number for a slot.
*
* @param int $slot the number used to identify this question within this attempt.
* @return string the displayed question number for the question in this slot.
* For example '1', '2', '3' or 'i'.
*/
public function get_question_number($slot): string {
return $this->questionnumbers[$slot];
}
/**
* If the section heading, if any, that should come just before this slot.
*
* @param int $slot identifies a particular question in this attempt.
* @return string|null the required heading, or null if there is not one here.
*/
public function get_heading_before_slot($slot) {
if ($this->slots[$slot]->firstinsection) {
return $this->slots[$slot]->section->heading;
} else {
return null;
}
}
/**
* Return the page of the quiz where this question appears.
*
* @param int $slot the number used to identify this question within this attempt.
* @return int the page of the quiz this question appears on.
*/
public function get_question_page($slot) {
return $this->questionpages[$slot];
}
/**
* Return the grade obtained on a particular question, if the user is permitted
* to see it. You must previously have called load_question_states to load the
* state data about this question.
*
* @param int $slot the number used to identify this question within this attempt.
* @return string the formatted grade, to the number of decimal places specified
* by the quiz.
*/
public function get_question_name($slot) {
return $this->quba->get_question($slot, false)->name;
}
/**
* Return the {@see question_state} that this question is in.
*
* @param int $slot the number used to identify this question within this attempt.
* @return question_state the state this question is in.
*/
public function get_question_state($slot) {
return $this->quba->get_question_state($slot);
}
/**
* Return the grade obtained on a particular question, if the user is permitted
* to see it. You must previously have called load_question_states to load the
* state data about this question.
*
* @param int $slot the number used to identify this question within this attempt.
* @param bool $showcorrectness Whether right/partial/wrong states should
* be distinguished.
* @return string the formatted grade, to the number of decimal places specified
* by the quiz.
*/
public function get_question_status($slot, $showcorrectness) {
return $this->quba->get_question_state_string($slot, $showcorrectness);
}
/**
* Return the grade obtained on a particular question, if the user is permitted
* to see it. You must previously have called load_question_states to load the
* state data about this question.
*
* @param int $slot the number used to identify this question within this attempt.
* @param bool $showcorrectness Whether right/partial/wrong states should
* be distinguished.
* @return string class name for this state.
*/
public function get_question_state_class($slot, $showcorrectness) {
return $this->quba->get_question_state_class($slot, $showcorrectness);
}
/**
* Return the grade obtained on a particular question.
*
* You must previously have called load_question_states to load the state
* data about this question.