diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 811a4d741f4..62be46997cc 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -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 === diff --git a/user/profile/field/checkbox/field.class.php b/user/profile/field/checkbox/field.class.php index 530485783bf..7e08b368277 100644 --- a/user/profile/field/checkbox/field.class.php +++ b/user/profile/field/checkbox/field.class.php @@ -91,6 +91,16 @@ class profile_field_checkbox extends profile_field_base { return ''; } + /** + * 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); + } } diff --git a/user/profile/field/datetime/field.class.php b/user/profile/field/datetime/field.class.php index be4af3770a9..acdfe573635 100644 --- a/user/profile/field/datetime/field.class.php +++ b/user/profile/field/datetime/field.class.php @@ -122,4 +122,15 @@ class profile_field_datetime extends profile_field_base { 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); + } } diff --git a/user/profile/field/menu/field.class.php b/user/profile/field/menu/field.class.php index bfb41f8279c..071115114bd 100644 --- a/user/profile/field/menu/field.class.php +++ b/user/profile/field/menu/field.class.php @@ -165,6 +165,17 @@ class profile_field_menu extends profile_field_base { } 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); + } } diff --git a/user/profile/field/text/field.class.php b/user/profile/field/text/field.class.php index eda624a5781..3ac0fc20839 100644 --- a/user/profile/field/text/field.class.php +++ b/user/profile/field/text/field.class.php @@ -68,6 +68,16 @@ class profile_field_text extends profile_field_base { $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); + } } diff --git a/user/profile/field/textarea/field.class.php b/user/profile/field/textarea/field.class.php index dd00b3d25cf..b6478d290c9 100644 --- a/user/profile/field/textarea/field.class.php +++ b/user/profile/field/textarea/field.class.php @@ -81,6 +81,16 @@ class profile_field_textarea extends profile_field_base { 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); + } } diff --git a/user/profile/lib.php b/user/profile/lib.php index f712a97464a..4bb3f264a04 100644 --- a/user/profile/lib.php +++ b/user/profile/lib.php @@ -387,6 +387,28 @@ class profile_field_base { 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); + } } /** @@ -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 @@ -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); } } }