forked from moodleworkplace/moodle-mod_coursecertificate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_form.php
228 lines (203 loc) · 8.54 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
<?php
// This file is part of the mod_coursecertificate plugin for 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/>.
/**
* The main mod_coursecertificate configuration form.
*
* @package mod_coursecertificate
* @copyright 2020 Mikel Martín <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use tool_certificate\certificate;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/course/moodleform_mod.php');
/**
* Module instance settings form.
*
* @package mod_coursecertificate
* @copyright 2020 Mikel Martín <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_coursecertificate_mod_form extends moodleform_mod {
/**
* Defines forms elements
*/
public function definition(): void {
global $CFG, $OUTPUT;
$mform = $this->_form;
$hasissues = $this->has_issues();
$canmanagetemplates = \tool_certificate\permission::can_manage_anywhere();
// Adding the "general" fieldset, where all the common settings are shown.
$mform->addElement('header', 'general', get_string('general', 'form'));
// Adding the standard "name" field.
$mform->addElement('text', 'name', get_string('name'), ['size' => '64']);
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();
// Adding the template selector.
if ($hasissues) {
// If coursecertificate has issues, just add the current template to the selector.
$templates = $this->get_current_template();
} else {
// Get all available templates for the user.
$templates = $this->get_template_select();
}
$templateoptions = ['' => get_string('chooseatemplate', 'coursecertificate')] + $templates;
$manageurl = new \moodle_url('/admin/tool/certificate/manage_templates.php');
$elements = [$mform->createElement('select', 'template', get_string('template', 'coursecertificate'), $templateoptions)];
// Adding "Manage templates" link if user has capabilities to manage templates.
if ($canmanagetemplates && !empty($templates)) {
$elements[] = $mform->createElement('static', 'managetemplates', '',
$OUTPUT->action_link($manageurl, get_string('managetemplates', 'coursecertificate')));
}
$mform->addGroup($elements, 'template_group', get_string('template', 'coursecertificate'),
\html_writer::div('', 'w-100'), false);
if (empty($templates)) {
// Adding warning text if there are not templates available.
if ($canmanagetemplates) {
$warningstr = get_string('notemplateswarningwithlink', 'coursecertificate', $manageurl->out());
} else {
$warningstr = get_string('notemplateswarning', 'coursecertificate');
}
$html = html_writer::tag('div', $warningstr, ['class' => 'alert alert-warning']);
$mform->addElement('static', 'notemplateswarning', '', $html);
} else {
$warningstr = get_string('selecttemplatewarning', 'mod_coursecertificate');
$html = html_writer::tag('div', $warningstr, ['class' => 'alert alert-warning']);
$mform->addElement('static', 'selecttemplatewarning', '', $html);
}
if (!$hasissues) {
$rules = [];
$rules['template'][] = [null, 'required', null, 'client'];
$mform->addGroupRule('template_group', $rules);
}
// If Certificate has issues it's not possible to change the template.
$mform->addElement('hidden', 'hasissues', $hasissues);
$mform->setType('hasissues', PARAM_TEXT);
$mform->disabledIf('template', 'hasissues', 'eq', 1);
// Adding the expirydate selector.
certificate::add_expirydate_to_form($mform);
// Add standard elements.
$this->standard_coursemodule_elements();
// Add standard buttons.
$this->add_action_buttons();
}
/**
* Enforce validation rules here
*
* @param array $data array of ("fieldname"=>value) of submitted data
* @param array $files array of uploaded files "element_name"=>tmp_file_path
* @return array
**/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
return $errors;
}
/**
* Enforce defaults here.
*
* @param array $defaultvalues Form defaults
* @return void
**/
public function data_preprocessing(&$defaultvalues) {
if (isset($defaultvalues['expirydatetype'])) {
if ($defaultvalues['expirydatetype'] == certificate::DATE_EXPIRATION_ABSOLUTE) {
$defaultvalues['expirydateabsolute'] = $defaultvalues['expirydateoffset'];
}
if ($defaultvalues['expirydatetype'] == certificate::DATE_EXPIRATION_AFTER) {
$defaultvalues['expirydaterelative'] = $defaultvalues['expirydateoffset'];
}
}
}
/**
* Allows modules 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 passed by reference
*/
public function data_postprocessing($data) {
parent::data_postprocessing($data);
switch ($data->expirydatetype) {
case certificate::DATE_EXPIRATION_ABSOLUTE:
$data->expirydateoffset = $data->expirydateabsolute;
break;
case certificate::DATE_EXPIRATION_AFTER:
$data->expirydateoffset = $data->expirydaterelative;
break;
default:
$data->expirydateoffset = 0;
}
}
/**
* Gets the current coursecertificate template for the template selector.
*
* @return array
*/
private function get_current_template(): array {
global $DB;
$templates = [];
if ($instance = $this->get_instance()) {
$sql = "SELECT ct.id, ct.name
FROM {tool_certificate_templates} ct
JOIN {coursecertificate} c
ON c.template = ct.id
AND c.id = :instance";
if ($record = $DB->get_record_sql($sql, ['instance' => $instance], IGNORE_MISSING)) {
$templates[$record->id] = format_string($record->name);
}
}
return $templates;
}
/**
* Gets array options of available templates for the user for the template selector.
*
* @return array
*/
private function get_template_select(): array {
$context = context_course::instance($this->current->course);
$templates = [];
if (!empty($records = \tool_certificate\permission::get_visible_templates($context))) {
foreach ($records as $record) {
$templates[$record->id] = format_string($record->name);
}
}
return $templates;
}
/**
* Returns "1" if course certificate has been issued.
*
* @return string
* @uses \tool_certificate\certificate
*/
private function has_issues(): string {
global $DB;
if ($instance = $this->get_instance()) {
$certificate = $certificate = $DB->get_record('coursecertificate', ['id' => $instance], '*', MUST_EXIST);
$courseissues = certificate::count_issues_for_course($certificate->template, $certificate->course,
'mod_coursecertificate', null, null);
if ($courseissues > 0) {
return "1";
}
}
return "0";
}
}