forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditlib.php
1206 lines (1074 loc) · 47.2 KB
/
editlib.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
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 1999 onwards Martin Dougiamas and others //
// //
// This program 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 2 of the License, or //
// (at your option) any later version. //
// //
// This program 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: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
/**
* Functions used by edit.php to edit quizzes
*
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package quiz
*//** */
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
define('NUM_QS_TO_SHOW_IN_RANDOM', 3);
/**
* Remove a question from a quiz
* @param object $quiz the quiz object.
* @param int $questionid The id of the question to be deleted.
*/
function quiz_remove_question($quiz, $questionid) {
global $DB;
$questionids = explode(',', $quiz->questions);
$key = array_search($questionid, $questionids);
if ($key === false) {
return;
}
unset($questionids[$key]);
$quiz->questions = implode(',', $questionids);
$DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
$DB->delete_records('quiz_question_instances', array('quiz' => $quiz->instance, 'question' => $questionid));
}
/**
* Remove an empty page from the quiz layout. If that is not possible, do nothing.
* @param string $layout the existinng layout, $quiz->questions.
* @param integer $index the position into $layout where the empty page should be removed.
* @return the updated layout
*/
function quiz_delete_empty_page($layout, $index) {
$questionids = explode(',', $layout);
if ($index < -1 || $index >= count($questionids) - 1) {
return $layout;
}
if (($index >= 0 && $questionids[$index] != 0) || $questionids[$index + 1] != 0) {
return $layout; // This was not an empty page.
}
unset($questionids[$index + 1]);
return implode(',', $questionids);
}
/**
* Add a question to a quiz
*
* Adds a question to a quiz by updating $quiz as well as the
* quiz and quiz_question_instances tables. It also adds a page break
* if required.
* @param int $id The id of the question to be added
* @param object $quiz The extended quiz object as used by edit.php
* This is updated by this function
* @param int $page Which page in quiz to add the question on. If 0 (default), add at the end
* @return boolean false if the question was already in the quiz
*/
function quiz_add_quiz_question($id, $quiz, $page = 0) {
global $DB;
$questions = explode(',', quiz_clean_layout($quiz->questions));
if (in_array($id, $questions)) {
return false;
}
// remove ending page break if it is not needed
if ($breaks = array_keys($questions, 0)) {
// determine location of the last two page breaks
$end = end($breaks);
$last = prev($breaks);
$last = $last ? $last : -1;
if (!$quiz->questionsperpage || (($end - $last - 1) < $quiz->questionsperpage)) {
array_pop($questions);
}
}
if (is_int($page) && $page >= 1) {
$numofpages = quiz_number_of_pages($quiz->questions);
if ($numofpages<$page) {
//the page specified does not exist in quiz
$page = 0;
} else {
// add ending page break - the following logic requires doing
//this at this point
$questions[] = 0;
$currentpage = 1;
$addnow = false;
foreach ($questions as $question) {
if ($question == 0) {
$currentpage++;
//The current page is the one after the one we want to add on,
//so we add the question before adding the current page.
if ($currentpage == $page + 1) {
$questions_new[] = $id;
}
}
$questions_new[] = $question;
}
$questions = $questions_new;
}
}
if ($page == 0) {
// add question
$questions[] = $id;
// add ending page break
$questions[] = 0;
}
// Save new questionslist in database
$quiz->questions = implode(',', $questions);
$DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
// update question grades
$quiz->grades[$id] = $DB->get_field('question', 'defaultgrade', array('id' => $id));
quiz_update_question_instance($quiz->grades[$id], $id, $quiz->instance);
return true;
}
function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number, $includesubcategories) {
global $DB, $QTYPES;
$category = $DB->get_record('question_categories', array('id' => $categoryid));
if (!$category) {
print_error('invalidcategoryid', 'error');
}
$catcontext = get_context_instance_by_id($category->contextid);
require_capability('moodle/question:useall', $catcontext);
// Find existing random questions in this category that are
// not used by any quiz.
if ($existingquestions = $DB->get_records_sql(
"SELECT q.id,q.qtype FROM {question} q
WHERE qtype = '" . RANDOM . "'
AND category = ?
AND " . $DB->sql_compare_text('questiontext') . " = ?
AND NOT EXISTS (SELECT * FROM {quiz_question_instances} WHERE question = q.id)
ORDER BY id", array($category->id, $includesubcategories))) {
// Take as many of these as needed.
while (($existingquestion = array_shift($existingquestions)) && $number > 0) {
quiz_add_quiz_question($existingquestion->id, $quiz, $addonpage);
$number -= 1;
}
}
if ($number <= 0) {
return;
}
// More random questions are needed, create them.
$form->questiontext = array('text' => $includesubcategories, 'format' => 0);
$form->defaultgrade = 1;
$form->hidden = 1;
for ($i = 0; $i < $number; $i += 1) {
$form->category = $category->id . ',' . $category->contextid;
$form->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
$question = new stdClass;
$question->qtype = RANDOM;
$question = $QTYPES[RANDOM]->save_question($question, $form);
if (!isset($question->id)) {
print_error('cannotinsertrandomquestion', 'quiz');
}
quiz_add_quiz_question($question->id, $quiz, $addonpage);
}
}
/**
* Add a page break after at particular position$.
* @param string $layout the existinng layout, $quiz->questions.
* @param integer $index the position into $layout where the empty page should be removed.
* @return the updated layout
*/
function quiz_add_page_break_at($layout, $index) {
$questionids = explode(',', $layout);
if ($index < 0 || $index >= count($questionids)) {
return $layout;
}
array_splice($questionids, $index, 0, '0');
return implode(',', $questionids);
}
/**
* Add a page break after a particular question.
* @param string $layout the existinng layout, $quiz->questions.
* @param integer $qustionid the question to add the page break after.
* @return the updated layout
*/
function quiz_add_page_break_after($layout, $questionid) {
$questionids = explode(',', $layout);
$key = array_search($questionid, $questionids);
if ($key === false || !$questionid) {
return $layout;
}
array_splice($questionids, $key + 1, 0, '0');
return implode(',', $questionids);
}
/**
* Update the database after $quiz->questions has been changed. For example,
* this deletes preview attempts and updates $quiz->sumgrades.
* @param $quiz the quiz object.
*/
function quiz_save_new_layout($quiz) {
global $DB;
$DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
quiz_update_sumgrades($quiz);
quiz_delete_previews($quiz);
}
/**
* Save changes to question instance
*
* Saves changes to the question grades in the quiz_question_instances table.
* It does not update 'sumgrades' in the quiz table.
* @return boolean Indicates success or failure.
* @param integer grade The maximal grade for the question
* @param integer $questionid The id of the question
* @param integer $quizid The id of the quiz to update / add the instances for.
*/
function quiz_update_question_instance($grade, $questionid, $quizid) {
global $DB;
if ($instance = $DB->get_record('quiz_question_instances', array('quiz' => $quizid, 'question' => $questionid))) {
$instance->grade = $grade;
return $DB->update_record('quiz_question_instances', $instance);
} else {
unset($instance);
$instance->quiz = $quizid;
$instance->question = $questionid;
$instance->grade = $grade;
return $DB->insert_record('quiz_question_instances', $instance);
}
}
// Private function used by the following two.
function _quiz_move_question($layout, $questionid, $shift) {
if (!$questionid || !($shift == 1 || $shift == -1)) {
return $layout;
}
$questionids = explode(',', $layout);
$key = array_search($questionid, $questionids);
if ($key === false) {
return $layout;
}
$otherkey = $key + $shift;
if ($otherkey < 0 || $otherkey >= count($questionids) - 1) {
return $layout;
}
$temp = $questionids[$otherkey];
$questionids[$otherkey] = $questionids[$key];
$questionids[$key] = $temp;
return implode(',', $questionids);
}
/**
* Move a particular question one space earlier in the $quiz->questions list.
* If that is not possible, do nothing.
* @param string $layout the existinng layout, $quiz->questions.
* @param integer $questionid the id of a question.
* @return the updated layout
*/
function quiz_move_question_up($layout, $questionid) {
return _quiz_move_question($layout, $questionid, -1);
}
/**
* Move a particular question one space later in the $quiz->questions list.
* If that is not possible, do nothing.
* @param string $layout the existinng layout, $quiz->questions.
* @param integer $questionid the id of a question.
* @return the updated layout
*/
function quiz_move_question_down($layout, $questionid) {
return _quiz_move_question($layout, $questionid, +1);
}
/**
* Prints a list of quiz questions for the edit.php main view for edit
* ($reordertool = false) and order and paging ($reordertool = true) tabs
*
* @return int sum of maximum grades
* @param object $quiz This is not the standard quiz object used elsewhere but
* it contains the quiz layout in $quiz->questions and the grades in
* $quiz->grades
* @param object $pageurl The url of the current page with the parameters required
* for links returning to the current page, as a moodle_url object
* @param boolean $allowdelete Indicates whether the delete icons should be displayed
* @param boolean $reordertool Indicates whether the reorder tool should be displayed
* @param boolean $quiz_qbanktool Indicates whether the question bank should be displayed
* @param boolean $hasattempts Indicates whether the quiz has attempts
*/
function quiz_print_question_list($quiz, $pageurl, $allowdelete, $reordertool,
$quiz_qbanktool, $hasattempts, $defaultcategoryobj) {
global $USER, $CFG, $QTYPES, $DB, $OUTPUT;
$strorder = get_string('order');
$strquestionname = get_string('questionname', 'quiz');
$strgrade = get_string('grade');
$strremove = get_string('remove', 'quiz');
$stredit = get_string('edit');
$strview = get_string('view');
$straction = get_string('action');
$strmove = get_string('move');
$strmoveup = get_string('moveup');
$strmovedown = get_string('movedown');
$strsave = get_string('save', 'quiz');
$strreorderquestions = get_string('reorderquestions', 'quiz');
$strselectall = get_string('selectall', 'quiz');
$strselectnone = get_string('selectnone', 'quiz');
$strtype = get_string('type', 'quiz');
$strpreview = get_string('preview', 'quiz');
if ($quiz->questions) {
list($usql, $params) = $DB->get_in_or_equal(explode(',', $quiz->questions));
$questions = $DB->get_records_sql("SELECT q.*,c.contextid
FROM {question} q,
{question_categories} c
WHERE q.id $usql
AND q.category = c.id", $params);
} else {
$questions = array();
}
$layout = quiz_clean_layout($quiz->questions);
$order = explode(',', $layout);
$lastindex = count($order) - 1;
$disabled = '';
$pagingdisabled = '';
if ($hasattempts) {
$disabled = 'disabled="disabled"';
}
if ($hasattempts || $quiz->shufflequestions) {
$pagingdisabled = 'disabled="disabled"';
}
$reordercontrolssetdefaultsubmit = '<div style="display:none;">' .
'<input type="submit" name="savechanges" value="' .
$strreorderquestions . '" ' . $pagingdisabled . ' /></div>';
$reordercontrols1 = '<div class="addnewpagesafterselected">' .
'<input type="submit" name="addnewpagesafterselected" value="' .
get_string('addnewpagesafterselected', 'quiz') . '" ' .
$pagingdisabled . ' /></div>';
$reordercontrols1 .= '<div class="quizdeleteselected">' .
'<input type="submit" name="quizdeleteselected" ' .
'onclick="return confirm(\'' .
get_string('areyousureremoveselected', 'quiz') . '\');" value="' .
get_string('removeselected', 'quiz') . '" ' . $disabled . ' /></div>';
$a = '<input name="moveselectedonpagetop" type="text" size="2" ' .
$pagingdisabled . ' />';
$reordercontrols2top = '<div class="moveselectedonpage">' .
get_string('moveselectedonpage', 'quiz', $a) .
'<input type="submit" name="savechanges" value="' .
$strmove . '" ' . $pagingdisabled . ' />' . '
<br /><input type="submit" name="savechanges" value="' .
$strreorderquestions . '" /></div>';
$reordercontrols2bottom = '<div class="moveselectedonpage">' .
'<input type="submit" name="savechanges" value="' .
$strreorderquestions . '" /><br />' .
get_string('moveselectedonpage', 'quiz', $a) .
'<input type="submit" name="savechanges" value="' .
$strmove . '" ' . $pagingdisabled . ' /> ' . '</div>';
$reordercontrols3 = '<a href="javascript:select_all_in(\'FORM\', null, ' .
'\'quizquestions\');">' .
$strselectall . '</a> /';
$reordercontrols3.= ' <a href="javascript:deselect_all_in(\'FORM\', ' .
'null, \'quizquestions\');">' .
$strselectnone . '</a>';
$reordercontrolstop = '<div class="reordercontrols">' .
$reordercontrolssetdefaultsubmit .
$reordercontrols1 . $reordercontrols2top . $reordercontrols3 . "</div>";
$reordercontrolsbottom = '<div class="reordercontrols">' .
$reordercontrolssetdefaultsubmit .
$reordercontrols2bottom . $reordercontrols1 . $reordercontrols3 . "</div>";
if ($reordertool) {
echo '<form method="post" action="edit.php" id="quizquestions"><div>';
echo html_writer::input_hidden_params($pageurl);
echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
echo $reordercontrolstop;
}
//the current question ordinal (no descriptions)
$qno = 1;
//the current question (includes questions and descriptions)
$questioncount = 0;
//the ordinal of current element in the layout
//(includes page breaks, questions and descriptions)
$count = 0;
//the current page number in iteration
$pagecount = 0;
$sumgrade = 0;
$pageopen = false;
$returnurl = str_replace($CFG->wwwroot, '', $pageurl->out(false));
$questiontotalcount = count($order);
foreach ($order as $i => $qnum) {
$reordercheckbox = '';
$reordercheckboxlabel = '';
$reordercheckboxlabelclose = '';
if ($qnum && empty($questions[$qnum])) {
continue;
}
// If the questiontype is missing change the question type
if ($qnum && !array_key_exists($questions[$qnum]->qtype, $QTYPES)) {
$questions[$qnum]->qtype = 'missingtype';
}
if ($qnum != 0 || ($qnum == 0 && !$pageopen)) {
//this is either a question or a page break after another
// (no page is currently open)
if (!$pageopen) {
//if no page is open, start display of a page
$pagecount++;
echo '<div class="quizpage"><span class="pagetitle">' .
get_string('page') . ' ' . $pagecount .
'</span><div class="pagecontent">';
$pageopen = true;
}
if ($qnum == 0 && $i < $questiontotalcount) {
// This is the second successive page break. Tell the user the page is empty.
echo '<div class="pagestatus">';
print_string('noquestionsonpage', 'quiz');
echo '</div>';
if ($allowdelete) {
echo '<div class="quizpagedelete">';
echo '<a title="' . get_string('removeemptypage', 'quiz') . '" href="' .
$pageurl->out(true, array('deleteemptypage' => $i - 1, 'sesskey'=>sesskey())) .
'"><img src="' . $OUTPUT->pix_url('t/delete') . '" ' .
'class="iconsmall" alt="' . $strremove . '" /></a>';
echo '</div>';
}
}
if ($qnum != 0) {
$question = $questions[$qnum];
$questionparams = array(
'returnurl' => $returnurl,
'cmid' => $quiz->cmid,
'id' => $question->id);
$questionurl = new moodle_url('/question/question.php',
$questionparams);
$questioncount++;
//this is an actual question
/* Display question start */
?>
<div class="question">
<div class="questioncontainer <?php echo $question->qtype; ?>">
<div class="qnum">
<?php
$reordercheckbox = '';
$reordercheckboxlabel = '';
$reordercheckboxlabelclose = '';
if ($reordertool) {
$reordercheckbox = '<input type="checkbox" name="s' . $question->id .
'" id="s' . $question->id . '" />';
$reordercheckboxlabel = '<label for="s' . $question->id . '">';
$reordercheckboxlabelclose = '</label>';
}
if (!$quiz->shufflequestions) {
// Print and increment question number
$questioncountstring = '';
if ($questioncount>999 || ($reordertool && $questioncount>99)) {
$questioncountstring =
"$reordercheckboxlabel<small>$questioncount</small>" .
$reordercheckboxlabelclose . $reordercheckbox;
} else {
$questioncountstring = $reordercheckboxlabel . $questioncount .
$reordercheckboxlabelclose . $reordercheckbox;
}
echo $questioncountstring;
$qno += $question->length;
} else {
echo "$reordercheckboxlabel ? $reordercheckboxlabelclose" .
" $reordercheckbox";
}
?>
</div>
<div class="content">
<div class="questioncontrols">
<?php
if ($count != 0) {
if (!$hasattempts) {
$upbuttonclass = '';
if ($count >= $lastindex - 1) {
$upbuttonclass = 'upwithoutdown';
}
echo "<a title=\"$strmoveup\" href=\"" .
$pageurl->out(true, array('up' => $question->id, 'sesskey'=>sesskey())) . "\"><img
src=\"" . $OUTPUT->pix_url('t/up') . "\" class=\"iconsmall
$upbuttonclass\" alt=\"$strmoveup\" /></a>";
}
}
if ($count < $lastindex - 1) {
if (!$hasattempts) {
echo "<a title=\"$strmovedown\" href=\"" .
$pageurl->out(true, array('down' => $question->id, 'sesskey'=>sesskey())) . "\"><img
src=\"" . $OUTPUT->pix_url('t/down') . "\" class=\"iconsmall\"" .
" alt=\"$strmovedown\" /></a>";
}
}
if ($allowdelete && question_has_capability_on($question, 'use', $question->category)) {
// remove from quiz, not question delete.
if (!$hasattempts) {
echo "<a title=\"$strremove\" href=\"" .
$pageurl->out(true, array('remove' => $question->id, 'sesskey'=>sesskey())) . "\">
<img src=\"" . $OUTPUT->pix_url('t/delete') . "\" " .
"class=\"iconsmall\" alt=\"$strremove\" /></a>";
}
}
?>
</div><?php
if ($question->qtype != 'description' && !$reordertool) {
?>
<div class="points">
<form method="post" action="edit.php"><div>
<fieldset class="invisiblefieldset" style="display: block;">
<label for="<?php echo "inputq$question->id" ?>"><?php echo $strgrade; ?></label>:<br />
<input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
<?php echo html_writer::input_hidden_params($pageurl); ?>
<input type="hidden" name="savechanges" value="save" />
<?php
echo '<input type="text" name="g' . $question->id . '" id="inputq' . $question->id .
'" size="' . ($quiz->decimalpoints + 2) . '" value="' . (0 + $quiz->grades[$qnum]) .
'" tabindex="' . ($lastindex + $qno) . '" />';
?>
<input type="submit" class="pointssubmitbutton" value="<?php echo $strsave; ?>" />
</fieldset>
<?php if ($question->qtype == 'random') {
echo '<a href="' . $questionurl->out() . '" class="configurerandomquestion">' . get_string("configurerandomquestion", "quiz") . '</a>';
}
?>
</div>
</form>
</div>
<?php
} else if ($reordertool) {
if ($qnum) {
?>
<div class="qorder">
<?php
echo '<input type="text" name="o' . $question->id . '" size="2" value="' .
(10*$count + 10) .
'" tabindex="' . ($lastindex + $qno) .
'" />';
?>
</div>
<?php
}
}
?>
<div class="questioncontentcontainer">
<?php
if ($question->qtype == 'random') { // it is a random question
if (!$reordertool) {
quiz_print_randomquestion($question, $pageurl, $quiz, $quiz_qbanktool);
} else {
quiz_print_randomquestion_reordertool($question, $pageurl, $quiz);
}
} else { // it is a single question
if (!$reordertool) {
quiz_print_singlequestion($question, $returnurl, $quiz);
} else {
quiz_print_singlequestion_reordertool($question, $returnurl, $quiz);
}
}
?>
</div>
</div>
</div>
</div>
<?php
/* Display question end */
$count++;
$sumgrade += $quiz->grades[$qnum];
}
}
//a page break: end the existing page.
if ($qnum == 0) {
if ($pageopen) {
if (!$reordertool && !($quiz->shufflequestions && $i < $questiontotalcount - 1)) {
quiz_print_pagecontrols($quiz, $pageurl, $pagecount,
$hasattempts, $defaultcategoryobj);
} else if ($i < $questiontotalcount - 1) {
//do not include the last page break for reordering
//to avoid creating a new extra page in the end
echo '<input type="hidden" name="opg' . $pagecount . '" size="2" value="' .
(10*$count + 10) . '" />';
}
echo "</div></div>";
if (!$reordertool && !$quiz->shufflequestions) {
echo $OUTPUT->container_start('addpage');
$url = new moodle_url($pageurl->out_omit_querystring(), array('cmid' => $quiz->cmid, 'courseid' => $quiz->course, 'addpage' => $count, 'sesskey' => sesskey()));
echo $OUTPUT->single_button($url, get_string('addpagehere', 'quiz'), 'get', array('disabled'=>$hasattempts));
echo $OUTPUT->container_end();
}
$pageopen = false;
$count++;
}
}
}
if ($reordertool) {
echo $reordercontrolsbottom;
echo '</div></form>';
}
return $sumgrade;
}
/**
* Print all the controls for adding questions directly into the
* specific page in the edit tab of edit.php
*
* @param unknown_type $quiz
* @param unknown_type $pageurl
* @param unknown_type $page
* @param unknown_type $hasattempts
*/
function quiz_print_pagecontrols($quiz, $pageurl, $page, $hasattempts, $defaultcategoryobj) {
global $CFG, $OUTPUT;
static $randombuttoncount = 0;
$randombuttoncount++;
echo '<div class="pagecontrols">';
// Get the current context
$thiscontext = get_context_instance(CONTEXT_COURSE, $quiz->course);
$contexts = new question_edit_contexts($thiscontext);
// Get the default category.
list($defaultcategoryid) = explode(',', $pageurl->param('cat'));
if (empty($defaultcategoryid)) {
$defaultcategoryid = $defaultcategoryobj->id;
}
// Create the url the question page will return to
$returnurladdtoquiz = new moodle_url($pageurl, array('addonpage' => $page));
// Print a button linking to the choose question type page.
$returnurladdtoquiz = str_replace($CFG->wwwroot, '', $returnurladdtoquiz->out(false));
$newquestionparams = array('returnurl' => $returnurladdtoquiz,
'cmid' => $quiz->cmid, 'appendqnumstring' => 'addquestion');
create_new_question_button($defaultcategoryid, $newquestionparams, get_string('addaquestion', 'quiz'),
get_string('createquestionandadd', 'quiz'), $hasattempts);
if ($hasattempts) {
$disabled = 'disabled="disabled"';
} else {
$disabled = '';
}
?>
<div class="singlebutton">
<form class="randomquestionform" action="<?php echo $CFG->wwwroot; ?>/mod/quiz/addrandom.php" method="get">
<div>
<input type="hidden" class="addonpage_formelement" name="addonpage" value="<?php echo $page; ?>" />
<input type="hidden" name="cmid" value="<?php echo $quiz->cmid; ?>" />
<input type="hidden" name="courseid" value="<?php echo $quiz->course; ?>" />
<input type="hidden" name="category" value="<?php echo $pageurl->param('cat'); ?>" />
<input type="hidden" name="returnurl" value="<?php echo s(str_replace($CFG->wwwroot, '', $pageurl->out(false))); ?>" />
<input type="submit" id="addrandomdialoglaunch_<?php echo $randombuttoncount; ?>" value="<?php echo get_string('addarandomquestion', 'quiz'); ?>" <?php echo " $disabled"; ?> />
</div>
</form>
</div>
<?php echo $OUTPUT->help_icon('addarandomquestion', 'quiz'); ?>
<?php
echo "\n</div>";
}
/**
* Print a given single question in quiz for the edit tab of edit.php.
* Meant to be used from quiz_print_question_list()
*
* @param object $question A question object from the database questions table
* @param object $returnurl The url to get back to this page, for example after editing.
* @param object $quiz The quiz in the context of which the question is being displayed
*/
function quiz_print_singlequestion($question, $returnurl, $quiz) {
global $QTYPES;
echo '<div class="singlequestion">';
echo quiz_question_edit_button($quiz->cmid, $question, $returnurl, quiz_question_tostring($question) . ' ');
echo '<span class="questiontype">';
$namestr = $QTYPES[$question->qtype]->local_name();
print_question_icon($question);
echo " $namestr</span>";
echo '<span class="questionpreview">' . quiz_question_preview_button($quiz, $question, true) . '</span>';
echo "</div>\n";
}
/**
* Print a given random question in quiz for the edit tab of edit.php.
* Meant to be used from quiz_print_question_list()
*
* @param object $question A question object from the database questions table
* @param object $questionurl The url of the question editing page as a moodle_url object
* @param object $quiz The quiz in the context of which the question is being displayed
* @param boolean $quiz_qbanktool Indicate to this function if the question bank window open
*/
function quiz_print_randomquestion(&$question, &$pageurl, &$quiz, $quiz_qbanktool) {
global $DB, $QTYPES, $OUTPUT;
echo '<div class="quiz_randomquestion">';
if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
echo $OUTPUT->notification('Random question category not found!');
return;
}
echo '<div class="randomquestionfromcategory">';
print_question_icon($question);
print_random_option_icon($question);
echo ' ' . get_string('randomfromcategory', 'quiz') . '</div>';
$a = new stdClass;
$a->arrow = $OUTPUT->rarrow();
$strshowcategorycontents = get_string('showcategorycontents', 'quiz', $a);
$openqbankurl = $pageurl->out(true, array('qbanktool' => 1,
'cat' => $category->id . ',' . $category->contextid));
$linkcategorycontents = ' <a href="' . $openqbankurl . '">' . $strshowcategorycontents . '</a>';
echo '<div class="randomquestioncategory">';
echo '<a href="' . $openqbankurl . '" title="' . $strshowcategorycontents . '">' . $category->name . '</a>';
echo '<span class="questionpreview">' . quiz_question_preview_button($quiz, $question, true) . '</span>';
echo '</div>';
$questionids = $QTYPES['random']->get_usable_questions_from_category(
$category->id, $question->questiontext == '1', '0');
$questioncount = count($questionids);
echo '<div class="randomquestionqlist">';
if ($questioncount == 0) {
// No questions in category, give an error plus instructions
echo '<span class="error">';
print_string('noquestionsnotinuse', 'quiz');
echo '</span>';
echo '<br />';
// Embed the link into the string with instructions
$a = new stdClass;
$a->catname = '<strong>' . $category->name . '</strong>';
$a->link = $linkcategorycontents;
echo get_string('addnewquestionsqbank', 'quiz', $a);
} else {
// Category has questions
// Get a sample from the database,
$toshow = array_slice($questionids, 0, NUM_QS_TO_SHOW_IN_RANDOM);
$questionidstoshow = array();
foreach ($toshow as $a) {
$questionidstoshow[] = $a->id;
}
$questionstoshow = $DB->get_records_list('question', 'id', $questionidstoshow,
'', 'id,qtype,name,questiontext,questiontextformat');
// list them,
echo '<ul>';
foreach ($questionstoshow as $question) {
echo '<li>' . quiz_question_tostring($question, true) . '</li>';
}
// and then display the total number.
echo '<li class="totalquestionsinrandomqcategory">';
if ($questioncount > NUM_QS_TO_SHOW_IN_RANDOM) {
echo '... ';
}
print_string('totalquestionsinrandomqcategory', 'quiz', $questioncount);
echo ' ' . $linkcategorycontents;
echo '</li>';
echo '</ul>';
}
echo '</div>';
echo '<div class="randomquestioncategorycount">';
echo '</div>';
echo '</div>';
}
/**
* Print a given single question in quiz for the reordertool tab of edit.php.
* Meant to be used from quiz_print_question_list()
*
* @param object $question A question object from the database questions table
* @param object $questionurl The url of the question editing page as a moodle_url object
* @param object $quiz The quiz in the context of which the question is being displayed
*/
function quiz_print_singlequestion_reordertool($question, $returnurl, $quiz) {
echo '<div class="singlequestion">';
echo '<label for="s' . $question->id . '">';
print_question_icon($question);
echo ' ' . quiz_question_tostring($question);
echo '</label>';
echo '<span class="questionpreview">' .
quiz_question_action_icons($quiz, $quiz->cmid, $question, $returnurl) . '</span>';
echo "</div>\n";
}
/**
* Print a given random question in quiz for the reordertool tab of edit.php.
* Meant to be used from quiz_print_question_list()
*
* @param object $question A question object from the database questions table
* @param object $questionurl The url of the question editing page as a moodle_url object
* @param object $quiz The quiz in the context of which the question is being displayed
*/
function quiz_print_randomquestion_reordertool(&$question, &$pageurl, &$quiz) {
global $DB, $QTYPES, $OUTPUT;
// Load the category, and the number of available questions in it.
if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
echo $OUTPUT->notification('Random question category not found!');
return;
}
$questioncount = count($QTYPES['random']->get_usable_questions_from_category(
$category->id, $question->questiontext == '1', '0'));
$reordercheckboxlabel = '<label for="s' . $question->id . '">';
$reordercheckboxlabelclose = '</label>';
echo '<div class="quiz_randomquestion">';
echo '<div class="randomquestionfromcategory">';
echo $reordercheckboxlabel;
print_question_icon($question);
print_random_option_icon($question);
if ($questioncount == 0) {
echo '<span class="error">';
print_string('empty', 'quiz');
echo '</span> ';
}
print_string('random', 'quiz');
echo ": $reordercheckboxlabelclose</div>";
echo '<div class="randomquestioncategory">';
echo $reordercheckboxlabel . $category->name . $reordercheckboxlabelclose;
echo '<span class="questionpreview">';
echo quiz_question_preview_button($quiz, $question, false);
echo '</span>';
echo "</div>";
echo '<div class="randomquestioncategorycount">';
echo '</div>';
echo '</div>';
}
/**
* Print an icon to indicate the 'include subcategories' state of a random question.
* @param $question the random question.
*/
function print_random_option_icon($question) {
global $OUTPUT;
if (!empty($question->questiontext)) {
$icon = 'withsubcat';
$tooltip = get_string('randomwithsubcat', 'quiz');
} else {
$icon = 'nosubcat';
$tooltip = get_string('randomnosubcat', 'quiz');
}
echo '<img src="' . $OUTPUT->pix_url('i/' . $icon) . '" alt="' .
$tooltip . '" title="' . $tooltip . '" class="uihint" />';
}
/**
* Creates a textual representation of a question for display.
*
* @param object $question A question object from the database questions table
* @param boolean $showicon If true, show the question's icon with the question. False by default.
* @param boolean $showquestiontext If true (default), show question text after question name.
* If false, show only question name.
* @param boolean $return If true (default), return the output. If false, print it.
*/
function quiz_question_tostring(&$question, $showicon = false, $showquestiontext = true, $return = true) {
global $COURSE;
$result = '';
$result .= '<span class="questionname">';
if ($showicon) {
$result .= print_question_icon($question, true);
echo " ";
}
$result .= shorten_text(format_string($question->name), 200) . '</span>';
if ($showquestiontext) {
$formatoptions = new stdClass;
$formatoptions->noclean = true;
$formatoptions->para = false;
$questiontext = strip_tags(format_text($question->questiontext,
$question->questiontextformat,
$formatoptions, $COURSE->id));
$questiontext = shorten_text($questiontext, 200);
$result .= '<span class="questiontext">';
if (!empty($questiontext)) {
$result .= $questiontext;
} else {
$result .= '<span class="error">';
$result .= get_string('questiontextisempty', 'quiz');
$result .= '</span>';
}
$result .= '</span>';
}
if ($return) {
return $result;
} else {
echo $result;
}
}
/**
* A column type for the add this question to the quiz.
*/
class question_bank_add_to_quiz_action_column extends question_bank_action_column_base {
protected $stradd;
public function init() {
parent::init();
$this->stradd = get_string('addtoquiz', 'quiz');
}
public function get_name() {
return 'addtoquizaction';
}
protected function display_content($question, $rowclasses) {
// for RTL languages: switch right and left arrows
if (right_to_left()) {
$movearrow = 't/removeright';
} else {
$movearrow = 't/moveleft';
}
$this->print_icon($movearrow, $this->stradd, $this->qbank->add_to_quiz_url($question->id));
}
public function get_required_fields() {
return array('q.id');
}
}
/**
* A column type for the name followed by the start of the question text.
*/
class question_bank_question_name_text_column extends question_bank_question_name_column {
public function get_name() {