weekly release 3.10.1+
[moodle.git] / group / grouping.php
CommitLineData
f3f7610c 1<?php
4d8e2417
AG
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17
f3f7610c
ML
18/**
19 * Create grouping OR edit grouping settings.
20 *
4d8e2417
AG
21 * @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package core_group
f3f7610c
ML
24 */
25require_once('../config.php');
26require_once('lib.php');
ddff2fa8 27require_once('grouping_form.php');
f3f7610c 28
ddff2fa8 29/// get url variables
62d63838 30$courseid = optional_param('courseid', 0, PARAM_INT);
31$id = optional_param('id', 0, PARAM_INT);
32$delete = optional_param('delete', 0, PARAM_BOOL);
33$confirm = optional_param('confirm', 0, PARAM_BOOL);
f3f7610c 34
a6855934 35$url = new moodle_url('/group/grouping.php');
ddff2fa8 36if ($id) {
bbe0bd98 37 $url->param('id', $id);
dfdaabd6 38 if (!$grouping = $DB->get_record('groupings', array('id'=>$id))) {
b121a4ee 39 print_error('invalidgroupid');
ddff2fa8 40 }
3980c306 41 $grouping->description = clean_text($grouping->description);
ddff2fa8 42 if (empty($courseid)) {
62d63838 43 $courseid = $grouping->courseid;
62d63838 44 } else if ($courseid != $grouping->courseid) {
b121a4ee 45 print_error('invalidcourseid');
bbe0bd98 46 } else {
47 $url->param('courseid', $courseid);
ddff2fa8 48 }
08103c93 49
dfdaabd6 50 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
b121a4ee 51 print_error('invalidcourseid');
ddff2fa8 52 }
03221650 53
ddff2fa8 54} else {
bbe0bd98 55 $url->param('courseid', $courseid);
dfdaabd6 56 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
b121a4ee 57 print_error('invalidcourseid');
ddff2fa8 58 }
ace9051c 59 $grouping = new stdClass();
ddff2fa8 60 $grouping->courseid = $course->id;
c7b0485f 61}
f3f7610c 62
bbe0bd98 63$PAGE->set_url($url);
64
ddff2fa8 65require_login($course);
bf0f06b1 66$context = context_course::instance($course->id);
ddff2fa8 67require_capability('moodle/course:managegroups', $context);
f3f7610c 68
f196f42c
FM
69$strgroupings = get_string('groupings', 'group');
70$PAGE->set_title($strgroupings);
71$PAGE->set_heading($course->fullname. ': '.$strgroupings);
566889aa 72$PAGE->set_pagelayout('admin');
f196f42c 73navigation_node::override_active_url(new moodle_url('/group/index.php', array('id' => $course->id)));
ddff2fa8 74
f196f42c 75$returnurl = $CFG->wwwroot.'/group/groupings.php?id='.$course->id;
ddff2fa8 76
77if ($id and $delete) {
74b714df
ARN
78 if (!empty($grouping->idnumber) && !has_capability('moodle/course:changeidnumber', $context)) {
79 print_error('groupinghasidnumber', '', '', $grouping->name);
80 }
ddff2fa8 81 if (!$confirm) {
b87573d7 82 $PAGE->set_title(get_string('deletegrouping', 'group'));
bd08a24a 83 $PAGE->set_heading($course->fullname. ': '. get_string('deletegrouping', 'group'));
b87573d7 84 echo $OUTPUT->header();
ddff2fa8 85 $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
86 $optionsno = array('id'=>$courseid);
dc6896ef
PS
87 $formcontinue = new single_button(new moodle_url('grouping.php', $optionsyes), get_string('yes'), 'get');
88 $formcancel = new single_button(new moodle_url('groupings.php', $optionsno), get_string('no'), 'get');
768cefa0 89 echo $OUTPUT->confirm(get_string('deletegroupingconfirm', 'group', $grouping->name), $formcontinue, $formcancel);
653468d4 90 echo $OUTPUT->footer();
ddff2fa8 91 die;
92
93 } else if (confirm_sesskey()){
94 if (groups_delete_grouping($id)) {
62d63838 95 redirect($returnurl);
ddff2fa8 96 } else {
97 print_error('erroreditgrouping', 'group', $returnurl);
98 }
e4596a4a 99 }
100}
101
8bdc9cac
SH
102// Prepare the description editor: We do support files for grouping descriptions
103$editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$course->maxbytes, 'trust'=>true, 'context'=>$context, 'noclean'=>true);
104if (!empty($grouping->id)) {
64f93798 105 $grouping = file_prepare_standard_editor($grouping, 'description', $editoroptions, $context, 'grouping', 'description', $grouping->id);
8bdc9cac 106} else {
64f93798 107 $grouping = file_prepare_standard_editor($grouping, 'description', $editoroptions, $context, 'grouping', 'description', null);
8bdc9cac
SH
108}
109
ddff2fa8 110/// First create the form
8bdc9cac 111$editform = new grouping_form(null, compact('editoroptions'));
ddff2fa8 112$editform->set_data($grouping);
113
c7b0485f 114if ($editform->is_cancelled()) {
ddff2fa8 115 redirect($returnurl);
116
c7b0485f 117} elseif ($data = $editform->get_data()) {
118 $success = true;
74b714df
ARN
119 if (!has_capability('moodle/course:changeidnumber', $context)) {
120 // Remove the idnumber if the user doesn't have permission to modify it
121 unset($data->idnumber);
122 }
ddff2fa8 123
124 if ($data->id) {
8bdc9cac 125 groups_update_grouping($data, $editoroptions);
c7b0485f 126 } else {
8bdc9cac 127 groups_create_grouping($data, $editoroptions);
5bca3fed 128 }
ddff2fa8 129
130 redirect($returnurl);
131
f3f7610c 132}
ddff2fa8 133
ddff2fa8 134$strparticipants = get_string('participants');
ddff2fa8 135if ($id) {
136 $strheading = get_string('editgroupingsettings', 'group');
137} else {
138 $strheading = get_string('creategrouping', 'group');
139}
140
a6855934
PS
141$PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
142$PAGE->navbar->add($strgroupings, new moodle_url('/group/groupings.php', array('id'=>$courseid)));
b87573d7 143$PAGE->navbar->add($strheading);
778918fd 144
145/// Print header
b87573d7 146echo $OUTPUT->header();
9cb990ec 147echo $OUTPUT->heading($strheading);
ddff2fa8 148$editform->display();
653468d4 149echo $OUTPUT->footer();