MDL-68665 assignfeedback_editpdf: Improve caching on stamps
[moodle.git] / mod / assign / feedback / editpdf / lib.php
CommitLineData
5c386472
DW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This file contains the version information for the comments feedback plugin
19 *
20 * @package assignfeedback_editpdf
21 * @copyright 2012 Davo Smith
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Serves assignment feedback and other files.
29 *
30 * @param mixed $course course or id of the course
31 * @param mixed $cm course module or id of the course module
32 * @param context $context
33 * @param string $filearea
34 * @param array $args
35 * @param bool $forcedownload
c593a0b0 36 * @param array $options - List of options affecting file serving.
5c386472
DW
37 * @return bool false if file not found, does not return if found - just send the file
38 */
39function assignfeedback_editpdf_pluginfile($course,
40 $cm,
41 context $context,
42 $filearea,
43 $args,
c593a0b0
DW
44 $forcedownload,
45 array $options=array()) {
5c386472
DW
46 global $USER, $DB, $CFG;
47
166fa40a
DW
48 require_once($CFG->dirroot . '/mod/assign/locallib.php');
49
1343d84c
JS
50 if ($filearea === 'stamps' && $context->contextlevel === CONTEXT_SYSTEM) {
51
52 $itemid = (int)array_shift($args);
53
54 $relativepath = implode('/', $args);
55
56 $fullpath = "/{$context->id}/assignfeedback_editpdf/$filearea/$itemid/$relativepath";
57
58 $fs = get_file_storage();
59 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
60 return false;
61 }
62
63 $options['cacheability'] = 'public';
64 $options['immutable'] = true;
65
66 send_stored_file($file, 0, 0, true, $options);
67 } else if ($context->contextlevel == CONTEXT_MODULE) {
5c386472
DW
68
69 require_login($course, false, $cm);
70 $itemid = (int)array_shift($args);
71
166fa40a 72 $assign = new assign($context, $cm, $course);
5c386472 73
166fa40a 74 $record = $DB->get_record('assign_grades', array('id' => $itemid), 'userid,assignment', MUST_EXIST);
5c386472 75 $userid = $record->userid;
166fa40a 76 if ($assign->get_instance()->id != $record->assignment) {
5c386472
DW
77 return false;
78 }
79
166fa40a
DW
80 // Rely on mod_assign checking permissions.
81 if (!$assign->can_view_submission($userid)) {
5c386472
DW
82 return false;
83 }
84
85 $relativepath = implode('/', $args);
86
87 $fullpath = "/{$context->id}/assignfeedback_editpdf/$filearea/$itemid/$relativepath";
88
89 $fs = get_file_storage();
90 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
91 return false;
92 }
93 // Download MUST be forced - security!
c593a0b0 94 send_stored_file($file, 0, 0, true, $options);// Check if we want to retrieve the stamps.
5c386472
DW
95 }
96
97}