From 1fad6ff3f5a411ff4591f9c2c85da005274a4b1b Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Fri, 2 Jun 2017 11:17:32 +0800 Subject: [PATCH] MDL-59008 mod_resource: add option to serve external files embed --- lib/filelib.php | 9 ++++++--- mod/resource/locallib.php | 11 +++++++++-- pluginfile.php | 4 ++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/filelib.php b/lib/filelib.php index 28faa7eaae8..20112a00a5f 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -972,7 +972,9 @@ function file_save_draft_area_files($draftitemid, $contextid, $component, $filea if (!empty($repoid)) { $context = context::instance_by_id($contextid, MUST_EXIST); $repo = repository::get_repository_by_id($repoid, $context); - + if (!empty($options)) { + $repo->options = $options; + } $file_record['repositoryid'] = $repoid; // This hook gives the repo a place to do some house cleaning, and update the $reference before it's saved // to the file store. E.g. transfer ownership of the file to a system account etc. @@ -3886,9 +3888,10 @@ class curl_cache { * @param null|string $preview the preview mode, defaults to serving the original file * @param boolean $offline If offline is requested - don't serve a redirect to an external file, return a file suitable for viewing * offline (e.g. mobile app). + * @param bool $embed Whether this file will be served embed into an iframe. * @todo MDL-31088 file serving improments */ -function file_pluginfile($relativepath, $forcedownload, $preview = null, $offline = false) { +function file_pluginfile($relativepath, $forcedownload, $preview = null, $offline = false, $embed = false) { global $DB, $CFG, $USER; // relative path must start with '/' if (!$relativepath) { @@ -3912,7 +3915,7 @@ function file_pluginfile($relativepath, $forcedownload, $preview = null, $offlin $fs = get_file_storage(); - $sendfileoptions = ['preview' => $preview, 'offline' => $offline]; + $sendfileoptions = ['preview' => $preview, 'offline' => $offline, 'embed' => $embed]; // ======================================================================================================================== if ($component === 'blog') { diff --git a/mod/resource/locallib.php b/mod/resource/locallib.php index 0dd1f1fe2a3..7629859ea4f 100644 --- a/mod/resource/locallib.php +++ b/mod/resource/locallib.php @@ -93,8 +93,11 @@ function resource_display_embed($resource, $cm, $course, $file) { $code = $mediamanager->embed_url($moodleurl, $title, 0, 0, $embedoptions); } else { + // We need a way to discover if we are loading remote docs inside an iframe. + $moodleurl->param('embed', 1); + // anything else - just try object tag enlarged as much as possible - $code = resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype); + $code = resourcelib_embed_general($moodleurl, $title, $clicktoopen, $mimetype); } resource_print_header($resource, $cm, $course); @@ -525,7 +528,11 @@ function resource_set_mainfile($data) { $context = context_module::instance($cmid); if ($draftitemid) { - file_save_draft_area_files($draftitemid, $context->id, 'mod_resource', 'content', 0, array('subdirs'=>true)); + $options = array('subdirs' => true, 'embed' => false); + if ($data->display == RESOURCELIB_DISPLAY_EMBED) { + $options['embed'] = true; + } + file_save_draft_area_files($draftitemid, $context->id, 'mod_resource', 'content', 0, $options); } $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder', false); if (count($files) == 1) { diff --git a/pluginfile.php b/pluginfile.php index 9148b624447..3d6d542496e 100644 --- a/pluginfile.php +++ b/pluginfile.php @@ -36,5 +36,5 @@ $preview = optional_param('preview', null, PARAM_ALPHANUM); // Offline means download the file from the repository and serve it, even if it was an external link. // The repository may have to export the file to an offline format. $offline = optional_param('offline', 0, PARAM_BOOL); - -file_pluginfile($relativepath, $forcedownload, $preview, $offline); +$embed = optional_param('embed', 0, PARAM_BOOL); +file_pluginfile($relativepath, $forcedownload, $preview, $offline, $embed);