forked from learnweb/moodle-tool_lifecycle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delayedcourses.php
159 lines (139 loc) · 5.55 KB
/
delayedcourses.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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.
//
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Display the delays of courses
*
* @package tool_lifecycle
* @copyright 2019 Justus Dieckmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use tool_lifecycle\local\form\form_delays_filter;
use tool_lifecycle\local\table\delayed_courses_table;
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
$PAGE->set_context(context_system::instance());
require_login();
require_capability('moodle/site:config', context_system::instance());
admin_externalpage_setup('tool_lifecycle_delayed_courses');
// Action handling (delete, bulk-delete).
$action = optional_param('action', null, PARAM_ALPHANUMEXT);
if ($action) {
global $DB;
require_sesskey();
if ($action == 'delete') {
$cid = required_param('cid', PARAM_INT);
$workflow = optional_param('workflow', null, PARAM_ALPHANUM);
if ($workflow) {
if (is_number($workflow)) {
$DB->delete_records('tool_lifecycle_delayed_workf', array('courseid' => $cid, 'workflowid' => $workflow));
} else if ($workflow == 'global') {
$DB->delete_records('tool_lifecycle_delayed', array('courseid' => $cid));
} else {
throw new \coding_exception('workflow has to be "global" or a int value');
}
} else {
$DB->delete_records('tool_lifecycle_delayed', array('courseid' => $cid));
$DB->delete_records('tool_lifecycle_delayed_workf', array('courseid' => $cid));
}
} else if ($action == 'bulk-delete') {
$workflow = optional_param('workflow', null, PARAM_ALPHANUM);
$deleteglobal = true;
$deleteseperate = true;
$workflowfilterid = null;
if ($workflow) {
if ($workflow == 'global') {
$deleteseperate = false;
} else if (is_number($workflow)) {
$deleteglobal = false;
$workflowfilterid = $workflow;
} else {
throw new \coding_exception('workflow has to be "global" or a int value');
}
}
$coursename = optional_param('coursename', null, PARAM_TEXT);
$categoryid = optional_param('catid', null, PARAM_INT);
$params = [];
$whereforcourse = [];
if ($coursename) {
$whereforcourse[] = 'c.fullname LIKE :cname';
$params['cname'] = '%' . $DB->sql_like_escape($coursename) . '%';
}
if ($categoryid) {
$whereforcourse[] = 'cat.id = :catid';
$params['catid'] = $categoryid;
}
$whereforcourse = implode(' AND ', $whereforcourse);
if ($deleteglobal) {
$sql = 'DELETE FROM {tool_lifecycle_delayed} d ';
if ($whereforcourse) {
$sql .= 'WHERE d.courseid IN ( ' .
'SELECT c.id FROM {course} c ' .
'JOIN {course_categories} cat ON c.category = cat.id ' .
'WHERE ' . $whereforcourse .
')';
}
$DB->execute($sql, $params);
}
if ($deleteseperate) {
$sql = 'DELETE FROM {tool_lifecycle_delayed_workf} dw ' .
'WHERE TRUE ';
if ($whereforcourse) {
$sql .= 'AND dw.courseid IN ( ' .
'SELECT c.id FROM {course} c ' .
'JOIN {course_categories} cat ON c.category = cat.id ' .
'WHERE ' . $whereforcourse .
')';
}
if ($workflowfilterid) {
$sql .= 'AND dw.workflowid = :workflowid';
$params['workflowid'] = $workflowfilterid;
}
$DB->execute($sql, $params);
}
}
redirect($PAGE->url);
}
$PAGE->set_url(new \moodle_url('/admin/tool/lifecycle/delayedcourses.php'));
$PAGE->set_title(get_string('delayed_courses_header', 'tool_lifecycle'));
$PAGE->set_heading(get_string('delayed_courses_header', 'tool_lifecycle'));
$mform = new form_delays_filter($PAGE->url);
// Cache handling.
$cache = cache::make('tool_lifecycle', 'mformdata');
if ($mform->is_cancelled()) {
$cache->delete('delays_filter');
redirect($PAGE->url);
} else if ($data = $mform->get_data()) {
$cache->set('delays_filter', $data);
} else {
$data = $cache->get('delays_filter');
if ($data) {
$mform->set_data($data);
}
}
$table = new delayed_courses_table($data);
$table->define_baseurl($PAGE->url);
echo $OUTPUT->header();
$mform->display();
$table->out(100, false);
$params = ['sesskey' => sesskey(), 'action' => 'bulk-delete'];
if ($data) {
$params = array_merge($params, (array) $data);
}
$button = new single_button(new moodle_url($PAGE->url, $params),
get_string('delete_all_delays', 'tool_lifecycle'));
echo "<br>";
echo $OUTPUT->render($button);
echo $OUTPUT->footer();