MDL-68819 core_contentbank: "Upload" button is not localized.
[moodle.git] / contentbank / classes / content.php
CommitLineData
33b8ca26
AA
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 * Content manager class
19 *
20 * @package core_contentbank
21 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_contentbank;
26
448012c6 27use core_text;
33b8ca26
AA
28use stored_file;
29use stdClass;
30use coding_exception;
31use moodle_url;
db15dab8 32use core\event\contentbank_content_updated;
33b8ca26
AA
33
34/**
35 * Content manager class
36 *
37 * @package core_contentbank
38 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41abstract class content {
42
43 /** @var stdClass $content The content of the current instance. **/
44 protected $content = null;
45
46 /**
47 * Content bank constructor
48 *
de322e3d 49 * @param stdClass $record A contentbank_content record.
33b8ca26
AA
50 * @throws coding_exception If content type is not right.
51 */
de322e3d 52 public function __construct(stdClass $record) {
33b8ca26 53 // Content type should exist and be linked to plugin classname.
de322e3d 54 $classname = $record->contenttype.'\\content';
33b8ca26 55 if (get_class($this) != $classname) {
de322e3d 56 throw new coding_exception(get_string('contenttypenotfound', 'error', $record->contenttype));
33b8ca26 57 }
de322e3d 58 $typeclass = $record->contenttype.'\\contenttype';
33b8ca26 59 if (!class_exists($typeclass)) {
de322e3d 60 throw new coding_exception(get_string('contenttypenotfound', 'error', $record->contenttype));
33b8ca26
AA
61 }
62 // A record with the id must exist in 'contenbank_content' table.
63 // To improve performance, we are only checking the id is set, but no querying the database.
de322e3d 64 if (!isset($record->id)) {
33b8ca26
AA
65 throw new coding_exception(get_string('invalidcontentid', 'error'));
66 }
de322e3d 67 $this->content = $record;
33b8ca26
AA
68 }
69
70 /**
71 * Returns $this->content.
72 *
73 * @return stdClass $this->content.
74 */
75 public function get_content(): stdClass {
76 return $this->content;
77 }
78
79 /**
80 * Returns $this->content->contenttype.
81 *
82 * @return string $this->content->contenttype.
83 */
84 public function get_content_type(): string {
85 return $this->content->contenttype;
86 }
87
e695d9d0
BB
88 /**
89 * Returns $this->content->timemodified.
90 *
91 * @return int $this->content->timemodified.
92 */
93 public function get_timemodified(): int {
94 return $this->content->timemodified;
95 }
96
33b8ca26
AA
97 /**
98 * Updates content_bank table with information in $this->content.
99 *
100 * @return boolean True if the content has been succesfully updated. False otherwise.
101 * @throws \coding_exception if not loaded.
102 */
103 public function update_content(): bool {
104 global $USER, $DB;
105
106 // A record with the id must exist in 'contenbank_content' table.
107 // To improve performance, we are only checking the id is set, but no querying the database.
108 if (!isset($this->content->id)) {
109 throw new coding_exception(get_string('invalidcontentid', 'error'));
110 }
111 $this->content->usermodified = $USER->id;
112 $this->content->timemodified = time();
fb302d38
AA
113 $result = $DB->update_record('contentbank_content', $this->content);
114 if ($result) {
115 // Trigger an event for updating this content.
116 $event = contentbank_content_updated::create_from_record($this->content);
117 $event->trigger();
118 }
119 return $result;
33b8ca26
AA
120 }
121
448012c6
AA
122 /**
123 * Set a new name to the content.
124 *
125 * @param string $name The name of the content.
126 * @return bool True if the content has been succesfully updated. False otherwise.
127 * @throws \coding_exception if not loaded.
128 */
129 public function set_name(string $name): bool {
130 if (empty($name)) {
131 return false;
132 }
133
134 // Clean name.
135 $name = clean_param($name, PARAM_TEXT);
136 if (core_text::strlen($name) > 255) {
137 $name = core_text::substr($name, 0, 255);
138 }
139
140 $oldname = $this->content->name;
141 $this->content->name = $name;
142 $updated = $this->update_content();
143 if (!$updated) {
144 $this->content->name = $oldname;
145 }
146 return $updated;
147 }
148
33b8ca26
AA
149 /**
150 * Returns the name of the content.
151 *
152 * @return string The name of the content.
153 */
154 public function get_name(): string {
155 return $this->content->name;
156 }
157
f2a9bb6e
AA
158 /**
159 * Set a new contextid to the content.
160 *
161 * @param int $contextid The new contextid of the content.
162 * @return bool True if the content has been succesfully updated. False otherwise.
163 */
164 public function set_contextid(int $contextid): bool {
165 if ($this->content->contextid == $contextid) {
166 return true;
167 }
168
169 $oldcontextid = $this->content->contextid;
170 $this->content->contextid = $contextid;
171 $updated = $this->update_content();
172 if ($updated) {
173 // Move files to new context
174 $fs = get_file_storage();
175 $fs->move_area_files_to_new_context($oldcontextid, $contextid, 'contentbank', 'public', $this->content->id);
176 } else {
177 $this->content->contextid = $oldcontextid;
178 }
179 return $updated;
180 }
181
182 /**
183 * Returns the contextid of the content.
184 *
185 * @return int The id of the content context.
186 */
187 public function get_contextid(): string {
188 return $this->content->contextid;
189 }
190
33b8ca26
AA
191 /**
192 * Returns the content ID.
193 *
194 * @return int The content ID.
195 */
196 public function get_id(): int {
197 return $this->content->id;
198 }
199
200 /**
201 * Change the content instanceid value.
202 *
203 * @param int $instanceid New instanceid for this content
204 * @return boolean True if the instanceid has been succesfully updated. False otherwise.
205 */
206 public function set_instanceid(int $instanceid): bool {
207 $this->content->instanceid = $instanceid;
208 return $this->update_content();
209 }
210
211 /**
212 * Returns the $instanceid of this content.
213 *
214 * @return int contentbank instanceid
215 */
216 public function get_instanceid(): int {
217 return $this->content->instanceid;
218 }
219
220 /**
221 * Change the content config values.
222 *
223 * @param string $configdata New config information for this content
224 * @return boolean True if the configdata has been succesfully updated. False otherwise.
225 */
226 public function set_configdata(string $configdata): bool {
227 $this->content->configdata = $configdata;
228 return $this->update_content();
229 }
230
231 /**
232 * Return the content config values.
233 *
234 * @return mixed Config information for this content (json decoded)
235 */
236 public function get_configdata() {
237 return $this->content->configdata;
238 }
239
240 /**
241 * Returns the $file related to this content.
242 *
243 * @return stored_file File stored in content bank area related to the given itemid.
244 * @throws \coding_exception if not loaded.
245 */
246 public function get_file(): ?stored_file {
247 $itemid = $this->get_id();
248 $fs = get_file_storage();
249 $files = $fs->get_area_files(
250 $this->content->contextid,
251 'contentbank',
252 'public',
253 $itemid,
254 'itemid, filepath, filename',
255 false
256 );
257 if (!empty($files)) {
258 $file = reset($files);
259 return $file;
260 }
261 return null;
262 }
263
264 /**
265 * Returns the file url related to this content.
266 *
267 * @return string URL of the file stored in content bank area related to the given itemid.
268 * @throws \coding_exception if not loaded.
269 */
270 public function get_file_url(): string {
271 if (!$file = $this->get_file()) {
272 return '';
273 }
274 $fileurl = moodle_url::make_pluginfile_url(
275 $this->content->contextid,
276 'contentbank',
277 'public',
278 $file->get_itemid(),
279 $file->get_filepath(),
280 $file->get_filename()
281 );
282
283 return $fileurl;
284 }
285
286 /**
287 * Returns user has access permission for the content itself (based on what plugin needs).
288 *
289 * @return bool True if content could be accessed. False otherwise.
290 */
fb445c76 291 public function is_view_allowed(): bool {
33b8ca26
AA
292 // There's no capability at content level to check,
293 // but plugins can overwrite this method in case they want to check something related to content properties.
294 return true;
295 }
296}