From 7160fb19d74fed65ac3f4cd81321b0e31e3f0f48 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Wed, 6 Apr 2016 17:26:10 +0800 Subject: [PATCH] MDL-53700 competency: Migrating file serving to core --- admin/tool/lp/lib.php | 40 ---------------- admin/tool/lp/user_evidence_edit.php | 2 +- competency/classes/api.php | 6 +-- competency/lib.php | 68 ++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 44 deletions(-) create mode 100644 competency/lib.php diff --git a/admin/tool/lp/lib.php b/admin/tool/lp/lib.php index c40362925c0..23105619744 100644 --- a/admin/tool/lp/lib.php +++ b/admin/tool/lp/lib.php @@ -152,46 +152,6 @@ function tool_lp_extend_navigation_category_settings($navigation, $coursecategor } } - -/** - * File serving. - * - * @param stdClass $course The course object. - * @param stdClass $cm The cm object. - * @param context $context The context object. - * @param string $filearea The file area. - * @param array $args List of arguments. - * @param bool $forcedownload Whether or not to force the download of the file. - * @param array $options Array of options. - * @return void|false - */ -function tool_lp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) { - global $CFG; - - if (!\core_competency\api::is_enabled()) { - return false; - } - - $fs = get_file_storage(); - $file = null; - - $itemid = array_shift($args); - $filename = array_shift($args); - $filepath = $args ? '/' .implode('/', $args) . '/' : '/'; - - if ($filearea == 'userevidence' && $context->contextlevel == CONTEXT_USER) { - if (\core_competency\user_evidence::can_read_user($context->instanceid)) { - $file = $fs->get_file($context->id, 'tool_lp', $filearea, $itemid, $filepath, $filename); - } - } - - if (!$file) { - return false; - } - - send_stored_file($file, null, 0, $forcedownload); -} - /** * Hook when a comment is added. * diff --git a/admin/tool/lp/user_evidence_edit.php b/admin/tool/lp/user_evidence_edit.php index 33c8e10dbf0..e353514c4a7 100644 --- a/admin/tool/lp/user_evidence_edit.php +++ b/admin/tool/lp/user_evidence_edit.php @@ -89,7 +89,7 @@ if ($userevidence) { // Massaging the file API. $draftitemid = file_get_submitted_draft_itemid('files'); -file_prepare_draft_area($draftitemid, $context->id, 'tool_lp', 'userevidence', $itemid, $fileareaoptions); +file_prepare_draft_area($draftitemid, $context->id, 'core_competency', 'userevidence', $itemid, $fileareaoptions); $form->set_data((object) array('files' => $draftitemid)); // Hurray, the user has submitted the form! Everyone loves forms :)! diff --git a/competency/classes/api.php b/competency/classes/api.php index b86f3860eea..7ee87552581 100644 --- a/competency/classes/api.php +++ b/competency/classes/api.php @@ -3695,7 +3695,7 @@ class api { if (!empty($draftitemid)) { $fileareaoptions = array('subdirs' => true); $itemid = $userevidence->get_id(); - file_save_draft_area_files($draftitemid, $context->id, 'tool_lp', 'userevidence', $itemid, $fileareaoptions); + file_save_draft_area_files($draftitemid, $context->id, 'core_competency', 'userevidence', $itemid, $fileareaoptions); } // Trigger an evidence of prior learning created event. @@ -3729,7 +3729,7 @@ class api { if (!empty($draftitemid)) { $fileareaoptions = array('subdirs' => true); $itemid = $userevidence->get_id(); - file_save_draft_area_files($draftitemid, $context->id, 'tool_lp', 'userevidence', $itemid, $fileareaoptions); + file_save_draft_area_files($draftitemid, $context->id, 'core_competency', 'userevidence', $itemid, $fileareaoptions); } // Trigger an evidence of prior learning updated event. @@ -3758,7 +3758,7 @@ class api { // Delete associated files. $fs = get_file_storage(); - $fs->delete_area_files($context->id, 'tool_lp', 'userevidence', $id); + $fs->delete_area_files($context->id, 'core_competency', 'userevidence', $id); // Delete relation between evidence and competencies. $userevidence->set_id($id); // Restore the ID to fully mock the object. diff --git a/competency/lib.php b/competency/lib.php new file mode 100644 index 00000000000..5272d79cd49 --- /dev/null +++ b/competency/lib.php @@ -0,0 +1,68 @@ +. + +/** + * Competency lib. + * + * @package core_competency + * @copyright 2016 Frédéric Massart - FMCorz.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +use core_competency\api; +use core_competency\user_evidence; + + +/** + * File serving. + * + * @param stdClass $course The course object. + * @param stdClass $cm The cm object. + * @param context $context The context object. + * @param string $filearea The file area. + * @param array $args List of arguments. + * @param bool $forcedownload Whether or not to force the download of the file. + * @param array $options Array of options. + * @return void|false + */ +function core_competency_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) { + global $CFG; + + if (!api::is_enabled()) { + return false; + } + + $fs = get_file_storage(); + $file = null; + + $itemid = array_shift($args); + $filename = array_shift($args); + $filepath = $args ? '/' .implode('/', $args) . '/' : '/'; + + if ($filearea == 'userevidence' && $context->contextlevel == CONTEXT_USER) { + if (user_evidence::can_read_user($context->instanceid)) { + $file = $fs->get_file($context->id, 'core_competency', $filearea, $itemid, $filepath, $filename); + } + } + + if (!$file) { + return false; + } + + send_stored_file($file, null, 0, $forcedownload); +}