lib.php 11.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-text
 * @author     Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
 *
 */

defined('INTERNAL') || die();

class PluginBlocktypeText extends SystemBlocktype {

    public static function get_title() {
        return get_string('title', 'blocktype.text');
    }

    public static function get_description() {
        return get_string('description', 'blocktype.text');
    }

    public static function get_categories() {
25
        return array('shortcut' => 1000);
26
27
    }

28
29
30
31
    public static function get_artefacts(BlockInstance $instance) {
        safe_require('artefact', 'file');
        $configdata = $instance->get('configdata');
        // Add all artefacts found in the text
32
33
34
35
36
37
        if (empty($configdata['text'])) {
            $artefacts = array();
        }
        else {
            $artefacts = array_unique(artefact_get_references_in_html($configdata['text']));
        }
38
39
40
        return $artefacts;
    }

41
    public static function render_instance(BlockInstance $instance, $editing=false) {
42
        safe_require('artefact', 'file');
43
44
45
        $configdata = $instance->get('configdata');
        $smarty = smarty_core();
        if (array_key_exists('text', $configdata)) {
46
47
            $newtext = ArtefactTypeFolder::append_view_url($configdata['text'], $instance->get('view'));
            $smarty->assign('text', $newtext);
48
49
50
51
52
53
54
55
56
57
58
        }
        else {
            $smarty->assign('text', '');
        }
        return $smarty->fetch('blocktype:text:content.tpl');
    }

    public static function has_instance_config() {
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
59
    public static function instance_config_form(BlockInstance $instance) {
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        require_once('license.php');
        $configdata = $instance->get('configdata');
        if (!$height = get_config('blockeditorheight')) {
            $cfheight = param_integer('cfheight', 0);
            $height = $cfheight ? $cfheight * 0.7 : 150;
        }

        $view = $instance->get_view();
        $text = '';
        if (array_key_exists('text', $configdata)) {
            $text = $configdata['text'];
        }

        $elements = array (
            'text' => array (
                'type' => 'wysiwyg',
                'title' => get_string('blockcontent', 'blocktype.text'),
                'width' => '100%',
                'height' => $height . 'px',
                'defaultvalue' => $text,
                'rules' => array('maxlength' => 65536),
            ),
        );
        return $elements;
    }

86
87
88
89
90
91
92
    public static function instance_config_save($values, $instance) {
        require_once('embeddedimage.php');
        $newtext = EmbeddedImage::prepare_embedded_images($values['text'], 'text', $instance->get('id'));
        $values['text'] = $newtext;
        return $values;
    }

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    public static function default_copy_type() {
        return 'full';
    }

    /**
     *
     * @param array $biconfig   The block instance config
     * @param array $viewconfig The view config
     * @return BlockInstance    The newly made block instance
     */
    public static function import_create_blockinstance_leap(array $biconfig, array $viewconfig) {
        $configdata = $biconfig['config'];

        $bi = new BlockInstance(0,
            array(
                'blocktype'  => $biconfig['type'],
                'configdata' => $configdata,
            )
        );

        return $bi;
    }

116
117
118
119
120
121
    /**
     * Rewrite extra config data for a Text blockinstance
     *
     *      See more PluginBlocktype::import_rewrite_blockinstance_extra_config_leap()
     */
    public static function import_rewrite_blockinstance_extra_config_leap(array $artefactids, array $configdata) {
122
123
124
125
        // Rewrite embedded image urls in the configdata['text']
        require_once('embeddedimage.php');
        $configdata['text'] = EmbeddedImage::rewrite_embedded_image_urls_from_import($configdata['text'], $artefactids);

126
127
128
        return $configdata;
    }

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    /**
     * Set the text property of the block config so that exports can be imported
     * into older versions.
     *
     * @param BlockInstance $bi The blockinstance to export the config for.
     * @return array The config for the blockinstance
     */
    public static function export_blockinstance_config_leap(BlockInstance $bi) {
        $configdata = $bi->get('configdata');
        $result = array('text' => json_encode(array($configdata['text'])));
        return $result;
    }

    public static function has_config() {
        return true;
    }

    public static function get_config_options() {
147
148
149
150
151
152
153
        $blocks = self::find_convertible_text_blocks();
        if (is_object($blocks)) {
            $convertibleblocksnumber = $blocks->NumRows();
        }
        else {
            $convertibleblocksnumber = 0;
        }
154
155
156
157
158
159
160
        return array(
            'elements' => array(
                'convertdescription' => array(
                    'value' => get_string('convertdescriptionfeatures', 'blocktype.text') . ' ' . get_string('convertdescription', 'blocktype.text',
                            $convertibleblocksnumber),
                ),
                'convertcheckbox' => array(
161
                    'type' => 'switchbox',
162
163
                    'defaultvalue' => false,
                    'title' => get_string('optionlegend', 'blocktype.text'),
164
                    'description' => get_string('switchdescription', 'blocktype.text'),
165
166
167
168
169
170
                ),
            )
        );
    }

    /**
171
172
173
174
     * Retrieves the text boxes that may be converted from Note to Textbox
     *
     * @param integer $limit Limit the number of records processed to this many.
     * @return ADORecordSet
175
     */
176
177
    private static function find_convertible_text_blocks($limit = null) {
        raise_memory_limit("512M");
178
179
180
181
        // find all note(textbox)-blocks that link to a note-artefact which is
        // not linked to any other note(textbox)-block, i.e. all textbox-blocks
        // whose artefact is used only once
        $query = "
182
            SELECT bi.id, bi.configdata, va.artefact, a.artefacttype
183
184
            FROM {block_instance} AS bi
            INNER JOIN {view_artefact} AS va ON va.block = bi.id AND bi.blocktype = 'textbox'
185
            INNER JOIN {artefact} AS a ON a.id = va.artefact
186
187
188
            LEFT JOIN {view_artefact} AS dummy ON va.artefact = dummy.artefact AND va.block != dummy.block
            LEFT JOIN {artefact_comment_comment} AS comment ON va.artefact = comment.onartefact
            WHERE dummy.block IS NULL AND comment.artefact IS NULL";
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
        if ($limit) {
            $query .= " LIMIT {$limit}";
        }

        return get_recordset_sql($query, array());
    }

    /**
     * Pieform success callback function for the config form. Converts the
     * text blocks, if the checkbox is ticked
     *
     * @param $form the pieform to send the ok-message to
     * @param array $values
     */
    public static function save_config_options($form, $values) {
        global $SESSION;
        if (!array_key_exists('convertcheckbox', $values) || !$values['convertcheckbox']) {
            return;
        }
        $countconverted = self::convert_notes_to_text_blocks();
        $form->json_reply(PIEFORM_OK, get_string('convertibleokmessage', 'blocktype.text', $countconverted));
    }


    /**
     * This function is meant to be run (either via the "convertnotes.php" CLI script,
     * or from the blocktype/text plugin config page) shortly after upgrading to
     * Mahara 1.10. It will locate all the existing Note blocks & their underlying Note
     * artefacts, and convert them into simple Text blocks if they are not using
     * any of the Note artefact's advanced features.
     *
     * @param integer $limit Limit the number of records processed to this many.
     * @return integer The number of notes converted
     */
    public static function convert_notes_to_text_blocks($limit = null) {
224

225
226
227
228
        $rs = self::find_convertible_text_blocks($limit);
        if (!$rs) {
            log_info("No old-style Text Box blocks to process.");
            return 0;
229
        }
230
231
232
233
234
235
236
237
238
239
240
241
242
243

        $total = $rs->NumRows();
        $countprocessed = 0;
        $countconverted = 0;

        log_info("Preparing to process {$total} old-style Text Box blocks.");
        while ($record = $rs->FetchRow()) {

            $countprocessed++;
            if ($countprocessed % 1000 == 0) {
                log_info("{$countprocessed}/{$total} processed...");
            }

            $record = (object)$record;
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
            $oldconfigdata = unserialize($record->configdata);
            // don't convert textboxes with tags, because the text doesn't support tags
            if (array_key_exists('tags', $oldconfigdata) && count($oldconfigdata['tags']) > 0) {
                continue;
            }
            // don't convert textboxes with a license, because the text
            // doesn't support licenses
            if (array_key_exists('license', $oldconfigdata) && strlen($oldconfigdata['license']) > 0) {
                continue;
            }
            // don't convert textboxes with connected artefacts, because the text
            // doesn't support additional artefacts
            if (array_key_exists('artefactids', $oldconfigdata) && count($oldconfigdata['artefactids']) > 0) {
                continue;
            }
259
260
261
262
263
264
265
            // ignore if the artefacttype returned is not 'html' - seems to exist if a text box has a download link in the markup
            if ($record->artefacttype != 'html') {
                continue;
            }

            db_begin();
            $record = (object)array(
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
                'id' => $record->id,
                'configdata' => $oldconfigdata,
                'artefact' => $record->artefact,
            );

            $htmlartefact = new ArtefactTypeHtml($record->artefact);
            $newconfigdata = array(
                'text' => $htmlartefact->get('description'),
                'retractable' => false,
                'retractedonload' => false,
            );
            if (array_key_exists('retractable', $record->configdata)) {
                $newconfigdata['retractable'] = $record->configdata['retractable'];
            }
            if (array_key_exists('retractedonload', $record->configdata)) {
                $newconfigdata['retractedonload'] = $record->configdata['retractedonload'];
            }
            $whereobj = (object)(array('id' => $record->id,));
            $newobj = (object)(array(
                'blocktype' => 'text',
                'configdata' => serialize($newconfigdata),
            ));
            update_record('block_instance', $newobj, $whereobj);
            $htmlartefact->delete();
290
291
            $countconverted++;
            db_commit();
292
        }
293
        return $countconverted;
294
    }
295
296
297
298
299
300
301
302
303
304
305

    /**
     * Rewrites embedded image urls in the $configdata['text']
     *
     * See more in PluginBlocktype::rewrite_blockinstance_extra_config()
     */
    public static function rewrite_blockinstance_extra_config(View $view, BlockInstance $block, $configdata, $artefactcopies) {
        $regexp = array();
        $replacetext = array();
        foreach ($artefactcopies as $copyobj) {
            // Change the old image id to the new one
306
307
308
309
310
311
312
313
314
315
            $regexp[] = '#<img([^>]+)src=("|\\")'
                    . preg_quote(
                        get_config('wwwroot')
                        . 'artefact/file/download.php?file=' . $copyobj->oldid
                    )
                    . '(&|&amp;)embedded=1([^"]*)"#';
            $replacetext[] = '<img$1src="'
                    . get_config('wwwroot')
                    . 'artefact/file/download.php?file=' . $copyobj->newid
                    . '&embedded=1"';
316
317
318
319
        }
        $configdata['text'] = preg_replace($regexp, $replacetext, $configdata['text']);
        return $configdata;
    }
320
}