PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/course/classes/deletecategory_form.php

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