Skip to content

Commit

Permalink
MDL-56092 core_user: Changes in profilelib to support WS
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Oct 4, 2016
1 parent f106281 commit 3ee392c
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 10 deletions.
2 changes: 2 additions & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ information provided here is intended especially for developers.
* Action menus do_not_enhance() is deprecated, use a list of action_icon instead.
* The user_not_fully_set_up() function has a new $strict parameter (defaulting to true) in order to decide when
custom fields (and other checks) should be evaluated to determine if the user has been completely setup.
* profile_field_base class has new methods: get_field_config_for_external() and get_field_properties().
This two new methods should be implemented by profile field plugins to make them compatible with Web Services.

=== 3.1 ===

Expand Down
10 changes: 10 additions & 0 deletions user/profile/field/checkbox/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ public function display_data() {
return '<input disabled="disabled" type="checkbox" name="'.$this->inputname.'" '.$checked.' />';
}

/**
* Return the field type and null properties.
* This will be used for validating the data submitted by a user.
*
* @return array the param type and null property
* @since Moodle 3.2
*/
public function get_field_properties() {
return array(PARAM_BOOL, NULL_NOT_ALLOWED);
}
}


11 changes: 11 additions & 0 deletions user/profile/field/datetime/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,15 @@ public function display_data() {
public function is_empty() {
return empty($this->data);
}

/**
* Return the field type and null properties.
* This will be used for validating the data submitted by a user.
*
* @return array the param type and null property
* @since Moodle 3.2
*/
public function get_field_properties() {
return array(PARAM_INT, NULL_NOT_ALLOWED);
}
}
11 changes: 11 additions & 0 deletions user/profile/field/menu/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ public function convert_external_data($value) {
}
return $retval;
}

/**
* Return the field type and null properties.
* This will be used for validating the data submitted by a user.
*
* @return array the param type and null property
* @since Moodle 3.2
*/
public function get_field_properties() {
return array(PARAM_TEXT, NULL_NOT_ALLOWED);
}
}


10 changes: 10 additions & 0 deletions user/profile/field/text/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public function edit_field_add($mform) {
$mform->setType($this->inputname, PARAM_TEXT);
}

/**
* Return the field type and null properties.
* This will be used for validating the data submitted by a user.
*
* @return array the param type and null property
* @since Moodle 3.2
*/
public function get_field_properties() {
return array(PARAM_TEXT, NULL_NOT_ALLOWED);
}
}


10 changes: 10 additions & 0 deletions user/profile/field/textarea/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ public function display_data() {
return format_text($this->data, $this->dataformat, array('overflowdiv' => true));
}

/**
* Return the field type and null properties.
* This will be used for validating the data submitted by a user.
*
* @return array the param type and null property
* @since Moodle 3.2
*/
public function get_field_properties() {
return array(PARAM_RAW, NULL_NOT_ALLOWED);
}
}


66 changes: 56 additions & 10 deletions user/profile/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,28 @@ public function is_unique() {
public function is_signup_field() {
return (boolean)$this->field->signup;
}

/**
* Return the field settings suitable to be exported via an external function.
* By default it return all the field settings.
*
* @return array all the settings
* @since Moodle 3.2
*/
public function get_field_config_for_external() {
return (array) $this->field;
}

/**
* Return the field type and null properties.
* This will be used for validating the data submitted by a user.
*
* @return array the param type and null property
* @since Moodle 3.2
*/
public function get_field_properties() {
return array(PARAM_RAW, NULL_NOT_ALLOWED);
}
}

/**
Expand Down Expand Up @@ -528,13 +550,15 @@ function profile_display_fields($userid) {
}

/**
* Adds code snippet to a moodle form object for custom profile fields that
* should appear on the signup page
* @param moodleform $mform moodle form object
* Retrieves a list of profile fields that must be displayed in the sign-up form.
*
* @return array list of profile fields info
* @since Moodle 3.2
*/
function profile_signup_fields($mform) {
function profile_get_signup_fields() {
global $CFG, $DB;

$profilefields = array();
// Only retrieve required custom fields (with category information)
// results are sort by categories, then by fields.
$sql = "SELECT uf.id as fieldid, ic.id as categoryid, ic.name as categoryname, uf.datatype
Expand All @@ -543,17 +567,39 @@ function profile_signup_fields($mform) {
ON uf.categoryid = ic.id AND uf.signup = 1 AND uf.visible<>0
ORDER BY ic.sortorder ASC, uf.sortorder ASC";

if ( $fields = $DB->get_records_sql($sql)) {
if ($fields = $DB->get_records_sql($sql)) {
foreach ($fields as $field) {
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$fieldobject = new $newfield($field->fieldid);

$profilefields[] = (object) array(
'categoryid' => $field->categoryid,
'categoryname' => $field->categoryname,
'fieldid' => $field->fieldid,
'datatype' => $field->datatype,
'object' => $fieldobject
);
}
}
return $profilefields;
}

/**
* Adds code snippet to a moodle form object for custom profile fields that
* should appear on the signup page
* @param moodleform $mform moodle form object
*/
function profile_signup_fields($mform) {

if ($fields = profile_get_signup_fields()) {
foreach ($fields as $field) {
// Check if we change the categories.
if (!isset($currentcat) || $currentcat != $field->categoryid) {
$currentcat = $field->categoryid;
$mform->addElement('header', 'category_'.$field->categoryid, format_string($field->categoryname));
}
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->fieldid);
$formfield->edit_field($mform);
};
$field->object->edit_field($mform);
}
}
}
Expand Down

0 comments on commit 3ee392c

Please sign in to comment.