3 * Create group OR edit group settings.
5 * @copyright © 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
12 require_once('../config.php');
13 require_once($CFG->libdir
.'/moodlelib.php');
14 require_once($CFG->libdir
.'/uploadlib.php');
15 require_once('lib.php');
16 require_once('edit_form.php');
19 $courseid = required_param('courseid', PARAM_INT
);
20 $id = optional_param('id', false, PARAM_INT
);
21 $groupingid = optional_param('grouping', false, PARAM_INT
);
22 $newgrouping = optional_param('newgrouping', false, PARAM_INT
);
23 $delete = optional_param('delete', 0, PARAM_BOOL
);
24 $confirm = optional_param('confirm', 0, PARAM_BOOL
);
26 if (empty($CFG->enablegroupings
)) {
28 $groupingid = GROUP_NOT_IN_GROUPING
;
31 /// Course must be valid
32 if (!$course = get_record('course', 'id', $courseid)) {
33 error('Course ID was incorrect');
36 /// Delete action should not be called without a group id
37 if ($delete && !$id) {
38 error(get_string('errorinvalidgroup'));
41 if ($delete && !$confirm) {
42 print_header(get_string('deleteselectedgroup', 'group'), get_string('deleteselectedgroup', 'group'));
43 $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
44 $optionsno = array('id'=>$courseid);
45 if (!$group = get_record('groups', 'id', $id)) {
46 error('Group ID was incorrect');
48 notice_yesno(get_string('deletegroupconfirm', 'group', $group->name
), 'edit.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
53 /// basic access control checks
55 if (!$group = get_record('groups', 'id', $id)) {
56 error('Group ID was incorrect');
58 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
59 require_capability('moodle/course:managegroups', $context);
61 // If group given but no groupingid, retrieve grouping id
62 if (empty($groupingid)) {
63 $groupings = groups_get_groupings_for_group($id);
64 if (empty($groupings)) {
67 $groupingid = $groupings[0];
72 /// First create the form
73 $editform = new group_edit_form('edit.php', compact('group', 'groupingid', 'newgrouping', 'group', 'courseid'));
75 /// Override defaults if group is set
77 $editform->set_data($group);
80 // Process delete action
82 if (!confirm_sesskey()) {
83 error('Sesskey error');
85 if (groups_delete_group($id)) {
87 $eventdata = new object();
88 $eventdata->group
= $id;
89 $eventdata->course
= $courseid;
90 events_trigger('group_deleted', $eventdata);
91 redirect(groups_home_url($course->id
, null, $groupingid, false));
93 print_error('erroreditgroup', 'group', groups_home_url($course->id
));
99 if ($editform->is_cancelled()) {
100 redirect(groups_home_url($courseid, $id, $groupingid, false));
101 } elseif ($data = $editform->get_data()) {
104 if (empty($group)) { // New group
105 // First check if this group name doesn't already exist
106 if (groups_group_name_exists($courseid, $data->name
)) {
107 $error = get_string('groupnameexists', 'group', $data->name
);
109 } elseif (!$id = groups_create_group($course->id
, $data)) {
110 print_error('erroreditgroup');
112 $success = (bool)$id;
115 $success = $success && groups_add_group_to_grouping($id, $groupingid);
119 events_trigger('group_created', $data);
122 } elseif ($groupingid != $newgrouping) { // Moving group to new grouping
123 $success = $success && groups_remove_group_from_grouping($id, $groupingid);
124 $success = $success && groups_add_group_to_grouping($id, $newgrouping);
125 } else { // Updating group
126 $group = groups_get_group($data->id
);
127 if (groups_group_name_exists($courseid, $data->name
) && $group->name
!= $data->name
) {
128 $error = get_string('groupnameexists', 'group', $data->name
);
130 } elseif (!groups_update_group($data, $course->id
)) {
131 print_error('groupnotupdated');
135 events_trigger('group_updated', $data);
138 // Handle file upload
140 require_once("$CFG->libdir/gdlib.php");
141 if (save_profile_image($id, $editform->_upload_manager
, 'groups')) {
143 $success = $success && groups_update_group($data, $course->id
);
148 redirect(groups_home_url($course->id
, $id, $groupingid, false));
149 } elseif (empty($error)) {
150 print_error('erroreditgroup', 'group', groups_home_url($course->id
));
153 $strgroups = get_string('groups');
154 $strparticipants = get_string('participants');
157 $strheading = get_string('editgroupsettings', 'group');
159 $strheading = get_string('creategroup', 'group');
161 print_header("$course->shortname: ". $strheading,
163 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
164 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
165 '-> <a href="' .format_string(groups_home_url($courseid, $id, $groupingid, false)) . "\">$strgroups</a>".
166 "-> $strheading", '', '', true, '', user_login_string($course, $USER));
168 print_heading($strheading);
174 echo '<div id="grouppicture">';
176 print_group_picture($group, $course->id
);
179 $editform->display();
180 print_footer($course);