MDL-20636 Reveiw all throw statements.
[moodle.git] / question / category.php
CommitLineData
aeb15530 1<?php
d3603157
TH
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
516cf3eb 18/**
d3603157 19 * This script allows a teacher to create, edit and delete question categories.
bc649d80 20 *
d3603157
TH
21 * @package moodlecore
22 * @subpackage questionbank
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
bc649d80 25 */
516cf3eb 26
516cf3eb 27
d3603157
TH
28require_once("../config.php");
29require_once($CFG->dirroot."/question/editlib.php");
30require_once($CFG->dirroot."/question/category_class.php");
31
32list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
33 question_edit_setup('categories', '/question/category.php');
b72ff476 34
d3603157
TH
35// get values from form for actions on this page
36$param = new stdClass();
37$param->moveup = optional_param('moveup', 0, PARAM_INT);
38$param->movedown = optional_param('movedown', 0, PARAM_INT);
39$param->moveupcontext = optional_param('moveupcontext', 0, PARAM_INT);
40$param->movedowncontext = optional_param('movedowncontext', 0, PARAM_INT);
41$param->tocontext = optional_param('tocontext', 0, PARAM_INT);
42$param->left = optional_param('left', 0, PARAM_INT);
43$param->right = optional_param('right', 0, PARAM_INT);
44$param->delete = optional_param('delete', 0, PARAM_INT);
45$param->confirm = optional_param('confirm', 0, PARAM_INT);
46$param->cancel = optional_param('cancel', '', PARAM_ALPHA);
47$param->move = optional_param('move', 0, PARAM_INT);
48$param->moveto = optional_param('moveto', 0, PARAM_INT);
49$param->edit = optional_param('edit', 0, PARAM_INT);
dac786f3 50
d3603157
TH
51$url = new moodle_url($thispageurl);
52foreach ((array)$param as $key=>$value) {
53 if (($key !== 'cancel' && $value !== 0) || ($key === 'cancel' && $value !== '')) {
54 $url->param($key, $value);
7f093351 55 }
d3603157
TH
56}
57$PAGE->set_url($url);
58$PAGE->set_pagelayout('standard');
e2c80339 59
d3603157
TH
60$qcobject = new question_category_object($pagevars['cpage'], $thispageurl, $contexts->having_one_edit_tab_cap('categories'), $param->edit, $pagevars['cat'], $param->delete,
61 $contexts->having_cap('moodle/question:add'));
516cf3eb 62
d3603157
TH
63$streditingcategories = get_string('editcategories', 'quiz');
64if ($param->left || $param->right || $param->moveup || $param->movedown|| $param->moveupcontext || $param->movedowncontext){
65 require_sesskey();
66 foreach ($qcobject->editlists as $list){
67 //processing of these actions is handled in the method where appropriate and page redirects.
68 $list->process_actions($param->left, $param->right, $param->moveup, $param->movedown,
69 $param->moveupcontext, $param->movedowncontext, $param->tocontext);
271e6dec 70 }
d3603157
TH
71}
72if ($param->delete && ($questionstomove = $DB->count_records("question", array("category" => $param->delete)))){
73 if (!$category = $DB->get_record("question_categories", array("id" => $param->delete))) { // security
74 print_error('nocate', 'question', $thispageurl->out(), $param->delete);
271e6dec 75 }
d3603157
TH
76 $categorycontext = get_context_instance_by_id($category->contextid);
77 $qcobject->moveform = new question_move_form($thispageurl,
78 array('contexts'=>array($categorycontext), 'currentcat'=>$param->delete));
79 if ($qcobject->moveform->is_cancelled()){
e42f153c 80 redirect($thispageurl);
d3603157
TH
81 } elseif ($formdata = $qcobject->moveform->get_data()) {
82 /// 'confirm' is the category to move existing questions to
83 list($tocategoryid, $tocontextid) = explode(',', $formdata->category);
84 $qcobject->move_questions_and_delete_category($formdata->delete, $tocategoryid);
21cbf9da 85 $thispageurl->remove_params('cat', 'category');
e42f153c 86 redirect($thispageurl);
271e6dec 87 }
d3603157
TH
88} else {
89 $questionstomove = 0;
90}
91if ($qcobject->catform->is_cancelled()) {
92 redirect($thispageurl);
93} else if ($catformdata = $qcobject->catform->get_data()) {
94 if (!$catformdata->id) {//new category
95 $qcobject->add_category($catformdata->parent, $catformdata->name, $catformdata->info);
96 } else {
97 $qcobject->update_category($catformdata->id, $catformdata->parent, $catformdata->name, $catformdata->info);
271e6dec 98 }
d3603157
TH
99 redirect($thispageurl);
100} else if ((!empty($param->delete) and (!$questionstomove) and confirm_sesskey())) {
101 $qcobject->delete_category($param->delete);//delete the category now no questions to move
102 $thispageurl->remove_params('cat', 'category');
103 redirect($thispageurl);
104}
b72ff476 105
d3603157
TH
106if ($param->edit){
107 $PAGE->navbar->add(get_string('editingcategory', 'question'));
108}
b72ff476 109
d3603157
TH
110$PAGE->set_title($streditingcategories);
111$PAGE->set_heading($COURSE->fullname);
112echo $OUTPUT->header();
aeb15530 113
d3603157
TH
114// display UI
115if (!empty($param->edit)) {
116 $qcobject->edit_single_category($param->edit);
117} else if ($questionstomove){
118 $qcobject->display_move_form($questionstomove, $category);
119} else {
120 // display the user interface
121 $qcobject->display_user_interface();
122}
123echo $OUTPUT->footer();