Skip to content

Commit

Permalink
Customisable user profile fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ikawhero committed Jan 5, 2007
1 parent d69e9c1 commit 6b64d3b
Show file tree
Hide file tree
Showing 3 changed files with 317 additions and 0 deletions.
52 changes: 52 additions & 0 deletions user/profile/field/menu/field.class.php
@@ -0,0 +1,52 @@
<?php //$Id$

class profile_field_menu extends profile_field_base {

function init() {
/// Param 1 for menu type is the options
if (($options = explode("\n", $this->field->param1)) === false) {
$options = array();
}
$this->options = $options;
}

function display_field_add(&$form) {
/// Create the form field
$form->addElement('select', $this->fieldname, $this->field->name, $this->options);
$form->setType($this->fieldname, PARAM_INT);
}

/// Override base class method
function display_field_default(&$form) {
/// Default data is either what user has already set othewise the default value for the field othewise nothing
if (!($default = get_field('user_info_data', 'data', 'userid', $this->userid, 'fieldid', $this->field->id))) {
$default = (empty($this->field->defaultdata)) ? '' : $this->field->defaultdata;
}
if (($defaultkey = array_search($default, $this->options)) === NULL) {
$defaultkey = 0;
}
$form->setDefault($this->fieldname, $defaultkey);
}

function set_data_type() {
$this->datatype = 'menu';
}

function validate_data($data) {
if ($data >= count($this->options)) {
return get_string('invaliddata');
} else {
return '';
}
}

function save_data_preprocess($data) {
if (!isset($this->options[$data])) { /// validate_data should already have caught this
return '';
} else {
return $this->options[$data];
}
}
}

?>
22 changes: 22 additions & 0 deletions user/profile/field/text/field.class.php
@@ -0,0 +1,22 @@
<?php //$Id$

class profile_field_text extends profile_field_base {

function display_field_add(&$form) {
/// Param 1 for text type is the size of the field
$size = (empty($this->field->param1)) ? '30' : $this->field->param1;

/// Param 2 for text type is the maxlength of the field
$maxlength = (empty($this->field->param2)) ? '254' : $this->field->param2;

/// Create the form field
$form->addElement('text', $this->fieldname, $this->field->name, 'maxlength="'.$maxlength.'" size="'.$size.'" ');
$form->setType($this->fieldname, PARAM_MULTILANG);
}

function set_data_type() {
$this->datatype = 'text';
}
}

?>
243 changes: 243 additions & 0 deletions user/profile/lib.php
@@ -0,0 +1,243 @@
<?php //$Id$

/// Some constants

define ('PROFILE_REQUIRED_YES', '1');
define ('PROFILE_REQUIRED_NO', '0');

define ('PROFILE_VISIBLE_ALL', '2');
define ('PROFILE_VISIBLE_PRIVATE', '1');
define ('PROFILE_VISIBLE_NONE', '0');

define ('PROFILE_LOCKED_YES', '1');
define ('PROFILE_LOCKED_NO', '0');



class profile_field_base {

var $datatype = ''; /// data type of this field
var $fieldid = 0; /// id from user_info_field table
var $dataid = 0; /// id from user_info_data table
var $userid = 0; /// id from the user table
var $field = null; /// a copy of the field information
var $fieldname = ''; /// form name of the field


/***** The following methods must be overwritten in the child classes *****/


/**
* Set the data type for this profile field
*/
function set_data_type() {
$this->datatype = 'unknown';
}

/**
* Adds the profile field to the moodle form class
* @param form instance of the moodleform class
*/
function display_field_add(&$form) {
/// Add the element to the form class
/// By default we add a static field
$form->addElement('static', $this->fieldname, $this->field->name, '');
}

/**
* Validate the form field from profile page
* @return string contains error message otherwise NULL
**/
function validate_profile_field ($data) {
return NULL;
}


/***** The following methods may be overwritten by child classes *****/

/**
* Print out the form field in the profile page
* @param form instance of the moodleform class
* $return boolean
*/
function display_field (&$form) {
if (empty($this->field)) {
return false;
}

/// Check that this field is visible to current user
if (!$this->_is_visible()) {
return false;
}

$this->display_field_add($form);
$this->display_field_lock($form);
$this->display_field_default($form);
$this->display_field_required($form);

return true;
}


/// Constructor function
function profile_field_base($fieldid=0, $userid=0, $dataid=0) {
global $USER;

/// If userid is empty, assume the current user
if (empty($userid)) $userid = $USER->id;

/// If dataid is not set we can set it from fieldid and userid
if (empty($dataid) and !empty($fieldid) and !empty($userid)) {
if (($dataid = get_field('user_info_data', 'id', 'fieldid', $fieldid, 'userid', $userid)) === false) {
$dataid = 0;
}
}

/// If the fieldid is set then we can automatically set the datatype
if (!empty($fieldid)) {
$field = get_record('user_info_field', 'id', $fieldid);
$datatype = $field->datatype;
} else {
$datatype = 'unknown';
$field = null;
}

$this->fieldid = $fieldid;
$this->userid = $userid;
$this->dataid = $dataid;
$this->datatype = $datatype;
$this->field = $field;
$this->fieldname = $this->datatype.'_'.$this->field->id; /// hack to avoid using integers as the field name

/// Post setup processing
$this->init();
}

/**
* A hook for child classes to perform any post-setup processes
*/
function init() {
/// do nothing - overwrite if necessary
}


/// Prints out the form for creating a new profile field
function edit_new_field () {

}

/// Removes a profile field and all data associated with it
function edit_remove_field () {

}

/**
* Validates the data coming from form
* @param mixed data from the form
* @return string error message
*/
function validate_data ($data) {
return '';
}

/**
* Saves the data coming from form
* @param mixed data coming from the form
* @return mixed returns data id if success of db insert/update, false on fail, 0 if not permitted
*/
function save_data ($data) {
if ($this->_is_visible() and !$this->_is_locked()) { /// check that we have permission

$data = $this->save_data_preprocess($data);

unset($datarecord);
$datarecord->userid = $this->userid;
$datarecord->fieldid = $this->fieldid;
$datarecord->data = $data;

if ($this->dataid == 0) { /// inserting a new record
if ($ret = insert_record('user_info_data', $datarecord)) {
$this->dataid = $ret;
}
} else {
$datarecord->id = $this->dataid;
if (update_record('user_info_data', $datarecord)) {
$ret = $this->dataid;
} else {
$ret = false;
}
}

} else {
$ret = 0;
}

return $ret;
}

/**
* Hook for child classess to process the data before it gets saved in database
* @param mixed
* @return mixed
*/
function save_data_preprocess($data) {
return $data;
}

function display_field_lock (&$form) {
$form->disabledIf($this->fieldname, $this->_is_locked(), true);
}

function display_field_default(&$form) {
if (!($default = get_field('user_info_data', 'data', 'userid', $this->userid, 'fieldid', $this->field->id))) {
$default = (empty($this->field->defaultdata)) ? '' : $this->field->defaultdata;
}
$form->setDefault($this->fieldname, $default);
}

function display_field_required(&$form) {
if ($this->_is_required()) {
$form->addRule($this->fieldname, get_string('required'), 'required', null, 'client');
}
}

/***** The following methods should never be overwritten *****/

/**
* Check if the current field is visible to the current user
* @return boolean
*/
function _is_visible() {
global $USER;
/* Can we see this field? Choices are:
1 - VISIBLE ALL;
2 - VISIBLE PRIVATE - either we are viewing our own profile or we have required capability;
3 - VISIBLE NONE - we have the required capability
*/
return ( ($this->field->visible === PROFILE_VISIBLE_ALL)
or ( ($this->field->visible === PROFILE_VISIBLE_PRIVATE)
and ($this->userid == $USER->id) )
or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID)) );
}

/**
* Check if the current field is locked to the current user
* @return boolean
*/
function _is_locked() {
return ( ($this->field->locked == PROFILE_LOCKED_YES)
and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID)));
}

/**
* Check if the current field is required
* @return boolean
*/
function _is_required() {
return ($this->field->required == PROFILE_REQUIRED_YES);
}


} /// End of class definition

?>

0 comments on commit 6b64d3b

Please sign in to comment.