[ Index ] |
PHP Cross Reference of Unnamed Project |
[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 __construct($fieldid = 0, $userid = 0) { 48 // First call parent constructor. 49 parent::__construct($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[$option] = format_string($option); // Multilang formatting with filters. 63 } 64 65 // Set the data key. 66 if ($this->data !== null) { 67 $key = $this->data; 68 if (isset($this->options[$key]) || ($key = array_search($key, $this->options)) !== false) { 69 $this->data = $key; 70 $this->datakey = $key; 71 } 72 } 73 } 74 75 /** 76 * Old syntax of class constructor. Deprecated in PHP7. 77 * 78 * @deprecated since Moodle 3.1 79 */ 80 public function profile_field_menu($fieldid=0, $userid=0) { 81 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER); 82 self::__construct($fieldid, $userid); 83 } 84 85 /** 86 * Create the code snippet for this field instance 87 * Overwrites the base class method 88 * @param moodleform $mform Moodle form instance 89 */ 90 public function edit_field_add($mform) { 91 $mform->addElement('select', $this->inputname, format_string($this->field->name), $this->options); 92 } 93 94 /** 95 * Set the default value for this field instance 96 * Overwrites the base class method. 97 * @param moodleform $mform Moodle form instance 98 */ 99 public function edit_field_set_default($mform) { 100 $key = $this->field->defaultdata; 101 if (isset($this->options[$key]) || ($key = array_search($key, $this->options)) !== false){ 102 $defaultkey = $key; 103 } else { 104 $defaultkey = ''; 105 } 106 $mform->setDefault($this->inputname, $defaultkey); 107 } 108 109 /** 110 * The data from the form returns the key. 111 * 112 * This should be converted to the respective option string to be saved in database 113 * Overwrites base class accessor method. 114 * 115 * @param mixed $data The key returned from the select input in the form 116 * @param stdClass $datarecord The object that will be used to save the record 117 * @return mixed Data or null 118 */ 119 public function edit_save_data_preprocess($data, $datarecord) { 120 return isset($this->options[$data]) ? $data : null; 121 } 122 123 /** 124 * When passing the user object to the form class for the edit profile page 125 * we should load the key for the saved data 126 * 127 * Overwrites the base class method. 128 * 129 * @param stdClass $user User object. 130 */ 131 public function edit_load_user_data($user) { 132 $user->{$this->inputname} = $this->datakey; 133 } 134 135 /** 136 * HardFreeze the field if locked. 137 * @param moodleform $mform instance of the moodleform class 138 */ 139 public function edit_field_set_locked($mform) { 140 if (!$mform->elementExists($this->inputname)) { 141 return; 142 } 143 if ($this->is_locked() and !has_capability('moodle/user:update', context_system::instance())) { 144 $mform->hardFreeze($this->inputname); 145 $mform->setConstant($this->inputname, format_string($this->datakey)); 146 } 147 } 148 /** 149 * Convert external data (csv file) from value to key for processing later by edit_save_data_preprocess 150 * 151 * @param string $value one of the values in menu options. 152 * @return int options key for the menu 153 */ 154 public function convert_external_data($value) { 155 if (isset($this->options[$value])) { 156 $retval = $value; 157 } else { 158 $retval = array_search($value, $this->options); 159 } 160 161 // If value is not found in options then return null, so that it can be handled 162 // later by edit_save_data_preprocess. 163 if ($retval === false) { 164 $retval = null; 165 } 166 return $retval; 167 } 168 } 169 170
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |