"MDL-14129, fix print_error"
[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)) {
5a2a5331 23 print_error('Group ID was incorrect');
ddff2fa8 24 }
3980c306 25 $grouping->description = clean_text($grouping->description);
ddff2fa8 26 if (empty($courseid)) {
62d63838 27 $courseid = $grouping->courseid;
5bca3fed 28
62d63838 29 } else if ($courseid != $grouping->courseid) {
5a2a5331 30 print_error('Course ID was incorrect');
ddff2fa8 31 }
08103c93 32
ddff2fa8 33 if (!$course = get_record('course', 'id', $courseid)) {
5a2a5331 34 print_error('Course ID was incorrect');
ddff2fa8 35 }
f3f7610c 36
ddff2fa8 37} else {
38 if (!$course = get_record('course', 'id', $courseid)) {
5a2a5331 39 print_error('Course ID was incorrect');
ddff2fa8 40 }
41 $grouping = new object();
42 $grouping->courseid = $course->id;
c7b0485f 43}
f3f7610c 44
ddff2fa8 45require_login($course);
46$context = get_context_instance(CONTEXT_COURSE, $course->id);
47require_capability('moodle/course:managegroups', $context);
f3f7610c 48
62d63838 49$returnurl = $CFG->wwwroot.'/group/groupings.php?id='.$course->id;
ddff2fa8 50
51
52if ($id and $delete) {
53 if (!$confirm) {
739f2027 54 print_header(get_string('deletegrouping', 'group'), get_string('deletegrouping', 'group'));
ddff2fa8 55 $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
56 $optionsno = array('id'=>$courseid);
62d63838 57 notice_yesno(get_string('deletegroupingconfirm', 'group', $grouping->name), 'grouping.php', 'groupings.php', $optionsyes, $optionsno, 'get', 'get');
ddff2fa8 58 print_footer();
59 die;
60
61 } else if (confirm_sesskey()){
62 if (groups_delete_grouping($id)) {
63 // MDL-9983
64 $eventdata = new object();
65 $eventdata->group = $id;
66 $eventdata->course = $courseid;
67 events_trigger('grouping_deleted', $eventdata);
62d63838 68 redirect($returnurl);
ddff2fa8 69 } else {
70 print_error('erroreditgrouping', 'group', $returnurl);
71 }
e4596a4a 72 }
73}
74
ddff2fa8 75/// First create the form
76$editform = new grouping_form();
77$editform->set_data($grouping);
78
c7b0485f 79if ($editform->is_cancelled()) {
ddff2fa8 80 redirect($returnurl);
81
c7b0485f 82} elseif ($data = $editform->get_data()) {
83 $success = true;
ddff2fa8 84
85 if ($data->id) {
f16fa0a3 86 if (!groups_update_grouping($data)) {
5a2a5331 87 print_error('Error updating grouping');
f3f7610c 88 }
c7b0485f 89
c7b0485f 90 } else {
f16fa0a3 91 if (!groups_create_grouping($data)) {
5a2a5331 92 print_error('Error creating grouping');
ddff2fa8 93 }
5bca3fed 94 }
ddff2fa8 95
96 redirect($returnurl);
97
f3f7610c 98}
ddff2fa8 99
62d63838 100$strgroupings = get_string('groupings', 'group');
ddff2fa8 101$strparticipants = get_string('participants');
102
103if ($id) {
104 $strheading = get_string('editgroupingsettings', 'group');
105} else {
106 $strheading = get_string('creategrouping', 'group');
107}
108
778918fd 109$navlinks = array(array('name'=>$strparticipants, 'link'=>$CFG->wwwroot.'/user/index.php?id='.$courseid, 'type'=>'misc'),
110 array('name'=>$strgroupings, 'link'=>$CFG->wwwroot.'/group/groupings.php?id='.$courseid, 'type'=>'misc'),
111 array('name'=>$strheading, 'link'=>'', 'type'=>'misc'));
112$navigation = build_navigation($navlinks);
113
114/// Print header
115print_header_simple($strgroupings, ': '.$strgroupings, $navigation, '', '', true, '', navmenu($course));
116
117
ddff2fa8 118print_heading($strheading);
119$editform->display();
120print_footer($course);
121
f3f7610c 122?>