lib.php 21.5 KB
Newer Older
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
6
 *
Francois Marier's avatar
Francois Marier committed
7
8
9
10
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
11
 *
Francois Marier's avatar
Francois Marier committed
12
13
14
15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
16
 *
Francois Marier's avatar
Francois Marier committed
17
18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
 *
 * @package    mahara
Penny Leach's avatar
Penny Leach committed
21
 * @subpackage artefact-internal
22
 * @author     Catalyst IT Ltd
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25
26
27
28
29
30
31
32
 *
 */

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

class PluginArtefactInternal extends PluginArtefact {

    public static function get_artefact_types() {
33
        return array(
34
35
36
37
38
            'firstname',
            'lastname',
            'studentid',
            'preferredname',
            'introduction',
Martyn Smith's avatar
Martyn Smith committed
39
            'email',
40
41
            'officialwebsite',
            'personalwebsite',
42
            'blogaddress',
Martyn Smith's avatar
Martyn Smith committed
43
            'address',
44
            'town',
Martyn Smith's avatar
Martyn Smith committed
45
            'city',
46
            'country',
Martyn Smith's avatar
Martyn Smith committed
47
48
49
            'homenumber',
            'businessnumber',
            'mobilenumber',
50
51
            'faxnumber',
            'icqnumber',
Martyn Smith's avatar
Martyn Smith committed
52
            'msnnumber',
53
54
55
56
57
58
            'aimscreenname',
            'yahoochat',
            'skypeusername',
            'jabberusername',
            'occupation',
            'industry',
59
        );
60
    }
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

    public static function get_contactinfo_artefact_types() {
        return array(
            'email',
            'officialwebsite',
            'personalwebsite',
            'blogaddress',
            'address',
            'town',
            'city',
            'country',
            'homenumber',
            'businessnumber',
            'mobilenumber',
            'faxnumber',
            'icqnumber',
            'msnnumber',
            'aimscreenname',
            'yahoochat',
            'skypeusername',
            'jabberusername',
        );
    }
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
    public static function menu_items() {
        return array(
            array(
96
97
98
99
100
101
102
103
104
105
106
                'path' => 'profile',
                'url'  => 'artefact/internal/', // @todo possibly do path aliasing and dispatch?
                'title' => get_string('profile', 'artefact.internal'),
                'weight' => 20,
            ),
            array(
                'path' => 'profile/edit',
                'url' => 'artefact/internal/',
                'title' => get_string('editprofile', 'artefact.internal'),
                'weight' => 10,
            ),
107
108
        );
    }
109

110
111
112
113
114
115
116
117
118
119
120
121
122
123
    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())));
    }

124
125
126
    public static function sort_child_data($a, $b) {
        return strnatcasecmp($a->text, $b->text);
    }
127
128
129
130

    public static function can_be_disabled() {
        return false;
    }
131
132
133
134
135
136

    public static function get_artefact_type_content_types() {
        return array(
            'introduction' => array('text'),
        );
    }
137
138
139
140
141
142

    /**
     * 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
143
     * Leap2A export doesn't export profile related artefacts as entries, so we
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
     * 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
161
                WHERE \"owner\" = ?
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
                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;
                }
179
                $result['fields'] = json_encode(array($result['fields']));
180
181
            }

Francois Marier's avatar
Francois Marier committed
182
            // Email addresses are not entries in Leap2A (they're elements on
183
184
185
            // the persondata element), so we export the actual address here
            // instead of an artefact ID.
            if (!empty($configdata['email']) && isset($cache[$owner][$configdata['email']])) {
186
                $result['email'] = json_encode(array($cache[$owner][$configdata['email']]->title));
187
188
189
            }

            if (!empty($configdata['profileicon'])) {
190
                $result['artefactid'] = json_encode(array(intval($configdata['profileicon'])));
191
192
193
            }

            if (isset($configdata['introtext'])) {
194
                $result['introtext'] = json_encode(array($configdata['introtext']));
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
            }
        }

        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
229
                            WHERE \"owner\" = ?
230
231
232
233
234
235
236
                            AND artefacttype IN (
                                SELECT name
                                FROM {artefact_installed_type}
                                WHERE plugin = 'internal'
                        )", array($owner));
                    }

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

            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;
    }
Penny Leach's avatar
Penny Leach committed
266
267
}

268
269
class ArtefactTypeProfile extends ArtefactType {

270
271
272
273
274
275
276
277
278
279
280
281
282
    /**
     * overriding this because profile fields
     * are unique in that except for email, you only get ONE
     * so if we don't get an id, we still need to go look for it
     */
    public function __construct($id=0, $data=null) {
        $type = $this->get_artefact_type();
        if (!empty($id) || $type == 'email') {
            return parent::__construct($id, $data);
        }
        if (!empty($data['owner'])) {
            if ($a = get_record('artefact', 'artefacttype', $type, 'owner', $data['owner'])) {
                return parent::__construct($a->id, $a);
Penny Leach's avatar
Penny Leach committed
283
284
285
            } 
            else {
                $this->owner = $data['owner'];
286
287
288
289
290
            }
        } 
        $this->ctime = time();
        $this->atime = time();
        $this->artefacttype = $type;
291
292
293
294
295
296
297
298
299
300
301
302
        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;
                }
            }
        }
303
304
    }

305
306
307
308
309
310
311
    public function set($field, $value) {
        if ($field == 'title' && empty($value)) {
            return $this->delete();
        }
        return parent::set($field, $value);
    }

312
    public static function get_icon($options=null) {
313
314
315

    }

316
    public static function is_singular() {
Penny Leach's avatar
Penny Leach committed
317
318
        return true;
    }
319
    
Martyn Smith's avatar
Martyn Smith committed
320
321
322
323
324
325
326
327
328
329
    public static function get_all_fields() {
        return array(
            'firstname'       => 'text',
            'lastname'        => 'text',
            'studentid'       => 'text',
            'preferredname'   => 'text',
            'introduction'    => 'wysiwyg',
            'email'           => 'emaillist',
            'officialwebsite' => 'text',
            'personalwebsite' => 'text',
330
            'blogaddress'     => 'text',
Martyn Smith's avatar
Martyn Smith committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
            'address'         => 'textarea',
            'town'            => 'text',
            'city'            => 'text',
            'country'         => 'select',
            'homenumber'      => 'text',
            'businessnumber'  => 'text',
            'mobilenumber'    => 'text',
            'faxnumber'       => 'text',
            'icqnumber'       => 'text',
            'msnnumber'       => 'text',
            'aimscreenname'   => 'text',
            'yahoochat'       => 'text',
            'skypeusername'   => 'text',
            'jabberusername'  => 'text',
            'occupation'      => 'text',
            'industry'        => 'text',
347
            'maildisabled'    => 'html',
Martyn Smith's avatar
Martyn Smith committed
348
349
350
        );
    }

351
    public static function get_mandatory_fields() {
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
        $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();
        }
        foreach ($mandatory as $mf) {
            $m[$mf] = $all[$mf];
        }
        return array_merge($m, $alwaysm);
    }

    public static function get_always_mandatory_fields() {
Martyn Smith's avatar
Martyn Smith committed
368
369
370
        return array(
            'firstname' => 'text', 
            'lastname'  => 'text', 
371
            'email'     => 'emaillist',
Martyn Smith's avatar
Martyn Smith committed
372
        );
373
    }
374

375
    public static function get_public_fields() {
376
        $all = self::get_all_fields();
377
        $alwaysp = self::get_always_public_fields();
378
379
380
381
382
383
384
385
386
387
        $p = array();
        if ($pub = get_config_plugin('artefact', 'internal', 'profilepublic')) {
            $public = explode(',', $pub);
        }
        else {
            $public = array();
        }
        foreach ($public as $pf) {
            $p[$pf] = $all[$pf];
        }
388
389
390
391
392
393
394
        return array_merge($p, $alwaysp);
    }

    public static function get_always_public_fields() {
        return array(
            'introduction' => 'wysiwyg'
        );
395
396
    }

397
398
399
400
401
    public static function has_config() {
        return true;
    }

    public static function get_config_options() {
402
403
        $mandatory = self::get_mandatory_fields();
        $public = self::get_public_fields();
404
        $alwaysmandatory = self::get_always_mandatory_fields();
405
        $alwayspublic = self::get_always_public_fields();
406
        $form = array(
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
            'renderer'   => 'multicolumntable',
            'elements'   => array(
                'mandatory' =>  array(
                    'title' => ' ', 
                    'type'  => 'html',
                    'class' => 'header',
                    'value' => get_string('mandatory', 'artefact.internal'),
                ),
                'public' => array(
                    'title' => ' ', 
                    'class' => 'header',
                    'type'  => 'html',
                    'value' => get_string('public', 'artefact.internal'),
                )
            ),
422
423
424
        );

        foreach (array_keys(self::get_all_fields()) as $field) {
425
426
            $form['elements'][$field . '_mandatory'] = array(
                'defaultvalue' => ((isset($mandatory[$field])) ? 'checked' : ''),
427
428
429
                'title'        => get_string($field, 'artefact.internal'),
                'type'         => 'checkbox',
            );
430
431
432
433
434
435
            if (isset($alwaysmandatory[$field])) {
                $form['elements'][$field . '_mandatory']['value'] = 'checked';
                $form['elements'][$field . '_mandatory']['disabled'] = true;
            }
            $form['elements'][$field . '_public'] = array(
                'defaultvalue' => ((isset($public[$field])) ? 'checked' : ''),
436
437
438
439
                'title'        => get_string($field, 'artefact.internal'),
                'type'         => 'checkbox',

            );
440
441
442
443
            if (isset($alwayspublic[$field])) {
                $form['elements'][$field . '_public']['value'] = 'checked';
                $form['elements'][$field . '_public']['disabled'] = true;
            }
444
        }
445

446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
        return $form;
    }

    public function save_config_options($values) {
        $mandatory = '';
        $public = '';
        foreach ($values as $field => $value) {
            if (($value == 'on' || $value == 'checked')
                && preg_match('/([a-zA-Z]+)_(mandatory|public)/', $field, $matches)) {
                if (empty($$matches[2])) {
                    $$matches[2] = $matches[1];
                } 
                else {
                    $$matches[2] .= ',' . $matches[1];
                }
            }
        }
        set_config_plugin('artefact', 'internal', 'profilepublic', $public);
        set_config_plugin('artefact', 'internal', 'profilemandatory', $mandatory);
465
    }
Martyn Smith's avatar
Martyn Smith committed
466
467
468
469
470
471
472
473

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

        return array(
            '_default' => $wwwroot . 'artefact/internal/',
        );
    }
474
475
476
477

    public function in_view_list() {
        return false;
    }
478

479
480
481
    public function display_title($maxlen=null) {
        return get_string($this->get('artefacttype'), 'artefact.internal');
    }
482
483
}

484
485
486
487
class ArtefactTypeProfileField extends ArtefactTypeProfile {
    public static function collapse_config() {
        return 'profile';
    }
488
489

    public function render_self($options) {
490
        return array('html' => hsc($this->title), 'javascript' => null);
491
    }
492
493
494
}

class ArtefactTypeCachedProfileField extends ArtefactTypeProfileField {
495
496
    
    public function commit() {
497
        global $USER;
498
        parent::commit();
499
500
501
502
503
504
        if (!$this->deleted) {
            $field = $this->get_artefact_type();
            set_field('usr', $field, $this->title, 'id', $this->owner);
            if ($this->owner == $USER->get('id')) {
                $USER->{$field} = $this->title;
            }
Nigel McNie's avatar
Nigel McNie committed
505
        }
506
507
508
    }

    public function delete() {
509
        parent::delete();
510
511
        $field = $this->get_artefact_type();
        set_field('usr', $field, null, 'id', $this->owner);
512
        $this->title = null;
513
514
515
516
    }

}

517
518
519
class ArtefactTypeFirstname extends ArtefactTypeCachedProfileField {}
class ArtefactTypeLastname extends ArtefactTypeCachedProfileField {}
class ArtefactTypePreferredname extends ArtefactTypeCachedProfileField {}
520
521
522
class ArtefactTypeEmail extends ArtefactTypeProfileField {
    public function commit() {

523
        parent::commit();
524
525

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

527
        if(!$email_record) {
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
            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,
                    )
                );
557
                update_record('usr', (object)array('email' => $this->title, 'id' => $this->owner));
558
            }
559
560
        }
    }
561
562
563
564
565

    public function delete() {
        delete_records('artefact_internal_profile_email', 'artefact', $this->id);
        parent::delete();
    }
566
567
568
569
570
571
572
573
574
575

    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);
    }
576
}
577
578

class ArtefactTypeStudentid extends ArtefactTypeProfileField {}
579
580
581
582
583
class ArtefactTypeIntroduction extends ArtefactTypeProfileField {
    public function render_self($options) {
        return array('html' => clean_html($this->title), 'javascript' => null);
    }
}
584
class ArtefactTypeWebAddress extends ArtefactTypeProfileField {
Penny Leach's avatar
Penny Leach committed
585

586
587
    public function commit() {
        $url = $this->get('title');
588
        if (strlen($url) && strpos($url, '://') == false) {
589
590
591
592
593
            $this->set('title', 'http://' . $url);
        }
        parent::commit();
    }

Penny Leach's avatar
Penny Leach committed
594
    public function render_self($options) {
595
        if (array_key_exists('link', $options) && $options['link'] == true) {
596
            $html = '<a href="' . hsc($this->title) . '">' . hsc($this->title) . '</a>';
597
598
599
600
601
        }
        else {
            $html = $this->title;
        }
        return array('html' => $html, 'javascript' => null);
602
603
    }
}
604
605
class ArtefactTypeOfficialwebsite extends ArtefactTypeWebAddress {}
class ArtefactTypePersonalwebsite extends ArtefactTypeWebAddress {}
606
class ArtefactTypeBlogAddress extends ArtefactTypeWebAddress {}
607
608
609
class ArtefactTypeAddress extends ArtefactTypeProfileField {}
class ArtefactTypeTown extends ArtefactTypeProfileField {}
class ArtefactTypeCity extends ArtefactTypeProfileField {}
610
class ArtefactTypeCountry extends ArtefactTypeProfileField {
Penny Leach's avatar
Penny Leach committed
611
612
613
614

    public function render_self($options) {
          $countries = getoptions_country();
          return array('html' => $countries[$this->title], 'javascript' => null);
615
616
    }
}
617
618
619
620
621
622
class ArtefactTypeHomenumber extends ArtefactTypeProfileField {}
class ArtefactTypeBusinessnumber extends ArtefactTypeProfileField {}
class ArtefactTypeMobilenumber extends ArtefactTypeProfileField {}
class ArtefactTypeFaxnumber extends ArtefactTypeProfileField {}
class ArtefactTypeIcqnumber extends ArtefactTypeProfileField {}
class ArtefactTypeMsnnumber extends ArtefactTypeProfileField {}
Martyn Smith's avatar
Martyn Smith committed
623
624
625
626
627
628
class ArtefactTypeAimscreenname extends ArtefactTypeProfileField {}
class ArtefactTypeYahoochat extends ArtefactTypeProfileField {}
class ArtefactTypeSkypeusername extends ArtefactTypeProfileField {}
class ArtefactTypeJabberusername extends ArtefactTypeProfileField {}
class ArtefactTypeOccupation extends ArtefactTypeProfileField {}
class ArtefactTypeIndustry extends ArtefactTypeProfileField {}
629

630

631
632

?>