lib.php 19.7 KB
Newer Older
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-profileinfo
6
 * @author     Catalyst IT Ltd
7
8
 * @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.
9
10
11
12
13
 *
 */

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

14
class PluginBlocktypeProfileinfo extends MaharaCoreBlocktype {
15
16
17
18
19
20
21
22
23
24

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

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

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

28
29
30
31
    public static function get_viewtypes() {
        return array('dashboard', 'portfolio', 'profile');
    }

32
33
34
35
    public static function get_css_icon_type($blockname) {
        return 'icon-regular';
    }

Son Nguyen's avatar
Son Nguyen committed
36
    public static function get_instance_config_javascript(BlockInstance $instance) {
37
38
39
        return array('js/configform.js');
    }

40
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
41
        safe_require('artefact', 'internal');
42
43
44
45
        $smarty = smarty_core();
        $configdata = $instance->get('configdata');

        $data = array();
46
        $data['internalprofiles'] = array();
47
        $data['socialprofiles'] = array();
48

49
        // add in the selected email address
50
        if (!empty($configdata['email']) && get_field('artefact', 'id', 'id', $configdata['email'])) {
51
52
53
            $configdata['artefactids'][] = $configdata['email'];
        }

54
        $viewowner = get_field('view', 'owner', 'id', $instance->get('view'));
55
        $sortorder = array_keys(ArtefactTypeProfile::get_all_fields());
56
57
        // Get data about the profile fields in this blockinstance
        if (!empty($configdata['artefactids'])) {
58
            foreach ($configdata['artefactids'] as $id) {
59
60
                try {
                    $artefact = artefact_instance_from_id($id);
61
                    if (is_a($artefact, 'ArtefactTypeProfile') && $artefact->get('owner') == $viewowner) {
62
                        $rendered = $artefact->render_self(array('link' => true));
63
64
65
66
67
68
69
70
71
72
73
74
75
76
                        $artefacttype = $artefact->get('artefacttype');
                        if ($artefacttype == 'socialprofile') {
                            if (get_record('blocktype_installed', 'active', 1, 'name', 'socialprofile', 'artefactplugin', 'internal')) {
                                $data['socialprofiles'][] = array(
                                    'link' => ArtefactTypeSocialprofile::get_profile_link(
                                        $artefact->get('title'),
                                        $artefact->get('note')),
                                    'title' => $artefact->get('title'),
                                    'description' => $artefact->get('description'),
                                    'note' => $artefact->get('note'),
                                );
                            }
                        }
                        else {
77
78
79
80
81
82
83
84
85
86
                            if ($artefacttype == 'introduction') {
                                $data['introduction'] = $rendered['html'];
                            }
                            else {
                                $data['internalprofiles'][] = array(
                                    'type' => $artefacttype,
                                    'description' => $rendered['html'],
                                    'order' => array_search($artefacttype, $sortorder),
                                );
                            }
87
                        }
88
89
90
91
92
93
94
                    }
                }
                catch (ArtefactNotFoundException $e) {
                    log_debug('Artefact not found when rendering a block instance. '
                        . 'There might be a bug with deleting artefacts of this type? '
                        . 'Original error follows:');
                    log_debug($e->getMessage());
95
                }
96
            }
97
98
99
100
101
102
            // Sort internal profiles by how they display on Content -> Profile page
            $orders = array();
            foreach ($data['internalprofiles'] as $key => $row) {
                $orders[$key]  = $row['order'];
            }
            array_multisort($orders, SORT_ASC, $data['internalprofiles']);
103
104
105
106
107
108
            // Sort social profiles alphabetically (in ASC order)
            $description = array();
            foreach ($data['socialprofiles'] as $key => $row) {
                $description[$key]  = $row['description'];
            }
            array_multisort($description, SORT_ASC, $data['socialprofiles']);
109
        }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
        else if ($editing && !empty($configdata['templateids'])) {
            foreach ($configdata['templateids'] as $artefacttype) {
                $data['internalprofiles'][] = array(
                    'type' => $artefacttype,
                    'description' => '',
                    'order' => array_search($artefacttype, $sortorder),
                );
            }
            // Sort internal profiles by how they display on Content -> Profile page
            $orders = array();
            foreach ($data['internalprofiles'] as $key => $row) {
                $orders[$key]  = $row['order'];
            }
            array_multisort($orders, SORT_ASC, $data['internalprofiles']);
        }
        else if ($editing) {
            $data['nodata'] = get_string('noprofilesselectone', 'blocktype.internal/profileinfo');
        }
128
129
130

        // Work out the path to the thumbnail for the profile image
        if (!empty($configdata['profileicon'])) {
131
132
133
134
135
136
137
138
139
140
141
            if (!empty($configdata['templateids'])) {
                $downloadpath = get_config('wwwroot') . 'thumb.php?type=profileicon&id=0';
                $smarty->assign('profileiconpath', $downloadpath);
                $smarty->assign('profileiconalt', get_string('profileimagetexttemplate', 'mahara'));
            }
            else {
                $downloadpath = get_config('wwwroot') . 'thumb.php?type=profileiconbyid&id=' . $configdata['profileicon'] . '&view=' . $instance->get('view');
                $downloadpath .= '&maxwidth=80';
                $smarty->assign('profileiconpath', $downloadpath);
                $smarty->assign('profileiconalt', get_string('profileimagetext', 'mahara', display_default_name(get_user($viewowner))));
            }
142
        }
Aaron Wells's avatar
Aaron Wells committed
143
        // Override the introduction text if the user has any for this
144
145
146
147
148
149
150
151
152
153
        // particular blockinstance
        if (!empty($configdata['introtext'])) {
            $data['introduction'] = $configdata['introtext'];
        }

        $smarty->assign('profileinfo', $data);

        return $smarty->fetch('blocktype:profileinfo:content.tpl');
    }

154
    /**
Aaron Wells's avatar
Aaron Wells committed
155
     * Overrides the standard get_artefacts method to make sure the profileicon
156
157
158
     * is added also.
     *
     * @param BlockInstance $instance The blockinstance to get artefacts for
Aaron Wells's avatar
Aaron Wells committed
159
     * @return array A list of artefact IDs in the blockinstance, or false if
160
161
     *               there are none
     */
162
163
    public static function get_artefacts(BlockInstance $instance) {
        $configdata = $instance->get('configdata');
164
165
        $return = array();
        if (isset($configdata['artefactids']) && is_array($configdata['artefactids'])) {
166
167
168
            $return = $configdata['artefactids'];
        }
        if (!empty($configdata['profileicon'])) {
169
            $return = array_merge($return, array($configdata['profileicon']));
170
171
        }

172
        return $return ? $return : false;
173
174
    }

175
    public static function has_instance_config(BlockInstance $instance) {
176
177
178
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
179
    public static function instance_config_form(BlockInstance $instance) {
180
181
182
        $configdata = $instance->get('configdata');

        $form = array();
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
        $owner = $instance->get_view()->get('owner');
        if (!$owner) {
            $configdata['artefactids'] = array();
            if (isset($configdata['templateids']) && !empty($configdata['templateids'])) {
                $element = self::artefactchooser_element(null, $owner);
                foreach ($element['artefacttypes'] as $key => $type) {
                    if (array_search($type, $configdata['templateids']) !== false) {
                        $configdata['artefactids'][] = $key;
                    }
                }
                $configdata['templateids'] = array();
            }
            $form['blocktemplatehtml'] = array(
                'type' => 'html',
                'value' => get_string('blockinstanceconfigownerchange', 'mahara'),
            );
199
        }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
        // Which fields does the user want
        $form[] = self::artefactchooser_element((isset($configdata['artefactids'])) ? $configdata['artefactids'] : null, $owner);
        if ($owner) {
            // Profile icon
            if (!$result = get_records_sql_array('SELECT a.id, a.artefacttype, a.title, a.note
                FROM {artefact} a
                WHERE (artefacttype = \'profileicon\' OR artefacttype = \'email\')
                AND a.owner = (
                    SELECT "owner"
                    FROM {view}
                    WHERE id = ?
                )
                ORDER BY a.id', array($instance->get('view')))) {
                $result = array();
            }
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
            $iconoptions = array(
                0 => get_string('dontshowprofileicon', 'blocktype.internal/profileinfo'),
            );
            $emailoptions = array(
                0 => get_string('dontshowemail', 'blocktype.internal/profileinfo'),
            );
            foreach ($result as $profilefield) {
                if ($profilefield->artefacttype == 'profileicon') {
                    $iconoptions[$profilefield->id] = ($profilefield->title) ? $profilefield->title : $profilefield->note;
                }
                else {
                    $emailoptions[$profilefield->id] = $profilefield->title;
                }
            }

            if (count($iconoptions) == 1) {
                $form['noprofileicon'] = array(
                    'type'  => 'html',
                    'title' => get_string('profileicon', 'artefact.file'),
                    'description' => get_string('uploadaprofileicon', 'blocktype.internal/profileinfo', get_config('wwwroot')),
                    'value' => '',
                );
                $form['profileicon'] = array(
                    'type'    => 'hidden',
                    'value'   => 0,
                );
Aaron Wells's avatar
Aaron Wells committed
242
            }
243
            else {
244
245
246
247
248
249
                $form['profileicon'] = array(
                    'type'    => 'radio',
                    'title'   => get_string('profileicon', 'artefact.file'),
                    'options' => $iconoptions,
                    'defaultvalue' => (isset($configdata['profileicon'])) ? $configdata['profileicon'] : 0,
                );
250
            }
251

252
253
254
255
            $form['email'] = array(
                'type'    => 'radio',
                'title'   => get_string('email', 'artefact.internal'),
                'options' => $emailoptions,
256
                'defaultvalue' => (isset($configdata['email']) && get_field('artefact', 'id', 'id', $configdata['email'])) ? $configdata['email'] : 0,
257
258
259
260
            );
        }
        else {
            $form['profileicon'] = array(
261
262
263
264
265
266
267
                'title' => get_string('profileicon', 'artefact.file'),
                'type'  => 'switchbox',
                'defaultvalue' => (isset($configdata['profileicon'])) ? boolval($configdata['profileicon']) : 0,
            );
            $form['blocktemplate'] = array(
                    'type'    => 'hidden',
                    'value'   => 1,
268
269
            );
        }
270
271
        // Introduction
        $form['introtext'] = array(
272
            'type'    => 'wysiwyg',
273
274
275
276
277
            'title'   => get_string('introtext', 'blocktype.internal/profileinfo'),
            'description' => get_string('useintroductioninstead', 'blocktype.internal/profileinfo'),
            'defaultvalue' => (isset($configdata['introtext'])) ? $configdata['introtext'] : '',
            'width' => '100%',
            'height' => '150px',
278
            'rules' => array('maxlength' => 1000000),
279
280
281
282
283
        );

        return $form;
    }

284
285
286
287
288
289
290
291
292
    public static function instance_config_save($values, $instance) {
        require_once('embeddedimage.php');
        if (!empty($values['introtext'])) {
            $newtext = EmbeddedImage::prepare_embedded_images($values['introtext'], 'introtext', $instance->get('id'));
            $values['introtext'] = $newtext;
        }
        else {
            EmbeddedImage::delete_embedded_images('introtext', $instance->get('id'));
        }
293
294
295
296
297
298
299
300
301
302
303
304
305
        if (isset($values['blocktemplate']) && !empty($values['blocktemplate'])) {
            // Need to adjust info to be a template
            $owner = $instance->get_view()->get('owner');
            $values['templateids'] = array();
            $element = self::artefactchooser_element(null, $owner);
            foreach ($element['artefacttypes'] as $key => $type) {
                if (array_search($key, $values['artefactids']) !== false) {
                    $values['templateids'][] = $type;
                }
            }
            $values['artefactids'] = array();
        }

306
307
308
        return $values;
    }

309
    public static function delete_instance(BlockInstance $instance) {
310
311
312
313
        require_once('embeddedimage.php');
        EmbeddedImage::delete_embedded_images('introtext', $instance->get('id'));
    }

314
    public static function artefactchooser_element($default=null, $owner=true) {
315
        safe_require('artefact', 'internal');
316
317
318
        $artefacttypes = PluginArtefactInternal::get_profile_artefact_types();
        if ($owner) {
            $artefacttypes = array_diff($artefacttypes, array('email'));
319
320
321
322
323
324
325
            if ($default && is_array($default)) {
                $emails = get_column_sql("SELECT id FROM {artefact} WHERE artefacttype = 'email'
                                          AND id IN (" . join(',', array_map('db_quote', $default)) . ")");
                if ($emails) {
                    $default = array_diff($default, $emails);
                }
            }
326
327
        }

328
329
330
331
        if (!get_record('blocktype_installed', 'active', 1, 'name', 'socialprofile')) {
            $artefacttypes = array_diff($artefacttypes, array('socialprofile'));
        }

332
333
334
335
336
337
        return array(
            'name'  => 'artefactids',
            'type'  => 'artefactchooser',
            'title' => get_string('fieldstoshow', 'blocktype.internal/profileinfo'),
            'defaultvalue' => $default,
            'blocktype' => 'profileinfo',
338
            'blocktemplate' => empty($owner),
339
340
            'limit'     => 655360, // 640K profile fields is enough for anyone!
            'selectone' => false,
341
            'search'    => false,
342
            'artefacttypes' => $artefacttypes,
343
344
345
            'template'  => 'artefact:internal:artefactchooser-element.tpl',
        );
    }
346
347
348
349
350
351
352
353
354
355
    /**
     * Allow the introduction option to be displayed correctly
     */
    public static function artefactchooser_get_element_data($artefact) {

        if ($artefact->artefacttype == 'introduction') {
            unset($artefact->description);
        }
        return $artefact;
    }
356
357

    /**
Aaron Wells's avatar
Aaron Wells committed
358
359
     * Deliberately enforce _no_ sort order. The database will return them in
     * the order they were inserted, which means roughly the order that they
360
361
362
     * are listed in the profile screen
     */
    public static function artefactchooser_get_sort_order() {
363
364
365
366
367
368
369
370
371
372
373
374
        safe_require('artefact', 'internal');
        $artefacttypes = array_diff(PluginArtefactInternal::get_profile_artefact_types(), array('email'));
        if (!get_record('blocktype_installed', 'active', 1, 'name', 'socialprofile')) {
            $artefacttypes = array_diff($artefacttypes, array('socialprofile'));
        }
        $sortorder = array();
        foreach ($artefacttypes as $type) {
            $sortorder[] = array('fieldname' => 'artefacttype',
                                 'fieldvalue' => $type,
                                 'order' => 'DESC');
        }
        return $sortorder;
375
    }
Richard Mansfield's avatar
Richard Mansfield committed
376

377
    public static function rewrite_blockinstance_config(View $view, $configdata) {
378
        safe_require('artefact', 'internal');
379
        if ($view->get('owner') !== null) {
380
            $artefacttypes = array_diff(PluginArtefactInternal::get_profile_artefact_types(), array('email'));
381
382
383
            if (!get_record('blocktype_installed', 'active', 1, 'name', 'socialprofile')) {
                $artefacttypes = array_diff($artefacttypes, array('socialprofile'));
            }
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
            if (!empty($configdata['blocktemplate'])) {
                if (!empty($configdata['templateids'])) {
                    $artefactids = get_column_sql('
                        SELECT a.id FROM {artefact} a
                        WHERE a.owner = ? AND a.artefacttype != ? AND a.artefacttype IN (' . join(',', array_map('db_quote', $configdata['templateids'])) . ')', array($view->get('owner'), 'email'));
                    if (in_array('email', $configdata['templateids'])) {
                        if ($newemail = get_field('artefact_internal_profile_email', 'artefact', 'principal', 1, 'owner', $view->get('owner'))) {
                            $configdata['email'] = $newemail;
                        }
                    }
                }
                else {
                    $artefactids = array();
                }
                unset($configdata['blocktemplatehtml']);
                unset($configdata['templateids']);
                unset($configdata['blocktemplate']);
            }
            else {
                $artefactids = get_column_sql('
                    SELECT a.id FROM {artefact} a
                    WHERE a.owner = ? AND a.artefacttype IN (' . join(',', array_map('db_quote', $artefacttypes)) . ')', array($view->get('owner')));
            }
407
            $configdata['artefactids'] = $artefactids;
408
409
410
411
412
413
414
415
            if (isset($configdata['email'])) {
                if ($newemail = get_field('artefact_internal_profile_email', 'artefact', 'principal', 1, 'owner', $view->get('owner'))) {
                    $configdata['email'] = $newemail;
                }
                else {
                    unset($configdata['email']);
                }
            }
416
417
418
419
420
421
422
423
            if (isset($configdata['profileicon'])) {
                if ($newicon = get_field('usr', 'profileicon', 'id', $view->get('owner'))) {
                    $configdata['profileicon'] = $newicon;
                }
                else {
                    unset($configdata['profileicon']);
                }
            }
424
425
426
427
428
429
430
        }
        else {
            $configdata['artefactids'] = array();
        }
        return $configdata;
    }

431
    public static function default_copy_type(BlockInstance $instance, View $view) {
432
433
434
        return 'shallow';
    }

435
    /**
Aaron Wells's avatar
Aaron Wells committed
436
     * Profileinfo blocktype is only allowed in personal views, because
437
438
439
     * there's no such thing as group/site profiles
     */
    public static function allowed_in_view(View $view) {
440
        return true;
441
442
    }

443
444
445
446
    /**
     * Overrides the default implementation so we can export enough information
     * to reconstitute profile information again.
     *
Francois Marier's avatar
Francois Marier committed
447
     * Leap2A export doesn't export profile related artefacts as entries, so we
448
449
450
     * need to take that into account when exporting config for it.
     */
    public static function export_blockinstance_config_leap(BlockInstance $bi) {
451
        return PluginArtefactInternal::export_blockinstance_config_leap($bi);
452
453
454
455
456
457
458
    }

    /**
     * Sister method to export_blockinstance_config_leap (creates block
     * instance based of what that method exports)
     */
    public static function import_create_blockinstance_leap(array $biconfig, array $viewconfig) {
459
        return PluginArtefactInternal::import_create_blockinstance_leap($biconfig, $viewconfig);
460
    }
461
}