MDL-10383 - group edit form refactoring and minor bugfixing
[moodle.git] / group / grouping.php
CommitLineData
f3f7610c
ML
1<?php
2/**
3 * Create grouping OR edit grouping 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 */
2524b0f2 11
12//TODO: reimplement
13die;die;die;
14
f3f7610c
ML
15require_once('../config.php');
16require_once('lib.php');
17require_once($CFG->libdir.'/moodlelib.php');
c7b0485f 18require_once('grouping_edit_form.php');
f3f7610c 19
f3f7610c 20$courseid = required_param('courseid', PARAM_INT);
c7b0485f 21$id = optional_param('id', false, PARAM_INT);
f3f7610c 22
5bca3fed 23$delete = optional_param('delete', false, PARAM_BOOL);
24
08103c93
ML
25if (empty($CFG->enablegroupings)) {
26 // NO GROUPIGS YET!
27 error('No groupings yet');
28}
29
f3f7610c
ML
30// Get the course information so we can print the header and
31// check the course id is valid
32$course = groups_get_course_info($courseid);
33if (! $course) {
34 $success = false;
b1f627d9 35 print_error('invalidcourse'); //'The course ID is invalid'
36}
c7b0485f 37if (GROUP_NOT_IN_GROUPING == $id) {
b1f627d9 38 print_error('errornotingroupingedit', 'group', groups_home_url($courseid), get_string('notingrouping', 'group'));
f3f7610c
ML
39}
40
c7b0485f 41/// basic access control checks
42if ($id) {
43 if (!$grouping = get_record('groups_groupings', 'id', $id)) {
44 error('Grouping ID was incorrect');
45 }
46 $context = get_context_instance(CONTEXT_COURSE, $course->id);
47 require_capability('moodle/course:managegroups', $context);
48}
f3f7610c 49
c7b0485f 50/// First create the form
51$editform = new grouping_edit_form('grouping.php', compact('grouping', 'courseid'));
f3f7610c 52
c7b0485f 53/// Override defaults if group is set
54if (!empty($grouping)) {
55 $editform->set_data($grouping);
56}
f3f7610c 57
e4596a4a 58// preprocess data
59if ($delete) {
60 if (groups_delete_grouping($id)) {
61 redirect(groups_home_url($course->id));
62 } else {
63 print_error('erroreditgrouping', 'group', groups_home_url($course->id));
64 }
65}
66
c7b0485f 67if ($editform->is_cancelled()) {
68 redirect(groups_home_url($courseid, false, $id, false));
69} elseif ($data = $editform->get_data()) {
70 $success = true;
71
e4596a4a 72 if (empty($grouping)) { // New grouping
c7b0485f 73 if (!$id = groups_create_grouping($course->id, $data)) {
74 print_error('erroreditgrouping');
75 } else {
76 $success = (bool)$id;
77 $data->id = $id;
f3f7610c 78 }
c7b0485f 79 } else { // Updating grouping
80 if (!groups_update_grouping($data, $course->id)) {
81 print_error('groupingnotupdated');
f3f7610c
ML
82 }
83 }
c7b0485f 84
85 if ($success) {
86 redirect(groups_home_url($courseid, false, $id, false));
f3f7610c 87 } else {
c7b0485f 88 print_error('erroreditgrouping', 'group', groups_home_url($courseid));
f3f7610c 89 }
c7b0485f 90
91} else { // Prepare and output form
f3f7610c
ML
92 $strgroups = get_string('groups');
93 $strparticipants = get_string('participants');
c7b0485f 94
95 if ($id) {
96 $strheading = get_string('editgroupingsettings', 'group');
97 } else {
98 $strheading = get_string('creategrouping', 'group');
5bca3fed 99 }
c7b0485f 100 print_header("$course->shortname: ". $strheading,
6ba65fa0 101 $course->fullname,
f3f7610c
ML
102 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
103 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
c7b0485f 104 '-> <a href="' .format_string(groups_home_url($courseid, false, $id, false)) . "\">$strgroups</a>".
105 "-> $strheading", '', '', true, '', user_login_string($course, $USER));
106 print_heading($strheading);
107 $editform->display();
f3f7610c
ML
108 print_footer($course);
109}
f3f7610c 110?>