Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

   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   * Edit category form.
  19   *
  20   * @package core_course
  21   * @copyright 2002 onwards Martin Dougiamas (http://dougiamas.com)
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die;
  26  
  27  require_once($CFG->libdir.'/formslib.php');
  28  
  29  /**
  30   * Edit category form.
  31   *
  32   * @package core_course
  33   * @copyright 2002 onwards Martin Dougiamas (http://dougiamas.com)
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class core_course_editcategory_form extends moodleform {
  37  
  38      /**
  39       * The form definition.
  40       */
  41      public function definition() {
  42          global $CFG, $DB;
  43          $mform = $this->_form;
  44          $categoryid = $this->_customdata['categoryid'];
  45          $parent = $this->_customdata['parent'];
  46  
  47          // Get list of categories to use as parents, with site as the first one.
  48          $options = array();
  49          if (has_capability('moodle/category:manage', context_system::instance()) || $parent == 0) {
  50              $options[0] = get_string('top');
  51          }
  52          if ($categoryid) {
  53              // Editing an existing category.
  54              $options += core_course_category::make_categories_list('moodle/category:manage', $categoryid);
  55              if (empty($options[$parent])) {
  56                  // Ensure the the category parent has been included in the options.
  57                  $options[$parent] = $DB->get_field('course_categories', 'name', array('id'=>$parent));
  58              }
  59              $strsubmit = get_string('savechanges');
  60          } else {
  61              // Making a new category.
  62              $options += core_course_category::make_categories_list('moodle/category:manage');
  63              $strsubmit = get_string('createcategory');
  64          }
  65  
  66          $mform->addElement('autocomplete', 'parent', get_string('parentcategory'), $options);
  67          $mform->addRule('parent', null, 'required', null, 'client');
  68  
  69          $mform->addElement('text', 'name', get_string('categoryname'), array('size' => '30'));
  70          $mform->addRule('name', get_string('required'), 'required', null);
  71          $mform->setType('name', PARAM_TEXT);
  72  
  73          $mform->addElement('text', 'idnumber', get_string('idnumbercoursecategory'), 'maxlength="100" size="10"');
  74          $mform->addHelpButton('idnumber', 'idnumbercoursecategory');
  75          $mform->setType('idnumber', PARAM_RAW);
  76  
  77          $mform->addElement('editor', 'description_editor', get_string('description'), null,
  78              $this->get_description_editor_options());
  79          $mform->setType('description_editor', PARAM_RAW);
  80  
  81          if (!empty($CFG->allowcategorythemes)) {
  82              $themes = array(''=>get_string('forceno'));
  83              $allthemes = get_list_of_themes();
  84              foreach ($allthemes as $key => $theme) {
  85                  if (empty($theme->hidefromselector)) {
  86                      $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
  87                  }
  88              }
  89              $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
  90          }
  91  
  92          $mform->addElement('hidden', 'id', 0);
  93          $mform->setType('id', PARAM_INT);
  94          $mform->setDefault('id', $categoryid);
  95  
  96          $this->add_action_buttons(true, $strsubmit);
  97      }
  98  
  99      /**
 100       * Returns the description editor options.
 101       * @return array
 102       */
 103      public function get_description_editor_options() {
 104          global $CFG;
 105          $context = $this->_customdata['context'];
 106          $itemid = $this->_customdata['itemid'];
 107          return array(
 108              'maxfiles'  => EDITOR_UNLIMITED_FILES,
 109              'maxbytes'  => $CFG->maxbytes,
 110              'trusttext' => false,
 111              'noclean'   => true,
 112              'context'   => $context,
 113              'subdirs'   => file_area_contains_subdirs($context, 'coursecat', 'description', $itemid),
 114          );
 115      }
 116  
 117      /**
 118       * Validates the data submit for this form.
 119       *
 120       * @param array $data An array of key,value data pairs.
 121       * @param array $files Any files that may have been submit as well.
 122       * @return array An array of errors.
 123       */
 124      public function validation($data, $files) {
 125          global $DB;
 126          $errors = parent::validation($data, $files);
 127          if (!empty($data['idnumber'])) {
 128              if ($existing = $DB->get_record('course_categories', array('idnumber' => $data['idnumber']))) {
 129                  if (!$data['id'] || $existing->id != $data['id']) {
 130                      $errors['idnumber'] = get_string('categoryidnumbertaken', 'error');
 131                  }
 132              }
 133          }
 134          return $errors;
 135      }
 136  }