1 |
efrain |
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 |
* Edit Form
|
|
|
19 |
*
|
|
|
20 |
* @package block_featured_courses
|
|
|
21 |
* @copyright 2020 - CALL Learning - Laurent David <laurent@call-learning>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Class block_featured_courses_edit_form
|
|
|
27 |
*
|
|
|
28 |
* @package block_featured_courses
|
|
|
29 |
* @copyright 2020 - CALL Learning - Laurent David <laurent@call-learning>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
class block_featured_courses_edit_form extends block_edit_form {
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Form definition
|
|
|
36 |
*
|
|
|
37 |
* @param object $mform
|
|
|
38 |
* @throws coding_exception|moodle_exception
|
|
|
39 |
*/
|
|
|
40 |
protected function specific_definition($mform) {
|
|
|
41 |
|
|
|
42 |
// Section header title according to language file.
|
|
|
43 |
$mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
|
|
|
44 |
|
|
|
45 |
// Title of the block.
|
|
|
46 |
$mform->addElement('text', 'config_title', get_string('config:title', 'block_featured_courses'));
|
|
|
47 |
$mform->setDefault('config_title', get_string('pluginname', 'block_featured_courses'));
|
|
|
48 |
$mform->setType('config_title', PARAM_TEXT);
|
|
|
49 |
|
|
|
50 |
$courses = (core_course_category::get(0))->get_courses(['recursive' => true]);
|
|
|
51 |
$courseitems = [];
|
|
|
52 |
foreach ($courses as $c) {
|
|
|
53 |
$courseitems[$c->id] = " {$c->get_formatted_name()} ($c->id)";
|
|
|
54 |
}
|
|
|
55 |
$repeatarray = array();
|
|
|
56 |
$repeatedoptions = array();
|
|
|
57 |
|
|
|
58 |
$repeatarray[] = $mform->createElement('searchableselector',
|
|
|
59 |
'config_selectedcourses',
|
|
|
60 |
get_string('config:selectedcourses', 'block_featured_courses'),
|
|
|
61 |
$courseitems
|
|
|
62 |
);
|
|
|
63 |
$repeatedoptions['config_selectedcourses']['type'] = PARAM_RAW;
|
|
|
64 |
$numbcourses = empty($this->block->config->selectedcourses) ? 1 : count($this->block->config->selectedcourses);
|
|
|
65 |
$this->repeat_elements($repeatarray,
|
|
|
66 |
$numbcourses,
|
|
|
67 |
$repeatedoptions,
|
|
|
68 |
'selectcourses_repeats',
|
|
|
69 |
'selectcourse_add_fields',
|
|
|
70 |
3,
|
|
|
71 |
get_string('addmorecourses', 'block_featured_courses'),
|
|
|
72 |
false,
|
|
|
73 |
'selectcourse_remove_fields',
|
|
|
74 |
get_string('removelastcourses', 'block_featured_courses')
|
|
|
75 |
);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Method to add a repeating group of elements to a form.
|
|
|
80 |
*
|
|
|
81 |
* We can also remove the last element of the list.
|
|
|
82 |
*
|
|
|
83 |
* @param array $elementobjs Array of elements or groups of elements that are to be repeated
|
|
|
84 |
* @param int $repeats no of times to repeat elements initially
|
|
|
85 |
* @param array $options a nested array. The first array key is the element name.
|
|
|
86 |
* the second array key is the type of option to set, and depend on that option,
|
|
|
87 |
* the value takes different forms.
|
|
|
88 |
* 'default' - default value to set. Can include '{no}' which is replaced by the repeat number.
|
|
|
89 |
* 'type' - PARAM_* type.
|
|
|
90 |
* 'helpbutton' - array containing the helpbutton params.
|
|
|
91 |
* 'disabledif' - array containing the disabledIf() arguments after the element name.
|
|
|
92 |
* 'rule' - array containing the addRule arguments after the element name.
|
|
|
93 |
* 'expanded' - whether this section of the form should be expanded by default. (Name be a header element.)
|
|
|
94 |
* 'advanced' - whether this element is hidden by 'Show more ...'.
|
|
|
95 |
* @param string $repeathiddenname name for hidden element storing no of repeats in this form
|
|
|
96 |
* @param string $addfieldsname name for button to add more fields
|
|
|
97 |
* @param int $addfieldsno how many fields to add at a time
|
|
|
98 |
* @param string $addstring name of button, {no} is replaced by no of blanks that will be added.
|
|
|
99 |
* @param bool $addbuttoninside if true, don't call closeHeaderBefore($addfieldsname). Default false.
|
|
|
100 |
* @param string $deletefieldsname name of the button that will trigger the deletion of the repeat element
|
|
|
101 |
* @param string $deletestring name for button to remove the last field
|
|
|
102 |
* @return int no of repeats of element in this page
|
|
|
103 |
* @throws coding_exception
|
|
|
104 |
*/
|
|
|
105 |
public function repeat_elements($elementobjs, $repeats, $options, $repeathiddenname,
|
|
|
106 |
$addfieldsname,
|
|
|
107 |
$addfieldsno = 5,
|
|
|
108 |
$addstring = null,
|
|
|
109 |
$addbuttoninside = false,
|
|
|
110 |
$deletefieldsname = null,
|
|
|
111 |
$deletestring = null
|
|
|
112 |
): int {
|
|
|
113 |
$repeats = $this->optional_param($repeathiddenname, $repeats, PARAM_INT);
|
|
|
114 |
if ($deletefieldsname) {
|
|
|
115 |
$removefields = $this->optional_param($deletefieldsname, '', PARAM_TEXT);
|
|
|
116 |
if (!empty($removefields)) {
|
|
|
117 |
$repeats -= 1; // Remove last course.
|
|
|
118 |
}
|
|
|
119 |
if ($deletestring === null) {
|
|
|
120 |
$deletestring = get_string('delete', 'moodle');
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
if ($addstring === null) {
|
|
|
124 |
$addstring = get_string('addfields', 'form', $addfieldsno);
|
|
|
125 |
} else {
|
|
|
126 |
$addstring = str_ireplace('{no}', $addfieldsno, $addstring);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
$addfields = $this->optional_param($addfieldsname, '', PARAM_TEXT);
|
|
|
130 |
if (!empty($addfields)) {
|
|
|
131 |
$repeats += $addfieldsno;
|
|
|
132 |
}
|
|
|
133 |
$mform =& $this->_form;
|
|
|
134 |
$mform->registerNoSubmitButton($addfieldsname);
|
|
|
135 |
$mform->addElement('hidden', $repeathiddenname, $repeats);
|
|
|
136 |
$mform->setType($repeathiddenname, PARAM_INT);
|
|
|
137 |
// Value not to be overridden by submitted value.
|
|
|
138 |
$mform->setConstants(array($repeathiddenname => $repeats));
|
|
|
139 |
$namecloned = array();
|
|
|
140 |
for ($i = 0; $i < $repeats; $i++) {
|
|
|
141 |
foreach ($elementobjs as $elementobj) {
|
|
|
142 |
$elementclone = fullclone($elementobj);
|
|
|
143 |
$this->repeat_elements_fix_clone($i, $elementclone, $namecloned);
|
|
|
144 |
|
|
|
145 |
if ($elementclone instanceof HTML_QuickForm_group && !$elementclone->_appendName) {
|
|
|
146 |
foreach ($elementclone->getElements() as $el) {
|
|
|
147 |
$this->repeat_elements_fix_clone($i, $el, $namecloned);
|
|
|
148 |
}
|
|
|
149 |
$elementclone->setLabel(str_replace('{no}', $i + 1, $elementclone->getLabel()));
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
$mform->addElement($elementclone);
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
for ($i = 0; $i < $repeats; $i++) {
|
|
|
156 |
foreach ($options as $elementname => $elementoptions) {
|
|
|
157 |
$pos = strpos($elementname, '[');
|
|
|
158 |
if ($pos !== false) {
|
|
|
159 |
$realelementname = substr($elementname, 0, $pos) . "[$i]";
|
|
|
160 |
$realelementname .= substr($elementname, $pos);
|
|
|
161 |
} else {
|
|
|
162 |
$realelementname = $elementname . "[$i]";
|
|
|
163 |
}
|
|
|
164 |
foreach ($elementoptions as $option => $params) {
|
|
|
165 |
|
|
|
166 |
switch ($option) {
|
|
|
167 |
case 'default' :
|
|
|
168 |
$mform->setDefault($realelementname, str_replace('{no}', $i + 1, $params));
|
|
|
169 |
break;
|
|
|
170 |
case 'helpbutton' :
|
|
|
171 |
$params = array_merge(array($realelementname), $params);
|
|
|
172 |
call_user_func_array(array(&$mform, 'addHelpButton'), $params);
|
|
|
173 |
break;
|
|
|
174 |
case 'disabledif' :
|
|
|
175 |
foreach ($namecloned as $name) {
|
|
|
176 |
if ($params[0] == $name) {
|
|
|
177 |
$params[0] = $params[0] . "[$i]";
|
|
|
178 |
break;
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
$params = array_merge(array($realelementname), $params);
|
|
|
182 |
call_user_func_array(array(&$mform, 'disabledIf'), $params);
|
|
|
183 |
break;
|
|
|
184 |
case 'hideif' :
|
|
|
185 |
foreach ($namecloned as $name) {
|
|
|
186 |
if ($params[0] == $name) {
|
|
|
187 |
$params[0] = $params[0] . "[$i]";
|
|
|
188 |
break;
|
|
|
189 |
}
|
|
|
190 |
}
|
|
|
191 |
$params = array_merge(array($realelementname), $params);
|
|
|
192 |
call_user_func_array(array(&$mform, 'hideIf'), $params);
|
|
|
193 |
break;
|
|
|
194 |
case 'rule' :
|
|
|
195 |
if (is_string($params)) {
|
|
|
196 |
$params = array(null, $params, null, 'client');
|
|
|
197 |
}
|
|
|
198 |
$params = array_merge(array($realelementname), $params);
|
|
|
199 |
call_user_func_array(array(&$mform, 'addRule'), $params);
|
|
|
200 |
break;
|
|
|
201 |
|
|
|
202 |
case 'type':
|
|
|
203 |
$mform->setType($realelementname, $params);
|
|
|
204 |
break;
|
|
|
205 |
|
|
|
206 |
case 'expanded':
|
|
|
207 |
$mform->setExpanded($realelementname, $params);
|
|
|
208 |
break;
|
|
|
209 |
|
|
|
210 |
case 'advanced' :
|
|
|
211 |
$mform->setAdvanced($realelementname, $params);
|
|
|
212 |
break;
|
|
|
213 |
}
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
$mform->addElement('submit', $addfieldsname, $addstring);
|
|
|
218 |
if ($deletefieldsname) {
|
|
|
219 |
$mform->addElement('submit', $deletefieldsname, $deletestring);
|
|
|
220 |
$mform->registerNoSubmitButton($deletefieldsname);
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
if (!$addbuttoninside) {
|
|
|
224 |
$mform->closeHeaderBefore($addfieldsname);
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
return $repeats;
|
|
|
228 |
}
|
|
|
229 |
}
|