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 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [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  /**
  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      navigation_node::override_active_url(new moodle_url('/course/index.php', ['categoryid' => $category->id]));
  42      $PAGE->navbar->add(get_string('settings'));
  43      $PAGE->set_primary_active_tab('home');
  44      $PAGE->set_secondary_active_tab('edit');
  45  
  46      $url->param('id', $id);
  47      $strtitle = new lang_string('editcategorysettings');
  48      $itemid = 0; // Initialise itemid, as all files in category description has item id 0.
  49      $title = $strtitle;
  50      $fullname = $coursecat->get_formatted_name();
  51  
  52  } else {
  53      $parent = required_param('parent', PARAM_INT);
  54      $url->param('parent', $parent);
  55      $strtitle = get_string('addnewcategory');
  56      if ($parent) {
  57          $parentcategory = $DB->get_record('course_categories', array('id' => $parent), '*', MUST_EXIST);
  58          $context = context_coursecat::instance($parent);
  59          navigation_node::override_active_url(new moodle_url('/course/index.php', ['categoryid' => $parent]));
  60          $fullname = format_string($parentcategory->name, true, ['context' => $context->id]);
  61          $title = "$fullname: $strtitle";
  62          $managementurl = new moodle_url('/course/management.php');
  63          // These are the caps required in order to see the management interface.
  64          $managementcaps = array('moodle/category:manage', 'moodle/course:create');
  65          if (!has_any_capability($managementcaps, context_system::instance())) {
  66              // If the user doesn't have either manage caps then they can only manage within the given category.
  67              $managementurl->param('categoryid', $parent);
  68          }
  69          $PAGE->set_primary_active_tab('home');
  70          $PAGE->navbar->add(get_string('coursemgmt', 'admin'), $managementurl);
  71          $PAGE->navbar->add(get_string('addcategory', 'admin'));
  72      } else {
  73          $context = context_system::instance();
  74          $fullname = $SITE->fullname;
  75          $title = $strtitle;
  76          $PAGE->set_secondary_active_tab('courses');
  77      }
  78  
  79      $category = new stdClass();
  80      $category->id = 0;
  81      $category->parent = $parent;
  82      $itemid = null; // Set this explicitly, so files for parent category should not get loaded in draft area.
  83  }
  84  
  85  require_capability('moodle/category:manage', $context);
  86  
  87  $PAGE->set_context($context);
  88  $PAGE->set_url($url);
  89  $PAGE->set_pagelayout('admin');
  90  $PAGE->set_title($title);
  91  $PAGE->set_heading($fullname);
  92  
  93  $mform = new core_course_editcategory_form(null, array(
  94      'categoryid' => $id,
  95      'parent' => $category->parent,
  96      'context' => $context,
  97      'itemid' => $itemid
  98  ));
  99  $mform->set_data(file_prepare_standard_editor(
 100      $category,
 101      'description',
 102      $mform->get_description_editor_options(),
 103      $context,
 104      'coursecat',
 105      'description',
 106      $itemid
 107  ));
 108  
 109  $manageurl = new moodle_url('/course/management.php');
 110  if ($mform->is_cancelled()) {
 111      if ($id) {
 112          $manageurl->param('categoryid', $id);
 113      } else if ($parent) {
 114          $manageurl->param('categoryid', $parent);
 115      }
 116      redirect($manageurl);
 117  } else if ($data = $mform->get_data()) {
 118      if (isset($coursecat)) {
 119          if ((int)$data->parent !== (int)$coursecat->parent && !$coursecat->can_change_parent($data->parent)) {
 120              throw new \moodle_exception('cannotmovecategory');
 121          }
 122          $coursecat->update($data, $mform->get_description_editor_options());
 123      } else {
 124          $category = core_course_category::create($data, $mform->get_description_editor_options());
 125      }
 126      $manageurl->param('categoryid', $category->id);
 127      redirect($manageurl);
 128  }
 129  
 130  echo $OUTPUT->header();
 131  echo $OUTPUT->heading($strtitle);
 132  $mform->display();
 133  echo $OUTPUT->footer();