Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core\output;
18
 
19
use core\plugin_manager;
20
use core\task\adhoc_task;
21
use core\task\stored_progress_task_trait;
22
use core\url;
23
use core\context\system;
24
use stdClass;
25
 
26
/**
27
 * Indicator for displaying status and progress of a background task
28
 *
29
 * This will display a section containing an icon, heading and message describing the background task being performed,
30
 * as well as a progress bar that is updated as the task progresses. Optionally, it will redirect to a given URL (or reload
31
 * the current one) when the task completes. If the task is still waiting in the queue, an admin viewing the indicator
32
 * will also see a "Run now" button.
33
 *
34
 * @package   core
35
 * @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
36
 * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
37
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class task_indicator implements renderable, templatable {
40
    /** @var ?stdClass $taskrecord */
41
    protected ?stdClass $taskrecord;
42
 
43
    /** @var ?stored_progress_bar $progressbar */
44
    protected ?stored_progress_bar $progressbar;
45
 
46
    /** @var ?url $runurl The URL to manually run the task. */
47
    protected ?url $runurl = null;
48
 
49
    /** @var string $runlabel Label for the link to run the task. */
50
    protected string $runlabel = '';
51
 
52
    /**
53
     * Find the task record, and get the progress bar object.
54
     *
55
     * @param adhoc_task $task The task whose progress is being indicated. The task class must use stored_progress_task_trait.
56
     * @param string $heading The header text for the indicator.
57
     * @param string $message A message to explain what is happening while the task is running.
58
     * @param ?url $redirecturl An optional URL to redirect to when the task completes.
59
     * @param ?pix_icon $icon An optional icon to display with the heading.
60
     * @param array $extraclasses Extra class names to apply to the indicator's container.
61
     * @throws \coding_exception
62
     */
63
    public function __construct(
64
        /** @var adhoc_task $task The task whose progress is being indicated. The task class must use stored_progress_task_trait. */
65
        protected adhoc_task $task,
66
        /** @var string $heading The header text for the indicator. */
67
        protected string $heading,
68
        /** @var string $message A message to explain what is happening while the task is running. */
69
        protected string $message,
70
        /** @var ?url $redirecturl An optional URL to redirect to when the task completes. */
71
        protected ?url $redirecturl = null,
72
        /** @var ?pix_icon $icon An optional icon to display with the heading. */
73
        protected ?pix_icon $icon = new pix_icon('i/timer', ''),
74
        /** @var array $extraclasses Extra class names to apply to the indicator's container. */
75
        protected array $extraclasses = [],
76
    ) {
77
        if (!class_uses($task::class, stored_progress_task_trait::class)) {
78
            throw new \coding_exception('task_indicator can only be used for tasks using stored_progress_task_trait.');
79
        }
80
        $this->setup_task_data();
81
    }
82
 
83
    /**
84
     * Fetch the task record matching the current task, if there is one.
85
     *
86
     * If one exists, also set up a progress bar, and set up the "run now" link if permitted.
87
     *
88
     * @return void
89
     */
90
    protected function setup_task_data(): void {
91
        $this->taskrecord = \core\task\manager::get_queued_adhoc_task_record($this->task) ?: null;
92
        if ($this->taskrecord) {
93
            $this->task->set_id($this->taskrecord->id);
94
            $idnumber = stored_progress_bar::convert_to_idnumber($this->task::class, $this->task->get_id());
95
            $this->progressbar = stored_progress_bar::get_by_idnumber($idnumber);
96
            // As long as the tool_task plugin hasn't been removed,
97
            // allow admins to trigger the task manually if it's not running yet.
98
            if (
99
                array_key_exists('task', plugin_manager::instance()->get_present_plugins('tool'))
100
                && is_null($this->taskrecord->timestarted)
101
                && has_capability('moodle/site:config', system::instance())
102
            ) {
103
                $this->runurl = new url('/admin/tool/task/run_adhoctasks.php', ['id' => $this->taskrecord->id]);
104
                $this->runlabel = get_string('runnow', 'tool_task');
105
            }
106
        }
107
    }
108
 
109
    /**
110
     * Check if there is a task record matching the defined task.
111
     *
112
     * If so, set the task ID and progress bar, then return true. If not, return false.
113
     *
114
     * @return bool
115
     */
116
    public function has_task_record(): bool {
117
        $this->setup_task_data();
118
        return !is_null($this->taskrecord);
119
    }
120
 
121
    #[\Override]
122
    public function export_for_template(renderer_base $output): array {
123
        $export = [];
124
        if ($this->taskrecord) {
125
            $export['heading'] = $this->heading;
126
            $export['message'] = $this->message;
127
            $export['progress'] = $this->progressbar->export_for_template($output);
128
            $export['icon'] = $this->icon ? $this->icon->export_for_template($output) : '';
129
            $export['redirecturl'] = $this->redirecturl?->out();
130
            $export['extraclasses'] = implode(' ', $this->extraclasses);
131
            $export['runurl'] = $this->runurl?->out();
132
            $export['runlabel'] = $this->runlabel;
133
            $this->progressbar->init_js();
134
        }
135
        return $export;
136
    }
137
}