MDL-27631 question bank Show question text in the question list when top level catego...
[moodle.git] / mod / quiz / editlib.php
CommitLineData
83192608 1<?php
f9b0500f
TH
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
f4b879dd 17
ee1fb969 18/**
f9b0500f
TH
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.
f63a4ff2 24 *
ba643847 25 * @package mod
f9b0500f 26 * @subpackage quiz
ba643847
TH
27 * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
f9b0500f
TH
29 */
30
ee1fb969 31
a17b297d 32defined('MOODLE_INTERNAL') || die();
ee1fb969 33
a17b297d 34require_once($CFG->dirroot . '/mod/quiz/locallib.php');
f9b0500f 35
2a874d65 36define('NUM_QS_TO_SHOW_IN_RANDOM', 3);
37
ee1fb969 38/**
4299df1d 39 * Remove a question from a quiz
40 * @param object $quiz the quiz object.
41 * @param int $questionid The id of the question to be deleted.
42 */
43function quiz_remove_question($quiz, $questionid) {
9cf4a18b 44 global $DB;
ee1fb969 45
4299df1d 46 $questionids = explode(',', $quiz->questions);
47 $key = array_search($questionid, $questionids);
48 if ($key === false) {
49 return;
ee1fb969 50 }
51
4299df1d 52 unset($questionids[$key]);
53 $quiz->questions = implode(',', $questionids);
54 $DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
aac80ff3
TH
55 $DB->delete_records('quiz_question_instances',
56 array('quiz' => $quiz->instance, 'question' => $questionid));
4299df1d 57}
58
59/**
60 * Remove an empty page from the quiz layout. If that is not possible, do nothing.
61 * @param string $layout the existinng layout, $quiz->questions.
f7970e3c 62 * @param int $index the position into $layout where the empty page should be removed.
4299df1d 63 * @return the updated layout
64 */
65function quiz_delete_empty_page($layout, $index) {
66 $questionids = explode(',', $layout);
67
68 if ($index < -1 || $index >= count($questionids) - 1) {
69 return $layout;
ee1fb969 70 }
4299df1d 71
72 if (($index >= 0 && $questionids[$index] != 0) || $questionids[$index + 1] != 0) {
73 return $layout; // This was not an empty page.
74 }
75
76 unset($questionids[$index + 1]);
77
78 return implode(',', $questionids);
ee1fb969 79}
80
ee1fb969 81/**
f5831eea 82 * Add a question to a quiz
83 *
84 * Adds a question to a quiz by updating $quiz as well as the
85 * quiz and quiz_question_instances tables. It also adds a page break
86 * if required.
94dbfb3a
TH
87 * @param int $id The id of the question to be added
88 * @param object $quiz The extended quiz object as used by edit.php
89 * This is updated by this function
aac80ff3
TH
90 * @param int $page Which page in quiz to add the question on. If 0 (default),
91 * add at the end
f7970e3c 92 * @return bool false if the question was already in the quiz
f5831eea 93 */
94dbfb3a 94function quiz_add_quiz_question($id, $quiz, $page = 0) {
9cf4a18b 95 global $DB;
78634f1e 96 $questions = explode(',', quiz_clean_layout($quiz->questions));
ee1fb969 97 if (in_array($id, $questions)) {
98 return false;
99 }
100
101 // remove ending page break if it is not needed
102 if ($breaks = array_keys($questions, 0)) {
103 // determine location of the last two page breaks
104 $end = end($breaks);
105 $last = prev($breaks);
106 $last = $last ? $last : -1;
78634f1e 107 if (!$quiz->questionsperpage || (($end - $last - 1) < $quiz->questionsperpage)) {
ee1fb969 108 array_pop($questions);
109 }
110 }
f5831eea 111 if (is_int($page) && $page >= 1) {
112 $numofpages = quiz_number_of_pages($quiz->questions);
113 if ($numofpages<$page) {
fa583f5f 114 //the page specified does not exist in quiz
f5831eea 115 $page = 0;
116 } else {
fa583f5f 117 // add ending page break - the following logic requires doing
118 //this at this point
119 $questions[] = 0;
f5831eea 120 $currentpage = 1;
121 $addnow = false;
122 foreach ($questions as $question) {
123 if ($question == 0) {
fa583f5f 124 $currentpage++;
125 //The current page is the one after the one we want to add on,
126 //so we add the question before adding the current page.
f5831eea 127 if ($currentpage == $page + 1) {
128 $questions_new[] = $id;
fa583f5f 129 }
130 }
f5831eea 131 $questions_new[] = $question;
fa583f5f 132 }
f5831eea 133 $questions = $questions_new;
fa583f5f 134 }
135 }
f5831eea 136 if ($page == 0) {
fa583f5f 137 // add question
138 $questions[] = $id;
139 // add ending page break
140 $questions[] = 0;
141 }
ee1fb969 142
143 // Save new questionslist in database
f5831eea 144 $quiz->questions = implode(',', $questions);
f685e830 145 $DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
ee1fb969 146
f9b0500f 147 // Add the new question instance.
0ff4bd08 148 $instance = new stdClass();
f9b0500f
TH
149 $instance->quiz = $quiz->id;
150 $instance->question = $id;
151 $instance->grade = $DB->get_field('question', 'defaultmark', array('id' => $id));
152 $DB->insert_record('quiz_question_instances', $instance);
ee1fb969 153}
154
aac80ff3
TH
155function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number,
156 $includesubcategories) {
f9b0500f 157 global $DB;
94dbfb3a
TH
158
159 $category = $DB->get_record('question_categories', array('id' => $categoryid));
160 if (!$category) {
161 print_error('invalidcategoryid', 'error');
162 }
163
164 $catcontext = get_context_instance_by_id($category->contextid);
165 require_capability('moodle/question:useall', $catcontext);
166
167 // Find existing random questions in this category that are
168 // not used by any quiz.
169 if ($existingquestions = $DB->get_records_sql(
25a03faa 170 "SELECT q.id, q.qtype FROM {question} q
f9b0500f 171 WHERE qtype = 'random'
94dbfb3a
TH
172 AND category = ?
173 AND " . $DB->sql_compare_text('questiontext') . " = ?
aac80ff3
TH
174 AND NOT EXISTS (
175 SELECT *
176 FROM {quiz_question_instances}
177 WHERE question = q.id)
94dbfb3a
TH
178 ORDER BY id", array($category->id, $includesubcategories))) {
179 // Take as many of these as needed.
180 while (($existingquestion = array_shift($existingquestions)) && $number > 0) {
181 quiz_add_quiz_question($existingquestion->id, $quiz, $addonpage);
182 $number -= 1;
183 }
184 }
185
186 if ($number <= 0) {
187 return;
188 }
189
190 // More random questions are needed, create them.
94dbfb3a 191 for ($i = 0; $i < $number; $i += 1) {
fd214b59
TH
192 $form = new stdClass();
193 $form->questiontext = array('text' => $includesubcategories, 'format' => 0);
94dbfb3a 194 $form->category = $category->id . ',' . $category->contextid;
fd214b59
TH
195 $form->defaultmark = 1;
196 $form->hidden = 1;
94dbfb3a 197 $form->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
0ff4bd08 198 $question = new stdClass();
f9b0500f
TH
199 $question->qtype = 'random';
200 $question = question_bank::get_qtype('random')->save_question($question, $form);
94dbfb3a
TH
201 if (!isset($question->id)) {
202 print_error('cannotinsertrandomquestion', 'quiz');
203 }
204 quiz_add_quiz_question($question->id, $quiz, $addonpage);
205 }
206}
207
4299df1d 208/**
209 * Add a page break after at particular position$.
210 * @param string $layout the existinng layout, $quiz->questions.
f7970e3c 211 * @param int $index the position into $layout where the empty page should be removed.
4299df1d 212 * @return the updated layout
213 */
214function quiz_add_page_break_at($layout, $index) {
215 $questionids = explode(',', $layout);
216 if ($index < 0 || $index >= count($questionids)) {
217 return $layout;
218 }
219
220 array_splice($questionids, $index, 0, '0');
221
222 return implode(',', $questionids);
223}
224
225/**
226 * Add a page break after a particular question.
227 * @param string $layout the existinng layout, $quiz->questions.
f7970e3c 228 * @param int $qustionid the question to add the page break after.
4299df1d 229 * @return the updated layout
230 */
231function quiz_add_page_break_after($layout, $questionid) {
232 $questionids = explode(',', $layout);
233 $key = array_search($questionid, $questionids);
234 if ($key === false || !$questionid) {
235 return $layout;
236 }
237
238 array_splice($questionids, $key + 1, 0, '0');
239
240 return implode(',', $questionids);
241}
242
243/**
244 * Update the database after $quiz->questions has been changed. For example,
245 * this deletes preview attempts and updates $quiz->sumgrades.
246 * @param $quiz the quiz object.
247 */
248function quiz_save_new_layout($quiz) {
249 global $DB;
250 $DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
251 quiz_update_sumgrades($quiz);
252 quiz_delete_previews($quiz);
253}
254
ee1fb969 255/**
f5831eea 256 * Save changes to question instance
257 *
258 * Saves changes to the question grades in the quiz_question_instances table.
259 * It does not update 'sumgrades' in the quiz table.
f9b0500f 260 *
f7970e3c
TH
261 * @param int grade The maximal grade for the question
262 * @param int $questionid The id of the question
263 * @param int $quizid The id of the quiz to update / add the instances for.
f5831eea 264 */
f9b0500f 265function quiz_update_question_instance($grade, $questionid, $quiz) {
fd214b59
TH
266 global $DB;
267 $instance = $DB->get_record('quiz_question_instances', array('quiz' => $quiz->id,
f9b0500f
TH
268 'question' => $questionid));
269 $slot = quiz_get_slot_for_question($quiz, $questionid);
270
271 if (!$instance || !$slot) {
272 throw new coding_exception('Attempt to change the grade of a quesion not in the quiz.');
ee1fb969 273 }
f9b0500f
TH
274
275 if (abs($grade - $instance->grade) < 1e-7) {
276 // Grade has not changed. Nothing to do.
277 return;
278 }
279
280 $instance->grade = $grade;
281 $DB->update_record('quiz_question_instances', $instance);
6b5f24d3 282 question_engine::set_max_mark_in_attempts(new qubaids_for_quiz($quiz->id),
f9b0500f 283 $slot, $grade);
ee1fb969 284}
285
4299df1d 286// Private function used by the following two.
287function _quiz_move_question($layout, $questionid, $shift) {
288 if (!$questionid || !($shift == 1 || $shift == -1)) {
289 return $layout;
290 }
291
292 $questionids = explode(',', $layout);
293 $key = array_search($questionid, $questionids);
294 if ($key === false) {
295 return $layout;
296 }
297
298 $otherkey = $key + $shift;
299 if ($otherkey < 0 || $otherkey >= count($questionids) - 1) {
300 return $layout;
301 }
302
303 $temp = $questionids[$otherkey];
304 $questionids[$otherkey] = $questionids[$key];
305 $questionids[$key] = $temp;
306
307 return implode(',', $questionids);
308}
309
310/**
311 * Move a particular question one space earlier in the $quiz->questions list.
312 * If that is not possible, do nothing.
313 * @param string $layout the existinng layout, $quiz->questions.
f7970e3c 314 * @param int $questionid the id of a question.
4299df1d 315 * @return the updated layout
316 */
317function quiz_move_question_up($layout, $questionid) {
318 return _quiz_move_question($layout, $questionid, -1);
319}
320
321/**
322 * Move a particular question one space later in the $quiz->questions list.
323 * If that is not possible, do nothing.
324 * @param string $layout the existinng layout, $quiz->questions.
f7970e3c 325 * @param int $questionid the id of a question.
4299df1d 326 * @return the updated layout
327 */
328function quiz_move_question_down($layout, $questionid) {
329 return _quiz_move_question($layout, $questionid, +1);
330}
331
ee1fb969 332/**
f5831eea 333 * Prints a list of quiz questions for the edit.php main view for edit
334 * ($reordertool = false) and order and paging ($reordertool = true) tabs
335 *
336 * @return int sum of maximum grades
337 * @param object $quiz This is not the standard quiz object used elsewhere but
338 * it contains the quiz layout in $quiz->questions and the grades in
339 * $quiz->grades
340 * @param object $pageurl The url of the current page with the parameters required
341 * for links returning to the current page, as a moodle_url object
f7970e3c
TH
342 * @param bool $allowdelete Indicates whether the delete icons should be displayed
343 * @param bool $reordertool Indicates whether the reorder tool should be displayed
344 * @param bool $quiz_qbanktool Indicates whether the question bank should be displayed
345 * @param bool $hasattempts Indicates whether the quiz has attempts
f5831eea 346 */
f2956c98
TH
347function quiz_print_question_list($quiz, $pageurl, $allowdelete, $reordertool,
348 $quiz_qbanktool, $hasattempts, $defaultcategoryobj) {
f9b0500f 349 global $USER, $CFG, $DB, $OUTPUT;
f5831eea 350 $strorder = get_string('order');
351 $strquestionname = get_string('questionname', 'quiz');
352 $strgrade = get_string('grade');
ee1fb969 353 $strremove = get_string('remove', 'quiz');
f5831eea 354 $stredit = get_string('edit');
355 $strview = get_string('view');
356 $straction = get_string('action');
357 $strmove = get_string('move');
358 $strmoveup = get_string('moveup');
359 $strmovedown = get_string('movedown');
360 $strsave = get_string('save', 'quiz');
361 $strreorderquestions = get_string('reorderquestions', 'quiz');
362
363 $strselectall = get_string('selectall', 'quiz');
364 $strselectnone = get_string('selectnone', 'quiz');
365 $strtype = get_string('type', 'quiz');
366 $strpreview = get_string('preview', 'quiz');
ee1fb969 367
477c217f 368 if ($quiz->questions) {
fa583f5f 369 list($usql, $params) = $DB->get_in_or_equal(explode(',', $quiz->questions));
f9b0500f
TH
370 $params[] = $quiz->id;
371 $questions = $DB->get_records_sql("SELECT q.*, qc.contextid, qqi.grade as maxmark
372 FROM {question} q
373 JOIN {question_categories} qc ON qc.id = q.category
374 JOIN {quiz_question_instances} qqi ON qqi.question = q.id
375 WHERE q.id $usql AND qqi.quiz = ?", $params);
477c217f 376 } else {
377 $questions = array();
ee1fb969 378 }
379
f5831eea 380 $layout = quiz_clean_layout($quiz->questions);
fa583f5f 381 $order = explode(',', $layout);
f5831eea 382 $lastindex = count($order) - 1;
fa583f5f 383
3e10e429 384 $disabled = '';
385 $pagingdisabled = '';
477c217f 386 if ($hasattempts) {
f5831eea 387 $disabled = 'disabled="disabled"';
fa583f5f 388 }
3e10e429 389 if ($hasattempts || $quiz->shufflequestions) {
f5831eea 390 $pagingdisabled = 'disabled="disabled"';
ee68029a 391 }
ee1fb969 392
f5831eea 393 $reordercontrolssetdefaultsubmit = '<div style="display:none;">' .
394 '<input type="submit" name="savechanges" value="' .
3e10e429 395 $strreorderquestions . '" ' . $pagingdisabled . ' /></div>';
f5831eea 396 $reordercontrols1 = '<div class="addnewpagesafterselected">' .
397 '<input type="submit" name="addnewpagesafterselected" value="' .
398 get_string('addnewpagesafterselected', 'quiz') . '" ' .
399 $pagingdisabled . ' /></div>';
7b161e12 400 $reordercontrols1 .= '<div class="quizdeleteselected">' .
401 '<input type="submit" name="quizdeleteselected" ' .
402 'onclick="return confirm(\'' .
403 get_string('areyousureremoveselected', 'quiz') . '\');" value="' .
404 get_string('removeselected', 'quiz') . '" ' . $disabled . ' /></div>';
fa583f5f 405
f5831eea 406 $a = '<input name="moveselectedonpagetop" type="text" size="2" ' .
407 $pagingdisabled . ' />';
408
409 $reordercontrols2top = '<div class="moveselectedonpage">' .
410 get_string('moveselectedonpage', 'quiz', $a) .
411 '<input type="submit" name="savechanges" value="' .
412 $strmove . '" ' . $pagingdisabled . ' />' . '
413 <br /><input type="submit" name="savechanges" value="' .
3e10e429 414 $strreorderquestions . '" /></div>';
f5831eea 415 $reordercontrols2bottom = '<div class="moveselectedonpage">' .
416 '<input type="submit" name="savechanges" value="' .
3e10e429 417 $strreorderquestions . '" /><br />' .
f5831eea 418 get_string('moveselectedonpage', 'quiz', $a) .
419 '<input type="submit" name="savechanges" value="' .
420 $strmove . '" ' . $pagingdisabled . ' /> ' . '</div>';
421
422 $reordercontrols3 = '<a href="javascript:select_all_in(\'FORM\', null, ' .
423 '\'quizquestions\');">' .
424 $strselectall . '</a> /';
425 $reordercontrols3.= ' <a href="javascript:deselect_all_in(\'FORM\', ' .
426 'null, \'quizquestions\');">' .
427 $strselectnone . '</a>';
428
429 $reordercontrolstop = '<div class="reordercontrols">' .
430 $reordercontrolssetdefaultsubmit .
431 $reordercontrols1 . $reordercontrols2top . $reordercontrols3 . "</div>";
432 $reordercontrolsbottom = '<div class="reordercontrols">' .
433 $reordercontrolssetdefaultsubmit .
434 $reordercontrols2bottom . $reordercontrols1 . $reordercontrols3 . "</div>";
435
436 if ($reordertool) {
fa583f5f 437 echo '<form method="post" action="edit.php" id="quizquestions"><div>';
438
6ea66ff3 439 echo html_writer::input_hidden_params($pageurl);
f5831eea 440 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
fa583f5f 441
442 echo $reordercontrolstop;
9cf4a18b 443 }
9aa82ed6 444
fa583f5f 445 //the current question ordinal (no descriptions)
446 $qno = 1;
447 //the current question (includes questions and descriptions)
f5831eea 448 $questioncount = 0;
fa583f5f 449 //the current page number in iteration
450 $pagecount = 0;
451
f5831eea 452 $pageopen = false;
fa583f5f 453
fb6dcdab 454 $returnurl = str_replace($CFG->wwwroot, '', $pageurl->out(false));
f5831eea 455 $questiontotalcount = count($order);
fa583f5f 456
f9b0500f 457 foreach ($order as $count => $qnum) {
ee1fb969 458
f5831eea 459 $reordercheckbox = '';
460 $reordercheckboxlabel = '';
461 $reordercheckboxlabelclose = '';
fa583f5f 462
f5831eea 463 if ($qnum && empty($questions[$qnum])) {
14afbbf0 464 continue;
465 }
f9b0500f 466
4eda4eec 467 // If the questiontype is missing change the question type
f9b0500f
TH
468 if ($qnum && !array_key_exists($qnum, $questions)) {
469 $fakequestion = new stdClass();
470 $fakequestion->id = 0;
471 $fakequestion->qtype = 'missingtype';
472 $fakequestion->name = get_string('deletedquestion', 'qtype_missingtype');
aac80ff3
TH
473 $fakequestion->questiontext = '<p>' .
474 get_string('deletedquestion', 'qtype_missing') . '</p>';
f9b0500f
TH
475 $fakequestion->length = 0;
476 $questions[$qnum] = $fakequestion;
477 $quiz->grades[$qnum] = 0;
478
fd214b59 479 } else if ($qnum && !question_bank::qtype_exists($questions[$qnum]->qtype)) {
4eda4eec 480 $questions[$qnum]->qtype = 'missingtype';
481 }
f9b0500f 482
4299df1d 483 if ($qnum != 0 || ($qnum == 0 && !$pageopen)) {
fa583f5f 484 //this is either a question or a page break after another
485 // (no page is currently open)
4299df1d 486 if (!$pageopen) {
fa583f5f 487 //if no page is open, start display of a page
488 $pagecount++;
f5831eea 489 echo '<div class="quizpage"><span class="pagetitle">' .
490 get_string('page') . '&nbsp;' . $pagecount .
fa583f5f 491 '</span><div class="pagecontent">';
4299df1d 492 $pageopen = true;
fa583f5f 493 }
f9b0500f 494 if ($qnum == 0 && $count < $questiontotalcount) {
4299df1d 495 // This is the second successive page break. Tell the user the page is empty.
fa583f5f 496 echo '<div class="pagestatus">';
f5831eea 497 print_string('noquestionsonpage', 'quiz');
fa583f5f 498 echo '</div>';
5be23736 499 if ($allowdelete) {
fa583f5f 500 echo '<div class="quizpagedelete">';
aac80ff3
TH
501 echo $OUTPUT->action_icon($pageurl->out(true,
502 array('deleteemptypage' => $count - 1, 'sesskey'=>sesskey())),
fd214b59 503 new pix_icon('t/delete', $strremove),
aac80ff3
TH
504 new component_action('click',
505 'M.core_scroll_manager.save_scroll_action'),
fd214b59 506 array('title' => $strremove));
fa583f5f 507 echo '</div>';
508 }
509 }
510
5be23736 511 if ($qnum != 0) {
fa583f5f 512 $question = $questions[$qnum];
fb6dcdab
TH
513 $questionparams = array(
514 'returnurl' => $returnurl,
515 'cmid' => $quiz->cmid,
516 'id' => $question->id);
517 $questionurl = new moodle_url('/question/question.php',
fa583f5f 518 $questionparams);
519 $questioncount++;
520 //this is an actual question
4eda4eec 521
fa583f5f 522 /* Display question start */
aac80ff3 523 ?>
fa583f5f 524<div class="question">
944efb3e 525 <div class="questioncontainer <?php echo $question->qtype; ?>">
fa583f5f 526 <div class="qnum">
aac80ff3
TH
527 <?php
528 $reordercheckbox = '';
529 $reordercheckboxlabel = '';
530 $reordercheckboxlabelclose = '';
531 if ($reordertool) {
532 $reordercheckbox = '<input type="checkbox" name="s' . $question->id .
533 '" id="s' . $question->id . '" />';
534 $reordercheckboxlabel = '<label for="s' . $question->id . '">';
535 $reordercheckboxlabelclose = '</label>';
536 }
8418e5b7
WO
537 if ($question->length == 0) {
538 $qnodisplay = get_string('infoshort', 'quiz');
539 } else if ($quiz->shufflequestions) {
540 $qnodisplay = '?';
541 } else {
542 if ($qno > 999 || ($reordertool && $qno > 99)) {
543 $qnodisplay = html_writer::tag('small', $qno);
aac80ff3 544 } else {
8418e5b7 545 $qnodisplay = $qno;
aac80ff3 546 }
aac80ff3 547 $qno += $question->length;
fa583f5f 548 }
8418e5b7
WO
549 echo $reordercheckboxlabel . $qnodisplay . $reordercheckboxlabelclose .
550 $reordercheckbox;
fa583f5f 551
aac80ff3 552 ?>
fa583f5f 553 </div>
554 <div class="content">
555 <div class="questioncontrols">
556 <?php
aac80ff3
TH
557 if ($count != 0) {
558 if (!$hasattempts) {
559 $upbuttonclass = '';
560 if ($count >= $lastindex - 1) {
561 $upbuttonclass = 'upwithoutdown';
562 }
563 echo $OUTPUT->action_icon($pageurl->out(true,
564 array('up' => $question->id, 'sesskey'=>sesskey())),
565 new pix_icon('t/up', $strmoveup),
566 new component_action('click',
567 'M.core_scroll_manager.save_scroll_action'),
568 array('title' => $strmoveup));
fa583f5f 569 }
ee1fb969 570
ee1fb969 571 }
aac80ff3
TH
572 if ($count < $lastindex - 1) {
573 if (!$hasattempts) {
574 echo $OUTPUT->action_icon($pageurl->out(true,
575 array('down' => $question->id, 'sesskey'=>sesskey())),
576 new pix_icon('t/down', $strmovedown),
577 new component_action('click',
578 'M.core_scroll_manager.save_scroll_action'),
579 array('title' => $strmovedown));
580 }
581 }
582 if ($allowdelete && (empty($question->id) ||
583 question_has_capability_on($question, 'use', $question->category))) {
584 // remove from quiz, not question delete.
585 if (!$hasattempts) {
586 echo $OUTPUT->action_icon($pageurl->out(true,
587 array('remove' => $question->id, 'sesskey'=>sesskey())),
588 new pix_icon('t/delete', $strremove),
589 new component_action('click',
590 'M.core_scroll_manager.save_scroll_action'),
591 array('title' => $strremove));
592 }
fa583f5f 593 }
fa583f5f 594 ?>
595 </div><?php
aac80ff3
TH
596 if ($question->qtype != 'description' && !$reordertool) {
597 ?>
fa583f5f 598<div class="points">
fd214b59 599<form method="post" action="edit.php" class="quizsavegradesform"><div>
fa583f5f 600 <fieldset class="invisiblefieldset" style="display: block;">
3e10e429 601 <label for="<?php echo "inputq$question->id" ?>"><?php echo $strgrade; ?></label>:<br />
973d2660 602 <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
6ea66ff3 603 <?php echo html_writer::input_hidden_params($pageurl); ?>
fa583f5f 604 <input type="hidden" name="savechanges" value="save" />
aac80ff3
TH
605 <?php
606 echo '<input type="text" name="g' . $question->id .
607 '" id="inputq' . $question->id .
608 '" size="' . ($quiz->decimalpoints + 2) .
609 '" value="' . (0 + $quiz->grades[$qnum]) .
610 '" tabindex="' . ($lastindex + $qno) . '" />';
611 ?>
3fd9edf0 612 <input type="submit" class="pointssubmitbutton" value="<?php echo $strsave; ?>" />
fa583f5f 613 </fieldset>
aac80ff3
TH
614 <?php
615 if ($question->qtype == 'random') {
616 echo '<a href="' . $questionurl->out() .
617 '" class="configurerandomquestion">' .
618 get_string("configurerandomquestion", "quiz") . '</a>';
619 }
f4b879dd 620
aac80ff3 621 ?>
fa583f5f 622</div>
623</form>
624
625 </div>
aac80ff3
TH
626 <?php
627 } else if ($reordertool) {
628 if ($qnum) {
629 ?>
fa583f5f 630<div class="qorder">
aac80ff3
TH
631 <?php
632 echo '<input type="text" name="o' . $question->id .
633 '" size="2" value="' . (10*$count + 10) .
634 '" tabindex="' . ($lastindex + $qno) . '" />';
635 ?>
fa583f5f 636</div>
aac80ff3
TH
637 <?php
638 }
fa583f5f 639 }
aac80ff3 640 ?>
fa583f5f 641 <div class="questioncontentcontainer">
aac80ff3
TH
642 <?php
643 if ($question->qtype == 'random') { // it is a random question
644 if (!$reordertool) {
645 quiz_print_randomquestion($question, $pageurl, $quiz, $quiz_qbanktool);
646 } else {
647 quiz_print_randomquestion_reordertool($question, $pageurl, $quiz);
648 }
649 } else { // it is a single question
650 if (!$reordertool) {
651 quiz_print_singlequestion($question, $returnurl, $quiz);
652 } else {
653 quiz_print_singlequestion_reordertool($question, $returnurl, $quiz);
654 }
fa583f5f 655 }
fa583f5f 656 ?>
657 </div>
658 </div>
659 </div>
660</div>
661
aac80ff3 662 <?php
fa583f5f 663 }
ee1fb969 664 }
fa583f5f 665 //a page break: end the existing page.
f5831eea 666 if ($qnum == 0) {
667 if ($pageopen) {
aac80ff3
TH
668 if (!$reordertool && !($quiz->shufflequestions &&
669 $count < $questiontotalcount - 1)) {
fa583f5f 670 quiz_print_pagecontrols($quiz, $pageurl, $pagecount,
f2956c98 671 $hasattempts, $defaultcategoryobj);
f9b0500f 672 } else if ($count < $questiontotalcount - 1) {
fa583f5f 673 //do not include the last page break for reordering
674 //to avoid creating a new extra page in the end
3e10e429 675 echo '<input type="hidden" name="opg' . $pagecount . '" size="2" value="' .
f5831eea 676 (10*$count + 10) . '" />';
fa583f5f 677 }
678 echo "</div></div>";
679
3e10e429 680 if (!$reordertool && !$quiz->shufflequestions) {
39e37019 681 echo $OUTPUT->container_start('addpage');
aac80ff3
TH
682 $url = new moodle_url($pageurl->out_omit_querystring(),
683 array('cmid' => $quiz->cmid, 'courseid' => $quiz->course,
684 'addpage' => $count, 'sesskey' => sesskey()));
fd214b59
TH
685 echo $OUTPUT->single_button($url, get_string('addpagehere', 'quiz'), 'post',
686 array('disabled' => $hasattempts,
aac80ff3
TH
687 'actions' => array(new component_action('click',
688 'M.core_scroll_manager.save_scroll_action'))));
39e37019 689 echo $OUTPUT->container_end();
fa583f5f 690 }
f5831eea 691 $pageopen = false;
fa583f5f 692 $count++;
693 }
ee1fb969 694 }
ee1fb969 695
fa583f5f 696 }
f5831eea 697 if ($reordertool) {
fa583f5f 698 echo $reordercontrolsbottom;
699 echo '</div></form>';
700 }
fa583f5f 701}
702
703/**
704 * Print all the controls for adding questions directly into the
705 * specific page in the edit tab of edit.php
706 *
707 * @param unknown_type $quiz
708 * @param unknown_type $pageurl
709 * @param unknown_type $page
710 * @param unknown_type $hasattempts
711 */
f2956c98 712function quiz_print_pagecontrols($quiz, $pageurl, $page, $hasattempts, $defaultcategoryobj) {
825116fb 713 global $CFG, $OUTPUT;
3e10e429 714 static $randombuttoncount = 0;
715 $randombuttoncount++;
fa583f5f 716 echo '<div class="pagecontrols">';
cd120b23 717
718 // Get the current context
fa583f5f 719 $thiscontext = get_context_instance(CONTEXT_COURSE, $quiz->course);
720 $contexts = new question_edit_contexts($thiscontext);
cd120b23 721
722 // Get the default category.
94dbfb3a 723 list($defaultcategoryid) = explode(',', $pageurl->param('cat'));
f2956c98
TH
724 if (empty($defaultcategoryid)) {
725 $defaultcategoryid = $defaultcategoryobj->id;
726 }
cd120b23 727
728 // Create the url the question page will return to
90c7912e 729 $returnurladdtoquiz = new moodle_url($pageurl, array('addonpage' => $page));
cd120b23 730
731 // Print a button linking to the choose question type page.
fb6dcdab 732 $returnurladdtoquiz = str_replace($CFG->wwwroot, '', $returnurladdtoquiz->out(false));
90c7912e 733 $newquestionparams = array('returnurl' => $returnurladdtoquiz,
cd120b23 734 'cmid' => $quiz->cmid, 'appendqnumstring' => 'addquestion');
aac80ff3
TH
735 create_new_question_button($defaultcategoryid, $newquestionparams,
736 get_string('addaquestion', 'quiz'),
cd120b23 737 get_string('createquestionandadd', 'quiz'), $hasattempts);
738
fa583f5f 739 if ($hasattempts) {
740 $disabled = 'disabled="disabled"';
741 } else {
742 $disabled = '';
743 }
fa583f5f 744 ?>
fa583f5f 745 <div class="singlebutton">
aac80ff3
TH
746 <form class="randomquestionform" action="<?php echo $CFG->wwwroot;
747 ?>/mod/quiz/addrandom.php" method="get">
fa583f5f 748 <div>
aac80ff3
TH
749 <input type="hidden" class="addonpage_formelement" name="addonpage" value="<?php
750 echo $page; ?>" />
fa583f5f 751 <input type="hidden" name="cmid" value="<?php echo $quiz->cmid; ?>" />
752 <input type="hidden" name="courseid" value="<?php echo $quiz->course; ?>" />
aac80ff3
TH
753 <input type="hidden" name="category" value="<?php
754 echo $pageurl->param('cat'); ?>" />
755 <input type="hidden" name="returnurl" value="<?php
756 echo s(str_replace($CFG->wwwroot, '', $pageurl->out(false))); ?>" />
757 <input type="submit" id="addrandomdialoglaunch_<?php
758 echo $randombuttoncount; ?>" value="<?php
759 echo get_string('addarandomquestion', 'quiz'); ?>" <?php
760 echo " $disabled"; ?> />
fa583f5f 761 </div>
762 </form>
763 </div>
7292c11f 764 <?php echo $OUTPUT->help_icon('addarandomquestion', 'quiz'); ?>
fa583f5f 765 <?php
766 echo "\n</div>";
767}
fa583f5f 768
fa583f5f 769/**
770 * Print a given single question in quiz for the edit tab of edit.php.
771 * Meant to be used from quiz_print_question_list()
f5831eea 772 *
414d5bfb 773 * @param object $question A question object from the database questions table
2a874d65 774 * @param object $returnurl The url to get back to this page, for example after editing.
414d5bfb 775 * @param object $quiz The quiz in the context of which the question is being displayed
fa583f5f 776 */
f5831eea 777function quiz_print_singlequestion($question, $returnurl, $quiz) {
2a874d65 778 echo '<div class="singlequestion">';
aac80ff3
TH
779 echo quiz_question_edit_button($quiz->cmid, $question, $returnurl,
780 quiz_question_tostring($question) . ' ');
2a874d65 781 echo '<span class="questiontype">';
2a874d65 782 print_question_icon($question);
f9b0500f 783 echo ' ' . question_bank::get_qtype_name($question->qtype) . '</span>';
aac80ff3
TH
784 echo '<span class="questionpreview">' .
785 quiz_question_preview_button($quiz, $question, true) . '</span>';
2a874d65 786 echo "</div>\n";
fa583f5f 787}
788/**
789 * Print a given random question in quiz for the edit tab of edit.php.
790 * Meant to be used from quiz_print_question_list()
f5831eea 791 *
414d5bfb 792 * @param object $question A question object from the database questions table
793 * @param object $questionurl The url of the question editing page as a moodle_url object
794 * @param object $quiz The quiz in the context of which the question is being displayed
f7970e3c 795 * @param bool $quiz_qbanktool Indicate to this function if the question bank window open
fa583f5f 796 */
f5831eea 797function quiz_print_randomquestion(&$question, &$pageurl, &$quiz, $quiz_qbanktool) {
f9b0500f 798 global $DB, $OUTPUT;
fa583f5f 799 echo '<div class="quiz_randomquestion">';
800
aac80ff3
TH
801 if (!$category = $DB->get_record('question_categories',
802 array('id' => $question->category))) {
825116fb 803 echo $OUTPUT->notification('Random question category not found!');
fa583f5f 804 return;
805 }
806
807 echo '<div class="randomquestionfromcategory">';
808 print_question_icon($question);
3cac440b 809 print_random_option_icon($question);
8e84d978 810 echo ' ' . get_string('randomfromcategory', 'quiz') . '</div>';
fa583f5f 811
0ff4bd08 812 $a = new stdClass();
92e01ab7 813 $a->arrow = $OUTPUT->rarrow();
2a874d65 814 $strshowcategorycontents = get_string('showcategorycontents', 'quiz', $a);
f4b879dd 815
b9bc2019 816 $openqbankurl = $pageurl->out(true, array('qbanktool' => 1,
2a874d65 817 'cat' => $category->id . ',' . $category->contextid));
818 $linkcategorycontents = ' <a href="' . $openqbankurl . '">' . $strshowcategorycontents . '</a>';
fa583f5f 819
2a874d65 820 echo '<div class="randomquestioncategory">';
aac80ff3
TH
821 echo '<a href="' . $openqbankurl . '" title="' . $strshowcategorycontents . '">' .
822 $category->name . '</a>';
823 echo '<span class="questionpreview">' .
824 quiz_question_preview_button($quiz, $question, true) . '</span>';
2a874d65 825 echo '</div>';
fa583f5f 826
fd214b59 827 $questionids = question_bank::get_qtype('random')->get_available_questions_from_category(
2a874d65 828 $category->id, $question->questiontext == '1', '0');
829 $questioncount = count($questionids);
fa583f5f 830
831 echo '<div class="randomquestionqlist">';
2a874d65 832 if ($questioncount == 0) {
833 // No questions in category, give an error plus instructions
fa583f5f 834 echo '<span class="error">';
2a874d65 835 print_string('noquestionsnotinuse', 'quiz');
9cdcf826 836 echo '</span>';
fa583f5f 837 echo '<br />';
9cdcf826 838
2a874d65 839 // Embed the link into the string with instructions
0ff4bd08 840 $a = new stdClass();
9cdcf826 841 $a->catname = '<strong>' . $category->name . '</strong>';
2a874d65 842 $a->link = $linkcategorycontents;
f5831eea 843 echo get_string('addnewquestionsqbank', 'quiz', $a);
537daa79 844
2a874d65 845 } else {
846 // Category has questions
f4b879dd 847
2a874d65 848 // Get a sample from the database,
fd214b59 849 $questionidstoshow = array_slice($questionids, 0, NUM_QS_TO_SHOW_IN_RANDOM);
2a874d65 850 $questionstoshow = $DB->get_records_list('question', 'id', $questionidstoshow,
25a03faa 851 '', 'id, qtype, name, questiontext, questiontextformat');
9cdcf826 852
2a874d65 853 // list them,
854 echo '<ul>';
855 foreach ($questionstoshow as $question) {
856 echo '<li>' . quiz_question_tostring($question, true) . '</li>';
857 }
f4b879dd 858
2a874d65 859 // and then display the total number.
860 echo '<li class="totalquestionsinrandomqcategory">';
861 if ($questioncount > NUM_QS_TO_SHOW_IN_RANDOM) {
862 echo '... ';
863 }
864 print_string('totalquestionsinrandomqcategory', 'quiz', $questioncount);
865 echo ' ' . $linkcategorycontents;
866 echo '</li>';
867 echo '</ul>';
ee1fb969 868 }
869
2a874d65 870 echo '</div>';
fa583f5f 871 echo '<div class="randomquestioncategorycount">';
2a874d65 872 echo '</div>';
873 echo '</div>';
fa583f5f 874}
ee1fb969 875
fa583f5f 876/**
877 * Print a given single question in quiz for the reordertool tab of edit.php.
878 * Meant to be used from quiz_print_question_list()
f5831eea 879 *
414d5bfb 880 * @param object $question A question object from the database questions table
881 * @param object $questionurl The url of the question editing page as a moodle_url object
882 * @param object $quiz The quiz in the context of which the question is being displayed
fa583f5f 883 */
f5831eea 884function quiz_print_singlequestion_reordertool($question, $returnurl, $quiz) {
2a874d65 885 echo '<div class="singlequestion">';
886 echo '<label for="s' . $question->id . '">';
887 print_question_icon($question);
888 echo ' ' . quiz_question_tostring($question);
889 echo '</label>';
890 echo '<span class="questionpreview">' .
891 quiz_question_action_icons($quiz, $quiz->cmid, $question, $returnurl) . '</span>';
892 echo "</div>\n";
fa583f5f 893}
3cac440b 894
fa583f5f 895/**
896 * Print a given random question in quiz for the reordertool tab of edit.php.
897 * Meant to be used from quiz_print_question_list()
f5831eea 898 *
414d5bfb 899 * @param object $question A question object from the database questions table
900 * @param object $questionurl The url of the question editing page as a moodle_url object
901 * @param object $quiz The quiz in the context of which the question is being displayed
fa583f5f 902 */
f5831eea 903function quiz_print_randomquestion_reordertool(&$question, &$pageurl, &$quiz) {
f9b0500f 904 global $DB, $OUTPUT;
fa583f5f 905
2a874d65 906 // Load the category, and the number of available questions in it.
fa583f5f 907 if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
825116fb 908 echo $OUTPUT->notification('Random question category not found!');
fa583f5f 909 return;
910 }
aac80ff3
TH
911 $questioncount = count(question_bank::get_qtype(
912 'random')->get_available_questions_from_category(
2a874d65 913 $category->id, $question->questiontext == '1', '0'));
fa583f5f 914
f5831eea 915 $reordercheckboxlabel = '<label for="s' . $question->id . '">';
2a874d65 916 $reordercheckboxlabelclose = '</label>';
917
918 echo '<div class="quiz_randomquestion">';
919 echo '<div class="randomquestionfromcategory">';
fa583f5f 920 echo $reordercheckboxlabel;
921 print_question_icon($question);
3cac440b 922 print_random_option_icon($question);
fa583f5f 923
2a874d65 924 if ($questioncount == 0) {
fa583f5f 925 echo '<span class="error">';
2a874d65 926 print_string('empty', 'quiz');
fa583f5f 927 echo '</span> ';
928 }
929
2a874d65 930 print_string('random', 'quiz');
fa583f5f 931 echo ": $reordercheckboxlabelclose</div>";
932
933 echo '<div class="randomquestioncategory">';
2a874d65 934 echo $reordercheckboxlabel . $category->name . $reordercheckboxlabelclose;
3814fb96 935 echo '<span class="questionpreview">';
2a874d65 936 echo quiz_question_preview_button($quiz, $question, false);
fa583f5f 937 echo '</span>';
938 echo "</div>";
939
fa583f5f 940 echo '<div class="randomquestioncategorycount">';
2a874d65 941 echo '</div>';
942 echo '</div>';
fa583f5f 943}
3cac440b 944
945/**
946 * Print an icon to indicate the 'include subcategories' state of a random question.
947 * @param $question the random question.
948 */
949function print_random_option_icon($question) {
5d3b9994 950 global $OUTPUT;
3cac440b 951 if (!empty($question->questiontext)) {
952 $icon = 'withsubcat';
953 $tooltip = get_string('randomwithsubcat', 'quiz');
954 } else {
955 $icon = 'nosubcat';
956 $tooltip = get_string('randomnosubcat', 'quiz');
957 }
b5d0cafc 958 echo '<img src="' . $OUTPUT->pix_url('i/' . $icon) . '" alt="' .
8e84d978 959 $tooltip . '" title="' . $tooltip . '" class="uihint" />';
3cac440b 960}
961
fa583f5f 962/**
963 * Creates a textual representation of a question for display.
f5831eea 964 *
414d5bfb 965 * @param object $question A question object from the database questions table
f7970e3c
TH
966 * @param bool $showicon If true, show the question's icon with the question. False by default.
967 * @param bool $showquestiontext If true (default), show question text after question name.
414d5bfb 968 * If false, show only question name.
f7970e3c 969 * @param bool $return If true (default), return the output. If false, print it.
fa583f5f 970 */
aac80ff3
TH
971function quiz_question_tostring($question, $showicon = false,
972 $showquestiontext = true, $return = true) {
f9b0500f
TH
973 global $COURSE;
974 $result = '';
975 $result .= '<span class="questionname">';
976 if ($showicon) {
977 $result .= print_question_icon($question, true);
978 echo ' ';
979 }
980 $result .= shorten_text(format_string($question->name), 200) . '</span>';
981 if ($showquestiontext) {
0ff4bd08 982 $formatoptions = new stdClass();
f9b0500f
TH
983 $formatoptions->noclean = true;
984 $formatoptions->para = false;
985 $questiontext = strip_tags(format_text($question->questiontext,
986 $question->questiontextformat,
987 $formatoptions, $COURSE->id));
988 $questiontext = shorten_text($questiontext, 200);
989 $result .= '<span class="questiontext">';
990 if (!empty($questiontext)) {
991 $result .= $questiontext;
f5831eea 992 } else {
f9b0500f
TH
993 $result .= '<span class="error">';
994 $result .= get_string('questiontextisempty', 'quiz');
995 $result .= '</span>';
fa583f5f 996 }
f9b0500f
TH
997 $result .= '</span>';
998 }
999 if ($return) {
1000 return $result;
1001 } else {
1002 echo $result;
1003 }
fa583f5f 1004}
96241873 1005
971c6fca 1006/**
1007 * A column type for the add this question to the quiz.
f7970e3c
TH
1008 *
1009 * @copyright 2009 Tim Hunt
1010 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
971c6fca 1011 */
1012class question_bank_add_to_quiz_action_column extends question_bank_action_column_base {
1013 protected $stradd;
1014
1015 public function init() {
1016 parent::init();
1017 $this->stradd = get_string('addtoquiz', 'quiz');
1018 }
1019
1020 public function get_name() {
1021 return 'addtoquizaction';
1022 }
1023
1024 protected function display_content($question, $rowclasses) {
1025 // for RTL languages: switch right and left arrows
1026 if (right_to_left()) {
453b28d8 1027 $movearrow = 't/removeright';
971c6fca 1028 } else {
453b28d8 1029 $movearrow = 't/moveleft';
971c6fca 1030 }
1031 $this->print_icon($movearrow, $this->stradd, $this->qbank->add_to_quiz_url($question->id));
1032 }
1033
1034 public function get_required_fields() {
1035 return array('q.id');
1036 }
1037}
1038
1039/**
1040 * A column type for the name followed by the start of the question text.
f7970e3c
TH
1041 *
1042 * @copyright 2009 Tim Hunt
1043 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
971c6fca 1044 */
1045class question_bank_question_name_text_column extends question_bank_question_name_column {
1046 public function get_name() {
1047 return 'questionnametext';
1048 }
1049
1050 protected function display_content($question, $rowclasses) {
1051 echo '<div>';
1052 $labelfor = $this->label_for($question);
1053 if ($labelfor) {
1054 echo '<label for="' . $labelfor . '">';
1055 }
1056 echo quiz_question_tostring($question, false, true, true);
1057 if ($labelfor) {
1058 echo '</label>';
1059 }
1060 echo '</div>';
1061 }
1062
1063 public function get_required_fields() {
1064 $fields = parent::get_required_fields();
1065 $fields[] = 'q.questiontext';
e4d1d5e0 1066 $fields[] = 'q.questiontextformat';
971c6fca 1067 return $fields;
1068 }
1069}
1070
f4b879dd 1071/**
1072 * Subclass to customise the view of the question bank for the quiz editing screen.
f7970e3c
TH
1073 *
1074 * @copyright 2009 Tim Hunt
1075 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
f4b879dd 1076 */
1077class quiz_question_bank_view extends question_bank_view {
1078 protected $quizhasattempts = false;
612106b3
TH
1079 /** @var object the quiz settings. */
1080 protected $quiz = false;
1081
1082 /**
1083 * Constructor
1084 * @param question_edit_contexts $contexts
1085 * @param moodle_url $pageurl
1086 * @param object $course course settings
1087 * @param object $cm activity settings.
1088 * @param object $quiz quiz settings.
1089 */
1090 public function __construct($contexts, $pageurl, $course, $cm, $quiz) {
1091 parent::__construct($contexts, $pageurl, $course, $cm);
1092 $this->quiz = $quiz;
1093 }
96241873 1094
cd120b23 1095 protected function known_field_types() {
1096 $types = parent::known_field_types();
971c6fca 1097 $types[] = new question_bank_add_to_quiz_action_column($this);
1098 $types[] = new question_bank_question_name_text_column($this);
1099 return $types;
1100 }
1101
1102 protected function wanted_columns() {
aac80ff3
TH
1103 return array('addtoquizaction', 'checkbox', 'qtype', 'questionnametext',
1104 'editaction', 'previewaction');
f4b879dd 1105 }
fa583f5f 1106
f4b879dd 1107 /**
1108 * Let the question bank display know whether the quiz has been attempted,
1109 * hence whether some bits of UI, like the add this question to the quiz icon,
1110 * should be displayed.
f7970e3c 1111 * @param bool $quizhasattempts whether the quiz has attempts.
f4b879dd 1112 */
1113 public function set_quiz_has_attempts($quizhasattempts) {
1114 $this->quizhasattempts = $quizhasattempts;
971c6fca 1115 if ($quizhasattempts && isset($this->visiblecolumns['addtoquizaction'])) {
1116 unset($this->visiblecolumns['addtoquizaction']);
1117 }
1118 }
1119
612106b3
TH
1120 public function preview_question_url($question) {
1121 return quiz_question_preview_url($this->quiz, $question);
e4d1d5e0 1122 }
1123
971c6fca 1124 public function add_to_quiz_url($questionid) {
1125 global $CFG;
8afba50b
PS
1126 $params = $this->baseurl->params();
1127 $params['addquestion'] = $questionid;
1128 $params['sesskey'] = sesskey();
1129 return new moodle_url('/mod/quiz/edit.php', $params);
f4b879dd 1130 }
fa583f5f 1131
5bd22790 1132 public function display($tabname, $page, $perpage, $sortorder,
f5831eea 1133 $sortorderdecoded, $cat, $recurse, $showhidden, $showquestiontext) {
3b1d5cc4 1134 global $OUTPUT;
5bd22790 1135 if ($this->process_actions_needing_ui()) {
5b5e5ab2 1136 return;
1137 }
f4b879dd 1138
971c6fca 1139 // Display the current category.
1140 if (!$category = $this->get_current_category($cat)) {
5b5e5ab2 1141 return;
fa583f5f 1142 }
971c6fca 1143 $this->print_category_info($category);
5b5e5ab2 1144
3b1d5cc4 1145 echo $OUTPUT->box_start('generalbox questionbank');
5b5e5ab2 1146
5bd22790 1147 $this->display_category_form($this->contexts->having_one_edit_tab_cap($tabname),
971c6fca 1148 $this->baseurl, $cat);
5b5e5ab2 1149
1150 // continues with list of questions
aac80ff3
TH
1151 $this->display_question_list($this->contexts->having_one_edit_tab_cap($tabname),
1152 $this->baseurl, $cat, $this->cm, $recurse, $page,
1153 $perpage, $showhidden, $sortorder, $sortorderdecoded, $showquestiontext,
5bd22790 1154 $this->contexts->having_cap('moodle/question:add'));
5b5e5ab2 1155
971c6fca 1156 $this->display_options($recurse, $showhidden, $showquestiontext);
3b1d5cc4 1157 echo $OUTPUT->box_end();
f4b879dd 1158 }
5b5e5ab2 1159
971c6fca 1160 protected function print_choose_category_message($categoryandcontext) {
3b1d5cc4 1161 global $OUTPUT;
1162 echo $OUTPUT->box_start('generalbox questionbank');
aac80ff3
TH
1163 $this->display_category_form($this->contexts->having_one_edit_tab_cap('edit'),
1164 $this->baseurl, $categoryandcontext);
971c6fca 1165 echo "<p style=\"text-align:center;\"><b>";
f5831eea 1166 print_string('selectcategoryabove', 'quiz');
971c6fca 1167 echo "</b></p>";
3b1d5cc4 1168 echo $OUTPUT->box_end();
5bd22790 1169 }
1170
971c6fca 1171 protected function print_category_info($category) {
0ff4bd08 1172 $formatoptions = new stdClass();
971c6fca 1173 $formatoptions->noclean = true;
1174 $strcategory = get_string('category', 'quiz');
f5831eea 1175 echo '<div class="categoryinfo"><div class="categorynamefieldcontainer">' .
971c6fca 1176 $strcategory;
1177 echo ': <span class="categorynamefield">';
22cebed5 1178 echo shorten_text(strip_tags(format_string($category->name)), 60);
aac80ff3
TH
1179 echo '</span></div><div class="categoryinfofieldcontainer">' .
1180 '<span class="categoryinfofield">';
22cebed5 1181 echo shorten_text(strip_tags(format_text($category->info, $category->infoformat,
f5831eea 1182 $formatoptions, $this->course->id)), 200);
971c6fca 1183 echo '</span></div></div>';
5bd22790 1184 }
1185
92e6e128 1186 protected function display_options($recurse, $showhidden, $showquestiontext) {
971c6fca 1187 echo '<form method="get" action="edit.php" id="displayoptions">';
1188 echo "<fieldset class='invisiblefieldset'>";
aac80ff3
TH
1189 echo html_writer::input_hidden_params($this->baseurl,
1190 array('recurse', 'showhidden', 'showquestiontext'));
92e6e128 1191 $this->display_category_form_checkbox('recurse', $recurse,
aac80ff3 1192 get_string('includesubcategories', 'question'));
92e6e128 1193 $this->display_category_form_checkbox('showhidden', $showhidden,
aac80ff3
TH
1194 get_string('showhidden', 'question'));
1195 echo '<noscript><div class="centerpara"><input type="submit" value="' .
1196 get_string('go') . '" />';
971c6fca 1197 echo '</div></noscript></fieldset></form>';
5bd22790 1198 }
8bccba71 1199}
f4b879dd 1200
8bccba71 1201/**
1202 * Prints the form for setting a quiz' overall grade
f5831eea 1203 *
414d5bfb 1204 * @param object $quiz The quiz object of the quiz in question
f5831eea 1205 * @param object $pageurl The url of the current page with the parameters required
414d5bfb 1206 * for links returning to the current page, as a moodle_url object
f7970e3c
TH
1207 * @param int $tabindex The tabindex to start from for the form elements created
1208 * @return int The tabindex from which the calling page can continue, that is,
414d5bfb 1209 * the last value used +1.
8bccba71 1210 */
f5831eea 1211function quiz_print_grading_form($quiz, $pageurl, $tabindex) {
825116fb 1212 global $USER, $OUTPUT;
f5831eea 1213 $strsave = get_string('save', 'quiz');
fd214b59 1214 echo '<form method="post" action="edit.php" class="quizsavegradesform"><div>';
8bccba71 1215 echo '<fieldset class="invisiblefieldset" style="display: block;">';
f5831eea 1216 echo "<input type=\"hidden\" name=\"sesskey\" value=\"" . sesskey() . "\" />";
6ea66ff3 1217 echo html_writer::input_hidden_params($pageurl);
aac80ff3
TH
1218 $a = '<input type="text" id="inputmaxgrade" name="maxgrade" size="' .
1219 ($quiz->decimalpoints + 2) . '" tabindex="' . $tabindex
f5831eea 1220 . '" value="' . quiz_format_grade($quiz, $quiz->grade) . '" />';
1221 echo '<label for="inputmaxgrade">' . get_string('maximumgradex', '', $a) . "</label>";
1222 echo '<input type="hidden" name="savechanges" value="save" />';
1223 echo '<input type="submit" value="' . $strsave . '" />';
8bccba71 1224 echo '</fieldset>';
1225 echo "</div></form>\n";
f5831eea 1226 return $tabindex + 1;
8bccba71 1227}
f4b879dd 1228
8bccba71 1229/**
1230 * Print the status bar
1231 *
414d5bfb 1232 * @param object $quiz The quiz object of the quiz in question
8bccba71 1233 */
f5831eea 1234function quiz_print_status_bar($quiz) {
8bccba71 1235 global $CFG;
c5da295b
TH
1236
1237 $bits = array();
1238
1239 $bits[] = html_writer::tag('span',
1240 get_string('totalpointsx', 'quiz', quiz_format_grade($quiz, $quiz->sumgrades)),
1241 array('class' => 'totalpoints'));
1242
1243 $bits[] = html_writer::tag('span',
1244 get_string('numquestionsx', 'quiz', quiz_number_of_questions_in_quiz($quiz->questions)),
1245 array('class' => 'numberofquestions'));
1246
e476804c 1247 $timenow = time();
c5da295b
TH
1248
1249 // Exact open and close dates for the tool-tip.
1250 $dates = array();
e476804c 1251 if ($quiz->timeopen > 0) {
1252 if ($timenow > $quiz->timeopen) {
940b5fbb 1253 $dates[] = get_string('quizopenedon', 'quiz', userdate($quiz->timeopen));
e476804c 1254 } else {
940b5fbb 1255 $dates[] = get_string('quizwillopen', 'quiz', userdate($quiz->timeopen));
e476804c 1256 }
1257 }
1258 if ($quiz->timeclose > 0) {
1259 if ($timenow > $quiz->timeclose) {
940b5fbb 1260 $dates[] = get_string('quizclosed', 'quiz', userdate($quiz->timeclose));
e476804c 1261 } else {
940b5fbb 1262 $dates[] = get_string('quizcloseson', 'quiz', userdate($quiz->timeclose));
e476804c 1263 }
1264 }
1265 if (empty($dates)) {
1266 $dates[] = get_string('alwaysavailable', 'quiz');
1267 }
c5da295b
TH
1268 $tooltip = implode(', ', $dates);;
1269
1270 // Brief summary on the page.
1271 if ($timenow < $quiz->timeopen) {
1272 $currentstatus = get_string('quizisclosedwillopen', 'quiz',
1273 userdate($quiz->timeclose, get_string('strftimedatetimeshort', 'langconfig')));
1274 } else if ($quiz->timeclose && $timenow <= $quiz->timeclose) {
1275 $currentstatus = get_string('quizisopenwillclose', 'quiz',
1276 userdate($quiz->timeclose, get_string('strftimedatetimeshort', 'langconfig')));
1277 } else if ($quiz->timeclose && $timenow > $quiz->timeclose) {
1278 $currentstatus = get_string('quizisclosed', 'quiz');
1279 } else {
1280 $currentstatus = get_string('quizisopen', 'quiz');
1281 }
940b5fbb 1282
c5da295b
TH
1283 $bits[] = html_writer::tag('span', $currentstatus,
1284 array('class' => 'quizopeningstatus', 'title' => implode(', ', $dates)));
1285
1286 echo html_writer::tag('div', implode(' | ', $bits), array('class' => 'statusbar'));
8bccba71 1287}