Little strings...
[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
08103c93 19$courseid = required_param('courseid', PARAM_INT);
80efbf98 20$id = optional_param('id', false, PARAM_INT);
21$groupingid = optional_param('grouping', false, PARAM_INT);
22$newgrouping = optional_param('newgrouping', false, PARAM_INT);
08103c93
ML
23$delete = optional_param('delete', 0, PARAM_BOOL);
24$confirm = optional_param('confirm', 0, PARAM_BOOL);
77e90955 25
08103c93
ML
26if (empty($CFG->enablegroupings)) {
27 // NO GROUPINGS YET!
28 $groupingid = GROUP_NOT_IN_GROUPING;
29}
77e90955 30
31/// Course must be valid
80efbf98 32if (!$course = get_record('course', 'id', $courseid)) {
77e90955 33 error('Course ID was incorrect');
34}
35
36/// Delete action should not be called without a group id
80efbf98 37if ($delete && !$id) {
77e90955 38 error(get_string('errorinvalidgroup'));
39}
40
08103c93
ML
41if ($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');
47 }
48 notice_yesno(get_string('deletegroupconfirm', 'group', $group->name), 'edit.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
49 print_footer();
50 die;
51}
52
77e90955 53/// basic access control checks
80efbf98 54if ($id) {
55 if (!$group = get_record('groups', 'id', $id)) {
77e90955 56 error('Group ID was incorrect');
57 }
80efbf98 58 $context = get_context_instance(CONTEXT_COURSE, $course->id);
59 require_capability('moodle/course:managegroups', $context);
53b16b2b 60
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)) {
65 $groupingid = -1;
66 } else {
67 $groupingid = $groupings[0];
68 }
69 }
77e90955 70}
71
72/// First create the form
80efbf98 73$editform = new group_edit_form('edit.php', compact('group', 'groupingid', 'newgrouping', 'group', 'courseid'));
77e90955 74
75/// Override defaults if group is set
76if (!empty($group)) {
77 $editform->set_data($group);
78}
79
e4596a4a 80// Process delete action
81if ($delete) {
08103c93
ML
82 if (!confirm_sesskey()) {
83 error('Sesskey error');
84 }
e4596a4a 85 if (groups_delete_group($id)) {
e5138261 86 // MDL-9983
a160cf0b 87 $eventdata = new object();
88 $eventdata->group = $id;
89 $eventdata->course = $courseid;
90 events_trigger('group_deleted', $eventdata);
e4596a4a 91 redirect(groups_home_url($course->id, null, $groupingid, false));
92 } else {
93 print_error('erroreditgroup', 'group', groups_home_url($course->id));
94 }
95}
96
97$error = null;
98
77e90955 99if ($editform->is_cancelled()) {
80efbf98 100 redirect(groups_home_url($courseid, $id, $groupingid, false));
77e90955 101} elseif ($data = $editform->get_data()) {
102 $success = true;
77e90955 103 // preprocess data
e4596a4a 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);
108 $success = false;
109 } elseif (!$id = groups_create_group($course->id, $data)) {
77e90955 110 print_error('erroreditgroup');
111 } else {
80efbf98 112 $success = (bool)$id;
113 $data->id = $id;
114 if ($groupingid) {
115 $success = $success && groups_add_group_to_grouping($id, $groupingid);
e5138261 116 }
117 // MDL-9983
118 if ($success) {
119 events_trigger('group_created', $data);
120 }
121 }
77e90955 122 } elseif ($groupingid != $newgrouping) { // Moving group to new grouping
80efbf98 123 $success = $success && groups_remove_group_from_grouping($id, $groupingid);
124 $success = $success && groups_add_group_to_grouping($id, $newgrouping);
77e90955 125 } else { // Updating group
e4596a4a 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);
129 $success = false;
130 } elseif (!groups_update_group($data, $course->id)) {
77e90955 131 print_error('groupnotupdated');
132 }
e5138261 133 // MDL-9983
134 if ($success) {
135 events_trigger('group_updated', $data);
136 }
77e90955 137 }
77e90955 138 // Handle file upload
139 if ($success) {
80efbf98 140 require_once("$CFG->libdir/gdlib.php");
141 if (save_profile_image($id, $editform->_upload_manager, 'groups')) {
142 $data->picture = 1;
143 $success = $success && groups_update_group($data, $course->id);
144 }
77e90955 145 }
77e90955 146
147 if ($success) {
80efbf98 148 redirect(groups_home_url($course->id, $id, $groupingid, false));
e4596a4a 149 } elseif (empty($error)) {
77e90955 150 print_error('erroreditgroup', 'group', groups_home_url($course->id));
151 }
e4596a4a 152}
153$strgroups = get_string('groups');
154$strparticipants = get_string('participants');
155
156if ($id) {
157 $strheading = get_string('editgroupsettings', 'group');
158} else {
159 $strheading = get_string('creategroup', 'group');
160}
161print_header("$course->shortname: ". $strheading,
162 $course->fullname,
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));
167
168print_heading($strheading);
169
170if ($error) {
171 notify($error);
172}
173
174echo '<div id="grouppicture">';
175if ($id) {
176 print_group_picture($group, $course->id);
77e90955 177}
e4596a4a 178echo '</div>';
179$editform->display();
180print_footer($course);
77e90955 181?>