lib.php 43.3 KB
Newer Older
1
2
3
4
<?php
/**
 *
 * @package    mahara
Penny Leach's avatar
Penny Leach committed
5
 * @subpackage artefact-internal
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
14
15
16
 *
 */

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

class PluginArtefactInternal extends PluginArtefact {

    public static function get_artefact_types() {
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
        return array(
            'firstname',
            'lastname',
            'studentid',
            'preferredname',
            'introduction',
            'email',
            'officialwebsite',
            'personalwebsite',
            'blogaddress',
            'address',
            'town',
            'city',
            'country',
            'homenumber',
            'businessnumber',
            'mobilenumber',
            'faxnumber',
            'occupation',
            'industry',
            'html',
38
            'socialprofile',
39
40
41
42
        );
    }

    public static function get_profile_artefact_types() {
43
        return array(
44
45
46
47
48
            'firstname',
            'lastname',
            'studentid',
            'preferredname',
            'introduction',
Martyn Smith's avatar
Martyn Smith committed
49
            'email',
50
51
            'officialwebsite',
            'personalwebsite',
52
            'blogaddress',
Martyn Smith's avatar
Martyn Smith committed
53
            'address',
54
            'town',
Martyn Smith's avatar
Martyn Smith committed
55
            'city',
56
            'country',
Martyn Smith's avatar
Martyn Smith committed
57
58
59
            'homenumber',
            'businessnumber',
            'mobilenumber',
60
61
62
            'faxnumber',
            'occupation',
            'industry',
63
            'socialprofile',
64
        );
65
    }
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

    public static function get_contactinfo_artefact_types() {
        return array(
            'email',
            'officialwebsite',
            'personalwebsite',
            'blogaddress',
            'address',
            'town',
            'city',
            'country',
            'homenumber',
            'businessnumber',
            'mobilenumber',
            'faxnumber',
81
            'socialprofile',
82
83
        );
    }
Aaron Wells's avatar
Aaron Wells committed
84

85
86
87
    public static function get_block_types() {
        return array();
    }
88
89
90
91
92

    public static function get_plugin_name() {
        return 'internal';
    }

93
94
95
96
    public static function is_active() {
        return get_field('artefact_installed', 'active', 'name', 'internal');
    }

97
98
    public static function menu_items() {
        return array(
99
            'content/profile' => array(
100
                'path' => 'content/profile',
101
                'url' => 'artefact/internal/index.php',
102
                'title' => get_string('profile', 'artefact.internal'),
103
104
                'weight' => 10,
            ),
105
106
107
108
109
110
            'content/notes' => array(
                'path' => 'content/notes',
                'url' => 'artefact/internal/notes.php',
                'title' => get_string('Notes', 'artefact.internal'),
                'weight' => 60,
            ),
111
112
        );
    }
113

114
115
    public static function submenu_items() {
        $tabs = array(
116
117
118
            'subnav' => array(
                'class' => 'profile'
            ),
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
            'profile' => array(
                'page'  => 'profile',
                'url'   => 'artefact/internal/index.php',
                'title' => get_string('aboutme', 'artefact.internal'),
            ),
            'contact' => array(
                'page'  => 'contact',
                'url'   => 'artefact/internal/index.php?fs=contact',
                'title' => get_string('contact', 'artefact.internal'),
            ),
            'social' => array(
                'page'  => 'social',
                'url'   => 'artefact/internal/index.php?fs=social',
                'title' => get_string('social', 'artefact.internal'),
            ),
            'general' => array(
                'page'  => 'general',
                'url'   => 'artefact/internal/index.php?fs=general',
                'title' => get_string('general'),
            ),
        );
        if (!get_field('artefact_installed_type', 'name', 'name', 'socialprofile')) {
            unset($tabs['social']);
        }
        if (defined('INTERNAL_SUBPAGE') && isset($tabs[INTERNAL_SUBPAGE])) {
            $tabs[INTERNAL_SUBPAGE]['selected'] = true;
        }
        return $tabs;
    }

149
150
151
152
153
154
155
156
157
158
159
160
161
162
    public static function get_cron() {
        return array(
            (object)array(
                'callfunction' => 'clean_email_validations',
                'hour'         => '4',
                'minute'       => '10',
            ),
        );
    }

    public static function clean_email_validations() {
        delete_records_select('artefact_internal_profile_email', 'verified=0 AND expiry IS NOT NULL AND expiry < ?', array(db_format_timestamp(time())));
    }

163
164
165
    public static function sort_child_data($a, $b) {
        return strnatcasecmp($a->text, $b->text);
    }
166
167
168
169

    public static function can_be_disabled() {
        return false;
    }
170
171
172

    public static function get_artefact_type_content_types() {
        return array(
173
174
175
            'introduction'  => array('text'),
            'html'          => array('text'),
            'socialprofile' => array('html'),
176
177
        );
    }
178
179
180
181
182
183

    /**
     * This method is provided by the plugin class so it can be used by the
     * profileinfo and contactinfo blocktypes. See the blocktypes'
     * export_blockinstance_config_leap method for more information.
     *
Francois Marier's avatar
Francois Marier committed
184
     * Leap2A export doesn't export profile related artefacts as entries, so we
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
     * need to take that into account when exporting config for it.
     *
     * @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) {
        static $cache = array();
        $owner = $bi->get_view()->get('owner');

        // This blocktype is only allowed in personal views
        if (!$owner) {
            return array();
        }

        if (!isset($cache[$owner])) {
            $cache[$owner] = get_records_sql_assoc("SELECT id, artefacttype, title
                FROM {artefact}
Francois Marier's avatar
Francois Marier committed
202
                WHERE \"owner\" = ?
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
                AND artefacttype IN (
                    SELECT name
                    FROM {artefact_installed_type}
                    WHERE plugin = 'internal'
            )", array($owner));
        }

        $configdata = $bi->get('configdata');

        $result = array();
        if (is_array($configdata)) {
            // Convert the actual profile artefact IDs to their field names
            if (isset($configdata['artefactids']) && is_array($configdata['artefactids'])) {
                $result['fields'] = array();
                foreach ($configdata['artefactids'] as $id) {
                    $result['fields'][] = $cache[$owner][$id]->artefacttype;
                }
220
                $result['fields'] = json_encode(array($result['fields']));
221
222
            }

Francois Marier's avatar
Francois Marier committed
223
            // Email addresses are not entries in Leap2A (they're elements on
224
225
226
            // the persondata element), so we export the actual address here
            // instead of an artefact ID.
            if (!empty($configdata['email']) && isset($cache[$owner][$configdata['email']])) {
227
                $result['email'] = json_encode(array($cache[$owner][$configdata['email']]->title));
228
229
230
            }

            if (!empty($configdata['profileicon'])) {
231
                $result['artefactid'] = json_encode(array(intval($configdata['profileicon'])));
232
233
234
            }

            if (isset($configdata['introtext'])) {
235
                $result['introtext'] = json_encode(array($configdata['introtext']));
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
            }
        }

        return $result;
    }

    /**
     * This method is provided by the plugin class so it can be used by the
     * profileinfo and contactinfo blocktypes. See the blocktypes'
     * import_create_blockinstance_leap method for more information.
     *
     * @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) {
        static $cache = array();
        $configdata = array();

        // This blocktype is only allowed in personal views
        if (empty($viewconfig['owner'])) {
            return;
        }
        $owner = $viewconfig['owner'];

        if (isset($biconfig['config']) && is_array($biconfig['config'])) {
            $impcfg = $biconfig['config'];
            if (isset($impcfg['fields']) && is_array($impcfg['fields'])) {
                // Convert the fields to their artefact ids
                $configdata['artefactids'] = array();
                foreach ($impcfg['fields'] as $field) {
                    if (!isset($cache[$owner])) {
                        $cache[$owner] = get_records_sql_assoc("SELECT artefacttype, id
                            FROM {artefact}
Francois Marier's avatar
Francois Marier committed
270
                            WHERE \"owner\" = ?
271
272
273
274
275
276
277
                            AND artefacttype IN (
                                SELECT name
                                FROM {artefact_installed_type}
                                WHERE plugin = 'internal'
                        )", array($owner));
                    }

278
279
280
                    if (isset($cache[$owner][$field])) {
                        $configdata['artefactids'][] = $cache[$owner][$field]->id;
                    }
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
                }
            }

            if (!empty($impcfg['email'])) {
                if ($artefactid = get_field('artefact_internal_profile_email', 'artefact', 'owner', $owner, 'email', $impcfg['email'])) {
                    $configdata['email'] = $artefactid;
                }
            }

            if (!empty($impcfg['artefactid'])) {
                $configdata['profileicon'] = intval($impcfg['artefactid']);
            }

            if (isset($impcfg['introtext'])) {
                $configdata['introtext'] = $impcfg['introtext'];
            }
        }
        $bi = new BlockInstance(0,
            array(
                'blocktype'  => $biconfig['type'],
                'configdata' => $configdata,
            )
        );

        return $bi;
    }
307
308

    public static function progressbar_additional_items() {
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
        return array(
                (object)array(
                    'name' => 'joingroup',
                    'title' => get_string('progressbaritem_joingroup', 'artefact.internal'),
                    'plugin' => 'internal',
                    'active' => true,
                    'iscountable' => true,
                    'is_metaartefact' => true,
                ),
                (object)array(
                    'name' => 'makefriend',
                    'title' => get_string('progressbaritem_makefriend', 'artefact.internal'),
                    'plugin' => 'internal',
                    'active' => true,
                    'iscountable' => true,
                    'is_metaartefact' => true,
                )
        );
327
328
    }

329
    public static function progressbar_metaartefact_count($name) {
330
331
332
        global $USER;

        $meta = new StdClass();
333
        $meta->artefacttype = $name;
334
        $meta->completed = 0;
335
        switch ($name) {
336
337
338
339
340
341
342
343
            case 'joingroup':
                $sql = "SELECT COUNT(*) AS completed
                         FROM {group_member}
                       WHERE member = ?";
                $count = get_records_sql_array($sql, array($USER->get('id')));
                $meta->completed = $count[0]->completed;
                break;
            case 'makefriend':
344
                // We count make friend as either initiating or accepting a friendship
345
346
                $sql = "SELECT COUNT(*) AS completed
                         FROM {usr_friend}
347
348
                       WHERE usr1 = ? OR usr2 = ?";
                $count = get_records_sql_array($sql, array($USER->get('id'), $USER->get('id')));
349
350
                $meta->completed = $count[0]->completed;
                break;
351
352
            default:
                return false;
353
354
355
356
357
358
        }
        return $meta;
    }

    public static function progressbar_link($artefacttype) {
        switch ($artefacttype) {
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
            case 'firstname':
            case 'lastname':
            case 'studentid':
            case 'preferredname':
            case 'introduction':
                return 'artefact/internal/index.php';
                break;
            case 'email':
            case 'officialwebsite':
            case 'personalwebsite':
            case 'blogaddress':
            case 'address':
            case 'town':
            case 'city':
            case 'country':
            case 'homenumber':
            case 'businessnumber':
            case 'mobilenumber':
            case 'faxnumber':
                return 'artefact/internal/index.php?fs=contact';
                break;
380
381
            case 'socialprofile':
                return 'artefact/internal/index.php?fs=social';
382
383
384
385
386
387
388
389
390
391
392
393
394
                break;
            case 'occupation':
            case 'industry':
                return 'artefact/internal/index.php?fs=general';
                break;
            case 'joingroup':
                return 'group/find.php';
                break;
            case 'makefriend':
                return 'user/find.php';
                break;
            default:
                return 'view/index.php';
395
396
        }
    }
Penny Leach's avatar
Penny Leach committed
397
398
}

399
400
class ArtefactTypeProfile extends ArtefactType {

401
402
403
    /**
     * overriding this because profile fields
     * are unique in that except for email, you only get ONE
404
405
406
     * so if we don't get an id, we still need to go look for it.
     * On the other hand, if our caller knows the artefact is new,
     * we can skip the query.
407
     */
408
    public function __construct($id=0, $data=null, $new = FALSE) {
409
        $type = $this->get_artefact_type();
410
        if (!empty($id) || $type == 'email' || $type == 'socialprofile') {
411
412
413
            return parent::__construct($id, $data);
        }
        if (!empty($data['owner'])) {
414
            if (!$new && $a = get_record('artefact', 'artefacttype', $type, 'owner', $data['owner'])) {
415
                return parent::__construct($a->id, $a);
Aaron Wells's avatar
Aaron Wells committed
416
            }
Penny Leach's avatar
Penny Leach committed
417
418
            else {
                $this->owner = $data['owner'];
419
            }
Aaron Wells's avatar
Aaron Wells committed
420
        }
421
422
423
        $this->ctime = time();
        $this->atime = time();
        $this->artefacttype = $type;
424
425
426
427
428
429
430
431
432
433
434
435
        if (empty($id)) {
            $this->dirty = true;
            $this->ctime = $this->mtime = time();
            if (empty($data)) {
                $data = array();
            }
            foreach ((array)$data as $field => $value) {
                if (property_exists($this, $field)) {
                    $this->{$field} = $value;
                }
            }
        }
436
437
    }

438
439
440
441
442
443
444
    public function set($field, $value) {
        if ($field == 'title' && empty($value)) {
            return $this->delete();
        }
        return parent::set($field, $value);
    }

445
    public static function get_icon($options=null) {
446
447
448

    }

449
    public static function is_singular() {
Penny Leach's avatar
Penny Leach committed
450
451
        return true;
    }
Aaron Wells's avatar
Aaron Wells committed
452

Martyn Smith's avatar
Martyn Smith committed
453
    public static function get_all_fields() {
454
        $out = array(
Martyn Smith's avatar
Martyn Smith committed
455
456
457
458
459
460
461
462
            'firstname'       => 'text',
            'lastname'        => 'text',
            'studentid'       => 'text',
            'preferredname'   => 'text',
            'introduction'    => 'wysiwyg',
            'email'           => 'emaillist',
            'officialwebsite' => 'text',
            'personalwebsite' => 'text',
463
            'blogaddress'     => 'text',
Martyn Smith's avatar
Martyn Smith committed
464
465
466
467
468
469
470
471
472
473
            'address'         => 'textarea',
            'town'            => 'text',
            'city'            => 'text',
            'country'         => 'select',
            'homenumber'      => 'text',
            'businessnumber'  => 'text',
            'mobilenumber'    => 'text',
            'faxnumber'       => 'text',
            'occupation'      => 'text',
            'industry'        => 'text',
474
            'maildisabled'    => 'html',
Martyn Smith's avatar
Martyn Smith committed
475
        );
476
477
478
479
480
481
482
483
        $social = array();
        if (get_record('blocktype_installed', 'active', 1, 'name', 'socialprofile')) {
            $social = array(
                'socialprofile'   => 'html',
            );
        }
        $out = array_merge($out, $social);
        return $out;
Martyn Smith's avatar
Martyn Smith committed
484
485
    }

486
487
488
489
490
491
492
493
494
    public static function get_field_element_data() {
        return array(
            'firstname'       => array('rules' => array('maxlength' => 50)),
            'lastname'        => array('rules' => array('maxlength' => 50)),
            'studentid'       => array('rules' => array('maxlength' => 50)),
            'preferredname'   => array('rules' => array('maxlength' => 50)),
        );
    }

495
    public static function get_mandatory_fields() {
496
497
498
499
500
501
502
503
504
        $m = array();
        $all = self::get_all_fields();
        $alwaysm = self::get_always_mandatory_fields();
        if ($man = get_config_plugin('artefact', 'internal', 'profilemandatory')) {
            $mandatory = explode(',', $man);
        }
        else {
            $mandatory = array();
        }
505
506
507
508
509
        // If socialprofile is disabled, we need to remove any fields that may
        // have been selected when it was enabled.
        // If socialprofile is enabled, we need to remove any fields that my
        // have been selected when it was disabled.
        $need_to_update = false;
510
        foreach ($mandatory as $mf) {
511
512
513
514
515
516
517
518
519
520
            if (isset($all[$mf])) {
                $m[$mf] = $all[$mf];
            }
            else {
                $need_to_update = true;
            }
        }
        if ($need_to_update) {
            // We need to save the config settings for the mandatory fields for the plugin.
            set_config_plugin('artefact', 'internal', 'profilemandatory', join(',', array_keys($m)));
521
522
523
524
525
        }
        return array_merge($m, $alwaysm);
    }

    public static function get_always_mandatory_fields() {
Martyn Smith's avatar
Martyn Smith committed
526
        return array(
Aaron Wells's avatar
Aaron Wells committed
527
528
            'firstname' => 'text',
            'lastname'  => 'text',
529
            'email'     => 'emaillist',
Martyn Smith's avatar
Martyn Smith committed
530
        );
531
    }
532

533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
    public static function get_all_searchable_fields() {
        return array(
            'firstname'       => 'text',
            'lastname'        => 'text',
            'studentid'       => 'text',
            'preferredname'   => 'text',
            'email'           => 'emaillist',
        );
    }

    public static function get_always_searchable_fields() {
        return array(
            'firstname'       => 'text',
            'lastname'        => 'text',
            'preferredname'   => 'text',
        );
    }

    public static function get_searchable_fields() {
        if ($pub = get_config_plugin('artefact', 'internal', 'profilepublic')) {
            $public = explode(',', $pub);
        }
        else {
            $public = array();
        }

        $all      = self::get_all_searchable_fields();
        $selected = self::get_always_searchable_fields();

562
563
564
565
566
        // If socialprofile is disabled, we need to remove any fields that may
        // have been selected when it was enabled.
        // If socialprofile is enabled, we need to remove any fields that my
        // have been selected when it was disabled.
        $need_to_update = false;
567
568
569
570
        foreach ($public as $pf) {
            if (isset($all[$pf])) {
                $selected[$pf] = $all[$pf];
            }
571
572
573
574
575
576
            else {
                $need_to_update = true;
            }
        }
        if ($need_to_update) {
            set_config_plugin('artefact', 'internal', 'profilepublic', join(',', array_keys($selected)));
577
578
579
580
581
        }

        return $selected;
    }

582
583
584
585
586
    public static function has_config() {
        return true;
    }

    public static function get_config_options() {
587
        $allmandatory    = self::get_all_fields();
588
        $alwaysmandatory = self::get_always_mandatory_fields();
589
        $mandatory       = self::get_mandatory_fields();
590

591
592
593
        $mandatoryfields = array();
        foreach (array_keys($allmandatory) as $field) {
            $mandatoryfields[$field] = array(
594
                'title'        => get_string($field, 'artefact.internal'),
595
596
597
                'value'        => $field,
                'defaultvalue' => isset($alwaysmandatory[$field]) || isset($mandatory[$field]),
                'disabled'     => isset($alwaysmandatory[$field]),
598
            );
599
600
601
602
603
        }

        $allsearchable    = self::get_all_searchable_fields();
        $alwayssearchable = self::get_always_searchable_fields();
        $searchable       = self::get_searchable_fields();
604

605
606
607
608
609
610
611
        $searchablefields = array();
        foreach (array_keys($allsearchable) as $field) {
            $searchablefields[$field] = array(
                'title'        => get_string($field, 'artefact.internal'),
                'value'        => $field,
                'defaultvalue' => isset($alwayssearchable[$field]) || isset($searchable[$field]),
                'disabled'     => isset($alwayssearchable[$field]),
612
613
            );
        }
614

615
        $form = array(
616
            'class'     => 'panel panel-body',
617
618
619
            'elements'   => array(
                'mandatory' =>  array(
                    'title'        => get_string('mandatoryfields', 'artefact.internal'),
620
621
                    'description'  => get_string('mandatoryfieldsdescription', 'artefact.internal'),
                    'help'         => true,
622
                    'class'        => 'stacked',
623
624
625
626
627
628
629
                    'type'         => 'checkboxes',
                    'elements'     => $mandatoryfields,
                    'options'      => $allmandatory, // Only the keys are used by validateoptions
                    'rules'        => array('validateoptions' => true),
                ),
                'searchable' =>  array(
                    'title'        => get_string('searchablefields', 'artefact.internal'),
630
631
                    'description'  => get_string('searchablefieldsdescription', 'artefact.internal'),
                    'help'         => true,
632
                    'class'        => 'stacked',
633
634
635
636
637
638
639
640
                    'type'         => 'checkboxes',
                    'elements'     => $searchablefields,
                    'options'      => $allsearchable, // Only the keys are used by validateoptions
                    'rules'        => array('validateoptions' => true),
                ),
            ),
        );

641
642
643
        return $form;
    }

644
    public function save_config_options($form, $values) {
645
646
647
648
        $mandatory = array_merge(array_keys(self::get_always_mandatory_fields()), $values['mandatory']);
        set_config_plugin('artefact', 'internal', 'profilemandatory', join(',', $mandatory));
        $searchable = array_merge(array_keys(self::get_always_searchable_fields()), $values['searchable']);
        set_config_plugin('artefact', 'internal', 'profilepublic', join(',', $searchable));
649
    }
Martyn Smith's avatar
Martyn Smith committed
650
651
652
653
654

    public static function get_links($id) {
        $wwwroot = get_config('wwwroot');

        return array(
655
            '_default' => $wwwroot . 'artefact/internal/index.php',
Martyn Smith's avatar
Martyn Smith committed
656
657
        );
    }
658
659
660
661

    public function in_view_list() {
        return false;
    }
662

663
664
665
    public function display_title($maxlen=null) {
        return get_string($this->get('artefacttype'), 'artefact.internal');
    }
666
667
}

668
669
670
671
class ArtefactTypeProfileField extends ArtefactTypeProfile {
    public static function collapse_config() {
        return 'profile';
    }
672
673

    public function render_self($options) {
674
        return array('html' => hsc($this->title), 'javascript' => null);
675
    }
676
677
678
679
680
681
682

    /**
     * Render the import entry request for profile fields
     */
    public static function render_import_entry_request($entry_content) {
        return clean_html($entry_content['title']);
    }
683
684
685
}

class ArtefactTypeCachedProfileField extends ArtefactTypeProfileField {
686

687
    public function commit() {
688
        global $USER;
689
        parent::commit();
690
        $field = $this->get_artefact_type();
691
692
        if (!$this->deleted) {
            set_field('usr', $field, $this->title, 'id', $this->owner);
693
694
695
        }
        if ($this->owner == $USER->get('id')) {
            $USER->{$field} = $this->title;
Nigel McNie's avatar
Nigel McNie committed
696
        }
697
698
699
    }

    public function delete() {
700
        parent::delete();
701
702
        $field = $this->get_artefact_type();
        set_field('usr', $field, null, 'id', $this->owner);
703
        $this->title = null;
704
705
706
707
    }

}

708
709
710
class ArtefactTypeFirstname extends ArtefactTypeCachedProfileField {}
class ArtefactTypeLastname extends ArtefactTypeCachedProfileField {}
class ArtefactTypePreferredname extends ArtefactTypeCachedProfileField {}
711
class ArtefactTypeEmail extends ArtefactTypeProfileField {
712
713
714
715
    public static function is_singular() {
        return false;
    }

716
717
    public function commit() {

718
        parent::commit();
719
720

        $email_record = get_record('artefact_internal_profile_email', 'owner', $this->owner, 'email', $this->title);
721

722
        if(!$email_record) {
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
            if (record_exists('artefact_internal_profile_email', 'owner', $this->owner, 'artefact', $this->id)) {
                update_record(
                    'artefact_internal_profile_email',
                    (object) array(
                        'email'     => $this->title,
                        'verified'  => 1,
                    ),
                    (object) array(
                        'owner'     => $this->owner,
                        'artefact'  => $this->id,
                    )
                );
                if (get_field('artefact_internal_profile_email', 'principal', 'owner', $this->owner, 'artefact', $this->id)) {
                    update_record('usr', (object) array( 'email' => $this->title, 'id' => $this->owner));
                }
            }
            else {
                $principal = get_record('artefact_internal_profile_email', 'owner', $this->owner, 'principal', 1);

                insert_record(
                    'artefact_internal_profile_email',
                    (object) array(
                        'owner'     => $this->owner,
                        'email'     => $this->title,
                        'verified'  => 1,
                        'principal' => ( $principal ? 0 : 1 ),
                        'artefact'  => $this->id,
                    )
                );
752
                update_record('usr', (object)array('email' => $this->title, 'id' => $this->owner));
753
            }
754
755
        }
    }
756
757
758
759
760

    public function delete() {
        delete_records('artefact_internal_profile_email', 'artefact', $this->id);
        parent::delete();
    }
761

762
763
764
765
766
767
768
769
770
771
772
773
774
    public static function bulk_delete($artefactids) {
        if (empty($artefactids)) {
            return;
        }

        $idstr = join(',', array_map('intval', $artefactids));

        db_begin();
        delete_records_select('artefact_internal_profile_email', 'artefact IN (' . $idstr . ')');
        parent::bulk_delete($artefactids);
        db_commit();
    }

775
776
777
778
779
780
781
782
783
    public function render_self($options) {
        if (array_key_exists('link', $options) && $options['link'] == true) {
            $html = '<a href="mailto:' . hsc($this->title) . '">' . hsc($this->title) . '</a>';
        }
        else {
            $html = $this->title;
        }
        return array('html' => $html, 'javascript' => null);
    }
784
785
786
787

    static public function is_allowed_in_progressbar() {
        return false;
    }
788
}
789

790
class ArtefactTypeStudentid extends ArtefactTypeCachedProfileField {}
791
792
793
794
795
class ArtefactTypeIntroduction extends ArtefactTypeProfileField {
    public function render_self($options) {
        return array('html' => clean_html($this->title), 'javascript' => null);
    }
}
796
class ArtefactTypeWebAddress extends ArtefactTypeProfileField {
Penny Leach's avatar
Penny Leach committed
797

798
799
    public function commit() {
        $url = $this->get('title');
800
        if (strlen($url) && strpos($url, '://') == false) {
801
802
803
804
805
            $this->set('title', 'http://' . $url);
        }
        parent::commit();
    }

Penny Leach's avatar
Penny Leach committed
806
    public function render_self($options) {
807
        if (array_key_exists('link', $options) && $options['link'] == true) {
808
            $html = '<a href="' . hsc($this->title) . '">' . hsc($this->title) . '</a>';
809
810
811
812
813
        }
        else {
            $html = $this->title;
        }
        return array('html' => $html, 'javascript' => null);
814
815
    }
}
816
817
class ArtefactTypeOfficialwebsite extends ArtefactTypeWebAddress {}
class ArtefactTypePersonalwebsite extends ArtefactTypeWebAddress {}
818
class ArtefactTypeBlogAddress extends ArtefactTypeWebAddress {}
819
820
821
822
823
class ArtefactTypeAddress extends ArtefactTypeProfileField {
    public function render_self($options) {
        return array('html' => format_whitespace($this->title), 'javascript' => null);
    }
}
824
825
class ArtefactTypeTown extends ArtefactTypeProfileField {}
class ArtefactTypeCity extends ArtefactTypeProfileField {}
826
class ArtefactTypeCountry extends ArtefactTypeProfileField {
Penny Leach's avatar
Penny Leach committed
827
828
829
830

    public function render_self($options) {
          $countries = getoptions_country();
          return array('html' => $countries[$this->title], 'javascript' => null);
831
    }
832
833
834
835
836
837
838
    /**
     * Render the import entry request for country fields
     */
    public static function render_import_entry_request($entry_content) {
        $countries = getoptions_country();
        return (isset($countries[$entry_content['title']]) ? $countries[$entry_content['title']] : '');
    }
839
}
840
841
842
843
class ArtefactTypeHomenumber extends ArtefactTypeProfileField {}
class ArtefactTypeBusinessnumber extends ArtefactTypeProfileField {}
class ArtefactTypeMobilenumber extends ArtefactTypeProfileField {}
class ArtefactTypeFaxnumber extends ArtefactTypeProfileField {}
Martyn Smith's avatar
Martyn Smith committed
844
845
class ArtefactTypeOccupation extends ArtefactTypeProfileField {}
class ArtefactTypeIndustry extends ArtefactTypeProfileField {}
846
847
848

/* Artefact type for generic html fragments */
class ArtefactTypeHtml extends ArtefactType {
849
850
851
852
853
854
855
856
857

    public function describe_size() {
        return $this->count_attachments() . ' ' . get_string('attachments', 'artefact.blog');
    }

    public function can_have_attachments() {
        return true;
    }

858
    public static function get_icon($options=null) {
859
        global $THEME;
860
        return false;
861
862
863
864
865
866
867
    }

    public static function is_singular() {
        return false;
    }

    public static function get_links($id) {
868
869
870
        return array(
            '_default' => get_config('wwwroot') . 'artefact/internal/editnote.php?id=' . $id,
        );
871
    }
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891

    public function render_self($options) {
        $smarty = smarty_core();
        $smarty->assign('title', $this->get('title'));
        $smarty->assign('owner', $this->get('owner'));
        $smarty->assign('tags', $this->get('tags'));
        $smarty->assign('description', $this->get('description'));
        if (!empty($options['details']) and get_config('licensemetadata')) {
            $smarty->assign('license', render_license($this));
        }
        else {
            $smarty->assign('license', false);
        }
        $attachments = $this->get_attachments();
        if ($attachments) {
            require_once(get_config('docroot') . 'artefact/lib.php');
            foreach ($attachments as &$attachment) {
                $f = artefact_instance_from_id($attachment->id);
                $attachment->size = $f->describe_size();
                $attachment->iconpath = $f->get_icon(array('id' => $attachment->id, 'viewid' => isset($options['viewid']) ? $options['viewid'] : 0));
892
                $attachment->viewpath = get_config('wwwroot') . 'artefact/artefact.php?artefact=' . $attachment->id . '&view=' . (isset($options['viewid']) ? $options['viewid'] : 0);
893
894
895
896
897
898
899
900
901
902
903
904
                $attachment->downloadpath = get_config('wwwroot') . 'artefact/file/download.php?file=' . $attachment->id;
                if (isset($options['viewid'])) {
                    $attachment->downloadpath .= '&view=' . $options['viewid'];
                }
            }
            $smarty->assign('attachments', $attachments);
        }
        return array(
            'html' => $smarty->fetch('artefact.tpl'),
            'javascript'=>''
        );
    }
905
906
907
908

    public static function is_allowed_in_progressbar() {
        return false;
    }
909
910
911

    public function update_artefact_references(&$view, &$template, &$artefactcopies, $oldid) {
        parent::update_artefact_references($view, $template, $artefactcopies, $oldid);
912
        // 1. Attach copies of the files that were attached to the old note.
913
914
915
916
917
918
919
        if (isset($artefactcopies[$oldid]->oldattachments)) {
            foreach ($artefactcopies[$oldid]->oldattachments as $a) {
                if (isset($artefactcopies[$a])) {
                    $this->attach($artefactcopies[$a]->newid);
                }
            }
        }
920
921
922
923
924
925
926
        // 2. Update embedded images in the note and db
        $regexp = array();
        $replacetext = array();
        if (!empty($artefactcopies[$oldid]->oldembeds)) {
            foreach ($artefactcopies[$oldid]->oldembeds as $a) {
                if (isset($artefactcopies[$a])) {
                    // Change the old image id to the new one
927
928
                    $regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $a . '(&|&amp;)embedded=1(.*?"[^>]+)#';
                    $replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '$2embedded=1$3';
929
930
931
932
933
934
935
936
937
938
939
                }
            }
            require_once('embeddedimage.php');
            $newdescription = EmbeddedImage::prepare_embedded_images(
                    preg_replace($regexp, $replacetext, $this->get('description')),
                    'textbox',
                    $this->get('id'),
                    $view->get('group')
                );
            $this->set('description', $newdescription);
        }
940
    }
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
    /**
     * This function extends ArtefactType::delete() by deleting embedded images
     */
    public function delete() {
        if (empty($this->id)) {
            return;
        }

        db_begin();
        // Delete embedded images in the note
        require_once('embeddedimage.php');
        EmbeddedImage::delete_embedded_images('textbox', $this->id);
        // Delete the artefact and all children.
        parent::delete();
        db_commit();
    }

958
}
959
960
961
962


class ArtefactTypeSocialprofile extends ArtefactTypeProfileField {

963
964
965
966
967
968
969
970
971
972
973
974
975
    public static $socialnetworks = array(
        'facebook',
        'twitter',
        'tumblr',
        'instagram',
        'pinterest',
        'aim',
        'icq',
        'jabber',
        'skype',
        'yahoo',
    );

976
977
978
979
980
981
982
983
984
985
986
    public static function is_singular() {
        return false;
    }

    public function can_have_attachments() {
        return false;
    }

    public function render_self($options) {
        if (array_key_exists('link', $options) && $options['link'] == true) {
            $link = self::get_profile_link($this->title, $this->note);
987
988
989
990
991
992
993
            if ($link) {
                $html = '<a href="' . hsc($link) . '">' . hsc($this->title) . '</a>';
            }
            else {
                // No valid link, even though you asked for one.
                $html = hsc($this->title);
            }
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
        }
        else {
            $html = $this->title;
        }
        return array('html' => $html, 'javascript' => null);
    }

    /**
     * Render the import entry request for social profile fields
     */
    public static function render_import_entry_request($entry_content) {
        $html = '<strong>' . $entry_content['description'] . ':</strong>&nbsp;' . $entry_content['title'];
        return clean_html($html);
    }

    /**
     * Get an array of all the social profiles input for this user.
     * @return array of social profiles.
     */
    public function get_social_profiles() {
        global $USER;

        $sql = 'SELECT * FROM {artefact}
            WHERE owner = ? AND artefacttype = ?
            ORDER BY description ASC';

        if (!$data = get_records_sql_array($sql, array($USER->get('id'), 'socialprofile'))) {
            $data = array();
        }

        $data = self::get_profile_icons($data);
        return $data;
    }

    /*
     * Create and return url of input messaging system or return social profile url of input social site.
     *
     * @param string $data The string containing messaging username or user social profile url
     * @param string $type Social profile subtype; one of icq, aim, yahoo, skype, jabber or webpage (default)
     * @return string The URL address
     */
1035
1036
1037
1038
1039
1040
1041
    public static function get_profile_link($data, $type) {

        // If they've entered a full URL, just use that
        if (filter_var($data, FILTER_VALIDATE_URL)) {
            return $data;
        }

1042
        switch ($type) {
1043
1044
1045
            case 'facebook':
                $link = 'https://www.facebook.com/' . hsc($data);
                break;
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
            case 'twitter':
                // Strip an "@" sign if they put one on.
                if (strlen($data) && $data[0] == '@') {
                    $data = substr($data, 1);
                }
                $link = 'https://twitter.com/' . hsc($data);
                break;
            case 'instagram':
                // Strip an "@" sign if they put one on.
                if (strlen($data) && $data[0] == '@') {
                    $data = substr($data, 1);
                }
                $link = 'https://instagram.com/' . hsc($data) . '/';
                break;
            case 'pinterest':
                $link = 'https://www.pinterest.com/' . hsc($data) . '/';
                break;
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
            case 'icq':
                $link = 'http://www.icq.com/people/' . hsc($data);
                break;
            case 'aim':
                $link = 'aim:goim?screenname=' . hsc($data);
                break;
            case 'yahoo':
                $link = 'ymsgr:chat?' . hsc($data);
                break;
            case 'skype':
                $link = 'skype:' . hsc($data) . '?call';
                break;
            case 'jabber':
                $link = 'xmpp:' . hsc($data);
                break;
            default:
1079
                $link = false;
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
        }

        return $link;
    }

    /**
     * Add favicon of different messaging systems or
     * social sites, contained in the input data.
     * @param array $data with details of the social profile.
     * $data[]->note - the type of social profile (i.e. icq, aim, etc).
     * $data[]->title - display name of the social profile.
     * $data[]->icon - the URL of the icon. Will be populated by this function.
     * @return array of icon details for the specified social profile.
     * $newdata[]->note - originally passed into function.
     * $newdata[]->title - originally passed into function.
     * $newdata[]->icon - the URL of the icon.
     * $newdata[]->link - URL or application call.
     */
1098
    public static function get_profile_icons($data) {
1099
1100
        $newdata = array();
        foreach ($data as $record) {
1101
1102
1103

            $record->link = self::get_profile_link($record->title, $record->note);

1104
            switch ($record->note) {
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
                case 'facebook':
                    $record->icon = favicon_display_url('facebook.com');
                    break;
                case 'tumblr':
                    $record->icon = favicon_display_url('tumblr.com');
                    break;
                case 'twitter':
                    $record->icon = favicon_display_url('twitter.com');
                    break;
                case 'instagram':
                    $record->icon = favicon_display_url('instagram.com');
                    break;
                case 'pinterest':
                    $record->icon = favicon_display_url('www.pinterest.com');
                    break;
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
                case 'icq':
                    $record->icon = favicon_display_url('www.icq.com');
                    break;
                case 'aim':
                    $record->icon = favicon_display_url('www.aim.com');
                    break;
                case 'yahoo':
                    $record->icon = favicon_display_url('messenger.yahoo.com');
                    break;
                case 'skype':
                    // Since www.skype.com favicon is not working...
                    $record->icon = favicon_display_url('support.skype.com');
                    break;
                case 'jabber':
                    // Since www.jabber.org favicon is not working...
                    $record->icon = favicon_display_url('planet.jabber.org');
                    break;
                default:
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
                    // We'll fall back to the "no favicon" default icon
                    $record->icon = favicon_display_url('example.com');

                    // If they've supplied a URL, use its favicon
                    if (filter_var($record->title, FILTER_VALIDATE_URL)) {
                        $url = parse_url($record->title);
                        // Check if $url['host'] actually exists - just in case
                        // it was badly formatted.
                        if (isset($url['host'])) {
                            $record->icon = favicon_display_url($url['host']);
                        }
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
                    }
            }
            $newdata[] = $record;
        }
        return $newdata;
    }

    public function render_profile_element() {
        $data = self::get_social_profiles();

        // Build pagination for 'socialprofile' artefacts table
        $baseurl = get_config('wwwroot') . 'artefact/internal/index.php' .
                   '?' . http_build_query(array('fs' => 'social'));
        $count   = count($data);
        $limit   = 500;
        $offset  = 0;

        $pagination = build_pagination(array(
            'id'        => 'socialprofiles_pagination',
            'url'       => $baseurl,
            'datatable' => 'socialprofilelist',
            'count'     => $count,
            'limit'     => $limit,
            'offset'    => $offset,
        ));

        // User may delete social profile if:
        //  - there is more than 1 social profile and 'socialprofile' is a mandatory field.
        //  - 'socialprofile' is not mandatory.
        $candelete = true;
        $mandatory_fields = ArtefactTypeProfile::get_mandatory_fields();
        if (isset($mandatory_fields['socialprofile']) && count($data) <= 1) {
            $candelete = false;
        }

        $smarty = smarty_core();
        $smarty->assign('controls', true);
        $smarty->assign('rows', $data);
        $smarty->assign('candelete', $candelete);
        $smarty->assign('pagination', $pagination);

        return array(
            'type' => 'html',
            'value' => $smarty->fetch('artefact:internal:socialprofiles.tpl')
        );
    }

    /**
     * Used in the mandatory fields check during the authentication process.
     */
    public function get_new_profile_elements() {

1201
1202
1203
1204
1205
        $socialnetworkoptions = array();
        foreach (ArtefactTypeSocialprofile::$socialnetworks as $socialnetwork) {
            $socialnetworkoptions[$socialnetwork] = get_string($socialnetwork . '.input', 'artefact.internal');
        }

1206
1207
1208
1209
        $items = array(
            'socialprofile_profiletype' => array(
                'type'        => 'select',
                'title'       => get_string('profiletype', 'artefact.internal'),
1210
1211
                'options'     => $socialnetworkoptions,
                'allowother'  => true,
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
                'width'        => 171,
                'rules'        => array('required' => true),
            ),
            'socialprofile_profileurl' => array(
                'type'         => 'text',
                'title'        => get_string('profileurl', 'artefact.internal'),
                'description'  => get_string('profileurldesc', 'artefact.internal'),
                'defaultvalue' => null,
                'size'         => 40,
                'rules'        => array('required' => true),
            ),
        );
        $element = array(
            'type'     => 'fieldset',
            'legend'   => get_string('social', 'artefact.internal'),
            'class'    => 'social',
            'elements' => $items,
        );

        return $element;
    }

}