MDL-32471 preview thumbnails support for activity modules
[moodle.git] / blocks / html / lib.php
CommitLineData
64f93798
PS
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Form for editing HTML block instances.
20 *
64f93798
PS
21 * @copyright 2010 Petr Skoda (http://skodak.org)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d2b7803e
DC
23 * @package block_html
24 * @category files
25 * @param stdClass $course course object
26 * @param stdClass $birecord_or_cm block instance record
27 * @param stdClass $context context object
28 * @param string $filearea file area
29 * @param array $args extra arguments
30 * @param bool $forcedownload whether or not force download
31 * @return bool
64f93798 32 */
64f93798 33function block_html_pluginfile($course, $birecord_or_cm, $context, $filearea, $args, $forcedownload) {
d449b246 34 global $SCRIPT;
64f93798
PS
35
36 if ($context->contextlevel != CONTEXT_BLOCK) {
37 send_file_not_found();
38 }
39
40 require_course_login($course);
41
42 if ($filearea !== 'content') {
43 send_file_not_found();
44 }
45
46 $fs = get_file_storage();
47
48 $filename = array_pop($args);
d85ab705 49 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798
PS
50
51 if (!$file = $fs->get_file($context->id, 'block_html', 'content', 0, $filepath, $filename) or $file->is_directory()) {
52 send_file_not_found();
53 }
54
d449b246
PS
55 if ($parentcontext = get_context_instance_by_id($birecord_or_cm->parentcontextid)) {
56 if ($parentcontext->contextlevel == CONTEXT_USER) {
57 // force download on all personal pages including /my/
58 //because we do not have reliable way to find out from where this is used
59 $forcedownload = true;
60 }
61 } else {
62 // weird, there should be parent context, better force dowload then
63 $forcedownload = true;
64 }
65
64f93798
PS
66 session_get_instance()->write_close();
67 send_stored_file($file, 60*60, 0, $forcedownload);
68}
c3b5e829
PS
69
70/**
71 * Perform global search replace such as when migrating site to new URL.
72 * @param $search
73 * @param $replace
74 * @return void
75 */
76function block_html_global_db_replace($search, $replace) {
77 global $DB;
78
79 $instances = $DB->get_recordset('block_instances', array('blockname' => 'html'));
80 foreach ($instances as $instance) {
81 // TODO: intentionally hardcoded until MDL-26800 is fixed
82 $config = unserialize(base64_decode($instance->configdata));
83 if (isset($config->text) and is_string($config->text)) {
84 $config->text = str_replace($search, $replace, $config->text);
85 $DB->set_field('block_instances', 'configdata', base64_encode(serialize($config)), array('id' => $instance->id));
86 }
87 }
88 $instances->close();
d2b7803e 89}