MDL-20601 fixed fatal SQL upgrade error, missing {} around table names
[moodle.git] / question / category.php
CommitLineData
516cf3eb 1<?php // $Id$
2/**
bc649d80 3 * Allows a teacher to create, edit and delete categories
4 *
bc649d80 5 * @author Martin Dougiamas and many others.
6 * {@link http://moodle.org}
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
4323d029 8 * @package questionbank
bc649d80 9 */
516cf3eb 10
e586cfb4 11 require_once("../config.php");
271e6dec 12 require_once($CFG->dirroot."/question/editlib.php");
13 require_once($CFG->dirroot."/question/category_class.php");
516cf3eb 14
b72ff476 15
16
271e6dec 17 list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) = question_edit_setup('categories');
b72ff476 18
19 // get values from form for actions on this page
516cf3eb 20 $param = new stdClass();
dac786f3 21
dac786f3 22
23 $param->moveup = optional_param('moveup', 0, PARAM_INT);
24 $param->movedown = optional_param('movedown', 0, PARAM_INT);
271e6dec 25 $param->moveupcontext = optional_param('moveupcontext', 0, PARAM_INT);
26 $param->movedowncontext = optional_param('movedowncontext', 0, PARAM_INT);
27 $param->tocontext = optional_param('tocontext', 0, PARAM_INT);
dac786f3 28 $param->left = optional_param('left', 0, PARAM_INT);
29 $param->right = optional_param('right', 0, PARAM_INT);
dac786f3 30 $param->delete = optional_param('delete', 0, PARAM_INT);
31 $param->confirm = optional_param('confirm', 0, PARAM_INT);
32 $param->cancel = optional_param('cancel', '', PARAM_ALPHA);
33 $param->move = optional_param('move', 0, PARAM_INT);
34 $param->moveto = optional_param('moveto', 0, PARAM_INT);
dac786f3 35 $param->edit = optional_param('edit', 0, PARAM_INT);
dac786f3 36
7f093351 37 $url = new moodle_url($thispageurl);
38 foreach ((array)$param as $key=>$value) {
39 if (($key !== 'cancel' && $value !== 0) || ($key === 'cancel' && $value !== '')) {
40 $url->param($key, $value);
41 }
42 }
43 $PAGE->set_url($url);
e2c80339 44
271e6dec 45 $qcobject = new question_category_object($pagevars['cpage'], $thispageurl, $contexts->having_one_edit_tab_cap('categories'), $param->edit, $pagevars['cat'], $param->delete,
46 $contexts->having_cap('moodle/question:add'));
516cf3eb 47
b72ff476 48 $streditingcategories = get_string('editcategories', 'quiz');
271e6dec 49 if ($param->left || $param->right || $param->moveup || $param->movedown|| $param->moveupcontext || $param->movedowncontext){
81d1f091 50 require_sesskey();
271e6dec 51 foreach ($qcobject->editlists as $list){
52 //processing of these actions is handled in the method where appropriate and page redirects.
53 $list->process_actions($param->left, $param->right, $param->moveup, $param->movedown,
54 $param->moveupcontext, $param->movedowncontext, $param->tocontext);
55 }
56 }
f34488b2 57 if ($param->delete && ($questionstomove = $DB->count_records("question", array("category" => $param->delete)))){
58 if (!$category = $DB->get_record("question_categories", array("id" => $param->delete))) { // security
0be2c858 59 print_error('nocate', 'question', $thispageurl->out(), $param->delete);
271e6dec 60 }
61 $categorycontext = get_context_instance_by_id($category->contextid);
62 $qcobject->moveform = new question_move_form($thispageurl,
63 array('contexts'=>array($categorycontext), 'currentcat'=>$param->delete));
64 if ($qcobject->moveform->is_cancelled()){
e42f153c 65 redirect($thispageurl);
271e6dec 66 } elseif ($formdata = $qcobject->moveform->get_data()) {
67 /// 'confirm' is the category to move existing questions to
767fac29 68 list($tocategoryid, $tocontextid) = explode(',', $formdata->category);
69 $qcobject->move_questions_and_delete_category($formdata->delete, $tocategoryid);
21cbf9da 70 $thispageurl->remove_params('cat', 'category');
e42f153c 71 redirect($thispageurl);
271e6dec 72 }
73 } else {
74 $questionstomove = 0;
75 }
21cbf9da 76 if ($qcobject->catform->is_cancelled()) {
e42f153c 77 redirect($thispageurl);
21cbf9da 78 } else if ($catformdata = $qcobject->catform->get_data()) {
271e6dec 79 if (!$catformdata->id) {//new category
80 $qcobject->add_category($catformdata->parent, $catformdata->name, $catformdata->info);
81 } else {
82 $qcobject->update_category($catformdata->id, $catformdata->parent, $catformdata->name, $catformdata->info);
83 }
e42f153c 84 redirect($thispageurl);
21cbf9da 85 } else if ((!empty($param->delete) and (!$questionstomove) and confirm_sesskey())) {
271e6dec 86 $qcobject->delete_category($param->delete);//delete the category now no questions to move
21cbf9da 87 $thispageurl->remove_params('cat', 'category');
e42f153c 88 redirect($thispageurl);
271e6dec 89 }
271e6dec 90 if ($cm!==null) {
dac786f3 91 // Page header
271e6dec 92 $strupdatemodule = has_capability('moodle/course:manageactivities', $contexts->lowest())
1d58b567 93 ? $OUTPUT->update_module_button($cm->id, $cm->modname)
516cf3eb 94 : "";
d412354c 95 $PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url($CFG->wwwroot.'/mod/'.$cm->modname.'/index.php', array('id'=>$COURSE->id)));
96 $PAGE->navbar->add(format_string($module->name), new moodle_url($CFG->wwwroot.'/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)));
271e6dec 97 } else {
98 // Print basic page layout.
99 $strupdatemodule = '';
271e6dec 100 }
101
102 if (!$param->edit){
d412354c 103 $PAGE->navbar->add($streditingcategories);
271e6dec 104 } else {
d412354c 105 $PAGE->navbar->add($streditingcategories, $thispageurl->out());
106 $PAGE->navbar->add(get_string('editingcategory', 'question'));
271e6dec 107 }
b72ff476 108
d412354c 109 $PAGE->set_title($streditingcategories);
110 $PAGE->set_button($strupdatemodule);
111 echo $OUTPUT->header();
b72ff476 112
271e6dec 113 // print tabs
114 if ($cm!==null) {
516cf3eb 115 $currenttab = 'edit';
116 $mode = 'categories';
b72ff476 117 ${$cm->modname} = $module;
118 include($CFG->dirroot."/mod/{$cm->modname}/tabs.php");
516cf3eb 119 } else {
567709c4 120 $currenttab = 'categories';
271e6dec 121 $context = $contexts->lowest();
567709c4 122 include('tabs.php');
516cf3eb 123 }
124
271e6dec 125 // display UI
126 if (!empty($param->edit)) {
127 $qcobject->edit_single_category($param->edit);
128 } else if ($questionstomove){
129 $qcobject->display_move_form($questionstomove, $category);
130 } else {
131 // display the user interface
132 $qcobject->display_user_interface();
516cf3eb 133 }
9b59580b 134 echo $OUTPUT->footer();
516cf3eb 135?>