lib.php 19.5 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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
    public static function single_only() {
        return false;
    }

    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() {
        return false;  // true; // need to do more work on aretfact/artefact.php before this can be switched on.
    }

    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;
    }

118
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
        $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);
134
            list($feedbackcount, $annotationfeedback) = ArtefactTypeAnnotationfeedback::get_annotation_feedback_for_view($artefact, $view, $instance->get('id'), true, $editing, $versioning);
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
            $smarty->assign('annotationfeedback', $annotationfeedback);
        }
        $smarty->assign('text', $text);
        $smarty->assign('artefactid', $artefactid);
        $smarty->assign('annotationfeedbackcount', $feedbackcount);
        $html = $smarty->fetch('blocktype:annotation:annotation.tpl');

        return $html;
    }

    public static function has_instance_config() {
        return true;
    }

    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;
                }

188
                $text = clean_html($artefact->get('description'));
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
                $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,
204
205
206
207
                'rules' => array(
                    'maxlength' => 65536,
                    'required' => true
                ),
208
209
210
211
212
213
214
215
            ),
            'annotationreadonlymsg' => array(
                'type' => 'html',
                'class' => 'message info' . ($textreadonly ? '' : ' hidden'),
                'value' => get_string('annotationreadonlymessage', 'blocktype.annotation/annotation'),
                'help' => true,
            ),
            'allowfeedback' => array(
216
                'type'         => 'switchbox',
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
                'title'        => get_string('allowannotationfeedback', 'artefact.annotation'),
                'defaultvalue' => (!empty($artefact) ? $artefact->get('allowcomments') : 1),
            ),
            'tags' => array(
                'type' => 'tags',
                'class' => $readonly ? 'hidden' : '',
                'width' => '100%',
                'title' => get_string('tags'),
                'description' => get_string('tagsdescprofile'),
                'defaultvalue' => $tags,
            ),
            'tagsreadonly' => array(
                'type' => 'html',
                'class' => $readonly ? '' : 'hidden',
                '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;
        }
241
        $collection = $view->get('collection');
242
        if (is_object($collection) && $collection->has_framework()) {
243
244
245
            safe_require('module', 'framework');
            $framework = new Framework($collection->get('framework'));
            $standards = $framework->standards();
Robert Lyon's avatar
Robert Lyon committed
246
            $evidence = $framework->get_evidence($collection->get('id'), $instance->get('id'));
247
248
249
250
251
            $selectoptions = array();
            $selectdesciptions = array();
            foreach ($standards['standards'] as $standard) {
                if (isset($standard->options)) {
                    $selectoptions[$standard->id] = array(
Robert Lyon's avatar
Robert Lyon committed
252
                        'label' => $standard->name,
253
254
255
                        'options' => array(),
                    );
                    foreach ($standard->options as $option) {
256
257
258
259
260
261
262
263
                        // 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;
                        }
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
                        $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) {
                $a = '<div class="hidden" id="option_' . $b . '">' . $a . '</div>';
            });
            $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;
            }
        }
292
293
294
        return $elements;
    }

295
    public static function delete_instance(BlockInstance $instance) {
296
297
298
299
        // 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'));
        }
300
301
302
303
304
305
306
307
308
309
310
311
        $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();
            }
        }
    }

312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
    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
            $block = $form->get_element('blockconfig');
            $view = $form->get_element('id');
            require_once('view.php');
            $view = new View($view['value']);
            $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']);
                if ($annotationid && $annotationid != $block['value']) {
                    $result['message'] = get_string('annotationclash', 'module.framework');
                    $form->set_error('smartevidence', $result['message']);
                    $form->reply(PIEFORM_ERR, $result);
                }
            }
        }
    }

335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
    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;
350
        $values['title'] = !empty($values['title']) ? $values['title'] : $title;
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
        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);

380
381
382
383
384
385
        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']);
        }
386
387
388
        unset($values['text']);
        unset($values['allowfeedback']);
        unset($values['annotationreadonlymsg']);
389
390
        unset($values['smartevidence']);
        unset($values['smartevidencedesc']);
391
392
393
394
395
396
397
398
399
400
401
402
        // 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;
    }

    public static function default_copy_type() {
403
404
405
406
407
        return 'fullinclself';
    }

    public static function ignore_copy_artefacttypes() {
        return array('annotationfeedback');
408
    }
409

410
411
412
413
414
415
416
417
418
419
420
    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));
    }

421
422
423
    public static function get_instance_javascript(BlockInstance $bi) {
        return array(
            array(
424
                'file' => 'js/annotation.js'
425
426
427
428
429
430
431
432
433
            )
        );
    }

    public static function jsstrings() {
        return array(
            'mahara' => array('Close')
        );
    }
434
435
436
437
438
439

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

    public static function get_instance_config_javascript(BlockInstance $instance) {
        return <<<EOF
443
        jQuery(function($) {
444
445
446
447
            function show_se_desc(id) {
                $("#instconf_smartevidencedesc_container div:not(.description)").addClass('hidden');
                $("#option_" + id).removeClass('hidden');
            }
448
449
450
451
452
453
454
455
456
457
458
459
460
            if ($("#instconf_smartevidence").length) {
                // block title will be overwritten with framework choice so make it disabled
                $("#instconf_title").attr('disabled', true);

                // Set up evidence choices and show/hide related descriptions
                $("#instconf_smartevidence").select2();

                show_se_desc($("#instconf_smartevidence").val());
                $("#instconf_smartevidence").on('change', function() {
                    show_se_desc($(this).val());
                });
            }
        });
461
462
EOF;
    }
463
}