2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Functions used to show question editing interface
21 * @subpackage questionbank
22 * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 use core_question\bank\search\category_condition
;
29 defined('MOODLE_INTERNAL') ||
die();
31 require_once($CFG->libdir
. '/questionlib.php');
33 define('DEFAULT_QUESTIONS_PER_PAGE', 20);
34 define('MAXIMUM_QUESTIONS_PER_PAGE', 1000);
36 function get_module_from_cmid($cmid) {
38 if (!$cmrec = $DB->get_record_sql("SELECT cm.*, md.name as modname
39 FROM {course_modules} cm,
42 md.id = cm.module", array($cmid))){
43 print_error('invalidcoursemodule');
44 } elseif (!$modrec =$DB->get_record($cmrec->modname
, array('id' => $cmrec->instance
))) {
45 print_error('invalidcoursemodule');
47 $modrec->instance
= $modrec->id
;
48 $modrec->cmid
= $cmrec->id
;
49 $cmrec->name
= $modrec->name
;
51 return array($modrec, $cmrec);
55 * Function to read all questions for category into big array
57 * @param object $category category number
58 * @param bool $noparent if true only questions with NO parent will be selected
59 * @param bool $recurse include subdirectories
60 * @param bool $export set true if this is called by questionbank export
61 * @param bool $latestversion if only the latest versions needed
64 function get_questions_category(object $category, bool $noparent, bool $recurse = true, bool $export = true,
65 bool $latestversion = false): array {
68 // Build sql bit for $noparent.
71 $npsql = " and q.parent='0' ";
74 // Get list of categories.
76 $categorylist = question_categorylist($category->id
);
78 $categorylist = [$category->id
];
81 // Get the list of questions for the category.
82 list($usql, $params) = $DB->get_in_or_equal($categorylist);
84 // Get the latest version of a question.
87 $version = 'AND (qv.version = (SELECT MAX(v.version)
88 FROM {question_versions} v
89 JOIN {question_bank_entries} be
90 ON be.id = v.questionbankentryid
91 WHERE be.id = qbe.id) OR qv.version is null)';
93 $questions = $DB->get_records_sql("SELECT q.*, qv.status, qc.id AS category
95 JOIN {question_versions} qv ON qv.questionid = q.id
96 JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
97 JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid
98 WHERE qc.id {$usql} {$npsql} {$version}
99 ORDER BY qc.id, q.qtype, q.name", $params);
101 // Iterate through questions, getting stuff we need.
103 foreach($questions as $key => $question) {
104 $question->export_process
= $export;
105 $qtype = question_bank
::get_qtype($question->qtype
, false);
106 if ($export && $qtype->name() === 'missingtype') {
107 // Unrecognised question type. Skip this question when exporting.
110 $qtype->get_question_options($question);
111 $qresults[] = $question;
118 * Checks whether this is the only child of a top category in a context.
120 * @param int $categoryid a category id.
122 * @deprecated since Moodle 4.0 MDL-71585
123 * @see qbank_managecategories\helper
124 * @todo Final deprecation on Moodle 4.4 MDL-72438
126 function question_is_only_child_of_top_category_in_context($categoryid) {
127 debugging('Function question_is_only_child_of_top_category_in_context()
128 has been deprecated and moved to qbank_managecategories plugin,
129 Please use qbank_managecategories\helper::question_is_only_child_of_top_category_in_context() instead.',
131 return \qbank_managecategories\helper
::question_is_only_child_of_top_category_in_context($categoryid);
135 * Checks whether the category is a "Top" category (with no parent).
137 * @param int $categoryid a category id.
139 * @deprecated since Moodle 4.0 MDL-71585
140 * @see qbank_managecategories\helper
141 * @todo Final deprecation on Moodle 4.4 MDL-72438
143 function question_is_top_category($categoryid) {
144 debugging('Function question_is_top_category() has been deprecated and moved to qbank_managecategories plugin,
145 Please use qbank_managecategories\helper::question_is_top_category() instead.', DEBUG_DEVELOPER
);
146 return \qbank_managecategories\helper
::question_is_top_category($categoryid);
150 * Ensures that this user is allowed to delete this category.
152 * @param int $todelete a category id.
153 * @deprecated since Moodle 4.0 MDL-71585
154 * @see qbank_managecategories\helper
155 * @todo Final deprecation on Moodle 4.4 MDL-72438
157 function question_can_delete_cat($todelete) {
158 debugging('Function question_can_delete_cat() has been deprecated and moved to qbank_managecategories plugin,
159 Please use qbank_managecategories\helper::question_can_delete_cat() instead.', DEBUG_DEVELOPER
);
160 \qbank_managecategories\helper
::question_can_delete_cat($todelete);
164 * Common setup for all pages for editing questions.
165 * @param string $baseurl the name of the script calling this funciton. For examle 'qusetion/edit.php'.
166 * @param string $edittab code for this edit tab
167 * @param bool $requirecmid require cmid? default false
168 * @param bool $unused no longer used, do no pass
169 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
171 function question_edit_setup($edittab, $baseurl, $requirecmid = false, $unused = null) {
174 if ($unused !== null) {
175 debugging('Deprecated argument passed to question_edit_setup()', DEBUG_DEVELOPER
);
181 $params['cmid'] = required_param('cmid', PARAM_INT
);
183 $params['cmid'] = optional_param('cmid', null, PARAM_INT
);
186 if (!$params['cmid']) {
187 $params['courseid'] = required_param('courseid', PARAM_INT
);
190 $params['qpage'] = optional_param('qpage', null, PARAM_INT
);
192 // Pass 'cat' from page to page and when 'category' comes from a drop down menu
193 // then we also reset the qpage so we go to page 1 of
195 $params['cat'] = optional_param('cat', null, PARAM_SEQUENCE
); // If empty will be set up later.
196 $params['category'] = optional_param('category', null, PARAM_SEQUENCE
);
197 $params['qperpage'] = optional_param('qperpage', null, PARAM_INT
);
199 // Question table sorting options.
200 for ($i = 1; $i <= core_question\local\bank\view
::MAX_SORTS
; $i++
) {
202 if ($sort = optional_param($param, '', PARAM_TEXT
)) {
203 $params[$param] = $sort;
210 $params['recurse'] = optional_param('recurse', null, PARAM_BOOL
);
211 $params['showhidden'] = optional_param('showhidden', null, PARAM_BOOL
);
212 $params['qbshowtext'] = optional_param('qbshowtext', null, PARAM_BOOL
);
213 // Category list page.
214 $params['cpage'] = optional_param('cpage', null, PARAM_INT
);
215 $params['qtagids'] = optional_param_array('qtagids', null, PARAM_INT
);
217 $PAGE->set_pagelayout('admin');
219 return question_build_edit_resources($edittab, $baseurl, $params);
223 * Common function for building the generic resources required by the
224 * editing questions pages.
226 * Either a cmid or a course id must be provided as keys in $params or
227 * an exception will be thrown. All other params are optional and will have
228 * sane default applied if not provided.
230 * The acceptable keys for $params are:
232 * 'cmid' => PARAM_INT,
233 * 'courseid' => PARAM_INT,
234 * 'qpage' => PARAM_INT,
235 * 'cat' => PARAM_SEQUENCE,
236 * 'category' => PARAM_SEQUENCE,
237 * 'qperpage' => PARAM_INT,
238 * 'recurse' => PARAM_INT,
239 * 'showhidden' => PARAM_INT,
240 * 'qbshowtext' => PARAM_INT,
241 * 'cpage' => PARAM_INT,
242 * 'recurse' => PARAM_BOOL,
243 * 'showhidden' => PARAM_BOOL,
244 * 'qbshowtext' => PARAM_BOOL,
245 * 'qtagids' => [PARAM_INT], (array of integers)
246 * 'qbs1' => PARAM_TEXT,
247 * 'qbs2' => PARAM_TEXT,
248 * 'qbs3' => PARAM_TEXT,
249 * ... and more qbs keys up to core_question\local\bank\view::MAX_SORTS ...
252 * @param string $edittab Code for this edit tab
253 * @param string $baseurl The name of the script calling this funciton. For examle 'qusetion/edit.php'.
254 * @param array $params The provided parameters to construct the resources with.
255 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
257 function question_build_edit_resources($edittab, $baseurl, $params) {
258 global $DB, $PAGE, $CFG;
260 $thispageurl = new moodle_url($baseurl);
261 $thispageurl->remove_all_params(); // We are going to explicity add back everything important - this avoids unwanted params from being retained.
269 'courseid' => PARAM_INT
,
270 'qpage' => PARAM_INT
,
271 'cat' => PARAM_SEQUENCE
,
272 'category' => PARAM_SEQUENCE
,
273 'qperpage' => PARAM_INT
,
274 'recurse' => PARAM_INT
,
275 'showhidden' => PARAM_INT
,
276 'qbshowtext' => PARAM_INT
,
277 'cpage' => PARAM_INT
,
278 'recurse' => PARAM_BOOL
,
279 'showhidden' => PARAM_BOOL
,
280 'qbshowtext' => PARAM_BOOL
283 foreach ($paramtypes as $name => $type) {
284 if (isset($params[$name])) {
285 $cleanparams[$name] = clean_param($params[$name], $type);
287 $cleanparams[$name] = null;
291 if (!empty($params['qtagids'])) {
292 $cleanparams['qtagids'] = clean_param_array($params['qtagids'], PARAM_INT
);
295 $cmid = $cleanparams['cmid'];
296 $courseid = $cleanparams['courseid'];
297 $qpage = $cleanparams['qpage'] ?
: -1;
298 $cat = $cleanparams['cat'] ?
: 0;
299 $category = $cleanparams['category'] ?
: 0;
300 $qperpage = $cleanparams['qperpage'];
301 $recurse = $cleanparams['recurse'];
302 $showhidden = $cleanparams['showhidden'];
303 $qbshowtext = $cleanparams['qbshowtext'];
304 $cpage = $cleanparams['cpage'] ?
: 1;
305 $recurse = $cleanparams['recurse'];
306 $showhidden = $cleanparams['showhidden'];
307 $qbshowtext = $cleanparams['qbshowtext'];
308 $qsorts = $cleanparams['qsorts'];
309 $qtagids = $cleanparams['qtagids'];
311 if (is_null($cmid) && is_null($courseid)) {
312 throw new \
moodle_exception('Must provide a cmid or courseid');
316 list($module, $cm) = get_module_from_cmid($cmid);
317 $courseid = $cm->course
;
318 $thispageurl->params(compact('cmid'));
319 require_login($courseid, false, $cm);
320 $thiscontext = context_module
::instance($cmid);
324 $thispageurl->params(compact('courseid'));
325 require_login($courseid, false);
326 $thiscontext = context_course
::instance($courseid);
330 $contexts = new core_question\local\bank\
question_edit_contexts($thiscontext);
331 $contexts->require_one_edit_tab_cap($edittab);
336 $pagevars['qpage'] = $qpage;
338 // Pass 'cat' from page to page and when 'category' comes from a drop down menu
339 // then we also reset the qpage so we go to page 1 of
341 if ($category && $category != $cat) { // Is this a move to a new category?
342 $pagevars['cat'] = $category;
343 $pagevars['qpage'] = 0;
345 $pagevars['cat'] = $cat; // If empty will be set up later.
348 if ($pagevars['cat']){
349 $thispageurl->param('cat', $pagevars['cat']);
352 if (strpos($baseurl, '/question/') === 0) {
353 navigation_node
::override_active_url($thispageurl);
356 // This need to occur after the override_active_url call above because
357 // these values change on the page request causing the URLs to mismatch
358 // when trying to work out the active node.
359 for ($i = 1; $i <= core_question\local\bank\view
::MAX_SORTS
; $i++
) {
361 if (isset($params[$param])) {
362 $value = clean_param($params[$param], PARAM_TEXT
);
366 $thispageurl->param($param, $value);
369 if ($pagevars['qpage'] > -1) {
370 $thispageurl->param('qpage', $pagevars['qpage']);
372 $pagevars['qpage'] = 0;
375 $pagevars['qperpage'] = question_set_or_get_user_preference(
376 'qperpage', $qperpage, DEFAULT_QUESTIONS_PER_PAGE
, $thispageurl);
378 $defaultcategory = question_make_default_categories($contexts->all());
380 $contextlistarr = [];
381 foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){
382 $contextlistarr[] = "'{$context->id}'";
384 $contextlist = join(' ,', $contextlistarr);
385 if (!empty($pagevars['cat'])){
386 $catparts = explode(',', $pagevars['cat']);
387 if (!$catparts[0] ||
(false !== array_search($catparts[1], $contextlistarr)) ||
388 !$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) {
389 print_error('invalidcategory', 'question');
392 $category = $defaultcategory;
393 $pagevars['cat'] = "{$category->id},{$category->contextid}";
397 $pagevars['recurse'] = question_set_or_get_user_preference('recurse', $recurse, 1, $thispageurl);
398 $pagevars['showhidden'] = question_set_or_get_user_preference('showhidden', $showhidden, 0, $thispageurl);
399 $pagevars['qbshowtext'] = question_set_or_get_user_preference('qbshowtext', $qbshowtext, 0, $thispageurl);
401 // Category list page.
402 $pagevars['cpage'] = $cpage;
403 if ($pagevars['cpage'] != 1){
404 $thispageurl->param('cpage', $pagevars['cpage']);
407 $pagevars['qtagids'] = $qtagids;
408 foreach ($pagevars['qtagids'] as $index => $qtagid) {
409 $thispageurl->param("qtagids[{$index}]", $qtagid);
412 return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
416 * Get the category id from $pagevars.
417 * @param array $pagevars from {@link question_edit_setup()}.
418 * @return int the category id.
420 function question_get_category_id_from_pagevars(array $pagevars) {
421 list($questioncategoryid) = explode(',', $pagevars['cat']);
422 return $questioncategoryid;
426 * Get a particular question preference that is also stored as a user preference.
427 * If the the value is given in the GET/POST request, then that value is used,
428 * and the user preference is updated to that value. Otherwise, the last set
429 * value of the user preference is used, or if it has never been set the default
430 * passed to this function.
432 * @param string $param the param name. The URL parameter set, and the GET/POST
433 * parameter read. The user_preference name is 'question_bank_' . $param.
434 * @param mixed $default The default value to use, if not otherwise set.
435 * @param int $type one of the PARAM_... constants.
436 * @param moodle_url $thispageurl if the value has been explicitly set, we add
438 * @return mixed the parameter value to use.
440 function question_get_display_preference($param, $default, $type, $thispageurl) {
441 $submittedvalue = optional_param($param, null, $type);
442 return question_set_or_get_user_preference($param, $submittedvalue, $default, $thispageurl);
446 * Get a user preference by name or set the user preference to a given value.
448 * If $value is null then the function will only attempt to retrieve the
449 * user preference requested by $name. If no user preference is found then the
450 * $default value will be returned. In this case the user preferences are not
451 * modified and nor are the params on $thispageurl.
453 * If $value is anything other than null then the function will set the user
454 * preference $name to the provided $value and will also set it as a param
457 * @param string $name The user_preference name is 'question_bank_' . $name.
458 * @param mixed $value The preference value.
459 * @param mixed $default The default value to use, if not otherwise set.
460 * @param moodle_url $thispageurl if the value has been explicitly set, we add
462 * @return mixed the parameter value to use.
464 function question_set_or_get_user_preference($name, $value, $default, $thispageurl) {
465 if (is_null($value)) {
466 return get_user_preferences('question_bank_' . $name, $default);
469 set_user_preference('question_bank_' . $name, $value);
470 $thispageurl->param($name, $value);
475 * Make sure user is logged in as required in this context.
477 function require_login_in_context($contextorid = null){
479 if (!is_object($contextorid)){
480 $context = context
::instance_by_id($contextorid, IGNORE_MISSING
);
482 $context = $contextorid;
484 if ($context && ($context->contextlevel
== CONTEXT_COURSE
)) {
485 require_login($context->instanceid
);
486 } else if ($context && ($context->contextlevel
== CONTEXT_MODULE
)) {
487 if ($cm = $DB->get_record('course_modules',array('id' =>$context->instanceid
))) {
488 if (!$course = $DB->get_record('course', array('id' => $cm->course
))) {
489 print_error('invalidcourseid');
491 require_course_login($course, true, $cm);
494 print_error('invalidcoursemodule');
496 } else if ($context && ($context->contextlevel
== CONTEXT_SYSTEM
)) {
497 if (!empty($CFG->forcelogin
)) {
507 * Print a form to let the user choose which question type to add.
508 * When the form is submitted, it goes to the question.php script.
509 * @param $hiddenparams hidden parameters to add to the form, in addition to
510 * the qtype radio buttons.
511 * @param $allowedqtypes optional list of qtypes that are allowed. If given, only
512 * those qtypes will be shown. Example value array('description', 'multichoice').
513 * @deprecated since Moodle 4.0
514 * @see \qbank_editquestion\editquestion_helper::print_choose_qtype_to_add_form()
515 * @todo Final deprecation of this class in moodle 4.4 MDL-72438
517 function print_choose_qtype_to_add_form($hiddenparams, array $allowedqtypes = null, $enablejs = true) {
518 debugging('Function print_choose_qtype_to_add_form() is deprecated,
519 please use \qbank_editquestion\editquestion_helper::print_choose_qtype_to_add_form() instead.', DEBUG_DEVELOPER
);
520 global $CFG, $PAGE, $OUTPUT;
522 $chooser = \qbank_editquestion\qbank_chooser
::get($PAGE->course
, $hiddenparams, $allowedqtypes);
523 $renderer = $PAGE->get_renderer('question', 'bank');
525 return $renderer->render($chooser);
529 * Print a button for creating a new question. This will open question/addquestion.php,
530 * which in turn goes to question/question.php before getting back to $params['returnurl']
531 * (by default the question bank screen).
533 * @param int $categoryid The id of the category that the new question should be added to.
534 * @param array $params Other paramters to add to the URL. You need either $params['cmid'] or
535 * $params['courseid'], and you should probably set $params['returnurl']
536 * @param string $caption the text to display on the button.
537 * @param string $tooltip a tooltip to add to the button (optional).
538 * @param bool $disabled if true, the button will be disabled.
539 * @deprecated since Moodle 4.0
540 * @see \qbank_editquestion\editquestion_helper::create_new_question_button()
541 * @todo Final deprecation of this class in moodle 4.4 MDL-72438
543 function create_new_question_button($categoryid, $params, $caption, $tooltip = '', $disabled = false) {
544 debugging('Function create_new_question_button() has been deprecated and moved to bank/editquestion,
545 please use qbank_editquestion\editquestion_helper::create_new_question_button() instead.', DEBUG_DEVELOPER
);
546 global $CFG, $PAGE, $OUTPUT;
547 static $choiceformprinted = false;
548 $params['category'] = $categoryid;
549 $url = new moodle_url('/question/bank/editquestion/addquestion.php', $params);
550 echo $OUTPUT->single_button($url, $caption, 'get', array('disabled'=>$disabled, 'title'=>$tooltip));
552 if (!$choiceformprinted) {
553 echo '<div id="qtypechoicecontainer">';
554 echo print_choose_qtype_to_add_form(array());
556 $choiceformprinted = true;