-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfield.class.php
More file actions
204 lines (183 loc) · 6.72 KB
/
Copy pathfield.class.php
File metadata and controls
204 lines (183 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
*
* Multiselect profile field.
*
* @package profilefield_dynamicmultiselect
* @copyright 2016 onwards Antonello Moro {http://antonellomoro.it}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Class profile_field_multiselect
*
* @copyright 2016 onwards Antonello Moro {http://antonellomoro.it}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class profile_field_dynamicmultiselect extends profile_field_base {
/** @var array $options */
public $options;
/** @var int $datakey */
public $datakey;
/** @var array @calls array indexed by @fieldid-$userid. It keeps track of recordset,
* so that we don't do the query twice for the same field */
private static $acalls = array();
/**
* Constructor method.
*
* Pulls out the options for the menu from the database and sets the the corresponding key for the data if it exists.
*
* @param int $fieldid
* @param int $userid
*/
public function __construct($fieldid=0, $userid=0) {
// First call parent constructor.
parent::__construct($fieldid, $userid);
// Only if we actually need data.
if ($fieldid !== 0 && $userid !== 0) {
$mykey = $fieldid.','.$userid; // It will always work because they are number, so no chance of ambiguity.
if (array_key_exists($mykey , self::$acalls)) {
$rs = self::$acalls[$mykey];
} else {
$sql = $this->field->param1;
global $DB;
$rs = $DB->get_records_sql($sql);
self::$acalls[$mykey] = $rs;
}
$this->options = array();
if ($this->field->required) {
$this->options[''] = get_string('choose').'...';
}
foreach ($rs as $key => $option) {
$this->options[format_string($key)] = format_string($option->data); // Multilang formatting.
}
// Set the data key.
if ($this->data !== null) {
$this->data = str_replace("\r", '', $this->data);
$this->datatmp = explode("\n", $this->data);
foreach ($this->datatmp as $key => $option1) {
$this->datakey[] = (int)array_search($option1, $this->options);
}
}
}
}
/**
* deprecated old costructor
* @param int $fieldid
* @param int $userid
*/
public function profile_field_menu($fieldid=0, $userid=0) {
self::__construct($fieldid, $userid);
}
/**
* Create the code snippet for this field instance
* Overwrites the base class method
* @param moodleform $mform
*/
public function edit_field_add($mform) {
$mform->addElement('select', $this->inputname, format_string($this->field->name), $this->options);
$mform->getElement($this->inputname)->setMultiple(true);
}
/**
* Set the default value for this field instance
* Overwrites the base class method
* @param moodleform $mform
*/
public function edit_field_set_default($mform) {
if (false !== array_search($this->field->defaultdata, $this->options)) {
$defaultkey = (int)array_search($this->field->defaultdata, $this->options);
} else {
$defaultkey = '';
}
$mform->setDefault($this->inputname, $defaultkey);
}
/**
* The data from the form returns the key. This should be converted to the
* respective option string to be saved in database
* Overwrites base class accessor method
* @param mixed $data - the key returned from the select input in the form
* @param stdClass $datarecord The object that will be used to save the record
*/
public function edit_save_data_preprocess($data, $datarecord) {
$string = '';
if (is_array($data)) {
foreach ($data as $key) {
if (isset($this->options[$key])) {
$string .= $this->options[$key]."\r\n";
}
}
return substr($string, 0, -2);
}
return isset($this->options[$data]) ? $this->options[$data] : null;
}
/**
* When passing the user object to the form class for the edit profile page
* we should load the key for the saved data
* Overwrites the base class method
* @param stdClass $user
*/
public function edit_load_user_data($user) {
$user->{$this->inputname} = $this->datakey;
}
/**
* HardFreeze the field if locked.
* @param moodleform $mform
* @throws coding_exception
* @throws dml_exception
*/
public function edit_field_set_locked($mform) {
if (!$mform->elementExists($this->inputname)) {
return;
}
if ($this->is_locked() and !has_capability('moodle/user:update', context_system::instance())) {
$mform->hardFreeze($this->inputname);
$mform->setConstant($this->inputname, $this->datakey);
}
}
/**
* Convert external data (csv file) from value to key for processing later
* by edit_save_data_preprocess
*
* @param string $value one of the values in menu options.
* @return int options key for the menu
*/
public function convert_external_data($value) {
$retval = array_search($value, $this->options);
// If value is not found in options then return null, so that it can be handled
// later by edit_save_data_preprocess.
if ($retval === false) {
$retval = null;
}
return $retval;
}
/**
* Display the data for this field.
*/
public function display_data() {
$sql = $this->field->param1;
global $DB;
$rs = $DB->get_records_sql($sql);
$ret = '';
foreach ($this->datakey as $key) {
if (array_key_exists($key, $rs)) {
$ret .= $rs[$key]->data . "\r\n";
} else {
$ret .= 'N/A' . "\r\n";
}
}
return $ret;
}
}