forked from sebsoftnl/moodle-mod_gwpayments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_form.php
309 lines (273 loc) · 11.9 KB
/
mod_form.php
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?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/>.
/**
* gwpayments configuration form
*
* File mod_form.php
* Encoding UTF-8
*
* @package mod_gwpayments
*
* @copyright 2021 Ing. R.J. van Dongen
* @author Ing. R.J. van Dongen <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot . '/course/moodleform_mod.php');
require_once($CFG->dirroot . '/mod/page/locallib.php');
require_once($CFG->libdir . '/filelib.php');
/**
* gwpayments configuration form
*
* @package mod_gwpayments
*
* @copyright 2021 Ing. R.J. van Dongen
* @author Ing. R.J. van Dongen <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_gwpayments_mod_form extends moodleform_mod {
/**
* Form definition.
*/
protected function definition() {
global $CFG, $DB, $COURSE;
$mform = $this->_form;
$config = get_config('gwpayments');
// echo serialize($config);
// die;
// -------------------------------------------------------
$mform->addElement('header', 'general', get_string('general', 'form'));
$mform->addElement('text', 'name', get_string('name'), ['size' => '48']);
if (!empty($CFG->formatstringstriptags)) {
$mform->setType('name', PARAM_TEXT);
} else {
$mform->setType('name', PARAM_CLEANHTML);
}
$mform->addRule('name', null, 'required', null, 'client');
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$this->standard_intro_elements();
$mform->addElement('advcheckbox', 'printintro', get_string('printintro', 'page'), 0);
$mform->setType('printintro', PARAM_INT);
// -------------------------------------------------------
$mform->addElement('header', 'content', get_string('contentheader', 'mod_gwpayments'));
// $mform->addElement('textarea', 'page', get_string('content'), ['rows' => "10", 'cols' => "10"]);
$mform->addElement('float', 'cost', get_string('cost', 'mod_gwpayments'), ['size' => 7]);
// $mform->setType('cost', PARAM_FLOAT);
$mform->addRule('cost', null, 'required', null, 'client');
// $mform->addRule('cost', null, 'numeric', null, 'client');
$mform->setDefault('cost', $config->cost);
$mform->addHelpButton('cost', 'cost', 'mod_gwpayments');
$mform->addElement(
'advcheckbox',
'showcost',
get_string('showcost', 'mod_gwpayments')
);
$mform->setType('showcost', PARAM_INT);
// This is used for expiry determination.
$mform->addElement('duration', 'costduration', get_string('costduration', 'mod_gwpayments'), ['optional' => true]);
// $mform->setDefault('costduration', 86400);
$mform->addHelpButton('costduration', 'costduration', 'mod_gwpayments');
$mform->addElement(
'advcheckbox',
'showduration',
get_string('showduration', 'mod_gwpayments')
);
$mform->setType('showduration', PARAM_INT);
/*
$mform->addElement('text', 'vat', get_string('vat', 'mod_gwpayments'), array('size' => 4));
$mform->setType('vat', PARAM_RAW);
$mform->setDefault('vat', $config->vat);
$mform->addHelpButton('vat', 'vat', 'mod_gwpayments');
*/
$supportedcurrencies = \mod_gwpayments\local\helper::get_possible_currencies();
$mform->addElement('select', 'currency', get_string('currency', 'mod_gwpayments'), $supportedcurrencies);
$mform->setDefault('currency', $config->currency);
$mform->addRule('currency', null, 'required', null, 'client');
$mform->addHelpButton('currency', 'currency', 'mod_gwpayments');
$accounts = \core_payment\helper::get_payment_accounts_menu($this->context);
if (count($accounts) == 0) {
// Add warning!
$mform->addElement(
'static',
'accountid_text',
get_string('paymentaccount', 'mod_gwpayments'),
html_writer::span(get_string('noaccountsavilable', 'payment'), 'alert alert-danger')
);
}
$accounts = ((count($accounts) > 1) ? ['' => ''] : []) + $accounts;
$mform->addElement('select', 'accountid', get_string('paymentaccount', 'mod_gwpayments'), $accounts);
$mform->setType('accountid', PARAM_INT);
$mform->addHelpButton('accountid', 'paymentaccount', 'mod_gwpayments');
$mform->disabledIf('accountid', 'hidepaymentaccount', "neq", 0);
$mform->addRule('accountid', null, 'required', null, 'client');
$mform->addElement(
'advcheckbox',
'hidepaymentaccount',
get_string('hidepaymentaccount', 'mod_gwpayments')
);
$mform->setType('hidepaymentaccount', PARAM_INT);
$mform->addHelpButton('hidepaymentaccount', 'hidepaymentaccount', 'mod_gwpayments');
$mform->addElement(
'text',
'addpaymentlink',
get_string('addpaymentlink', 'mod_gwpayments'),
['size' => 48]
);
$mform->setType('addpaymentlink', PARAM_TEXT);
$mform->addHelpButton('addpaymentlink', 'addpaymentlink', 'mod_gwpayments');
$mform->addElement(
'passwordunmask',
'password',
get_string('password', 'mod_gwpayments'),
['size' => 24]
);
$mform->setType('password', PARAM_TEXT);
$mform->addHelpButton('password', 'password', 'mod_gwpayments');
$options = [1 => get_string('yes'), 0 => get_string('no'), 2 => get_string('hide')];
$mform->addElement(
'select',
'studentdisplayonpayments',
get_string('studentdisplayonpayments', 'mod_gwpayments'),
$options
);
$mform->setType('studentdisplayonpayments', PARAM_INT);
$mform->setDefault('studentdisplayonpayments', $config->studentdisplayonpayments);
$mform->addHelpButton('studentdisplayonpayments', 'studentdisplayonpayments', 'mod_gwpayments');
$mform->addElement(
'advcheckbox',
'disablepaymentonmisconfig',
get_string('disablepaymentonmisconfig', 'mod_gwpayments')
);
$mform->setType('disablepaymentonmisconfig', PARAM_INT);
$mform->setDefault('disablepaymentonmisconfig', $config->disablepaymentonmisconfig);
$mform->addHelpButton('disablepaymentonmisconfig', 'disablepaymentonmisconfig', 'mod_gwpayments');
$mform->addElement(
'advcheckbox',
'showamount',
get_string('showamount', 'mod_gwpayments')
);
$mform->setType('showamount', PARAM_INT);
$mform->addElement(
'advcheckbox',
'showallpayments',
get_string('showallpayments', 'mod_gwpayments')
);
$mform->setType('showallpayments', PARAM_INT);
$mform->setExpanded('content');
// -------------------------------------------------------
$this->standard_coursemodule_elements();
// -------------------------------------------------------
$completion = new completion_info($COURSE);
if ($completion->is_enabled()) {
$this->_form->setConstant('completion', COMPLETION_TRACKING_AUTOMATIC);
// $this->_form->freeze('completion');
} else {
$mform->addElement(
'static',
'completiondisabled',
get_string('completiondisabled:label', 'mod_gwpayments'),
get_string('completiondisabled:warning', 'mod_gwpayments')
);
$mform->closeHeaderBefore('completiondisabled');
}
// -------------------------------------------------------
$this->add_action_buttons();
}
/**
* Can be overridden to add custom completion rules if the module wishes
* them. If overriding this, you should also override completion_rule_enabled.
* <p>
* Just add elements to the form as needed and return the list of IDs. The
* system will call disabledIf and handle other behaviour for each returned
* ID.
* @return array Array of string IDs of added items, empty array if none
*/
public function add_completion_rules() {
global $OUTPUT;
$img = html_writer::img($OUTPUT->pix_icon('t/check', ''), '');
$img = html_writer::img($OUTPUT->pix_icon('i/completion-auto-', 'moodle'), '');
$mform =& $this->_form;
$mform->addElement('static', '_completionsubmit', '', $img . ' ' . get_string('completionsubmit', 'mod_gwpayments'));
$mform->addElement('hidden', 'completionsubmit', 1);
$mform->setType('completionsubmit', PARAM_INT);
return ['_completionsubmit', 'completionsubmit'];
}
/**
* Called during validation. Override to indicate, based on the data, whether
* a custom completion rule is enabled (selected).
*
* @param array $data Input data (not yet validated)
* @return bool True if one or more rules is enabled, false if none are;
* default returns false
*/
public function completion_rule_enabled($data) {
return !empty($data['completionsubmit']);
}
/**
* Allows module to modify data returned by get_moduleinfo_data() or prepare_new_moduleinfo_data() before calling set_data()
* This method is also called in the bulk activity completion form.
*
* Only available on moodleform_mod.
*
* @param array $defaultvalues passed by reference
*/
public function data_preprocessing(&$defaultvalues) {
/*
if (!empty($defaultvalues['advoptions'])) {
$advoptions = (array) unserialize_array($defaultvalues['advoptions']);
if (isset($advoptions['printintro'])) {
$defaultvalues['printintro'] = $advoptions['printintro'];
}
}
*/
}
/**
* Allows module to modify the data returned by form get_data().
* This method is also called in the bulk activity completion form.
*
* Only available on moodleform_mod.
*
* @param stdClass $data the form data to be modified.
*/
public function data_postprocessing($data) {
parent::data_postprocessing($data);
// Set up completion section even if checkbox is not ticked.
if (!empty($data->completionunlocked)) {
if (empty($data->completionsubmit)) {
$data->completionsubmit = 1;
}
}
}
/**
* Perform form validation
*
* @param array $data
* @param array $files
* @return array
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
// Validating Entered gwpayments, we are looking for obvious problems only,
// teachers are responsible for testing if it actually works.
if ($data['cost'] < 0.01) {
$errors['cost'] = get_string('mincosterror', 'mod_gwpayments');
}
if ($data['hidepaymentaccount'] && empty($data['addpaymentlink'])) {
$errors['addpaymentlink'] = get_string('addpaymentlinkempty', 'mod_gwpayments');
}
return $errors;
}
}