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 $id = optional_param('id', false, PARAM_INT
);
20 $groupingid = optional_param('grouping', false, PARAM_INT
);
21 $newgrouping = optional_param('newgrouping', false, PARAM_INT
);
22 $courseid = required_param('courseid', PARAM_INT
);
24 $delete = optional_param('delete', false, PARAM_BOOL
);
26 /// Course must be valid
27 if (!$course = get_record('course', 'id', $courseid)) {
28 error('Course ID was incorrect');
31 /// Delete action should not be called without a group id
32 if ($delete && !$id) {
33 error(get_string('errorinvalidgroup'));
36 /// basic access control checks
38 if (!$group = get_record('groups', 'id', $id)) {
39 error('Group ID was incorrect');
41 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
42 require_capability('moodle/course:managegroups', $context);
44 // If group given but no groupingid, retrieve grouping id
45 if (empty($groupingid)) {
46 $groupings = groups_get_groupings_for_group($id);
47 if (empty($groupings)) {
50 $groupingid = $groupings[0];
55 /// First create the form
56 $editform = new group_edit_form('edit.php', compact('group', 'groupingid', 'newgrouping', 'group', 'courseid'));
58 /// Override defaults if group is set
60 $editform->set_data($group);
63 // Process delete action
65 if (groups_delete_group($id)) {
66 redirect(groups_home_url($course->id
, null, $groupingid, false));
68 print_error('erroreditgroup', 'group', groups_home_url($course->id
));
74 if ($editform->is_cancelled()) {
75 redirect(groups_home_url($courseid, $id, $groupingid, false));
76 } elseif ($data = $editform->get_data()) {
79 if (empty($group)) { // New group
80 // First check if this group name doesn't already exist
81 if (groups_group_name_exists($courseid, $data->name
)) {
82 $error = get_string('groupnameexists', 'group', $data->name
);
84 } elseif (!$id = groups_create_group($course->id
, $data)) {
85 print_error('erroreditgroup');
90 $success = $success && groups_add_group_to_grouping($id, $groupingid);
93 } elseif ($groupingid != $newgrouping) { // Moving group to new grouping
94 $success = $success && groups_remove_group_from_grouping($id, $groupingid);
95 $success = $success && groups_add_group_to_grouping($id, $newgrouping);
96 } else { // Updating group
97 $group = groups_get_group($data->id
);
98 if (groups_group_name_exists($courseid, $data->name
) && $group->name
!= $data->name
) {
99 $error = get_string('groupnameexists', 'group', $data->name
);
101 } elseif (!groups_update_group($data, $course->id
)) {
102 print_error('groupnotupdated');
105 // Handle file upload
107 require_once("$CFG->libdir/gdlib.php");
108 if (save_profile_image($id, $editform->_upload_manager
, 'groups')) {
110 $success = $success && groups_update_group($data, $course->id
);
115 redirect(groups_home_url($course->id
, $id, $groupingid, false));
116 } elseif (empty($error)) {
117 print_error('erroreditgroup', 'group', groups_home_url($course->id
));
120 $strgroups = get_string('groups');
121 $strparticipants = get_string('participants');
124 $strheading = get_string('editgroupsettings', 'group');
126 $strheading = get_string('creategroup', 'group');
128 print_header("$course->shortname: ". $strheading,
130 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
131 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
132 '-> <a href="' .format_string(groups_home_url($courseid, $id, $groupingid, false)) . "\">$strgroups</a>".
133 "-> $strheading", '', '', true, '', user_login_string($course, $USER));
135 print_heading($strheading);
141 echo '<div id="grouppicture">';
143 print_group_picture($group, $course->id
);
146 $editform->display();
147 print_footer($course);