| 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 |
namespace mod_subsection;
|
|
|
18 |
|
|
|
19 |
use cm_info;
|
|
|
20 |
use context_module;
|
|
|
21 |
use completion_info;
|
|
|
22 |
use core_courseformat\formatactions;
|
|
|
23 |
use mod_subsection\event\course_module_viewed;
|
|
|
24 |
use moodle_page;
|
|
|
25 |
use section_info;
|
|
|
26 |
use stdClass;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Class manager for subsection
|
|
|
30 |
*
|
|
|
31 |
* @package mod_subsection
|
|
|
32 |
* @copyright 2023 Amaia Anabitarte <amaia@moodle.com>
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
class manager {
|
|
|
36 |
|
|
|
37 |
/** Module name. */
|
|
|
38 |
const MODULE = 'subsection';
|
|
|
39 |
|
|
|
40 |
/** The plugin name. */
|
|
|
41 |
const PLUGINNAME = 'mod_subsection';
|
|
|
42 |
|
|
|
43 |
/** @var string plugin path. */
|
|
|
44 |
public $path;
|
|
|
45 |
|
|
|
46 |
/** @var stdClass course_module record. */
|
|
|
47 |
private $instance;
|
|
|
48 |
|
|
|
49 |
/** @var context_module the current context. */
|
|
|
50 |
private $context;
|
|
|
51 |
|
|
|
52 |
/** @var cm_info course_modules record. */
|
|
|
53 |
private $cm;
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Class constructor.
|
|
|
57 |
*
|
|
|
58 |
* @param cm_info $cm course module info object
|
|
|
59 |
* @param stdClass $instance activity instance object.
|
|
|
60 |
*/
|
|
|
61 |
public function __construct(cm_info $cm, stdClass $instance) {
|
|
|
62 |
global $CFG;
|
|
|
63 |
$this->cm = $cm;
|
|
|
64 |
$this->instance = $instance;
|
|
|
65 |
$this->context = context_module::instance($cm->id);
|
|
|
66 |
$this->instance->cmidnumber = $cm->idnumber;
|
|
|
67 |
$this->path = $CFG->dirroot . '/mod/' . self::MODULE;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Create a manager instance from an instance record.
|
|
|
72 |
*
|
|
|
73 |
* @param stdClass $instance an activity record
|
|
|
74 |
* @return manager
|
|
|
75 |
*/
|
|
|
76 |
public static function create_from_instance(stdClass $instance): self {
|
|
|
77 |
$cm = get_coursemodule_from_instance(self::MODULE, $instance->id);
|
|
|
78 |
// Ensure that $this->cm is a cm_info object.
|
|
|
79 |
$cm = cm_info::create($cm);
|
|
|
80 |
return new self($cm, $instance);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Create a manager instance from a course_modules record.
|
|
|
85 |
*
|
|
|
86 |
* @param stdClass|cm_info $cm an activity record
|
|
|
87 |
* @return manager
|
|
|
88 |
*/
|
|
|
89 |
public static function create_from_coursemodule($cm): self {
|
|
|
90 |
global $DB;
|
|
|
91 |
// Ensure that $this->cm is a cm_info object.
|
|
|
92 |
$cm = cm_info::create($cm);
|
|
|
93 |
$instance = $DB->get_record(self::MODULE, ['id' => $cm->instance], '*', MUST_EXIST);
|
|
|
94 |
return new self($cm, $instance);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Create a manager instance from a record id.
|
|
|
99 |
*
|
|
|
100 |
* @param int $courseid the course id
|
|
|
101 |
* @param int $id an activity id
|
|
|
102 |
* @return manager
|
|
|
103 |
*/
|
|
|
104 |
public static function create_from_id(int $courseid, int $id): self {
|
|
|
105 |
$cm = get_coursemodule_from_instance('subsection', $id, $courseid);
|
|
|
106 |
return self::create_from_coursemodule($cm);
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Create a manager instance from a subsection_record entry.
|
|
|
111 |
*
|
|
|
112 |
* @param stdClass $record the subsection_record record
|
|
|
113 |
* @return manager
|
|
|
114 |
*/
|
|
|
115 |
public static function create_from_data_record($record): self {
|
|
|
116 |
global $DB;
|
|
|
117 |
$instance = $DB->get_record(self::MODULE, ['id' => $record->dataid], '*', MUST_EXIST);
|
|
|
118 |
$cm = get_coursemodule_from_instance(self::MODULE, $instance->id);
|
|
|
119 |
$cm = cm_info::create($cm);
|
|
|
120 |
return new self($cm, $instance);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Return the current context.
|
|
|
125 |
*
|
|
|
126 |
* @return context_module
|
|
|
127 |
*/
|
|
|
128 |
public function get_context(): context_module {
|
|
|
129 |
return $this->context;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Return the current instance.
|
|
|
134 |
*
|
|
|
135 |
* @return stdClass the instance record
|
|
|
136 |
*/
|
|
|
137 |
public function get_instance(): stdClass {
|
|
|
138 |
return $this->instance;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Return the current cm_info.
|
|
|
143 |
*
|
|
|
144 |
* @return cm_info the course module
|
|
|
145 |
*/
|
|
|
146 |
public function get_coursemodule(): cm_info {
|
|
|
147 |
return $this->cm;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Return the current module renderer.
|
|
|
152 |
*
|
|
|
153 |
* @param moodle_page|null $page the current page
|
|
|
154 |
* @return \mod_subsection_renderer the module renderer
|
|
|
155 |
*/
|
|
|
156 |
public function get_renderer(?moodle_page $page = null): \mod_subsection_renderer {
|
|
|
157 |
global $PAGE;
|
|
|
158 |
$page = $page ?? $PAGE;
|
|
|
159 |
return $page->get_renderer(self::PLUGINNAME);
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* Trigger module viewed event and set the module viewed for completion.
|
|
|
164 |
*
|
|
|
165 |
* @param stdClass $course course object
|
|
|
166 |
*/
|
|
|
167 |
public function set_module_viewed(stdClass $course) {
|
|
|
168 |
global $CFG;
|
|
|
169 |
require_once($CFG->libdir . '/completionlib.php');
|
|
|
170 |
|
|
|
171 |
// Trigger module viewed event.
|
|
|
172 |
$event = course_module_viewed::create([
|
|
|
173 |
'objectid' => $this->instance->id,
|
|
|
174 |
'context' => $this->context,
|
|
|
175 |
]);
|
|
|
176 |
$event->add_record_snapshot('course', $course);
|
|
|
177 |
$event->add_record_snapshot('course_modules', $this->cm);
|
|
|
178 |
$event->add_record_snapshot(self::MODULE, $this->instance);
|
|
|
179 |
$event->trigger();
|
|
|
180 |
|
|
|
181 |
// Completion.
|
|
|
182 |
$completion = new completion_info($course);
|
|
|
183 |
$completion->set_module_viewed($this->cm);
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
/**
|
|
|
187 |
* Get the delegated section info.
|
|
|
188 |
*
|
|
|
189 |
* @return section_info|null the delegated section info
|
|
|
190 |
*/
|
|
|
191 |
public function get_delegated_section_info(): ?section_info {
|
|
|
192 |
$delegatedsection = $this->cm->get_delegated_section_info();
|
|
|
193 |
if (!$delegatedsection) {
|
|
|
194 |
// Some restorations can produce a situation where the section is not found.
|
|
|
195 |
// In that case, we create a new one.
|
|
|
196 |
$delegatedsection = formatactions::section($this->cm->course)->create_delegated(
|
|
|
197 |
self::PLUGINNAME,
|
|
|
198 |
$this->cm->instance,
|
|
|
199 |
(object) [
|
|
|
200 |
'name' => $this->cm->name,
|
|
|
201 |
'visible' => $this->cm->visible,
|
|
|
202 |
'availability' => (!empty($this->cm->availability)) ? $this->cm->availability : null,
|
|
|
203 |
],
|
|
|
204 |
);
|
|
|
205 |
}
|
|
|
206 |
return $delegatedsection;
|
|
|
207 |
}
|
|
|
208 |
}
|