Merge branch 'MDL-56724-master' of git://github.com/damyon/moodle
[moodle.git] / group / grouping.php
blob16bd02d426c63272e13ff4bfb4e6a33f72433c21
1 <?php
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.
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/>.
18 /**
19 * Create grouping OR edit grouping settings.
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
25 require_once('../config.php');
26 require_once('lib.php');
27 require_once('grouping_form.php');
29 /// get url variables
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);
35 $url = new moodle_url('/group/grouping.php');
36 if ($id) {
37 $url->param('id', $id);
38 if (!$grouping = $DB->get_record('groupings', array('id'=>$id))) {
39 print_error('invalidgroupid');
41 $grouping->description = clean_text($grouping->description);
42 if (empty($courseid)) {
43 $courseid = $grouping->courseid;
44 } else if ($courseid != $grouping->courseid) {
45 print_error('invalidcourseid');
46 } else {
47 $url->param('courseid', $courseid);
50 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
51 print_error('invalidcourseid');
54 } else {
55 $url->param('courseid', $courseid);
56 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
57 print_error('invalidcourseid');
59 $grouping = new stdClass();
60 $grouping->courseid = $course->id;
63 $PAGE->set_url($url);
65 require_login($course);
66 $context = context_course::instance($course->id);
67 require_capability('moodle/course:managegroups', $context);
69 $strgroupings = get_string('groupings', 'group');
70 $PAGE->set_title($strgroupings);
71 $PAGE->set_heading($course->fullname. ': '.$strgroupings);
72 $PAGE->set_pagelayout('admin');
73 navigation_node::override_active_url(new moodle_url('/group/index.php', array('id' => $course->id)));
75 $returnurl = $CFG->wwwroot.'/group/groupings.php?id='.$course->id;
77 if ($id and $delete) {
78 if (!empty($grouping->idnumber) && !has_capability('moodle/course:changeidnumber', $context)) {
79 print_error('groupinghasidnumber', '', '', $grouping->name);
81 if (!$confirm) {
82 $PAGE->set_title(get_string('deletegrouping', 'group'));
83 $PAGE->set_heading($course->fullname. ': '. get_string('deletegrouping', 'group'));
84 echo $OUTPUT->header();
85 $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
86 $optionsno = array('id'=>$courseid);
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');
89 echo $OUTPUT->confirm(get_string('deletegroupingconfirm', 'group', $grouping->name), $formcontinue, $formcancel);
90 echo $OUTPUT->footer();
91 die;
93 } else if (confirm_sesskey()){
94 if (groups_delete_grouping($id)) {
95 redirect($returnurl);
96 } else {
97 print_error('erroreditgrouping', 'group', $returnurl);
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);
104 if (!empty($grouping->id)) {
105 $grouping = file_prepare_standard_editor($grouping, 'description', $editoroptions, $context, 'grouping', 'description', $grouping->id);
106 } else {
107 $grouping = file_prepare_standard_editor($grouping, 'description', $editoroptions, $context, 'grouping', 'description', null);
110 /// First create the form
111 $editform = new grouping_form(null, compact('editoroptions'));
112 $editform->set_data($grouping);
114 if ($editform->is_cancelled()) {
115 redirect($returnurl);
117 } elseif ($data = $editform->get_data()) {
118 $success = true;
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);
124 if ($data->id) {
125 groups_update_grouping($data, $editoroptions);
126 } else {
127 groups_create_grouping($data, $editoroptions);
130 redirect($returnurl);
134 $strparticipants = get_string('participants');
135 if ($id) {
136 $strheading = get_string('editgroupingsettings', 'group');
137 } else {
138 $strheading = get_string('creategrouping', 'group');
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)));
143 $PAGE->navbar->add($strheading);
145 /// Print header
146 echo $OUTPUT->header();
147 echo $OUTPUT->heading($strheading);
148 $editform->display();
149 echo $OUTPUT->footer();