MDL-10383 - groups/groupings refactoring and improvements - basic UI for groupings...
[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
ddff2fa8 7 * @author J.White AT open.ac.uk
f3f7610c
ML
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package groups
10 */
11require_once('../config.php');
12require_once('lib.php');
ddff2fa8 13require_once('grouping_form.php');
f3f7610c 14
ddff2fa8 15/// get url variables
62d63838 16$courseid = optional_param('courseid', 0, PARAM_INT);
17$id = optional_param('id', 0, PARAM_INT);
18$delete = optional_param('delete', 0, PARAM_BOOL);
19$confirm = optional_param('confirm', 0, PARAM_BOOL);
f3f7610c 20
ddff2fa8 21if ($id) {
22 if (!$grouping = get_record('groupings', 'id', $id)) {
23 error('Group ID was incorrect');
24 }
25 if (empty($courseid)) {
62d63838 26 $courseid = $grouping->courseid;
5bca3fed 27
62d63838 28 } else if ($courseid != $grouping->courseid) {
ddff2fa8 29 error('Course ID was incorrect');
30 }
08103c93 31
ddff2fa8 32 if (!$course = get_record('course', 'id', $courseid)) {
33 error('Course ID was incorrect');
34 }
f3f7610c 35
ddff2fa8 36} else {
37 if (!$course = get_record('course', 'id', $courseid)) {
38 error('Course ID was incorrect');
39 }
40 $grouping = new object();
41 $grouping->courseid = $course->id;
c7b0485f 42}
f3f7610c 43
ddff2fa8 44require_login($course);
45$context = get_context_instance(CONTEXT_COURSE, $course->id);
46require_capability('moodle/course:managegroups', $context);
f3f7610c 47
62d63838 48$returnurl = $CFG->wwwroot.'/group/groupings.php?id='.$course->id;
ddff2fa8 49
50
51if ($id and $delete) {
52 if (!$confirm) {
53 print_header(get_string('deleteselectedgrouping', 'group'), get_string('deleteselectedgroup', 'group'));
54 $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
55 $optionsno = array('id'=>$courseid);
62d63838 56 notice_yesno(get_string('deletegroupingconfirm', 'group', $grouping->name), 'grouping.php', 'groupings.php', $optionsyes, $optionsno, 'get', 'get');
ddff2fa8 57 print_footer();
58 die;
59
60 } else if (confirm_sesskey()){
61 if (groups_delete_grouping($id)) {
62 // MDL-9983
63 $eventdata = new object();
64 $eventdata->group = $id;
65 $eventdata->course = $courseid;
66 events_trigger('grouping_deleted', $eventdata);
62d63838 67 redirect($returnurl);
ddff2fa8 68 } else {
69 print_error('erroreditgrouping', 'group', $returnurl);
70 }
e4596a4a 71 }
72}
73
ddff2fa8 74/// First create the form
75$editform = new grouping_form();
76$editform->set_data($grouping);
77
c7b0485f 78if ($editform->is_cancelled()) {
ddff2fa8 79 redirect($returnurl);
80
c7b0485f 81} elseif ($data = $editform->get_data()) {
82 $success = true;
ddff2fa8 83
84 if ($data->id) {
85 $data->timemodified = time();
86 if (!update_record('groupings', $data)) {
87 error('Error updating group');
f3f7610c 88 }
c7b0485f 89
c7b0485f 90 } else {
ddff2fa8 91 $data->timecreated = time();
92 $data->timemodified = $data->timecreated;
93 if (!$data->id = insert_record('groupings', $data)) {
94 error('Error updating grouping');
95 }
5bca3fed 96 }
ddff2fa8 97
98 redirect($returnurl);
99
f3f7610c 100}
ddff2fa8 101
62d63838 102$strgroups = get_string('groups');
103$strgroupings = get_string('groupings', 'group');
ddff2fa8 104$strparticipants = get_string('participants');
105
106if ($id) {
107 $strheading = get_string('editgroupingsettings', 'group');
108} else {
109 $strheading = get_string('creategrouping', 'group');
110}
111
112print_header("$course->shortname: ". $strheading,
113 $course->fullname,
114 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
115 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
62d63838 116 "-> <a href=\"$returnurl\">$strgroupings</a>".
ddff2fa8 117 "-> $strheading", '', '', true, '', user_login_string($course, $USER));
118print_heading($strheading);
119$editform->display();
120print_footer($course);
121
f3f7610c 122?>