MDL-32245 question bank: fix moving categories between contexts.
[moodle.git] / question / category.php
CommitLineData
aeb15530 1<?php
d3603157
TH
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
516cf3eb 17/**
d3603157 18 * This script allows a teacher to create, edit and delete question categories.
bc649d80 19 *
d3603157
TH
20 * @package moodlecore
21 * @subpackage questionbank
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
bc649d80 24 */
516cf3eb 25
516cf3eb 26
d3603157
TH
27require_once("../config.php");
28require_once($CFG->dirroot."/question/editlib.php");
29require_once($CFG->dirroot."/question/category_class.php");
30
31list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
32 question_edit_setup('categories', '/question/category.php');
b72ff476 33
d3603157
TH
34// get values from form for actions on this page
35$param = new stdClass();
36$param->moveup = optional_param('moveup', 0, PARAM_INT);
37$param->movedown = optional_param('movedown', 0, PARAM_INT);
38$param->moveupcontext = optional_param('moveupcontext', 0, PARAM_INT);
39$param->movedowncontext = optional_param('movedowncontext', 0, PARAM_INT);
40$param->tocontext = optional_param('tocontext', 0, PARAM_INT);
41$param->left = optional_param('left', 0, PARAM_INT);
42$param->right = optional_param('right', 0, PARAM_INT);
43$param->delete = optional_param('delete', 0, PARAM_INT);
44$param->confirm = optional_param('confirm', 0, PARAM_INT);
45$param->cancel = optional_param('cancel', '', PARAM_ALPHA);
46$param->move = optional_param('move', 0, PARAM_INT);
47$param->moveto = optional_param('moveto', 0, PARAM_INT);
48$param->edit = optional_param('edit', 0, PARAM_INT);
dac786f3 49
d3603157
TH
50$url = new moodle_url($thispageurl);
51foreach ((array)$param as $key=>$value) {
52 if (($key !== 'cancel' && $value !== 0) || ($key === 'cancel' && $value !== '')) {
53 $url->param($key, $value);
7f093351 54 }
d3603157
TH
55}
56$PAGE->set_url($url);
e2c80339 57
d3603157
TH
58$qcobject = new question_category_object($pagevars['cpage'], $thispageurl, $contexts->having_one_edit_tab_cap('categories'), $param->edit, $pagevars['cat'], $param->delete,
59 $contexts->having_cap('moodle/question:add'));
516cf3eb 60
5e8a85aa 61$streditingcategories = get_string('editcategories', 'question');
d3603157
TH
62if ($param->left || $param->right || $param->moveup || $param->movedown|| $param->moveupcontext || $param->movedowncontext){
63 require_sesskey();
87bee3bc
TL
64
65 if ($param->moveupcontext || $param->movedowncontext) {
66 $catid = ($param->moveupcontext > 0) ? $param->moveupcontext : $param->movedowncontext;
67 $oldcat = $DB->get_record('question_categories', array('id' => $catid));
68 $qcobject->update_category($catid, '0,'.$param->tocontext, $oldcat->name, $oldcat->info);
69 } else {
70 foreach ($qcobject->editlists as $list){
71 //processing of these actions is handled in the method where appropriate and page redirects.
72 $list->process_actions($param->left, $param->right, $param->moveup, $param->movedown,
73 $param->moveupcontext, $param->movedowncontext, $param->tocontext);
74 }
271e6dec 75 }
d3603157
TH
76}
77if ($param->delete && ($questionstomove = $DB->count_records("question", array("category" => $param->delete)))){
78 if (!$category = $DB->get_record("question_categories", array("id" => $param->delete))) { // security
79 print_error('nocate', 'question', $thispageurl->out(), $param->delete);
271e6dec 80 }
d197ea43 81 $categorycontext = context::instance_by_id($category->contextid);
d3603157
TH
82 $qcobject->moveform = new question_move_form($thispageurl,
83 array('contexts'=>array($categorycontext), 'currentcat'=>$param->delete));
84 if ($qcobject->moveform->is_cancelled()){
e42f153c 85 redirect($thispageurl);
d3603157
TH
86 } elseif ($formdata = $qcobject->moveform->get_data()) {
87 /// 'confirm' is the category to move existing questions to
88 list($tocategoryid, $tocontextid) = explode(',', $formdata->category);
89 $qcobject->move_questions_and_delete_category($formdata->delete, $tocategoryid);
21cbf9da 90 $thispageurl->remove_params('cat', 'category');
e42f153c 91 redirect($thispageurl);
271e6dec 92 }
d3603157
TH
93} else {
94 $questionstomove = 0;
95}
96if ($qcobject->catform->is_cancelled()) {
97 redirect($thispageurl);
98} else if ($catformdata = $qcobject->catform->get_data()) {
99 if (!$catformdata->id) {//new category
100 $qcobject->add_category($catformdata->parent, $catformdata->name, $catformdata->info);
101 } else {
102 $qcobject->update_category($catformdata->id, $catformdata->parent, $catformdata->name, $catformdata->info);
271e6dec 103 }
d3603157
TH
104 redirect($thispageurl);
105} else if ((!empty($param->delete) and (!$questionstomove) and confirm_sesskey())) {
106 $qcobject->delete_category($param->delete);//delete the category now no questions to move
107 $thispageurl->remove_params('cat', 'category');
108 redirect($thispageurl);
109}
b72ff476 110
d3603157
TH
111if ($param->edit){
112 $PAGE->navbar->add(get_string('editingcategory', 'question'));
113}
b72ff476 114
d3603157
TH
115$PAGE->set_title($streditingcategories);
116$PAGE->set_heading($COURSE->fullname);
117echo $OUTPUT->header();
aeb15530 118
d3603157
TH
119// display UI
120if (!empty($param->edit)) {
121 $qcobject->edit_single_category($param->edit);
122} else if ($questionstomove){
123 $qcobject->display_move_form($questionstomove, $category);
124} else {
125 // display the user interface
126 $qcobject->display_user_interface();
127}
128echo $OUTPUT->footer();