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\external\output;
18
 
19
use core\output\stored_progress_bar;
20
use core_external\external_api;
21
use core_external\external_function_parameters;
22
use core_external\external_multiple_structure;
23
use core_external\external_single_structure;
24
use core_external\external_value;
25
 
26
/**
27
 * Poll Stored Progress webservice.
28
 *
29
 * @package    core
30
 * @copyright  2023 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 * @author     Conn Warwicker <conn.warwicker@catalyst-eu.net>
33
 */
34
class poll_stored_progress extends external_api {
35
 
36
    /**
37
     * Returns description of method parameters
38
     *
39
     * @return external_function_parameters
40
     */
41
    public static function execute_parameters(): external_function_parameters {
42
        return new external_function_parameters([
43
            'ids' => new external_multiple_structure(
44
                new external_value(PARAM_INT, 'The stored_progress ID', VALUE_REQUIRED)
45
            ),
46
        ]);
47
    }
48
 
49
    /**
50
     * Returns description of method return data
51
     *
52
     * @return external_multiple_structure
53
     */
54
    public static function execute_returns(): external_multiple_structure {
55
        return new external_multiple_structure(
56
            new external_single_structure([
57
                'id' => new external_value(PARAM_INT, 'stored_progress record id'),
58
                'uniqueid' => new external_value(PARAM_TEXT, 'unique element id'),
59
                'progress' => new external_value(PARAM_FLOAT, 'percentage progress'),
60
                'estimated' => new external_value(PARAM_RAW, 'estimated time left string'),
61
                'message' => new external_value(PARAM_TEXT, 'message to be displayed with the bar'),
62
                'error' => new external_value(PARAM_TEXT, 'error', VALUE_OPTIONAL),
63
                'timeout' => new external_value(PARAM_TEXT, 'timeout to use in the polling', VALUE_OPTIONAL),
64
            ])
65
        );
66
    }
67
 
68
    /**
69
     * Poll the database for the progress of stored progress objects
70
     *
71
     * @param array $ids
72
     * @return array
73
     */
74
    public static function execute(array $ids): array {
75
        $params = self::validate_parameters(self::execute_parameters(), [
76
            'ids' => $ids,
77
        ]);
78
 
79
        $return = [];
80
        $ids = $params['ids'];
81
        foreach ($ids as $id) {
82
            // Load the stored progress bar object.
83
            $bar = stored_progress_bar::get_by_id($id);
84
            if ($bar) {
85
                // Return the updated bar data.
86
                $return[$id] = [
87
                    'id' => $id,
88
                    'uniqueid' => $bar->get_id(),
89
                    'progress' => $bar->get_percent(),
90
                    'estimated' => $bar->get_estimate_message($bar->get_percent()),
91
                    'message' => $bar->get_message(),
92
                    'timeout' => stored_progress_bar::get_timeout(),
93
                    'error' => $bar->get_haserrored(),
94
                ];
95
 
96
            } else {
97
                // If we could not find the record, we still need to return the right arguments in the array for the webservice.
98
                $return[$id] = [
99
                    'id' => $id,
100
                    'uniqueid' => '',
101
                    'progress' => 0,
102
                    'estimated' => '',
103
                    'message' => get_string('invalidrecordunknown', 'error'),
104
                    'timeout' => stored_progress_bar::get_timeout(),
105
                    'error' => true,
106
                ];
107
            }
108
        }
109
 
110
        return $return;
111
    }
112
}