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.
   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\form;
  18  
  19  use moodleform;
  20  use qbank_managecategories\helper;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  require_once($CFG->libdir.'/formslib.php');
  25  
  26  
  27  /**
  28   * Defines the form for editing question categories.
  29   *
  30   * Form for editing questions categories (name, description, etc.)
  31   *
  32   * @package    qbank_managecategories
  33   * @copyright  2007 Jamie Pratt me@jamiep.org
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class question_category_edit_form extends moodleform {
  37  
  38      /**
  39       * Build the form definition.
  40       *
  41       * This adds all the form fields that the manage categories feature needs.
  42       * @throws \coding_exception
  43       */
  44      protected function definition() {
  45          $mform = $this->_form;
  46  
  47          $contexts = $this->_customdata['contexts'];
  48          $currentcat = $this->_customdata['currentcat'];
  49  
  50          $mform->addElement('header', 'categoryheader', get_string('addcategory', 'question'));
  51  
  52          $mform->addElement('questioncategory', 'parent', get_string('parentcategory', 'question'),
  53                  ['contexts' => $contexts, 'top' => true, 'currentcat' => $currentcat, 'nochildrenof' => $currentcat]);
  54          $mform->setType('parent', PARAM_SEQUENCE);
  55          if (helper::question_is_only_child_of_top_category_in_context($currentcat)) {
  56              $mform->hardFreeze('parent');
  57          }
  58          $mform->addHelpButton('parent', 'parentcategory', 'question');
  59  
  60          $mform->addElement('text', 'name', get_string('name'), 'maxlength="254" size="50"');
  61          $mform->setDefault('name', '');
  62          $mform->addRule('name', get_string('categorynamecantbeblank', 'question'), 'required', null, 'client');
  63          $mform->setType('name', PARAM_TEXT);
  64  
  65          $mform->addElement('editor', 'info', get_string('categoryinfo', 'question'),
  66                  ['rows' => 10], ['noclean' => 1]);
  67          $mform->setDefault('info', '');
  68          $mform->setType('info', PARAM_RAW);
  69  
  70          $mform->addElement('text', 'idnumber', get_string('idnumber', 'question'), 'maxlength="100"  size="10"');
  71          $mform->addHelpButton('idnumber', 'idnumber', 'question');
  72          $mform->setType('idnumber', PARAM_RAW);
  73  
  74          $this->add_action_buttons(true, get_string('addcategory', 'question'));
  75  
  76          $mform->addElement('hidden', 'id', 0);
  77          $mform->setType('id', PARAM_INT);
  78      }
  79  
  80      /**
  81       * Set data method.
  82       *
  83       * Add additional information to current data.
  84       * @param \stdClass|array $current Object or array of default current data.
  85       */
  86      public function set_data($current) {
  87          if (is_object($current)) {
  88              $current = (array) $current;
  89          }
  90          if (!empty($current['info'])) {
  91              $current['info'] = ['text' => $current['info'], 'infoformat' => $current['infoformat']];
  92          } else {
  93              $current['info'] = ['text' => '', 'infoformat' => FORMAT_HTML];
  94          }
  95          parent::set_data($current);
  96      }
  97  
  98      /**
  99       * Validation.
 100       *
 101       * @param array $data
 102       * @param array $files
 103       * @return array the errors that were found
 104       * @throws \dml_exception|\coding_exception
 105       */
 106      public function validation($data, $files) {
 107          global $DB;
 108  
 109          $errors = parent::validation($data, $files);
 110  
 111          // Add field validation check for duplicate idnumber.
 112          list($parentid, $contextid) = explode(',', $data['parent']);
 113          if (((string) $data['idnumber'] !== '') && !empty($contextid)) {
 114              $conditions = 'contextid = ? AND idnumber = ?';
 115              $params = [$contextid, $data['idnumber']];
 116              if (!empty($data['id'])) {
 117                  $conditions .= ' AND id <> ?';
 118                  $params[] = $data['id'];
 119              }
 120              if ($DB->record_exists_select('question_categories', $conditions, $params)) {
 121                  $errors['idnumber'] = get_string('idnumbertaken', 'error');
 122              }
 123          }
 124  
 125          return $errors;
 126      }
 127  }