1441 |
ariadna |
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 |
* Execute an update action on a course format and structure.
|
|
|
19 |
*
|
|
|
20 |
* @package core_courseformat
|
|
|
21 |
* @copyright 2024 Ferran Recio <ferran@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
require_once('../../config.php');
|
|
|
26 |
require_once($CFG->dirroot . '/course/lib.php');
|
|
|
27 |
|
|
|
28 |
use core\url;
|
|
|
29 |
use core\exception\moodle_exception;
|
|
|
30 |
use core_courseformat\base as course_format;
|
|
|
31 |
|
|
|
32 |
$action = required_param('action', PARAM_ALPHANUMEXT);
|
|
|
33 |
$courseid = required_param('courseid', PARAM_INT);
|
|
|
34 |
$targetsectionid = optional_param('targetsectionid', null, PARAM_INT);
|
|
|
35 |
$targetcmid = optional_param('targetcmid', null, PARAM_INT);
|
|
|
36 |
$confirm = optional_param('confirm', false, PARAM_BOOL);
|
|
|
37 |
|
|
|
38 |
$returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
// All state updates are designed to be batch compatible. However, we also
|
|
|
42 |
// accept single id values for simplicity.
|
|
|
43 |
$ids = optional_param_array('ids', [], PARAM_INT);
|
|
|
44 |
if (empty($ids)) {
|
|
|
45 |
$ids = [required_param('id', PARAM_INT)];
|
|
|
46 |
}
|
|
|
47 |
if (empty($ids)) {
|
|
|
48 |
throw new moodle_exception('missingparam', '', '', 'ids');
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
$format = course_get_format($courseid);
|
|
|
52 |
$course = $format->get_course();
|
|
|
53 |
|
|
|
54 |
if ($returnurl === null) {
|
|
|
55 |
$returnurl = new url('/course/view.php', ['id' => $course->id]);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
// Normalize the return URL.
|
|
|
59 |
$returnurl = new moodle_url($returnurl);
|
|
|
60 |
|
|
|
61 |
$currenturl = new moodle_url(
|
|
|
62 |
'/course/format/update.php',
|
|
|
63 |
[
|
|
|
64 |
'action' => $action,
|
|
|
65 |
'courseid' => $courseid,
|
|
|
66 |
'targetsectionid' => $targetsectionid,
|
|
|
67 |
'targetcmid' => $targetcmid,
|
|
|
68 |
'returnurl' => $returnurl,
|
|
|
69 |
'sesskey' => sesskey(),
|
|
|
70 |
]
|
|
|
71 |
);
|
|
|
72 |
foreach ($ids as $key => $id) {
|
|
|
73 |
$currenturl->param("ids[]", $id);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
require_sesskey();
|
|
|
77 |
|
|
|
78 |
$PAGE->set_url($currenturl);
|
|
|
79 |
$PAGE->set_context($format->get_context());
|
|
|
80 |
$PAGE->set_pagelayout('course');
|
|
|
81 |
$PAGE->add_body_class('limitedwidth');
|
|
|
82 |
$PAGE->set_heading($course->fullname);
|
|
|
83 |
|
|
|
84 |
require_login($course);
|
|
|
85 |
require_all_capabilities(
|
|
|
86 |
['moodle/course:update', 'moodle/course:sectionvisibility', 'moodle/course:activityvisibility'],
|
|
|
87 |
$format->get_context(),
|
|
|
88 |
);
|
|
|
89 |
|
|
|
90 |
// Some actions may require a confirmation dialog.
|
|
|
91 |
$actionuiclass = $format->get_output_classname('courseupdate');
|
|
|
92 |
/** @var core_courseformat\output\local\courseupdate $actionui */
|
|
|
93 |
$actionui = new $actionuiclass($format, $currenturl, $returnurl);
|
|
|
94 |
|
|
|
95 |
if (
|
|
|
96 |
!$confirm
|
|
|
97 |
&& $actionui->is_confirmation_required($action)
|
|
|
98 |
) {
|
|
|
99 |
/** @var \core_course_renderer $renderer */
|
|
|
100 |
$renderer = $format->get_renderer($PAGE);
|
|
|
101 |
echo $renderer->header();
|
|
|
102 |
echo $actionui->get_confirmation_dialog(
|
|
|
103 |
output: $renderer,
|
|
|
104 |
course: $course,
|
|
|
105 |
action: $action,
|
|
|
106 |
ids: $ids,
|
|
|
107 |
targetsectionid: $targetsectionid,
|
|
|
108 |
targetcmid: $targetcmid,
|
|
|
109 |
);
|
|
|
110 |
echo $renderer->footer();
|
|
|
111 |
die;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
$updates = $format->get_stateupdates_instance();
|
|
|
115 |
$actions = $format->get_stateactions_instance();
|
|
|
116 |
|
|
|
117 |
if (!is_callable([$actions, $action])) {
|
|
|
118 |
throw new moodle_exception("Invalid course state action $action in ".get_class($actions));
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
// Execute the action.
|
|
|
122 |
$actions->$action($updates, $course, $ids, $targetsectionid, $targetcmid);
|
|
|
123 |
|
|
|
124 |
// Dispatch the hook for post course content update.
|
|
|
125 |
$hook = new \core_courseformat\hook\after_course_content_updated(
|
|
|
126 |
course: $course,
|
|
|
127 |
);
|
|
|
128 |
\core\di::get(\core\hook\manager::class)->dispatch($hook);
|
|
|
129 |
|
|
|
130 |
redirect($returnurl);
|