MDL-20636 Preserve scroll position when doing action on the quiz edit page.
[moodle.git] / question / type / random / questiontype.php
CommitLineData
aeb15530 1<?php
f9b0500f
TH
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
1976496e 19/**
f9b0500f 20 * Question type class for the random question type.
f34488b2 21 *
f9b0500f
TH
22 * @package qtype_random
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27
28/**
29 * The random question type.
30 *
31 * This question type does not have a question definition class, nor any
32 * renderers. When you load a question of this type, it actually loads a
33 * question chosen randomly from a particular category in the question bank.
f34488b2 34 *
f9b0500f
TH
35 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
53a4d39f 37 */
f9b0500f
TH
38class qtype_random extends question_type {
39 /** @var string comma-separated list of qytpe names not to select, can be used in SQL. */
f24493ec 40 protected $excludedqtypes = null;
516cf3eb 41
f9b0500f
TH
42 /** @var string comma-separated list of manually graded qytpe names, can be used in SQL. */
43 protected $manualqtypes = null;
516cf3eb 44
f9b0500f
TH
45 /**
46 * Cache of availabe question ids from a particular category.
47 * @var array two-dimensional array. The first key is a category id, the
48 * second key is wether subcategories should be included.
49 */
50 private $availablequestionsbycategory = array();
516cf3eb 51
f9b0500f 52 public function menu_name() {
8b192edb 53 // Don't include this question type in the 'add new question' menu.
a2156789 54 return false;
55 }
56
f9b0500f 57 public function is_manual_graded() {
f24493ec 58 return true;
59 }
60
f9b0500f
TH
61 public function is_usable_by_random() {
62 return false;
f24493ec 63 }
64
f9b0500f 65 public function is_question_manual_graded($question, $otherquestionsinuse) {
f24493ec 66 global $DB;
67 // We take our best shot at working whether a particular question is manually
68 // graded follows: We look to see if any of the questions that this random
69 // question might select if of a manually graded type. If a category contains
70 // a mixture of manual and non-manual questions, and if all the attempts so
71 // far selected non-manual ones, this will give the wrong answer, but we
72 // don't care. Even so, this is an expensive calculation!
73 $this->init_qtype_lists();
74 if (!$this->manualqtypes) {
75 return false;
76 }
77 if ($question->questiontext) {
78 $categorylist = question_categorylist($question->category);
79 } else {
80 $categorylist = $question->category;
81 }
82 return $DB->record_exists_select('question',
83 "category IN ($categorylist)
84 AND parent = 0
85 AND hidden = 0
86 AND id NOT IN ($otherquestionsinuse)
87 AND qtype IN ($this->manualqtypes)");
88 }
89
f24493ec 90 /**
91 * This method needs to be called before the ->excludedqtypes and
92 * ->manualqtypes fields can be used.
93 */
f9b0500f
TH
94 protected function init_qtype_lists() {
95 if (!is_null($this->excludedqtypes)) {
96 return; // Already done.
97 }
98 $excludedqtypes = array();
99 $manualqtypes = array();
100 foreach (question_bank::get_all_qtypes() as $qtype) {
101 $quotedname = "'" . $qtype->name() . "'";
102 if (!$qtype->is_usable_by_random()) {
103 $excludedqtypes[] = $quotedname;
104 } else if ($qtype->is_manual_graded()) {
105 $manualqtypes[] = $quotedname;
f24493ec 106 }
f24493ec 107 }
f9b0500f
TH
108 $this->excludedqtypes = implode(',', $excludedqtypes);
109 $this->manualqtypes = implode(',', $manualqtypes);
f24493ec 110 }
111
bcc234b0 112 function display_question_editing_page(&$mform, $question, $wizardnow){
fe6ce234 113 global $OUTPUT;
4995b9c1 114 $heading = $this->get_heading(empty($question->id));
4bcc5118 115 echo $OUTPUT->heading_with_help($heading, $this->name(), $this->plugin_name());
bcc234b0 116 $mform->display();
117 }
118
f9b0500f 119 public function get_question_options($question) {
516cf3eb 120 return true;
121 }
122
f59dba84 123 /**
124 * Random questions always get a question name that is Random (cateogryname).
125 * This function is a centralised place to calculate that, given the category.
3cac440b 126 * @param object $category the category this question picks from. (Only $category->name is used.)
127 * @param boolean $includesubcategories whether this question also picks from subcategories.
128 * @return string the name this question should have.
f59dba84 129 */
f9b0500f 130 public function question_name($category, $includesubcategories) {
3cac440b 131 if ($includesubcategories) {
132 $string = 'randomqplusname';
133 } else {
134 $string = 'randomqname';
135 }
136 return get_string($string, 'qtype_random', $category->name);
f59dba84 137 }
138
f9b0500f
TH
139 protected function set_selected_question_name($question, $randomname) {
140 $a = new stdClass;
141 $a->randomname = $randomname;
142 $a->questionname = $question->name;
143 $question->name = get_string('selectedby', 'qtype_random', $a);
5348b899 144 }
145
f9b0500f
TH
146 public function save_question($question, $form, $course) {
147 $form->name = '';
148 // Name is not a required field for random questions, but
149 // parent::save_question Assumes that it is.
150 return parent::save_question($question, $form, $course);
151 }
24e8b9b6 152
f9b0500f 153 public function save_question_options($question) {
24e8b9b6 154 // No options, as such, but we set the parent field to the question's
155 // own id. Setting the parent field has the effect of hiding this
156 // question in various places.
157 $updateobject = new stdClass;
158 $updateobject->id = $question->id;
159 $updateobject->parent = $question->id;
160
161 // We also force the question name to be 'Random (categoryname)'.
f9b0500f 162 $category = $DB->get_record('question_categories', array('id' => $question->category), '*', MUST_EXIST);
3cac440b 163 $updateobject->name = $this->question_name($category, !empty($question->questiontext));
24e8b9b6 164 return $DB->update_record('question', $updateobject);
516cf3eb 165 }
166
339ef4c2 167 /**
168 * Get all the usable questions from a particular question category.
169 *
170 * @param integer $categoryid the id of a question category.
171 * @param boolean whether to include questions from subcategories.
172 * @param string $questionsinuse comma-separated list of question ids to exclude from consideration.
173 * @return array of question records.
174 */
f9b0500f
TH
175 public function get_available_questions_from_category($categoryid, $subcategories) {
176 if (isset($this->availablequestionsbycategory[$categoryid][$subcategories])) {
177 return $this->availablequestionsbycategory[$categoryid][$subcategories];
178 }
179
f24493ec 180 $this->init_qtype_lists();
339ef4c2 181 if ($subcategories) {
f9b0500f 182 $categoryids = question_categorylist($categoryid);
339ef4c2 183 } else {
f9b0500f 184 $categoryids = $categoryid;
516cf3eb 185 }
186
f9b0500f
TH
187 $questionids = question_bank::get_finder()->get_questions_from_categories(
188 $categoryids, 'qtype NOT IN (' . $this->excludedqtypes . ')');
189 $this->availablequestionsbycategory[$categoryid][$subcategories] = $questionids;
190 return $questionids;
516cf3eb 191 }
192
f9b0500f
TH
193 public function make_question($questiondata) {
194 return $this->choose_other_question($questiondata, array());
516cf3eb 195 }
196
f9b0500f
TH
197 /**
198 * Load the definition of another question picked randomly by this question.
199 * @param object $questiondata the data defining a random question.
200 * @param array $excludedquestions of question ids. We will no pick any question whose id is in this list.
201 * @param boolean $allowshuffle if false, then any shuffle option on the selected quetsion is disabled.
202 * @return question_definition|null the definition of the question that was
203 * selected, or null if no suitable question could be found.
204 */
205 public function choose_other_question($questiondata, $excludedquestions, $allowshuffle = true) {
206 $available = $this->get_available_questions_from_category($questiondata->category,
207 !empty($questiondata->questiontext));
208 shuffle($available);
209
210 foreach ($available as $questionid) {
211 if (in_array($questionid, $excludedquestions)) {
212 continue;
213 }
516cf3eb 214
f9b0500f
TH
215 $question = question_bank::load_question($questionid, $allowshuffle);
216 $this->set_selected_question_name($question, $questiondata->name);
217 return $question;
516cf3eb 218 }
f9b0500f 219 return null;
516cf3eb 220 }
221
f9b0500f
TH
222 function get_random_guess_score($questiondata) {
223 return null;
516cf3eb 224 }
516cf3eb 225}