2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This contains functions that are called from within the quiz module only
20 * Functions that are also called by core Moodle are in {@link lib.php}
21 * This script also loads the code in {@link questionlib.php} which holds
22 * the module-indpendent code for handling questions and which in turn
23 * initialises all the questiontype classes.
26 * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 defined('MOODLE_INTERNAL') ||
die();
33 require_once($CFG->dirroot
. '/mod/quiz/locallib.php');
35 define('NUM_QS_TO_SHOW_IN_RANDOM', 3);
38 * Verify that the question exists, and the user has permission to use it.
39 * Does not return. Throws an exception if the question cannot be used.
40 * @param int $questionid The id of the question.
42 function quiz_require_question_use($questionid) {
44 $question = $DB->get_record('question', array('id' => $questionid), '*', MUST_EXIST
);
45 question_require_capability_on($question, 'use');
49 * Verify that the question exists, and the user has permission to use it.
50 * @param object $quiz the quiz settings.
51 * @param int $slot which question in the quiz to test.
52 * @return bool whether the user can use this question.
54 function quiz_has_question_use($quiz, $slot) {
56 $question = $DB->get_record_sql("
58 FROM {quiz_slots} slot
59 JOIN {question} q ON q.id = slot.questionid
60 WHERE slot.quizid = ? AND slot.slot = ?", array($quiz->id
, $slot));
64 return question_has_capability_on($question, 'use');
68 * Remove a question from a quiz
69 * @param object $quiz the quiz object.
70 * @param int $questionid The id of the question to be deleted.
72 function quiz_remove_slot($quiz, $slotnumber) {
75 $slot = $DB->get_record('quiz_slots', array('quizid' => $quiz->id
, 'slot' => $slotnumber));
76 $maxslot = $DB->get_field_sql('SELECT MAX(slot) FROM {quiz_slots} WHERE quizid = ?', array($quiz->id
));
81 $trans = $DB->start_delegated_transaction();
82 $DB->delete_records('quiz_slots', array('id' => $slot->id
));
83 for ($i = $slot->slot +
1; $i <= $maxslot; $i++
) {
84 $DB->set_field('quiz_slots', 'slot', $i - 1,
85 array('quizid' => $quiz->id
, 'slot' => $i));
88 $qtype = $DB->get_field('question', 'qtype', array('id' => $slot->questionid
));
89 if ($qtype === 'random') {
90 // This function automatically checks if the question is in use, and won't delete if it is.
91 question_delete_question($slot->questionid
);
94 $trans->allow_commit();
98 * Remove an empty page from the quiz layout. If that is not possible, do nothing.
99 * @param object $quiz the quiz settings.
100 * @param int $pagenumber the page number to delete.
102 function quiz_delete_empty_page($quiz, $pagenumber) {
105 if ($DB->record_exists('quiz_slots', array('quizid' => $quiz->id
, 'page' => $pagenumber))) {
106 // This was not an empty page.
110 $DB->execute('UPDATE {quiz_slots} SET page = page - 1 WHERE quizid = ? AND page > ?',
111 array($quiz->id
, $pagenumber));
115 * Add a question to a quiz
117 * Adds a question to a quiz by updating $quiz as well as the
118 * quiz and quiz_slots tables. It also adds a page break if required.
119 * @param int $questionid The id of the question to be added
120 * @param object $quiz The extended quiz object as used by edit.php
121 * This is updated by this function
122 * @param int $page Which page in quiz to add the question on. If 0 (default),
124 * @param float $maxmark The maximum mark to set for this question. (Optional,
125 * defaults to question.defaultmark.
126 * @return bool false if the question was already in the quiz
128 function quiz_add_quiz_question($questionid, $quiz, $page = 0, $maxmark = null) {
130 $slots = $DB->get_records('quiz_slots', array('quizid' => $quiz->id
),
131 'slot', 'questionid, slot, page, id');
132 if (array_key_exists($questionid, $slots)) {
136 $trans = $DB->start_delegated_transaction();
140 foreach ($slots as $slot) {
141 if ($slot->page
> $maxpage) {
142 $maxpage = $slot->page
;
149 // Add the new question instance.
150 $slot = new stdClass();
151 $slot->quizid
= $quiz->id
;
152 $slot->questionid
= $questionid;
154 if ($maxmark !== null) {
155 $slot->maxmark
= $maxmark;
157 $slot->maxmark
= $DB->get_field('question', 'defaultmark', array('id' => $questionid));
160 if (is_int($page) && $page >= 1) {
161 // Adding on a given page.
163 foreach (array_reverse($slots) as $otherslot) {
164 if ($otherslot->page
> $page) {
165 $DB->set_field('quiz_slots', 'slot', $otherslot->slot +
1, array('id' => $otherslot->id
));
167 $lastslotbefore = $otherslot->slot
;
171 $slot->slot
= $lastslotbefore +
1;
172 $slot->page
= min($page, $maxpage +
1);
175 $lastslot = end($slots);
177 $slot->slot
= $lastslot->slot +
1;
181 if ($quiz->questionsperpage
&& $numonlastpage >= $quiz->questionsperpage
) {
182 $slot->page
= $maxpage +
1;
184 $slot->page
= $maxpage;
188 $DB->insert_record('quiz_slots', $slot);
189 $trans->allow_commit();
193 * Add a random question to the quiz at a given point.
194 * @param object $quiz the quiz settings.
195 * @param int $addonpage the page on which to add the question.
196 * @param int $categoryid the question category to add the question from.
197 * @param int $number the number of random questions to add.
198 * @param bool $includesubcategories whether to include questoins from subcategories.
200 function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number,
201 $includesubcategories) {
204 $category = $DB->get_record('question_categories', array('id' => $categoryid));
206 print_error('invalidcategoryid', 'error');
209 $catcontext = context
::instance_by_id($category->contextid
);
210 require_capability('moodle/question:useall', $catcontext);
212 // Find existing random questions in this category that are
213 // not used by any quiz.
214 if ($existingquestions = $DB->get_records_sql(
215 "SELECT q.id, q.qtype FROM {question} q
216 WHERE qtype = 'random'
218 AND " . $DB->sql_compare_text('questiontext') . " = ?
222 WHERE questionid = q.id)
223 ORDER BY id", array($category->id
, ($includesubcategories ?
'1' : '0')))) {
224 // Take as many of these as needed.
225 while (($existingquestion = array_shift($existingquestions)) && $number > 0) {
226 quiz_add_quiz_question($existingquestion->id
, $quiz, $addonpage);
235 // More random questions are needed, create them.
236 for ($i = 0; $i < $number; $i +
= 1) {
237 $form = new stdClass();
238 $form->questiontext
= array('text' => ($includesubcategories ?
'1' : '0'), 'format' => 0);
239 $form->category
= $category->id
. ',' . $category->contextid
;
240 $form->defaultmark
= 1;
242 $form->stamp
= make_unique_id_code(); // Set the unique code (not to be changed).
243 $question = new stdClass();
244 $question->qtype
= 'random';
245 $question = question_bank
::get_qtype('random')->save_question($question, $form);
246 if (!isset($question->id
)) {
247 print_error('cannotinsertrandomquestion', 'quiz');
249 quiz_add_quiz_question($question->id
, $quiz, $addonpage);
254 * Add a page break after a particular slot.
255 * @param object $quiz the quiz settings.
256 * @param int $slot the slot to add the page break after.
258 function quiz_add_page_break_after_slot($quiz, $slot) {
261 $DB->execute('UPDATE {quiz_slots} SET page = page + 1 WHERE quizid = ? AND slot > ?',
262 array($quiz->id
, $slot));
266 * Change the max mark for a slot.
268 * Saves changes to the question grades in the quiz_slots table and any
269 * corresponding question_attempts.
270 * It does not update 'sumgrades' in the quiz table.
272 * @param stdClass $slot row from the quiz_slots table.
273 * @param float $maxmark the new maxmark.
275 function quiz_update_slot_maxmark($slot, $maxmark) {
278 if (abs($maxmark - $slot->maxmark
) < 1e-7) {
279 // Grade has not changed. Nothing to do.
283 $slot->maxmark
= $maxmark;
284 $DB->update_record('quiz_slots', $slot);
285 question_engine
::set_max_mark_in_attempts(new qubaids_for_quiz($slot->quizid
),
286 $slot->slot
, $maxmark);
290 * Private function used by the following two.
291 * @param object $quiz the quiz settings.
292 * @param int $slotnumber the slot to move up.
293 * @param int $shift +1 means move down, -1 means move up.
295 function _quiz_move_question($quiz, $slotnumber, $shift) {
298 if (!$slotnumber ||
!($shift == 1 ||
$shift == -1)) {
302 $slot = $DB->get_record('quiz_slots',
303 array('quizid' => $quiz->id
, 'slot' => $slotnumber));
308 $otherslot = $DB->get_record('quiz_slots',
309 array('quizid' => $quiz->id
, 'slot' => $slotnumber +
$shift));
311 // Must be first or last question being moved further that way if we can.
312 if ($shift +
$slot->page
> 0) {
313 $DB->set_field('quiz_slots', 'page', $slot->page +
$shift, array('id' => $slot->id
));
318 if ($otherslot->page
!= $slot->page
) {
319 $DB->set_field('quiz_slots', 'page', $slot->page +
$shift, array('id' => $slot->id
));
323 $trans = $DB->start_delegated_transaction();
324 $DB->set_field('quiz_slots', 'slot', -1, array('id' => $slot->id
));
325 $DB->set_field('quiz_slots', 'slot', $slot->slot
, array('id' => $otherslot->id
));
326 $DB->set_field('quiz_slots', 'slot', $otherslot->slot
, array('id' => $slot->id
));
327 $trans->allow_commit();
331 * Move a particular question one space earlier in the quiz.
332 * If that is not possible, do nothing.
333 * @param object $quiz the quiz settings.
334 * @param int $slot the slot to move up.
336 function quiz_move_question_up($quiz, $slot) {
337 _quiz_move_question($quiz, $slot, -1);
341 * Move a particular question one space later in the quiz.
342 * If that is not possible, do nothing.
343 * @param object $quiz the quiz settings.
344 * @param int $slot the slot to move down.
346 function quiz_move_question_down($quiz, $slot) {
347 return _quiz_move_question($quiz, $slot, 1);
351 * Prints a list of quiz questions for the edit.php main view for edit
352 * ($reordertool = false) and order and paging ($reordertool = true) tabs
354 * @param object $quiz The quiz settings.
355 * @param moodle_url $pageurl The url of the current page with the parameters required
356 * for links returning to the current page, as a moodle_url object
357 * @param bool $allowdelete Indicates whether the delete icons should be displayed
358 * @param bool $reordertool Indicates whether the reorder tool should be displayed
359 * @param bool $quiz_qbanktool Indicates whether the question bank should be displayed
360 * @param bool $hasattempts Indicates whether the quiz has attempts
361 * @param object $defaultcategoryobj
362 * @param bool $canaddquestion is the user able to add and use questions anywere?
363 * @param bool $canaddrandom is the user able to add random questions anywere?
365 function quiz_print_question_list($quiz, $pageurl, $allowdelete, $reordertool,
366 $quiz_qbanktool, $hasattempts, $defaultcategoryobj, $canaddquestion, $canaddrandom) {
367 global $CFG, $DB, $OUTPUT;
368 $strorder = get_string('order');
369 $strquestionname = get_string('questionname', 'quiz');
370 $strmaxmark = get_string('markedoutof', 'question');
371 $strremove = get_string('remove', 'quiz');
372 $stredit = get_string('edit');
373 $strview = get_string('view');
374 $straction = get_string('action');
375 $strmove = get_string('move');
376 $strmoveup = get_string('moveup');
377 $strmovedown = get_string('movedown');
378 $strsave = get_string('save', 'quiz');
379 $strreorderquestions = get_string('reorderquestions', 'quiz');
381 $strselectall = get_string('selectall', 'quiz');
382 $strselectnone = get_string('selectnone', 'quiz');
383 $strtype = get_string('type', 'quiz');
384 $strpreview = get_string('preview', 'quiz');
386 $questions = $DB->get_records_sql("SELECT slot.slot, q.*, qc.contextid, slot.page, slot.maxmark
387 FROM {quiz_slots} slot
388 LEFT JOIN {question} q ON q.id = slot.questionid
389 LEFT JOIN {question_categories} qc ON qc.id = q.category
390 WHERE slot.quizid = ?
391 ORDER BY slot.slot", array($quiz->id
));
393 $lastindex = count($questions) - 1;
396 $pagingdisabled = '';
398 $disabled = 'disabled="disabled"';
400 if ($hasattempts ||
$quiz->shufflequestions
) {
401 $pagingdisabled = 'disabled="disabled"';
404 $reordercontrolssetdefaultsubmit = '<div style="display:none;">' .
405 '<input type="submit" name="savechanges" value="' .
406 $strreorderquestions . '" ' . $pagingdisabled . ' /></div>';
407 $reordercontrols1 = '<div class="addnewpagesafterselected">' .
408 '<input type="submit" name="addnewpagesafterselected" value="' .
409 get_string('addnewpagesafterselected', 'quiz') . '" ' .
410 $pagingdisabled . ' /></div>';
411 $reordercontrols1 .= '<div class="quizdeleteselected">' .
412 '<input type="submit" name="quizdeleteselected" ' .
413 'onclick="return confirm(\'' .
414 get_string('areyousureremoveselected', 'quiz') . '\');" value="' .
415 get_string('removeselected', 'quiz') . '" ' . $disabled . ' /></div>';
417 $a = '<input name="moveselectedonpagetop" type="text" size="2" ' .
418 $pagingdisabled . ' />';
419 $b = '<input name="moveselectedonpagebottom" type="text" size="2" ' .
420 $pagingdisabled . ' />';
422 $reordercontrols2top = '<div class="moveselectedonpage">' .
423 '<label>' . get_string('moveselectedonpage', 'quiz', $a) . '</label>' .
424 '<input type="submit" name="savechanges" value="' .
425 $strmove . '" ' . $pagingdisabled . ' />' . '
426 <br /><input type="submit" name="savechanges" value="' .
427 $strreorderquestions . '" /></div>';
428 $reordercontrols2bottom = '<div class="moveselectedonpage">' .
429 '<input type="submit" name="savechanges" value="' .
430 $strreorderquestions . '" /><br />' .
431 '<label>' . get_string('moveselectedonpage', 'quiz', $b) . '</label>' .
432 '<input type="submit" name="savechanges" value="' .
433 $strmove . '" ' . $pagingdisabled . ' /> ' . '</div>';
435 $reordercontrols3 = '<a href="javascript:select_all_in(\'FORM\', null, ' .
436 '\'quizquestions\');">' .
437 $strselectall . '</a> /';
438 $reordercontrols3.= ' <a href="javascript:deselect_all_in(\'FORM\', ' .
439 'null, \'quizquestions\');">' .
440 $strselectnone . '</a>';
442 $reordercontrolstop = '<div class="reordercontrols">' .
443 $reordercontrolssetdefaultsubmit .
444 $reordercontrols1 . $reordercontrols2top . $reordercontrols3 . "</div>";
445 $reordercontrolsbottom = '<div class="reordercontrols">' .
446 $reordercontrolssetdefaultsubmit .
447 $reordercontrols2bottom . $reordercontrols1 . $reordercontrols3 . "</div>";
450 echo '<form method="post" action="edit.php" id="quizquestions"><div>';
452 echo html_writer
::input_hidden_params($pageurl);
453 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
455 echo $reordercontrolstop;
458 // Build fake order for backwards compatibility.
461 foreach ($questions as $question) {
462 while ($question->page
> $currentpage) {
466 $order[] = $question->slot
;
470 // The current question ordinal (no descriptions).
472 // The current question (includes questions and descriptions).
474 // The current page number in iteration.
480 $returnurl = $pageurl->out_as_local_url(false);
481 $questiontotalcount = count($order);
483 $lastquestion = new stdClass();
484 $lastquestion->slot
= 0; // Used to get the add page here buttons right.
485 foreach ($order as $count => $qnum) { // Note: $qnum is acutally slot number, if it is not 0.
487 $reordercheckbox = '';
488 $reordercheckboxlabel = '';
489 $reordercheckboxlabelclose = '';
491 // If the questiontype is missing change the question type.
492 if ($qnum && $questions[$qnum]->qtype
=== null) {
493 $questions[$qnum]->id
= $qnum;
494 $questions[$qnum]->category
= 0;
495 $questions[$qnum]->qtype
= 'missingtype';
496 $questions[$qnum]->name
= get_string('missingquestion', 'quiz');
497 $questions[$qnum]->questiontext
= ' ';
498 $questions[$qnum]->questiontextformat
= FORMAT_HTML
;
499 $questions[$qnum]->length
= 1;
501 } else if ($qnum && !question_bank
::qtype_exists($questions[$qnum]->qtype
)) {
502 $questions[$qnum]->qtype
= 'missingtype';
505 if ($qnum != 0 ||
($qnum == 0 && !$pageopen)) {
506 // This is either a question or a page break after another (no page is currently open).
508 // If no page is open, start display of a page.
510 echo '<div class="quizpage"><span class="pagetitle">' .
511 get_string('page') . ' ' . $pagecount .
512 '</span><div class="pagecontent">';
516 // This is the second successive page break. Tell the user the page is empty.
517 echo '<div class="pagestatus">';
518 print_string('noquestionsonpage', 'quiz');
521 echo '<div class="quizpagedelete">';
522 echo $OUTPUT->action_icon($pageurl->out(true,
523 array('deleteemptypage' => $pagecount, 'sesskey' => sesskey())),
524 new pix_icon('t/delete', $strremove),
525 new component_action('click',
526 'M.core_scroll_manager.save_scroll_action'),
527 array('title' => $strremove));
533 $question = $questions[$qnum];
534 $questionparams = array(
535 'returnurl' => $returnurl,
536 'cmid' => $quiz->cmid
,
537 'id' => $question->id
);
538 $questionurl = new moodle_url('/question/question.php',
542 // This is an actual question.
544 <div
class="question">
545 <div
class="questioncontainer <?php echo $question->qtype; ?>">
548 $reordercheckbox = '';
549 $reordercheckboxlabel = '';
550 $reordercheckboxlabelclose = '';
552 $reordercheckbox = '<input type="checkbox" name="s' . $question->slot
.
553 '" id="s' . $question->slot
. '" />';
554 $reordercheckboxlabel = '<label for="s' . $question->slot
. '">';
555 $reordercheckboxlabelclose = '</label>';
557 if ($question->length
== 0) {
558 $qnodisplay = get_string('infoshort', 'quiz');
559 } else if ($quiz->shufflequestions
) {
562 if ($qno > 999 ||
($reordertool && $qno > 99)) {
563 $qnodisplay = html_writer
::tag('small', $qno);
567 $qno +
= $question->length
;
569 echo $reordercheckboxlabel . $qnodisplay . $reordercheckboxlabelclose .
574 <div
class="content">
575 <div
class="questioncontrols">
580 echo $OUTPUT->action_icon($pageurl->out(true,
581 array('up' => $question->slot
, 'sesskey' => sesskey())),
582 new pix_icon('t/up', $strmoveup),
583 new component_action('click',
584 'M.core_scroll_manager.save_scroll_action'),
585 array('title' => $strmoveup));
590 echo $OUTPUT->action_icon($pageurl->out(true,
591 array('down' => $question->slot
, 'sesskey' => sesskey())),
592 new pix_icon('t/down', $strmovedown),
593 new component_action('click',
594 'M.core_scroll_manager.save_scroll_action'),
595 array('title' => $strmovedown));
597 if ($allowdelete && ($question->qtype
== 'missingtype' ||
598 question_has_capability_on($question, 'use', $question->category
))) {
599 // Remove from quiz, not question delete.
601 echo $OUTPUT->action_icon($pageurl->out(true,
602 array('remove' => $question->slot
, 'sesskey' => sesskey())),
603 new pix_icon('t/delete', $strremove),
604 new component_action('click',
605 'M.core_scroll_manager.save_scroll_action'),
606 array('title' => $strremove));
611 if (!in_array($question->qtype
, array('description', 'missingtype')) && !$reordertool) {
614 <form method
="post" action
="edit.php" class="quizsavegradesform"><div
>
615 <fieldset
class="invisiblefieldset" style
="display: block;">
616 <label
for="<?php echo 'inputq' . $question->slot; ?>"><?php
echo $strmaxmark; ?
></label
>:<br
/>
617 <input type
="hidden" name
="sesskey" value
="<?php echo sesskey() ?>" />
618 <?php
echo html_writer
::input_hidden_params($pageurl); ?
>
619 <input type
="hidden" name
="savechanges" value
="save" />
621 echo '<input type="text" name="g' . $question->slot
.
622 '" id="inputq' . $question->slot
.
623 '" size="' . ($quiz->decimalpoints +
2) .
624 '" value="' . (0 +
$question->maxmark
) .
625 '" tabindex="' . ($lastindex +
$qno) . '" />';
627 <input type
="submit" class="pointssubmitbutton" value
="<?php echo $strsave; ?>" />
630 if ($question->qtype
== 'random') {
631 echo '<a href="' . $questionurl->out() .
632 '" class="configurerandomquestion">' .
633 get_string("configurerandomquestion", "quiz") . '</a>';
642 } else if ($reordertool) {
647 echo '<label class="accesshide" for="o' . $question->slot
. '">' .
648 get_string('questionposition', 'quiz', $qnodisplay) . '</label>';
649 echo '<input type="text" name="o' . $question->slot
.
650 '" id="o' . $question->id
. '"' .
651 '" size="2" value="' . (10*$count +
10) .
652 '" tabindex="' . ($lastindex +
$qno) . '" />';
659 <div
class="questioncontentcontainer">
661 if ($question->qtype
== 'random') { // It is a random question.
663 quiz_print_randomquestion($question, $pageurl, $quiz, $quiz_qbanktool);
665 quiz_print_randomquestion_reordertool($question, $pageurl, $quiz);
667 } else { // It is a single question.
669 quiz_print_singlequestion($question, $returnurl, $quiz);
671 quiz_print_singlequestion_reordertool($question, $returnurl, $quiz);
683 // A page break: end the existing page.
686 if (!$reordertool && !($quiz->shufflequestions
&&
687 $count < $questiontotalcount - 1)) {
688 quiz_print_pagecontrols($quiz, $pageurl, $pagecount,
689 $hasattempts, $defaultcategoryobj, $canaddquestion, $canaddrandom);
690 } else if ($count < $questiontotalcount - 1) {
691 // Do not include the last page break for reordering
692 // to avoid creating a new extra page in the end.
693 echo '<input type="hidden" name="opg' . $pagecount . '" size="2" value="' .
694 (10*$count +
10) . '" />';
698 if (!$reordertool && !$quiz->shufflequestions
&& $count < $questiontotalcount - 1) {
699 echo $OUTPUT->container_start('addpage');
700 $url = new moodle_url($pageurl->out_omit_querystring(),
701 array('cmid' => $quiz->cmid
, 'courseid' => $quiz->course
,
702 'addpage' => $lastquestion->slot
, 'sesskey' => sesskey()));
703 echo $OUTPUT->single_button($url, get_string('addpagehere', 'quiz'), 'post',
704 array('disabled' => $hasattempts,
705 'actions' => array(new component_action('click',
706 'M.core_scroll_manager.save_scroll_action'))));
707 echo $OUTPUT->container_end();
715 $lastquestion = $question;
720 echo $reordercontrolsbottom;
721 echo '</div></form>';
726 * Print all the controls for adding questions directly into the
727 * specific page in the edit tab of edit.php
729 * @param object $quiz The quiz settings.
730 * @param moodle_url $pageurl The url of the current page with the parameters required
731 * for links returning to the current page, as a moodle_url object
732 * @param int $page the current page number.
733 * @param bool $hasattempts Indicates whether the quiz has attempts
734 * @param object $defaultcategoryobj
735 * @param bool $canaddquestion is the user able to add and use questions anywere?
736 * @param bool $canaddrandom is the user able to add random questions anywere?
738 function quiz_print_pagecontrols($quiz, $pageurl, $page, $hasattempts,
739 $defaultcategoryobj, $canaddquestion, $canaddrandom) {
740 global $CFG, $OUTPUT;
741 static $randombuttoncount = 0;
742 $randombuttoncount++
;
743 echo '<div class="pagecontrols">';
745 // Get the current context.
746 $thiscontext = context_course
::instance($quiz->course
);
747 $contexts = new question_edit_contexts($thiscontext);
749 // Get the default category.
750 list($defaultcategoryid) = explode(',', $pageurl->param('cat'));
751 if (empty($defaultcategoryid)) {
752 $defaultcategoryid = $defaultcategoryobj->id
;
755 if ($canaddquestion) {
756 // Create the url the question page will return to.
757 $returnurladdtoquiz = new moodle_url($pageurl, array('addonpage' => $page));
759 // Print a button linking to the choose question type page.
760 $returnurladdtoquiz = $returnurladdtoquiz->out_as_local_url(false);
761 $newquestionparams = array('returnurl' => $returnurladdtoquiz,
762 'cmid' => $quiz->cmid
, 'appendqnumstring' => 'addquestion');
763 create_new_question_button($defaultcategoryid, $newquestionparams,
764 get_string('addaquestion', 'quiz'),
765 get_string('createquestionandadd', 'quiz'), $hasattempts);
769 $disabled = 'disabled="disabled"';
775 <div
class="singlebutton">
776 <form
class="randomquestionform" action
="<?php echo $CFG->wwwroot;
777 ?>/mod/quiz/addrandom.php" method
="get">
779 <input type
="hidden" class="addonpage_formelement" name
="addonpage" value
="<?php
781 <input type
="hidden" name
="cmid" value
="<?php echo $quiz->cmid; ?>" />
782 <input type
="hidden" name
="courseid" value
="<?php echo $quiz->course; ?>" />
783 <input type
="hidden" name
="category" value
="<?php
784 echo $pageurl->param('cat'); ?>" />
785 <input type
="hidden" name
="returnurl" value
="<?php
786 echo s(str_replace($CFG->wwwroot, '', $pageurl->out(false))); ?>" />
787 <input type
="submit" id
="addrandomdialoglaunch_<?php
788 echo $randombuttoncount; ?>" value
="<?php
789 echo get_string('addarandomquestion', 'quiz'); ?>" <?php
790 echo " $disabled"; ?
> />
794 <?php
echo $OUTPUT->help_icon('addarandomquestion', 'quiz');
800 * Print a given single question in quiz for the edit tab of edit.php.
801 * Meant to be used from quiz_print_question_list()
803 * @param object $question A question object from the database questions table
804 * @param object $returnurl The url to get back to this page, for example after editing.
805 * @param object $quiz The quiz in the context of which the question is being displayed
807 function quiz_print_singlequestion($question, $returnurl, $quiz) {
808 echo '<div class="singlequestion ' . $question->qtype
. '">';
809 echo quiz_question_edit_button($quiz->cmid
, $question, $returnurl,
810 quiz_question_tostring($question) . ' ');
811 echo '<span class="questiontype">';
812 echo print_question_icon($question);
813 echo ' ' . question_bank
::get_qtype_name($question->qtype
) . '</span>';
814 echo '<span class="questionpreview">' .
815 quiz_question_preview_button($quiz, $question, true) . '</span>';
819 * Print a given random question in quiz for the edit tab of edit.php.
820 * Meant to be used from quiz_print_question_list()
822 * @param object $question A question object from the database questions table
823 * @param object $questionurl The url of the question editing page as a moodle_url object
824 * @param object $quiz The quiz in the context of which the question is being displayed
825 * @param bool $quiz_qbanktool Indicate to this function if the question bank window open
827 function quiz_print_randomquestion($question, $pageurl, $quiz, $quiz_qbanktool) {
829 echo '<div class="quiz_randomquestion">';
831 if (!$category = $DB->get_record('question_categories',
832 array('id' => $question->category
))) {
833 echo $OUTPUT->notification('Random question category not found!');
837 echo '<div class="randomquestionfromcategory">';
838 echo print_question_icon($question);
839 print_random_option_icon($question);
840 echo ' ' . get_string('randomfromcategory', 'quiz') . '</div>';
843 $a->arrow
= $OUTPUT->rarrow();
844 $strshowcategorycontents = get_string('showcategorycontents', 'quiz', $a);
846 $openqbankurl = $pageurl->out(true, array('qbanktool' => 1,
847 'cat' => $category->id
. ',' . $category->contextid
));
848 $linkcategorycontents = ' <a href="' . $openqbankurl . '">' . $strshowcategorycontents . '</a>';
850 echo '<div class="randomquestioncategory">';
851 echo '<a href="' . $openqbankurl . '" title="' . $strshowcategorycontents . '">' .
852 $category->name
. '</a>';
853 echo '<span class="questionpreview">' .
854 quiz_question_preview_button($quiz, $question, true) . '</span>';
857 $questionids = question_bank
::get_qtype('random')->get_available_questions_from_category(
858 $category->id
, $question->questiontext
== '1', '0');
859 $questioncount = count($questionids);
861 echo '<div class="randomquestionqlist">';
862 if ($questioncount == 0) {
863 // No questions in category, give an error plus instructions.
864 echo '<span class="error">';
865 print_string('noquestionsnotinuse', 'quiz');
869 // Embed the link into the string with instructions.
871 $a->catname
= '<strong>' . $category->name
. '</strong>';
872 $a->link
= $linkcategorycontents;
873 echo get_string('addnewquestionsqbank', 'quiz', $a);
876 // Category has questions.
878 // Get a sample from the database.
879 $questionidstoshow = array_slice($questionids, 0, NUM_QS_TO_SHOW_IN_RANDOM
);
880 $questionstoshow = $DB->get_records_list('question', 'id', $questionidstoshow,
881 '', 'id, qtype, name, questiontext, questiontextformat');
885 foreach ($questionstoshow as $subquestion) {
886 echo '<li>' . quiz_question_tostring($subquestion, true) . '</li>';
889 // Finally display the total number.
890 echo '<li class="totalquestionsinrandomqcategory">';
891 if ($questioncount > NUM_QS_TO_SHOW_IN_RANDOM
) {
894 print_string('totalquestionsinrandomqcategory', 'quiz', $questioncount);
895 echo ' ' . $linkcategorycontents;
901 echo '<div class="randomquestioncategorycount">';
907 * Print a given single question in quiz for the reordertool tab of edit.php.
908 * Meant to be used from quiz_print_question_list()
910 * @param object $question A question object from the database questions table
911 * @param object $questionurl The url of the question editing page as a moodle_url object
912 * @param object $quiz The quiz in the context of which the question is being displayed
914 function quiz_print_singlequestion_reordertool($question, $returnurl, $quiz) {
915 echo '<div class="singlequestion ' . $question->qtype
. '">';
916 echo '<label for="s' . $question->id
. '">';
917 echo print_question_icon($question);
918 echo ' ' . quiz_question_tostring($question);
920 echo '<span class="questionpreview">' .
921 quiz_question_action_icons($quiz, $quiz->cmid
, $question, $returnurl) . '</span>';
926 * Print a given random question in quiz for the reordertool tab of edit.php.
927 * Meant to be used from quiz_print_question_list()
929 * @param object $question A question object from the database questions table
930 * @param object $questionurl The url of the question editing page as a moodle_url object
931 * @param object $quiz The quiz in the context of which the question is being displayed
933 function quiz_print_randomquestion_reordertool($question, $pageurl, $quiz) {
936 // Load the category, and the number of available questions in it.
937 if (!$category = $DB->get_record('question_categories', array('id' => $question->category
))) {
938 echo $OUTPUT->notification('Random question category not found!');
941 $questioncount = count(question_bank
::get_qtype(
942 'random')->get_available_questions_from_category(
943 $category->id
, $question->questiontext
== '1', '0'));
945 $reordercheckboxlabel = '<label for="s' . $question->id
. '">';
946 $reordercheckboxlabelclose = '</label>';
948 echo '<div class="quiz_randomquestion">';
949 echo '<div class="randomquestionfromcategory">';
950 echo $reordercheckboxlabel;
951 echo print_question_icon($question);
952 print_random_option_icon($question);
954 if ($questioncount == 0) {
955 echo '<span class="error">';
956 print_string('empty', 'quiz');
960 print_string('random', 'quiz');
961 echo ": $reordercheckboxlabelclose</div>";
963 echo '<div class="randomquestioncategory">';
964 echo $reordercheckboxlabel . $category->name
. $reordercheckboxlabelclose;
965 echo '<span class="questionpreview">';
966 echo quiz_question_preview_button($quiz, $question, false);
970 echo '<div class="randomquestioncategorycount">';
976 * Print an icon to indicate the 'include subcategories' state of a random question.
977 * @param $question the random question.
979 function print_random_option_icon($question) {
981 if (!empty($question->questiontext
)) {
982 $icon = 'withsubcat';
983 $tooltip = get_string('randomwithsubcat', 'quiz');
986 $tooltip = get_string('randomnosubcat', 'quiz');
988 echo '<img src="' . $OUTPUT->pix_url('i/' . $icon) . '" alt="' .
989 $tooltip . '" title="' . $tooltip . '" class="uihint" />';
993 * Creates a textual representation of a question for display.
995 * @param object $question A question object from the database questions table
996 * @param bool $showicon If true, show the question's icon with the question. False by default.
997 * @param bool $showquestiontext If true (default), show question text after question name.
998 * If false, show only question name.
999 * @param bool $return If true (default), return the output. If false, print it.
1001 function quiz_question_tostring($question, $showicon = false,
1002 $showquestiontext = true, $return = true) {
1005 $result .= '<span class="questionname">';
1007 $result .= print_question_icon($question, true);
1010 $result .= shorten_text(format_string($question->name
), 200) . '</span>';
1011 if ($showquestiontext) {
1012 $questiontext = question_utils
::to_plain_text($question->questiontext
,
1013 $question->questiontextformat
, array('noclean' => true, 'para' => false));
1014 $questiontext = shorten_text($questiontext, 200);
1015 $result .= '<span class="questiontext">';
1016 if (!empty($questiontext)) {
1017 $result .= s($questiontext);
1019 $result .= '<span class="error">';
1020 $result .= get_string('questiontextisempty', 'quiz');
1021 $result .= '</span>';
1023 $result .= '</span>';
1033 * A column type for the add this question to the quiz.
1035 * @copyright 2009 Tim Hunt
1036 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1038 class question_bank_add_to_quiz_action_column
extends question_bank_action_column_base
{
1041 public function init() {
1043 $this->stradd
= get_string('addtoquiz', 'quiz');
1046 public function get_name() {
1047 return 'addtoquizaction';
1050 protected function display_content($question, $rowclasses) {
1051 if (!question_has_capability_on($question, 'use')) {
1054 // For RTL languages: switch right and left arrows.
1055 if (right_to_left()) {
1056 $movearrow = 't/removeright';
1058 $movearrow = 't/moveleft';
1060 $this->print_icon($movearrow, $this->stradd
, $this->qbank
->add_to_quiz_url($question->id
));
1063 public function get_required_fields() {
1064 return array('q.id');
1069 * A column type for the name followed by the start of the question text.
1071 * @copyright 2009 Tim Hunt
1072 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1074 class question_bank_question_name_text_column
extends question_bank_question_name_column
{
1075 public function get_name() {
1076 return 'questionnametext';
1079 protected function display_content($question, $rowclasses) {
1081 $labelfor = $this->label_for($question);
1083 echo '<label for="' . $labelfor . '">';
1085 echo quiz_question_tostring($question, false, true, true);
1092 public function get_required_fields() {
1093 $fields = parent
::get_required_fields();
1094 $fields[] = 'q.questiontext';
1095 $fields[] = 'q.questiontextformat';
1101 * Subclass to customise the view of the question bank for the quiz editing screen.
1103 * @copyright 2009 Tim Hunt
1104 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1106 class quiz_question_bank_view
extends question_bank_view
{
1107 protected $quizhasattempts = false;
1108 /** @var object the quiz settings. */
1109 protected $quiz = false;
1110 /** @var int The maximum displayed length of the category info. */
1111 const MAX_TEXT_LENGTH
= 200;
1115 * @param question_edit_contexts $contexts
1116 * @param moodle_url $pageurl
1117 * @param object $course course settings
1118 * @param object $cm activity settings.
1119 * @param object $quiz quiz settings.
1121 public function __construct($contexts, $pageurl, $course, $cm, $quiz) {
1122 parent
::__construct($contexts, $pageurl, $course, $cm);
1123 $this->quiz
= $quiz;
1126 protected function known_field_types() {
1127 $types = parent
::known_field_types();
1128 $types[] = new question_bank_add_to_quiz_action_column($this);
1129 $types[] = new question_bank_question_name_text_column($this);
1133 protected function wanted_columns() {
1134 return array('addtoquizaction', 'checkbox', 'qtype', 'questionnametext',
1135 'editaction', 'copyaction', 'previewaction');
1139 * Specify the column heading
1141 * @return string Column name for the heading
1143 protected function heading_column() {
1144 return 'questionnametext';
1147 protected function default_sort() {
1148 $this->requiredcolumns
['qtype'] = $this->knowncolumntypes
['qtype'];
1149 $this->requiredcolumns
['questionnametext'] = $this->knowncolumntypes
['questionnametext'];
1150 return array('qtype' => 1, 'questionnametext' => 1);
1154 * Let the question bank display know whether the quiz has been attempted,
1155 * hence whether some bits of UI, like the add this question to the quiz icon,
1156 * should be displayed.
1157 * @param bool $quizhasattempts whether the quiz has attempts.
1159 public function set_quiz_has_attempts($quizhasattempts) {
1160 $this->quizhasattempts
= $quizhasattempts;
1161 if ($quizhasattempts && isset($this->visiblecolumns
['addtoquizaction'])) {
1162 unset($this->visiblecolumns
['addtoquizaction']);
1166 public function preview_question_url($question) {
1167 return quiz_question_preview_url($this->quiz
, $question);
1170 public function add_to_quiz_url($questionid) {
1172 $params = $this->baseurl
->params();
1173 $params['addquestion'] = $questionid;
1174 $params['sesskey'] = sesskey();
1175 return new moodle_url('/mod/quiz/edit.php', $params);
1178 public function display($tabname, $page, $perpage, $cat,
1179 $recurse, $showhidden, $showquestiontext) {
1181 if ($this->process_actions_needing_ui()) {
1185 $editcontexts = $this->contexts
->having_one_edit_tab_cap($tabname);
1186 array_unshift($this->searchconditions
,
1187 new \core_question\bank\search\
hidden_condition(!$showhidden));
1188 array_unshift($this->searchconditions
,
1189 new \core_question\bank\search\
category_condition($cat, $recurse,
1190 $editcontexts, $this->baseurl
, $this->course
, self
::MAX_TEXT_LENGTH
));
1192 echo $OUTPUT->box_start('generalbox questionbank');
1193 $this->display_options_form($showquestiontext);
1195 // Continues with list of questions.
1196 $this->display_question_list($this->contexts
->having_one_edit_tab_cap($tabname),
1197 $this->baseurl
, $cat, $this->cm
, $recurse, $page,
1198 $perpage, $showhidden, $showquestiontext,
1199 $this->contexts
->having_cap('moodle/question:add'));
1201 echo $OUTPUT->box_end();
1205 * prints a form to choose categories
1206 * @param string $categoryandcontext 'categoryID,contextID'.
1207 * @deprecated since Moodle 2.6 MDL-40313.
1208 * @see \core_question\bank\search\category_condition
1209 * @todo MDL-41978 This will be deleted in Moodle 2.8
1211 protected function print_choose_category_message($categoryandcontext) {
1213 debugging('print_choose_category_message() is deprecated, ' .
1214 'please use \core_question\bank\search\category_condition instead.', DEBUG_DEVELOPER
);
1215 echo $OUTPUT->box_start('generalbox questionbank');
1216 $this->display_category_form($this->contexts
->having_one_edit_tab_cap('edit'),
1217 $this->baseurl
, $categoryandcontext);
1218 echo "<p style=\"text-align:center;\"><b>";
1219 print_string('selectcategoryabove', 'question');
1221 echo $OUTPUT->box_end();
1224 protected function display_options_form($showquestiontext, $scriptpath = '/mod/quiz/edit.php',
1225 $showtextoption = false) {
1226 // Overridden just to change the default values of the arguments.
1227 parent
::display_options_form($showquestiontext, $scriptpath, $showtextoption);
1230 protected function print_category_info($category) {
1231 $formatoptions = new stdClass();
1232 $formatoptions->noclean
= true;
1233 $strcategory = get_string('category', 'quiz');
1234 echo '<div class="categoryinfo"><div class="categorynamefieldcontainer">' .
1236 echo ': <span class="categorynamefield">';
1237 echo shorten_text(strip_tags(format_string($category->name
)), 60);
1238 echo '</span></div><div class="categoryinfofieldcontainer">' .
1239 '<span class="categoryinfofield">';
1240 echo shorten_text(strip_tags(format_text($category->info
, $category->infoformat
,
1241 $formatoptions, $this->course
->id
)), 200);
1242 echo '</span></div></div>';
1245 protected function display_options($recurse, $showhidden, $showquestiontext) {
1246 debugging('display_options() is deprecated, see display_options_form() instead.', DEBUG_DEVELOPER
);
1247 echo '<form method="get" action="edit.php" id="displayoptions">';
1248 echo "<fieldset class='invisiblefieldset'>";
1249 echo html_writer
::input_hidden_params($this->baseurl
,
1250 array('recurse', 'showhidden', 'qbshowtext'));
1251 $this->display_category_form_checkbox('recurse', $recurse,
1252 get_string('includesubcategories', 'question'));
1253 $this->display_category_form_checkbox('showhidden', $showhidden,
1254 get_string('showhidden', 'question'));
1255 echo '<noscript><div class="centerpara"><input type="submit" value="' .
1256 get_string('go') . '" />';
1257 echo '</div></noscript></fieldset></form>';
1262 * Prints the form for setting a quiz' overall grade
1264 * @param object $quiz The quiz object of the quiz in question
1265 * @param object $pageurl The url of the current page with the parameters required
1266 * for links returning to the current page, as a moodle_url object
1267 * @param int $tabindex The tabindex to start from for the form elements created
1268 * @return int The tabindex from which the calling page can continue, that is,
1269 * the last value used +1.
1271 function quiz_print_grading_form($quiz, $pageurl, $tabindex) {
1273 $strsave = get_string('save', 'quiz');
1274 echo '<form method="post" action="edit.php" class="quizsavegradesform"><div>';
1275 echo '<fieldset class="invisiblefieldset" style="display: block;">';
1276 echo "<input type=\"hidden\" name=\"sesskey\" value=\"" . sesskey() . "\" />";
1277 echo html_writer
::input_hidden_params($pageurl);
1278 $a = '<input type="text" id="inputmaxgrade" name="maxgrade" size="' .
1279 ($quiz->decimalpoints +
2) . '" tabindex="' . $tabindex
1280 . '" value="' . quiz_format_grade($quiz, $quiz->grade
) . '" />';
1281 echo '<label for="inputmaxgrade">' . get_string('maximumgradex', '', $a) . "</label>";
1282 echo '<input type="hidden" name="savechanges" value="save" />';
1283 echo '<input type="submit" value="' . $strsave . '" />';
1285 echo "</div></form>\n";
1286 return $tabindex +
1;
1290 * Print the status bar
1292 * @param object $quiz The quiz object of the quiz in question
1294 function quiz_print_status_bar($quiz) {
1299 $bits[] = html_writer
::tag('span',
1300 get_string('totalmarksx', 'quiz', quiz_format_grade($quiz, $quiz->sumgrades
)),
1301 array('class' => 'totalpoints'));
1303 $bits[] = html_writer
::tag('span',
1304 get_string('numquestionsx', 'quiz', $DB->count_records('quiz_slots', array('quizid' => $quiz->id
))),
1305 array('class' => 'numberofquestions'));
1309 // Exact open and close dates for the tool-tip.
1311 if ($quiz->timeopen
> 0) {
1312 if ($timenow > $quiz->timeopen
) {
1313 $dates[] = get_string('quizopenedon', 'quiz', userdate($quiz->timeopen
));
1315 $dates[] = get_string('quizwillopen', 'quiz', userdate($quiz->timeopen
));
1318 if ($quiz->timeclose
> 0) {
1319 if ($timenow > $quiz->timeclose
) {
1320 $dates[] = get_string('quizclosed', 'quiz', userdate($quiz->timeclose
));
1322 $dates[] = get_string('quizcloseson', 'quiz', userdate($quiz->timeclose
));
1325 if (empty($dates)) {
1326 $dates[] = get_string('alwaysavailable', 'quiz');
1328 $tooltip = implode(', ', $dates);
1330 // Brief summary on the page.
1331 if ($timenow < $quiz->timeopen
) {
1332 $currentstatus = get_string('quizisclosedwillopen', 'quiz',
1333 userdate($quiz->timeopen
, get_string('strftimedatetimeshort', 'langconfig')));
1334 } else if ($quiz->timeclose
&& $timenow <= $quiz->timeclose
) {
1335 $currentstatus = get_string('quizisopenwillclose', 'quiz',
1336 userdate($quiz->timeclose
, get_string('strftimedatetimeshort', 'langconfig')));
1337 } else if ($quiz->timeclose
&& $timenow > $quiz->timeclose
) {
1338 $currentstatus = get_string('quizisclosed', 'quiz');
1340 $currentstatus = get_string('quizisopen', 'quiz');
1343 $bits[] = html_writer
::tag('span', $currentstatus,
1344 array('class' => 'quizopeningstatus', 'title' => implode(', ', $dates)));
1346 echo html_writer
::tag('div', implode(' | ', $bits), array('class' => 'statusbar'));