Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]
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/>. 17 18 /** 19 * Library of functions and constants for module label 20 * 21 * @package mod_label 22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die; 27 28 /** LABEL_MAX_NAME_LENGTH = 50 */ 29 define("LABEL_MAX_NAME_LENGTH", 50); 30 31 /** 32 * @uses LABEL_MAX_NAME_LENGTH 33 * @param object $label 34 * @return string 35 */ 36 function get_label_name($label) { 37 // Return label name if not empty. 38 if ($label->name) { 39 return $label->name; 40 } 41 42 $context = context_module::instance($label->coursemodule); 43 $intro = format_text($label->intro, $label->introformat, ['filter' => false, 'context' => $context]); 44 $name = html_to_text(format_string($intro, true, ['context' => $context])); 45 $name = preg_replace('/@@PLUGINFILE@@\/[[:^space:]]+/i', '', $name); 46 // Remove double space and also nbsp; characters. 47 $name = preg_replace('/\s+/u', ' ', $name); 48 $name = trim($name); 49 if (core_text::strlen($name) > LABEL_MAX_NAME_LENGTH) { 50 $name = core_text::substr($name, 0, LABEL_MAX_NAME_LENGTH) . "..."; 51 } 52 53 if (empty($name)) { 54 // arbitrary name 55 $name = get_string('modulename','label'); 56 } 57 58 return $name; 59 } 60 /** 61 * Given an object containing all the necessary data, 62 * (defined by the form in mod_form.php) this function 63 * will create a new instance and return the id number 64 * of the new instance. 65 * 66 * @global object 67 * @param object $label 68 * @return bool|int 69 */ 70 function label_add_instance($label) { 71 global $DB; 72 73 $label->name = get_label_name($label); 74 $label->timemodified = time(); 75 76 $id = $DB->insert_record("label", $label); 77 78 $completiontimeexpected = !empty($label->completionexpected) ? $label->completionexpected : null; 79 \core_completion\api::update_completion_date_event($label->coursemodule, 'label', $id, $completiontimeexpected); 80 81 return $id; 82 } 83 84 /** 85 * Sets the special label display on course page. 86 * 87 * @param cm_info $cm Course-module object 88 */ 89 function label_cm_info_view(cm_info $cm) { 90 $cm->set_custom_cmlist_item(true); 91 } 92 93 /** 94 * Given an object containing all the necessary data, 95 * (defined by the form in mod_form.php) this function 96 * will update an existing instance with new data. 97 * 98 * @global object 99 * @param object $label 100 * @return bool 101 */ 102 function label_update_instance($label) { 103 global $DB; 104 105 $label->name = get_label_name($label); 106 $label->timemodified = time(); 107 $label->id = $label->instance; 108 109 $completiontimeexpected = !empty($label->completionexpected) ? $label->completionexpected : null; 110 \core_completion\api::update_completion_date_event($label->coursemodule, 'label', $label->id, $completiontimeexpected); 111 112 return $DB->update_record("label", $label); 113 } 114 115 /** 116 * Given an ID of an instance of this module, 117 * this function will permanently delete the instance 118 * and any data that depends on it. 119 * 120 * @global object 121 * @param int $id 122 * @return bool 123 */ 124 function label_delete_instance($id) { 125 global $DB; 126 127 if (! $label = $DB->get_record("label", array("id"=>$id))) { 128 return false; 129 } 130 131 $result = true; 132 133 $cm = get_coursemodule_from_instance('label', $id); 134 \core_completion\api::update_completion_date_event($cm->id, 'label', $label->id, null); 135 136 if (! $DB->delete_records("label", array("id"=>$label->id))) { 137 $result = false; 138 } 139 140 return $result; 141 } 142 143 /** 144 * Given a course_module object, this function returns any 145 * "extra" information that may be needed when printing 146 * this activity in a course listing. 147 * See get_array_of_activities() in course/lib.php 148 * 149 * @global object 150 * @param object $coursemodule 151 * @return cached_cm_info|null 152 */ 153 function label_get_coursemodule_info($coursemodule) { 154 global $DB; 155 156 if ($label = $DB->get_record('label', array('id'=>$coursemodule->instance), 'id, name, intro, introformat')) { 157 if (empty($label->name)) { 158 // label name missing, fix it 159 $label->name = "label{$label->id}"; 160 $DB->set_field('label', 'name', $label->name, array('id'=>$label->id)); 161 } 162 $info = new cached_cm_info(); 163 // no filtering hre because this info is cached and filtered later 164 $info->content = format_module_intro('label', $label, $coursemodule->id, false); 165 $info->name = $label->name; 166 return $info; 167 } else { 168 return null; 169 } 170 } 171 172 /** 173 * This function is used by the reset_course_userdata function in moodlelib. 174 * 175 * @param object $data the data submitted from the reset course. 176 * @return array status array 177 */ 178 function label_reset_userdata($data) { 179 180 // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset. 181 // See MDL-9367. 182 183 return array(); 184 } 185 186 /** 187 * @uses FEATURE_IDNUMBER 188 * @uses FEATURE_GROUPS 189 * @uses FEATURE_GROUPINGS 190 * @uses FEATURE_MOD_INTRO 191 * @uses FEATURE_COMPLETION_TRACKS_VIEWS 192 * @uses FEATURE_GRADE_HAS_GRADE 193 * @uses FEATURE_GRADE_OUTCOMES 194 * @param string $feature FEATURE_xx constant for requested feature 195 * @return mixed True if module supports feature, false if not, null if doesn't know or string for the module purpose. 196 */ 197 function label_supports($feature) { 198 switch($feature) { 199 case FEATURE_IDNUMBER: return true; 200 case FEATURE_GROUPS: return false; 201 case FEATURE_GROUPINGS: return false; 202 case FEATURE_MOD_INTRO: return true; 203 case FEATURE_COMPLETION_TRACKS_VIEWS: return false; 204 case FEATURE_GRADE_HAS_GRADE: return false; 205 case FEATURE_GRADE_OUTCOMES: return false; 206 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE; 207 case FEATURE_BACKUP_MOODLE2: return true; 208 case FEATURE_NO_VIEW_LINK: return true; 209 case FEATURE_MOD_PURPOSE: return MOD_PURPOSE_CONTENT; 210 211 default: return null; 212 } 213 } 214 215 /** 216 * Register the ability to handle drag and drop file uploads 217 * @return array containing details of the files / types the mod can handle 218 */ 219 function label_dndupload_register() { 220 $strdnd = get_string('dnduploadlabel', 'mod_label'); 221 if (get_config('label', 'dndmedia')) { 222 $mediaextensions = file_get_typegroup('extension', ['web_image', 'web_video', 'web_audio']); 223 $files = array(); 224 foreach ($mediaextensions as $extn) { 225 $extn = trim($extn, '.'); 226 $files[] = array('extension' => $extn, 'message' => $strdnd); 227 } 228 $ret = array('files' => $files); 229 } else { 230 $ret = array(); 231 } 232 233 $strdndtext = get_string('dnduploadlabeltext', 'mod_label'); 234 return array_merge($ret, array('types' => array( 235 array('identifier' => 'text/html', 'message' => $strdndtext, 'noname' => true), 236 array('identifier' => 'text', 'message' => $strdndtext, 'noname' => true) 237 ))); 238 } 239 240 /** 241 * Handle a file that has been uploaded 242 * @param object $uploadinfo details of the file / content that has been uploaded 243 * @return int instance id of the newly created mod 244 */ 245 function label_dndupload_handle($uploadinfo) { 246 global $USER; 247 248 // Gather the required info. 249 $data = new stdClass(); 250 $data->course = $uploadinfo->course->id; 251 $data->name = $uploadinfo->displayname; 252 $data->intro = ''; 253 $data->introformat = FORMAT_HTML; 254 $data->coursemodule = $uploadinfo->coursemodule; 255 256 // Extract the first (and only) file from the file area and add it to the label as an img tag. 257 if (!empty($uploadinfo->draftitemid)) { 258 $fs = get_file_storage(); 259 $draftcontext = context_user::instance($USER->id); 260 $context = context_module::instance($uploadinfo->coursemodule); 261 $files = $fs->get_area_files($draftcontext->id, 'user', 'draft', $uploadinfo->draftitemid, '', false); 262 if ($file = reset($files)) { 263 if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) { 264 // It is an image - resize it, if too big, then insert the img tag. 265 $config = get_config('label'); 266 $data->intro = label_generate_resized_image($file, $config->dndresizewidth, $config->dndresizeheight); 267 } else { 268 // We aren't supposed to be supporting non-image types here, but fallback to adding a link, just in case. 269 $url = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename()); 270 $data->intro = html_writer::link($url, $file->get_filename()); 271 } 272 $data->intro = file_save_draft_area_files($uploadinfo->draftitemid, $context->id, 'mod_label', 'intro', 0, 273 null, $data->intro); 274 } 275 } else if (!empty($uploadinfo->content)) { 276 $data->intro = $uploadinfo->content; 277 if ($uploadinfo->type != 'text/html') { 278 $data->introformat = FORMAT_PLAIN; 279 } 280 } 281 282 return label_add_instance($data, null); 283 } 284 285 /** 286 * Resize the image, if required, then generate an img tag and, if required, a link to the full-size image 287 * @param stored_file $file the image file to process 288 * @param int $maxwidth the maximum width allowed for the image 289 * @param int $maxheight the maximum height allowed for the image 290 * @return string HTML fragment to add to the label 291 */ 292 function label_generate_resized_image(stored_file $file, $maxwidth, $maxheight) { 293 global $CFG; 294 295 $fullurl = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename()); 296 $link = null; 297 $attrib = array('alt' => $file->get_filename(), 'src' => $fullurl); 298 299 if ($imginfo = $file->get_imageinfo()) { 300 // Work out the new width / height, bounded by maxwidth / maxheight 301 $width = $imginfo['width']; 302 $height = $imginfo['height']; 303 if (!empty($maxwidth) && $width > $maxwidth) { 304 $height *= (float)$maxwidth / $width; 305 $width = $maxwidth; 306 } 307 if (!empty($maxheight) && $height > $maxheight) { 308 $width *= (float)$maxheight / $height; 309 $height = $maxheight; 310 } 311 312 $attrib['width'] = $width; 313 $attrib['height'] = $height; 314 315 // If the size has changed and the image is of a suitable mime type, generate a smaller version 316 if ($width != $imginfo['width']) { 317 $mimetype = $file->get_mimetype(); 318 if ($mimetype === 'image/gif' or $mimetype === 'image/jpeg' or $mimetype === 'image/png') { 319 require_once($CFG->libdir.'/gdlib.php'); 320 $data = $file->generate_image_thumbnail($width, $height); 321 322 if (!empty($data)) { 323 $fs = get_file_storage(); 324 $record = array( 325 'contextid' => $file->get_contextid(), 326 'component' => $file->get_component(), 327 'filearea' => $file->get_filearea(), 328 'itemid' => $file->get_itemid(), 329 'filepath' => '/', 330 'filename' => 's_'.$file->get_filename(), 331 ); 332 $smallfile = $fs->create_file_from_string($record, $data); 333 334 // Replace the image 'src' with the resized file and link to the original 335 $attrib['src'] = moodle_url::make_draftfile_url($smallfile->get_itemid(), $smallfile->get_filepath(), 336 $smallfile->get_filename()); 337 $link = $fullurl; 338 } 339 } 340 } 341 342 } else { 343 // Assume this is an image type that get_imageinfo cannot handle (e.g. SVG) 344 $attrib['width'] = $maxwidth; 345 } 346 347 $attrib['class'] = "img-fluid"; 348 $img = html_writer::empty_tag('img', $attrib); 349 if ($link) { 350 return html_writer::link($link, $img); 351 } else { 352 return $img; 353 } 354 } 355 356 /** 357 * Check if the module has any update that affects the current user since a given time. 358 * 359 * @param cm_info $cm course module data 360 * @param int $from the time to check updates from 361 * @param array $filter if we need to check only specific updates 362 * @return stdClass an object with the different type of areas indicating if they were updated or not 363 * @since Moodle 3.2 364 */ 365 function label_check_updates_since(cm_info $cm, $from, $filter = array()) { 366 $updates = course_check_module_updates_since($cm, $from, array(), $filter); 367 return $updates; 368 } 369 370 /** 371 * This function receives a calendar event and returns the action associated with it, or null if there is none. 372 * 373 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event 374 * is not displayed on the block. 375 * 376 * @param calendar_event $event 377 * @param \core_calendar\action_factory $factory 378 * @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default). 379 * @return \core_calendar\local\event\entities\action_interface|null 380 */ 381 function mod_label_core_calendar_provide_event_action(calendar_event $event, 382 \core_calendar\action_factory $factory, 383 int $userid = 0) { 384 $cm = get_fast_modinfo($event->courseid, $userid)->instances['label'][$event->instance]; 385 386 if (!$cm->uservisible) { 387 // The module is not visible to the user for any reason. 388 return null; 389 } 390 391 $completion = new \completion_info($cm->get_course()); 392 393 $completiondata = $completion->get_data($cm, false, $userid); 394 395 if ($completiondata->completionstate != COMPLETION_INCOMPLETE) { 396 return null; 397 } 398 399 return $factory->create_instance( 400 get_string('view'), 401 new \moodle_url('/mod/label/view.php', ['id' => $cm->id]), 402 1, 403 true 404 ); 405 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body