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