Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
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 * Private resource module utility functions 20 * 21 * @package mod_resource 22 * @copyright 2009 Petr Skoda {@link http://skodak.org} 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die; 27 28 require_once("$CFG->libdir/filelib.php"); 29 require_once("$CFG->libdir/resourcelib.php"); 30 require_once("$CFG->dirroot/mod/resource/lib.php"); 31 32 /** 33 * Redirected to migrated resource if needed, 34 * return if incorrect parameters specified 35 * @param int $oldid 36 * @param int $cmid 37 * @return void 38 */ 39 function resource_redirect_if_migrated($oldid, $cmid) { 40 global $DB, $CFG; 41 42 if ($oldid) { 43 $old = $DB->get_record('resource_old', array('oldid'=>$oldid)); 44 } else { 45 $old = $DB->get_record('resource_old', array('cmid'=>$cmid)); 46 } 47 48 if (!$old) { 49 return; 50 } 51 52 redirect("$CFG->wwwroot/mod/$old->newmodule/view.php?id=".$old->cmid); 53 } 54 55 /** 56 * Display embedded resource file. 57 * @param object $resource 58 * @param object $cm 59 * @param object $course 60 * @param stored_file $file main file 61 * @return does not return 62 */ 63 function resource_display_embed($resource, $cm, $course, $file) { 64 global $CFG, $PAGE, $OUTPUT; 65 66 $clicktoopen = resource_get_clicktoopen($file, $resource->revision); 67 68 $context = context_module::instance($cm->id); 69 $moodleurl = moodle_url::make_pluginfile_url($context->id, 'mod_resource', 'content', $resource->revision, 70 $file->get_filepath(), $file->get_filename()); 71 72 $mimetype = $file->get_mimetype(); 73 $title = $resource->name; 74 75 $extension = resourcelib_get_extension($file->get_filename()); 76 77 $mediamanager = core_media_manager::instance($PAGE); 78 $embedoptions = array( 79 core_media_manager::OPTION_TRUSTED => true, 80 core_media_manager::OPTION_BLOCK => true, 81 ); 82 83 if (file_mimetype_in_typegroup($mimetype, 'web_image')) { // It's an image 84 $code = resourcelib_embed_image($moodleurl->out(), $title); 85 86 } else if ($mimetype === 'application/pdf') { 87 // PDF document 88 $code = resourcelib_embed_pdf($moodleurl->out(), $title, $clicktoopen); 89 90 } else if ($mediamanager->can_embed_url($moodleurl, $embedoptions)) { 91 // Media (audio/video) file. 92 $code = $mediamanager->embed_url($moodleurl, $title, 0, 0, $embedoptions); 93 94 } else { 95 // We need a way to discover if we are loading remote docs inside an iframe. 96 $moodleurl->param('embed', 1); 97 98 // anything else - just try object tag enlarged as much as possible 99 $code = resourcelib_embed_general($moodleurl, $title, $clicktoopen, $mimetype); 100 } 101 102 resource_print_header($resource, $cm, $course); 103 resource_print_heading($resource, $cm, $course); 104 105 echo format_text($code, FORMAT_HTML, ['noclean' => true]); 106 107 resource_print_intro($resource, $cm, $course); 108 109 echo $OUTPUT->footer(); 110 die; 111 } 112 113 /** 114 * Display resource frames. 115 * @param object $resource 116 * @param object $cm 117 * @param object $course 118 * @param stored_file $file main file 119 * @return does not return 120 */ 121 function resource_display_frame($resource, $cm, $course, $file) { 122 global $PAGE, $OUTPUT, $CFG; 123 124 $frame = optional_param('frameset', 'main', PARAM_ALPHA); 125 126 if ($frame === 'top') { 127 $PAGE->set_pagelayout('frametop'); 128 resource_print_header($resource, $cm, $course); 129 resource_print_heading($resource, $cm, $course); 130 resource_print_intro($resource, $cm, $course); 131 echo $OUTPUT->footer(); 132 die; 133 134 } else { 135 $config = get_config('resource'); 136 $context = context_module::instance($cm->id); 137 $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename(); 138 $fileurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false); 139 $navurl = "$CFG->wwwroot/mod/resource/view.php?id=$cm->id&frameset=top"; 140 $title = strip_tags(format_string($course->shortname.': '.$resource->name)); 141 $framesize = $config->framesize; 142 $contentframetitle = s(format_string($resource->name)); 143 $modulename = s(get_string('modulename','resource')); 144 $dir = get_string('thisdirection', 'langconfig'); 145 146 $file = <<<EOF 147 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> 148 <html dir="$dir"> 149 <head> 150 <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 151 <title>$title</title> 152 </head> 153 <frameset rows="$framesize,*"> 154 <frame src="$navurl" title="$modulename" /> 155 <frame src="$fileurl" title="$contentframetitle" /> 156 </frameset> 157 </html> 158 EOF; 159 160 @header('Content-Type: text/html; charset=utf-8'); 161 echo $file; 162 die; 163 } 164 } 165 166 /** 167 * Internal function - create click to open text with link. 168 */ 169 function resource_get_clicktoopen($file, $revision, $extra='') { 170 global $CFG; 171 172 $filename = $file->get_filename(); 173 $path = '/'.$file->get_contextid().'/mod_resource/content/'.$revision.$file->get_filepath().$file->get_filename(); 174 $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false); 175 176 $string = get_string('clicktoopen2', 'resource', "<a href=\"$fullurl\" $extra>$filename</a>"); 177 178 return $string; 179 } 180 181 /** 182 * Internal function - create click to open text with link. 183 */ 184 function resource_get_clicktodownload($file, $revision) { 185 global $CFG; 186 187 $filename = $file->get_filename(); 188 $path = '/'.$file->get_contextid().'/mod_resource/content/'.$revision.$file->get_filepath().$file->get_filename(); 189 $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, true); 190 191 $string = get_string('clicktodownload', 'resource', "<a href=\"$fullurl\">$filename</a>"); 192 193 return $string; 194 } 195 196 /** 197 * Print resource info and workaround link when JS not available. 198 * @param object $resource 199 * @param object $cm 200 * @param object $course 201 * @param stored_file $file main file 202 * @return does not return 203 */ 204 function resource_print_workaround($resource, $cm, $course, $file) { 205 global $CFG, $OUTPUT; 206 207 resource_print_header($resource, $cm, $course); 208 resource_print_heading($resource, $cm, $course, true); 209 resource_print_intro($resource, $cm, $course, true); 210 211 $resource->mainfile = $file->get_filename(); 212 echo '<div class="resourceworkaround">'; 213 switch (resource_get_final_display_type($resource)) { 214 case RESOURCELIB_DISPLAY_POPUP: 215 $path = '/'.$file->get_contextid().'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename(); 216 $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false); 217 $options = empty($resource->displayoptions) ? [] : (array) unserialize_array($resource->displayoptions); 218 $width = empty($options['popupwidth']) ? 620 : $options['popupwidth']; 219 $height = empty($options['popupheight']) ? 450 : $options['popupheight']; 220 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes"; 221 $extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\""; 222 echo resource_get_clicktoopen($file, $resource->revision, $extra); 223 break; 224 225 case RESOURCELIB_DISPLAY_NEW: 226 $extra = 'onclick="this.target=\'_blank\'"'; 227 echo resource_get_clicktoopen($file, $resource->revision, $extra); 228 break; 229 230 case RESOURCELIB_DISPLAY_DOWNLOAD: 231 echo resource_get_clicktodownload($file, $resource->revision); 232 break; 233 234 case RESOURCELIB_DISPLAY_OPEN: 235 default: 236 echo resource_get_clicktoopen($file, $resource->revision); 237 break; 238 } 239 echo '</div>'; 240 241 echo $OUTPUT->footer(); 242 die; 243 } 244 245 /** 246 * Print resource header. 247 * @param object $resource 248 * @param object $cm 249 * @param object $course 250 * @return void 251 */ 252 function resource_print_header($resource, $cm, $course) { 253 global $PAGE, $OUTPUT; 254 255 $PAGE->set_title($course->shortname.': '.$resource->name); 256 $PAGE->set_heading($course->fullname); 257 $PAGE->set_activity_record($resource); 258 echo $OUTPUT->header(); 259 } 260 261 /** 262 * Print resource heading. 263 * @param object $resource 264 * @param object $cm 265 * @param object $course 266 * @param bool $notused This variable is no longer used 267 * @return void 268 */ 269 function resource_print_heading($resource, $cm, $course, $notused = false) { 270 global $OUTPUT; 271 echo $OUTPUT->heading(format_string($resource->name), 2); 272 } 273 274 275 /** 276 * Gets details of the file to cache in course cache to be displayed using {@link resource_get_optional_details()} 277 * 278 * @param object $resource Resource table row (only property 'displayoptions' is used here) 279 * @param object $cm Course-module table row 280 * @return string Size and type or empty string if show options are not enabled 281 */ 282 function resource_get_file_details($resource, $cm) { 283 $options = empty($resource->displayoptions) ? [] : (array) unserialize_array($resource->displayoptions); 284 $filedetails = array(); 285 if (!empty($options['showsize']) || !empty($options['showtype']) || !empty($options['showdate'])) { 286 $context = context_module::instance($cm->id); 287 $fs = get_file_storage(); 288 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false); 289 // For a typical file resource, the sortorder is 1 for the main file 290 // and 0 for all other files. This sort approach is used just in case 291 // there are situations where the file has a different sort order. 292 $mainfile = $files ? reset($files) : null; 293 if (!empty($options['showsize'])) { 294 $filedetails['size'] = 0; 295 foreach ($files as $file) { 296 // This will also synchronize the file size for external files if needed. 297 $filedetails['size'] += $file->get_filesize(); 298 if ($file->get_repository_id()) { 299 // If file is a reference the 'size' attribute can not be cached. 300 $filedetails['isref'] = true; 301 } 302 } 303 } 304 if (!empty($options['showtype'])) { 305 if ($mainfile) { 306 $filedetails['type'] = get_mimetype_description($mainfile); 307 $filedetails['mimetype'] = $mainfile->get_mimetype(); 308 // Only show type if it is not unknown. 309 if ($filedetails['type'] === get_mimetype_description('document/unknown')) { 310 $filedetails['type'] = ''; 311 } 312 } else { 313 $filedetails['type'] = ''; 314 } 315 } 316 if (!empty($options['showdate'])) { 317 if ($mainfile) { 318 // Modified date may be up to several minutes later than uploaded date just because 319 // teacher did not submit the form promptly. Give teacher up to 5 minutes to do it. 320 if ($mainfile->get_timemodified() > $mainfile->get_timecreated() + 5 * MINSECS) { 321 $filedetails['modifieddate'] = $mainfile->get_timemodified(); 322 } else { 323 $filedetails['uploadeddate'] = $mainfile->get_timecreated(); 324 } 325 if ($mainfile->get_repository_id()) { 326 // If main file is a reference the 'date' attribute can not be cached. 327 $filedetails['isref'] = true; 328 } 329 } else { 330 $filedetails['uploadeddate'] = ''; 331 } 332 } 333 } 334 return $filedetails; 335 } 336 337 /** 338 * Gets optional details for a resource, depending on resource settings. 339 * 340 * Result may include the file size and type if those settings are chosen, 341 * or blank if none. 342 * 343 * @param object $resource Resource table row (only property 'displayoptions' is used here) 344 * @param object $cm Course-module table row 345 * @return string Size and type or empty string if show options are not enabled 346 */ 347 function resource_get_optional_details($resource, $cm) { 348 global $DB; 349 350 $details = ''; 351 352 $options = empty($resource->displayoptions) ? [] : (array) unserialize_array($resource->displayoptions); 353 if (!empty($options['showsize']) || !empty($options['showtype']) || !empty($options['showdate'])) { 354 if (!array_key_exists('filedetails', $options)) { 355 $filedetails = resource_get_file_details($resource, $cm); 356 } else { 357 $filedetails = $options['filedetails']; 358 } 359 $size = ''; 360 $type = ''; 361 $date = ''; 362 $langstring = ''; 363 $infodisplayed = 0; 364 if (!empty($options['showsize'])) { 365 if (!empty($filedetails['size'])) { 366 $size = display_size($filedetails['size']); 367 $langstring .= 'size'; 368 $infodisplayed += 1; 369 } 370 } 371 if (!empty($options['showtype'])) { 372 if (!empty($filedetails['type'])) { 373 $type = $filedetails['type']; 374 $langstring .= 'type'; 375 $infodisplayed += 1; 376 } 377 } 378 if (!empty($options['showdate']) && (!empty($filedetails['modifieddate']) || !empty($filedetails['uploadeddate']))) { 379 if (!empty($filedetails['modifieddate'])) { 380 $date = get_string('modifieddate', 'mod_resource', userdate($filedetails['modifieddate'], 381 get_string('strftimedatetimeshort', 'langconfig'))); 382 } else if (!empty($filedetails['uploadeddate'])) { 383 $date = get_string('uploadeddate', 'mod_resource', userdate($filedetails['uploadeddate'], 384 get_string('strftimedatetimeshort', 'langconfig'))); 385 } 386 $langstring .= 'date'; 387 $infodisplayed += 1; 388 } 389 390 if ($infodisplayed > 1) { 391 $details = get_string("resourcedetails_{$langstring}", 'resource', 392 (object)array('size' => $size, 'type' => $type, 'date' => $date)); 393 } else { 394 // Only one of size, type and date is set, so just append. 395 $details = $size . $type . $date; 396 } 397 } 398 399 return $details; 400 } 401 402 /** 403 * Print resource introduction. 404 * @param object $resource 405 * @param object $cm 406 * @param object $course 407 * @param bool $ignoresettings print even if not specified in modedit 408 * @return void 409 */ 410 function resource_print_intro($resource, $cm, $course, $ignoresettings=false) { 411 global $OUTPUT; 412 413 $options = empty($resource->displayoptions) ? [] : (array) unserialize_array($resource->displayoptions); 414 415 $extraintro = resource_get_optional_details($resource, $cm); 416 if ($extraintro) { 417 // Put a paragaph tag around the details 418 $extraintro = html_writer::tag('p', $extraintro, array('class' => 'resourcedetails')); 419 } 420 421 if ($ignoresettings || !empty($options['printintro']) || $extraintro) { 422 $gotintro = trim(strip_tags($resource->intro)); 423 if ($gotintro || $extraintro) { 424 echo $OUTPUT->box_start('mod_introbox', 'resourceintro'); 425 if ($gotintro) { 426 echo format_module_intro('resource', $resource, $cm->id); 427 } 428 echo $extraintro; 429 echo $OUTPUT->box_end(); 430 } 431 } 432 } 433 434 /** 435 * Print warning that instance not migrated yet. 436 * @param object $resource 437 * @param object $cm 438 * @param object $course 439 * @return void, does not return 440 */ 441 function resource_print_tobemigrated($resource, $cm, $course) { 442 global $DB, $OUTPUT; 443 444 $resource_old = $DB->get_record('resource_old', array('oldid'=>$resource->id)); 445 resource_print_header($resource, $cm, $course); 446 resource_print_heading($resource, $cm, $course); 447 resource_print_intro($resource, $cm, $course); 448 echo $OUTPUT->notification(get_string('notmigrated', 'resource', $resource_old->type)); 449 echo $OUTPUT->footer(); 450 die; 451 } 452 453 /** 454 * Print warning that file can not be found. 455 * @param object $resource 456 * @param object $cm 457 * @param object $course 458 * @return void, does not return 459 */ 460 function resource_print_filenotfound($resource, $cm, $course) { 461 global $DB, $OUTPUT; 462 463 $resource_old = $DB->get_record('resource_old', array('oldid'=>$resource->id)); 464 resource_print_header($resource, $cm, $course); 465 resource_print_heading($resource, $cm, $course); 466 resource_print_intro($resource, $cm, $course); 467 if ($resource_old) { 468 echo $OUTPUT->notification(get_string('notmigrated', 'resource', $resource_old->type)); 469 } else { 470 echo $OUTPUT->notification(get_string('filenotfound', 'resource')); 471 } 472 echo $OUTPUT->footer(); 473 die; 474 } 475 476 /** 477 * Decide the best display format. 478 * @param object $resource 479 * @return int display type constant 480 */ 481 function resource_get_final_display_type($resource) { 482 global $CFG, $PAGE; 483 484 if ($resource->display != RESOURCELIB_DISPLAY_AUTO) { 485 return $resource->display; 486 } 487 488 if (empty($resource->mainfile)) { 489 return RESOURCELIB_DISPLAY_DOWNLOAD; 490 } else { 491 $mimetype = mimeinfo('type', $resource->mainfile); 492 } 493 494 if (file_mimetype_in_typegroup($mimetype, 'archive')) { 495 return RESOURCELIB_DISPLAY_DOWNLOAD; 496 } 497 if (file_mimetype_in_typegroup($mimetype, array('web_image', '.htm', 'web_video', 'web_audio'))) { 498 return RESOURCELIB_DISPLAY_EMBED; 499 } 500 501 // let the browser deal with it somehow 502 return RESOURCELIB_DISPLAY_OPEN; 503 } 504 505 /** 506 * File browsing support class 507 */ 508 class resource_content_file_info extends file_info_stored { 509 public function get_parent() { 510 if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') { 511 return $this->browser->get_file_info($this->context); 512 } 513 return parent::get_parent(); 514 } 515 public function get_visible_name() { 516 if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') { 517 return $this->topvisiblename; 518 } 519 return parent::get_visible_name(); 520 } 521 } 522 523 function resource_set_mainfile($data) { 524 global $DB; 525 $fs = get_file_storage(); 526 $cmid = $data->coursemodule; 527 $draftitemid = $data->files; 528 529 $context = context_module::instance($cmid); 530 if ($draftitemid) { 531 $options = array('subdirs' => true, 'embed' => false); 532 if ($data->display == RESOURCELIB_DISPLAY_EMBED) { 533 $options['embed'] = true; 534 } 535 file_save_draft_area_files($draftitemid, $context->id, 'mod_resource', 'content', 0, $options); 536 } 537 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder', false); 538 if (count($files) == 1) { 539 // only one file attached, set it as main file automatically 540 $file = reset($files); 541 file_set_sortorder($context->id, 'mod_resource', 'content', 0, $file->get_filepath(), $file->get_filename(), 1); 542 } 543 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body