MDL-18293 exception cleanup
[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) {
dfdaabd6 22 if (!$grouping = $DB->get_record('groupings', array('id'=>$id))) {
b121a4ee 23 print_error('invalidgroupid');
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) {
b121a4ee 30 print_error('invalidcourseid');
ddff2fa8 31 }
08103c93 32
dfdaabd6 33 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
b121a4ee 34 print_error('invalidcourseid');
ddff2fa8 35 }
f3f7610c 36
ddff2fa8 37} else {
dfdaabd6 38 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
b121a4ee 39 print_error('invalidcourseid');
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)) {
62d63838 63 redirect($returnurl);
ddff2fa8 64 } else {
65 print_error('erroreditgrouping', 'group', $returnurl);
66 }
e4596a4a 67 }
68}
69
ddff2fa8 70/// First create the form
71$editform = new grouping_form();
72$editform->set_data($grouping);
73
c7b0485f 74if ($editform->is_cancelled()) {
ddff2fa8 75 redirect($returnurl);
76
c7b0485f 77} elseif ($data = $editform->get_data()) {
78 $success = true;
ddff2fa8 79
80 if ($data->id) {
f16fa0a3 81 if (!groups_update_grouping($data)) {
b121a4ee 82 print_error('cannotupdategroup');
f3f7610c 83 }
c7b0485f 84
c7b0485f 85 } else {
f16fa0a3 86 if (!groups_create_grouping($data)) {
b121a4ee 87 print_error('cannotcreategroup');
ddff2fa8 88 }
5bca3fed 89 }
ddff2fa8 90
91 redirect($returnurl);
92
f3f7610c 93}
ddff2fa8 94
62d63838 95$strgroupings = get_string('groupings', 'group');
ddff2fa8 96$strparticipants = get_string('participants');
97
98if ($id) {
99 $strheading = get_string('editgroupingsettings', 'group');
100} else {
101 $strheading = get_string('creategrouping', 'group');
102}
103
778918fd 104$navlinks = array(array('name'=>$strparticipants, 'link'=>$CFG->wwwroot.'/user/index.php?id='.$courseid, 'type'=>'misc'),
105 array('name'=>$strgroupings, 'link'=>$CFG->wwwroot.'/group/groupings.php?id='.$courseid, 'type'=>'misc'),
106 array('name'=>$strheading, 'link'=>'', 'type'=>'misc'));
107$navigation = build_navigation($navlinks);
108
109/// Print header
110print_header_simple($strgroupings, ': '.$strgroupings, $navigation, '', '', true, '', navmenu($course));
111
112
ddff2fa8 113print_heading($strheading);
114$editform->display();
115print_footer($course);
116
f3f7610c 117?>