weekly release 2.5dev
[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 *
9516942a
PS
21 * @package mod
22 * @subpackage label
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44a87f63 25 */
2c5c3e62 26
9516942a
PS
27defined('MOODLE_INTERNAL') || die;
28
44a87f63 29/** LABEL_MAX_NAME_LENGTH = 50 */
2c5c3e62 30define("LABEL_MAX_NAME_LENGTH", 50);
31
44a87f63 32/**
33 * @uses LABEL_MAX_NAME_LENGTH
34 * @param object $label
35 * @return string
36 */
376ca347 37function get_label_name($label) {
43b44d5e 38 $name = strip_tags(format_string($label->intro,true));
f8311def
PS
39 if (textlib::strlen($name) > LABEL_MAX_NAME_LENGTH) {
40 $name = textlib::substr($name, 0, LABEL_MAX_NAME_LENGTH)."...";
376ca347 41 }
42
43 if (empty($name)) {
44 // arbitrary name
c0a578c0 45 $name = get_string('modulename','label');
376ca347 46 }
47
48 return $name;
49}
44a87f63 50/**
51 * Given an object containing all the necessary data,
52 * (defined by the form in mod_form.php) this function
53 * will create a new instance and return the id number
54 * of the new instance.
55 *
56 * @global object
57 * @param object $label
58 * @return bool|int
59 */
2c5c3e62 60function label_add_instance($label) {
c18269c7 61 global $DB;
2c5c3e62 62
376ca347 63 $label->name = get_label_name($label);
2c5c3e62 64 $label->timemodified = time();
65
c18269c7 66 return $DB->insert_record("label", $label);
2c5c3e62 67}
68
44a87f63 69/**
70 * Given an object containing all the necessary data,
71 * (defined by the form in mod_form.php) this function
72 * will update an existing instance with new data.
73 *
74 * @global object
75 * @param object $label
76 * @return bool
77 */
2c5c3e62 78function label_update_instance($label) {
c18269c7 79 global $DB;
2bf5b5d4 80
376ca347 81 $label->name = get_label_name($label);
2c5c3e62 82 $label->timemodified = time();
83 $label->id = $label->instance;
84
c18269c7 85 return $DB->update_record("label", $label);
2c5c3e62 86}
87
44a87f63 88/**
89 * Given an ID of an instance of this module,
90 * this function will permanently delete the instance
91 * and any data that depends on it.
92 *
93 * @global object
94 * @param int $id
95 * @return bool
96 */
2c5c3e62 97function label_delete_instance($id) {
c18269c7 98 global $DB;
2c5c3e62 99
c18269c7 100 if (! $label = $DB->get_record("label", array("id"=>$id))) {
2c5c3e62 101 return false;
102 }
103
104 $result = true;
105
c18269c7 106 if (! $DB->delete_records("label", array("id"=>$label->id))) {
2c5c3e62 107 $result = false;
108 }
109
110 return $result;
111}
112
f36cbf1d 113/**
114 * Given a course_module object, this function returns any
115 * "extra" information that may be needed when printing
116 * this activity in a course listing.
117 * See get_array_of_activities() in course/lib.php
44a87f63 118 *
119 * @global object
120 * @param object $coursemodule
121 * @return object|null
f36cbf1d 122 */
8dddba42 123function label_get_coursemodule_info($coursemodule) {
1b81b0a4 124 global $DB;
125
43b44d5e 126 if ($label = $DB->get_record('label', array('id'=>$coursemodule->instance), 'id, name, intro, introformat')) {
d02f1934 127 if (empty($label->name)) {
128 // label name missing, fix it
129 $label->name = "label{$label->id}";
130 $DB->set_field('label', 'name', $label->name, array('id'=>$label->id));
131 }
3008f86c 132 $info = new cached_cm_info();
43b44d5e 133 // no filtering hre because this info is cached and filtered later
3008f86c 134 $info->content = format_module_intro('label', $label, $coursemodule->id, false);
9a9012dc 135 $info->name = $label->name;
dd97c328 136 return $info;
137 } else {
138 return null;
139 }
8dddba42 140}
141
44a87f63 142/**
143 * @return array
144 */
f3221af9 145function label_get_view_actions() {
146 return array();
147}
148
44a87f63 149/**
150 * @return array
151 */
f3221af9 152function label_get_post_actions() {
153 return array();
154}
155
0b5a80a1 156/**
157 * This function is used by the reset_course_userdata function in moodlelib.
86342d63 158 *
44a87f63 159 * @param object $data the data submitted from the reset course.
0b5a80a1 160 * @return array status array
161 */
162function label_reset_userdata($data) {
163 return array();
164}
165
f432bebf 166/**
167 * Returns all other caps used in module
44a87f63 168 *
169 * @return array
f432bebf 170 */
e83a6a22 171function label_get_extra_capabilities() {
f432bebf 172 return array('moodle/site:accessallgroups');
173}
174
42f103be 175/**
44a87f63 176 * @uses FEATURE_IDNUMBER
177 * @uses FEATURE_GROUPS
178 * @uses FEATURE_GROUPINGS
179 * @uses FEATURE_GROUPMEMBERSONLY
180 * @uses FEATURE_MOD_INTRO
181 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
182 * @uses FEATURE_GRADE_HAS_GRADE
183 * @uses FEATURE_GRADE_OUTCOMES
42f103be 184 * @param string $feature FEATURE_xx constant for requested feature
44a87f63 185 * @return bool|null True if module supports feature, false if not, null if doesn't know
42f103be 186 */
187function label_supports($feature) {
188 switch($feature) {
189 case FEATURE_IDNUMBER: return false;
190 case FEATURE_GROUPS: return false;
191 case FEATURE_GROUPINGS: return false;
192 case FEATURE_GROUPMEMBERSONLY: return true;
43b44d5e 193 case FEATURE_MOD_INTRO: return true;
42f103be 194 case FEATURE_COMPLETION_TRACKS_VIEWS: return false;
195 case FEATURE_GRADE_HAS_GRADE: return false;
196 case FEATURE_GRADE_OUTCOMES: return false;
31dadc6f 197 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
f58a9e37 198 case FEATURE_BACKUP_MOODLE2: return true;
0d8b6a69 199 case FEATURE_NO_VIEW_LINK: return true;
42f103be 200
201 default: return null;
202 }
203}
204
785e09a7
DS
205/**
206 * Register the ability to handle drag and drop file uploads
207 * @return array containing details of the files / types the mod can handle
208 */
209function label_dndupload_register() {
210 if (get_config('label', 'dndmedia')) {
211 $mediaextensions = file_get_typegroup('extension', 'web_image');
212 $strdnd = get_string('dnduploadlabel', 'mod_label');
213 $files = array();
214 foreach ($mediaextensions as $extn) {
215 $extn = trim($extn, '.');
216 $files[] = array('extension' => $extn, 'message' => $strdnd);
217 }
218 return array('files' => $files);
219 } else {
220 return array();
221 }
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
DS
258 }
259 }
260
261 return label_add_instance($data, null);
262}
fbe45792
DS
263
264/**
265 * Resize the image, if required, then generate an img tag and, if required, a link to the full-size image
266 * @param stored_file $file the image file to process
267 * @param int $maxwidth the maximum width allowed for the image
268 * @param int $maxheight the maximum height allowed for the image
269 * @return string HTML fragment to add to the label
270 */
271function label_generate_resized_image(stored_file $file, $maxwidth, $maxheight) {
272 global $CFG;
273
274 $fullurl = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename());
275 $link = null;
276 $attrib = array('alt' => $file->get_filename(), 'src' => $fullurl);
277
278 if ($imginfo = $file->get_imageinfo()) {
279 // Work out the new width / height, bounded by maxwidth / maxheight
280 $width = $imginfo['width'];
281 $height = $imginfo['height'];
282 if (!empty($maxwidth) && $width > $maxwidth) {
283 $height *= (float)$maxwidth / $width;
284 $width = $maxwidth;
285 }
286 if (!empty($maxheight) && $height > $maxheight) {
287 $width *= (float)$maxheight / $height;
288 $height = $maxheight;
289 }
290
291 $attrib['width'] = $width;
292 $attrib['height'] = $height;
293
294 // If the size has changed and the image is of a suitable mime type, generate a smaller version
295 if ($width != $imginfo['width']) {
296 $mimetype = $file->get_mimetype();
297 if ($mimetype === 'image/gif' or $mimetype === 'image/jpeg' or $mimetype === 'image/png') {
298 require_once($CFG->libdir.'/gdlib.php');
299 $tmproot = make_temp_directory('mod_label');
300 $tmpfilepath = $tmproot.'/'.$file->get_contenthash();
301 $file->copy_content_to($tmpfilepath);
302 $data = generate_image_thumbnail($tmpfilepath, $width, $height);
303 unlink($tmpfilepath);
304
305 if (!empty($data)) {
306 $fs = get_file_storage();
307 $record = array(
308 'contextid' => $file->get_contextid(),
309 'component' => $file->get_component(),
310 'filearea' => $file->get_filearea(),
311 'itemid' => $file->get_itemid(),
312 'filepath' => '/',
313 'filename' => 's_'.$file->get_filename(),
314 );
315 $smallfile = $fs->create_file_from_string($record, $data);
316
317 // Replace the image 'src' with the resized file and link to the original
318 $attrib['src'] = moodle_url::make_draftfile_url($smallfile->get_itemid(), $smallfile->get_filepath(),
319 $smallfile->get_filename());
320 $link = $fullurl;
321 }
322 }
323 }
324
325 } else {
326 // Assume this is an image type that get_imageinfo cannot handle (e.g. SVG)
327 $attrib['width'] = $maxwidth;
328 }
329
330 $img = html_writer::empty_tag('img', $attrib);
331 if ($link) {
332 return html_writer::link($link, $img);
333 } else {
334 return $img;
335 }
336}