MDL-50427 mod_scorm: New Web Service mod_scorm_view_scorm
[moodle.git] / mod / scorm / classes / external.php
CommitLineData
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
27defined('MOODLE_INTERNAL') || die;
28
29require_once($CFG->libdir . '/externallib.php');
30require_once($CFG->dirroot . '/mod/scorm/lib.php');
31
32/**
33 * SCORM module external functions
34 *
35 * @package mod_scorm
36 * @category external
37 * @copyright 2015 Juan Leyva <juan@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 * @since Moodle 3.0
40 */
41class mod_scorm_external extends external_api {
42
43 /**
44 * Returns description of method parameters
45 *
46 * @return external_function_parameters
47 * @since Moodle 3.0
48 */
49 public static function view_scorm_parameters() {
50 return new external_function_parameters(
51 array(
52 'scormid' => new external_value(PARAM_INT, 'scorm instance id')
53 )
54 );
55 }
56
57 /**
58 * Trigger the course module viewed event.
59 *
60 * @param int $scormid the scorm instance id
61 * @return array of warnings and status result
62 * @since Moodle 3.0
63 * @throws moodle_exception
64 */
65 public static function view_scorm($scormid) {
66 global $DB, $CFG;
67 require_once($CFG->dirroot . '/mod/scorm/lib.php');
68
69 $params = self::validate_parameters(self::view_scorm_parameters(),
70 array(
71 'scormid' => $scormid
72 ));
73 $warnings = array();
74
75 // Request and permission validation.
76 $scorm = $DB->get_record('scorm', array('id' => $params['scormid']), '*', MUST_EXIST);
77 list($course, $cm) = get_course_and_cm_from_instance($scorm, 'scorm');
78
79 $context = context_module::instance($cm->id);
80 self::validate_context($context);
81
82 // Call the scorm/lib API.
83 scorm_view($scorm, $course, $cm, $context);
84
85 $result = array();
86 $result['status'] = true;
87 $result['warnings'] = $warnings;
88 return $result;
89 }
90
91 /**
92 * Returns description of method result value
93 *
94 * @return external_description
95 * @since Moodle 3.0
96 */
97 public static function view_scorm_returns() {
98 return new external_single_structure(
99 array(
100 'status' => new external_value(PARAM_BOOL, 'status: true if success'),
101 'warnings' => new external_warnings()
102 )
103 );
104 }
105}