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 400 and 401]

   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  defined('MOODLE_INTERNAL') || die();
  20  
  21  require_once($CFG->libdir. '/listlib.php');
  22  require_once($CFG->dirroot . '/question/editlib.php');
  23  
  24  use stdClass;
  25  use moodle_list;
  26  
  27  /**
  28   * Class representing a list of question categories.
  29   *
  30   * @package    qbank_managecategories
  31   * @copyright  1999 onwards Martin Dougiamas {@link http://moodle.com}
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class question_category_list extends moodle_list {
  35  
  36      /**
  37       * Table name.
  38       * @var $table
  39       */
  40      public $table = "question_categories";
  41  
  42      /**
  43       * List item class name.
  44       * @var $listitemclassname
  45       */
  46      public $listitemclassname = '\qbank_managecategories\question_category_list_item';
  47  
  48      /**
  49       * Reference to list displayed below this one.
  50       * @var $nextlist
  51       */
  52      public $nextlist = null;
  53  
  54      /**
  55       * Reference to list displayed above this one.
  56       * @var $lastlist
  57       */
  58      public $lastlist = null;
  59  
  60      /**
  61       * Context.
  62       * @var $context
  63       */
  64      public $context = null;
  65  
  66      /**
  67       * Sort by string.
  68       * @var $sortby
  69       */
  70      public $sortby = 'parent, sortorder, name';
  71  
  72      /**
  73       * Constructor.
  74       *
  75       * @param string $type
  76       * @param string $attributes
  77       * @param boolean $editable
  78       * @param \moodle_url $pageurl url for this page
  79       * @param integer $page if 0 no pagination. (These three params only used in top level list.)
  80       * @param string $pageparamname name of url param that is used for passing page no
  81       * @param integer $itemsperpage no of top level items.
  82       * @param \context $context
  83       */
  84      public function __construct($type='ul', $attributes='', $editable = false, $pageurl=null,
  85                                  $page = 0, $pageparamname = 'page',
  86                                  $itemsperpage = DEFAULT_QUESTIONS_PER_PAGE, $context = null) {
  87          parent::__construct('ul', '', $editable, $pageurl, $page, 'cpage', $itemsperpage);
  88          $this->context = $context;
  89      }
  90  
  91      /**
  92       * Set the array of records of list items.
  93       */
  94      public function get_records() : void {
  95          $this->records = helper::get_categories_for_contexts($this->context->id, $this->sortby);
  96      }
  97  
  98      /**
  99       * Returns the highest category id that the $item can have as its parent.
 100       * Note: question categories cannot go higher than the TOP category.
 101       *
 102       * @param \list_item $item The item which its top level parent is going to be returned.
 103       * @return int
 104       */
 105      public function get_top_level_parent_id($item) : int {
 106          // Put the item at the highest level it can go.
 107          $topcategory = question_get_top_category($item->item->contextid, true);
 108          return $topcategory->id;
 109      }
 110  
 111      /**
 112       * Process any actions.
 113       *
 114       * @param integer $left id of item to move left
 115       * @param integer $right id of item to move right
 116       * @param integer $moveup id of item to move up
 117       * @param integer $movedown id of item to move down
 118       * @return void
 119       */
 120      public function process_actions($left, $right, $moveup, $movedown) : void {
 121          $category = new stdClass();
 122          if (!empty($left)) {
 123              // Moved Left (In to another category).
 124              $category->id = $left;
 125              $category->contextid = $this->context->id;
 126              $event = \core\event\question_category_moved::create_from_question_category_instance($category);
 127              $event->trigger();
 128          } else if (!empty($right)) {
 129              // Moved Right (Out of the current category).
 130              $category->id = $right;
 131              $category->contextid = $this->context->id;
 132              $event = \core\event\question_category_moved::create_from_question_category_instance($category);
 133              $event->trigger();
 134          }
 135          parent::process_actions($left, $right, $moveup, $movedown);
 136      }
 137  }