MDL-9716
[moodle-linuxchix.git] / group / edit.php
blobcacbb5bf78e2ef66f1ed19cdb5bd3a561d68db22
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 /// include libraries
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');
18 /// get url variables
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
37 if ($id) {
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)) {
48 $groupingid = -1;
49 } else {
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
59 if (!empty($group)) {
60 $editform->set_data($group);
63 // Process delete action
64 if ($delete) {
65 if (groups_delete_group($id)) {
66 redirect(groups_home_url($course->id, null, $groupingid, false));
67 } else {
68 print_error('erroreditgroup', 'group', groups_home_url($course->id));
72 $error = null;
74 if ($editform->is_cancelled()) {
75 redirect(groups_home_url($courseid, $id, $groupingid, false));
76 } elseif ($data = $editform->get_data()) {
77 $success = true;
78 // preprocess 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);
83 $success = false;
84 } elseif (!$id = groups_create_group($course->id, $data)) {
85 print_error('erroreditgroup');
86 } else {
87 $success = (bool)$id;
88 $data->id = $id;
89 if ($groupingid) {
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);
100 $success = false;
101 } elseif (!groups_update_group($data, $course->id)) {
102 print_error('groupnotupdated');
105 // Handle file upload
106 if ($success) {
107 require_once("$CFG->libdir/gdlib.php");
108 if (save_profile_image($id, $editform->_upload_manager, 'groups')) {
109 $data->picture = 1;
110 $success = $success && groups_update_group($data, $course->id);
114 if ($success) {
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');
123 if ($id) {
124 $strheading = get_string('editgroupsettings', 'group');
125 } else {
126 $strheading = get_string('creategroup', 'group');
128 print_header("$course->shortname: ". $strheading,
129 $course->fullname,
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);
137 if ($error) {
138 notify($error);
141 echo '<div id="grouppicture">';
142 if ($id) {
143 print_group_picture($group, $course->id);
145 echo '</div>';
146 $editform->display();
147 print_footer($course);