PageRenderTime 58ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/course/classes/editcategory_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 134 lines | 73 code | 15 blank | 46 comment | 14 complexity | e980a2750a23bbb5771ca442898e2fcd 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. * Edit 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.'/coursecatlib.php');
  26. /**
  27. * Edit category form.
  28. *
  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_editcategory_form extends moodleform {
  34. /**
  35. * The form definition.
  36. */
  37. public function definition() {
  38. global $CFG, $DB;
  39. $mform = $this->_form;
  40. $categoryid = $this->_customdata['categoryid'];
  41. $parent = $this->_customdata['parent'];
  42. // Get list of categories to use as parents, with site as the first one.
  43. $options = array();
  44. if (has_capability('moodle/category:manage', context_system::instance()) || $parent == 0) {
  45. $options[0] = get_string('top');
  46. }
  47. if ($categoryid) {
  48. // Editing an existing category.
  49. $options += coursecat::make_categories_list('moodle/category:manage', $categoryid);
  50. if (empty($options[$parent])) {
  51. // Ensure the the category parent has been included in the options.
  52. $options[$parent] = $DB->get_field('course_categories', 'name', array('id'=>$parent));
  53. }
  54. $strsubmit = get_string('savechanges');
  55. } else {
  56. // Making a new category.
  57. $options += coursecat::make_categories_list('moodle/category:manage');
  58. $strsubmit = get_string('createcategory');
  59. }
  60. $mform->addElement('select', 'parent', get_string('parentcategory'), $options);
  61. $mform->addElement('text', 'name', get_string('categoryname'), array('size' => '30'));
  62. $mform->addRule('name', get_string('required'), 'required', null);
  63. $mform->setType('name', PARAM_TEXT);
  64. $mform->addElement('text', 'idnumber', get_string('idnumbercoursecategory'), 'maxlength="100" size="10"');
  65. $mform->addHelpButton('idnumber', 'idnumbercoursecategory');
  66. $mform->setType('idnumber', PARAM_RAW);
  67. $mform->addElement('editor', 'description_editor', get_string('description'), null,
  68. $this->get_description_editor_options());
  69. if (!empty($CFG->allowcategorythemes)) {
  70. $themes = array(''=>get_string('forceno'));
  71. $allthemes = get_list_of_themes();
  72. foreach ($allthemes as $key => $theme) {
  73. if (empty($theme->hidefromselector)) {
  74. $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
  75. }
  76. }
  77. $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
  78. }
  79. $mform->addElement('hidden', 'id', 0);
  80. $mform->setType('id', PARAM_INT);
  81. $mform->setDefault('id', $categoryid);
  82. $this->add_action_buttons(true, $strsubmit);
  83. }
  84. /**
  85. * Returns the description editor options.
  86. * @return array
  87. */
  88. public function get_description_editor_options() {
  89. global $CFG;
  90. $context = $this->_customdata['context'];
  91. $itemid = $this->_customdata['itemid'];
  92. return array(
  93. 'maxfiles' => EDITOR_UNLIMITED_FILES,
  94. 'maxbytes' => $CFG->maxbytes,
  95. 'trusttext' => true,
  96. 'context' => $context,
  97. 'subdirs' => file_area_contains_subdirs($context, 'coursecat', 'description', $itemid),
  98. );
  99. }
  100. /**
  101. * Validates the data submit for this form.
  102. *
  103. * @param array $data An array of key,value data pairs.
  104. * @param array $files Any files that may have been submit as well.
  105. * @return array An array of errors.
  106. */
  107. public function validation($data, $files) {
  108. global $DB;
  109. $errors = parent::validation($data, $files);
  110. if (!empty($data['idnumber'])) {
  111. if ($existing = $DB->get_record('course_categories', array('idnumber' => $data['idnumber']))) {
  112. if (!$data['id'] || $existing->id != $data['id']) {
  113. $errors['idnumber'] = get_string('categoryidnumbertaken', 'error');
  114. }
  115. }
  116. }
  117. return $errors;
  118. }
  119. }