MDL-18011 - removing reference to loancalc block from core
[moodle.git] / blocks / html / block_html.php
CommitLineData
4ca6cfbf 1<?php
86db09ef 2
3class block_html extends block_base {
4
5 function init() {
8c5bba16 6 $this->title = get_string('pluginname', 'block_html');
433c242f 7 $this->version = 2007101509;
86db09ef 8 }
9
b959599b 10 function applicable_formats() {
11 return array('all' => true);
12 }
13
86db09ef 14 function specialization() {
f36acb35 15 $this->title = isset($this->config->title) ? format_string($this->config->title) : format_string(get_string('newhtmlblock', 'block_html'));
86db09ef 16 }
17
18 function instance_allow_multiple() {
19 return true;
20 }
21
22 function get_content() {
86db09ef 23 if ($this->content !== NULL) {
24 return $this->content;
25 }
26
3179b000 27 if ($this->content_is_trusted()) {
e92c286c 28 // fancy html allowed only on course, category and system blocks.
e8a7edd7 29 $filteropt = new stdClass;
30 $filteropt->noclean = true;
31 } else {
32 $filteropt = null;
33 }
e345909c 34
86db09ef 35 $this->content = new stdClass;
86db09ef 36 $this->content->footer = '';
94ee9ae0 37 if (isset($this->config->text)) {
41976db3
DC
38 // rewrite url
39 $this->config->text['text'] = file_rewrite_pluginfile_urls($this->config->text['text'], 'pluginfile.php', $this->context->id, 'block_html', $this->instance->id);
94ee9ae0
MD
40 $this->content->text = format_text($this->config->text['text'], $this->config->text['format'], $filteropt);
41 } else {
42 $this->content->text = '';
43 }
86db09ef 44
e345909c 45 unset($filteropt); // memory footprint
46
86db09ef 47 return $this->content;
48 }
43457dc8 49
41976db3
DC
50
51 /**
52 * Serialize and store config data
53 */
54 function instance_config_save($data, $nolongerused = false) {
55 global $DB;
56
57 // Move embedded files into a proper filearea and adjust HTML links to match
58 $data->text['text'] = file_save_draft_area_files($data->text['itemid'], $this->context->id, 'block_html', $this->instance->id, array('subdirs'=>true), $data->text['text']);
59
60 parent::instance_config_save($data, $nolongerused);
61 }
62
af140288
DC
63 function instance_delete() {
64 global $DB;
65 $fs = get_file_storage();
66 $fs->delete_area_files($this->context->id, 'block_html', $this->instance->id);
67 return true;
68 }
41976db3 69
3179b000 70 function content_is_trusted() {
71 return in_array($this->page->context->contextlevel, array(CONTEXT_COURSE, CONTEXT_COURSECAT, CONTEXT_SYSTEM));
72 }
73
5cfefc9b 74 /**
75 * Will be called before an instance of this block is backed up, so that any links in
76 * any links in any HTML fields on config can be encoded.
77 * @return string
78 */
79 function get_backup_encoded_config() {
83fc4004 80 /// Prevent clone for non configured block instance. Delegate to parent as fallback.
81 if (empty($this->config)) {
82 return parent::get_backup_encoded_config();
83 }
5cfefc9b 84 $data = clone($this->config);
85 $data->text = backup_encode_absolute_links($data->text);
86 return base64_encode(serialize($data));
43457dc8 87 }
88
5cfefc9b 89 /**
90 * This function makes all the necessary calls to {@link restore_decode_content_links_worker()}
4ca6cfbf
PS
91 * function in order to decode contents of this block from the backup
92 * format to destination site/course in order to mantain inter-activities
93 * working in the backup/restore process.
94 *
5cfefc9b 95 * This is called from {@link restore_decode_content_links()} function in the restore process.
96 *
97 * NOTE: There is no block instance when this method is called.
98 *
99 * @param object $restore Standard restore object
100 * @return boolean
101 **/
102 function decode_content_links_caller($restore) {
f28f2d90 103 global $CFG, $DB;
5cfefc9b 104
f28f2d90 105 if ($restored_blocks = $DB->get_records_select("backup_ids", "table_name = 'block_instance' AND backup_code = ? AND new_id > 0", array($restore->backup_unique_code), "", "new_id")) {
5cfefc9b 106 $restored_blocks = implode(',', array_keys($restored_blocks));
107 $sql = "SELECT bi.*
f28f2d90 108 FROM {block_instance} bi
109 JOIN {block} b ON b.id = bi.blockid
4ca6cfbf 110 WHERE b.name = 'html' AND bi.id IN ($restored_blocks)";
5cfefc9b 111
f28f2d90 112 if ($instances = $DB->get_records_sql($sql)) {
5cfefc9b 113 foreach ($instances as $instance) {
114 $blockobject = block_instance('html', $instance);
115 $blockobject->config->text = restore_decode_absolute_links($blockobject->config->text);
116 $blockobject->config->text = restore_decode_content_links_worker($blockobject->config->text, $restore);
e92c286c 117 $blockobject->instance_config_commit();
5cfefc9b 118 }
119 }
120 }
121
122 return true;
43457dc8 123 }
af140288 124
41976db3
DC
125 function send_file($context, $filearea, $itemid, $filepath, $filename) {
126 $fs = get_file_storage();
127 $fullpath = $context->id.'block_html'.$itemid.$filepath.$filename;
128
129 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
130 send_file_not_found();
131 }
132
133 session_get_instance()->write_close();
134 send_stored_file($file, 60*60, 0, false);
135 }
86db09ef 136}