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