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 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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  /**
  18   * Page for creating or editing course category name/parent/description.
  19   *
  20   * When called with an id parameter, edits the category with that id.
  21   * Otherwise it creates a new category with default parent from the parent
  22   * parameter, which may be 0.
  23   *
  24   * @package    core_course
  25   * @copyright  2007 Nicolas Connault
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  require_once('../config.php');
  30  require_once($CFG->dirroot.'/course/lib.php');
  31  
  32  require_login();
  33  
  34  $id = optional_param('id', 0, PARAM_INT);
  35  
  36  $url = new moodle_url('/course/editcategory.php');
  37  if ($id) {
  38      $coursecat = core_course_category::get($id, MUST_EXIST, true);
  39      $category = $coursecat->get_db_record();
  40      $context = context_coursecat::instance($id);
  41  
  42      $url->param('id', $id);
  43      $strtitle = new lang_string('editcategorysettings');
  44      $itemid = 0; // Initialise itemid, as all files in category description has item id 0.
  45      $title = $strtitle;
  46      $fullname = $coursecat->get_formatted_name();
  47  
  48  } else {
  49      $parent = required_param('parent', PARAM_INT);
  50      $url->param('parent', $parent);
  51      if ($parent) {
  52          $DB->record_exists('course_categories', array('id' => $parent), '*', MUST_EXIST);
  53          $context = context_coursecat::instance($parent);
  54      } else {
  55          $context = context_system::instance();
  56      }
  57      navigation_node::override_active_url(new moodle_url('/course/editcategory.php', array('parent' => $parent)));
  58  
  59      $category = new stdClass();
  60      $category->id = 0;
  61      $category->parent = $parent;
  62      $strtitle = new lang_string("addnewcategory");
  63      $itemid = null; // Set this explicitly, so files for parent category should not get loaded in draft area.
  64      $title = "$SITE->shortname: ".get_string('addnewcategory');
  65      $fullname = $SITE->fullname;
  66  }
  67  
  68  require_capability('moodle/category:manage', $context);
  69  
  70  $PAGE->set_context($context);
  71  $PAGE->set_url($url);
  72  $PAGE->set_pagelayout('admin');
  73  $PAGE->set_title($title);
  74  $PAGE->set_heading($fullname);
  75  
  76  $mform = new core_course_editcategory_form(null, array(
  77      'categoryid' => $id,
  78      'parent' => $category->parent,
  79      'context' => $context,
  80      'itemid' => $itemid
  81  ));
  82  $mform->set_data(file_prepare_standard_editor(
  83      $category,
  84      'description',
  85      $mform->get_description_editor_options(),
  86      $context,
  87      'coursecat',
  88      'description',
  89      $itemid
  90  ));
  91  
  92  $manageurl = new moodle_url('/course/management.php');
  93  if ($mform->is_cancelled()) {
  94      if ($id) {
  95          $manageurl->param('categoryid', $id);
  96      } else if ($parent) {
  97          $manageurl->param('categoryid', $parent);
  98      }
  99      redirect($manageurl);
 100  } else if ($data = $mform->get_data()) {
 101      if (isset($coursecat)) {
 102          if ((int)$data->parent !== (int)$coursecat->parent && !$coursecat->can_change_parent($data->parent)) {
 103              print_error('cannotmovecategory');
 104          }
 105          $coursecat->update($data, $mform->get_description_editor_options());
 106      } else {
 107          $category = core_course_category::create($data, $mform->get_description_editor_options());
 108      }
 109      $manageurl->param('categoryid', $category->id);
 110      redirect($manageurl);
 111  }
 112  
 113  echo $OUTPUT->header();
 114  echo $OUTPUT->heading($strtitle);
 115  $mform->display();
 116  echo $OUTPUT->footer();