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 core_courseformat\external;
|
|
|
18 |
|
|
|
19 |
use core_external\external_api;
|
|
|
20 |
use core_external\external_function_parameters;
|
|
|
21 |
use core_external\external_value;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Class for exporting a course state.
|
|
|
25 |
*
|
|
|
26 |
* @package core_course
|
|
|
27 |
* @copyright 2021 Ferran Recio <ferran@moodle.com>
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
* @since Moodle 4.0
|
|
|
30 |
*/
|
|
|
31 |
class get_state extends external_api {
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Webservice parameters.
|
|
|
35 |
*
|
|
|
36 |
* @return external_function_parameters
|
|
|
37 |
*/
|
|
|
38 |
public static function execute_parameters(): external_function_parameters {
|
|
|
39 |
return new external_function_parameters([
|
|
|
40 |
'courseid' => new external_value(PARAM_INT, 'course id', VALUE_REQUIRED),
|
|
|
41 |
]);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* This method will load all course, sections and cm states needed to initialize the frontend
|
|
|
46 |
* course editor module. The state data of every individual course, section and cm is
|
|
|
47 |
* build using the specifics "state" output components.
|
|
|
48 |
*
|
|
|
49 |
* By default, the states are generated by:
|
|
|
50 |
* - core_courseformat\output\state\course
|
|
|
51 |
* - core_courseformat\output\state\section
|
|
|
52 |
* - core_courseformat\output\state\cm
|
|
|
53 |
*
|
|
|
54 |
* As the other main course outputs, format plugins can override those output components
|
|
|
55 |
* to send more information to the frontend course editor. These extended classes should
|
|
|
56 |
* be located in format_XXX\output\courseformat\state\course, format_XXX\output\courseformat\state\section
|
|
|
57 |
* or format_XXX\output\courseformat\state\cm.
|
|
|
58 |
*
|
|
|
59 |
* @param int $courseid the course id
|
|
|
60 |
* @return string Course state in JSON
|
|
|
61 |
*/
|
|
|
62 |
public static function execute(int $courseid): string {
|
|
|
63 |
global $PAGE, $CFG, $USER;
|
|
|
64 |
|
|
|
65 |
require_once($CFG->dirroot.'/course/lib.php');
|
|
|
66 |
|
|
|
67 |
$params = external_api::validate_parameters(self::execute_parameters(), [
|
|
|
68 |
'courseid' => $courseid,
|
|
|
69 |
]);
|
|
|
70 |
$courseid = $params['courseid'];
|
|
|
71 |
|
|
|
72 |
self::validate_context(\context_course::instance($courseid));
|
|
|
73 |
|
|
|
74 |
$courseformat = course_get_format($courseid);
|
|
|
75 |
$modinfo = $courseformat->get_modinfo();
|
|
|
76 |
$completioninfo = new \completion_info(get_course($courseid));
|
|
|
77 |
$istrackeduser = $completioninfo->is_tracked_user($USER->id);
|
|
|
78 |
|
|
|
79 |
// Get the proper renderer.
|
|
|
80 |
$renderer = $courseformat->get_renderer($PAGE);
|
|
|
81 |
|
|
|
82 |
$result = (object)[
|
|
|
83 |
'course' => (object)[],
|
|
|
84 |
'section' => [],
|
|
|
85 |
'cm' => [],
|
|
|
86 |
];
|
|
|
87 |
|
|
|
88 |
// Load the output class names.
|
|
|
89 |
$courseclass = $courseformat->get_output_classname('state\\course');
|
|
|
90 |
$sectionclass = $courseformat->get_output_classname('state\\section');
|
|
|
91 |
$cmclass = $courseformat->get_output_classname('state\\cm');
|
|
|
92 |
|
|
|
93 |
// General state.
|
|
|
94 |
$coursestate = new $courseclass($courseformat);
|
|
|
95 |
$result->course = $coursestate->export_for_template($renderer);
|
|
|
96 |
|
|
|
97 |
// Sections and course modules state.
|
|
|
98 |
$sections = $modinfo->get_section_info_all();
|
|
|
99 |
foreach ($sections as $section) {
|
|
|
100 |
if ($courseformat->is_section_visible($section)) {
|
|
|
101 |
// Only return this section data if it's visible by current user on the course page.
|
|
|
102 |
$sectionstate = new $sectionclass($courseformat, $section);
|
|
|
103 |
$result->section[] = $sectionstate->export_for_template($renderer);
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
foreach ($modinfo->cms as $cm) {
|
|
|
108 |
if ($cm->is_visible_on_course_page()) {
|
|
|
109 |
// Only return this course module data if it's visible by current user on the course page.
|
|
|
110 |
$section = $sections[$cm->sectionnum];
|
|
|
111 |
$cmstate = new $cmclass($courseformat, $section, $cm, istrackeduser: $istrackeduser);
|
|
|
112 |
$result->cm[] = $cmstate->export_for_template($renderer);
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
return json_encode($result);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Webservice returns.
|
|
|
121 |
*
|
|
|
122 |
* @return external_value
|
|
|
123 |
*/
|
|
|
124 |
public static function execute_returns(): external_value {
|
|
|
125 |
return new external_value(PARAM_RAW, 'Encoded course state JSON');
|
|
|
126 |
}
|
|
|
127 |
}
|