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