MDL-53700 competency: Migrating file serving to core

This commit is contained in:
Frederic Massart 2016-04-06 17:26:10 +08:00
parent c88b6c05ca
commit 7160fb19d7
4 changed files with 72 additions and 44 deletions

View File

@ -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.
*

View File

@ -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 :)!

View File

@ -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.

68
competency/lib.php Normal file
View File

@ -0,0 +1,68 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 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);
}