Proyectos de Subversion Moodle

Rev

| 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
namespace mod_h5pactivity\external;
18
 
19
use core_external\external_api;
20
use core_external\external_function_parameters;
21
use core_external\external_multiple_structure;
22
use core_external\external_single_structure;
23
use core_external\external_value;
24
use core_external\external_warnings;
25
use context_module;
26
use core_h5p\factory;
27
 
28
/**
29
 * This is the external method for returning a list of h5p activities.
30
 *
31
 * @copyright  2020 Carlos Escobedo <carlos@moodle.com>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class get_h5pactivities_by_courses extends external_api {
35
    /**
36
     * Parameters.
37
     *
38
     * @return external_function_parameters
39
     */
40
    public static function execute_parameters(): external_function_parameters {
41
        return new external_function_parameters (
42
            [
43
                'courseids' => new external_multiple_structure(
44
                    new external_value(PARAM_INT, 'Course id'), 'Array of course ids', VALUE_DEFAULT, []
45
                ),
46
            ]
47
        );
48
    }
49
 
50
    /**
51
     * Returns a list of h5p activities in a provided list of courses.
52
     * If no list is provided all h5p activities that the user can view will be returned.
53
     *
54
     * @param  array $courseids course ids
55
     * @return array of h5p activities and warnings
56
     * @since Moodle 3.9
57
     */
58
    public static function execute(array $courseids): array {
59
        global $PAGE;
60
 
61
        $warnings = [];
62
        $returnedh5pactivities = [];
63
 
64
        $params = external_api::validate_parameters(self::execute_parameters(), [
65
            'courseids' => $courseids
66
        ]);
67
 
68
        $mycourses = [];
69
        if (empty($params['courseids'])) {
70
            $mycourses = enrol_get_my_courses();
71
            $params['courseids'] = array_keys($mycourses);
72
        }
73
 
74
        // Ensure there are courseids to loop through.
75
        if (!empty($params['courseids'])) {
76
 
77
            $factory = new factory();
78
 
79
            list($courses, $warnings) = \core_external\util::validate_courses($params['courseids'], $mycourses);
80
            $output = $PAGE->get_renderer('core');
81
 
82
            // Get the h5p activities in this course, this function checks users visibility permissions.
83
            // We can avoid then additional validate_context calls.
84
            $h5pactivities = get_all_instances_in_courses('h5pactivity', $courses);
85
            foreach ($h5pactivities as $h5pactivity) {
86
                $context = context_module::instance($h5pactivity->coursemodule);
87
                // Remove fields that are not from the h5p activity (added by get_all_instances_in_courses).
88
                unset($h5pactivity->coursemodule, $h5pactivity->context,
89
                    $h5pactivity->visible, $h5pactivity->section,
90
                    $h5pactivity->groupmode, $h5pactivity->groupingid);
91
 
92
                $exporter = new h5pactivity_summary_exporter($h5pactivity,
93
                    ['context' => $context, 'factory' => $factory]);
94
                $summary = $exporter->export($output);
95
                $returnedh5pactivities[] = $summary;
96
            }
97
        }
98
 
99
        $h5pglobalsettings = [
100
            'enablesavestate' => get_config('mod_h5pactivity', 'enablesavestate'),
101
        ];
102
        if (!empty($h5pglobalsettings['enablesavestate'])) {
103
            $h5pglobalsettings['savestatefreq'] = get_config('mod_h5pactivity', 'savestatefreq');
104
        }
105
 
106
        $result = [
107
            'h5pactivities' => $returnedh5pactivities,
108
            'h5pglobalsettings' => $h5pglobalsettings,
109
            'warnings' => $warnings
110
        ];
111
        return $result;
112
    }
113
 
114
    /**
115
     * Describes the get_h5pactivities_by_courses return value.
116
     *
117
     * @return external_single_structure
118
     * @since Moodle 3.9
119
     */
120
    public static function execute_returns() {
121
        return new external_single_structure(
122
            [
123
                'h5pactivities' => new external_multiple_structure(
124
                    h5pactivity_summary_exporter::get_read_structure()
125
                ),
126
                'h5pglobalsettings' => new external_single_structure(
127
                    [
128
                        'enablesavestate' => new external_value(PARAM_BOOL, 'Whether saving state is enabled.'),
129
                        'savestatefreq' => new external_value(PARAM_INT, 'How often (in seconds) state is saved.', VALUE_OPTIONAL),
130
                    ],
131
                    'H5P global settings',
132
                    VALUE_OPTIONAL,
133
                ),
134
                'warnings' => new external_warnings(),
135
            ]
136
        );
137
    }
138
}