1 |
efrain |
1 |
<?php
|
|
|
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 |
|
|
|
17 |
/**
|
|
|
18 |
* Web run ad hoc task(s)
|
|
|
19 |
*
|
|
|
20 |
* This script runs a group or a single ad hoc task from the web UI.
|
|
|
21 |
*
|
|
|
22 |
* @package tool_task
|
|
|
23 |
* @copyright Catalyst IT
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
define('NO_OUTPUT_BUFFERING', true);
|
|
|
28 |
|
|
|
29 |
require_once(__DIR__ . '/../../../config.php');
|
|
|
30 |
require_once($CFG->libdir.'/adminlib.php');
|
|
|
31 |
|
|
|
32 |
admin_externalpage_setup('adhoctasks');
|
|
|
33 |
|
|
|
34 |
$runurl = '/admin/tool/task/run_adhoctasks.php';
|
|
|
35 |
$tasksurl = '/admin/tool/task/adhoctasks.php';
|
|
|
36 |
|
|
|
37 |
// Allow execution of single task. This requires login and has different rules.
|
|
|
38 |
$classname = optional_param('classname', null, PARAM_RAW);
|
|
|
39 |
$failedonly = optional_param('failedonly', false, PARAM_BOOL);
|
|
|
40 |
$taskid = optional_param('id', null, PARAM_INT);
|
|
|
41 |
$confirmed = optional_param('confirm', 0, PARAM_INT);
|
|
|
42 |
|
|
|
43 |
if (!\core\task\manager::is_runnable()) {
|
|
|
44 |
$redirecturl = new \moodle_url('/admin/settings.php', ['section' => 'systempaths']);
|
|
|
45 |
throw new moodle_exception('cannotfindthepathtothecli', 'tool_task', $redirecturl->out());
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
$params = ['classname' => $classname, 'failedonly' => $failedonly, 'id' => $taskid];
|
|
|
49 |
|
|
|
50 |
// Check input parameter id against all existing tasks.
|
|
|
51 |
if ($taskid) {
|
|
|
52 |
$record = $DB->get_record('task_adhoc', ['id' => $taskid]);
|
|
|
53 |
if (!$record) {
|
|
|
54 |
throw new \moodle_exception('invalidtaskid');
|
|
|
55 |
}
|
|
|
56 |
$classname = $record->classname;
|
|
|
57 |
$heading = get_string('runadhoctask', 'tool_task', ['task' => $classname, 'taskid' => $taskid]);
|
|
|
58 |
$tasks = [core\task\manager::adhoc_task_from_record($record)];
|
|
|
59 |
} else {
|
|
|
60 |
if (!$classname) {
|
|
|
61 |
throw new \moodle_exception('noclassname', 'tool_task');
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
$heading = get_string(
|
|
|
65 |
$failedonly ? 'runadhoctasksfailed' : 'runadhoctasks',
|
|
|
66 |
'tool_task',
|
|
|
67 |
s($classname),
|
|
|
68 |
);
|
|
|
69 |
|
|
|
70 |
$now = time();
|
|
|
71 |
$tasks = array_filter(
|
|
|
72 |
core\task\manager::get_adhoc_tasks($classname, $failedonly, true),
|
|
|
73 |
function ($t) use ($now) {
|
|
|
74 |
return $t->get_fail_delay() || $t->get_next_run_time() <= $now;
|
|
|
75 |
}
|
|
|
76 |
);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
// Start output.
|
|
|
80 |
$context = context_system::instance();
|
|
|
81 |
$PAGE->set_context($context);
|
|
|
82 |
$PAGE->set_heading($SITE->fullname);
|
|
|
83 |
$PAGE->set_title($classname);
|
|
|
84 |
|
|
|
85 |
echo $OUTPUT->header();
|
|
|
86 |
echo $OUTPUT->heading($heading);
|
|
|
87 |
|
|
|
88 |
if (!$tasks) {
|
|
|
89 |
echo $OUTPUT->single_button($tasksurl,
|
|
|
90 |
get_string('notasks', 'tool_task'),
|
|
|
91 |
'get');
|
|
|
92 |
echo $OUTPUT->footer();
|
|
|
93 |
exit;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
$renderer = $PAGE->get_renderer('tool_task');
|
|
|
97 |
if (!get_config('core', 'cron_enabled')) {
|
|
|
98 |
echo $renderer->cron_disabled();
|
|
|
99 |
}
|
|
|
100 |
echo $renderer->adhoc_tasks_simple_table($tasks);
|
|
|
101 |
|
|
|
102 |
// The initial request just shows the confirmation page; we don't do anything further unless
|
|
|
103 |
// they confirm.
|
|
|
104 |
if (!$confirmed) {
|
|
|
105 |
echo $OUTPUT->confirm(get_string('runadhoc_confirm', 'tool_task'),
|
|
|
106 |
new single_button(new moodle_url($runurl, array_merge($params, ['confirm' => 1])),
|
|
|
107 |
get_string('runadhoc', 'tool_task')),
|
|
|
108 |
new single_button(new moodle_url($tasksurl, $params),
|
|
|
109 |
get_string('cancel'), false));
|
|
|
110 |
echo $OUTPUT->footer();
|
|
|
111 |
exit;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
// Action requires session key.
|
|
|
115 |
require_sesskey();
|
|
|
116 |
|
|
|
117 |
\core\session\manager::write_close();
|
|
|
118 |
echo $OUTPUT->footer();
|
|
|
119 |
echo $OUTPUT->select_element_for_append();
|
|
|
120 |
|
|
|
121 |
// Prepare to handle output via mtrace.
|
|
|
122 |
require_once("{$CFG->dirroot}/{$CFG->admin}/tool/task/lib.php");
|
|
|
123 |
$CFG->mtrace_wrapper = 'tool_task_mtrace_wrapper';
|
|
|
124 |
|
|
|
125 |
// Run the specified tasks.
|
|
|
126 |
if ($taskid) {
|
|
|
127 |
$repeat = $DB->get_record('task_adhoc', ['id' => $taskid]);
|
|
|
128 |
|
|
|
129 |
echo html_writer::start_tag('pre', ['class' => 'task-output']);
|
|
|
130 |
\core\task\manager::run_adhoc_from_cli($taskid);
|
|
|
131 |
echo html_writer::end_tag('pre');
|
|
|
132 |
} else {
|
|
|
133 |
$repeat = core\task\manager::get_adhoc_tasks($classname, $failedonly, true);
|
|
|
134 |
|
|
|
135 |
// Run failed first (if any). We have to run them separately anyway,
|
|
|
136 |
// because faildelay is observed if failed flag is not true.
|
|
|
137 |
echo html_writer::tag('p', get_string('runningfailedtasks', 'tool_task'), ['class' => 'lead']);
|
|
|
138 |
echo html_writer::start_tag('pre', ['class' => 'task-output']);
|
|
|
139 |
\core\task\manager::run_all_adhoc_from_cli(true, $classname);
|
|
|
140 |
echo html_writer::end_tag('pre');
|
|
|
141 |
|
|
|
142 |
if (!$failedonly) {
|
|
|
143 |
echo html_writer::tag('p', get_string('runningalltasks', 'tool_task'), ['class' => 'lead']);
|
|
|
144 |
echo html_writer::start_tag('pre', ['class' => 'task-output']);
|
|
|
145 |
\core\task\manager::run_all_adhoc_from_cli(false, $classname);
|
|
|
146 |
echo html_writer::end_tag('pre');
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
if ($repeat) {
|
|
|
151 |
echo html_writer::div(
|
|
|
152 |
$OUTPUT->single_button(
|
|
|
153 |
new moodle_url($runurl, array_merge($params, ['confirm' => 1])),
|
|
|
154 |
get_string('runagain', 'tool_task')
|
|
|
155 |
)
|
|
|
156 |
);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
echo html_writer::div(
|
|
|
160 |
html_writer::link(
|
|
|
161 |
new moodle_url($tasksurl, $taskid ? ['classname' => $classname] : []),
|
|
|
162 |
get_string('backtoadhoctasks', 'tool_task')
|
|
|
163 |
)
|
|
|
164 |
);
|
|
|
165 |
|