Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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       * @deprecated No longer used by internal code and not recommended since Moodle 4.2 MDL-77299.
 311       */
 312      public function display_move_form($questionsincategory, $category): void {
 313          debugging(
 314              'display_move_form() is deprecated and no longer used by internal code.',
 315              DEBUG_DEVELOPER
 316          );
 317          global $OUTPUT;
 318          $vars = new stdClass();
 319          $vars->name = $category->name;
 320          $vars->count = $questionsincategory;
 321          echo $OUTPUT->box(get_string('categorymove', 'question', $vars), 'generalbox boxaligncenter');
 322          $this->moveform->display();
 323      }
 324  
 325      /**
 326       * Move questions to another category.
 327       *
 328       * @param int $oldcat id of the old category.
 329       * @param int $newcat id of the new category.
 330       * @throws \dml_exception
 331       */
 332      public function move_questions(int $oldcat, int $newcat): void {
 333          $questionids = $this->get_real_question_ids_in_category($oldcat);
 334          question_move_questions_to_category($questionids, $newcat);
 335      }
 336  
 337      /**
 338       * Create a new category.
 339       *
 340       * Data is expected to come from question_category_edit_form.
 341       *
 342       * By default redirects on success, unless $return is true.
 343       *
 344       * @param string $newparent 'categoryid,contextid' of the parent category.
 345       * @param string $newcategory the name.
 346       * @param string $newinfo the description.
 347       * @param bool $return if true, return rather than redirecting.
 348       * @param int|string $newinfoformat description format. One of the FORMAT_ constants.
 349       * @param null $idnumber the idnumber. '' is converted to null.
 350       * @return bool|int New category id if successful, else false.
 351       */
 352      public function add_category($newparent, $newcategory, $newinfo, $return = false, $newinfoformat = FORMAT_HTML,
 353              $idnumber = null): int {
 354          global $DB;
 355          if (empty($newcategory)) {
 356              throw new moodle_exception('categorynamecantbeblank', 'question');
 357          }
 358          list($parentid, $contextid) = explode(',', $newparent);
 359          // ...moodle_form makes sure select element output is legal no need for further cleaning.
 360          require_capability('moodle/question:managecategory', context::instance_by_id($contextid));
 361  
 362          if ($parentid) {
 363              if (!($DB->get_field('question_categories', 'contextid', ['id' => $parentid]) == $contextid)) {
 364                  throw new moodle_exception('cannotinsertquestioncatecontext', 'question', '',
 365                      ['cat' => $newcategory, 'ctx' => $contextid]);
 366              }
 367          }
 368  
 369          if ((string) $idnumber === '') {
 370              $idnumber = null;
 371          } else if (!empty($contextid)) {
 372              // While this check already exists in the form validation, this is a backstop preventing unnecessary errors.
 373              if ($DB->record_exists('question_categories',
 374                      ['idnumber' => $idnumber, 'contextid' => $contextid])) {
 375                  $idnumber = null;
 376              }
 377          }
 378  
 379          $cat = new stdClass();
 380          $cat->parent = $parentid;
 381          $cat->contextid = $contextid;
 382          $cat->name = $newcategory;
 383          $cat->info = $newinfo;
 384          $cat->infoformat = $newinfoformat;
 385          $cat->sortorder = 999;
 386          $cat->stamp = make_unique_id_code();
 387          $cat->idnumber = $idnumber;
 388          $categoryid = $DB->insert_record("question_categories", $cat);
 389  
 390          // Log the creation of this category.
 391          $category = new stdClass();
 392          $category->id = $categoryid;
 393          $category->contextid = $contextid;
 394          $event = \core\event\question_category_created::create_from_question_category_instance($category);
 395          $event->trigger();
 396  
 397          if ($return) {
 398              return $categoryid;
 399          } else {
 400              // Always redirect after successful action.
 401              redirect($this->pageurl);
 402          }
 403      }
 404  
 405      /**
 406       * Updates an existing category with given params.
 407       *
 408       * Warning! parameter order and meaning confusingly different from add_category in some ways!
 409       *
 410       * @param int $updateid id of the category to update.
 411       * @param int $newparent 'categoryid,contextid' of the parent category to set.
 412       * @param string $newname category name.
 413       * @param string $newinfo category description.
 414       * @param int|string $newinfoformat description format. One of the FORMAT_ constants.
 415       * @param int $idnumber the idnumber. '' is converted to null.
 416       * @param bool $redirect if true, will redirect once the DB is updated (default).
 417       */
 418      public function update_category($updateid, $newparent, $newname, $newinfo, $newinfoformat = FORMAT_HTML,
 419              $idnumber = null, $redirect = true): void {
 420          global $CFG, $DB;
 421          if (empty($newname)) {
 422              throw new moodle_exception('categorynamecantbeblank', 'question');
 423          }
 424  
 425          // Get the record we are updating.
 426          $oldcat = $DB->get_record('question_categories', ['id' => $updateid]);
 427          $lastcategoryinthiscontext = helper::question_is_only_child_of_top_category_in_context($updateid);
 428  
 429          if (!empty($newparent) && !$lastcategoryinthiscontext) {
 430              list($parentid, $tocontextid) = explode(',', $newparent);
 431          } else {
 432              $parentid = $oldcat->parent;
 433              $tocontextid = $oldcat->contextid;
 434          }
 435  
 436          // Check permissions.
 437          $fromcontext = context::instance_by_id($oldcat->contextid);
 438          require_capability('moodle/question:managecategory', $fromcontext);
 439  
 440          // If moving to another context, check permissions some more, and confirm contextid,stamp uniqueness.
 441          $newstamprequired = false;
 442          if ($oldcat->contextid != $tocontextid) {
 443              $tocontext = context::instance_by_id($tocontextid);
 444              require_capability('moodle/question:managecategory', $tocontext);
 445  
 446              // Confirm stamp uniqueness in the new context. If the stamp already exists, generate a new one.
 447              if ($DB->record_exists('question_categories', ['contextid' => $tocontextid, 'stamp' => $oldcat->stamp])) {
 448                  $newstamprequired = true;
 449              }
 450          }
 451  
 452          if ((string) $idnumber === '') {
 453              $idnumber = null;
 454          } else if (!empty($tocontextid)) {
 455              // While this check already exists in the form validation, this is a backstop preventing unnecessary errors.
 456              if ($DB->record_exists_select('question_categories',
 457                      'idnumber = ? AND contextid = ? AND id <> ?',
 458                      [$idnumber, $tocontextid, $updateid])) {
 459                  $idnumber = null;
 460              }
 461          }
 462  
 463          // Update the category record.
 464          $cat = new stdClass();
 465          $cat->id = $updateid;
 466          $cat->name = $newname;
 467          $cat->info = $newinfo;
 468          $cat->infoformat = $newinfoformat;
 469          $cat->parent = $parentid;
 470          $cat->contextid = $tocontextid;
 471          $cat->idnumber = $idnumber;
 472          if ($newstamprequired) {
 473              $cat->stamp = make_unique_id_code();
 474          }
 475          $DB->update_record('question_categories', $cat);
 476          // Update the set_reference records when moving a category to a different context.
 477          move_question_set_references($cat->id, $cat->id, $oldcat->contextid, $tocontextid);
 478  
 479          // Log the update of this category.
 480          $event = \core\event\question_category_updated::create_from_question_category_instance($cat);
 481          $event->trigger();
 482  
 483          if ($oldcat->contextid != $tocontextid) {
 484              // Moving to a new context. Must move files belonging to questions.
 485              question_move_category_to_context($cat->id, $oldcat->contextid, $tocontextid);
 486          }
 487  
 488          // Cat param depends on the context id, so update it.
 489          $this->pageurl->param('cat', $updateid . ',' . $tocontextid);
 490          if ($redirect) {
 491              // Always redirect after successful action.
 492              redirect($this->pageurl);
 493          }
 494      }
 495  
 496      /**
 497       * Returns ids of the question in the given question category.
 498       *
 499       * This method only returns the real question. It does not include
 500       * subquestions of question types like multianswer.
 501       *
 502       * @param int $categoryid id of the category.
 503       * @return int[] array of question ids.
 504       */
 505      public function get_real_question_ids_in_category(int $categoryid): array {
 506          global $DB;
 507  
 508          $sql = "SELECT q.id
 509                    FROM {question} q
 510                    JOIN {question_versions} qv ON qv.questionid = q.id
 511                    JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
 512                   WHERE qbe.questioncategoryid = :categoryid
 513                     AND (q.parent = 0 OR q.parent = q.id)";
 514  
 515          $questionids = $DB->get_records_sql($sql, ['categoryid' => $categoryid]);
 516          return array_keys($questionids);
 517      }
 518  }