lib.php 19.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-annotation
 * @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();

14
class PluginBlocktypeAnnotation extends MaharaCoreBlocktype {
15
16
17
18
    public static function single_only() {
        return false;
    }

Lisa Seeto's avatar
Lisa Seeto committed
19
20
21
22
    public static function single_artefact_per_block() {
        return false;
    }

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    public static function get_title() {
        return get_string('title', 'blocktype.annotation/annotation');
    }

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

    public static function get_categories() {
        return array('general' => 14500);
    }

    public static function get_viewtypes() {
        return array('portfolio');
    }

    public static function has_title_link() {
40
        return false;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    }

    public static function allowed_in_view(View $view) {
        // Annotations don't make sense in groups?
        return $view->get('group') == null;
    }

    /**
     * defines if the title should be shown if there is no content in the block
     *
     * If the title of the block should be hidden when there is no content,
     * override the the function in the blocktype class.
     *
     * @return boolean  whether the title of the block should be shown or not
     */
    public static function hide_title_on_empty_content() {
        return true;
    }

    /**
     * Returns a list of artefact IDs that are in this blockinstance.
     *
     * People may embed artefacts as images etc. They show up as links to the
     * download script, which isn't much to go on, but should be enough for us
     * to detect that the artefacts are therefore 'in' this blocktype.
     */
    public static function get_artefacts(BlockInstance $instance) {
        $configdata = $instance->get('configdata');
        $artefacts = array();
        if (isset($configdata['artefactid'])) {
            $artefacts[] = $configdata['artefactid'];

            // Add all artefacts found in the text
            $text = $instance->get_artefact_instance($configdata['artefactid'])->get('description');
            $artefacts = array_unique(array_merge($artefacts, artefact_get_references_in_html($text)));

            // Get all the feedback on this annotation
            // to retrieve all the artefacts found in their text
            // and to include the feedback as part of the view_artefact.
            // Please note that images owned by other users that are place on feedback
            // will not be part of the view_artefact because the owner of the
            // annotation does not own the image being placed on the feedback.
            // Therefore, when exported as Leap2A, these images will not come through.
            $sql = "SELECT a.id, a.description
                    FROM {artefact} a
                    INNER JOIN {artefact_annotation_feedback} af ON a.id = af.artefact
                    WHERE af.onannotation = ?";
            // Keep a list of the feedback ids.
            $artefactfeedback = array();
            if ($feedback = get_records_sql_array($sql, array($configdata['artefactid']))) {
                foreach ($feedback as $f) {
                    // Include the feedback artefact.
                    $artefactfeedback[] = $f->id;
                    // Include any artefacts found in its text.
                    // The BlockInstance::rebuild_artefact_list() will sort out the ownership.
                    $artefacts = array_unique(array_merge($artefacts, artefact_get_references_in_html($f->description)));
                }
                // Now merge the feedback artefacts as well.
                $artefacts = array_unique(array_merge($artefacts, $artefactfeedback));
            }
        }
        return $artefacts;
    }

    /**
     * Indicates whether this block can be loaded by Ajax after the page is done. This
     * improves page-load times by allowing blocks to be rendered in parallel instead
     * of in serial.
     *
     * You might want to disable this for:
     * - Blocks with particularly finicky Javascript contents
     * - Blocks that need to write to the session (the Ajax loader uses the session in read-only)
     * - Blocks that won't take long to render (static content, external content)
     *
     * @return boolean
     */
    public static function should_ajaxify() {
        // No, don't ajaxify this block. TinyMCE has issues.
        return false;
    }

122
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
        $smarty = smarty_core();
        $artefactid = '';
        $text = '';
        $feedbackcount = 0;
        $instance->set('artefactplugin', 'annotation');

        $configdata = $instance->get('configdata');
        if (!empty($configdata['artefactid'])) {
            safe_require('artefact', 'file');
            $artefactid = $configdata['artefactid'];
            $artefact = $instance->get_artefact_instance($artefactid);
            $viewid = $instance->get('view');
            $text = $artefact->get('description');
            require_once(get_config('docroot') . 'lib/view.php');
            $view = new View($viewid);
138
            list($feedbackcount, $annotationfeedback) = ArtefactTypeAnnotationfeedback::get_annotation_feedback_for_view($artefact, $view, $instance->get('id'), true, $editing, $versioning);
139
140
            $smarty->assign('annotationfeedback', $annotationfeedback);
        }
141
142
143
        if ($versioning) {
            $text = $configdata['text'];
        }
144
145
146
147
148
149
150
151
        $smarty->assign('text', $text);
        $smarty->assign('artefactid', $artefactid);
        $smarty->assign('annotationfeedbackcount', $feedbackcount);
        $html = $smarty->fetch('blocktype:annotation:annotation.tpl');

        return $html;
    }

152
153
154
155
156
157
158
159
160
161
162
    /**
     * Once created annotations should not be editable.
     *
     * @param BlockInstance $instance
     *   With this being a static we need to pass this in.
     *
     * @return boolean
     *   Return true/false based on if this is a new block.
     */
    public static function has_instance_config(BlockInstance $instance) {
        return $instance->is_new();
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    }

    public static function instance_config_form(BlockInstance $instance) {
        global $USER;

        $instance->set('artefactplugin', 'annotation');
        // Get the saved configs in the artefact
        $configdata = $instance->get('configdata');

        if (!$height = get_config('blockeditorheight')) {
            $cfheight = param_integer('cfheight', 0);
            $height = $cfheight ? $cfheight * 0.7 : 150;
        }

        // Default annotation text.
        $text = '';
        $tags = '';
        $artefactid = '';
        $readonly = false;
        $textreadonly = false;
        $view = $instance->get_view();

        if (!empty($configdata['artefactid'])) {
            $artefactid = $configdata['artefactid'];
            try {
                $artefact = $instance->get_artefact_instance($artefactid);
                // Get the annotation record -> to get the artefact it's linked to.
                $annotation = new ArtefactTypeAnnotation($artefactid);
                // Get the total annotation feedback inserted so far by anyone.
                $totalannotationfeedback = ArtefactTypeAnnotationfeedback::count_annotation_feedback($artefactid, array($view->get('id')), array($annotation->get('artefact')));

                $readonly = $artefact->get('owner') !== $view->get('owner')
                    || $artefact->get('group') !== $view->get('group')
                    || $artefact->get('institution') !== $view->get('institution')
                    || $artefact->get('locked')
                    || !$USER->can_edit_artefact($artefact);

                if (isset($totalannotationfeedback[$view->get('id')])) {
                    $textreadonly = $totalannotationfeedback[$view->get('id')]->total > 0;
                }

204
                $text = clean_html($artefact->get('description'));
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
                $tags = $artefact->get('tags');
            }
            catch (ArtefactNotFoundException $e) {
                unset($artefactid);
            }
        }

        $elements = array(
            'text' => array(
                'type' => ($textreadonly ? 'html' : 'wysiwyg'),
                'class' => '',
                'title' => get_string('Annotation', 'artefact.annotation'),
                'width' => '100%',
                'height' => $height . 'px',
                'defaultvalue' => $text,
220
                'rules' => array(
221
                    'maxlength' => 1000000,
222
223
                    'required' => true
                ),
224
225
226
            ),
            'annotationreadonlymsg' => array(
                'type' => 'html',
227
                'class' => 'message info' . ($textreadonly ? '' : ' d-none'),
228
229
230
231
                'value' => get_string('annotationreadonlymessage', 'blocktype.annotation/annotation'),
                'help' => true,
            ),
            'allowfeedback' => array(
232
                'type'         => 'switchbox',
233
234
235
236
237
                'title'        => get_string('allowannotationfeedback', 'artefact.annotation'),
                'defaultvalue' => (!empty($artefact) ? $artefact->get('allowcomments') : 1),
            ),
            'tags' => array(
                'type' => 'tags',
238
                'class' => $readonly ? 'd-none' : '',
239
240
241
242
243
244
245
                'width' => '100%',
                'title' => get_string('tags'),
                'description' => get_string('tagsdescprofile'),
                'defaultvalue' => $tags,
            ),
            'tagsreadonly' => array(
                'type' => 'html',
246
                'class' => $readonly ? '' : 'd-none',
247
248
249
250
251
252
253
254
255
256
                'width' => '100%',
                'title' => get_string('tags'),
                'value' => '<div id="instconf_tagsreadonly_display">' . (is_array($tags) ? hsc(join(', ', $tags)) : '') . '</div>',
            ),
        );

        if ($textreadonly) {
            // The annotation is displayed as html, need to populate its value.
            $elements['text']['value'] = $text;
        }
257
        $collection = $view->get('collection');
258
        if (is_object($collection) && $collection->has_framework()) {
259
260
261
            safe_require('module', 'framework');
            $framework = new Framework($collection->get('framework'));
            $standards = $framework->standards();
Robert Lyon's avatar
Robert Lyon committed
262
            $evidence = $framework->get_evidence($collection->get('id'), $instance->get('id'));
263
264
265
266
267
            $selectoptions = array();
            $selectdesciptions = array();
            foreach ($standards['standards'] as $standard) {
                if (isset($standard->options)) {
                    $selectoptions[$standard->id] = array(
Robert Lyon's avatar
Robert Lyon committed
268
                        'label' => $standard->name,
269
270
271
                        'options' => array(),
                    );
                    foreach ($standard->options as $option) {
272
273
274
275
276
277
278
279
                        // We are not allowed to change standard if either the assement has changed
                        // from initial state and/or there is feedback on the annotation
                        if ($evidence && $evidence->element && ((int) $evidence->state !== Framework::EVIDENCE_BEGUN || $textreadonly)) {
                            $selectoptions[$standard->id]['options'][$option->id] = array('value' => $option->name, 'disabled' => (!($evidence->element == $option->id)));
                        }
                        else {
                            $selectoptions[$standard->id]['options'][$option->id] = $option->name;
                        }
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
                        $selectdescriptions[$option->id] = $option->description;
                    }
                }
            }

            $elements['smartevidence'] = array(
                'type' => 'select',
                'title' => get_string('standard', 'module.framework'),
                'optgroups' => $selectoptions,
                'isSelect2' => true,
                'width' => '280px',
                'class' => 'last', // to remove base border
                'defaultvalue' => (($evidence) ? $evidence->element : null),
            );
            array_walk($selectdescriptions, function (&$a, $b) {
295
                $a = '<div class="d-none" id="option_' . $b . '">' . $a . '</div>';
296
297
298
299
300
301
302
303
304
305
306
307
            });
            $elements['smartevidencedesc'] = array(
                'type' => 'html',
                'class' => 'htmldescription',
                'value' => implode("\n", $selectdescriptions),
                'description' => get_string('standarddesc', 'module.framework'), // have desc for 'smartevidence' here so html falls between them
            );
            if (isset($instance->option) && !empty($instance->option)) {
                // Need to add a readonly SmartEvidence field
                $elements['smartevidence']['defaultvalue'] = $instance->option;
            }
        }
308
309
310
        return $elements;
    }

311
    public static function delete_instance(BlockInstance $instance) {
312
313
314
315
        // If annotation is evidence for SmartEvidence framework we need to delete that as well
        if (is_plugin_active('framework', 'module')) {
            delete_records('framework_evidence', 'annotation', $instance->get('id'));
        }
316
317
318
319
320
321
322
323
324
325
326
327
        $configdata = $instance->get('configdata');
        if (!empty($configdata)) {
            $artefactid = $configdata['artefactid'];
            if (!empty($artefactid) && $artefactid) {
                // Delete the annotation and all its feedback.
                safe_require('artefact', 'annotation');
                $annotation = new ArtefactTypeAnnotation($artefactid);
                $annotation->delete();
            }
        }
    }

328
329
330
331
    public static function instance_config_validate(Pieform $form, $values) {

        if (!empty($values['smartevidence'])) {
            // Check that the new smartevidence standard we are changing to is not alreay covered by another annotation block
332
333
            $block = $form->get_element_option('blockconfig', 'value');
            $viewid = $form->get_element_option('id', 'value');
334
            require_once('view.php');
335
            $view = new View($viewid);
336
337
338
339
340
341
            $collection = $view->get('collection');
            if (is_object($collection) && $collection->get('framework')) {
                $annotationid = get_field('framework_evidence', 'annotation',
                                           'view', $view->get('id'),
                                           'framework', $collection->get('framework'),
                                           'element', $values['smartevidence']);
342
                if ($annotationid && ($annotationid != $block)) {
343
344
345
346
347
348
349
350
                    $result['message'] = get_string('annotationclash', 'module.framework');
                    $form->set_error('smartevidence', $result['message']);
                    $form->reply(PIEFORM_ERR, $result);
                }
            }
        }
    }

351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
    public static function instance_config_save($values, $instance) {

        require_once('embeddedimage.php');
        safe_require('artefact', 'annotation');

        $data = array();
        $view = $instance->get_view();
        $configdata = $instance->get('configdata');
        foreach (array('owner', 'group', 'institution') as $f) {
            $data[$f] = $view->get($f);
        }

        // The title will always be Annotation.
        $title = get_string('Annotation', 'artefact.annotation');
        $data['title'] = $title;
366
        $values['title'] = !empty($values['title']) ? $values['title'] : $title;
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
        if (empty($configdata['artefactid'])) {
            // This is a new annotation.
            $artefact = new ArtefactTypeAnnotation(0, $data);
        }
        else {
            // The user is editing the annotation.
            $artefact = new ArtefactTypeAnnotation($configdata['artefactid']);
        }
        $artefact->set('title', $title);
        $artefact->set('description', $values['text']);
        $artefact->set('allowcomments', (!empty($values['allowfeedback']) ? $values['allowfeedback'] : 0));
        $artefact->set('tags', $values['tags']);
        $artefact->set('view', $view->get('id'));
        $artefact->commit();

        // Now fix up the text in case there were any embedded images.
        // Do this after saving because we may not have an artefactid yet.
        $newdescription = EmbeddedImage::prepare_embedded_images($values['text'], 'annotation', $artefact->get('id'), $view->get('group'));

        if ($newdescription !== false && $newdescription !== $values['text']) {
            $updatedartefact = new stdClass();
            $updatedartefact->id = $artefact->get('id');
            $updatedartefact->description = $newdescription;
            update_record('artefact', $updatedartefact, 'id');
        }

        $values['artefactid'] = $artefact->get('id');
        $instance->save_artefact_instance($artefact);

396
397
398
399
400
401
        if (is_plugin_active('framework', 'module') && !empty($values['smartevidence'])) {
            safe_require('module', 'framework');
            $title = get_field('framework_standard_element', 'shortname', 'id', $values['smartevidence']);
            $values['title'] = get_string('Annotation', 'artefact.annotation') . ': ' . $title;
            $result = Framework::save_evidence_in_block($instance->get('id'), $values['smartevidence']);
        }
402
403
404
        unset($values['text']);
        unset($values['allowfeedback']);
        unset($values['annotationreadonlymsg']);
405
406
        unset($values['smartevidence']);
        unset($values['smartevidencedesc']);
407
408
409
410
411
412
413
414
415
416
417
        // Pass back a list of any other blocks that need to be rendered
        // due to this change.
        $values['_redrawblocks'] = array_unique(get_column(
            'view_artefact', 'block',
            'artefact', $values['artefactid'],
            'view', $instance->get('view')
        ));

        return $values;
    }

418
419
420
421
422
    public static function default_copy_type(BlockInstance $instance, View $view) {
        if ($instance->get_view()->get('owner') == $view->get('owner')) {
            return 'fullinclself';
        }
        return 'nocopy';
423
424
    }

425
426
427
428
    /**
     * To stop original annotation getting copies of the feedback
     * when we copy a page
     */
429
430
    public static function ignore_copy_artefacttypes() {
        return array('annotationfeedback');
431
    }
432

433
434
435
436
437
438
439
440
441
442
443
    public static function has_feedback_allowed($id) {
        return (bool) get_field_sql("
            SELECT a.allowcomments FROM {artefact} a
            JOIN {view_artefact} va ON va.artefact = a.id
            JOIN {view} v ON v.id = va.view
            JOIN {block_instance} bi ON bi.id = va.block
            WHERE a.artefacttype = 'annotation'
            AND bi.blocktype = 'annotation'
            AND bi.id = ?", array($id));
    }

444
445
446
    public static function get_instance_javascript(BlockInstance $bi) {
        return array(
            array(
447
448
                'file' => 'js/annotation.js',
                'initjs' => " annotationBlockInit(); ",
449
450
451
452
453
454
455
456
457
            )
        );
    }

    public static function jsstrings() {
        return array(
            'mahara' => array('Close')
        );
    }
458
459
460
461
462
463

    public static function postinst($fromversion) {
        if ($fromversion == 0) {
            set_field('blocktype_installed', 'active', 0, 'artefactplugin', 'annotation');
        }
    }
464
}