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;
|
|
|
18 |
|
|
|
19 |
use core_courseformat\local\courseactions;
|
|
|
20 |
use core_courseformat\local\sectionactions;
|
|
|
21 |
use core_courseformat\local\cmactions;
|
|
|
22 |
use coding_exception;
|
|
|
23 |
use stdClass;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Class to instantiate course format actions.
|
|
|
27 |
*
|
|
|
28 |
* This class is used to access course content actions.
|
|
|
29 |
*
|
|
|
30 |
* All course actions are divided into three main clases:
|
|
|
31 |
* - course: actions related to the course.
|
|
|
32 |
* - section: actions related to the sections.
|
|
|
33 |
* - cm: actions related to the course modules.
|
|
|
34 |
*
|
|
|
35 |
* Format plugin can provide their own actions classes by extending the actions classes
|
|
|
36 |
* with the following namespaces:
|
|
|
37 |
* - course: format_{PLUGINNAME}\courseformat\courseactions
|
|
|
38 |
* - section: format_{PLUGINNAME}\courseformat\sectionactions
|
|
|
39 |
* - cm: format_{PLUGINNAME}\courseformat\cmactions
|
|
|
40 |
*
|
|
|
41 |
* There a static method to get the general formatactions instance:
|
|
|
42 |
* - formatactions::instance($courseorid): returns an instance to access all available actions.
|
|
|
43 |
*
|
|
|
44 |
* The class also provides some convenience methods to get specific actions level on a specific course:
|
|
|
45 |
* - formatactions::course($courseorid): returns an instance of the course actions class.
|
|
|
46 |
* - formatactions::section($courseorid): returns an instance of the section actions class.
|
|
|
47 |
* - formatactions::cm($courseorid): returns an instance of the cm actions class.
|
|
|
48 |
*
|
|
|
49 |
* There are two ways of executing actions. For example, to execute a section action
|
|
|
50 |
* called "move_after" the options are:
|
|
|
51 |
*
|
|
|
52 |
* Option A: ideal for executing only one action.
|
|
|
53 |
*
|
|
|
54 |
* formatactions::section($courseid)->move_after($sectioninfo, $aftersectioninfo);
|
|
|
55 |
*
|
|
|
56 |
* Option B: when actions in the same course are going to be executed at different levels.
|
|
|
57 |
*
|
|
|
58 |
* $actions = formatactions::instance($courseid);
|
|
|
59 |
* $actions->section->move_after($sectioninfo, $aftersectioninfo);
|
|
|
60 |
*
|
|
|
61 |
* @package core_courseformat
|
|
|
62 |
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
|
|
63 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
64 |
*/
|
|
|
65 |
final class formatactions {
|
|
|
66 |
/**
|
|
|
67 |
* @var courseactions|null courseactions instance.
|
|
|
68 |
*/
|
|
|
69 |
public courseactions $course;
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* @var sectionactions sectionactions instance.
|
|
|
73 |
*/
|
|
|
74 |
public sectionactions $section;
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* @var cmactions cmactions instance.
|
|
|
78 |
*/
|
|
|
79 |
public cmactions $cm;
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Returns an instance of the actions class for the given course format.
|
|
|
83 |
*
|
|
|
84 |
* @param base $format the course format.
|
|
|
85 |
*/
|
|
|
86 |
protected function __construct(base $format) {
|
|
|
87 |
$actionclasses = [
|
|
|
88 |
'course' => courseactions::class,
|
|
|
89 |
'section' => sectionactions::class,
|
|
|
90 |
'cm' => cmactions::class,
|
|
|
91 |
];
|
|
|
92 |
foreach ($actionclasses as $action => $classname) {
|
|
|
93 |
$formatalternative = 'format_' . $format->get_format() . '\\courseformat\\' . $action . 'actions';
|
|
|
94 |
if (class_exists($formatalternative)) {
|
|
|
95 |
if (!is_subclass_of($formatalternative, $classname)) {
|
|
|
96 |
throw new coding_exception("The \"$formatalternative\" must extend \"$classname\"");
|
|
|
97 |
}
|
|
|
98 |
$actionclasses[$action] = $formatalternative;
|
|
|
99 |
}
|
|
|
100 |
$this->$action = new $actionclasses[$action]($format->get_course());
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Returns an instance of the actions class for the given course format.
|
|
|
106 |
* @param int|stdClass $courseorid course id or record.
|
|
|
107 |
* @return courseactions
|
|
|
108 |
*/
|
|
|
109 |
public static function course($courseorid): courseactions {
|
|
|
110 |
return self::instance($courseorid)->course;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Returns an instance of the actions class for the given course format.
|
|
|
115 |
*
|
|
|
116 |
* @param int|stdClass $courseorid course id or record.
|
|
|
117 |
* @return sectionactions
|
|
|
118 |
*/
|
|
|
119 |
public static function section($courseorid): sectionactions {
|
|
|
120 |
return self::instance($courseorid)->section;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Returns an instance of the actions class for the given course format.
|
|
|
125 |
* @param int|stdClass $courseorid course id or record.
|
|
|
126 |
* @return cmactions
|
|
|
127 |
*/
|
|
|
128 |
public static function cm($courseorid): cmactions {
|
|
|
129 |
return self::instance($courseorid)->cm;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Get a course action loader instance.
|
|
|
134 |
* @param int|stdClass $courseorid course id or course.
|
|
|
135 |
* @return self
|
|
|
136 |
*/
|
|
|
137 |
public static function instance(int|stdClass $courseorid): self {
|
|
|
138 |
$coursesectionscache = \cache::make('core', 'courseactionsinstances');
|
|
|
139 |
$format = base::instance($courseorid);
|
|
|
140 |
$courseid = $format->get_courseid();
|
|
|
141 |
$cachekey = "{$courseid}_{$format->get_format()}";
|
|
|
142 |
$cachedinstance = $coursesectionscache->get($cachekey);
|
|
|
143 |
if ($cachedinstance) {
|
|
|
144 |
return $cachedinstance;
|
|
|
145 |
}
|
|
|
146 |
$result = new self($format);
|
|
|
147 |
$coursesectionscache->set($cachekey, $result);
|
|
|
148 |
return $result;
|
|
|
149 |
}
|
|
|
150 |
}
|