/course/format/classes/external/update_course.php

https://github.com/markn86/moodle · PHP · 152 lines · 79 code · 18 blank · 55 comment · 5 complexity · 6315a5ca7e911c6cebbc1c6ca2312fad MD5 · raw file

  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. namespace core_courseformat\external;
  17. defined('MOODLE_INTERNAL') || die();
  18. global $CFG;
  19. require_once($CFG->libdir . '/externallib.php');
  20. use external_api;
  21. use external_function_parameters;
  22. use external_value;
  23. use external_multiple_structure;
  24. use moodle_exception;
  25. use coding_exception;
  26. use context_course;
  27. /**
  28. * External secrvie to update the course from the course editor components.
  29. *
  30. * @package core_course
  31. * @copyright 2021 Ferran Recio <moodle@moodle.com>
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. * @since Moodle 4.0
  34. */
  35. class update_course extends external_api {
  36. /**
  37. * Webservice parameters.
  38. *
  39. * @return external_function_parameters
  40. */
  41. public static function execute_parameters(): external_function_parameters {
  42. return new external_function_parameters(
  43. [
  44. 'action' => new external_value(
  45. PARAM_ALPHANUMEXT,
  46. 'action: cm_hide, cm_show, section_hide, section_show, cm_moveleft...',
  47. VALUE_REQUIRED
  48. ),
  49. 'courseid' => new external_value(PARAM_INT, 'course id', VALUE_REQUIRED),
  50. 'ids' => new external_multiple_structure(
  51. new external_value(PARAM_INT, 'Target id'),
  52. 'Affected ids',
  53. VALUE_DEFAULT,
  54. []
  55. ),
  56. 'targetsectionid' => new external_value(
  57. PARAM_INT, 'Optional target section id', VALUE_DEFAULT, null
  58. ),
  59. 'targetcmid' => new external_value(
  60. PARAM_INT, 'Optional target cm id', VALUE_DEFAULT, null
  61. ),
  62. ]
  63. );
  64. }
  65. /**
  66. * This webservice will execute any action from the course editor. The default actions
  67. * are located in core_course\stateactions but the format plugin can extend that class
  68. * in format_XXX\course.
  69. *
  70. * The specific action methods will register in a core_course\stateupdates all the affected
  71. * sections, cms and course attribute. This object (in JSON) will be send back to the
  72. * frontend editor to refresh the updated state elements.
  73. *
  74. * By default, core_course\stateupdates will register only create, delete and update events
  75. * on cms, sections and the general course data. However, if some plugin needs adhoc messages for
  76. * its own mutation module, it extend this class in format_XXX\course.
  77. *
  78. * @param string $action the action name to execute
  79. * @param int $courseid the course id
  80. * @param int[] $ids the affected ids (section or cm depending on the action)
  81. * @param int $targetsectionid optional target section id (for move action)
  82. * @param int $targetcmid optional target cm id (for move action)
  83. * @return string Course state in JSON
  84. */
  85. public static function execute(string $action, int $courseid, array $ids = [],
  86. ?int $targetsectionid = null, ?int $targetcmid = null): string {
  87. global $CFG;
  88. require_once($CFG->dirroot . '/course/lib.php');
  89. $params = external_api::validate_parameters(self::execute_parameters(), [
  90. 'action' => $action,
  91. 'courseid' => $courseid,
  92. 'ids' => $ids,
  93. 'targetsectionid' => $targetsectionid,
  94. 'targetcmid' => $targetcmid,
  95. ]);
  96. $action = $params['action'];
  97. $courseid = $params['courseid'];
  98. $ids = $params['ids'];
  99. $targetsectionid = $params['targetsectionid'];
  100. $targetcmid = $params['targetcmid'];
  101. self::validate_context(context_course::instance($courseid));
  102. $courseformat = course_get_format($courseid);
  103. // Create a course changes tracker object.
  104. $defaultupdatesclass = 'core_courseformat\\stateupdates';
  105. $updatesclass = 'format_' . $courseformat->get_format() . '\\courseformat\\stateupdates';
  106. if (!class_exists($updatesclass)) {
  107. $updatesclass = $defaultupdatesclass;
  108. }
  109. $updates = new $updatesclass($courseformat);
  110. if (!is_a($updates, $defaultupdatesclass)) {
  111. throw new coding_exception("The \"$updatesclass\" class must extend \"$defaultupdatesclass\"");
  112. }
  113. // Get the actions class from the course format.
  114. $actionsclass = 'format_'. $courseformat->get_format().'\\courseformat\\stateactions';
  115. if (!class_exists($actionsclass)) {
  116. $actionsclass = 'core_courseformat\\stateactions';
  117. }
  118. $actions = new $actionsclass();
  119. if (!is_callable([$actions, $action])) {
  120. throw new moodle_exception("Invalid course state action $action in ".get_class($actions));
  121. }
  122. // Execute the action.
  123. $actions->$action($updates, $courseformat->get_course(), $ids, $targetsectionid, $targetcmid);
  124. return json_encode($updates);
  125. }
  126. /**
  127. * Webservice returns.
  128. *
  129. * @return external_value
  130. */
  131. public static function execute_returns(): external_value {
  132. return new external_value(PARAM_RAW, 'Encoded course update JSON');
  133. }
  134. }