[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Menu profile field. 19 * 20 * @package profilefield_menu 21 * @copyright 2007 onwards Shane Elliot {@link http://pukunui.com} 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 /** 26 * Class profile_field_menu 27 * 28 * @copyright 2007 onwards Shane Elliot {@link http://pukunui.com} 29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 30 */ 31 class profile_field_menu extends profile_field_base { 32 33 /** @var array $options */ 34 public $options; 35 36 /** @var int $datakey */ 37 public $datakey; 38 39 /** 40 * Constructor method. 41 * 42 * Pulls out the options for the menu from the database and sets the the corresponding key for the data if it exists. 43 * 44 * @param int $fieldid 45 * @param int $userid 46 */ 47 public function profile_field_menu($fieldid = 0, $userid = 0) { 48 // First call parent constructor. 49 $this->profile_field_base($fieldid, $userid); 50 51 // Param 1 for menu type is the options. 52 if (isset($this->field->param1)) { 53 $options = explode("\n", $this->field->param1); 54 } else { 55 $options = array(); 56 } 57 $this->options = array(); 58 if (!empty($this->field->required)) { 59 $this->options[''] = get_string('choose').'...'; 60 } 61 foreach ($options as $key => $option) { 62 $this->options[$key] = format_string($option); // Multilang formatting. 63 } 64 65 // Set the data key. 66 if ($this->data !== null) { 67 $this->datakey = (int)array_search($this->data, $this->options); 68 } 69 } 70 71 /** 72 * Create the code snippet for this field instance 73 * Overwrites the base class method 74 * @param moodleform $mform Moodle form instance 75 */ 76 public function edit_field_add($mform) { 77 $mform->addElement('select', $this->inputname, format_string($this->field->name), $this->options); 78 } 79 80 /** 81 * Set the default value for this field instance 82 * Overwrites the base class method. 83 * @param moodleform $mform Moodle form instance 84 */ 85 public function edit_field_set_default($mform) { 86 if (false !== array_search($this->field->defaultdata, $this->options)) { 87 $defaultkey = (int)array_search($this->field->defaultdata, $this->options); 88 } else { 89 $defaultkey = ''; 90 } 91 $mform->setDefault($this->inputname, $defaultkey); 92 } 93 94 /** 95 * The data from the form returns the key. 96 * 97 * This should be converted to the respective option string to be saved in database 98 * Overwrites base class accessor method. 99 * 100 * @param mixed $data The key returned from the select input in the form 101 * @param stdClass $datarecord The object that will be used to save the record 102 * @return mixed Data or null 103 */ 104 public function edit_save_data_preprocess($data, $datarecord) { 105 return isset($this->options[$data]) ? $this->options[$data] : null; 106 } 107 108 /** 109 * When passing the user object to the form class for the edit profile page 110 * we should load the key for the saved data 111 * 112 * Overwrites the base class method. 113 * 114 * @param stdClass $user User object. 115 */ 116 public function edit_load_user_data($user) { 117 $user->{$this->inputname} = $this->datakey; 118 } 119 120 /** 121 * HardFreeze the field if locked. 122 * @param moodleform $mform instance of the moodleform class 123 */ 124 public function edit_field_set_locked($mform) { 125 if (!$mform->elementExists($this->inputname)) { 126 return; 127 } 128 if ($this->is_locked() and !has_capability('moodle/user:update', context_system::instance())) { 129 $mform->hardFreeze($this->inputname); 130 $mform->setConstant($this->inputname, $this->datakey); 131 } 132 } 133 /** 134 * Convert external data (csv file) from value to key for processing later by edit_save_data_preprocess 135 * 136 * @param string $value one of the values in menu options. 137 * @return int options key for the menu 138 */ 139 public function convert_external_data($value) { 140 $retval = array_search($value, $this->options); 141 142 // If value is not found in options then return null, so that it can be handled 143 // later by edit_save_data_preprocess. 144 if ($retval === false) { 145 $retval = null; 146 } 147 return $retval; 148 } 149 } 150 151
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |