MDL-22054 converting block name strings to pluginname
[moodle.git] / blocks / html / block_html.php
1 <?php
3 class block_html extends block_base {
5     function init() {
6         $this->title = get_string('pluginname', 'block_html');
7         $this->version = 2007101509;
8     }
10     function applicable_formats() {
11         return array('all' => true);
12     }
14     function specialization() {
15         $this->title = isset($this->config->title) ? format_string($this->config->title) : format_string(get_string('newhtmlblock', 'block_html'));
16     }
18     function instance_allow_multiple() {
19         return true;
20     }
22     function get_content() {
23         if ($this->content !== NULL) {
24             return $this->content;
25         }
27         if ($this->content_is_trusted()) {
28             // fancy html allowed only on course, category and system blocks.
29             $filteropt = new stdClass;
30             $filteropt->noclean = true;
31         } else {
32             $filteropt = null;
33         }
35         $this->content = new stdClass;
36         $this->content->text = isset($this->config->text) ? format_text($this->config->text, FORMAT_HTML, $filteropt) : '';
37         $this->content->footer = '';
39         unset($filteropt); // memory footprint
41         return $this->content;
42     }
44     function content_is_trusted() {
45         return in_array($this->page->context->contextlevel, array(CONTEXT_COURSE, CONTEXT_COURSECAT, CONTEXT_SYSTEM));
46     }
48     /**
49      * Will be called before an instance of this block is backed up, so that any links in
50      * any links in any HTML fields on config can be encoded.
51      * @return string
52      */
53     function get_backup_encoded_config() {
54         /// Prevent clone for non configured block instance. Delegate to parent as fallback.
55         if (empty($this->config)) {
56             return parent::get_backup_encoded_config();
57         }
58         $data = clone($this->config);
59         $data->text = backup_encode_absolute_links($data->text);
60         return base64_encode(serialize($data));
61     }
63     /**
64      * This function makes all the necessary calls to {@link restore_decode_content_links_worker()}
65      * function in order to decode contents of this block from the backup
66      * format to destination site/course in order to mantain inter-activities
67      * working in the backup/restore process.
68      *
69      * This is called from {@link restore_decode_content_links()} function in the restore process.
70      *
71      * NOTE: There is no block instance when this method is called.
72      *
73      * @param object $restore Standard restore object
74      * @return boolean
75      **/
76     function decode_content_links_caller($restore) {
77         global $CFG, $DB;
79         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")) {
80             $restored_blocks = implode(',', array_keys($restored_blocks));
81             $sql = "SELECT bi.*
82                       FROM {block_instance} bi
83                            JOIN {block} b ON b.id = bi.blockid
84                      WHERE b.name = 'html' AND bi.id IN ($restored_blocks)";
86             if ($instances = $DB->get_records_sql($sql)) {
87                 foreach ($instances as $instance) {
88                     $blockobject = block_instance('html', $instance);
89                     $blockobject->config->text = restore_decode_absolute_links($blockobject->config->text);
90                     $blockobject->config->text = restore_decode_content_links_worker($blockobject->config->text, $restore);
91                     $blockobject->instance_config_commit();
92                 }
93             }
94         }
96         return true;
97     }
98 }