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