Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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