lib.php 13.1 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
188
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?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();

class PluginBlocktypeAnnotation extends SystemBlocktype {
    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 override_instance_title() {
        return get_string('Annotation', 'artefact.annotation');
    }

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

    public static function render_instance(BlockInstance $instance, $editing=false) {
        $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);
            list($feedbackcount, $annotationfeedback) = ArtefactTypeAnnotationfeedback::get_annotation_feedback_for_view($artefact, $view, $instance->get('id'), true, $editing);
            $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;
                }

                $text = $artefact->get('description');
                $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,
                'rules' => array('maxlength' => 65536),
            ),
            'annotationreadonlymsg' => array(
                'type' => 'html',
                'class' => 'message info' . ($textreadonly ? '' : ' hidden'),
                'value' => get_string('annotationreadonlymessage', 'blocktype.annotation/annotation'),
                'help' => true,
            ),
            'allowfeedback' => array(
                'type'         => 'checkbox',
                '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;
        }
        return $elements;
    }

    public static function delete_instance($instance) {
        $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();
            }
        }
    }

    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;
        $values['title'] = $title;
        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);

        unset($values['text']);
        unset($values['allowfeedback']);
        unset($values['annotationreadonlymsg']);

        // 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() {
        return 'full';
    }
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335

    public static function get_instance_javascript(BlockInstance $bi) {
        return array(
            array(
                'file' => 'js/annotation.js',
                'initjs' => 'connectAddAnnotationFeedbackForms(); connectAnnotationFeedbackLinks();',
            )
        );
    }

    public static function jsstrings() {
        return array(
            'mahara' => array('Close')
        );
    }
336
}