This repository has been archived by the owner on Jan 10, 2021. It is now read-only.
forked from trampgeek/moodle-qtype_coderunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deletecategorytree.php
92 lines (79 loc) · 3.06 KB
/
deletecategorytree.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
// This file is part of CodeRunner - http://coderunner.org.nz
//
// CodeRunner is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// CodeRunner is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Stack. If not, see <http://www.gnu.org/licenses/>.
/**
* This script deletes all categories in the category tree rooted at the
* categoryid passed as a parameter, provided all nodes in that tree are
* empty.
*
*
* @package qtype_coderunner
* @copyright 2018 Richard Lobb, The University of Canterbury
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__.'/../../../config.php');
require_once($CFG->libdir . '/questionlib.php');
// Create the helper class.
$bulktester = new qtype_coderunner_bulk_tester();
function display_and_count($catid, $level) {
global $bulktester;
$count = $bulktester->count_questions_in_category($catid);
$s = '';
for ($i = 0; $i < $level; $i++) {
$s .= ' ';
}
$s .= $bulktester->category_name($catid) . " ($count questions)";
echo html_writer::tag('pre', $s);
$children = $bulktester->child_categories($catid);
foreach ($children as $child) {
$count += display_and_count($child, $level + 1);
}
return $count;
}
// Delete all categories rooted at the given category id.
// The tree must be empty of questions.
function delete_tree($categoryid) {
global $bulktester;
$children = $bulktester->child_categories($categoryid);
foreach ($children as $child) {
delete_tree($child);
}
$bulktester->delete_category($categoryid);
}
// Get the parameters from the URL.
$contextid = required_param('contextid', PARAM_INT);
$categoryid = required_param('categoryid', PARAM_INT);
$categoryname = required_param('categoryname', PARAM_RAW);
// Login and check permissions.
require_login();
$context = context::instance_by_id($contextid);
require_capability('moodle/question:managecategory', $context);
$PAGE->set_url('/question/type/coderunner/deletecategorytree.php');
$PAGE->set_context($context);
$PAGE->set_title('Delete category tree');
// Create the helper class.
$bulktester = new qtype_coderunner_bulk_tester();
// Display.
echo $OUTPUT->header();
echo $OUTPUT->heading('Deleting Category Tree');
echo html_writer::tag('h3', 'Preparing to delete the following tree');
$num_questions = display_and_count($categoryid, 0);
if ($num_questions != 0) {
echo html_writer::tag('p', "Non-empty category tree ($num_questions questions). No deletions performed");
} else {
delete_tree($categoryid);
echo html_writer::tag('p', "All the above categories have now been deleted");
}
echo $OUTPUT->footer();