on-demand release 3.3dev+
[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
b3bd7a66
MN
65 $id = $DB->insert_record("label", $label);
66
67 $completiontimeexpected = !empty($label->completionexpected) ? $label->completionexpected : null;
68 \core_completion\api::update_completion_date_event($label->coursemodule, 'label', $id, $completiontimeexpected);
69
70 return $id;
2c5c3e62 71}
72
44a87f63 73/**
74 * Given an object containing all the necessary data,
75 * (defined by the form in mod_form.php) this function
76 * will update an existing instance with new data.
77 *
78 * @global object
79 * @param object $label
80 * @return bool
81 */
2c5c3e62 82function label_update_instance($label) {
c18269c7 83 global $DB;
2bf5b5d4 84
376ca347 85 $label->name = get_label_name($label);
2c5c3e62 86 $label->timemodified = time();
87 $label->id = $label->instance;
88
b3bd7a66
MN
89 $completiontimeexpected = !empty($label->completionexpected) ? $label->completionexpected : null;
90 \core_completion\api::update_completion_date_event($label->coursemodule, 'label', $label->id, $completiontimeexpected);
91
c18269c7 92 return $DB->update_record("label", $label);
2c5c3e62 93}
94
44a87f63 95/**
96 * Given an ID of an instance of this module,
97 * this function will permanently delete the instance
98 * and any data that depends on it.
99 *
100 * @global object
101 * @param int $id
102 * @return bool
103 */
2c5c3e62 104function label_delete_instance($id) {
c18269c7 105 global $DB;
2c5c3e62 106
c18269c7 107 if (! $label = $DB->get_record("label", array("id"=>$id))) {
2c5c3e62 108 return false;
109 }
110
111 $result = true;
112
b3bd7a66 113 $cm = get_coursemodule_from_instance('label', $id);
de036a2d 114 \core_completion\api::update_completion_date_event($cm->id, 'label', $label->id, null);
b3bd7a66 115
c18269c7 116 if (! $DB->delete_records("label", array("id"=>$label->id))) {
2c5c3e62 117 $result = false;
118 }
119
120 return $result;
121}
122
f36cbf1d 123/**
124 * Given a course_module object, this function returns any
125 * "extra" information that may be needed when printing
126 * this activity in a course listing.
127 * See get_array_of_activities() in course/lib.php
44a87f63 128 *
129 * @global object
130 * @param object $coursemodule
3c620d7f 131 * @return cached_cm_info|null
f36cbf1d 132 */
8dddba42 133function label_get_coursemodule_info($coursemodule) {
1b81b0a4 134 global $DB;
135
43b44d5e 136 if ($label = $DB->get_record('label', array('id'=>$coursemodule->instance), 'id, name, intro, introformat')) {
d02f1934 137 if (empty($label->name)) {
138 // label name missing, fix it
139 $label->name = "label{$label->id}";
140 $DB->set_field('label', 'name', $label->name, array('id'=>$label->id));
141 }
3008f86c 142 $info = new cached_cm_info();
43b44d5e 143 // no filtering hre because this info is cached and filtered later
3008f86c 144 $info->content = format_module_intro('label', $label, $coursemodule->id, false);
9a9012dc 145 $info->name = $label->name;
dd97c328 146 return $info;
147 } else {
148 return null;
149 }
8dddba42 150}
151
0b5a80a1 152/**
153 * This function is used by the reset_course_userdata function in moodlelib.
86342d63 154 *
44a87f63 155 * @param object $data the data submitted from the reset course.
0b5a80a1 156 * @return array status array
157 */
158function label_reset_userdata($data) {
159 return array();
160}
161
f432bebf 162/**
163 * Returns all other caps used in module
44a87f63 164 *
165 * @return array
f432bebf 166 */
e83a6a22 167function label_get_extra_capabilities() {
f432bebf 168 return array('moodle/site:accessallgroups');
169}
170
42f103be 171/**
44a87f63 172 * @uses FEATURE_IDNUMBER
173 * @uses FEATURE_GROUPS
174 * @uses FEATURE_GROUPINGS
44a87f63 175 * @uses FEATURE_MOD_INTRO
176 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
177 * @uses FEATURE_GRADE_HAS_GRADE
178 * @uses FEATURE_GRADE_OUTCOMES
42f103be 179 * @param string $feature FEATURE_xx constant for requested feature
44a87f63 180 * @return bool|null True if module supports feature, false if not, null if doesn't know
42f103be 181 */
182function label_supports($feature) {
183 switch($feature) {
184 case FEATURE_IDNUMBER: return false;
185 case FEATURE_GROUPS: return false;
186 case FEATURE_GROUPINGS: return false;
43b44d5e 187 case FEATURE_MOD_INTRO: return true;
42f103be 188 case FEATURE_COMPLETION_TRACKS_VIEWS: return false;
189 case FEATURE_GRADE_HAS_GRADE: return false;
190 case FEATURE_GRADE_OUTCOMES: return false;
31dadc6f 191 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
f58a9e37 192 case FEATURE_BACKUP_MOODLE2: return true;
0d8b6a69 193 case FEATURE_NO_VIEW_LINK: return true;
42f103be 194
195 default: return null;
196 }
197}
198
785e09a7
DS
199/**
200 * Register the ability to handle drag and drop file uploads
201 * @return array containing details of the files / types the mod can handle
202 */
203function label_dndupload_register() {
5efae682 204 $strdnd = get_string('dnduploadlabel', 'mod_label');
785e09a7 205 if (get_config('label', 'dndmedia')) {
59e004cd 206 $mediaextensions = file_get_typegroup('extension', ['web_image', 'web_video', 'web_audio']);
785e09a7
DS
207 $files = array();
208 foreach ($mediaextensions as $extn) {
209 $extn = trim($extn, '.');
210 $files[] = array('extension' => $extn, 'message' => $strdnd);
211 }
5efae682 212 $ret = array('files' => $files);
785e09a7 213 } else {
5efae682 214 $ret = array();
785e09a7 215 }
5efae682
DS
216
217 $strdndtext = get_string('dnduploadlabeltext', 'mod_label');
218 return array_merge($ret, array('types' => array(
2748d8ef
DS
219 array('identifier' => 'text/html', 'message' => $strdndtext, 'noname' => true),
220 array('identifier' => 'text', 'message' => $strdndtext, 'noname' => true)
5efae682 221 )));
785e09a7
DS
222}
223
224/**
225 * Handle a file that has been uploaded
226 * @param object $uploadinfo details of the file / content that has been uploaded
227 * @return int instance id of the newly created mod
228 */
229function label_dndupload_handle($uploadinfo) {
230 global $USER;
231
232 // Gather the required info.
233 $data = new stdClass();
234 $data->course = $uploadinfo->course->id;
235 $data->name = $uploadinfo->displayname;
236 $data->intro = '';
237 $data->introformat = FORMAT_HTML;
238 $data->coursemodule = $uploadinfo->coursemodule;
239
fbe45792 240 // Extract the first (and only) file from the file area and add it to the label as an img tag.
785e09a7
DS
241 if (!empty($uploadinfo->draftitemid)) {
242 $fs = get_file_storage();
243 $draftcontext = context_user::instance($USER->id);
244 $context = context_module::instance($uploadinfo->coursemodule);
245 $files = $fs->get_area_files($draftcontext->id, 'user', 'draft', $uploadinfo->draftitemid, '', false);
246 if ($file = reset($files)) {
785e09a7 247 if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) {
fbe45792
DS
248 // It is an image - resize it, if too big, then insert the img tag.
249 $config = get_config('label');
250 $data->intro = label_generate_resized_image($file, $config->dndresizewidth, $config->dndresizeheight);
251 } else {
252 // We aren't supposed to be supporting non-image types here, but fallback to adding a link, just in case.
253 $url = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename());
254 $data->intro = html_writer::link($url, $file->get_filename());
785e09a7 255 }
fbe45792
DS
256 $data->intro = file_save_draft_area_files($uploadinfo->draftitemid, $context->id, 'mod_label', 'intro', 0,
257 null, $data->intro);
785e09a7 258 }
5efae682
DS
259 } else if (!empty($uploadinfo->content)) {
260 $data->intro = $uploadinfo->content;
3d387773
DS
261 if ($uploadinfo->type != 'text/html') {
262 $data->introformat = FORMAT_PLAIN;
263 }
785e09a7
DS
264 }
265
266 return label_add_instance($data, null);
267}
fbe45792
DS
268
269/**
270 * Resize the image, if required, then generate an img tag and, if required, a link to the full-size image
271 * @param stored_file $file the image file to process
272 * @param int $maxwidth the maximum width allowed for the image
273 * @param int $maxheight the maximum height allowed for the image
274 * @return string HTML fragment to add to the label
275 */
276function label_generate_resized_image(stored_file $file, $maxwidth, $maxheight) {
277 global $CFG;
278
279 $fullurl = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename());
280 $link = null;
281 $attrib = array('alt' => $file->get_filename(), 'src' => $fullurl);
282
283 if ($imginfo = $file->get_imageinfo()) {
284 // Work out the new width / height, bounded by maxwidth / maxheight
285 $width = $imginfo['width'];
286 $height = $imginfo['height'];
287 if (!empty($maxwidth) && $width > $maxwidth) {
288 $height *= (float)$maxwidth / $width;
289 $width = $maxwidth;
290 }
291 if (!empty($maxheight) && $height > $maxheight) {
292 $width *= (float)$maxheight / $height;
293 $height = $maxheight;
294 }
295
296 $attrib['width'] = $width;
297 $attrib['height'] = $height;
298
299 // If the size has changed and the image is of a suitable mime type, generate a smaller version
300 if ($width != $imginfo['width']) {
301 $mimetype = $file->get_mimetype();
302 if ($mimetype === 'image/gif' or $mimetype === 'image/jpeg' or $mimetype === 'image/png') {
303 require_once($CFG->libdir.'/gdlib.php');
2b53b13f 304 $data = $file->generate_image_thumbnail($width, $height);
fbe45792
DS
305
306 if (!empty($data)) {
307 $fs = get_file_storage();
308 $record = array(
309 'contextid' => $file->get_contextid(),
310 'component' => $file->get_component(),
311 'filearea' => $file->get_filearea(),
312 'itemid' => $file->get_itemid(),
313 'filepath' => '/',
314 'filename' => 's_'.$file->get_filename(),
315 );
316 $smallfile = $fs->create_file_from_string($record, $data);
317
318 // Replace the image 'src' with the resized file and link to the original
319 $attrib['src'] = moodle_url::make_draftfile_url($smallfile->get_itemid(), $smallfile->get_filepath(),
320 $smallfile->get_filename());
321 $link = $fullurl;
322 }
323 }
324 }
325
326 } else {
327 // Assume this is an image type that get_imageinfo cannot handle (e.g. SVG)
328 $attrib['width'] = $maxwidth;
329 }
330
331 $img = html_writer::empty_tag('img', $attrib);
332 if ($link) {
333 return html_writer::link($link, $img);
334 } else {
335 return $img;
336 }
5efae682 337}
02a73d76
JL
338
339/**
340 * Check if the module has any update that affects the current user since a given time.
341 *
342 * @param cm_info $cm course module data
343 * @param int $from the time to check updates from
344 * @param array $filter if we need to check only specific updates
345 * @return stdClass an object with the different type of areas indicating if they were updated or not
346 * @since Moodle 3.2
347 */
348function label_check_updates_since(cm_info $cm, $from, $filter = array()) {
349 $updates = course_check_module_updates_since($cm, $from, array(), $filter);
350 return $updates;
351}
b3bd7a66
MN
352
353/**
354 * Handles creating actions for events.
355 *
e1cd93ce 356 * @param calendar_event $event
b3bd7a66 357 * @param \core_calendar\action_factory $factory
01f96180 358 * @return \core_calendar\local\event\entities\action_interface|null
b3bd7a66 359 */
e1cd93ce 360function mod_label_core_calendar_provide_event_action(calendar_event $event,
b3bd7a66
MN
361 \core_calendar\action_factory $factory) {
362 $cm = get_fast_modinfo($event->courseid)->instances['label'][$event->instance];
363
364 $course = new stdClass();
365 $course->id = $event->courseid;
366 $completion = new \completion_info($course);
367
368 $completiondata = $completion->get_data($cm, false);
369
370 if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
371 return null;
372 }
373
374 return $factory->create_instance(
375 get_string('view'),
376 new \moodle_url('/mod/label/view.php', ['id' => $cm->id]),
377 1,
378 true
379 );
380}