5 define ('PROFILE_VISIBLE_ALL', '2'); // only visible for users with moodle/user:update capability
6 define ('PROFILE_VISIBLE_PRIVATE', '1'); // either we are viewing our own profile or we have moodle/user:update capability
7 define ('PROFILE_VISIBLE_NONE', '0'); // only visible for moodle/user:update capability
12 * Base class for the cusomisable profile fields.
14 class profile_field_base
{
16 /// These 2 variables are really what we're interested in.
17 /// Everything else can be extracted from them
27 * @param integer id of the profile from the user_info_field table
28 * @param integer id of the user for whom we are displaying data
30 function profile_field_base($fieldid=0, $userid=0) {
33 $this->set_fieldid($fieldid);
34 $this->set_userid($userid);
39 /***** The following methods must be overwritten by child classes *****/
42 * Abstract method: Adds the profile field to the moodle form class
43 * @param form instance of the moodleform class
45 function edit_field_add(&$mform) {
46 error('This abstract method must be overriden');
50 /***** The following methods may be overwritten by child classes *****/
53 * Display the data for this field
55 function display_data() {
56 $options->para
= false;
57 return format_text($this->data
, FORMAT_MOODLE
, $options);
61 * Print out the form field in the edit profile page
62 * @param object instance of the moodleform class
65 function edit_field(&$mform) {
67 if ($this->field
->visible
!= PROFILE_VISIBLE_NONE
68 or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) {
70 $this->edit_field_add($mform);
71 $this->edit_field_set_default($mform);
72 $this->edit_field_set_required($mform);
73 $this->edit_field_set_locked($mform);
78 * Saves the data coming from form
79 * @param mixed data coming from the form
80 * @return mixed returns data id if success of db insert/update, false on fail, 0 if not permitted
82 function edit_save_data($usernew) {
84 if (!isset($usernew->{$this->inputname
})) {
85 // field not present in form, probably locked and invisible - skip it
89 $usernew->{$this->inputname
} = $this->edit_save_data_preprocess($usernew->{$this->inputname
});
92 $data->userid
= $usernew->id
;
93 $data->fieldid
= $this->field
->id
;
94 $data->data
= $usernew->{$this->inputname
};
96 if ($dataid = get_field('user_info_data', 'id', 'userid', $data->userid
, 'fieldid', $data->fieldid
)) {
98 if (!update_record('user_info_data', $data)) {
99 error('Error updating custom profile field!');
102 insert_record('user_info_data', $data);
107 * Validate the form field from profile page
108 * @return string contains error message otherwise NULL
110 function edit_validate_field($usernew) {
111 //no errors by default
116 * Sets the default data for the field in the form object
117 * @param object instance of the moodleform class
119 function edit_field_set_default(&$mform) {
120 if (!empty($default)) {
121 $mform->setDefault($this->inputname
, $this->field
->defaultdata
);
126 * Sets the required flag for the field in the form object
127 * @param object instance of the moodleform class
129 function edit_field_set_required(&$mform) {
130 if ($this->is_required() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) {
131 $mform->addRule($this->inputname
, get_string('required'), 'required', null, 'client');
136 * HardFreeze the field if locked.
137 * @param object instance of the moodleform class
139 function edit_field_set_locked(&$mform) {
140 if ($this->is_locked() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) {
141 $mform->hardFreeze($this->inputname
);
146 * Hook for child classess to process the data before it gets saved in database
150 function edit_save_data_preprocess($data) {
155 * Loads a user object with data for this field ready for the edit profile
157 * @param object a user object
159 function edit_load_user_data(&$user) {
160 if ($this->data
!== NULL) {
161 $user->{$this->inputname
} = $this->data
;
166 /***** The following methods generally should not be overwritten by child classes *****/
169 * Accessor method: set the userid for this instance
170 * @param integer id from the user table
172 function set_userid($userid) {
173 $this->userid
= $userid;
177 * Accessor method: set the fieldid for this instance
178 * @param integer id from the user_info_field table
180 function set_fieldid($fieldid) {
181 $this->fieldid
= $fieldid;
185 * Accessor method: Load the field record and user data associated with the
186 * object's fieldid and userid
188 function load_data() {
189 /// Load the field object
190 if (($this->fieldid
== 0) or (!($field = get_record('user_info_field', 'id', $this->fieldid
)))) {
192 $this->inputname
= '';
194 $this->field
= $field;
195 $this->inputname
= 'profile_field_'.$field->shortname
;
198 if (!empty($this->field
)) {
199 if ($datafield = get_field('user_info_data', 'data', 'userid', $this->userid
, 'fieldid', $this->fieldid
)) {
200 $this->data
= $datafield;
202 $this->data
= $this->field
->defaultdata
;
210 * Check if the field data is visible to the current user
213 function is_visible() {
216 switch ($this->field
->visible
) {
217 case PROFILE_VISIBLE_ALL
:
219 case PROFILE_VISIBLE_PRIVATE
:
220 return ($this->userid
== $USER->id
);
222 return has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM
, SITEID
));
227 * Check if the field data is considered empty
230 function is_empty() {
231 return ( ($this->data
!= '0') and empty($this->data
));
235 * Check if the field is required on the edit profile page
238 function is_required() {
239 return (boolean
)$this->field
->required
;
243 * Check if the field is locked on the edit profile page
246 function is_locked() {
247 return (boolean
)$this->field
->locked
;
250 } /// End of class definition
253 /***** General purpose functions for customisable user profiles *****/
255 function profile_load_data(&$user) {
258 if ($fields = get_records_select('user_info_field')) {
259 foreach ($fields as $field) {
260 require_once($CFG->dirroot
.'/user/profile/field/'.$field->datatype
.'/field.class.php');
261 $newfield = 'profile_field_'.$field->datatype
;
262 $formfield = new $newfield($field->id
, $user->id
);
263 $formfield->edit_load_user_data($user);
269 * Print out the customisable categories and fields for a users profile
270 * @param object instance of the moodleform class
272 function profile_definition(&$mform) {
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);
291 function profile_definition_after_data(&$mform) {
294 if ($fields = get_records('user_info_field')) {
295 foreach ($fields as $field) {
296 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
297 $newfield = 'profile_field_'.$field->datatype;
298 $formfield = new $newfield($field->id);
299 //TODO add: method into field class
305 function profile_validation($usernew) {
309 if ($fields = get_records('user_info_field')) {
310 foreach ($fields as $field) {
311 require_once($CFG->dirroot
.'/user/profile/field/'.$field->datatype
.'/field.class.php');
312 $newfield = 'profile_field_'.$field->datatype
;
313 $formfield = new $newfield($field->id
, $usernew->id
);
314 $err +
= $formfield->edit_validate_field($usernew);
320 function profile_save_data($usernew) {
323 if ($fields = get_records_select('user_info_field')) {
324 foreach ($fields as $field) {
325 require_once($CFG->dirroot
.'/user/profile/field/'.$field->datatype
.'/field.class.php');
326 $newfield = 'profile_field_'.$field->datatype
;
327 $formfield = new $newfield($field->id
, $usernew->id
);
328 $formfield->edit_save_data($usernew);
333 function profile_display_fields($userid) {
336 if ($categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
337 foreach ($categories as $category) {
338 if ($fields = get_records_select('user_info_field', "categoryid=$category->id", 'sortorder ASC')) {
339 foreach ($fields as $field) {
340 require_once($CFG->dirroot
.'/user/profile/field/'.$field->datatype
.'/field.class.php');
341 $newfield = 'profile_field_'.$field->datatype
;
342 $formfield = new $newfield($field->id
, $userid);
343 if ($formfield->is_visible() and !$formfield->is_empty()) {
344 print_row(s($formfield->field
->name
.':'), $formfield->display_data());