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\task;
18
 
19
use core\progress\db_updater;
20
use core\progress\stored;
21
use core\output\stored_progress_bar;
22
 
23
/**
24
 * Trait to use in tasks to automatically add stored progress functionality.
25
 *
26
 * @package    core
27
 * @copyright  2024 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @author     Conn Warwicker <conn.warwicker@catalyst-eu.net>
30
 */
31
trait stored_progress_task_trait {
32
    /** @var ?stored_progress_bar $progress */
33
    protected $progress = null;
34
 
35
    /**
36
     * Construct a unique name for the progress bar.
37
     *
38
     * For adhoc tasks, this will need the ID in it. For scheduled tasks just the class name.
39
     *
40
     * @return string
41
     */
42
    protected function get_progress_name(): string {
43
        if (method_exists($this, 'get_id')) {
44
            return get_class($this) . '_' . $this->get_id();
45
        } else {
46
            return get_class($this);
47
        }
48
    }
49
 
50
    /**
51
     * Initialise a stored progress record.
52
     */
53
    public function initialise_stored_progress(): void {
54
        $this->progress = new stored_progress_bar(
55
            stored_progress_bar::convert_to_idnumber($this->get_progress_name()),
56
            autostart: false,
57
        );
58
        $this->progress->store_pending();
59
    }
60
 
61
    /**
62
     * Get a stored object for the stored progress record.
63
     *
64
     * @return stored
65
     */
66
    public function get_progress(): stored {
67
        return new stored($this->progress);
68
    }
69
 
70
    /**
71
     * Start a stored progress bar implementation for the task this trait is used in.
72
     *
73
     * @return void
74
     */
75
    protected function start_stored_progress(): void {
76
        global $OUTPUT, $PAGE;
77
 
78
        // To get around the issue in MDL-80770, we are manually setting the renderer to cli.
79
        $OUTPUT = $PAGE->get_renderer('core', null, 'cli');
80
 
81
        $this->progress = new stored_progress_bar(
82
            stored_progress_bar::convert_to_idnumber($this->get_progress_name())
83
        );
84
 
85
        // Start the progress.
86
        $this->progress->start();
87
    }
88
 
89
}