MDL-67795 contentbank: delete content UI
[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
27use stored_file;
28use stdClass;
29use coding_exception;
30use moodle_url;
31
32/**
33 * Content manager class
34 *
35 * @package core_contentbank
36 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39abstract class content {
40
41 /** @var stdClass $content The content of the current instance. **/
42 protected $content = null;
43
44 /**
45 * Content bank constructor
46 *
47 * @param stdClass $content A contentbanck_content record.
48 * @throws coding_exception If content type is not right.
49 */
50 public function __construct(stdClass $content) {
51 // Content type should exist and be linked to plugin classname.
52 $classname = $content->contenttype.'\\content';
53 if (get_class($this) != $classname) {
54 throw new coding_exception(get_string('contenttypenotfound', 'error', $content->contenttype));
55 }
56 $typeclass = $content->contenttype.'\\contenttype';
57 if (!class_exists($typeclass)) {
58 throw new coding_exception(get_string('contenttypenotfound', 'error', $content->contenttype));
59 }
60 // A record with the id must exist in 'contenbank_content' table.
61 // To improve performance, we are only checking the id is set, but no querying the database.
62 if (!isset($content->id)) {
63 throw new coding_exception(get_string('invalidcontentid', 'error'));
64 }
65 $this->content = $content;
66 }
67
68 /**
69 * Returns $this->content.
70 *
71 * @return stdClass $this->content.
72 */
73 public function get_content(): stdClass {
74 return $this->content;
75 }
76
77 /**
78 * Returns $this->content->contenttype.
79 *
80 * @return string $this->content->contenttype.
81 */
82 public function get_content_type(): string {
83 return $this->content->contenttype;
84 }
85
86 /**
87 * Updates content_bank table with information in $this->content.
88 *
89 * @return boolean True if the content has been succesfully updated. False otherwise.
90 * @throws \coding_exception if not loaded.
91 */
92 public function update_content(): bool {
93 global $USER, $DB;
94
95 // A record with the id must exist in 'contenbank_content' table.
96 // To improve performance, we are only checking the id is set, but no querying the database.
97 if (!isset($this->content->id)) {
98 throw new coding_exception(get_string('invalidcontentid', 'error'));
99 }
100 $this->content->usermodified = $USER->id;
101 $this->content->timemodified = time();
102 return $DB->update_record('contentbank_content', $this->content);
103 }
104
105 /**
106 * Returns the name of the content.
107 *
108 * @return string The name of the content.
109 */
110 public function get_name(): string {
111 return $this->content->name;
112 }
113
114 /**
115 * Returns the content ID.
116 *
117 * @return int The content ID.
118 */
119 public function get_id(): int {
120 return $this->content->id;
121 }
122
123 /**
124 * Change the content instanceid value.
125 *
126 * @param int $instanceid New instanceid for this content
127 * @return boolean True if the instanceid has been succesfully updated. False otherwise.
128 */
129 public function set_instanceid(int $instanceid): bool {
130 $this->content->instanceid = $instanceid;
131 return $this->update_content();
132 }
133
134 /**
135 * Returns the $instanceid of this content.
136 *
137 * @return int contentbank instanceid
138 */
139 public function get_instanceid(): int {
140 return $this->content->instanceid;
141 }
142
143 /**
144 * Change the content config values.
145 *
146 * @param string $configdata New config information for this content
147 * @return boolean True if the configdata has been succesfully updated. False otherwise.
148 */
149 public function set_configdata(string $configdata): bool {
150 $this->content->configdata = $configdata;
151 return $this->update_content();
152 }
153
154 /**
155 * Return the content config values.
156 *
157 * @return mixed Config information for this content (json decoded)
158 */
159 public function get_configdata() {
160 return $this->content->configdata;
161 }
162
163 /**
164 * Returns the $file related to this content.
165 *
166 * @return stored_file File stored in content bank area related to the given itemid.
167 * @throws \coding_exception if not loaded.
168 */
169 public function get_file(): ?stored_file {
170 $itemid = $this->get_id();
171 $fs = get_file_storage();
172 $files = $fs->get_area_files(
173 $this->content->contextid,
174 'contentbank',
175 'public',
176 $itemid,
177 'itemid, filepath, filename',
178 false
179 );
180 if (!empty($files)) {
181 $file = reset($files);
182 return $file;
183 }
184 return null;
185 }
186
187 /**
188 * Returns the file url related to this content.
189 *
190 * @return string URL of the file stored in content bank area related to the given itemid.
191 * @throws \coding_exception if not loaded.
192 */
193 public function get_file_url(): string {
194 if (!$file = $this->get_file()) {
195 return '';
196 }
197 $fileurl = moodle_url::make_pluginfile_url(
198 $this->content->contextid,
199 'contentbank',
200 'public',
201 $file->get_itemid(),
202 $file->get_filepath(),
203 $file->get_filename()
204 );
205
206 return $fileurl;
207 }
208
209 /**
210 * Returns user has access permission for the content itself (based on what plugin needs).
211 *
212 * @return bool True if content could be accessed. False otherwise.
213 */
214 public function can_view(): bool {
215 // There's no capability at content level to check,
216 // but plugins can overwrite this method in case they want to check something related to content properties.
217 return true;
218 }
219}