4 |
ariadna |
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 |
* Function's library
|
|
|
19 |
*
|
|
|
20 |
* @package block_point_view
|
|
|
21 |
* @copyright 2020 Quentin Fombaron
|
|
|
22 |
* @author Quentin Fombaron <q.fombaron@outlook.fr>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
use core\session\manager;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Callback checking permissions and preparing the file for serving plugin files.
|
|
|
30 |
*
|
|
|
31 |
* @param stdClass $course Course object
|
|
|
32 |
* @param stdClass $bi Block instance record
|
|
|
33 |
* @param context_course|context_system $context Context object
|
|
|
34 |
* @param string $filearea File area
|
|
|
35 |
* @param array $args Extra arguments
|
|
|
36 |
* @param bool $forcedownload Whether or not force download
|
|
|
37 |
* @param array $options Additional options affecting the file serving
|
|
|
38 |
*
|
|
|
39 |
* @return bool
|
|
|
40 |
*/
|
|
|
41 |
function block_point_view_pluginfile($course, $bi, $context, $filearea, $args, $forcedownload, array $options = []) {
|
|
|
42 |
global $CFG, $USER;
|
|
|
43 |
|
|
|
44 |
$fs = get_file_storage();
|
|
|
45 |
$filename = array_pop($args);
|
|
|
46 |
|
|
|
47 |
if ($filearea === 'content') {
|
|
|
48 |
if ($context->contextlevel != CONTEXT_BLOCK) {
|
|
|
49 |
send_file_not_found();
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
if ($context->get_course_context(false)) {
|
|
|
53 |
require_course_login($course);
|
|
|
54 |
} else if ($CFG->forcelogin) {
|
|
|
55 |
require_login();
|
|
|
56 |
} else {
|
|
|
57 |
$parentcontext = $context->get_parent_context();
|
|
|
58 |
if ($parentcontext->contextlevel === CONTEXT_COURSECAT) {
|
|
|
59 |
if (!core_course_category::get($parentcontext->instanceid, IGNORE_MISSING)) {
|
|
|
60 |
send_file_not_found();
|
|
|
61 |
}
|
|
|
62 |
} else if ($parentcontext->contextlevel === CONTEXT_USER && $parentcontext->instanceid != $USER->id) {
|
|
|
63 |
send_file_not_found();
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
$file = $fs->get_file(
|
|
|
68 |
$context->id,
|
|
|
69 |
'block_point_view',
|
|
|
70 |
$filearea,
|
|
|
71 |
0,
|
|
|
72 |
'/',
|
|
|
73 |
$filename
|
|
|
74 |
);
|
|
|
75 |
if (!$file || $file->is_directory()) {
|
|
|
76 |
send_file_not_found();
|
|
|
77 |
}
|
|
|
78 |
} else if (($filearea === 'point_views_pix') || ($filearea === 'point_views_pix_admin')) {
|
|
|
79 |
$file = $fs->get_file(
|
|
|
80 |
$context->id,
|
|
|
81 |
'block_point_view',
|
|
|
82 |
$filearea,
|
|
|
83 |
0,
|
|
|
84 |
'/',
|
|
|
85 |
$filename . '.png'
|
|
|
86 |
);
|
|
|
87 |
if (!$file || $file->is_directory()) {
|
|
|
88 |
send_file_not_found();
|
|
|
89 |
}
|
|
|
90 |
} else {
|
|
|
91 |
send_file_not_found();
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
manager::write_close();
|
|
|
95 |
send_stored_file($file, null, 0, true, $options);
|
|
|
96 |
|
|
|
97 |
return true;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Returns the list of Moodle features this block supports.
|
|
|
102 |
* @param string $feature FEATURE_xx constant.
|
|
|
103 |
* @return boolean|null Whether this block supports feature, null if unspecified.
|
|
|
104 |
*/
|
|
|
105 |
function block_point_view_supports($feature) {
|
|
|
106 |
switch($feature) {
|
|
|
107 |
case FEATURE_BACKUP_MOODLE2 :
|
|
|
108 |
return true;
|
|
|
109 |
default:
|
|
|
110 |
return null;
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Perform global search replace such as when migrating site to new URL.
|
|
|
116 |
*
|
|
|
117 |
* @param string $search
|
|
|
118 |
* @param string $replace
|
|
|
119 |
*
|
|
|
120 |
* @throws dml_exception
|
|
|
121 |
*/
|
|
|
122 |
function block_point_view_global_db_replace($search, $replace) {
|
|
|
123 |
global $DB;
|
|
|
124 |
|
|
|
125 |
$instances = $DB->get_recordset('block_instances', [ 'blockname' => 'point_view' ]);
|
|
|
126 |
foreach ($instances as $instance) {
|
|
|
127 |
$config = unserialize(base64_decode($instance->configdata));
|
|
|
128 |
if (isset($config->text) && is_string($config->text)) {
|
|
|
129 |
$config->text = str_replace($search, $replace, $config->text);
|
|
|
130 |
$DB->update_record('block_instances', ['id' => $instance->id,
|
|
|
131 |
'configdata' => base64_encode(serialize($config)), 'timemodified' => time()]);
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
$instances->close();
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* Given an array with a file path, it returns the itemid and the filepath for the defined filearea.
|
|
|
139 |
*
|
|
|
140 |
* @param string $filearea
|
|
|
141 |
* @param array $args
|
|
|
142 |
*
|
|
|
143 |
* @return array
|
|
|
144 |
*/
|
|
|
145 |
function block_point_view_get_path_from_pluginfile($filearea, $args) {
|
|
|
146 |
array_shift($args);
|
|
|
147 |
|
|
|
148 |
if (empty($args)) {
|
|
|
149 |
$filepath = '/';
|
|
|
150 |
} else {
|
|
|
151 |
$filepath = '/' . implode('/', $args) . '/';
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
return [
|
|
|
155 |
'itemid' => 0,
|
|
|
156 |
'filepath' => $filepath,
|
|
|
157 |
];
|
|
|
158 |
}
|