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