Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / user / profile / field / menu / field.class.php
blobc360b2ad95ac6167978a074a218b1c7d798986fc
1 <?php //$Id$
3 class profile_field_menu extends profile_field_base {
4 var $options;
5 var $datakey;
7 /**
8 * Constructor method.
9 * Pulls out the options for the menu from the database and sets the
10 * the corresponding key for the data if it exists
12 function profile_field_menu($fieldid=0, $userid=0) {
13 //first call parent constructor
14 $this->profile_field_base($fieldid, $userid);
16 /// Param 1 for menu type is the options
17 $options = explode("\n", $this->field->param1);
18 $this->options = array();
19 foreach($options as $key => $option) {
20 $this->options[$key] = format_string($option);//multilang formatting
23 /// Set the data key
24 if ($this->data !== NULL) {
25 $this->datakey = (int)array_search($this->data, $this->options);
29 /**
30 * Create the code snippet for this field instance
31 * Overwrites the base class method
32 * @param object moodleform instance
34 function edit_field_add(&$mform) {
35 $mform->addElement('select', $this->inputname, format_string($this->field->name), $this->options);
38 /**
39 * Set the default value for this field instance
40 * Overwrites the base class method
42 function edit_field_set_default(&$mform) {
43 $defaultkey = (int)array_search($this->field->defaultdata, $this->options);
44 $mform->setDefault($this->inputname, $defaultkey);
47 /**
48 * The data from the form returns the key. This should be converted to the
49 * respective option string to be saved in database
50 * Overwrites base class accessor method
51 * @param integer the key returned from the select input in the form
53 function edit_save_data_preprocess($key) {
54 return isset($this->options[$key]) ? $this->options[$key] : NULL;
57 /**
58 * When passing the user object to the form class for the edit profile page
59 * we should load the key for the saved data
60 * Overwrites the base class method
61 * @param object user object
63 function edit_load_user_data(&$user) {
64 $user->{$this->inputname} = $this->datakey;