~ubuntu-branches/ubuntu/jaunty/moodle/jaunty

« back to all changes in this revision

Viewing changes to user/profile/lib.php

  • Committer: Bazaar Package Importer
  • Author(s): Jordan Mantha, Matt Oquist
  • Date: 2009-02-25 15:16:22 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090225151622-0ekt1liwhv2obfza
Tags: 1.9.4.dfsg-0ubuntu1
* Merge with Debian git (Closes LP: #322961, #239481, #334611):
  - use Ubuntu's smarty lib directory for linking
  - use internal yui library 
  - add update-notifier support back in

[Matt Oquist]
  * renamed prerm script
  * significantly rewrote postinst and other maintainer scripts to improve
    user experience and package maintainability
    (Closes LP: #225662, #325450, #327843, #303078, #234609)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php //$Id: lib.php,v 1.16.2.3 2007/04/18 02:08:50 ikawhero Exp $
 
1
<?php //$Id: lib.php,v 1.23.2.11 2009/01/14 10:06:06 thepurpleblob Exp $
2
2
 
3
3
/// Some constants
4
4
 
65
65
    function edit_field(&$mform) {
66
66
 
67
67
        if ($this->field->visible != PROFILE_VISIBLE_NONE
68
 
          or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
 
68
          or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
69
69
 
70
70
            $this->edit_field_add($mform);
71
71
            $this->edit_field_set_default($mform);
72
72
            $this->edit_field_set_required($mform);
73
73
            $this->edit_field_set_locked($mform);
 
74
            return true;
74
75
        }
 
76
        return false;
75
77
    }
76
78
 
77
79
    /**
108
110
     * @return  string  contains error message otherwise NULL
109
111
     **/
110
112
    function edit_validate_field($usernew) {
111
 
        //no errors by default
112
 
        return array();
 
113
        $errors = array();
 
114
        /// Check for uniqueness of data if required
 
115
        if ($this->is_unique()) {
 
116
            if ($userid = get_field('user_info_data', 'userid', 'fieldid', $this->field->id, 'data', $usernew->{$this->inputname})) {
 
117
                if ($userid != $usernew->id) {
 
118
                    $errors["{$this->inputname}"] = get_string('valuealreadyused');
 
119
                }
 
120
            }
 
121
        }
 
122
        return $errors;
113
123
    }
114
124
 
115
125
    /**
127
137
     * @param   object   instance of the moodleform class
128
138
     */
129
139
    function edit_field_set_required(&$mform) {
130
 
        if ($this->is_required() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
 
140
        if ($this->is_required() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
131
141
            $mform->addRule($this->inputname, get_string('required'), 'required', null, 'client');
132
142
        }
133
143
    }
137
147
     * @param   object   instance of the moodleform class
138
148
     */
139
149
    function edit_field_set_locked(&$mform) {
140
 
        if ($this->is_locked() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
 
150
        if ($this->is_locked() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
141
151
            $mform->hardFreeze($this->inputname);
 
152
            $mform->setConstant($this->inputname, $this->data);
142
153
        }
143
154
    }
144
155
 
162
173
        }
163
174
    }
164
175
 
 
176
    /**
 
177
     * Check if the field data should be loaded into the user object
 
178
     * By default it is, but for field types where the data may be potentially
 
179
     * large, the child class should override this and return false
 
180
     * @return boolean
 
181
     */
 
182
    function is_user_object_data() {
 
183
        return true;
 
184
    }
 
185
 
165
186
 
166
187
/***** The following methods generally should not be overwritten by child classes *****/
167
188
   
217
238
            case PROFILE_VISIBLE_ALL:
218
239
                return true;
219
240
            case PROFILE_VISIBLE_PRIVATE:
220
 
                return ($this->userid == $USER->id);
 
241
                if ($this->userid == $USER->id) {
 
242
                    return true;
 
243
                } else {
 
244
                    return has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
 
245
                }
221
246
            default:
222
 
                return has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID));
 
247
                return has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
223
248
        }
224
249
    }
225
250
 
247
272
        return (boolean)$this->field->locked;
248
273
    }
249
274
 
 
275
    /**
 
276
     * Check if the field data should be unique
 
277
     * @return   boolean
 
278
     */
 
279
    function is_unique() {
 
280
        return (boolean)$this->field->forceunique;
 
281
    }
 
282
 
 
283
    /**
 
284
     * Check if the field should appear on the signup page
 
285
     * @return   boolean
 
286
     */
 
287
    function is_signup_field() {
 
288
        return (boolean)$this->field->signup;
 
289
    }
 
290
     
 
291
 
250
292
} /// End of class definition
251
293
 
252
294
 
272
314
function profile_definition(&$mform) {
273
315
    global $CFG;
274
316
 
275
 
    if ($categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
276
 
        foreach ($categories as $category) {
277
 
            if ($fields = get_records_select('user_info_field', "categoryid=$category->id", 'sortorder ASC')) {
278
 
                $mform->addElement('header', 'category_'.$category->id, format_string($category->name));
279
 
                foreach ($fields as $field) {
280
 
                    require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
281
 
                    $newfield = 'profile_field_'.$field->datatype;
282
 
                    $formfield = new $newfield($field->id);
283
 
                    $formfield->edit_field($mform);
284
 
 
 
317
        // if user is "admin" fields are displayed regardless
 
318
        $update = has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
 
319
 
 
320
        if ($categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
 
321
            foreach ($categories as $category) {
 
322
                if ($fields = get_records_select('user_info_field', "categoryid=$category->id", 'sortorder ASC')) {
 
323
                    
 
324
                    // check first if *any* fields will be displayed
 
325
                    $display = false;
 
326
                    foreach ($fields as $field) {
 
327
                        if ($field->visible != PROFILE_VISIBLE_NONE) {
 
328
                            $display = true;
 
329
                        }
 
330
                    }
 
331
 
 
332
                    // display the header and the fields
 
333
                    if ($display or $update) {
 
334
                        $mform->addElement('header', 'category_'.$category->id, format_string($category->name));
 
335
                        foreach ($fields as $field) {
 
336
                            require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
 
337
                            $newfield = 'profile_field_'.$field->datatype;
 
338
                            $formfield = new $newfield($field->id);
 
339
                            $formfield->edit_field($mform);
 
340
                        }
 
341
                    }
285
342
                }
286
343
            }
287
344
        }
288
345
    }
289
 
}
290
346
 
291
347
function profile_definition_after_data(&$mform) {
292
348
    global $CFG;
302
358
    }*/
303
359
}
304
360
 
305
 
function profile_validation($usernew) {
 
361
function profile_validation($usernew, $files) {
306
362
    global $CFG;
307
363
 
308
364
    $err = array();
311
367
            require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
312
368
            $newfield = 'profile_field_'.$field->datatype;
313
369
            $formfield = new $newfield($field->id, $usernew->id);
314
 
            $err += $formfield->edit_validate_field($usernew);
 
370
            $err += $formfield->edit_validate_field($usernew, $files);
315
371
        }
316
372
    }
317
373
    return $err;
349
405
    }
350
406
}
351
407
 
352
 
 
 
408
/**
 
409
 * Adds code snippet to a moodle form object for custom profile fields that
 
410
 * should appear on the signup page
 
411
 * @param  object  moodle form object
 
412
 */
 
413
function profile_signup_fields(&$mform) {
 
414
    global $CFG;
 
415
 
 
416
    //only retrieve required custom fields (with category information)
 
417
    //results are sort by categories, then by fields
 
418
    $sql = "SELECT uf.id as fieldid, ic.id as categoryid, ic.name as categoryname, uf.datatype
 
419
                FROM ".$CFG->prefix."user_info_field uf
 
420
                JOIN ".$CFG->prefix."user_info_category ic
 
421
                ON uf.categoryid = ic.id AND uf.signup = 1 AND uf.visible<>0
 
422
                ORDER BY ic.sortorder ASC, uf.sortorder ASC";
 
423
 
 
424
    if ( $fields = get_records_sql($sql)) {
 
425
        foreach ($fields as $field) {
 
426
            //check if we change the categories
 
427
            if (!isset($currentcat) || $currentcat != $field->categoryid) {
 
428
                 $currentcat = $field->categoryid;
 
429
                 $mform->addElement('header', 'category_'.$field->categoryid, format_string($field->categoryname));
 
430
            }
 
431
            require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
 
432
            $newfield = 'profile_field_'.$field->datatype;
 
433
            $formfield = new $newfield($field->fieldid);
 
434
            $formfield->edit_field($mform);
 
435
        }
 
436
    }
 
437
}
 
438
 
 
439
/**
 
440
 * Returns an object with the custom profile fields set for the given user
 
441
 * @param  integer  userid
 
442
 * @return  object
 
443
 */
 
444
function profile_user_record($userid) {
 
445
    global $CFG;
 
446
 
 
447
    $user = new object();
 
448
 
 
449
    if ($fields = get_records_select('user_info_field')) {
 
450
        foreach ($fields as $field) {
 
451
            require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
 
452
            $newfield = 'profile_field_'.$field->datatype;
 
453
            $formfield = new $newfield($field->id, $userid);
 
454
            if ($formfield->is_user_object_data()) $user->{$field->shortname} = $formfield->data;
 
455
        }
 
456
    }
 
457
 
 
458
    return $user;
 
459
}
353
460
 
354
461
 
355
462
?>