Commit | Line | Data |
---|---|---|
e9bf3011 JL |
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 | /** | |
18 | * SCORM module external API | |
19 | * | |
20 | * @package mod_scorm | |
21 | * @category external | |
22 | * @copyright 2015 Juan Leyva <juan@moodle.com> | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | * @since Moodle 3.0 | |
25 | */ | |
26 | ||
27 | defined('MOODLE_INTERNAL') || die; | |
28 | ||
29 | require_once($CFG->libdir . '/externallib.php'); | |
30 | require_once($CFG->dirroot . '/mod/scorm/lib.php'); | |
22de67f4 | 31 | require_once($CFG->dirroot . '/mod/scorm/locallib.php'); |
e9bf3011 JL |
32 | |
33 | /** | |
34 | * SCORM module external functions | |
35 | * | |
36 | * @package mod_scorm | |
37 | * @category external | |
38 | * @copyright 2015 Juan Leyva <juan@moodle.com> | |
39 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
40 | * @since Moodle 3.0 | |
41 | */ | |
42 | class mod_scorm_external extends external_api { | |
43 | ||
44 | /** | |
45 | * Returns description of method parameters | |
46 | * | |
47 | * @return external_function_parameters | |
48 | * @since Moodle 3.0 | |
49 | */ | |
50 | public static function view_scorm_parameters() { | |
51 | return new external_function_parameters( | |
52 | array( | |
53 | 'scormid' => new external_value(PARAM_INT, 'scorm instance id') | |
54 | ) | |
55 | ); | |
56 | } | |
57 | ||
58 | /** | |
59 | * Trigger the course module viewed event. | |
60 | * | |
61 | * @param int $scormid the scorm instance id | |
62 | * @return array of warnings and status result | |
63 | * @since Moodle 3.0 | |
64 | * @throws moodle_exception | |
65 | */ | |
66 | public static function view_scorm($scormid) { | |
67 | global $DB, $CFG; | |
68 | require_once($CFG->dirroot . '/mod/scorm/lib.php'); | |
69 | ||
70 | $params = self::validate_parameters(self::view_scorm_parameters(), | |
71 | array( | |
72 | 'scormid' => $scormid | |
73 | )); | |
74 | $warnings = array(); | |
75 | ||
76 | // Request and permission validation. | |
77 | $scorm = $DB->get_record('scorm', array('id' => $params['scormid']), '*', MUST_EXIST); | |
78 | list($course, $cm) = get_course_and_cm_from_instance($scorm, 'scorm'); | |
79 | ||
80 | $context = context_module::instance($cm->id); | |
81 | self::validate_context($context); | |
82 | ||
83 | // Call the scorm/lib API. | |
84 | scorm_view($scorm, $course, $cm, $context); | |
85 | ||
86 | $result = array(); | |
87 | $result['status'] = true; | |
88 | $result['warnings'] = $warnings; | |
89 | return $result; | |
90 | } | |
91 | ||
92 | /** | |
93 | * Returns description of method result value | |
94 | * | |
95 | * @return external_description | |
96 | * @since Moodle 3.0 | |
97 | */ | |
98 | public static function view_scorm_returns() { | |
99 | return new external_single_structure( | |
100 | array( | |
101 | 'status' => new external_value(PARAM_BOOL, 'status: true if success'), | |
102 | 'warnings' => new external_warnings() | |
103 | ) | |
104 | ); | |
105 | } | |
22de67f4 JL |
106 | |
107 | /** | |
108 | * Describes the parameters for get_scorm_attempt_count. | |
109 | * | |
110 | * @return external_function_parameters | |
111 | * @since Moodle 3.0 | |
112 | */ | |
113 | public static function get_scorm_attempt_count_parameters() { | |
114 | return new external_function_parameters( | |
115 | array( | |
116 | 'scormid' => new external_value(PARAM_INT, 'SCORM instance id'), | |
117 | 'userid' => new external_value(PARAM_INT, 'User id'), | |
118 | 'ignoremissingcompletion' => new external_value(PARAM_BOOL, | |
119 | 'Ignores attempts that haven\'t reported a grade/completion', | |
120 | VALUE_DEFAULT, false), | |
121 | ) | |
122 | ); | |
123 | } | |
124 | ||
125 | /** | |
126 | * Return the number of attempts done by a user in the given SCORM. | |
127 | * | |
128 | * @param int $scormid the scorm id | |
129 | * @param int $userid the user id | |
130 | * @param bool $ignoremissingcompletion ignores attempts that haven't reported a grade/completion | |
131 | * @return array of warnings and the attempts count | |
132 | * @since Moodle 3.0 | |
133 | */ | |
134 | public static function get_scorm_attempt_count($scormid, $userid, $ignoremissingcompletion = false) { | |
135 | global $USER, $DB; | |
136 | ||
137 | $params = self::validate_parameters(self::get_scorm_attempt_count_parameters(), | |
138 | array('scormid' => $scormid, 'userid' => $userid, | |
139 | 'ignoremissingcompletion' => $ignoremissingcompletion)); | |
140 | ||
141 | $attempts = array(); | |
142 | $warnings = array(); | |
143 | ||
144 | $scorm = $DB->get_record('scorm', array('id' => $params['scormid']), '*', MUST_EXIST); | |
145 | $cm = get_coursemodule_from_instance('scorm', $scorm->id); | |
146 | ||
147 | $context = context_module::instance($cm->id); | |
148 | self::validate_context($context); | |
149 | ||
150 | // Validate the user obtaining the context, it will fail if the user doesn't exists or have been deleted. | |
151 | context_user::instance($params['userid']); | |
152 | ||
153 | // Extra checks so only users with permissions can view other users attempts. | |
154 | if ($USER->id != $params['userid']) { | |
155 | require_capability('mod/scorm:viewreport', $context); | |
156 | } | |
157 | ||
158 | // If the SCORM is not open this function will throw exceptions. | |
159 | scorm_require_available($scorm); | |
160 | ||
161 | $attemptscount = scorm_get_attempt_count($params['userid'], $scorm, false, $params['ignoremissingcompletion']); | |
162 | ||
163 | $result = array(); | |
164 | $result['attemptscount'] = $attemptscount; | |
165 | $result['warnings'] = $warnings; | |
166 | return $result; | |
167 | } | |
168 | ||
169 | /** | |
170 | * Describes the get_scorm_attempt_count return value. | |
171 | * | |
172 | * @return external_single_structure | |
173 | * @since Moodle 3.0 | |
174 | */ | |
175 | public static function get_scorm_attempt_count_returns() { | |
176 | ||
177 | return new external_single_structure( | |
178 | array( | |
179 | 'attemptscount' => new external_value(PARAM_INT, 'Attempts count'), | |
180 | 'warnings' => new external_warnings(), | |
181 | ) | |
182 | ); | |
183 | } | |
184 | ||
e9bf3011 | 185 | } |