Commit | Line | Data |
---|---|---|
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 |
27 | require_once("../config.php"); |
28 | require_once($CFG->dirroot."/question/editlib.php"); | |
29 | require_once($CFG->dirroot."/question/category_class.php"); | |
30 | ||
31 | list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) = | |
32 | question_edit_setup('categories', '/question/category.php'); | |
b72ff476 | 33 | |
e4ca1184 | 34 | // Get values from form for actions on this page. |
d3603157 TH |
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); |
51 | foreach ((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 | |
e4ca1184 TH |
58 | $qcobject = new question_category_object($pagevars['cpage'], $thispageurl, |
59 | $contexts->having_one_edit_tab_cap('categories'), $param->edit, | |
60 | $pagevars['cat'], $param->delete, $contexts->having_cap('moodle/question:add')); | |
516cf3eb | 61 | |
e4ca1184 | 62 | if ($param->left || $param->right || $param->moveup || $param->movedown) { |
d3603157 | 63 | require_sesskey(); |
87bee3bc | 64 | |
e4ca1184 TH |
65 | foreach ($qcobject->editlists as $list) { |
66 | // Processing of these actions is handled in the method where appropriate and page redirects. | |
67 | $list->process_actions($param->left, $param->right, $param->moveup, $param->movedown); | |
68 | } | |
69 | } | |
70 | ||
71 | if ($param->moveupcontext || $param->movedowncontext) { | |
72 | require_sesskey(); | |
73 | ||
74 | if ($param->moveupcontext) { | |
75 | $catid = $param->moveupcontext; | |
87bee3bc | 76 | } else { |
e4ca1184 | 77 | $catid = $param->movedowncontext; |
271e6dec | 78 | } |
ee1052bd SR |
79 | $newtopcat = question_get_top_category($param->tocontext); |
80 | if (!$newtopcat) { | |
81 | print_error('invalidcontext'); | |
82 | } | |
e4ca1184 | 83 | $oldcat = $DB->get_record('question_categories', array('id' => $catid), '*', MUST_EXIST); |
93e435b9 | 84 | // Log the move to another context. |
4ca60a56 V |
85 | $category = new stdClass(); |
86 | $category->id = explode(',', $pagevars['cat'], -1)[0]; | |
87 | $category->contextid = $param->tocontext; | |
88 | $event = \core\event\question_category_moved::create_from_question_category_instance($category); | |
93e435b9 | 89 | $event->trigger(); |
ee1052bd | 90 | $qcobject->update_category($catid, "{$newtopcat->id},{$param->tocontext}", $oldcat->name, $oldcat->info); |
e4ca1184 | 91 | // The previous line does a redirect(). |
d3603157 | 92 | } |
e4ca1184 | 93 | |
54640aba | 94 | if ($param->delete) { |
31031e98 FM |
95 | if (!$category = $DB->get_record("question_categories", array("id" => $param->delete))) { |
96 | print_error('nocate', 'question', $thispageurl->out(), $param->delete); | |
271e6dec | 97 | } |
54640aba | 98 | |
31031e98 FM |
99 | question_remove_stale_questions_from_category($param->delete); |
100 | $questionstomove = $DB->count_records("question", array("category" => $param->delete)); | |
101 | ||
102 | // Second pass, if we still have questions to move, setup the form. | |
54640aba EM |
103 | if ($questionstomove) { |
104 | $categorycontext = context::instance_by_id($category->contextid); | |
105 | $qcobject->moveform = new question_move_form($thispageurl, | |
31031e98 FM |
106 | array('contexts' => array($categorycontext), 'currentcat' => $param->delete)); |
107 | if ($qcobject->moveform->is_cancelled()) { | |
54640aba | 108 | redirect($thispageurl); |
31031e98 | 109 | } else if ($formdata = $qcobject->moveform->get_data()) { |
54640aba EM |
110 | list($tocategoryid, $tocontextid) = explode(',', $formdata->category); |
111 | $qcobject->move_questions_and_delete_category($formdata->delete, $tocategoryid); | |
112 | $thispageurl->remove_params('cat', 'category'); | |
113 | redirect($thispageurl); | |
114 | } | |
271e6dec | 115 | } |
d3603157 TH |
116 | } else { |
117 | $questionstomove = 0; | |
118 | } | |
e4ca1184 | 119 | |
d3603157 TH |
120 | if ($qcobject->catform->is_cancelled()) { |
121 | redirect($thispageurl); | |
122 | } else if ($catformdata = $qcobject->catform->get_data()) { | |
fa5c608c TH |
123 | $catformdata->infoformat = $catformdata->info['format']; |
124 | $catformdata->info = $catformdata->info['text']; | |
d3603157 | 125 | if (!$catformdata->id) {//new category |
fa5c608c | 126 | $qcobject->add_category($catformdata->parent, $catformdata->name, |
6189fda4 | 127 | $catformdata->info, false, $catformdata->infoformat, $catformdata->idnumber); |
d3603157 | 128 | } else { |
fa5c608c | 129 | $qcobject->update_category($catformdata->id, $catformdata->parent, |
6189fda4 | 130 | $catformdata->name, $catformdata->info, $catformdata->infoformat, $catformdata->idnumber); |
271e6dec | 131 | } |
d3603157 TH |
132 | redirect($thispageurl); |
133 | } else if ((!empty($param->delete) and (!$questionstomove) and confirm_sesskey())) { | |
134 | $qcobject->delete_category($param->delete);//delete the category now no questions to move | |
135 | $thispageurl->remove_params('cat', 'category'); | |
136 | redirect($thispageurl); | |
137 | } | |
b72ff476 | 138 | |
e4ca1184 | 139 | if ($param->edit) { |
d3603157 TH |
140 | $PAGE->navbar->add(get_string('editingcategory', 'question')); |
141 | } | |
b72ff476 | 142 | |
e4ca1184 | 143 | $PAGE->set_title(get_string('editcategories', 'question')); |
d3603157 TH |
144 | $PAGE->set_heading($COURSE->fullname); |
145 | echo $OUTPUT->header(); | |
aeb15530 | 146 | |
0fa21eb7 AA |
147 | // Print horizontal nav if needed. |
148 | $renderer = $PAGE->get_renderer('core_question', 'bank'); | |
149 | echo $renderer->extra_horizontal_navigation(); | |
150 | ||
e4ca1184 | 151 | // Display the UI. |
d3603157 TH |
152 | if (!empty($param->edit)) { |
153 | $qcobject->edit_single_category($param->edit); | |
154 | } else if ($questionstomove){ | |
155 | $qcobject->display_move_form($questionstomove, $category); | |
156 | } else { | |
e4ca1184 | 157 | // Display the user interface. |
d3603157 TH |
158 | $qcobject->display_user_interface(); |
159 | } | |
160 | echo $OUTPUT->footer(); |