PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/group/group_form.php

https://bitbucket.org/moodle/moodle
PHP | 201 lines | 118 code | 30 blank | 53 comment | 40 complexity | 821a1ac90f780f1c7529c6fdb255cc26 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 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. $mform->addElement('filepicker', 'imagefile', get_string('newpicture', 'group'));
  65. $mform->addHelpButton('imagefile', 'newpicture', 'group');
  66. $mform->addElement('hidden','id');
  67. $mform->setType('id', PARAM_INT);
  68. $mform->addElement('hidden','courseid');
  69. $mform->setType('courseid', PARAM_INT);
  70. $this->add_action_buttons();
  71. }
  72. /**
  73. * Extend the form definition after the data has been parsed.
  74. */
  75. public function definition_after_data() {
  76. global $COURSE, $DB, $USER;
  77. $mform = $this->_form;
  78. $groupid = $mform->getElementValue('id');
  79. $coursecontext = context_course::instance($COURSE->id);
  80. if ($group = $DB->get_record('groups', array('id' => $groupid))) {
  81. // If can create group conversation then get if a conversation area exists and it is enabled.
  82. if (\core_message\api::can_create_group_conversation($USER->id, $coursecontext)) {
  83. if (\core_message\api::is_conversation_area_enabled('core_group', 'groups', $groupid, $coursecontext->id)) {
  84. $mform->getElement('enablemessaging')->setSelected(1);
  85. }
  86. }
  87. // Print picture.
  88. if (!($pic = print_group_picture($group, $COURSE->id, true, true, false))) {
  89. $pic = get_string('none');
  90. if ($mform->elementExists('deletepicture')) {
  91. $mform->removeElement('deletepicture');
  92. }
  93. }
  94. $imageelement = $mform->getElement('currentpicture');
  95. $imageelement->setValue($pic);
  96. } else {
  97. if ($mform->elementExists('currentpicture')) {
  98. $mform->removeElement('currentpicture');
  99. }
  100. if ($mform->elementExists('deletepicture')) {
  101. $mform->removeElement('deletepicture');
  102. }
  103. }
  104. }
  105. /**
  106. * Form validation
  107. *
  108. * @param array $data
  109. * @param array $files
  110. * @return array $errors An array of errors
  111. */
  112. function validation($data, $files) {
  113. global $COURSE, $DB, $CFG;
  114. $errors = parent::validation($data, $files);
  115. $name = trim($data['name']);
  116. if (isset($data['idnumber'])) {
  117. $idnumber = trim($data['idnumber']);
  118. } else {
  119. $idnumber = '';
  120. }
  121. if ($data['id'] and $group = $DB->get_record('groups', array('id'=>$data['id']))) {
  122. if (core_text::strtolower($group->name) != core_text::strtolower($name)) {
  123. if (groups_get_group_by_name($COURSE->id, $name)) {
  124. $errors['name'] = get_string('groupnameexists', 'group', $name);
  125. }
  126. }
  127. if (!empty($idnumber) && $group->idnumber != $idnumber) {
  128. if (groups_get_group_by_idnumber($COURSE->id, $idnumber)) {
  129. $errors['idnumber']= get_string('idnumbertaken');
  130. }
  131. }
  132. if ($data['enrolmentkey'] != '') {
  133. $errmsg = '';
  134. if (!empty($CFG->groupenrolmentkeypolicy) && $group->enrolmentkey !== $data['enrolmentkey']
  135. && !check_password_policy($data['enrolmentkey'], $errmsg)) {
  136. // Enforce password policy when the password is changed.
  137. $errors['enrolmentkey'] = $errmsg;
  138. } else {
  139. // Prevent twice the same enrolment key in course groups.
  140. $sql = "SELECT id FROM {groups} WHERE id <> :groupid AND courseid = :courseid AND enrolmentkey = :key";
  141. $params = array('groupid' => $data['id'], 'courseid' => $COURSE->id, 'key' => $data['enrolmentkey']);
  142. if ($DB->record_exists_sql($sql, $params)) {
  143. $errors['enrolmentkey'] = get_string('enrolmentkeyalreadyinuse', 'group');
  144. }
  145. }
  146. }
  147. } else if (groups_get_group_by_name($COURSE->id, $name)) {
  148. $errors['name'] = get_string('groupnameexists', 'group', $name);
  149. } else if (!empty($idnumber) && groups_get_group_by_idnumber($COURSE->id, $idnumber)) {
  150. $errors['idnumber']= get_string('idnumbertaken');
  151. } else if ($data['enrolmentkey'] != '') {
  152. $errmsg = '';
  153. if (!empty($CFG->groupenrolmentkeypolicy) && !check_password_policy($data['enrolmentkey'], $errmsg)) {
  154. // Enforce password policy.
  155. $errors['enrolmentkey'] = $errmsg;
  156. } else if ($DB->record_exists('groups', array('courseid' => $COURSE->id, 'enrolmentkey' => $data['enrolmentkey']))) {
  157. // Prevent the same enrolment key from being used multiple times in course groups.
  158. $errors['enrolmentkey'] = get_string('enrolmentkeyalreadyinuse', 'group');
  159. }
  160. }
  161. return $errors;
  162. }
  163. /**
  164. * Get editor options for this form
  165. *
  166. * @return array An array of options
  167. */
  168. function get_editor_options() {
  169. return $this->_customdata['editoroptions'];
  170. }
  171. }