lib.php 15.2 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
        );
59
60
61
62
63
64
    }

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

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

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

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

97
98
class ArtefactTypeProfile extends ArtefactType {

99
100
101
102
103
104
105
106
107
108
109
110
111
    /**
     * 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
112
113
114
            } 
            else {
                $this->owner = $data['owner'];
115
116
117
118
119
120
121
            }
        } 
        $this->ctime = time();
        $this->atime = time();
        $this->artefacttype = $type;
    }

122
    public function render($format, $options) {
123
        if ($format == FORMAT_ARTEFACT_RENDERFULL) {
Martyn Smith's avatar
Martyn Smith committed
124
125
            return $this->title;
        }
126
        return parent::render($format, $options);
127
128
129
130
131
132
133
    }

    public function get_icon() {

    }

    public static function get_render_list() {
134
        return array(FORMAT_ARTEFACT_LISTSELF, FORMAT_ARTEFACT_RENDERFULL, FORMAT_ARTEFACT_RENDERMETADATA);
135
    }
Penny Leach's avatar
Penny Leach committed
136

137
    public static function is_singular() {
Penny Leach's avatar
Penny Leach committed
138
139
        return true;
    }
140
    
Martyn Smith's avatar
Martyn Smith committed
141
142
143
144
145
146
147
148
149
150
    public static function get_all_fields() {
        return array(
            'firstname'       => 'text',
            'lastname'        => 'text',
            'studentid'       => 'text',
            'preferredname'   => 'text',
            'introduction'    => 'wysiwyg',
            'email'           => 'emaillist',
            'officialwebsite' => 'text',
            'personalwebsite' => 'text',
151
            'blogaddress'     => 'text',
Martyn Smith's avatar
Martyn Smith committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
            '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',
        );
    }

171
    public static function get_mandatory_fields() {
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
        $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
188
189
190
        return array(
            'firstname' => 'text', 
            'lastname'  => 'text', 
191
            'email'     => 'emaillist', 
Martyn Smith's avatar
Martyn Smith committed
192
        );
193
    }
194

195
    public static function get_public_fields() {
196
197
198
199
200
201
202
203
204
205
206
207
        $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;
208
209
    }

210
211
212
213
214
    public static function has_config() {
        return true;
    }

    public static function get_config_options() {
215
216
        $mandatory = self::get_mandatory_fields();
        $public = self::get_public_fields();
217
        $alwaysmandatory = self::get_always_mandatory_fields();
218
219
220
221
222
223
        $form = array(
            'name'       => 'profileprefs',
            'method'     => 'post', 
            'ajaxpost'   => true,
            'plugintype' => 'artefact',
            'pluginname' => 'internal',
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
            'renderer'   => 'multicolumntable',
            'submitfunction' => 'save_config_options',
            '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'),
                )
            ),
240
241
242
        );

        foreach (array_keys(self::get_all_fields()) as $field) {
243
244
            $form['elements'][$field . '_mandatory'] = array(
                'defaultvalue' => ((isset($mandatory[$field])) ? 'checked' : ''),
245
246
247
                'title'        => get_string($field, 'artefact.internal'),
                'type'         => 'checkbox',
            );
248
249
250
251
252
253
            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' : ''),
254
255
256
257
258
                'title'        => get_string($field, 'artefact.internal'),
                'type'         => 'checkbox',

            );
        }
259
260
261
262
263
264

        $form['elements']['emptyrow'] = array(
            'title' => '    ',
            'type' => 'html',
            'value' => '&nbsp;',
        );
265
        $currentwidth = get_config_plugin('artefact', 'internal', 'profileiconwidth');
266
267
268
269
270
        $form['elements']['profileiconwidth'] = array(
            'type' => 'text',
            'size' => 4,
            'suffix' => get_string('widthshort'),
            'title' => get_string('profileiconsize', 'artefact.internal'),
271
            'defaultvalue' => ((!empty($currentwidth)) ? $currentwidth : 100),
272
273
274
275
276
            'rules' => array(
                'required' => true,
                'integer'  => true,
            )
        );
277
        $currentheight = get_config_plugin('artefact', 'internal', 'profileiconheight');
278
279
280
281
282
        $form['elements']['profileiconheight'] = array(
            'type' => 'text',
            'suffix' => get_string('heightshort'),
            'size' => 4,
            'title' => get_string('profileiconsize', 'artefact.internal'),
283
            'defaultvalue' => ((!empty($currentheight)) ? $currentheight : 100),
284
285
286
287
288
289
            'rules' => array(
                'required' => true,
                'integer'  => true,
            )
        );

290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
        $form['elements']['submit'] = array(
            'type' => 'submit',
            'value' =>get_string('save')
        );
        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);
313
314
        set_config_plugin('artefact', 'internal', 'profileiconwidth', $values['profileiconwidth']);
        set_config_plugin('artefact', 'internal', 'profileiconheight', $values['profileiconheight']);
315
    }
316
317
}

318
319
320
321
class ArtefactTypeProfileField extends ArtefactTypeProfile {
    public static function collapse_config() {
        return 'profile';
    }
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

    /**
     * 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;
342
343
344
        $res->id         = $data->id;
        $res->title      = $data->title;
        $res->isartefact = true;
345
346
347
348
349
350
351
352
353
354
        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;
    }
355

356
357
358
}

class ArtefactTypeCachedProfileField extends ArtefactTypeProfileField {
359
360
    
    public function commit() {
361
        global $USER;
362
        parent::commit();
363
364
        $field = $this->get_artefact_type();
        set_field('usr', $field, $this->title, 'id', $this->owner);
365
        $USER->set($field, $this->title);
366
367
368
    }

    public function delete() {
369
        parent::delete();
370
371
372
373
374
375
        $field = $this->get_artefact_type();
        set_field('usr', $field, null, 'id', $this->owner);
    }

}

376
377
378
class ArtefactTypeFirstname extends ArtefactTypeCachedProfileField {}
class ArtefactTypeLastname extends ArtefactTypeCachedProfileField {}
class ArtefactTypePreferredname extends ArtefactTypeCachedProfileField {}
379
380
381
class ArtefactTypeEmail extends ArtefactTypeProfileField {
    public function commit() {

382
        parent::commit();
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402

        $email_record = get_record('artefact_internal_profile_email', 'owner', $this->owner, 'email', $this->title);
        // we've created a new artefact that doesn't have a profile email thingy.
        // we assume that it's a validated email, and set it to primary (if there isn't already one)
        if(!$email_record) {
            $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,
                )
            );
        }
    }
}
403
404
405

class ArtefactTypeStudentid extends ArtefactTypeProfileField {}
class ArtefactTypeIntroduction extends ArtefactTypeProfileField {}
406
class ArtefactTypeWebAddress extends ArtefactTypeProfileField {
407
    public function render($format, $options) {
408
409
        if (($format == FORMAT_ARTEFACT_LISTSELF || $format == FORMAT_ARTEFACT_RENDERFULL)
            && $options['link'] == true) {
410
                return make_link($this->title);
411
        }
412
        return parent::render($format, $options);
413
414
    }
}
415
416
class ArtefactTypeOfficialwebsite extends ArtefactTypeWebAddress {}
class ArtefactTypePersonalwebsite extends ArtefactTypeWebAddress {}
417
class ArtefactTypeBlogAddress extends ArtefactTypeProfileField {}
418
419
420
class ArtefactTypeAddress extends ArtefactTypeProfileField {}
class ArtefactTypeTown extends ArtefactTypeProfileField {}
class ArtefactTypeCity extends ArtefactTypeProfileField {}
421
422
class ArtefactTypeCountry extends ArtefactTypeProfileField {
    public function render($format, $options) {
423
        if ($format == FORMAT_ARTEFACT_LISTSELF || $format == FORMAT_ARTEFACT_RENDERFULL) {
424
425
426
            $countries = getoptions_country();
            return $countries[$this->title];
        }
427
        return parent::render($format, $options);
428
429
    }
}
430
431
432
433
434
435
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
436
437
438
439
440
441
class ArtefactTypeAimscreenname extends ArtefactTypeProfileField {}
class ArtefactTypeYahoochat extends ArtefactTypeProfileField {}
class ArtefactTypeSkypeusername extends ArtefactTypeProfileField {}
class ArtefactTypeJabberusername extends ArtefactTypeProfileField {}
class ArtefactTypeOccupation extends ArtefactTypeProfileField {}
class ArtefactTypeIndustry extends ArtefactTypeProfileField {}
442

443
444

?>