-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdefine.class.php
More file actions
executable file
·168 lines (153 loc) · 6.16 KB
/
Copy pathdefine.class.php
File metadata and controls
executable file
·168 lines (153 loc) · 6.16 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
<?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/>.
/**
* Dynamic menu profile field definition.
* Based on moodle menu by Shane Elliot
*
* @package profilefield_dynamicmenu
* @copyright 2016 onwards Antonello Moro {@link http://treagles.it}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Class profile_define_dynamicmenu
*
* @copyright 2016 onwards Antonello Moro {@link http://treagles.it}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class profile_define_dynamicmenu extends profile_define_base
{
/**
* Adds elements to the form for creating/editing this type of profile field.
*
* @param moodleform $form
*/
public function define_form_specific($form) {
// Param 1 for menu type contains the options.
$form->addElement(
'textarea', 'param1', get_string('sqlquery', 'profilefield_dynamicmenu'),
array('rows' => 6, 'cols' => 40)
);
$form->setType('param1', PARAM_TEXT);
$form->addHelpButton('param1', 'param1sqlhelp', 'profilefield_dynamicmenu');
// Default data.
$form->addElement('text', 'defaultdata', get_string('profiledefaultdata', 'admin'), 'size="50"');
$form->setType('defaultdata', PARAM_TEXT);
// Let's see if the user can modify the sql.
$context = context_system::instance();
$hascap = has_capability('profilefield/dynamicmenu:caneditsql', $context);
if (!$hascap) {
$form->hardFreeze('param1');
$form->hardFreeze('defaultdata');
}
$form->addElement('text', 'sql_count_data', get_string('numbersqlvalues', 'profilefield_dynamicmenu'));
$form->setType('sql_count_data', PARAM_RAW);
$form->hardFreeze('sql_count_data');
$form->addElement(
'textarea', 'sql_sample_data', get_string('samplesqlvalues', 'profilefield_dynamicmenu'),
array('rows' => 6, 'cols' => 40)
);
$form->setType('sql_sample_data', PARAM_RAW);
$form->hardFreeze('sql_sample_data');
}
/**
* Alter form based on submitted or existing data
*
* @param moodleform $mform
*/
public function define_after_data(&$form) {
global $DB;
try {
$sql = $form->getElementValue('param1');
if ($sql) {
$rs = $DB->get_records_sql($sql);
$i = 0;
$defsample = '';
$countdata = count($rs);
foreach ($rs as $record) {
if ($i == 12) {
break;
}
if (isset($record->data) && isset($record->id)) {
if (strlen($record->data) > 40) {
$sampleval = substr(format_string($record->data), 0, 36).'...';
} else {
$sampleval = format_string($record->data);
}
$defsample .= 'id: '.format_string($record->id) .' - data: '.$sampleval."\n";
}
}
$form->setDefault('sql_count_data', $countdata);
$form->setDefault('sql_sample_data', $defsample);
} else {
$form->setDefault('sql_count_data', 0);
$form->setDefault('sql_sample_data', '');
}
} catch (Exception $e) {
// We don't have to do anything here, since the error shall be handled by define_validate_specific.
$form->setDefault('sql_count_data', 0);
$form->setDefault('sql_sample_data', '');
}
}
/**
* Validates data for the profile field.
*
* @param array $data
* @param array $files
* @return array
*/
public function define_validate_specific($data, $files) {
$err = array();
$data->param1 = str_replace("\r", '', $data->param1);
// Le'ts try to execute the query.
$sql = $data->param1;
global $DB;
try {
$rs = $DB->get_records_sql($sql);
if (!$rs) {
$err['param1'] = get_string('queryerrorfalse', 'profilefield_dynamicmenu');
} else {
if (count($rs) == 0) {
$err['param1'] = get_string('queryerrorempty', 'profilefield_dynamicmenu');
} else {
$firstval = reset($rs);
if (!object_property_exists($firstval, 'id')) {
$err['param1'] = get_string('queryerroridmissing', 'profilefield_dynamicmenu');
} else {
if (!object_property_exists($firstval, 'data')) {
$err['param1'] = get_string('queryerrordatamissing', 'profilefield_dynamicmenu');
} else if (!empty($data->defaultdata) && !isset($rs[$data->defaultdata])) {
// Def missing.
$err['defaultdata'] = get_string('queryerrordefaultmissing', 'profilefield_dynamicmenu');
}
}
}
}
} catch (Exception $e) {
$err['param1'] = get_string('sqlerror', 'profilefield_dynamicmenu') . ': ' .$e->getMessage();
}
return $err;
}
/**
* Processes data before it is saved.
*
* @param array|stdClass $data
* @return array|stdClass
*/
public function define_save_preprocess($data) {
$data->param1 = str_replace("\r", '', $data->param1);
return $data;
}
}