MDL-20636 Boilerplate and other cleanup in the question core code.
[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
a13d4fbd 146 public function save_question($question, $form) {
f9b0500f
TH
147 $form->name = '';
148 // Name is not a required field for random questions, but
149 // parent::save_question Assumes that it is.
a13d4fbd 150 return parent::save_question($question, $form);
f9b0500f 151 }
24e8b9b6 152
f9b0500f 153 public function save_question_options($question) {
a13d4fbd
TH
154 global $DB;
155
24e8b9b6 156 // No options, as such, but we set the parent field to the question's
157 // own id. Setting the parent field has the effect of hiding this
158 // question in various places.
159 $updateobject = new stdClass;
160 $updateobject->id = $question->id;
161 $updateobject->parent = $question->id;
162
163 // We also force the question name to be 'Random (categoryname)'.
f9b0500f 164 $category = $DB->get_record('question_categories', array('id' => $question->category), '*', MUST_EXIST);
3cac440b 165 $updateobject->name = $this->question_name($category, !empty($question->questiontext));
24e8b9b6 166 return $DB->update_record('question', $updateobject);
516cf3eb 167 }
168
339ef4c2 169 /**
170 * Get all the usable questions from a particular question category.
171 *
172 * @param integer $categoryid the id of a question category.
173 * @param boolean whether to include questions from subcategories.
174 * @param string $questionsinuse comma-separated list of question ids to exclude from consideration.
175 * @return array of question records.
176 */
f9b0500f
TH
177 public function get_available_questions_from_category($categoryid, $subcategories) {
178 if (isset($this->availablequestionsbycategory[$categoryid][$subcategories])) {
179 return $this->availablequestionsbycategory[$categoryid][$subcategories];
180 }
181
f24493ec 182 $this->init_qtype_lists();
339ef4c2 183 if ($subcategories) {
f9b0500f 184 $categoryids = question_categorylist($categoryid);
339ef4c2 185 } else {
f9b0500f 186 $categoryids = $categoryid;
516cf3eb 187 }
188
f9b0500f
TH
189 $questionids = question_bank::get_finder()->get_questions_from_categories(
190 $categoryids, 'qtype NOT IN (' . $this->excludedqtypes . ')');
191 $this->availablequestionsbycategory[$categoryid][$subcategories] = $questionids;
192 return $questionids;
516cf3eb 193 }
194
f9b0500f
TH
195 public function make_question($questiondata) {
196 return $this->choose_other_question($questiondata, array());
516cf3eb 197 }
198
f9b0500f
TH
199 /**
200 * Load the definition of another question picked randomly by this question.
201 * @param object $questiondata the data defining a random question.
202 * @param array $excludedquestions of question ids. We will no pick any question whose id is in this list.
203 * @param boolean $allowshuffle if false, then any shuffle option on the selected quetsion is disabled.
204 * @return question_definition|null the definition of the question that was
205 * selected, or null if no suitable question could be found.
206 */
207 public function choose_other_question($questiondata, $excludedquestions, $allowshuffle = true) {
208 $available = $this->get_available_questions_from_category($questiondata->category,
209 !empty($questiondata->questiontext));
210 shuffle($available);
211
212 foreach ($available as $questionid) {
213 if (in_array($questionid, $excludedquestions)) {
214 continue;
215 }
516cf3eb 216
f9b0500f
TH
217 $question = question_bank::load_question($questionid, $allowshuffle);
218 $this->set_selected_question_name($question, $questiondata->name);
219 return $question;
516cf3eb 220 }
f9b0500f 221 return null;
516cf3eb 222 }
223
f9b0500f
TH
224 function get_random_guess_score($questiondata) {
225 return null;
516cf3eb 226 }
516cf3eb 227}