MDL-8774: Also an effort to refactorise the poorly designed mod/data/Preset code.
[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
80efbf98 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);
77e90955 23
24$delete = optional_param('delete', false, PARAM_BOOL);
25
26/// Course must be valid
80efbf98 27if (!$course = get_record('course', 'id', $courseid)) {
77e90955 28 error('Course ID was incorrect');
29}
30
31/// Delete action should not be called without a group id
80efbf98 32if ($delete && !$id) {
77e90955 33 error(get_string('errorinvalidgroup'));
34}
35
36/// basic access control checks
80efbf98 37if ($id) {
38 if (!$group = get_record('groups', 'id', $id)) {
77e90955 39 error('Group ID was incorrect');
40 }
80efbf98 41 $context = get_context_instance(CONTEXT_COURSE, $course->id);
42 require_capability('moodle/course:managegroups', $context);
53b16b2b 43
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];
51 }
52 }
77e90955 53}
54
55/// First create the form
80efbf98 56$editform = new group_edit_form('edit.php', compact('group', 'groupingid', 'newgrouping', 'group', 'courseid'));
77e90955 57
58/// Override defaults if group is set
59if (!empty($group)) {
60 $editform->set_data($group);
61}
62
63if ($editform->is_cancelled()) {
80efbf98 64 redirect(groups_home_url($courseid, $id, $groupingid, false));
77e90955 65} elseif ($data = $editform->get_data()) {
66 $success = true;
80efbf98 67
77e90955 68 // preprocess data
69 if ($delete) {
80efbf98 70 if ($success = groups_delete_group($id)) {
77e90955 71 redirect(groups_home_url($course->id, null, $groupingid, false));
72 } else {
73 print_error('erroreditgroup', 'group', groups_home_url($course->id));
74 }
75 } elseif (empty($group)) { // New group
80efbf98 76 if (!$id = groups_create_group($course->id, $data)) {
77e90955 77 print_error('erroreditgroup');
78 } else {
80efbf98 79 $success = (bool)$id;
80 $data->id = $id;
81 if ($groupingid) {
82 $success = $success && groups_add_group_to_grouping($id, $groupingid);
83 }
77e90955 84 }
85 } elseif ($groupingid != $newgrouping) { // Moving group to new grouping
80efbf98 86 $success = $success && groups_remove_group_from_grouping($id, $groupingid);
87 $success = $success && groups_add_group_to_grouping($id, $newgrouping);
77e90955 88 } else { // Updating group
89 if (!groups_update_group($data, $course->id)) {
90 print_error('groupnotupdated');
91 }
92 }
77e90955 93 // Handle file upload
94 if ($success) {
80efbf98 95 require_once("$CFG->libdir/gdlib.php");
96 if (save_profile_image($id, $editform->_upload_manager, 'groups')) {
97 $data->picture = 1;
98 $success = $success && groups_update_group($data, $course->id);
99 }
77e90955 100 }
77e90955 101
102 if ($success) {
80efbf98 103 redirect(groups_home_url($course->id, $id, $groupingid, false));
77e90955 104 } else {
105 print_error('erroreditgroup', 'group', groups_home_url($course->id));
106 }
107} else { // Prepare and output form
108 $strgroups = get_string('groups');
80efbf98 109 $strparticipants = get_string('participants');
77e90955 110
80efbf98 111 if ($id) {
77e90955 112 $strheading = get_string('editgroupsettings', 'group');
113 } else {
114 $strheading = get_string('creategroup', 'group');
115 }
77e90955 116 print_header("$course->shortname: ". $strheading,
117 $course->fullname,
118 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
119 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
73d7d22b 120 '-> <a href="' .format_string(groups_home_url($courseid, $id, $groupingid, false)) . "\">$strgroups</a>".
121 "-> $strheading", '', '', true, '', user_login_string($course, $USER));
80efbf98 122
123 print_heading($strheading);
124 echo '<div id="grouppicture">';
125 if ($id) {
126 print_group_picture($group, $course->id);
127 }
128 echo '</div>';
77e90955 129 $editform->display();
130 print_footer($course);
131}
77e90955 132?>