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

/group/grouping_form.php

https://bitbucket.org/moodle/moodle
PHP | 115 lines | 57 code | 20 blank | 38 comment | 17 complexity | f6d187d0aa4444393eb807eba5510a76 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-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. * A form for creating and editing groupings.
  18. *
  19. * @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package core_group
  22. */
  23. if (!defined('MOODLE_INTERNAL')) {
  24. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  25. }
  26. require_once($CFG->dirroot.'/lib/formslib.php');
  27. /**
  28. * Grouping form class
  29. *
  30. * @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. * @package core_group
  33. */
  34. class grouping_form extends moodleform {
  35. /**
  36. * Form definition
  37. */
  38. function definition () {
  39. global $USER, $CFG, $COURSE;
  40. $coursecontext = context_course::instance($COURSE->id);
  41. $mform =& $this->_form;
  42. $editoroptions = $this->_customdata['editoroptions'];
  43. $mform->addElement('header', 'general', get_string('general', 'form'));
  44. $mform->addElement('text','name', get_string('groupingname', 'group'),'maxlength="254" size="50"');
  45. $mform->addRule('name', get_string('required'), 'required', null, 'server');
  46. $mform->setType('name', PARAM_TEXT);
  47. $mform->addElement('text','idnumber', get_string('idnumbergrouping'), 'maxlength="100" size="10"');
  48. $mform->addHelpButton('idnumber', 'idnumbergrouping');
  49. $mform->setType('idnumber', PARAM_RAW);
  50. if (!has_capability('moodle/course:changeidnumber', $coursecontext)) {
  51. $mform->hardFreeze('idnumber');
  52. }
  53. $mform->addElement('editor', 'description_editor', get_string('groupingdescription', 'group'), null, $editoroptions);
  54. $mform->setType('description_editor', PARAM_RAW);
  55. $mform->addElement('hidden','id');
  56. $mform->setType('id', PARAM_INT);
  57. $mform->addElement('hidden', 'courseid');
  58. $mform->setType('courseid', PARAM_INT);
  59. $this->add_action_buttons();
  60. }
  61. /**
  62. * Form validation
  63. *
  64. * @param array $data
  65. * @param array $files
  66. * @return array $errors An array of validataion errors for the form.
  67. */
  68. function validation($data, $files) {
  69. global $COURSE, $DB;
  70. $errors = parent::validation($data, $files);
  71. $name = trim($data['name']);
  72. if (isset($data['idnumber'])) {
  73. $idnumber = trim($data['idnumber']);
  74. } else {
  75. $idnumber = '';
  76. }
  77. if ($data['id'] and $grouping = $DB->get_record('groupings', array('id'=>$data['id']))) {
  78. if (core_text::strtolower($grouping->name) != core_text::strtolower($name)) {
  79. if (groups_get_grouping_by_name($COURSE->id, $name)) {
  80. $errors['name'] = get_string('groupingnameexists', 'group', $name);
  81. }
  82. }
  83. if (!empty($idnumber) && $grouping->idnumber != $idnumber) {
  84. if (groups_get_grouping_by_idnumber($COURSE->id, $idnumber)) {
  85. $errors['idnumber']= get_string('idnumbertaken');
  86. }
  87. }
  88. } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
  89. $errors['name'] = get_string('groupingnameexists', 'group', $name);
  90. } else if (!empty($idnumber) && groups_get_grouping_by_idnumber($COURSE->id, $idnumber)) {
  91. $errors['idnumber']= get_string('idnumbertaken');
  92. }
  93. return $errors;
  94. }
  95. }