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

/group/group_form.php

http://github.com/moodle/moodle
PHP | 204 lines | 120 code | 31 blank | 53 comment | 40 complexity | dc3bdf10f975658bdf0f8de5acd6e406 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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 the creation and editing of groups.
  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. defined('MOODLE_INTERNAL') || die;
  24. require_once($CFG->dirroot.'/lib/formslib.php');
  25. /**
  26. * Group form class
  27. *
  28. * @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. * @package core_group
  31. */
  32. class group_form extends moodleform {
  33. /**
  34. * Definition of the form
  35. */
  36. function definition () {
  37. global $USER, $CFG, $COURSE;
  38. $coursecontext = context_course::instance($COURSE->id);
  39. $mform =& $this->_form;
  40. $editoroptions = $this->_customdata['editoroptions'];
  41. $mform->addElement('header', 'general', get_string('general', 'form'));
  42. $mform->addElement('text','name', get_string('groupname', 'group'),'maxlength="254" size="50"');
  43. $mform->addRule('name', get_string('required'), 'required', null, 'client');
  44. $mform->setType('name', PARAM_TEXT);
  45. $mform->addElement('text','idnumber', get_string('idnumbergroup'), 'maxlength="100" size="10"');
  46. $mform->addHelpButton('idnumber', 'idnumbergroup');
  47. $mform->setType('idnumber', PARAM_RAW);
  48. if (!has_capability('moodle/course:changeidnumber', $coursecontext)) {
  49. $mform->hardFreeze('idnumber');
  50. }
  51. $mform->addElement('editor', 'description_editor', get_string('groupdescription', 'group'), null, $editoroptions);
  52. $mform->setType('description_editor', PARAM_RAW);
  53. $mform->addElement('passwordunmask', 'enrolmentkey', get_string('enrolmentkey', 'group'), 'maxlength="254" size="24"', get_string('enrolmentkey', 'group'));
  54. $mform->addHelpButton('enrolmentkey', 'enrolmentkey', 'group');
  55. $mform->setType('enrolmentkey', PARAM_RAW);
  56. // Group conversation messaging.
  57. if (\core_message\api::can_create_group_conversation($USER->id, $coursecontext)) {
  58. $mform->addElement('selectyesno', 'enablemessaging', get_string('enablemessaging', 'group'));
  59. $mform->addHelpButton('enablemessaging', 'enablemessaging', 'group');
  60. }
  61. $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
  62. $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
  63. $mform->setDefault('deletepicture', 0);
  64. $options = array(get_string('no'), get_string('yes'));
  65. $mform->addElement('select', 'hidepicture', get_string('hidepicture'), $options);
  66. $mform->addElement('filepicker', 'imagefile', get_string('newpicture', 'group'));
  67. $mform->addHelpButton('imagefile', 'newpicture', 'group');
  68. $mform->addElement('hidden','id');
  69. $mform->setType('id', PARAM_INT);
  70. $mform->addElement('hidden','courseid');
  71. $mform->setType('courseid', PARAM_INT);
  72. $this->add_action_buttons();
  73. }
  74. /**
  75. * Extend the form definition after the data has been parsed.
  76. */
  77. public function definition_after_data() {
  78. global $COURSE, $DB, $USER;
  79. $mform = $this->_form;
  80. $groupid = $mform->getElementValue('id');
  81. $coursecontext = context_course::instance($COURSE->id);
  82. if ($group = $DB->get_record('groups', array('id' => $groupid))) {
  83. // If can create group conversation then get if a conversation area exists and it is enabled.
  84. if (\core_message\api::can_create_group_conversation($USER->id, $coursecontext)) {
  85. if (\core_message\api::is_conversation_area_enabled('core_group', 'groups', $groupid, $coursecontext->id)) {
  86. $mform->getElement('enablemessaging')->setSelected(1);
  87. }
  88. }
  89. // Print picture.
  90. if (!($pic = print_group_picture($group, $COURSE->id, true, true, false))) {
  91. $pic = get_string('none');
  92. if ($mform->elementExists('deletepicture')) {
  93. $mform->removeElement('deletepicture');
  94. }
  95. }
  96. $imageelement = $mform->getElement('currentpicture');
  97. $imageelement->setValue($pic);
  98. } else {
  99. if ($mform->elementExists('currentpicture')) {
  100. $mform->removeElement('currentpicture');
  101. }
  102. if ($mform->elementExists('deletepicture')) {
  103. $mform->removeElement('deletepicture');
  104. }
  105. }
  106. }
  107. /**
  108. * Form validation
  109. *
  110. * @param array $data
  111. * @param array $files
  112. * @return array $errors An array of errors
  113. */
  114. function validation($data, $files) {
  115. global $COURSE, $DB, $CFG;
  116. $errors = parent::validation($data, $files);
  117. $name = trim($data['name']);
  118. if (isset($data['idnumber'])) {
  119. $idnumber = trim($data['idnumber']);
  120. } else {
  121. $idnumber = '';
  122. }
  123. if ($data['id'] and $group = $DB->get_record('groups', array('id'=>$data['id']))) {
  124. if (core_text::strtolower($group->name) != core_text::strtolower($name)) {
  125. if (groups_get_group_by_name($COURSE->id, $name)) {
  126. $errors['name'] = get_string('groupnameexists', 'group', $name);
  127. }
  128. }
  129. if (!empty($idnumber) && $group->idnumber != $idnumber) {
  130. if (groups_get_group_by_idnumber($COURSE->id, $idnumber)) {
  131. $errors['idnumber']= get_string('idnumbertaken');
  132. }
  133. }
  134. if ($data['enrolmentkey'] != '') {
  135. $errmsg = '';
  136. if (!empty($CFG->groupenrolmentkeypolicy) && $group->enrolmentkey !== $data['enrolmentkey']
  137. && !check_password_policy($data['enrolmentkey'], $errmsg)) {
  138. // Enforce password policy when the password is changed.
  139. $errors['enrolmentkey'] = $errmsg;
  140. } else {
  141. // Prevent twice the same enrolment key in course groups.
  142. $sql = "SELECT id FROM {groups} WHERE id <> :groupid AND courseid = :courseid AND enrolmentkey = :key";
  143. $params = array('groupid' => $data['id'], 'courseid' => $COURSE->id, 'key' => $data['enrolmentkey']);
  144. if ($DB->record_exists_sql($sql, $params)) {
  145. $errors['enrolmentkey'] = get_string('enrolmentkeyalreadyinuse', 'group');
  146. }
  147. }
  148. }
  149. } else if (groups_get_group_by_name($COURSE->id, $name)) {
  150. $errors['name'] = get_string('groupnameexists', 'group', $name);
  151. } else if (!empty($idnumber) && groups_get_group_by_idnumber($COURSE->id, $idnumber)) {
  152. $errors['idnumber']= get_string('idnumbertaken');
  153. } else if ($data['enrolmentkey'] != '') {
  154. $errmsg = '';
  155. if (!empty($CFG->groupenrolmentkeypolicy) && !check_password_policy($data['enrolmentkey'], $errmsg)) {
  156. // Enforce password policy.
  157. $errors['enrolmentkey'] = $errmsg;
  158. } else if ($DB->record_exists('groups', array('courseid' => $COURSE->id, 'enrolmentkey' => $data['enrolmentkey']))) {
  159. // Prevent the same enrolment key from being used multiple times in course groups.
  160. $errors['enrolmentkey'] = get_string('enrolmentkeyalreadyinuse', 'group');
  161. }
  162. }
  163. return $errors;
  164. }
  165. /**
  166. * Get editor options for this form
  167. *
  168. * @return array An array of options
  169. */
  170. function get_editor_options() {
  171. return $this->_customdata['editoroptions'];
  172. }
  173. }