MDL-9062 Created two new files so the unfinished conversion doesn't affect any curren...
[moodle.git] / group / edit.php
CommitLineData
77e90955 1<?php
2/**
3 * Create group OR edit group settings.
4 *
5 * @copyright &copy; 2006 The Open University
6 * @author N.D.Freear AT open.ac.uk
7 * @author J.White AT open.ac.uk
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package groups
10 */
11/// include libraries
12require_once('../config.php');
13require_once($CFG->libdir.'/moodlelib.php');
14require_once($CFG->libdir.'/uploadlib.php');
15require_once('lib.php');
16require_once('edit_form.php');
17
18/// get url variables
19$id = required_param('id', PARAM_INT);
20$groupingid = optional_param('grouping', false, PARAM_INT);
21$newgrouping= optional_param('newgrouping', false, PARAM_INT);
22$groupid = optional_param('group', false, PARAM_INT);
23
24$delete = optional_param('delete', false, PARAM_BOOL);
25
26/// Course must be valid
27if (!$course = get_record('course', 'id', $id)) {
28 error('Course ID was incorrect');
29}
30
31/// Delete action should not be called without a group id
32if ($delete && !$groupid) {
33 error(get_string('errorinvalidgroup'));
34}
35
36/// basic access control checks
37if ($groupid) {
38 if (!$group = get_record('groups', 'id', $groupid)) {
39 error('Group ID was incorrect');
40 }
41 require_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE), $course->id);
42}
43
44/// First create the form
45$editform = new group_edit_form('group.php', compact('group'));
46
47/// Override defaults if group is set
48if (!empty($group)) {
49 $editform->set_data($group);
50}
51
52if ($editform->is_cancelled()) {
53 redirect(groups_home_url($courseid, $groupid, $groupingid, false));
54} elseif ($data = $editform->get_data()) {
55 $success = true;
56
57 // preprocess data
58 if ($delete) {
59 if ($success = groups_delete_group($groupid)) {
60 redirect(groups_home_url($course->id, null, $groupingid, false));
61 } else {
62 print_error('erroreditgroup', 'group', groups_home_url($course->id));
63 }
64 } elseif (empty($group)) { // New group
65 if (!$group = groups_create_group($course->id)) {
66 print_error('erroreditgroup');
67 } else {
68 $success = (bool)$groupid = groups_create_group($course->id);
69 }
70 } elseif ($groupingid != $newgrouping) { // Moving group to new grouping
71 if ($groupingid != GROUP_NOT_IN_GROUPING) {
72 $success = $success && groups_remove_group_from_grouping($groupid, $groupingid);
73 }
74 } else { // Updating group
75 if (!groups_update_group($data, $course->id)) {
76 print_error('groupnotupdated');
77 }
78 }
79
80 // Handle file upload
81 if ($success) {
82 $um = new upload_manager('imagefile',false,false,$course=null,false,$modbytes=0,$silent=false,$allownull=true);
83 if ($um->preprocess_files()) {
84 require_once("$CFG->libdir/gdlib.php");
85
86 if (save_profile_image($groupid, $um, 'groups')) {
87 $groupsettings->picture = 1;
88 }
89 } else {
90 $success = false;
91 }
92 }
93 $success = $success && groups_set_group_settings($groupid, $groupsettings);
94
95 if ($success) {
96 redirect(groups_home_url($course->id, $groupid, $groupingid, false));
97 } else {
98 print_error('erroreditgroup', 'group', groups_home_url($course->id));
99 }
100} else { // Prepare and output form
101 $strgroups = get_string('groups');
102
103 if ($groupid) {
104 $strheading = get_string('editgroupsettings', 'group');
105 } else {
106 $strheading = get_string('creategroup', 'group');
107 }
108
109 print_header("$course->shortname: ". $strheading,
110 $course->fullname,
111 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
112 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
113 "-> $strgroups", '', '', true, '', user_login_string($course, $USER));
114 $editform->display();
115 print_footer($course);
116}
117
118/// OR, prepare the form.
119
120if ($groupid) {
121 // Form to edit existing group.
122 $group = groups_get_group_settings($groupid);
123 if (! $group) {
124 print_error('errorinvalidgroup', 'group', groups_home_url($courseid));
125 }
126 $strname = s($group->name);
127 $strdesc = s($group->description);
128
129 $strbutton = get_string('save', 'group');
130 $strheading = get_string('editgroupsettings', 'group');
131} else {
132 // Form to create a new one.
133 $strname = get_string('defaultgroupname', 'group');
134 $strdesc = '';
135 $strbutton = $strheading = get_string('creategroup', 'group');
136}
137$strgroups = get_string('groups');
138$strparticipants = get_string('participants');
139if ($delete) {
140 $strheading = get_string('deleteselectedgroup', 'group');
141} //else { $strheader = get_string('groupinfoedit'); }
142
143$maxbytes = get_max_upload_file_size($CFG->maxbytes, $course->maxbytes);
144if (!empty($CFG->gdversion) and $maxbytes) {
145 $printuploadpicture = true;
146} else {
147 $printuploadpicture = false;
148}
149
150/// Print the page and form
151
152print_header("$course->shortname: ". $strheading,
153 $course->fullname,
154 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
155 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
156 "-> $strgroups", '', '', true, '', user_login_string($course, $USER));
157
158$usehtmleditor = false;
159
160echo '<h3 class="main">' . $strheading . '</h3>'."\n";
161
162echo '<form action="group.php" method="post" enctype="multipart/form-data" class="mform notmform" id="groupform">'."\n";
163echo '<div>'."\n";
164echo '<input type="hidden" name="sesskey" value="' . s(sesskey()) . '" />'."\n";
165echo '<input type="hidden" name="courseid" value="' . s($courseid) . '" />'."\n";
166echo '<input type="hidden" name="grouping" value="' . s($groupingid) . '" />'."\n";
167
168if ($groupid) {
169 echo '<input type="hidden" name="group" value="'. $groupid .'" />'."\n";
170}
171
172if ($delete) {
173 /*echo 'Are you sure you want to delete group X ?';
174 choose_from_menu_yesno('confirmdelete', false, '', true);*/
175
176 echo '<p>' . get_string('deletegroupconfirm', 'group', $strname) . '</p>'."\n";
177 echo '<input type="hidden" name="delete" value="1" />'."\n";
178 echo '<input type="submit" name="confirmdelete" value="' . get_string('yes') . '" />'."\n";
179 echo '<input type="submit" name="cancel" value="' . get_string('no') . '" />'."\n";
180} else {
181 echo '<div class="fitem">'."\n";
182 echo '<p><label for="groupname">' . get_string('groupname', 'group');
183
184 if (isset($err['name'])) {
185 echo ' ';
186 formerr($err['name']);
187 }
188
189 echo '&nbsp; </label></p>'."\n";
190 echo '<p class="felement"><input id="groupname" name="name" type="text" size="40" value="' . $strname . '" /></p>'."\n";
191 echo '</div>'."\n";
192
193 echo '<p><label for="edit-description">' . get_string('groupdescription', 'group') . '&nbsp;</label></p>'."\n";
194 echo '<p>' . print_textarea($usehtmleditor, 5, 45, 200, 400, 'description', $strdesc, 0, true) . '</p>'."\n";
195
196 echo '<p><label for="enrolmentkey">' . get_string('enrolmentkey', 'group') . '&nbsp;</label></p>'."\n";
197 echo '<p><input id="enrolmentkey" name="enrolmentkey" type="text" size="25" /></p>'."\n";
198
199 if ($printuploadpicture) {
200 echo '<p><label for="menuhidepicture">' . get_string('hidepicture', 'group') . '&nbsp;</label></p>'."\n";
201 echo '<p>';
202 $options = array();
203 $options[0] = get_string('no');
204 $options[1] = get_string('yes');
205 choose_from_menu($options, 'hidepicture', isset($group)? $group->hidepicture: 1, '');
206 echo '</p>'."\n";
207
208 echo '<p><label >' . get_string('newpicture', 'group');
209 helpbutton('picture', get_string('helppicture'));
210 echo get_string('maxsize', '', display_size($maxbytes), 'group');
211
212 if (isset($err['imagefile'])) {
213 formerr($err['imagefile']);
214 }
215
216 echo '&nbsp;</label></p>'."\n";
217 echo '<p>' . upload_print_form_fragment(1, array('imagefile'), null,false,null,0,0,true) . '</p>'."\n";
218 }
219
220 if ($groupid) { //OK, editing - option to move grouping.
221
222 echo '<p><label for="groupings">' . get_string('addgroupstogrouping', 'group'). '</label></p>'."\n";
223 echo '<select name="newgrouping" id="groupings" class="select">'."\n";
224
225 $groupingids = groups_get_groupings($courseid);
226 if (GROUP_NOT_IN_GROUPING == $groupingid) {
227 $groupingids[] = GROUP_NOT_IN_GROUPING;
228 }
229 if ($groupingids) {
230 // Put the groupings into a hash and sort them
231 foreach($groupingids as $id) {
232 $listgroupings[$id] = groups_get_grouping_displayname($id, $courseid);
233 }
234 natcasesort($listgroupings);
235
236 // Print out the HTML
237 $count = 1;
238 foreach($listgroupings as $id => $name) {
239 $select = '';
240 if ($groupingid == $id) {
241 $select = ' selected="selected"';
242 }
243 echo "<option value=\"$id\"$select>$name</option>\n";
244 $count++;
245 }
246 }
247
248 echo '</select>'."\n";
249 } //IF($groupid)
250
251 echo '<p class="fitem">'."\n";
252 echo '<label for="id_submit">&nbsp;</label>'."\n";
253 echo '<span class="f--element fsubmit">'."\n";
254 echo '<input type="submit" name="update" id="id_submit" value="' . $strbutton . '" />'."\n";
255 echo '<input type="submit" name="cancel" value="' . get_string('cancel', 'group') . '" />'."\n";
256 echo '</span>'."\n";
257 echo '</p>'."\n";
258
259} //IF($delete)
260
261echo '<span class="clearer">&nbsp;</span>'."\n";
262echo '</div>';
263echo '</form>'."\n";
264
265print_footer($course);
266
267?>