lib.php 16.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
/**
 * This program is part of Mahara
 *
 *  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 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    mahara
Penny Leach's avatar
Penny Leach committed
20
21
 * @subpackage artefact-internal
 * @author     Penny Leach <penny@catalyst.net.nz>
22
23
24
25
26
27
28
29
30
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

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

class PluginArtefactInternal extends PluginArtefact {

    public static function get_artefact_types() {
32
        return array(
33
34
35
36
37
            'firstname',
            'lastname',
            'studentid',
            'preferredname',
            'introduction',
Martyn Smith's avatar
Martyn Smith committed
38
            'email',
39
40
            'officialwebsite',
            'personalwebsite',
41
            'blogaddress',
Martyn Smith's avatar
Martyn Smith committed
42
            'address',
43
            'town',
Martyn Smith's avatar
Martyn Smith committed
44
            'city',
45
            'country',
Martyn Smith's avatar
Martyn Smith committed
46
47
48
            'homenumber',
            'businessnumber',
            'mobilenumber',
49
50
            'faxnumber',
            'icqnumber',
Martyn Smith's avatar
Martyn Smith committed
51
            'msnnumber',
52
53
54
55
56
57
            'aimscreenname',
            'yahoochat',
            'skypeusername',
            'jabberusername',
            'occupation',
            'industry',
58
            'profileicon'
59
        );
60
61
62
63
64
65
    }

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

66
67
68
69
    public static function menu_items() {
        return array(
            array(
                'name' => 'myprofile',
70
71
                'link' => '',
            )
72
73
        );
    }
74

75
76
77
78
79
80
81
82
83
84
85
86
87
88
    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())));
    }

89
90
91
    public static function get_toplevel_artefact_types() {
        return array('profile');
    }
92
93
94
95
    
    public static function sort_child_data($a, $b) {
        return strnatcasecmp($a->text, $b->text);
    }
Penny Leach's avatar
Penny Leach committed
96
97
}

98
99
class ArtefactTypeProfile extends ArtefactType {

100
101
102
103
104
105
106
107
108
109
110
111
112
    /**
     * 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
113
114
115
            } 
            else {
                $this->owner = $data['owner'];
116
117
118
119
120
            }
        } 
        $this->ctime = time();
        $this->atime = time();
        $this->artefacttype = $type;
121
122
123
124
125
126
127
128
129
130
131
132
        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;
                }
            }
        }
133
134
    }

135
136
137
138
139
140
141
    public function set($field, $value) {
        if ($field == 'title' && empty($value)) {
            return $this->delete();
        }
        return parent::set($field, $value);
    }

142
    public function render($format, $options) {
143
        if ($format == FORMAT_ARTEFACT_RENDERFULL) {
Martyn Smith's avatar
Martyn Smith committed
144
145
            return $this->title;
        }
146
        return parent::render($format, $options);
147
148
149
150
151
152
153
    }

    public function get_icon() {

    }

    public static function get_render_list() {
154
        return array(FORMAT_ARTEFACT_LISTSELF, FORMAT_ARTEFACT_RENDERFULL, FORMAT_ARTEFACT_RENDERMETADATA);
155
    }
Penny Leach's avatar
Penny Leach committed
156

157
    public static function is_singular() {
Penny Leach's avatar
Penny Leach committed
158
159
        return true;
    }
160
    
Martyn Smith's avatar
Martyn Smith committed
161
162
163
164
165
166
167
168
169
170
    public static function get_all_fields() {
        return array(
            'firstname'       => 'text',
            'lastname'        => 'text',
            'studentid'       => 'text',
            'preferredname'   => 'text',
            'introduction'    => 'wysiwyg',
            'email'           => 'emaillist',
            'officialwebsite' => 'text',
            'personalwebsite' => 'text',
171
            'blogaddress'     => 'text',
Martyn Smith's avatar
Martyn Smith committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
            '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',
        );
    }

191
    public static function get_mandatory_fields() {
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
        $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
208
209
210
        return array(
            'firstname' => 'text', 
            'lastname'  => 'text', 
211
            'email'     => 'emaillist', 
Martyn Smith's avatar
Martyn Smith committed
212
        );
213
    }
214

215
    public static function get_public_fields() {
216
217
218
219
220
221
222
223
224
225
226
227
        $all = self::get_all_fields();
        $p = array();
        if ($pub = get_config_plugin('artefact', 'internal', 'profilepublic')) {
            $public = explode(',', $pub);
        }
        else {
            $public = array();
        }
        foreach ($public as $pf) {
            $p[$pf] = $all[$pf];
        }
        return $p;
228
229
    }

230
231
232
233
234
    public static function has_config() {
        return true;
    }

    public static function get_config_options() {
235
236
        $mandatory = self::get_mandatory_fields();
        $public = self::get_public_fields();
237
        $alwaysmandatory = self::get_always_mandatory_fields();
238
        $form = array(
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
            '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'),
                )
            ),
254
255
256
        );

        foreach (array_keys(self::get_all_fields()) as $field) {
257
258
            $form['elements'][$field . '_mandatory'] = array(
                'defaultvalue' => ((isset($mandatory[$field])) ? 'checked' : ''),
259
260
261
                'title'        => get_string($field, 'artefact.internal'),
                'type'         => 'checkbox',
            );
262
263
264
265
266
267
            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' : ''),
268
269
270
271
272
                'title'        => get_string($field, 'artefact.internal'),
                'type'         => 'checkbox',

            );
        }
273
274
275
276
277
278

        $form['elements']['emptyrow'] = array(
            'title' => '    ',
            'type' => 'html',
            'value' => '&nbsp;',
        );
279
        $currentwidth = get_config_plugin('artefact', 'internal', 'profileiconwidth');
280
281
282
283
284
        $form['elements']['profileiconwidth'] = array(
            'type' => 'text',
            'size' => 4,
            'suffix' => get_string('widthshort'),
            'title' => get_string('profileiconsize', 'artefact.internal'),
285
            'defaultvalue' => ((!empty($currentwidth)) ? $currentwidth : 100),
286
287
288
289
290
            'rules' => array(
                'required' => true,
                'integer'  => true,
            )
        );
291
        $currentheight = get_config_plugin('artefact', 'internal', 'profileiconheight');
292
293
294
295
296
        $form['elements']['profileiconheight'] = array(
            'type' => 'text',
            'suffix' => get_string('heightshort'),
            'size' => 4,
            'title' => get_string('profileiconsize', 'artefact.internal'),
297
            'defaultvalue' => ((!empty($currentheight)) ? $currentheight : 100),
298
299
300
301
302
303
            'rules' => array(
                'required' => true,
                'integer'  => true,
            )
        );

304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
        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);
323
324
        set_config_plugin('artefact', 'internal', 'profileiconwidth', $values['profileiconwidth']);
        set_config_plugin('artefact', 'internal', 'profileiconheight', $values['profileiconheight']);
325
    }
326
327
}

328
329
330
331
class ArtefactTypeProfileField extends ArtefactTypeProfile {
    public static function collapse_config() {
        return 'profile';
    }
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351

    /**
     * This method is optional, and specifies how child data should be formatted
     * for the artefact tree.
     *
     * It should return a StdClass object, with the following fields set:
     *
     *  - id
     *  - title
     *  - text
     *  - container
     *  - parent
     *
     *  @param object $data The data to reformat. Contains some fields from the
     *                      <kbd>artefact</kbd> table, namely title, artefacttype
     *                      and container
     *  @return object      The reformatted data
     */
    public static function format_child_data($data, $pluginname) {
        $res = new StdClass;
352
353
354
        $res->id         = $data->id;
        $res->title      = $data->title;
        $res->isartefact = true;
355
356
357
358
359
360
361
362
363
364
        if ($data->artefacttype == 'email') {
            $res->text = get_string('email') . ' - ' . $data->title;
        }
        else {
            $res->text = get_string($data->artefacttype, "artefact.$pluginname");
        }
        $res->container = 0;
        $res->parent    = null;
        return $res;
    }
365

366
367
368
}

class ArtefactTypeCachedProfileField extends ArtefactTypeProfileField {
369
370
    
    public function commit() {
371
        global $USER;
372
        parent::commit();
373
374
        $field = $this->get_artefact_type();
        set_field('usr', $field, $this->title, 'id', $this->owner);
Nigel McNie's avatar
Nigel McNie committed
375
376
377
        if ($this->owner == $USER->get('id')) {
            $USER->set($field, $this->title);
        }
378
379
380
    }

    public function delete() {
381
        parent::delete();
382
383
384
385
386
387
        $field = $this->get_artefact_type();
        set_field('usr', $field, null, 'id', $this->owner);
    }

}

388
389
390
class ArtefactTypeFirstname extends ArtefactTypeCachedProfileField {}
class ArtefactTypeLastname extends ArtefactTypeCachedProfileField {}
class ArtefactTypePreferredname extends ArtefactTypeCachedProfileField {}
391
392
393
class ArtefactTypeEmail extends ArtefactTypeProfileField {
    public function commit() {

394
        parent::commit();
395
396

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

398
        if(!$email_record) {
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
            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,
                    )
                );
            }
429
430
431
        }
    }
}
432
433
434

class ArtefactTypeStudentid extends ArtefactTypeProfileField {}
class ArtefactTypeIntroduction extends ArtefactTypeProfileField {}
435
class ArtefactTypeWebAddress extends ArtefactTypeProfileField {
436
    public function render($format, $options) {
437
438
        if (($format == FORMAT_ARTEFACT_LISTSELF || $format == FORMAT_ARTEFACT_RENDERFULL)
            && $options['link'] == true) {
439
                return make_link($this->title);
440
        }
441
        return parent::render($format, $options);
442
443
    }
}
444
445
class ArtefactTypeOfficialwebsite extends ArtefactTypeWebAddress {}
class ArtefactTypePersonalwebsite extends ArtefactTypeWebAddress {}
446
class ArtefactTypeBlogAddress extends ArtefactTypeProfileField {}
447
448
449
class ArtefactTypeAddress extends ArtefactTypeProfileField {}
class ArtefactTypeTown extends ArtefactTypeProfileField {}
class ArtefactTypeCity extends ArtefactTypeProfileField {}
450
451
class ArtefactTypeCountry extends ArtefactTypeProfileField {
    public function render($format, $options) {
452
        if ($format == FORMAT_ARTEFACT_LISTSELF || $format == FORMAT_ARTEFACT_RENDERFULL) {
453
454
455
            $countries = getoptions_country();
            return $countries[$this->title];
        }
456
        return parent::render($format, $options);
457
458
    }
}
459
460
461
462
463
464
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
465
466
467
468
469
470
class ArtefactTypeAimscreenname extends ArtefactTypeProfileField {}
class ArtefactTypeYahoochat extends ArtefactTypeProfileField {}
class ArtefactTypeSkypeusername extends ArtefactTypeProfileField {}
class ArtefactTypeJabberusername extends ArtefactTypeProfileField {}
class ArtefactTypeOccupation extends ArtefactTypeProfileField {}
class ArtefactTypeIndustry extends ArtefactTypeProfileField {}
471

472
473
474
475
476
477
class ArtefactTypeProfileIcon extends ArtefactTypeProfileField {
    public static function is_note_private() {
        return true;
    }
}

478
479

?>