lib.php 42.6 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
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
    public static function submenu_items() {
        $tabs = array(
            '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;
    }

146
147
148
149
150
151
152
153
154
155
156
157
158
159
    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())));
    }

160
161
162
    public static function sort_child_data($a, $b) {
        return strnatcasecmp($a->text, $b->text);
    }
163
164
165
166

    public static function can_be_disabled() {
        return false;
    }
167
168
169

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

    /**
     * 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
181
     * Leap2A export doesn't export profile related artefacts as entries, so we
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
     * 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
199
                WHERE \"owner\" = ?
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
                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;
                }
217
                $result['fields'] = json_encode(array($result['fields']));
218
219
            }

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

            if (!empty($configdata['profileicon'])) {
228
                $result['artefactid'] = json_encode(array(intval($configdata['profileicon'])));
229
230
231
            }

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

        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
267
                            WHERE \"owner\" = ?
268
269
270
271
272
273
274
                            AND artefacttype IN (
                                SELECT name
                                FROM {artefact_installed_type}
                                WHERE plugin = 'internal'
                        )", array($owner));
                    }

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

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

    public static function progressbar_additional_items() {
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
        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,
                )
        );
324
325
    }

326
    public static function progressbar_metaartefact_count($name) {
327
328
329
        global $USER;

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

    public static function progressbar_link($artefacttype) {
        switch ($artefacttype) {
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
            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;
377
378
            case 'socialprofile':
                return 'artefact/internal/index.php?fs=social';
379
380
381
382
383
384
385
386
387
388
389
390
391
                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';
392
393
        }
    }
Penny Leach's avatar
Penny Leach committed
394
395
}

396
397
class ArtefactTypeProfile extends ArtefactType {

398
399
400
    /**
     * overriding this because profile fields
     * are unique in that except for email, you only get ONE
401
402
403
     * 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.
404
     */
405
    public function __construct($id=0, $data=null, $new = FALSE) {
406
        $type = $this->get_artefact_type();
407
        if (!empty($id) || $type == 'email' || $type == 'socialprofile') {
408
409
410
            return parent::__construct($id, $data);
        }
        if (!empty($data['owner'])) {
411
            if (!$new && $a = get_record('artefact', 'artefacttype', $type, 'owner', $data['owner'])) {
412
                return parent::__construct($a->id, $a);
Aaron Wells's avatar
Aaron Wells committed
413
            }
Penny Leach's avatar
Penny Leach committed
414
415
            else {
                $this->owner = $data['owner'];
416
            }
Aaron Wells's avatar
Aaron Wells committed
417
        }
418
419
420
        $this->ctime = time();
        $this->atime = time();
        $this->artefacttype = $type;
421
422
423
424
425
426
427
428
429
430
431
432
        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;
                }
            }
        }
433
434
    }

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

442
    public static function get_icon($options=null) {
443
444
445

    }

446
    public static function is_singular() {
Penny Leach's avatar
Penny Leach committed
447
448
        return true;
    }
Aaron Wells's avatar
Aaron Wells committed
449

Martyn Smith's avatar
Martyn Smith committed
450
    public static function get_all_fields() {
451
        $out = array(
Martyn Smith's avatar
Martyn Smith committed
452
453
454
455
456
457
458
459
            'firstname'       => 'text',
            'lastname'        => 'text',
            'studentid'       => 'text',
            'preferredname'   => 'text',
            'introduction'    => 'wysiwyg',
            'email'           => 'emaillist',
            'officialwebsite' => 'text',
            'personalwebsite' => 'text',
460
            'blogaddress'     => 'text',
Martyn Smith's avatar
Martyn Smith committed
461
462
463
464
465
466
467
468
469
470
            'address'         => 'textarea',
            'town'            => 'text',
            'city'            => 'text',
            'country'         => 'select',
            'homenumber'      => 'text',
            'businessnumber'  => 'text',
            'mobilenumber'    => 'text',
            'faxnumber'       => 'text',
            'occupation'      => 'text',
            'industry'        => 'text',
471
            'maildisabled'    => 'html',
Martyn Smith's avatar
Martyn Smith committed
472
        );
473
474
475
476
477
478
479
480
        $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
481
482
    }

483
484
485
486
487
488
489
490
491
    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)),
        );
    }

492
    public static function get_mandatory_fields() {
493
494
495
496
497
498
499
500
501
        $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();
        }
502
503
504
505
506
        // 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;
507
        foreach ($mandatory as $mf) {
508
509
510
511
512
513
514
515
516
517
            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)));
518
519
520
521
522
        }
        return array_merge($m, $alwaysm);
    }

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

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
    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();

559
560
561
562
563
        // 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;
564
565
566
567
        foreach ($public as $pf) {
            if (isset($all[$pf])) {
                $selected[$pf] = $all[$pf];
            }
568
569
570
571
572
573
            else {
                $need_to_update = true;
            }
        }
        if ($need_to_update) {
            set_config_plugin('artefact', 'internal', 'profilepublic', join(',', array_keys($selected)));
574
575
576
577
578
        }

        return $selected;
    }

579
580
581
582
583
    public static function has_config() {
        return true;
    }

    public static function get_config_options() {
584
        $allmandatory    = self::get_all_fields();
585
        $alwaysmandatory = self::get_always_mandatory_fields();
586
        $mandatory       = self::get_mandatory_fields();
587

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

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

602
603
604
605
606
607
608
        $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]),
609
610
            );
        }
611

612
613
614
615
        $form = array(
            'elements'   => array(
                'mandatory' =>  array(
                    'title'        => get_string('mandatoryfields', 'artefact.internal'),
616
617
                    'description'  => get_string('mandatoryfieldsdescription', 'artefact.internal'),
                    'help'         => true,
618
619
620
621
622
623
624
                    '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'),
625
626
                    'description'  => get_string('searchablefieldsdescription', 'artefact.internal'),
                    'help'         => true,
627
628
629
630
631
632
633
634
                    'type'         => 'checkboxes',
                    'elements'     => $searchablefields,
                    'options'      => $allsearchable, // Only the keys are used by validateoptions
                    'rules'        => array('validateoptions' => true),
                ),
            ),
        );

635
636
637
        return $form;
    }

638
    public function save_config_options($form, $values) {
639
640
641
642
        $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));
643
    }
Martyn Smith's avatar
Martyn Smith committed
644
645
646
647
648

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

        return array(
649
            '_default' => $wwwroot . 'artefact/internal/index.php',
Martyn Smith's avatar
Martyn Smith committed
650
651
        );
    }
652
653
654
655

    public function in_view_list() {
        return false;
    }
656

657
658
659
    public function display_title($maxlen=null) {
        return get_string($this->get('artefacttype'), 'artefact.internal');
    }
660
661
}

662
663
664
665
class ArtefactTypeProfileField extends ArtefactTypeProfile {
    public static function collapse_config() {
        return 'profile';
    }
666
667

    public function render_self($options) {
668
        return array('html' => hsc($this->title), 'javascript' => null);
669
    }
670
671
672
673
674
675
676

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

class ArtefactTypeCachedProfileField extends ArtefactTypeProfileField {
680

681
    public function commit() {
682
        global $USER;
683
        parent::commit();
684
        $field = $this->get_artefact_type();
685
686
        if (!$this->deleted) {
            set_field('usr', $field, $this->title, 'id', $this->owner);
687
688
689
        }
        if ($this->owner == $USER->get('id')) {
            $USER->{$field} = $this->title;
Nigel McNie's avatar
Nigel McNie committed
690
        }
691
692
693
    }

    public function delete() {
694
        parent::delete();
695
696
        $field = $this->get_artefact_type();
        set_field('usr', $field, null, 'id', $this->owner);
697
        $this->title = null;
698
699
700
701
    }

}

702
703
704
class ArtefactTypeFirstname extends ArtefactTypeCachedProfileField {}
class ArtefactTypeLastname extends ArtefactTypeCachedProfileField {}
class ArtefactTypePreferredname extends ArtefactTypeCachedProfileField {}
705
class ArtefactTypeEmail extends ArtefactTypeProfileField {
706
707
708
709
    public static function is_singular() {
        return false;
    }

710
711
    public function commit() {

712
        parent::commit();
713
714

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

716
        if(!$email_record) {
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
            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,
                    )
                );
746
                update_record('usr', (object)array('email' => $this->title, 'id' => $this->owner));
747
            }
748
749
        }
    }
750
751
752
753
754

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

756
757
758
759
760
761
762
763
764
765
766
767
768
    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();
    }

769
770
771
772
773
774
775
776
777
    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);
    }
778
779
780
781

    static public function is_allowed_in_progressbar() {
        return false;
    }
782
}
783

784
class ArtefactTypeStudentid extends ArtefactTypeCachedProfileField {}
785
786
787
788
789
class ArtefactTypeIntroduction extends ArtefactTypeProfileField {
    public function render_self($options) {
        return array('html' => clean_html($this->title), 'javascript' => null);
    }
}
790
class ArtefactTypeWebAddress extends ArtefactTypeProfileField {
Penny Leach's avatar
Penny Leach committed
791

792
793
    public function commit() {
        $url = $this->get('title');
794
        if (strlen($url) && strpos($url, '://') == false) {
795
796
797
798
799
            $this->set('title', 'http://' . $url);
        }
        parent::commit();
    }

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

    public function render_self($options) {
          $countries = getoptions_country();
          return array('html' => $countries[$this->title], 'javascript' => null);
825
    }
826
827
828
829
830
831
832
    /**
     * 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']] : '');
    }
833
}
834
835
836
837
class ArtefactTypeHomenumber extends ArtefactTypeProfileField {}
class ArtefactTypeBusinessnumber extends ArtefactTypeProfileField {}
class ArtefactTypeMobilenumber extends ArtefactTypeProfileField {}
class ArtefactTypeFaxnumber extends ArtefactTypeProfileField {}
Martyn Smith's avatar
Martyn Smith committed
838
839
class ArtefactTypeOccupation extends ArtefactTypeProfileField {}
class ArtefactTypeIndustry extends ArtefactTypeProfileField {}
840
841
842

/* Artefact type for generic html fragments */
class ArtefactTypeHtml extends ArtefactType {
843
844
845
846
847
848
849
850
851

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

    public function can_have_attachments() {
        return true;
    }

852
    public static function get_icon($options=null) {
853
        global $THEME;
854
        return false;
855
856
857
858
859
860
861
    }

    public static function is_singular() {
        return false;
    }

    public static function get_links($id) {
862
863
864
        return array(
            '_default' => get_config('wwwroot') . 'artefact/internal/editnote.php?id=' . $id,
        );
865
    }
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885

    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));
886
                $attachment->viewpath = get_config('wwwroot') . 'artefact/artefact.php?artefact=' . $attachment->id . '&view=' . (isset($options['viewid']) ? $options['viewid'] : 0);
887
888
889
890
891
892
893
894
895
896
897
898
                $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'=>''
        );
    }
899
900
901
902

    public static function is_allowed_in_progressbar() {
        return false;
    }
903
904
905

    public function update_artefact_references(&$view, &$template, &$artefactcopies, $oldid) {
        parent::update_artefact_references($view, $template, $artefactcopies, $oldid);
906
        // 1. Attach copies of the files that were attached to the old note.
907
908
909
910
911
912
913
        if (isset($artefactcopies[$oldid]->oldattachments)) {
            foreach ($artefactcopies[$oldid]->oldattachments as $a) {
                if (isset($artefactcopies[$a])) {
                    $this->attach($artefactcopies[$a]->newid);
                }
            }
        }
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
        // 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
                    $regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $a . '&embedded=1([^"]+)"#';
                    $replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '&embedded=1"';
                }
            }
            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);
        }
934
    }
935
}
936
937
938
939


class ArtefactTypeSocialprofile extends ArtefactTypeProfileField {

940
941
942
943
944
945
946
947
948
949
950
951
952
    public static $socialnetworks = array(
        'facebook',
        'twitter',
        'tumblr',
        'instagram',
        'pinterest',
        'aim',
        'icq',
        'jabber',
        'skype',
        'yahoo',
    );

953
954
955
956
957
958
959
960
961
962
963
    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);
964
965
966
967
968
969
970
            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);
            }
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
        }
        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
     */
1012
1013
1014
1015
1016
1017
1018
    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;
        }

1019
        switch ($type) {
1020
1021
1022
            case 'facebook':
                $link = 'https://www.facebook.com/' . hsc($data);
                break;
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
            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;
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
            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:
1056
                $link = false;
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
        }

        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.
     */
1075
    public static function get_profile_icons($data) {
1076
1077
        $newdata = array();
        foreach ($data as $record) {
1078
1079
1080

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

1081
            switch ($record->note) {
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
                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;
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
                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:
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
                    // 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']);
                        }
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
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
                    }
            }
            $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() {

1178
1179
1180
1181
1182
        $socialnetworkoptions = array();
        foreach (ArtefactTypeSocialprofile::$socialnetworks as $socialnetwork) {
            $socialnetworkoptions[$socialnetwork] = get_string($socialnetwork . '.input', 'artefact.internal');
        }

1183
1184
1185
1186
        $items = array(
            'socialprofile_profiletype' => array(
                'type'        => 'select',
                'title'       => get_string('profiletype', 'artefact.internal'),
1187
1188
                'options'     => $socialnetworkoptions,
                'allowother'  => true,
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
                '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;
    }

}