MDL-57878 core_calendar: additional unit test for bailout callback
[moodle.git] / mod / label / lib.php
CommitLineData
44a87f63 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
2c5c3e62 17
44a87f63 18/**
19 * Library of functions and constants for module label
20 *
53945f38 21 * @package mod_label
9516942a
PS
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44a87f63 24 */
2c5c3e62 25
9516942a
PS
26defined('MOODLE_INTERNAL') || die;
27
44a87f63 28/** LABEL_MAX_NAME_LENGTH = 50 */
2c5c3e62 29define("LABEL_MAX_NAME_LENGTH", 50);
30
44a87f63 31/**
32 * @uses LABEL_MAX_NAME_LENGTH
33 * @param object $label
34 * @return string
35 */
376ca347 36function get_label_name($label) {
43b44d5e 37 $name = strip_tags(format_string($label->intro,true));
2f1e464a
PS
38 if (core_text::strlen($name) > LABEL_MAX_NAME_LENGTH) {
39 $name = core_text::substr($name, 0, LABEL_MAX_NAME_LENGTH)."...";
376ca347 40 }
41
42 if (empty($name)) {
43 // arbitrary name
c0a578c0 44 $name = get_string('modulename','label');
376ca347 45 }
46
47 return $name;
48}
44a87f63 49/**
50 * Given an object containing all the necessary data,
51 * (defined by the form in mod_form.php) this function
52 * will create a new instance and return the id number
53 * of the new instance.
54 *
55 * @global object
56 * @param object $label
57 * @return bool|int
58 */
2c5c3e62 59function label_add_instance($label) {
c18269c7 60 global $DB;
2c5c3e62 61
376ca347 62 $label->name = get_label_name($label);
2c5c3e62 63 $label->timemodified = time();
64
c18269c7 65 return $DB->insert_record("label", $label);
2c5c3e62 66}
67
44a87f63 68/**
69 * Given an object containing all the necessary data,
70 * (defined by the form in mod_form.php) this function
71 * will update an existing instance with new data.
72 *
73 * @global object
74 * @param object $label
75 * @return bool
76 */
2c5c3e62 77function label_update_instance($label) {
c18269c7 78 global $DB;
2bf5b5d4 79
376ca347 80 $label->name = get_label_name($label);
2c5c3e62 81 $label->timemodified = time();
82 $label->id = $label->instance;
83
c18269c7 84 return $DB->update_record("label", $label);
2c5c3e62 85}
86
44a87f63 87/**
88 * Given an ID of an instance of this module,
89 * this function will permanently delete the instance
90 * and any data that depends on it.
91 *
92 * @global object
93 * @param int $id
94 * @return bool
95 */
2c5c3e62 96function label_delete_instance($id) {
c18269c7 97 global $DB;
2c5c3e62 98
c18269c7 99 if (! $label = $DB->get_record("label", array("id"=>$id))) {
2c5c3e62 100 return false;
101 }
102
103 $result = true;
104
c18269c7 105 if (! $DB->delete_records("label", array("id"=>$label->id))) {
2c5c3e62 106 $result = false;
107 }
108
109 return $result;
110}
111
f36cbf1d 112/**
113 * Given a course_module object, this function returns any
114 * "extra" information that may be needed when printing
115 * this activity in a course listing.
116 * See get_array_of_activities() in course/lib.php
44a87f63 117 *
118 * @global object
119 * @param object $coursemodule
3c620d7f 120 * @return cached_cm_info|null
f36cbf1d 121 */
8dddba42 122function label_get_coursemodule_info($coursemodule) {
1b81b0a4 123 global $DB;
124
43b44d5e 125 if ($label = $DB->get_record('label', array('id'=>$coursemodule->instance), 'id, name, intro, introformat')) {
d02f1934 126 if (empty($label->name)) {
127 // label name missing, fix it
128 $label->name = "label{$label->id}";
129 $DB->set_field('label', 'name', $label->name, array('id'=>$label->id));
130 }
3008f86c 131 $info = new cached_cm_info();
43b44d5e 132 // no filtering hre because this info is cached and filtered later
3008f86c 133 $info->content = format_module_intro('label', $label, $coursemodule->id, false);
9a9012dc 134 $info->name = $label->name;
dd97c328 135 return $info;
136 } else {
137 return null;
138 }
8dddba42 139}
140
0b5a80a1 141/**
142 * This function is used by the reset_course_userdata function in moodlelib.
86342d63 143 *
44a87f63 144 * @param object $data the data submitted from the reset course.
0b5a80a1 145 * @return array status array
146 */
147function label_reset_userdata($data) {
148 return array();
149}
150
f432bebf 151/**
152 * Returns all other caps used in module
44a87f63 153 *
154 * @return array
f432bebf 155 */
e83a6a22 156function label_get_extra_capabilities() {
f432bebf 157 return array('moodle/site:accessallgroups');
158}
159
42f103be 160/**
44a87f63 161 * @uses FEATURE_IDNUMBER
162 * @uses FEATURE_GROUPS
163 * @uses FEATURE_GROUPINGS
44a87f63 164 * @uses FEATURE_MOD_INTRO
165 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
166 * @uses FEATURE_GRADE_HAS_GRADE
167 * @uses FEATURE_GRADE_OUTCOMES
42f103be 168 * @param string $feature FEATURE_xx constant for requested feature
44a87f63 169 * @return bool|null True if module supports feature, false if not, null if doesn't know
42f103be 170 */
171function label_supports($feature) {
172 switch($feature) {
173 case FEATURE_IDNUMBER: return false;
174 case FEATURE_GROUPS: return false;
175 case FEATURE_GROUPINGS: return false;
43b44d5e 176 case FEATURE_MOD_INTRO: return true;
42f103be 177 case FEATURE_COMPLETION_TRACKS_VIEWS: return false;
178 case FEATURE_GRADE_HAS_GRADE: return false;
179 case FEATURE_GRADE_OUTCOMES: return false;
31dadc6f 180 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
f58a9e37 181 case FEATURE_BACKUP_MOODLE2: return true;
0d8b6a69 182 case FEATURE_NO_VIEW_LINK: return true;
42f103be 183
184 default: return null;
185 }
186}
187
785e09a7
DS
188/**
189 * Register the ability to handle drag and drop file uploads
190 * @return array containing details of the files / types the mod can handle
191 */
192function label_dndupload_register() {
5efae682 193 $strdnd = get_string('dnduploadlabel', 'mod_label');
785e09a7 194 if (get_config('label', 'dndmedia')) {
59e004cd 195 $mediaextensions = file_get_typegroup('extension', ['web_image', 'web_video', 'web_audio']);
785e09a7
DS
196 $files = array();
197 foreach ($mediaextensions as $extn) {
198 $extn = trim($extn, '.');
199 $files[] = array('extension' => $extn, 'message' => $strdnd);
200 }
5efae682 201 $ret = array('files' => $files);
785e09a7 202 } else {
5efae682 203 $ret = array();
785e09a7 204 }
5efae682
DS
205
206 $strdndtext = get_string('dnduploadlabeltext', 'mod_label');
207 return array_merge($ret, array('types' => array(
2748d8ef
DS
208 array('identifier' => 'text/html', 'message' => $strdndtext, 'noname' => true),
209 array('identifier' => 'text', 'message' => $strdndtext, 'noname' => true)
5efae682 210 )));
785e09a7
DS
211}
212
213/**
214 * Handle a file that has been uploaded
215 * @param object $uploadinfo details of the file / content that has been uploaded
216 * @return int instance id of the newly created mod
217 */
218function label_dndupload_handle($uploadinfo) {
219 global $USER;
220
221 // Gather the required info.
222 $data = new stdClass();
223 $data->course = $uploadinfo->course->id;
224 $data->name = $uploadinfo->displayname;
225 $data->intro = '';
226 $data->introformat = FORMAT_HTML;
227 $data->coursemodule = $uploadinfo->coursemodule;
228
fbe45792 229 // Extract the first (and only) file from the file area and add it to the label as an img tag.
785e09a7
DS
230 if (!empty($uploadinfo->draftitemid)) {
231 $fs = get_file_storage();
232 $draftcontext = context_user::instance($USER->id);
233 $context = context_module::instance($uploadinfo->coursemodule);
234 $files = $fs->get_area_files($draftcontext->id, 'user', 'draft', $uploadinfo->draftitemid, '', false);
235 if ($file = reset($files)) {
785e09a7 236 if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) {
fbe45792
DS
237 // It is an image - resize it, if too big, then insert the img tag.
238 $config = get_config('label');
239 $data->intro = label_generate_resized_image($file, $config->dndresizewidth, $config->dndresizeheight);
240 } else {
241 // We aren't supposed to be supporting non-image types here, but fallback to adding a link, just in case.
242 $url = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename());
243 $data->intro = html_writer::link($url, $file->get_filename());
785e09a7 244 }
fbe45792
DS
245 $data->intro = file_save_draft_area_files($uploadinfo->draftitemid, $context->id, 'mod_label', 'intro', 0,
246 null, $data->intro);
785e09a7 247 }
5efae682
DS
248 } else if (!empty($uploadinfo->content)) {
249 $data->intro = $uploadinfo->content;
3d387773
DS
250 if ($uploadinfo->type != 'text/html') {
251 $data->introformat = FORMAT_PLAIN;
252 }
785e09a7
DS
253 }
254
255 return label_add_instance($data, null);
256}
fbe45792
DS
257
258/**
259 * Resize the image, if required, then generate an img tag and, if required, a link to the full-size image
260 * @param stored_file $file the image file to process
261 * @param int $maxwidth the maximum width allowed for the image
262 * @param int $maxheight the maximum height allowed for the image
263 * @return string HTML fragment to add to the label
264 */
265function label_generate_resized_image(stored_file $file, $maxwidth, $maxheight) {
266 global $CFG;
267
268 $fullurl = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename());
269 $link = null;
270 $attrib = array('alt' => $file->get_filename(), 'src' => $fullurl);
271
272 if ($imginfo = $file->get_imageinfo()) {
273 // Work out the new width / height, bounded by maxwidth / maxheight
274 $width = $imginfo['width'];
275 $height = $imginfo['height'];
276 if (!empty($maxwidth) && $width > $maxwidth) {
277 $height *= (float)$maxwidth / $width;
278 $width = $maxwidth;
279 }
280 if (!empty($maxheight) && $height > $maxheight) {
281 $width *= (float)$maxheight / $height;
282 $height = $maxheight;
283 }
284
285 $attrib['width'] = $width;
286 $attrib['height'] = $height;
287
288 // If the size has changed and the image is of a suitable mime type, generate a smaller version
289 if ($width != $imginfo['width']) {
290 $mimetype = $file->get_mimetype();
291 if ($mimetype === 'image/gif' or $mimetype === 'image/jpeg' or $mimetype === 'image/png') {
292 require_once($CFG->libdir.'/gdlib.php');
2b53b13f 293 $data = $file->generate_image_thumbnail($width, $height);
fbe45792
DS
294
295 if (!empty($data)) {
296 $fs = get_file_storage();
297 $record = array(
298 'contextid' => $file->get_contextid(),
299 'component' => $file->get_component(),
300 'filearea' => $file->get_filearea(),
301 'itemid' => $file->get_itemid(),
302 'filepath' => '/',
303 'filename' => 's_'.$file->get_filename(),
304 );
305 $smallfile = $fs->create_file_from_string($record, $data);
306
307 // Replace the image 'src' with the resized file and link to the original
308 $attrib['src'] = moodle_url::make_draftfile_url($smallfile->get_itemid(), $smallfile->get_filepath(),
309 $smallfile->get_filename());
310 $link = $fullurl;
311 }
312 }
313 }
314
315 } else {
316 // Assume this is an image type that get_imageinfo cannot handle (e.g. SVG)
317 $attrib['width'] = $maxwidth;
318 }
319
320 $img = html_writer::empty_tag('img', $attrib);
321 if ($link) {
322 return html_writer::link($link, $img);
323 } else {
324 return $img;
325 }
5efae682 326}
02a73d76
JL
327
328/**
329 * Check if the module has any update that affects the current user since a given time.
330 *
331 * @param cm_info $cm course module data
332 * @param int $from the time to check updates from
333 * @param array $filter if we need to check only specific updates
334 * @return stdClass an object with the different type of areas indicating if they were updated or not
335 * @since Moodle 3.2
336 */
337function label_check_updates_since(cm_info $cm, $from, $filter = array()) {
338 $updates = course_check_module_updates_since($cm, $from, array(), $filter);
339 return $updates;
340}