Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/user/profile/lib.php
Go to the documentation of this file.
00001 <?php
00002 
00004 
00005 define ('PROFILE_VISIBLE_ALL',     '2'); // only visible for users with moodle/user:update capability
00006 define ('PROFILE_VISIBLE_PRIVATE', '1'); // either we are viewing our own profile or we have moodle/user:update capability
00007 define ('PROFILE_VISIBLE_NONE',    '0'); // only visible for moodle/user:update capability
00008 
00009 
00010 
00014 class profile_field_base {
00015 
00018     var $fieldid;
00019     var $userid;
00020 
00021     var $field;
00022     var $inputname;
00023     var $data;
00024     var $dataformat;
00025 
00031     function profile_field_base($fieldid=0, $userid=0) {
00032         global $USER;
00033 
00034         $this->set_fieldid($fieldid);
00035         $this->set_userid($userid);
00036         $this->load_data();
00037     }
00038 
00039 
00040 /***** The following methods must be overwritten by child classes *****/
00041 
00046     function edit_field_add(&$mform) {
00047         print_error('mustbeoveride', 'debug', '', 'edit_field_add');
00048     }
00049 
00050 
00051 /***** The following methods may be overwritten by child classes *****/
00052 
00056     function display_data() {
00057         $options = new stdClass();
00058         $options->para = false;
00059         return format_text($this->data, FORMAT_MOODLE, $options);
00060     }
00061 
00067     function edit_field(&$mform) {
00068 
00069         if ($this->field->visible != PROFILE_VISIBLE_NONE
00070           or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
00071 
00072             $this->edit_field_add($mform);
00073             $this->edit_field_set_default($mform);
00074             $this->edit_field_set_required($mform);
00075             return true;
00076         }
00077         return false;
00078     }
00079 
00085     function edit_after_data(&$mform) {
00086 
00087         if ($this->field->visible != PROFILE_VISIBLE_NONE
00088           or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
00089             $this->edit_field_set_locked($mform);
00090             return true;
00091         }
00092         return false;
00093     }
00094 
00100     function edit_save_data($usernew) {
00101         global $DB;
00102 
00103         if (!isset($usernew->{$this->inputname})) {
00104             // field not present in form, probably locked and invisible - skip it
00105             return;
00106         }
00107 
00108         $data = new stdClass();
00109 
00110         $usernew->{$this->inputname} = $this->edit_save_data_preprocess($usernew->{$this->inputname}, $data);
00111 
00112         $data->userid  = $usernew->id;
00113         $data->fieldid = $this->field->id;
00114         $data->data    = $usernew->{$this->inputname};
00115 
00116         if ($dataid = $DB->get_field('user_info_data', 'id', array('userid'=>$data->userid, 'fieldid'=>$data->fieldid))) {
00117             $data->id = $dataid;
00118             $DB->update_record('user_info_data', $data);
00119         } else {
00120             $DB->insert_record('user_info_data', $data);
00121         }
00122     }
00123 
00128     function edit_validate_field($usernew) {
00129         global $DB;
00130 
00131         $errors = array();
00133         if ($this->is_unique()) {
00134             $value = (is_array($usernew->{$this->inputname}) and isset($usernew->{$this->inputname}['text'])) ? $usernew->{$this->inputname}['text'] : $usernew->{$this->inputname};
00135             $data = $DB->get_records_sql('
00136                     SELECT id, userid
00137                       FROM {user_info_data}
00138                      WHERE fieldid = ?
00139                        AND ' . $DB->sql_compare_text('data', 255) . ' = ' . $DB->sql_compare_text('?', 255),
00140                     array($this->field->id, $value));
00141             if ($data) {
00142                 $existing = false;
00143                 foreach ($data as $v) {
00144                     if ($v->userid == $usernew->id) {
00145                         $existing = true;
00146                         break;
00147                     }
00148                 }
00149                 if (!$existing) {
00150                     $errors[$this->inputname] = get_string('valuealreadyused');
00151                 }
00152             }
00153         }
00154         return $errors;
00155     }
00156 
00161     function edit_field_set_default(&$mform) {
00162         if (!empty($default)) {
00163             $mform->setDefault($this->inputname, $this->field->defaultdata);
00164         }
00165     }
00166 
00171     function edit_field_set_required(&$mform) {
00172         if ($this->is_required() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
00173             $mform->addRule($this->inputname, get_string('required'), 'required', null, 'client');
00174         }
00175     }
00176 
00181     function edit_field_set_locked(&$mform) {
00182         if (!$mform->elementExists($this->inputname)) {
00183             return;
00184         }
00185         if ($this->is_locked() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
00186             $mform->hardFreeze($this->inputname);
00187             $mform->setConstant($this->inputname, $this->data);
00188         }
00189     }
00190 
00197     function edit_save_data_preprocess($data, &$datarecord) {
00198         return $data;
00199     }
00200 
00206     function edit_load_user_data(&$user) {
00207         if ($this->data !== NULL) {
00208             $user->{$this->inputname} = $this->data;
00209         }
00210     }
00211 
00218     function is_user_object_data() {
00219         return true;
00220     }
00221 
00222 
00223 /***** The following methods generally should not be overwritten by child classes *****/
00224 
00229     function set_userid($userid) {
00230         $this->userid = $userid;
00231     }
00232 
00237     function set_fieldid($fieldid) {
00238         $this->fieldid = $fieldid;
00239     }
00240 
00245     function load_data() {
00246         global $DB;
00247 
00249         if (($this->fieldid == 0) or (!($field = $DB->get_record('user_info_field', array('id'=>$this->fieldid))))) {
00250             $this->field = NULL;
00251             $this->inputname = '';
00252         } else {
00253             $this->field = $field;
00254             $this->inputname = 'profile_field_'.$field->shortname;
00255         }
00256 
00257         if (!empty($this->field)) {
00258             if ($data = $DB->get_record('user_info_data', array('userid'=>$this->userid, 'fieldid'=>$this->fieldid), 'data, dataformat')) {
00259                 $this->data = $data->data;
00260                 $this->dataformat = $data->dataformat;
00261             } else {
00262                 $this->data = $this->field->defaultdata;
00263                 $this->dataformat = FORMAT_HTML;
00264             }
00265         } else {
00266             $this->data = NULL;
00267         }
00268     }
00269 
00274     function is_visible() {
00275         global $USER;
00276 
00277         switch ($this->field->visible) {
00278             case PROFILE_VISIBLE_ALL:
00279                 return true;
00280             case PROFILE_VISIBLE_PRIVATE:
00281                 if ($this->userid == $USER->id) {
00282                     return true;
00283                 } else {
00284                     return has_capability('moodle/user:viewalldetails',
00285                             get_context_instance(CONTEXT_USER, $this->userid));
00286                 }
00287             default:
00288                 return has_capability('moodle/user:viewalldetails',
00289                         get_context_instance(CONTEXT_USER, $this->userid));
00290         }
00291     }
00292 
00297     function is_empty() {
00298         return ( ($this->data != '0') and empty($this->data));
00299     }
00300 
00305     function is_required() {
00306         return (boolean)$this->field->required;
00307     }
00308 
00313     function is_locked() {
00314         return (boolean)$this->field->locked;
00315     }
00316 
00321     function is_unique() {
00322         return (boolean)$this->field->forceunique;
00323     }
00324 
00329     function is_signup_field() {
00330         return (boolean)$this->field->signup;
00331     }
00332 
00333 
00334 } 
00335 
00336 
00337 /***** General purpose functions for customisable user profiles *****/
00338 
00339 function profile_load_data(&$user) {
00340     global $CFG, $DB;
00341 
00342     if ($fields = $DB->get_records('user_info_field')) {
00343         foreach ($fields as $field) {
00344             require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
00345             $newfield = 'profile_field_'.$field->datatype;
00346             $formfield = new $newfield($field->id, $user->id);
00347             $formfield->edit_load_user_data($user);
00348         }
00349     }
00350 }
00351 
00356 function profile_definition(&$mform) {
00357     global $CFG, $DB;
00358 
00359     // if user is "admin" fields are displayed regardless
00360     $update = has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
00361 
00362     if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
00363         foreach ($categories as $category) {
00364             if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) {
00365 
00366                 // check first if *any* fields will be displayed
00367                 $display = false;
00368                 foreach ($fields as $field) {
00369                     if ($field->visible != PROFILE_VISIBLE_NONE) {
00370                         $display = true;
00371                     }
00372                 }
00373 
00374                 // display the header and the fields
00375                 if ($display or $update) {
00376                     $mform->addElement('header', 'category_'.$category->id, format_string($category->name));
00377                     foreach ($fields as $field) {
00378                         require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
00379                         $newfield = 'profile_field_'.$field->datatype;
00380                         $formfield = new $newfield($field->id);
00381                         $formfield->edit_field($mform);
00382                     }
00383                 }
00384             }
00385         }
00386     }
00387 }
00388 
00389 function profile_definition_after_data(&$mform, $userid) {
00390     global $CFG, $DB;
00391 
00392     $userid = ($userid < 0) ? 0 : (int)$userid;
00393 
00394     if ($fields = $DB->get_records('user_info_field')) {
00395         foreach ($fields as $field) {
00396             require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
00397             $newfield = 'profile_field_'.$field->datatype;
00398             $formfield = new $newfield($field->id, $userid);
00399             $formfield->edit_after_data($mform);
00400         }
00401     }
00402 }
00403 
00404 function profile_validation($usernew, $files) {
00405     global $CFG, $DB;
00406 
00407     $err = array();
00408     if ($fields = $DB->get_records('user_info_field')) {
00409         foreach ($fields as $field) {
00410             require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
00411             $newfield = 'profile_field_'.$field->datatype;
00412             $formfield = new $newfield($field->id, $usernew->id);
00413             $err += $formfield->edit_validate_field($usernew, $files);
00414         }
00415     }
00416     return $err;
00417 }
00418 
00419 function profile_save_data($usernew) {
00420     global $CFG, $DB;
00421 
00422     if ($fields = $DB->get_records('user_info_field')) {
00423         foreach ($fields as $field) {
00424             require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
00425             $newfield = 'profile_field_'.$field->datatype;
00426             $formfield = new $newfield($field->id, $usernew->id);
00427             $formfield->edit_save_data($usernew);
00428         }
00429     }
00430 }
00431 
00432 function profile_display_fields($userid) {
00433     global $CFG, $USER, $DB;
00434 
00435     if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
00436         foreach ($categories as $category) {
00437             if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) {
00438                 foreach ($fields as $field) {
00439                     require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
00440                     $newfield = 'profile_field_'.$field->datatype;
00441                     $formfield = new $newfield($field->id, $userid);
00442                     if ($formfield->is_visible() and !$formfield->is_empty()) {
00443                         print_row(format_string($formfield->field->name.':'), $formfield->display_data());
00444                     }
00445                 }
00446             }
00447         }
00448     }
00449 }
00450 
00456 function profile_signup_fields(&$mform) {
00457     global $CFG, $DB;
00458 
00459      //only retrieve required custom fields (with category information)
00460     //results are sort by categories, then by fields
00461     $sql = "SELECT uf.id as fieldid, ic.id as categoryid, ic.name as categoryname, uf.datatype
00462                 FROM {user_info_field} uf
00463                 JOIN {user_info_category} ic
00464                 ON uf.categoryid = ic.id AND uf.signup = 1 AND uf.visible<>0
00465                 ORDER BY ic.sortorder ASC, uf.sortorder ASC";
00466 
00467     if ( $fields = $DB->get_records_sql($sql)) {
00468         foreach ($fields as $field) {
00469             //check if we change the categories
00470             if (!isset($currentcat) || $currentcat != $field->categoryid) {
00471                  $currentcat = $field->categoryid;
00472                  $mform->addElement('header', 'category_'.$field->categoryid, format_string($field->categoryname));
00473             }
00474             require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
00475             $newfield = 'profile_field_'.$field->datatype;
00476             $formfield = new $newfield($field->fieldid);
00477             $formfield->edit_field($mform);
00478         }
00479     }
00480 }
00481 
00487 function profile_user_record($userid) {
00488     global $CFG, $DB;
00489 
00490     $usercustomfields = new stdClass();
00491 
00492     if ($fields = $DB->get_records('user_info_field')) {
00493         foreach ($fields as $field) {
00494             require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
00495             $newfield = 'profile_field_'.$field->datatype;
00496             $formfield = new $newfield($field->id, $userid);
00497             if ($formfield->is_user_object_data()) {
00498                 $usercustomfields->{$field->shortname} = $formfield->data;
00499             }
00500         }
00501     }
00502 
00503     return $usercustomfields;
00504 }
00505 
00516 function profile_load_custom_fields(&$user) {
00517     $user->profile = (array)profile_user_record($user->id);
00518 }
00519 
00520 
 All Data Structures Namespaces Files Functions Variables Enumerations