MDL-36088 questions: Add new events
[moodle.git] / question / category_class.php
CommitLineData
aeb15530 1<?php
d3603157
TH
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * A class for representing question categories.
19 *
20 * @package moodlecore
21 * @subpackage questionbank
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
516cf3eb 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
516cf3eb 29// number of categories to display on page
d3603157 30define('QUESTION_PAGE_LENGTH', 25);
dac786f3 31
94dbfb3a
TH
32require_once($CFG->libdir . '/listlib.php');
33require_once($CFG->dirroot . '/question/category_form.php');
017bc1d9
TH
34require_once($CFG->dirroot . '/question/move_form.php');
35
dac786f3 36
017bc1d9
TH
37/**
38 * Class representing a list of question categories
39 *
40 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
dac786f3 43class question_category_list extends moodle_list {
f34488b2 44 public $table = "question_categories";
45 public $listitemclassname = 'question_category_list_item';
271e6dec 46 /**
47 * @var reference to list displayed below this one.
48 */
f34488b2 49 public $nextlist = null;
271e6dec 50 /**
51 * @var reference to list displayed above this one.
52 */
f34488b2 53 public $lastlist = null;
271e6dec 54
f34488b2 55 public $context = null;
c65921d4 56 public $sortby = 'parent, sortorder, name';
271e6dec 57
20d89122
TH
58 public function __construct($type='ul', $attributes='', $editable = false, $pageurl=null, $page = 0, $pageparamname = 'page', $itemsperpage = 20, $context = null){
59 parent::__construct('ul', '', $editable, $pageurl, $page, 'cpage', $itemsperpage);
271e6dec 60 $this->context = $context;
61 }
986effb6 62
f34488b2 63 public function get_records() {
271e6dec 64 $this->records = get_categories_for_contexts($this->context->id, $this->sortby);
65 }
18d6ff78
SR
66
67 /**
68 * Returns the highest category id that the $item can have as its parent.
69 * Note: question categories cannot go higher than the TOP category.
70 *
71 * @param list_item $item The item which its top level parent is going to be returned.
72 * @return int
73 */
74 public function get_top_level_parent_id($item) {
75 // Put the item at the highest level it can go.
76 $topcategory = question_get_top_category($item->item->contextid, true);
77 return $topcategory->id;
78 }
93e435b9
SB
79
80 public function process_actions($left, $right, $moveup, $movedown) {
81 if (!empty($left)) {
82 // Moved Left (In to another category).
83 $params = array(
84 'objectid' => $left,
85 'contextid' => $this->context->id
86 );
87 $event = \core\event\question_category_moved::create($params);
88 $event->trigger();
89 } else if (!empty($right)) {
90 // Moved Right (Out of the current category).
91 $params = array(
92 'objectid' => $right,
93 'contextid' => $this->context->id
94 );
95 $event = \core\event\question_category_moved::create($params);
96 $event->trigger();
97 }
98 parent::process_actions($left, $right, $moveup, $movedown);
99 }
dac786f3 100}
101
017bc1d9
TH
102
103/**
104 * An item in a list of question categories.
105 *
106 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
107 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
108 */
dac786f3 109class question_category_list_item extends list_item {
e2bb3c92 110 public function set_icon_html($first, $last, $lastitem){
271e6dec 111 global $CFG;
112 $category = $this->item;
8afba50b
PS
113 $url = new moodle_url('/question/category.php', ($this->parentlist->pageurl->params() + array('edit'=>$category->id)));
114 $this->icons['edit']= $this->image_icon(get_string('editthiscategory', 'question'), $url, 'edit');
271e6dec 115 parent::set_icon_html($first, $last, $lastitem);
116 $toplevel = ($this->parentlist->parentitem === null);//this is a top level item
117 if (($this->parentlist->nextlist !== null) && $last && $toplevel && (count($this->parentlist->items)>1)){
8afba50b 118 $url = new moodle_url($this->parentlist->pageurl, array('movedowncontext'=>$this->id, 'tocontext'=>$this->parentlist->nextlist->context->id, 'sesskey'=>sesskey()));
f34488b2 119 $this->icons['down'] = $this->image_icon(
329846f1 120 get_string('shareincontext', 'question', $this->parentlist->nextlist->context->get_context_name()), $url, 'down');
271e6dec 121 }
122 if (($this->parentlist->lastlist !== null) && $first && $toplevel && (count($this->parentlist->items)>1)){
8afba50b 123 $url = new moodle_url($this->parentlist->pageurl, array('moveupcontext'=>$this->id, 'tocontext'=>$this->parentlist->lastlist->context->id, 'sesskey'=>sesskey()));
f34488b2 124 $this->icons['up'] = $this->image_icon(
329846f1 125 get_string('shareincontext', 'question', $this->parentlist->lastlist->context->get_context_name()), $url, 'up');
271e6dec 126 }
127 }
20d89122 128
f34488b2 129 public function item_html($extraargs = array()){
42d5737a 130 global $CFG, $OUTPUT;
dac786f3 131 $str = $extraargs['str'];
132 $category = $this->item;
133
5e8a85aa 134 $editqestions = get_string('editquestions', 'question');
dac786f3 135
8009810e
TH
136 // Each section adds html to be displayed as part of this list item.
137 $questionbankurl = new moodle_url('/question/edit.php', $this->parentlist->pageurl->params());
138 $questionbankurl->param('cat', $category->id . ',' . $category->contextid);
8009810e 139 $item = '';
2ea8fbb0
V
140 $text = format_string($category->name, true, ['context' => $this->parentlist->context])
141 . ' (' . $category->questioncount . ')';
142 $item .= html_writer::tag('b', html_writer::link($questionbankurl, $text,
143 ['title' => $editqestions]) . ' ');
8009810e 144 $item .= format_text($category->info, $category->infoformat,
fc51c2bc 145 array('context' => $this->parentlist->context, 'noclean' => true));
dac786f3 146
9275220d
SR
147 // Don't allow delete if this is the top category, or the last editable category in this context.
148 if ($category->parent && !question_is_only_child_of_top_category_in_context($category->id)) {
8009810e
TH
149 $deleteurl = new moodle_url($this->parentlist->pageurl, array('delete' => $this->id, 'sesskey' => sesskey()));
150 $item .= html_writer::link($deleteurl,
663640f5 151 $OUTPUT->pix_icon('t/delete', $str->delete),
8009810e 152 array('title' => $str->delete));
dac786f3 153 }
154
155 return $item;
dac786f3 156 }
dac786f3 157}
158
516cf3eb 159
4323d029 160/**
017bc1d9 161 * Class representing q question category
dac786f3 162 *
017bc1d9
TH
163 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
164 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4323d029 165 */
dc1f00de 166class question_category_object {
516cf3eb 167
dac786f3 168 /**
fa5c608c 169 * @var array common language strings.
dac786f3 170 */
fa5c608c
TH
171 public $str;
172
173 /**
174 * @var array nested lists to display categories.
175 */
176 public $editlists = array();
177 public $newtable;
178 public $tab;
179 public $tabsize = 3;
017bc1d9 180
986effb6 181 /**
182 * @var moodle_url Object representing url for this page
183 */
fa5c608c
TH
184 public $pageurl;
185
271e6dec 186 /**
187 * @var question_category_edit_form Object representing form for adding / editing categories.
188 */
fa5c608c 189 public $catform;
516cf3eb 190
bc649d80 191 /**
192 * Constructor
193 *
194 * Gets necessary strings and sets relevant path information
195 */
c4d0b752 196 public function __construct($page, $pageurl, $contexts, $currentcat, $defaultcategory, $todelete, $addcontexts) {
a5cb8d69 197 global $CFG, $COURSE, $OUTPUT;
516cf3eb 198
199 $this->tab = str_repeat('&nbsp;', $this->tabsize);
200
e2bb3c92 201 $this->str = new stdClass();
516cf3eb 202 $this->str->course = get_string('course');
5e8a85aa
TH
203 $this->str->category = get_string('category', 'question');
204 $this->str->categoryinfo = get_string('categoryinfo', 'question');
205 $this->str->questions = get_string('questions', 'question');
516cf3eb 206 $this->str->add = get_string('add');
207 $this->str->delete = get_string('delete');
208 $this->str->moveup = get_string('moveup');
209 $this->str->movedown = get_string('movedown');
85edde5c 210 $this->str->edit = get_string('editthiscategory', 'question');
516cf3eb 211 $this->str->hide = get_string('hide');
516cf3eb 212 $this->str->order = get_string('order');
5e8a85aa 213 $this->str->parent = get_string('parent', 'question');
516cf3eb 214 $this->str->add = get_string('add');
215 $this->str->action = get_string('action');
5e8a85aa
TH
216 $this->str->top = get_string('top');
217 $this->str->addcategory = get_string('addcategory', 'question');
218 $this->str->editcategory = get_string('editcategory', 'question');
516cf3eb 219 $this->str->cancel = get_string('cancel');
5e8a85aa 220 $this->str->editcategories = get_string('editcategories', 'question');
2befe778 221 $this->str->page = get_string('page');
516cf3eb 222
986effb6 223 $this->pageurl = $pageurl;
271e6dec 224
225 $this->initialize($page, $contexts, $currentcat, $defaultcategory, $todelete, $addcontexts);
516cf3eb 226 }
271e6dec 227
9e313e79
DM
228 /**
229 * Old syntax of class constructor. Deprecated in PHP7.
230 *
231 * @deprecated since Moodle 3.1
232 */
233 public function question_category_object($page, $pageurl, $contexts, $currentcat, $defaultcategory, $todelete, $addcontexts) {
234 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
235 self::__construct($page, $pageurl, $contexts, $currentcat, $defaultcategory, $todelete, $addcontexts);
236 }
237
271e6dec 238 /**
239 * Initializes this classes general category-related variables
240 */
f34488b2 241 public function initialize($page, $contexts, $currentcat, $defaultcategory, $todelete, $addcontexts) {
271e6dec 242 $lastlist = null;
243 foreach ($contexts as $context){
244 $this->editlists[$context->id] = new question_category_list('ul', '', true, $this->pageurl, $page, 'cpage', QUESTION_PAGE_LENGTH, $context);
245 $this->editlists[$context->id]->lastlist =& $lastlist;
246 if ($lastlist!== null){
247 $lastlist->nextlist =& $this->editlists[$context->id];
248 }
249 $lastlist =& $this->editlists[$context->id];
250 }
251
252 $count = 1;
253 $paged = false;
254 foreach ($this->editlists as $key => $list){
20f500dc 255 list($paged, $count) = $this->editlists[$key]->list_from_records($paged, $count);
271e6dec 256 }
257 $this->catform = new question_category_edit_form($this->pageurl, compact('contexts', 'currentcat'));
258 if (!$currentcat){
259 $this->catform->set_data(array('parent'=>$defaultcategory));
260 }
261 }
017bc1d9 262
bc649d80 263 /**
264 * Displays the user interface
265 *
bc649d80 266 */
f34488b2 267 public function display_user_interface() {
dac786f3 268
269 /// Interface for editing existing categories
271e6dec 270 $this->output_edit_lists();
dac786f3 271
516cf3eb 272
dac786f3 273 echo '<br />';
516cf3eb 274 /// Interface for adding a new category:
516cf3eb 275 $this->output_new_table();
276 echo '<br />';
277
516cf3eb 278 }
279
bc649d80 280 /**
281 * Outputs a table to allow entry of a new category
282 */
f34488b2 283 public function output_new_table() {
271e6dec 284 $this->catform->display();
516cf3eb 285 }
286
bc649d80 287 /**
dac786f3 288 * Outputs a list to allow editing/rearranging of existing categories
bc649d80 289 *
290 * $this->initialize() must have already been called
291 *
bc649d80 292 */
f34488b2 293 public function output_edit_lists() {
723d610c 294 global $OUTPUT;
1d58b567 295
5fcefc97 296 echo $OUTPUT->heading_with_help(get_string('editcategories', 'question'), 'editcategories', 'question');
1d58b567 297
271e6dec 298 foreach ($this->editlists as $context => $list){
299 $listhtml = $list->to_html(0, array('str'=>$this->str));
300 if ($listhtml){
beb677cd 301 echo $OUTPUT->box_start('boxwidthwide boxaligncenter generalbox questioncategories contextlevel' . $list->context->contextlevel);
329846f1
AG
302 $fullcontext = context::instance_by_id($context);
303 echo $OUTPUT->heading(get_string('questioncatsfor', 'question', $fullcontext->get_context_name()), 3);
271e6dec 304 echo $listhtml;
beb677cd 305 echo $OUTPUT->box_end();
271e6dec 306 }
307 }
308 echo $list->display_page_numbers();
309 }
516cf3eb 310
bc649d80 311 /**
312 * gets all the courseids for the given categories
313 *
314 * @param array categories contains category objects in a tree representation
315 * @return array courseids flat array in form categoryid=>courseid
316 */
f34488b2 317 public function get_course_ids($categories) {
516cf3eb 318 $courseids = array();
319 foreach ($categories as $key=>$cat) {
320 $courseids[$key] = $cat->course;
321 if (!empty($cat->children)) {
322 $courseids = array_merge($courseids, $this->get_course_ids($cat->children));
323 }
324 }
325 return $courseids;
326 }
327
f34488b2 328 public function edit_single_category($categoryid) {
516cf3eb 329 /// Interface for adding a new category
9275220d 330 global $DB;
516cf3eb 331 /// Interface for editing existing categories
9275220d
SR
332 $category = $DB->get_record("question_categories", array("id" => $categoryid));
333 if (empty($category)) {
334 print_error('invalidcategory', '', '', $categoryid);
335 } else if ($category->parent == 0) {
336 print_error('cannotedittopcat', 'question', '', $categoryid);
337 } else {
f4fe3968 338 $category->parent = "{$category->parent},{$category->contextid}";
271e6dec 339 $category->submitbutton = get_string('savechanges');
340 $category->categoryheader = $this->str->edit;
341 $this->catform->set_data($category);
342 $this->catform->display();
516cf3eb 343 }
516cf3eb 344 }
345
bc649d80 346 /**
347 * Sets the viable parents
348 *
349 * Viable parents are any except for the category itself, or any of it's descendants
350 * The parentstrings parameter is passed by reference and changed by this function.
351 *
352 * @param array parentstrings a list of parentstrings
353 * @param object category
354 */
f34488b2 355 public function set_viable_parents(&$parentstrings, $category) {
516cf3eb 356
357 unset($parentstrings[$category->id]);
358 if (isset($category->children)) {
359 foreach ($category->children as $child) {
360 $this->set_viable_parents($parentstrings, $child);
361 }
362 }
363 }
364
bc649d80 365 /**
366 * Gets question categories
367 *
368 * @param int parent - if given, restrict records to those with this parent id.
369 * @param string sort - [[sortfield [,sortfield]] {ASC|DESC}]
370 * @return array categories
371 */
f34488b2 372 public function get_question_categories($parent=null, $sort="sortorder ASC") {
373 global $COURSE, $DB;
516cf3eb 374 if (is_null($parent)) {
f34488b2 375 $categories = $DB->get_records('question_categories', array('course' => $COURSE->id), $sort);
516cf3eb 376 } else {
f34488b2 377 $select = "parent = ? AND course = ?";
378 $categories = $DB->get_records_select('question_categories', $select, array($parent, $COURSE->id), $sort);
516cf3eb 379 }
380 return $categories;
381 }
382
bc649d80 383 /**
384 * Deletes an existing question category
385 *
21cbf9da 386 * @param int deletecat id of category to delete
bc649d80 387 */
f34488b2 388 public function delete_category($categoryid) {
389 global $CFG, $DB;
271e6dec 390 question_can_delete_cat($categoryid);
f34488b2 391 if (!$category = $DB->get_record("question_categories", array("id" => $categoryid))) { // security
64e71d27 392 print_error('unknowcategory');
516cf3eb 393 }
516cf3eb 394 /// Send the children categories to live with their grandparent
bb4b6010 395 $DB->set_field("question_categories", "parent", $category->parent, array("parent" => $category->id));
516cf3eb 396
397 /// Finally delete the category itself
21cbf9da 398 $DB->delete_records("question_categories", array("id" => $category->id));
93e435b9
SB
399
400 // Log the deletion of this category.
401 $params = array(
402 'objectid' => $category->id,
403 'contextid' => $category->contextid
404 );
405 $event = \core\event\question_category_deleted::create($params);
406 $event->trigger();
407
516cf3eb 408 }
21cbf9da 409
f34488b2 410 public function move_questions_and_delete_category($oldcat, $newcat){
271e6dec 411 question_can_delete_cat($oldcat);
412 $this->move_questions($oldcat, $newcat);
413 $this->delete_category($oldcat);
414 }
516cf3eb 415
f34488b2 416 public function display_move_form($questionsincategory, $category){
beb677cd 417 global $OUTPUT;
0ff4bd08 418 $vars = new stdClass();
271e6dec 419 $vars->name = $category->name;
420 $vars->count = $questionsincategory;
5e8a85aa 421 echo $OUTPUT->box(get_string('categorymove', 'question', $vars), 'generalbox boxaligncenter');
271e6dec 422 $this->moveform->display();
423 }
516cf3eb 424
f34488b2 425 public function move_questions($oldcat, $newcat){
534792cd 426 global $DB;
5d548d3e
TH
427 $questionids = $DB->get_records_select_menu('question',
428 'category = ? AND (parent = 0 OR parent = id)', array($oldcat), '', 'id,1');
9d0ac0ff 429 question_move_questions_to_category(array_keys($questionids), $newcat);
516cf3eb 430 }
431
bc649d80 432 /**
433 * Creates a new category with given params
bc649d80 434 */
6189fda4
JB
435 public function add_category($newparent, $newcategory, $newinfo, $return = false, $newinfoformat = FORMAT_HTML,
436 $idnumber = null) {
f34488b2 437 global $DB;
bc649d80 438 if (empty($newcategory)) {
5e8a85aa 439 print_error('categorynamecantbeblank', 'question');
bc649d80 440 }
271e6dec 441 list($parentid, $contextid) = explode(',', $newparent);
442 //moodle_form makes sure select element output is legal no need for further cleaning
d197ea43 443 require_capability('moodle/question:managecategory', context::instance_by_id($contextid));
516cf3eb 444
271e6dec 445 if ($parentid) {
f34488b2 446 if(!($DB->get_field('question_categories', 'contextid', array('id' => $parentid)) == $contextid)) {
3e0e25ef 447 print_error('cannotinsertquestioncatecontext', 'question', '', array('cat'=>$newcategory, 'ctx'=>$contextid));
516cf3eb 448 }
449 }
450
2d696f8f
TH
451 if ((string) $idnumber === '') {
452 $idnumber = null;
453 } else if (!empty($contextid)) {
6189fda4
JB
454 // While this check already exists in the form validation, this is a backstop preventing unnecessary errors.
455 if ($DB->record_exists('question_categories',
456 ['idnumber' => $idnumber, 'contextid' => $contextid])) {
457 $idnumber = null;
458 }
459 }
460
7f389342 461 $cat = new stdClass();
271e6dec 462 $cat->parent = $parentid;
463 $cat->contextid = $contextid;
516cf3eb 464 $cat->name = $newcategory;
465 $cat->info = $newinfo;
fa5c608c 466 $cat->infoformat = $newinfoformat;
516cf3eb 467 $cat->sortorder = 999;
468 $cat->stamp = make_unique_id_code();
6189fda4
JB
469 if ($idnumber) {
470 $cat->idnumber = $idnumber;
471 }
bb4b6010 472 $categoryid = $DB->insert_record("question_categories", $cat);
58940b19
MN
473
474 // Log the creation of this category.
475 $params = array(
476 'objectid' => $categoryid,
477 'contextid' => $contextid
478 );
479 $event = \core\event\question_category_created::create($params);
480 $event->trigger();
481
bb4b6010 482 if ($return) {
483 return $categoryid;
516cf3eb 484 } else {
e42f153c 485 redirect($this->pageurl);//always redirect after successful action
516cf3eb 486 }
516cf3eb 487 }
488
bc649d80 489 /**
490 * Updates an existing category with given params
93e435b9
SB
491 *
492 * @param int $updateid
493 * @param int $newparent
494 * @param string $newname
495 * @param string $newinfo
496 * @param int $newinfoformat
497 * @param int $idnumber
498 * @param bool $redirect
499 * @return int
bc649d80 500 */
6189fda4 501 public function update_category($updateid, $newparent, $newname, $newinfo, $newinfoformat = FORMAT_HTML,
93e435b9 502 $idnumber = null, $redirect = true) {
f9b0500f 503 global $CFG, $DB;
271e6dec 504 if (empty($newname)) {
5e8a85aa 505 print_error('categorynamecantbeblank', 'question');
bc649d80 506 }
516cf3eb 507
41c96651 508 // Get the record we are updating.
f34488b2 509 $oldcat = $DB->get_record('question_categories', array('id' => $updateid));
9275220d 510 $lastcategoryinthiscontext = question_is_only_child_of_top_category_in_context($updateid);
41c96651 511
512 if (!empty($newparent) && !$lastcategoryinthiscontext) {
513 list($parentid, $tocontextid) = explode(',', $newparent);
514 } else {
515 $parentid = $oldcat->parent;
516 $tocontextid = $oldcat->contextid;
517 }
518
519 // Check permissions.
d197ea43 520 $fromcontext = context::instance_by_id($oldcat->contextid);
271e6dec 521 require_capability('moodle/question:managecategory', $fromcontext);
41c96651 522
d7d4a097
JD
523 // If moving to another context, check permissions some more, and confirm contextid,stamp uniqueness.
524 $newstamprequired = false;
5d548d3e 525 if ($oldcat->contextid != $tocontextid) {
d197ea43 526 $tocontext = context::instance_by_id($tocontextid);
271e6dec 527 require_capability('moodle/question:managecategory', $tocontext);
d7d4a097
JD
528
529 // Confirm stamp uniqueness in the new context. If the stamp already exists, generate a new one.
530 if ($DB->record_exists('question_categories', array('contextid' => $tocontextid, 'stamp' => $oldcat->stamp))) {
531 $newstamprequired = true;
532 }
271e6dec 533 }
41c96651 534
2d696f8f
TH
535 if ((string) $idnumber === '') {
536 $idnumber = null;
537 } else if (!empty($tocontextid)) {
6189fda4
JB
538 // While this check already exists in the form validation, this is a backstop preventing unnecessary errors.
539 if ($DB->record_exists('question_categories',
540 ['idnumber' => $idnumber, 'contextid' => $tocontextid])) {
541 $idnumber = null;
542 }
543 }
544
41c96651 545 // Update the category record.
9aa09d8f 546 $cat = new stdClass();
516cf3eb 547 $cat->id = $updateid;
271e6dec 548 $cat->name = $newname;
549 $cat->info = $newinfo;
fa5c608c 550 $cat->infoformat = $newinfoformat;
41c96651 551 $cat->parent = $parentid;
5d548d3e 552 $cat->contextid = $tocontextid;
6189fda4
JB
553 if ($idnumber) {
554 $cat->idnumber = $idnumber;
555 }
d7d4a097
JD
556 if ($newstamprequired) {
557 $cat->stamp = make_unique_id_code();
558 }
bb4b6010 559 $DB->update_record('question_categories', $cat);
f59dba84 560
93e435b9
SB
561 // Log the update of this category.
562 $params = array(
563 'objectid' => $cat->id,
564 'contextid' => $cat->contextid
565 );
566 $event = \core\event\question_category_updated::create($params);
567 $event->trigger();
568
df4e2244 569 // If the category name has changed, rename any random questions in that category.
41c96651 570 if ($oldcat->name != $cat->name) {
3cac440b 571 $where = "qtype = 'random' AND category = ? AND " . $DB->sql_compare_text('questiontext') . " = ?";
572
f9b0500f
TH
573 $randomqtype = question_bank::get_qtype('random');
574 $randomqname = $randomqtype->question_name($cat, false);
3cac440b 575 $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '0'));
576
f9b0500f 577 $randomqname = $randomqtype->question_name($cat, true);
3cac440b 578 $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '1'));
41c96651 579 }
580
5d548d3e
TH
581 if ($oldcat->contextid != $tocontextid) {
582 // Moving to a new context. Must move files belonging to questions.
583 question_move_category_to_context($cat->id, $oldcat->contextid, $tocontextid);
516cf3eb 584 }
271e6dec 585
9645fe83
TH
586 // Cat param depends on the context id, so update it.
587 $this->pageurl->param('cat', $updateid . ',' . $tocontextid);
93e435b9
SB
588 if ($redirect) {
589 redirect($this->pageurl); // Always redirect after successful action.
590 }
271e6dec 591 }
516cf3eb 592}