MDL-8857
[moodle-linuxchix.git] / group / group.php
blob060f70a031886d54afb29ffc4e1a067e341b09dc
1 <?php
2 /**
3 * Create group OR edit group settings.
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
11 require_once('../config.php');
12 require_once('lib.php');
13 require_once($CFG->libdir.'/moodlelib.php');
14 require_once($CFG->libdir.'/uploadlib.php');
16 $success = true;
17 $err = array();
19 $courseid = required_param('courseid', 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);
24 $groupsettings->name = optional_param('name', false, PARAM_TEXT);
25 $groupsettings->description= optional_param('description', '', PARAM_TEXT);
26 $groupsettings->enrolmentkey= optional_param('enrolmentkey', '', PARAM_TEXT);
27 $groupsettings->hidepicture= optional_param('hidepicture', true, PARAM_BOOL);
29 $delete = optional_param('delete', false, PARAM_BOOL);
31 // Get the course information so we can print the header and
32 // check the course id is valid
33 $course = groups_get_course_info($courseid);
34 if (! $course) {
35 $success = false;
36 print_error('invalidcourse'); //'The course ID is invalid'
38 if ($delete && !$groupid) {
39 $success = false;
40 print_error('errorinvalidgroup', 'group', groups_home_url($courseid));
43 if ($success) {
44 // Make sure that the user has permissions to manage groups.
45 require_login($courseid);
47 $context = get_context_instance(CONTEXT_COURSE, $courseid);
48 if (! has_capability('moodle/course:managegroups', $context)) {
49 redirect();
52 /// If data submitted, then process and store.
54 if ($frm = data_submitted() and confirm_sesskey()) {
56 if (isset($frm->cancel)) {
57 redirect(groups_home_url($courseid, $groupid, $groupingid, false));
59 elseif (isset($frm->confirmdelete)) {
60 if ($success = groups_delete_group($groupid)) {
61 redirect(groups_home_url($courseid, null, $groupingid, false));
62 } else {
63 print_error('erroreditgroup', 'group', groups_home_url($courseid));
66 elseif (empty($frm->name)) {
67 $err['name'] = get_string('missingname');
69 elseif (isset($frm->update)) {
70 if (! $groupid) { //OK, new group.
71 if (GROUP_NOT_IN_GROUPING == $groupingid) {
72 print_error('errornotingrouping', 'group', groups_home_url($courseid), get_string('notingrouping', 'group'));
74 $success = (bool)$groupid = groups_create_group($courseid); //$groupsettings);
75 $success = groups_add_group_to_grouping($groupid, $groupingid);
77 elseif ($groupingid != $newgrouping) { //OK, move group.
78 if (GROUP_NOT_IN_GROUPING == $newgrouping) {
79 print_error('errornotingrouping', 'group', groups_home_url($courseid), get_string('notingrouping', 'group'));
81 $success = $success && groups_remove_group_from_grouping($groupid, $groupingid);
82 $success = $success && groups_add_group_to_grouping($groupid, $newgrouping);
84 if ($success) {
85 //require_once($CFG->dirroot.'/lib/uploadlib.php');
87 $um = new upload_manager('imagefile',false,false,$course=null,false,$modbytes=0,$silent=false,$allownull=true);
88 if ($um->preprocess_files()) {
89 require_once("$CFG->libdir/gdlib.php");
91 if (save_profile_image($groupid, $um, 'groups')) {
92 $groupsettings->picture = 1;
94 } else {
95 $success = false;
98 $success = $success && groups_set_group_settings($groupid, $groupsettings);
100 if ($success) {
101 redirect(groups_home_url($courseid, $groupid, $groupingid, false));
103 else {
104 print_error('erroreditgroup', 'group', groups_home_url($courseid));
109 /// OR, prepare the form.
111 if ($groupid) {
112 // Form to edit existing group.
113 $group = groups_get_group_settings($groupid);
114 if (! $group) {
115 print_error('errorinvalidgroup', 'group', groups_home_url($courseid));
117 $strname = s($group->name);
118 $strdesc = s($group->description);
120 $strbutton = get_string('save', 'group');
121 $strheading = get_string('editgroupsettings', 'group');
122 } else {
123 // Form to create a new one.
124 $strname = get_string('defaultgroupname', 'group');
125 $strdesc = '';
126 $strbutton = $strheading = get_string('creategroup', 'group');
128 $strgroups = get_string('groups');
129 $strparticipants = get_string('participants');
130 if ($delete) {
131 $strheading = get_string('deleteselectedgroup', 'group');
132 } //else { $strheader = get_string('groupinfoedit'); }
134 $maxbytes = get_max_upload_file_size($CFG->maxbytes, $course->maxbytes);
135 if (!empty($CFG->gdversion) and $maxbytes) {
136 $printuploadpicture = true;
137 } else {
138 $printuploadpicture = false;
141 /// Print the page and form
143 print_header("$course->shortname: ". $strheading,
144 $course->fullname,
145 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
146 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
147 "-> $strgroups", '', '', true, '', user_login_string($course, $USER));
149 $usehtmleditor = false;
151 <h3 class="main"><?php echo $strheading ?></h3>
153 <form action="group.php" method="post" enctype="multipart/form-data" class="mform notmform" id="groupform">
155 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
156 <input type="hidden" name="courseid" value="<?php p($courseid); ?>" />
157 <input type="hidden" name="grouping" value="<?php p($groupingid); ?>" />
158 <?php
159 if ($groupid) {
160 echo '<input type="hidden" name="group" value="'. $groupid .'" />';
163 if ($delete) {
164 /*echo 'Are you sure you want to delete group X ?';
165 choose_from_menu_yesno('confirmdelete', false, '', true);*/
168 <p><?php print_string('deletegroupconfirm', 'group', $strname); ?></p>
169 <input type="hidden" name="delete" value="1" />
170 <input type="submit" name="confirmdelete" value="<?php print_string('yes'); ?>" />
171 <input type="submit" name="cancel" value="<?php print_string('no'); ?>" />
172 <?php
173 } else {
176 <div class="fitem">
177 <p><label for="groupname"><?php
178 print_string('groupname', 'group');
179 if (isset($err['name'])) {
180 echo' ';
181 formerr($err['name']);
182 } ?>&nbsp; </label></p>
183 <p class="felement"><input id="groupname" name="name" type="text" size="40" value="<?php echo $strname; ?>" /></p>
184 </div>
186 <p><label for="edit-description"><?php print_string('groupdescription', 'group'); ?>&nbsp;</label></p>
187 <p><?php print_textarea($usehtmleditor, 5, 45, 200, 400, 'description', $strdesc); ?></p>
189 <p><label for="enrolmentkey"><?php print_string('enrolmentkey', 'group'); ?>&nbsp;</label></p>
190 <p><input id="enrolmentkey" name="enrolmentkey" type="text" size="25" /></p>
192 <?php if ($printuploadpicture) { ?>
193 <p><label for="menuhidepicture"><?php print_string('hidepicture', 'group'); ?>&nbsp;</label></p>
194 <p><?php $options = array();
195 $options[0] = get_string('no');
196 $options[1] = get_string('yes');
197 choose_from_menu($options, 'hidepicture', isset($group)? $group->hidepicture: 1, '');?></p>
199 <p><label ><?php /* for="imagefile" */ print_string('newpicture', 'group');
200 helpbutton('picture', get_string('helppicture'));
201 print_string('maxsize', '', display_size($maxbytes), 'group');
202 if (isset($err['imagefile'])) formerr($err['imagefile']);
203 ?>&nbsp;</label></p>
204 <p><?php upload_print_form_fragment(1, array('imagefile'), null,false,null,0,0,false); ?></p>
205 <?php
208 if ($groupid) { //OK, editing - option to move grouping.
210 <p><label for="groupings"><?php print_string('addgroupstogrouping', 'group'); ?></label></p>
211 <select name="newgrouping" id="groupings" class="select">
212 <?php
213 $groupingids = groups_get_groupings($courseid);
214 if (GROUP_NOT_IN_GROUPING == $groupingid) {
215 $groupingids[] = GROUP_NOT_IN_GROUPING;
217 if ($groupingids) {
218 // Put the groupings into a hash and sort them
219 foreach($groupingids as $id) {
220 $listgroupings[$id] = groups_get_grouping_displayname($id, $courseid);
222 natcasesort($listgroupings);
224 // Print out the HTML
225 $count = 1;
226 foreach($listgroupings as $id => $name) {
227 $select = '';
228 if ($groupingid == $id) {
229 $select = ' selected="selected"';
231 echo "<option value=\"$id\"$select>$name</option>\n";
232 $count++;
236 </select>
237 <?php } //IF($groupid) ?>
239 <p class="fitem">
240 <label for="id_submit">&nbsp;</label>
241 <span class="f--element fsubmit">
242 <input type="submit" name="update" id="id_submit" value="<?php echo $strbutton; ?>" />
243 <input type="submit" name="cancel" value="<?php print_string('cancel', 'group'); ?>" />
244 </span>
245 </p>
247 <?php } //IF($delete) ?>
249 <span class="clearer">&nbsp;</span>
251 </form>
252 <?php
253 print_footer($course);