Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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  namespace qbank_managecategories;
  18  
  19  /**
  20   * QUESTION_PAGE_LENGTH - Number of categories to display on page.
  21   */
  22  define('QUESTION_PAGE_LENGTH', 25);
  23  
  24  use context;
  25  use moodle_exception;
  26  use moodle_url;
  27  use qbank_managecategories\form\question_category_edit_form;
  28  use question_bank;
  29  use stdClass;
  30  
  31  /**
  32   * Class for performing operations on question categories.
  33   *
  34   * @package    qbank_managecategories
  35   * @copyright  1999 onwards Martin Dougiamas {@link http://moodle.com}
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class question_category_object {
  39  
  40      /**
  41       * @var array common language strings.
  42       */
  43      public $str;
  44  
  45      /**
  46       * @var array nested lists to display categories.
  47       */
  48      public $editlists = [];
  49  
  50      /**
  51       * @var string tab.
  52       */
  53      public $tab;
  54  
  55      /**
  56       * @var int tab size.
  57       */
  58      public $tabsize = 3;
  59  
  60      /**
  61       * @var moodle_url Object representing url for this page
  62       */
  63      public $pageurl;
  64  
  65      /**
  66       * @var question_category_edit_form Object representing form for adding / editing categories.
  67       */
  68      public $catform;
  69  
  70      /**
  71       * Constructor.
  72       *
  73       * @param int $page page number.
  74       * @param moodle_url $pageurl base URL of the display categories page. Used for redirects.
  75       * @param context[] $contexts contexts where the current user can edit categories.
  76       * @param int $currentcat id of the category to be edited. 0 if none.
  77       * @param int|null $defaultcategory id of the current category. null if none.
  78       * @param int $todelete id of the category to delete. 0 if none.
  79       * @param context[] $addcontexts contexts where the current user can add questions.
  80       */
  81      public function __construct($page, $pageurl, $contexts, $currentcat, $defaultcategory, $todelete, $addcontexts) {
  82  
  83          $this->tab = str_repeat('&nbsp;', $this->tabsize);
  84  
  85          $this->str = new stdClass();
  86          $this->str->course         = get_string('course');
  87          $this->str->category       = get_string('category', 'question');
  88          $this->str->categoryinfo   = get_string('categoryinfo', 'question');
  89          $this->str->questions      = get_string('questions', 'question');
  90          $this->str->add            = get_string('add');
  91          $this->str->delete         = get_string('delete');
  92          $this->str->moveup         = get_string('moveup');
  93          $this->str->movedown       = get_string('movedown');
  94          $this->str->edit           = get_string('editthiscategory', 'question');
  95          $this->str->hide           = get_string('hide');
  96          $this->str->order          = get_string('order');
  97          $this->str->parent         = get_string('parent', 'question');
  98          $this->str->add            = get_string('add');
  99          $this->str->action         = get_string('action');
 100          $this->str->top            = get_string('top');
 101          $this->str->addcategory    = get_string('addcategory', 'question');
 102          $this->str->editcategory   = get_string('editcategory', 'question');
 103          $this->str->cancel         = get_string('cancel');
 104          $this->str->editcategories = get_string('editcategories', 'question');
 105          $this->str->page           = get_string('page');
 106  
 107          $this->pageurl = $pageurl;
 108  
 109          $this->initialize($page, $contexts, $currentcat, $defaultcategory, $todelete, $addcontexts);
 110      }
 111  
 112      /**
 113       * Initializes this classes general category-related variables
 114       *
 115       * @param int $page page number.
 116       * @param context[] $contexts contexts where the current user can edit categories.
 117       * @param int $currentcat id of the category to be edited. 0 if none.
 118       * @param int|null $defaultcategory id of the current category. null if none.
 119       * @param int $todelete id of the category to delete. 0 if none.
 120       * @param context[] $addcontexts contexts where the current user can add questions.
 121       */
 122      public function initialize($page, $contexts, $currentcat, $defaultcategory, $todelete, $addcontexts): void {
 123          $lastlist = null;
 124          foreach ($contexts as $context) {
 125              $this->editlists[$context->id] =
 126                  new question_category_list('ul', '', true, $this->pageurl, $page, 'cpage', QUESTION_PAGE_LENGTH, $context);
 127              $this->editlists[$context->id]->lastlist =& $lastlist;
 128              if ($lastlist !== null) {
 129                  $lastlist->nextlist =& $this->editlists[$context->id];
 130              }
 131              $lastlist =& $this->editlists[$context->id];
 132          }
 133  
 134          $count = 1;
 135          $paged = false;
 136          foreach ($this->editlists as $key => $list) {
 137              list($paged, $count) = $this->editlists[$key]->list_from_records($paged, $count);
 138          }
 139          $this->catform = new question_category_edit_form($this->pageurl,
 140                  ['contexts' => $contexts, 'currentcat' => $currentcat ?? 0]);
 141          if (!$currentcat) {
 142              $this->catform->set_data(['parent' => $defaultcategory]);
 143          }
 144      }
 145  
 146      /**
 147       * Displays the user interface.
 148       *
 149       */
 150      public function display_user_interface(): void {
 151          // Interface for editing existing categories.
 152          $this->output_edit_lists();
 153      }
 154  
 155      /**
 156       * Outputs a table to allow entry of a new category
 157       */
 158      public function output_new_table(): void {
 159          $this->catform->display();
 160      }
 161  
 162      /**
 163       * Outputs a list to allow editing/rearranging of existing categories.
 164       *
 165       * $this->initialize() must have already been called
 166       *
 167       */
 168      public function output_edit_lists(): void {
 169          global $OUTPUT;
 170  
 171          echo $OUTPUT->heading_with_help(get_string('questioncategories', 'question'), 'editcategories', 'question');
 172  
 173          foreach ($this->editlists as $context => $list) {
 174              $listhtml = $list->to_html(0, ['str' => $this->str]);
 175              if ($listhtml) {
 176                  echo $OUTPUT->box_start('boxwidthwide boxaligncenter generalbox questioncategories contextlevel' .
 177                      $list->context->contextlevel);
 178                  $fullcontext = context::instance_by_id($context);
 179                  echo $OUTPUT->heading(get_string('questioncatsfor', 'question', $fullcontext->get_context_name()), 3);
 180                  echo $listhtml;
 181                  echo $OUTPUT->box_end();
 182              }
 183          }
 184          echo $list->display_page_numbers();
 185      }
 186  
 187      /**
 188       * Gets all the courseids for the given categories.
 189       *
 190       * @param array $categories contains category objects in  a tree representation
 191       * @return array courseids flat array in form categoryid=>courseid
 192       */
 193      public function get_course_ids(array $categories): array {
 194          $courseids = [];
 195          foreach ($categories as $key => $cat) {
 196              $courseids[$key] = $cat->course;
 197              if (!empty($cat->children)) {
 198                  $courseids = array_merge($courseids, $this->get_course_ids($cat->children));
 199              }
 200          }
 201          return $courseids;
 202      }
 203  
 204      /**
 205       * Edit a category, or add a new one if the id is zero.
 206       *
 207       * @param int $categoryid Category id.
 208       */
 209      public function edit_single_category(int $categoryid): void {
 210          // Interface for adding a new category.
 211          global $DB;
 212  
 213          if ($categoryid) {
 214              // Editing an existing category.
 215              $category = $DB->get_record("question_categories", ["id" => $categoryid], '*', MUST_EXIST);
 216              if ($category->parent == 0) {
 217                  throw new moodle_exception('cannotedittopcat', 'question', '', $categoryid);
 218              }
 219  
 220              $category->parent = "{$category->parent},{$category->contextid}";
 221              $category->submitbutton = get_string('savechanges');
 222              $category->categoryheader = $this->str->edit;
 223              $this->catform->set_data($category);
 224          }
 225  
 226          // Show the form.
 227          $this->catform->display();
 228      }
 229  
 230      /**
 231       * Sets the viable parents.
 232       *
 233       *  Viable parents are any except for the category itself, or any of it's descendants
 234       *  The parentstrings parameter is passed by reference and changed by this function.
 235       *
 236       * @param array $parentstrings a list of parentstrings
 237       * @param object $category Category object
 238       */
 239      public function set_viable_parents(array &$parentstrings, object $category): void {
 240  
 241          unset($parentstrings[$category->id]);
 242          if (isset($category->children)) {
 243              foreach ($category->children as $child) {
 244                  $this->set_viable_parents($parentstrings, $child);
 245              }
 246          }
 247      }
 248  
 249      /**
 250       * Gets question categories.
 251       *
 252       * @param int|null $parent - if given, restrict records to those with this parent id.
 253       * @param string $sort - [[sortfield [,sortfield]] {ASC|DESC}].
 254       * @return array categories.
 255       */
 256      public function get_question_categories(int $parent = null, string $sort = "sortorder ASC"): array {
 257          global $COURSE, $DB;
 258          if (is_null($parent)) {
 259              $categories = $DB->get_records('question_categories', ['course' => $COURSE->id], $sort);
 260          } else {
 261              $select = "parent = ? AND course = ?";
 262              $categories = $DB->get_records_select('question_categories', $select, [$parent, $COURSE->id], $sort);
 263          }
 264          return $categories;
 265      }
 266  
 267      /**
 268       * Deletes an existing question category.
 269       *
 270       * @param int $categoryid id of category to delete.
 271       */
 272      public function delete_category(int $categoryid): void {
 273          global $CFG, $DB;
 274          helper::question_can_delete_cat($categoryid);
 275          if (!$category = $DB->get_record("question_categories", ["id" => $categoryid])) {  // Security.
 276              throw new moodle_exception('unknowcategory');
 277          }
 278          // Send the children categories to live with their grandparent.
 279          $DB->set_field("question_categories", "parent", $category->parent, ["parent" => $category->id]);
 280  
 281          // Finally delete the category itself.
 282          $DB->delete_records("question_categories", ["id" => $category->id]);
 283  
 284          // Log the deletion of this category.
 285          $event = \core\event\question_category_deleted::create_from_question_category_instance($category);
 286          $event->add_record_snapshot('question_categories', $category);
 287          $event->trigger();
 288  
 289      }
 290  
 291      /**
 292       * Move questions and then delete the category.
 293       *
 294       * @param int $oldcat id of the old category.
 295       * @param int $newcat id of the new category.
 296       */
 297      public function move_questions_and_delete_category(int $oldcat, int $newcat): void {
 298          helper::question_can_delete_cat($oldcat);
 299          $this->move_questions($oldcat, $newcat);
 300          $this->delete_category($oldcat);
 301      }
 302  
 303      /**
 304       * Display the form to move a category.
 305       *
 306       * @param int $questionsincategory
 307       * @param object $category
 308       * @throws \coding_exception
 309       */
 310      public function display_move_form($questionsincategory, $category): void {
 311          global $OUTPUT;
 312          $vars = new stdClass();
 313          $vars->name = $category->name;
 314          $vars->count = $questionsincategory;
 315          echo $OUTPUT->box(get_string('categorymove', 'question', $vars), 'generalbox boxaligncenter');
 316          $this->moveform->display();
 317      }
 318  
 319      /**
 320       * Move questions to another category.
 321       *
 322       * @param int $oldcat id of the old category.
 323       * @param int $newcat id of the new category.
 324       * @throws \dml_exception
 325       */
 326      public function move_questions(int $oldcat, int $newcat): void {
 327          $questionids = $this->get_real_question_ids_in_category($oldcat);
 328          question_move_questions_to_category($questionids, $newcat);
 329      }
 330  
 331      /**
 332       * Create a new category.
 333       *
 334       * Data is expected to come from question_category_edit_form.
 335       *
 336       * By default redirects on success, unless $return is true.
 337       *
 338       * @param string $newparent 'categoryid,contextid' of the parent category.
 339       * @param string $newcategory the name.
 340       * @param string $newinfo the description.
 341       * @param bool $return if true, return rather than redirecting.
 342       * @param int|string $newinfoformat description format. One of the FORMAT_ constants.
 343       * @param null $idnumber the idnumber. '' is converted to null.
 344       * @return bool|int New category id if successful, else false.
 345       */
 346      public function add_category($newparent, $newcategory, $newinfo, $return = false, $newinfoformat = FORMAT_HTML,
 347              $idnumber = null): int {
 348          global $DB;
 349          if (empty($newcategory)) {
 350              throw new moodle_exception('categorynamecantbeblank', 'question');
 351          }
 352          list($parentid, $contextid) = explode(',', $newparent);
 353          // ...moodle_form makes sure select element output is legal no need for further cleaning.
 354          require_capability('moodle/question:managecategory', context::instance_by_id($contextid));
 355  
 356          if ($parentid) {
 357              if (!($DB->get_field('question_categories', 'contextid', ['id' => $parentid]) == $contextid)) {
 358                  throw new moodle_exception('cannotinsertquestioncatecontext', 'question', '',
 359                      ['cat' => $newcategory, 'ctx' => $contextid]);
 360              }
 361          }
 362  
 363          if ((string) $idnumber === '') {
 364              $idnumber = null;
 365          } else if (!empty($contextid)) {
 366              // While this check already exists in the form validation, this is a backstop preventing unnecessary errors.
 367              if ($DB->record_exists('question_categories',
 368                      ['idnumber' => $idnumber, 'contextid' => $contextid])) {
 369                  $idnumber = null;
 370              }
 371          }
 372  
 373          $cat = new stdClass();
 374          $cat->parent = $parentid;
 375          $cat->contextid = $contextid;
 376          $cat->name = $newcategory;
 377          $cat->info = $newinfo;
 378          $cat->infoformat = $newinfoformat;
 379          $cat->sortorder = 999;
 380          $cat->stamp = make_unique_id_code();
 381          $cat->idnumber = $idnumber;
 382          $categoryid = $DB->insert_record("question_categories", $cat);
 383  
 384          // Log the creation of this category.
 385          $category = new stdClass();
 386          $category->id = $categoryid;
 387          $category->contextid = $contextid;
 388          $event = \core\event\question_category_created::create_from_question_category_instance($category);
 389          $event->trigger();
 390  
 391          if ($return) {
 392              return $categoryid;
 393          } else {
 394              // Always redirect after successful action.
 395              redirect($this->pageurl);
 396          }
 397      }
 398  
 399      /**
 400       * Updates an existing category with given params.
 401       *
 402       * Warning! parameter order and meaning confusingly different from add_category in some ways!
 403       *
 404       * @param int $updateid id of the category to update.
 405       * @param int $newparent 'categoryid,contextid' of the parent category to set.
 406       * @param string $newname category name.
 407       * @param string $newinfo category description.
 408       * @param int|string $newinfoformat description format. One of the FORMAT_ constants.
 409       * @param int $idnumber the idnumber. '' is converted to null.
 410       * @param bool $redirect if true, will redirect once the DB is updated (default).
 411       */
 412      public function update_category($updateid, $newparent, $newname, $newinfo, $newinfoformat = FORMAT_HTML,
 413              $idnumber = null, $redirect = true): void {
 414          global $CFG, $DB;
 415          if (empty($newname)) {
 416              throw new moodle_exception('categorynamecantbeblank', 'question');
 417          }
 418  
 419          // Get the record we are updating.
 420          $oldcat = $DB->get_record('question_categories', ['id' => $updateid]);
 421          $lastcategoryinthiscontext = helper::question_is_only_child_of_top_category_in_context($updateid);
 422  
 423          if (!empty($newparent) && !$lastcategoryinthiscontext) {
 424              list($parentid, $tocontextid) = explode(',', $newparent);
 425          } else {
 426              $parentid = $oldcat->parent;
 427              $tocontextid = $oldcat->contextid;
 428          }
 429  
 430          // Check permissions.
 431          $fromcontext = context::instance_by_id($oldcat->contextid);
 432          require_capability('moodle/question:managecategory', $fromcontext);
 433  
 434          // If moving to another context, check permissions some more, and confirm contextid,stamp uniqueness.
 435          $newstamprequired = false;
 436          if ($oldcat->contextid != $tocontextid) {
 437              $tocontext = context::instance_by_id($tocontextid);
 438              require_capability('moodle/question:managecategory', $tocontext);
 439  
 440              // Confirm stamp uniqueness in the new context. If the stamp already exists, generate a new one.
 441              if ($DB->record_exists('question_categories', ['contextid' => $tocontextid, 'stamp' => $oldcat->stamp])) {
 442                  $newstamprequired = true;
 443              }
 444          }
 445  
 446          if ((string) $idnumber === '') {
 447              $idnumber = null;
 448          } else if (!empty($tocontextid)) {
 449              // While this check already exists in the form validation, this is a backstop preventing unnecessary errors.
 450              if ($DB->record_exists_select('question_categories',
 451                      'idnumber = ? AND contextid = ? AND id <> ?',
 452                      [$idnumber, $tocontextid, $updateid])) {
 453                  $idnumber = null;
 454              }
 455          }
 456  
 457          // Update the category record.
 458          $cat = new stdClass();
 459          $cat->id = $updateid;
 460          $cat->name = $newname;
 461          $cat->info = $newinfo;
 462          $cat->infoformat = $newinfoformat;
 463          $cat->parent = $parentid;
 464          $cat->contextid = $tocontextid;
 465          $cat->idnumber = $idnumber;
 466          if ($newstamprequired) {
 467              $cat->stamp = make_unique_id_code();
 468          }
 469          $DB->update_record('question_categories', $cat);
 470          // Update the set_reference records when moving a category to a different context.
 471          move_question_set_references($cat->id, $cat->id, $oldcat->contextid, $tocontextid);
 472  
 473          // Log the update of this category.
 474          $event = \core\event\question_category_updated::create_from_question_category_instance($cat);
 475          $event->trigger();
 476  
 477          if ($oldcat->contextid != $tocontextid) {
 478              // Moving to a new context. Must move files belonging to questions.
 479              question_move_category_to_context($cat->id, $oldcat->contextid, $tocontextid);
 480          }
 481  
 482          // Cat param depends on the context id, so update it.
 483          $this->pageurl->param('cat', $updateid . ',' . $tocontextid);
 484          if ($redirect) {
 485              // Always redirect after successful action.
 486              redirect($this->pageurl);
 487          }
 488      }
 489  
 490      /**
 491       * Returns ids of the question in the given question category.
 492       *
 493       * This method only returns the real question. It does not include
 494       * subquestions of question types like multianswer.
 495       *
 496       * @param int $categoryid id of the category.
 497       * @return int[] array of question ids.
 498       */
 499      public function get_real_question_ids_in_category(int $categoryid): array {
 500          global $DB;
 501  
 502          $sql = "SELECT q.id
 503                    FROM {question} q
 504                    JOIN {question_versions} qv ON qv.questionid = q.id
 505                    JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
 506                   WHERE qbe.questioncategoryid = :categoryid
 507                     AND (q.parent = 0 OR q.parent = q.id)";
 508  
 509          $questionids = $DB->get_records_sql($sql, ['categoryid' => $categoryid]);
 510          return array_keys($questionids);
 511      }
 512  }