Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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);
1441 ariadna 40
$dueonly = optional_param('dueonly', false, PARAM_BOOL);
1 efrain 41
$taskid = optional_param('id', null, PARAM_INT);
42
$confirmed = optional_param('confirm', 0, PARAM_INT);
43
 
44
if (!\core\task\manager::is_runnable()) {
45
    $redirecturl = new \moodle_url('/admin/settings.php', ['section' => 'systempaths']);
46
    throw new moodle_exception('cannotfindthepathtothecli', 'tool_task', $redirecturl->out());
47
}
48
 
1441 ariadna 49
$params = ['classname' => $classname, 'failedonly' => $failedonly, 'dueonly' => $dueonly, 'id' => $taskid];
1 efrain 50
 
51
// Check input parameter id against all existing tasks.
52
if ($taskid) {
53
    $record = $DB->get_record('task_adhoc', ['id' => $taskid]);
54
    if (!$record) {
55
        throw new \moodle_exception('invalidtaskid');
56
    }
57
    $classname = $record->classname;
58
    $heading = get_string('runadhoctask', 'tool_task', ['task' => $classname, 'taskid' => $taskid]);
59
    $tasks = [core\task\manager::adhoc_task_from_record($record)];
60
} else {
61
    if (!$classname) {
62
        throw new \moodle_exception('noclassname', 'tool_task');
63
    }
64
 
65
    $heading = get_string(
66
        $failedonly ? 'runadhoctasksfailed' : 'runadhoctasks',
67
        'tool_task',
68
        s($classname),
69
    );
70
 
71
    $now = time();
72
    $tasks = array_filter(
1441 ariadna 73
        core\task\manager::get_adhoc_tasks($classname, $failedonly, $dueonly, true),
1 efrain 74
        function ($t) use ($now) {
75
            return $t->get_fail_delay() || $t->get_next_run_time() <= $now;
76
        }
77
    );
78
}
79
 
80
// Start output.
81
$context = context_system::instance();
82
$PAGE->set_context($context);
83
$PAGE->set_heading($SITE->fullname);
84
$PAGE->set_title($classname);
85
 
86
echo $OUTPUT->header();
87
echo $OUTPUT->heading($heading);
88
 
89
if (!$tasks) {
90
    echo $OUTPUT->single_button($tasksurl,
91
            get_string('notasks', 'tool_task'),
92
            'get');
93
    echo $OUTPUT->footer();
94
    exit;
95
}
96
 
97
$renderer = $PAGE->get_renderer('tool_task');
98
if (!get_config('core', 'cron_enabled')) {
99
    echo $renderer->cron_disabled();
100
}
101
echo $renderer->adhoc_tasks_simple_table($tasks);
102
 
103
// The initial request just shows the confirmation page; we don't do anything further unless
104
// they confirm.
105
if (!$confirmed) {
106
    echo $OUTPUT->confirm(get_string('runadhoc_confirm', 'tool_task'),
107
            new single_button(new moodle_url($runurl, array_merge($params, ['confirm' => 1])),
108
            get_string('runadhoc', 'tool_task')),
109
            new single_button(new moodle_url($tasksurl, $params),
110
            get_string('cancel'), false));
111
    echo $OUTPUT->footer();
112
    exit;
113
}
114
 
115
// Action requires session key.
116
require_sesskey();
117
 
118
\core\session\manager::write_close();
119
echo $OUTPUT->footer();
120
echo $OUTPUT->select_element_for_append();
121
 
122
// Prepare to handle output via mtrace.
123
require_once("{$CFG->dirroot}/{$CFG->admin}/tool/task/lib.php");
124
$CFG->mtrace_wrapper = 'tool_task_mtrace_wrapper';
125
 
126
// Run the specified tasks.
127
if ($taskid) {
128
    $repeat = $DB->get_record('task_adhoc', ['id' => $taskid]);
129
 
130
    echo html_writer::start_tag('pre', ['class' => 'task-output']);
131
    \core\task\manager::run_adhoc_from_cli($taskid);
132
    echo html_writer::end_tag('pre');
133
} else {
1441 ariadna 134
    $repeat = core\task\manager::get_adhoc_tasks($classname, $failedonly, $dueonly, true);
1 efrain 135
 
136
    // Run failed first (if any). We have to run them separately anyway,
137
    // because faildelay is observed if failed flag is not true.
1441 ariadna 138
    if (!$dueonly) {
139
        echo html_writer::tag('p', get_string('runningfailedtasks', 'tool_task'), ['class' => 'lead']);
140
        echo html_writer::start_tag('pre', ['class' => 'task-output']);
141
        \core\task\manager::run_all_adhoc_from_cli(true, $classname);
142
        echo html_writer::end_tag('pre');
143
    }
1 efrain 144
 
145
    if (!$failedonly) {
146
        echo html_writer::tag('p', get_string('runningalltasks', 'tool_task'), ['class' => 'lead']);
147
        echo html_writer::start_tag('pre', ['class' => 'task-output']);
148
        \core\task\manager::run_all_adhoc_from_cli(false, $classname);
149
        echo html_writer::end_tag('pre');
150
    }
151
}
152
 
153
if ($repeat) {
154
    echo html_writer::div(
155
        $OUTPUT->single_button(
156
            new moodle_url($runurl, array_merge($params, ['confirm' => 1])),
157
            get_string('runagain', 'tool_task')
158
        )
159
    );
160
}
161
 
162
echo html_writer::div(
163
    html_writer::link(
164
        new moodle_url($tasksurl, $taskid ? ['classname' => $classname] : []),
165
        get_string('backtoadhoctasks', 'tool_task')
166
    )
167
);
168