Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]
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 18 /** 19 * A form for the creation and editing of groups. 20 * 21 * @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 * @package core_group 24 */ 25 26 defined('MOODLE_INTERNAL') || die; 27 28 use core_group\visibility; 29 30 require_once($CFG->dirroot.'/lib/formslib.php'); 31 32 /** 33 * Group form class 34 * 35 * @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 * @package core_group 38 */ 39 class group_form extends moodleform { 40 41 /** 42 * Definition of the form 43 */ 44 function definition () { 45 global $USER, $CFG, $COURSE; 46 $coursecontext = context_course::instance($COURSE->id); 47 48 $mform =& $this->_form; 49 $editoroptions = $this->_customdata['editoroptions']; 50 $group = $this->_customdata['group']; 51 52 $mform->addElement('header', 'general', get_string('general', 'form')); 53 54 $mform->addElement('text','name', get_string('groupname', 'group'),'maxlength="254" size="50"'); 55 $mform->addRule('name', get_string('required'), 'required', null, 'client'); 56 $mform->setType('name', PARAM_TEXT); 57 58 $mform->addElement('text','idnumber', get_string('idnumbergroup'), 'maxlength="100" size="10"'); 59 $mform->addHelpButton('idnumber', 'idnumbergroup'); 60 $mform->setType('idnumber', PARAM_RAW); 61 if (!has_capability('moodle/course:changeidnumber', $coursecontext)) { 62 $mform->hardFreeze('idnumber'); 63 } 64 65 $mform->addElement('editor', 'description_editor', get_string('groupdescription', 'group'), null, $editoroptions); 66 $mform->setType('description_editor', PARAM_RAW); 67 68 $mform->addElement('passwordunmask', 'enrolmentkey', get_string('enrolmentkey', 'group'), 'maxlength="254" size="24"', get_string('enrolmentkey', 'group')); 69 $mform->addHelpButton('enrolmentkey', 'enrolmentkey', 'group'); 70 $mform->setType('enrolmentkey', PARAM_RAW); 71 72 $visibilityoptions = [ 73 GROUPS_VISIBILITY_ALL => get_string('visibilityall', 'group'), 74 GROUPS_VISIBILITY_MEMBERS => get_string('visibilitymembers', 'group'), 75 GROUPS_VISIBILITY_OWN => get_string('visibilityown', 'group'), 76 GROUPS_VISIBILITY_NONE => get_string('visibilitynone', 'group') 77 ]; 78 $mform->addElement('select', 'visibility', get_string('visibility', 'group'), $visibilityoptions); 79 $mform->addHelpButton('visibility', 'visibility', 'group'); 80 $mform->setType('visibility', PARAM_INT); 81 82 $mform->addElement('advcheckbox', 'participation', '', get_string('participation', 'group')); 83 $mform->addHelpButton('participation', 'participation', 'group'); 84 $mform->setType('participation', PARAM_BOOL); 85 $mform->setDefault('participation', 1); 86 $mform->hideIf('participation', 'visibility', 'in', [GROUPS_VISIBILITY_OWN, GROUPS_VISIBILITY_NONE]); 87 88 // Group conversation messaging. 89 if (\core_message\api::can_create_group_conversation($USER->id, $coursecontext)) { 90 $mform->addElement('selectyesno', 'enablemessaging', get_string('enablemessaging', 'group')); 91 $mform->addHelpButton('enablemessaging', 'enablemessaging', 'group'); 92 $mform->hideIf('enablemessaging', 'visibility', 'in', [GROUPS_VISIBILITY_OWN, GROUPS_VISIBILITY_NONE]); 93 } 94 95 $mform->addElement('static', 'currentpicture', get_string('currentpicture')); 96 97 $mform->addElement('checkbox', 'deletepicture', get_string('delete')); 98 $mform->setDefault('deletepicture', 0); 99 100 $mform->addElement('filepicker', 'imagefile', get_string('newpicture', 'group')); 101 $mform->addHelpButton('imagefile', 'newpicture', 'group'); 102 103 $handler = \core_group\customfield\group_handler::create(); 104 $handler->instance_form_definition($mform, empty($group->id) ? 0 : $group->id); 105 $handler->instance_form_before_set_data($group); 106 107 $mform->addElement('hidden','id'); 108 $mform->setType('id', PARAM_INT); 109 110 $mform->addElement('hidden','courseid'); 111 $mform->setType('courseid', PARAM_INT); 112 113 $this->add_action_buttons(); 114 } 115 116 /** 117 * Extend the form definition after the data has been parsed. 118 */ 119 public function definition_after_data() { 120 global $COURSE, $DB, $USER; 121 122 $mform = $this->_form; 123 $groupid = $mform->getElementValue('id'); 124 $coursecontext = context_course::instance($COURSE->id); 125 126 if ($group = $DB->get_record('groups', array('id' => $groupid))) { 127 // If can create group conversation then get if a conversation area exists and it is enabled. 128 if (\core_message\api::can_create_group_conversation($USER->id, $coursecontext)) { 129 if (\core_message\api::is_conversation_area_enabled('core_group', 'groups', $groupid, $coursecontext->id)) { 130 $mform->getElement('enablemessaging')->setSelected(1); 131 } 132 } 133 // Print picture. 134 if (!($pic = print_group_picture($group, $COURSE->id, true, true, false))) { 135 $pic = get_string('none'); 136 if ($mform->elementExists('deletepicture')) { 137 $mform->removeElement('deletepicture'); 138 } 139 } 140 $imageelement = $mform->getElement('currentpicture'); 141 $imageelement->setValue($pic); 142 } else { 143 if ($mform->elementExists('currentpicture')) { 144 $mform->removeElement('currentpicture'); 145 } 146 if ($mform->elementExists('deletepicture')) { 147 $mform->removeElement('deletepicture'); 148 } 149 } 150 151 if ($DB->record_exists('groups_members', ['groupid' => $groupid])) { 152 // If the group has members, lock visibility and participation fields. 153 /** @var MoodleQuickForm_select $visibility */ 154 $visibility = $mform->getElement('visibility'); 155 $visibility->freeze(); 156 /** @var MoodleQuickForm_advcheckbox $participation */ 157 $participation = $mform->getElement('participation'); 158 $participation->freeze(); 159 } 160 161 $handler = core_group\customfield\group_handler::create(); 162 $handler->instance_form_definition_after_data($this->_form, empty($groupid) ? 0 : $groupid); 163 } 164 165 /** 166 * Form validation 167 * 168 * @param array $data 169 * @param array $files 170 * @return array $errors An array of errors 171 */ 172 function validation($data, $files) { 173 global $COURSE, $DB, $CFG; 174 175 $errors = parent::validation($data, $files); 176 177 $name = trim($data['name']); 178 if (isset($data['idnumber'])) { 179 $idnumber = trim($data['idnumber']); 180 } else { 181 $idnumber = ''; 182 } 183 if ($data['id'] and $group = $DB->get_record('groups', array('id'=>$data['id']))) { 184 if (core_text::strtolower($group->name) != core_text::strtolower($name)) { 185 if (groups_get_group_by_name($COURSE->id, $name)) { 186 $errors['name'] = get_string('groupnameexists', 'group', $name); 187 } 188 } 189 if (!empty($idnumber) && $group->idnumber != $idnumber) { 190 if (groups_get_group_by_idnumber($COURSE->id, $idnumber)) { 191 $errors['idnumber']= get_string('idnumbertaken'); 192 } 193 } 194 195 if ($data['enrolmentkey'] != '') { 196 $errmsg = ''; 197 if (!empty($CFG->groupenrolmentkeypolicy) && $group->enrolmentkey !== $data['enrolmentkey'] 198 && !check_password_policy($data['enrolmentkey'], $errmsg)) { 199 // Enforce password policy when the password is changed. 200 $errors['enrolmentkey'] = $errmsg; 201 } else { 202 // Prevent twice the same enrolment key in course groups. 203 $sql = "SELECT id FROM {groups} WHERE id <> :groupid AND courseid = :courseid AND enrolmentkey = :key"; 204 $params = array('groupid' => $data['id'], 'courseid' => $COURSE->id, 'key' => $data['enrolmentkey']); 205 if ($DB->record_exists_sql($sql, $params)) { 206 $errors['enrolmentkey'] = get_string('enrolmentkeyalreadyinuse', 'group'); 207 } 208 } 209 } 210 211 } else if (groups_get_group_by_name($COURSE->id, $name)) { 212 $errors['name'] = get_string('groupnameexists', 'group', $name); 213 } else if (!empty($idnumber) && groups_get_group_by_idnumber($COURSE->id, $idnumber)) { 214 $errors['idnumber']= get_string('idnumbertaken'); 215 } else if ($data['enrolmentkey'] != '') { 216 $errmsg = ''; 217 if (!empty($CFG->groupenrolmentkeypolicy) && !check_password_policy($data['enrolmentkey'], $errmsg)) { 218 // Enforce password policy. 219 $errors['enrolmentkey'] = $errmsg; 220 } else if ($DB->record_exists('groups', array('courseid' => $COURSE->id, 'enrolmentkey' => $data['enrolmentkey']))) { 221 // Prevent the same enrolment key from being used multiple times in course groups. 222 $errors['enrolmentkey'] = get_string('enrolmentkeyalreadyinuse', 'group'); 223 } 224 } 225 226 $handler = \core_group\customfield\group_handler::create(); 227 $errors = array_merge($errors, $handler->instance_form_validation($data, $files)); 228 229 return $errors; 230 } 231 232 /** 233 * Get editor options for this form 234 * 235 * @return array An array of options 236 */ 237 function get_editor_options() { 238 return $this->_customdata['editoroptions']; 239 } 240 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body