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
namespace mod_h5pactivity\external;
18
 
19
use mod_h5pactivity\local\manager;
20
use mod_h5pactivity\local\attempt;
21
use mod_h5pactivity\local\report;
22
use mod_h5pactivity\local\report\attempts as report_attempts;
23
use core_external\external_api;
24
use core_external\external_function_parameters;
25
use core_external\external_multiple_structure;
26
use core_external\external_single_structure;
27
use core_external\external_value;
28
use core_external\external_warnings;
29
use moodle_exception;
30
use context_module;
31
use stdClass;
32
 
33
/**
34
 * This is the external method to return the information needed to list all enrolled user attempts.
35
 *
36
 * @package    mod_h5pactivity
37
 * @since      Moodle 3.11
38
 * @copyright  2020 Ilya Tregubov <ilya@moodle.com>
39
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class get_user_attempts extends external_api {
42
 
43
    /**
44
     * Webservice parameters.
45
     *
46
     * @return external_function_parameters
47
     */
48
    public static function execute_parameters(): external_function_parameters {
49
        return new external_function_parameters(
50
            [
51
                'h5pactivityid' => new external_value(PARAM_INT, 'h5p activity instance id'),
52
                'sortorder' => new external_value(PARAM_TEXT,
53
                    'sort by either user id, firstname or lastname (with optional asc/desc)', VALUE_DEFAULT, 'id ASC'),
54
                'page' => new external_value(PARAM_INT, 'current page', VALUE_DEFAULT, -1),
55
                'perpage' => new external_value(PARAM_INT, 'items per page', VALUE_DEFAULT, 0),
56
                'firstinitial' => new external_value(PARAM_TEXT, 'Users whose first name ' .
57
                    'starts with $firstinitial', VALUE_DEFAULT, ''),
58
                'lastinitial' => new external_value(PARAM_TEXT, 'Users whose last name ' .
59
                    'starts with $lastinitial', VALUE_DEFAULT, ''),
60
            ]
61
        );
62
    }
63
 
64
    /**
65
     * Return user attempts information in a h5p activity.
66
     *
67
     * @throws  moodle_exception if the user cannot see the report
68
     * @param  int $h5pactivityid The h5p activity id
69
     * @param int $sortorder The sort order
70
     * @param int $page page number
71
     * @param int $perpage items per page
72
     * @param int $firstinitial Users whose first name starts with $firstinitial
73
     * @param int $lastinitial Users whose last name starts with $lastinitial
74
     * @return stdClass report data
75
     */
76
    public static function execute(int $h5pactivityid, $sortorder = 'id ASC', ?int $page = 0,
77
            ?int $perpage = 0, $firstinitial = '', $lastinitial = ''): stdClass {
78
        [
79
            'h5pactivityid' => $h5pactivityid,
80
            'sortorder' => $sortorder,
81
            'page' => $page,
82
            'perpage' => $perpage,
83
            'firstinitial' => $firstinitial,
84
            'lastinitial' => $lastinitial,
85
        ] = external_api::validate_parameters(self::execute_parameters(), [
86
            'h5pactivityid' => $h5pactivityid,
87
            'sortorder' => $sortorder,
88
            'page' => $page,
89
            'perpage' => $perpage,
90
            'firstinitial' => $firstinitial,
91
            'lastinitial' => $lastinitial,
92
        ]);
93
 
94
        $warnings = [];
95
 
96
        [$course, $cm] = get_course_and_cm_from_instance($h5pactivityid, 'h5pactivity');
97
 
98
        $context = context_module::instance($cm->id);
99
        self::validate_context($context);
100
 
101
        $manager = manager::create_from_coursemodule($cm);
102
        $instance = $manager->get_instance();
103
        if (!$manager->can_view_all_attempts()) {
104
            throw new moodle_exception('nopermissiontoviewattempts', 'error', '', null,
105
                'h5pactivity:reviewattempts required view attempts of all enrolled users.');
106
        }
107
 
108
        // Ensure sortorder parameter is safe to use. Fallback to default value of the parameter itself.
109
        $sortorderparts = explode(' ', $sortorder, 2);
110
        $sortorder = get_safe_orderby([
111
            'id' => 'u.id',
112
            'firstname' => 'u.firstname',
113
            'lastname' => 'u.lastname',
114
            'default' => 'u.id',
115
        ], $sortorderparts[0], $sortorderparts[1] ?? '');
116
 
117
        $users = self::get_active_users($manager, 'u.id, u.firstname, u.lastname',
118
            $sortorder, $page * $perpage, $perpage);
119
 
120
        $usersattempts = [];
121
 
122
        foreach ($users as $user) {
123
 
124
            if ($firstinitial) {
125
                if (strpos($user->firstname, $firstinitial) === false) {
126
                    continue;
127
                }
128
            }
129
 
130
            if ($lastinitial) {
131
                if (strpos($user->lastname, $lastinitial) === false) {
132
                    continue;
133
                }
134
            }
135
 
136
            $report = $manager->get_report($user->id);
137
            if ($report && $report instanceof report_attempts) {
138
                $usersattempts[] = self::export_user_attempts($report, $user->id);
139
            } else {
140
                $warnings[] = [
141
                    'item' => 'user',
142
                    'itemid' => $user->id,
143
                    'warningcode' => '1',
144
                    'message' => "Cannot access user attempts",
145
                ];
146
            }
147
        }
148
 
149
        $result = (object)[
150
            'activityid' => $instance->id,
151
            'usersattempts' => $usersattempts,
1441 ariadna 152
            'totalattempts' => $manager->count_attempts(),
1 efrain 153
            'warnings' => $warnings,
154
        ];
155
 
156
        return $result;
157
    }
158
 
159
    /**
160
     * Generate the active users list
161
     *
162
     * @param manager $manager the h5pactivity manager
163
     * @param string $userfields the user fields to get
164
     * @param string $sortorder the SQL sortorder
165
     * @param int $limitfrom SQL limit from
166
     * @param int $limitnum SQL limit num
167
     */
168
    private static function get_active_users(
169
        manager $manager,
170
        string $userfields = 'u.*',
171
        string $sortorder = '',
172
        int $limitfrom = 0,
173
        int $limitnum = 0
174
    ): array {
175
 
176
        global $DB;
177
 
178
        $capjoin = $manager->get_active_users_join(true);
179
 
180
        // Final SQL.
181
        $sql = "SELECT DISTINCT {$userfields}
182
                  FROM {user} u {$capjoin->joins}
183
                 WHERE {$capjoin->wheres}
184
                       {$sortorder}";
185
 
186
        return $DB->get_records_sql($sql, $capjoin->params, $limitfrom, $limitnum);
187
    }
188
 
189
    /**
190
     * Export attempts data for a specific user.
191
     *
192
     * @param report $report the report attempts object
193
     * @param int $userid the user id
194
     * @return stdClass
195
     */
196
    private static function export_user_attempts(report $report, int $userid): stdClass {
197
        $scored = $report->get_scored();
198
        $attempts = $report->get_attempts();
199
 
200
        $result = (object)[
201
            'userid' => $userid,
202
            'attempts' => [],
203
        ];
204
 
205
        foreach ($attempts as $attempt) {
206
            $result->attempts[] = self::export_attempt($attempt);
207
        }
208
 
209
        if (!empty($scored)) {
210
            $result->scored = (object)[
211
                'title' => $scored->title,
212
                'grademethod' => $scored->grademethod,
213
                'attempts' => [self::export_attempt($scored->attempt)],
214
            ];
215
        }
216
 
217
        return $result;
218
    }
219
 
220
    /**
221
     * Return a data object from an attempt.
222
     *
223
     * @param attempt $attempt the attempt object
224
     * @return stdClass a WS compatible version of the attempt
225
     */
226
    private static function export_attempt(attempt $attempt): stdClass {
227
        $result = (object)[
228
            'id' => $attempt->get_id(),
229
            'h5pactivityid' => $attempt->get_h5pactivityid(),
230
            'userid' => $attempt->get_userid(),
231
            'timecreated' => $attempt->get_timecreated(),
232
            'timemodified' => $attempt->get_timemodified(),
233
            'attempt' => $attempt->get_attempt(),
234
            'rawscore' => $attempt->get_rawscore(),
235
            'maxscore' => $attempt->get_maxscore(),
236
            'duration' => $attempt->get_duration(),
237
            'scaled' => $attempt->get_scaled(),
238
        ];
239
        if ($attempt->get_completion() !== null) {
240
            $result->completion = $attempt->get_completion();
241
        }
242
        if ($attempt->get_success() !== null) {
243
            $result->success = $attempt->get_success();
244
        }
245
        return $result;
246
    }
247
 
248
    /**
249
     * Describes the get_h5pactivity_access_information return value.
250
     *
251
     * @return external_single_structure
252
     */
253
    public static function execute_returns(): external_single_structure {
254
        return new external_single_structure([
255
            'activityid' => new external_value(PARAM_INT, 'Activity course module ID'),
256
            'usersattempts' => new external_multiple_structure(
257
                self::get_user_attempts_returns(), 'The complete users attempts list'
258
            ),
1441 ariadna 259
            'totalattempts' => new external_value(PARAM_INT, 'Total number of attempts'),
1 efrain 260
            'warnings' => new external_warnings(),
261
        ], 'Activity attempts data');
262
    }
263
 
264
    /**
265
     * Describes the get_h5pactivity_access_information return value.
266
     *
267
     * @return external_single_structure
268
     */
269
    private static function get_user_attempts_returns(): external_single_structure {
270
        $structure = [
271
            'userid' => new external_value(PARAM_INT, 'The user id'),
272
            'attempts' => new external_multiple_structure(self::get_user_attempt_returns(), 'The complete attempts list'),
273
            'scored' => new external_single_structure([
274
                'title' => new external_value(PARAM_NOTAGS, 'Scored attempts title'),
275
                'grademethod' => new external_value(PARAM_NOTAGS, 'Grading method'),
276
                'attempts' => new external_multiple_structure(self::get_user_attempt_returns(), 'List of the grading attempts'),
277
            ], 'Attempts used to grade the activity', VALUE_OPTIONAL),
278
        ];
279
        return new external_single_structure($structure);
280
    }
281
 
282
    /**
283
     * Return the external structure of an attempt.
284
     *
285
     * @return external_single_structure
286
     */
287
    private static function get_user_attempt_returns(): external_single_structure {
288
        $result = new external_single_structure([
289
            'id' => new external_value(PARAM_INT, 'ID of the context'),
290
            'h5pactivityid' => new external_value(PARAM_INT, 'ID of the H5P activity'),
291
            'userid' => new external_value(PARAM_INT, 'ID of the user'),
292
            'timecreated' => new external_value(PARAM_INT, 'Attempt creation'),
293
            'timemodified' => new external_value(PARAM_INT, 'Attempt modified'),
294
            'attempt' => new external_value(PARAM_INT, 'Attempt number'),
295
            'rawscore' => new external_value(PARAM_INT, 'Attempt score value'),
296
            'maxscore' => new external_value(PARAM_INT, 'Attempt max score'),
297
            'duration' => new external_value(PARAM_INT, 'Attempt duration in seconds'),
298
            'completion' => new external_value(PARAM_INT, 'Attempt completion', VALUE_OPTIONAL),
299
            'success' => new external_value(PARAM_INT, 'Attempt success', VALUE_OPTIONAL),
300
            'scaled' => new external_value(PARAM_FLOAT, 'Attempt scaled'),
301
        ]);
302
        return $result;
303
    }
304
}