[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/course/classes/ -> deletecategory_form.php (source)

   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   * Delete 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  require_once($CFG->libdir . '/questionlib.php');
  29  require_once($CFG->libdir . '/coursecatlib.php');
  30  
  31  /**
  32   * Delete category moodleform.
  33   * @package core_course
  34   * @copyright 2002 onwards Martin Dougiamas (http://dougiamas.com)
  35   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class core_course_deletecategory_form extends moodleform {
  38  
  39      /**
  40       * The coursecat object for that category being deleted.
  41       * @var coursecat
  42       */
  43      protected $coursecat;
  44  
  45      /**
  46       * Defines the form.
  47       */
  48      public function definition() {
  49          $mform = $this->_form;
  50          $this->coursecat = $this->_customdata;
  51  
  52          $categorycontext = context_coursecat::instance($this->coursecat->id);
  53          $categoryname = $this->coursecat->get_formatted_name();
  54  
  55          // Check permissions, to see if it OK to give the option to delete
  56          // the contents, rather than move elsewhere.
  57          $candeletecontent = $this->coursecat->can_delete_full();
  58  
  59          // Get the list of categories we might be able to move to.
  60          $displaylist = $this->coursecat->move_content_targets_list();
  61  
  62          // Now build the options.
  63          $options = array();
  64          if ($displaylist) {
  65              $options[0] = get_string('movecontentstoanothercategory');
  66          }
  67          if ($candeletecontent) {
  68              $options[1] = get_string('deleteallcannotundo');
  69          }
  70          if (empty($options)) {
  71              print_error('youcannotdeletecategory', 'error', 'index.php', $categoryname);
  72          }
  73  
  74          // Now build the form.
  75          $mform->addElement('header', 'general', get_string('categorycurrentcontents', '', $categoryname));
  76  
  77          // Describe the contents of this category.
  78          $contents = '';
  79          if ($this->coursecat->has_children()) {
  80              $contents .= '<li>' . get_string('subcategories') . '</li>';
  81          }
  82          if ($this->coursecat->has_courses()) {
  83              $contents .= '<li>' . get_string('courses') . '</li>';
  84          }
  85          if (question_context_has_any_questions($categorycontext)) {
  86              $contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>';
  87          }
  88          if (!empty($contents)) {
  89              $mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), html_writer::tag('ul', $contents));
  90          } else {
  91              $mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty'));
  92          }
  93  
  94          // Give the options for what to do.
  95          $mform->addElement('select', 'fulldelete', get_string('whattodo'), $options);
  96          if (count($options) == 1) {
  97              $optionkeys = array_keys($options);
  98              $option = reset($optionkeys);
  99              $mform->hardFreeze('fulldelete');
 100              $mform->setConstant('fulldelete', $option);
 101          }
 102  
 103          if ($displaylist) {
 104              $mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
 105              if (in_array($this->coursecat->parent, $displaylist)) {
 106                  $mform->setDefault('newparent', $this->coursecat->parent);
 107              }
 108              $mform->disabledIf('newparent', 'fulldelete', 'eq', '1');
 109          }
 110  
 111          $mform->addElement('hidden', 'categoryid', $this->coursecat->id);
 112          $mform->setType('categoryid', PARAM_ALPHANUM);
 113          $mform->addElement('hidden', 'action', 'deletecategory');
 114          $mform->setType('action', PARAM_ALPHANUM);
 115          $mform->addElement('hidden', 'sure');
 116          // This gets set by default to ensure that if the user changes it manually we can detect it.
 117          $mform->setDefault('sure', md5(serialize($this->coursecat)));
 118          $mform->setType('sure', PARAM_ALPHANUM);
 119  
 120          $this->add_action_buttons(true, get_string('delete'));
 121      }
 122  
 123      /**
 124       * Perform some extra moodle validation.
 125       *
 126       * @param array $data
 127       * @param array $files
 128       * @return array An array of errors.
 129       */
 130      public function validation($data, $files) {
 131          $errors = parent::validation($data, $files);
 132          if (empty($data['fulldelete']) && empty($data['newparent'])) {
 133              // When they have chosen the move option, they must specify a destination.
 134              $errors['newparent'] = get_string('required');
 135          }
 136  
 137          if ($data['sure'] !== md5(serialize($this->coursecat))) {
 138              $errors['categorylabel'] = get_string('categorymodifiedcancel');
 139          }
 140  
 141          return $errors;
 142      }
 143  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1