function profile_edit_field($id, $datatype, $redirect) { global $CFG, $DB, $OUTPUT, $PAGE; if (!($field = $DB->get_record('user_info_field', array('id' => $id)))) { $field = new stdClass(); $field->datatype = $datatype; $field->description = ''; $field->descriptionformat = FORMAT_HTML; $field->defaultdata = ''; $field->defaultdataformat = FORMAT_HTML; } // Clean and prepare description for the editor $field->description = clean_text($field->description, $field->descriptionformat); $field->description = array('text' => $field->description, 'format' => $field->descriptionformat, 'itemid' => 0); require_once 'index_field_form.php'; $fieldform = new field_form(null, $field->datatype); // Convert the data format for if (is_array($fieldform->editors())) { foreach ($fieldform->editors() as $editor) { if (isset($field->{$editor})) { $field->{$editor} = clean_text($field->{$editor}, $field->{$editor . 'format'}); $field->{$editor} = array('text' => $field->{$editor}, 'format' => $field->{$editor . 'format'}, 'itemid' => 0); } } } $fieldform->set_data($field); if ($fieldform->is_cancelled()) { redirect($redirect); } else { if ($data = $fieldform->get_data()) { require_once $CFG->dirroot . '/user/profile/field/' . $datatype . '/define.class.php'; $newfield = 'profile_define_' . $datatype; $formfield = new $newfield(); // Collect the description and format back into the proper data structure from the editor // Note: This field will ALWAYS be an editor $data->descriptionformat = $data->description['format']; $data->description = $data->description['text']; // Check whether the default data is an editor, this is (currently) only the // textarea field type if (is_array($data->defaultdata) && array_key_exists('text', $data->defaultdata)) { // Collect the default data and format back into the proper data structure from the editor $data->defaultdataformat = $data->defaultdata['format']; $data->defaultdata = $data->defaultdata['text']; } // Convert the data format for if (is_array($fieldform->editors())) { foreach ($fieldform->editors() as $editor) { if (isset($field->{$editor})) { $field->{$editor . 'format'} = $field->{$editor}['format']; $field->{$editor} = $field->{$editor}['text']; } } } $formfield->define_save($data); profile_reorder_fields(); profile_reorder_categories(); redirect($redirect); } $datatypes = profile_list_datatypes(); if (empty($id)) { $strheading = get_string('profilecreatenewfield', 'admin', $datatypes[$datatype]); } else { $strheading = get_string('profileeditfield', 'admin', $field->name); } /// Print the page $PAGE->navbar->add($strheading); echo $OUTPUT->header(); echo $OUTPUT->heading($strheading); $fieldform->display(); echo $OUTPUT->footer(); die; } }
/** * Test saving the minimum and max year settings for the user datetime field. * * @param string $type the calendar type we want to test * @param array $date the date variables */ private function datetime_field_submission_test($type, $date) { $this->set_calendar_type($type); // Get the data we are submitting for the form. $formdata = array(); $formdata['id'] = 0; $formdata['shortname'] = 'Shortname'; $formdata['name'] = 'Name'; $formdata['param1'] = $date['inputminyear']; $formdata['param2'] = $date['inputmaxyear']; // Mock submitting this. field_form::mock_submit($formdata); // Create the user datetime form. $form = new field_form(null, 'datetime'); // Get the data from the submission. $submissiondata = $form->get_data(); // On the user profile field page after get_data, the function define_save is called // in the field base class, which then calls the field's function define_save_preprocess. $field = new profile_define_datetime(); $submissiondata = $field->define_save_preprocess($submissiondata); // Create an array we want to compare with the date passed. $comparedate = $date; $comparedate['expectedminyear'] = $submissiondata->param1; $comparedate['expectedmaxyear'] = $submissiondata->param2; $this->assertEquals($comparedate, $date); }
function profile_edit_field($id, $datatype, $redirect) { global $CFG; if (!($field = get_record('user_info_field', 'id', $id))) { $field = new object(); $field->datatype = $datatype; } require_once 'index_field_form.php'; $fieldform = new field_form(null, $field->datatype); $fieldform->set_data($field); if ($fieldform->is_cancelled()) { redirect($redirect); } else { if ($data = $fieldform->get_data()) { require_once $CFG->dirroot . '/user/profile/field/' . $datatype . '/define.class.php'; $newfield = 'profile_define_' . $datatype; $formfield = new $newfield(); $formfield->define_save($data); profile_reorder_fields(); profile_reorder_categories(); redirect($redirect); } $datatypes = profile_list_datatypes(); if (empty($id)) { $strheading = get_string('profilecreatenewfield', 'admin', $datatypes[$datatype]); } else { $strheading = get_string('profileeditfield', 'admin', $field->name); } /// Print the page admin_externalpage_print_header(); print_heading($strheading); $fieldform->display(); admin_externalpage_print_footer(); die; } }