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 |
* This file manages the public functions of this module
|
|
|
19 |
*
|
|
|
20 |
* @package mod_custommailing
|
|
|
21 |
* @author jeanfrancois@cblue.be,olivier@cblue.be
|
|
|
22 |
* @copyright 2021 CBlue SPRL
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
defined('MOODLE_INTERNAL') || die;
|
|
|
27 |
|
|
|
28 |
global $CFG;
|
|
|
29 |
require_once $CFG->libdir . '/formslib.php';
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Class mailing_form
|
|
|
33 |
*/
|
|
|
34 |
class mailing_form extends moodleform
|
|
|
35 |
{
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* @throws coding_exception
|
|
|
39 |
* @throws moodle_exception
|
|
|
40 |
*/
|
|
|
41 |
public function definition()
|
|
|
42 |
{
|
|
|
43 |
global $COURSE, $PAGE;
|
|
|
44 |
|
|
|
45 |
$cert = false;
|
|
|
46 |
$mform =& $this->_form;
|
|
|
47 |
$course_module_context = $PAGE->context;
|
|
|
48 |
$custom_cert = core_plugin_manager::instance()->get_plugin_info('mod_customcert');
|
|
|
49 |
if (!empty($this->_customdata['mailingid'])) {
|
|
|
50 |
$mailing = \mod_custommailing\Mailing::get($this->_customdata['mailingid']);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
$days = [];
|
|
|
54 |
for ($i = 1; $i <= 30; $i++) {
|
|
|
55 |
$days[$i] = $i;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
$hours = [];
|
|
|
59 |
for ($i = 0; $i < 24; $i++) {
|
|
|
60 |
if ($i < 10) {
|
|
|
61 |
$hours[$i] = "0$i";
|
|
|
62 |
} else {
|
|
|
63 |
$hours[$i] = $i;
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
$minutes = [];
|
|
|
68 |
for ($i = 0; $i < 60; $i += 5) {
|
|
|
69 |
if ($i < 10) {
|
|
|
70 |
$minutes[$i] = "0$i";
|
|
|
71 |
} else {
|
|
|
72 |
$minutes[$i] = $i;
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
$scorm = custommailing_get_activities('scorm');
|
|
|
77 |
if ($custom_cert) {
|
|
|
78 |
$cert = custommailing_get_activities('customcert');
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
$source = [];
|
|
|
82 |
$source[0] = get_string('select', 'mod_custommailing');
|
|
|
83 |
if ($scorm) {
|
|
|
84 |
$source[MAILING_SOURCE_MODULE] = get_string('module', 'mod_custommailing');
|
|
|
85 |
}
|
|
|
86 |
$source[MAILING_SOURCE_COURSE] = get_string('course', 'mod_custommailing');
|
|
|
87 |
if ($cert) {
|
|
|
88 |
$source[MAILING_SOURCE_CERT] = get_string('certificate', 'mod_custommailing');
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
// Add id
|
|
|
92 |
$mform->addElement('hidden', 'id', 'id');
|
|
|
93 |
$mform->setType('id', PARAM_INT);
|
|
|
94 |
$mform->setDefault('id', $course_module_context->instanceid);
|
|
|
95 |
|
|
|
96 |
if (!empty($this->_customdata['mailingid'])) {
|
|
|
97 |
// Add mailingid
|
|
|
98 |
$mform->addElement('hidden', 'mailingid', 'mailingid');
|
|
|
99 |
$mform->setType('mailingid', PARAM_INT);
|
|
|
100 |
$mform->setDefault('mailingid', $this->_customdata['mailingid']);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
// Add name
|
|
|
104 |
$mform->addElement('text', 'mailingname', get_string('mailingname', 'mod_custommailing'), 'maxlength="255" size="32"');
|
|
|
105 |
$mform->setType('mailingname', PARAM_RAW_TRIMMED);
|
|
|
106 |
$mform->addRule('mailingname', get_string('required'), 'required');
|
|
|
107 |
|
|
|
108 |
// Todo v2 : Add lang
|
|
|
109 |
// $mform->addElement('select', 'mailinglang', get_string('mailinglang', 'mod_custommailing'), get_string_manager()->get_list_of_translations());
|
|
|
110 |
// $mform->setType('mailinglang', PARAM_LANG);
|
|
|
111 |
// $mform->addRule('mailinglang', get_string('required'), 'required');
|
|
|
112 |
|
|
|
113 |
// Select Source
|
|
|
114 |
$mform->addElement('select', 'source', get_string('selectsource', 'mod_custommailing'), $source);
|
|
|
115 |
$mform->setType('source', PARAM_INT);
|
|
|
116 |
if (!empty($this->_customdata['mailingid'])) {
|
|
|
117 |
$mform->disabledIf('source', 'mailingid', 'noteq', 0);
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
// Add target activity
|
|
|
121 |
$mform->addElement('select', 'targetmoduleid', get_string('targetmoduleid', 'mod_custommailing'), $scorm);
|
|
|
122 |
$mform->setType('targetmoduleid', PARAM_INT);
|
|
|
123 |
$mform->hideIf('targetmoduleid', 'source', 'noteq', 1);
|
|
|
124 |
|
|
|
125 |
// Add custom cert
|
|
|
126 |
if ($cert) {
|
|
|
127 |
$mform->addElement('select', 'customcert', get_string('certificate', 'mod_custommailing'), custommailing_getcustomcertsfromcourse($COURSE->id));
|
|
|
128 |
$mform->setType('customcert', PARAM_INT);
|
|
|
129 |
$mform->hideIf('customcert', 'source', 'noteq', 3);
|
|
|
130 |
$mform->addHelpButton('customcert', 'customcert', 'mod_custommailing');
|
|
|
131 |
if (!empty($mailing->customcertmoduleid)) {
|
|
|
132 |
$mform->setDefault('customcert', $mailing->customcertmoduleid);
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
// Add mode
|
|
|
137 |
$mailing_mode_module = [];
|
|
|
138 |
|
|
|
139 |
$mailing_mode_module[] =& $mform->createElement('radio', 'mailingmodemodule', null, '', 'option');
|
|
|
140 |
$mailing_mode_module[] =& $mform->createElement('select', 'mailingdelaymodule', null, $days);
|
|
|
141 |
$mailing_mode_module[] =& $mform->createElement(
|
|
|
142 |
'select', 'mailingmodemoduleoption', null, [
|
|
|
143 |
MAILING_MODE_DAYSFROMFIRSTLAUNCH => get_string('firstlaunch', 'mod_custommailing'),
|
|
|
144 |
MAILING_MODE_DAYSFROMLASTLAUNCH => get_string('lastlaunch', 'mod_custommailing'),
|
|
|
145 |
]
|
|
|
146 |
);
|
|
|
147 |
$mailing_mode_module[] =& $mform->createElement('checkbox', 'mailingmodecompletion', get_string('andtargetactivitynotcompleted', 'mod_custommailing'));
|
|
|
148 |
$mform->addGroup($mailing_mode_module, 'mailingmodemodulegroup', get_string('sendmailing', 'mod_custommailing'), ' ', false);
|
|
|
149 |
$mform->setType('mailingmodemodule', PARAM_INT);
|
|
|
150 |
$mform->setDefault('mailingmodemodule', 0);
|
|
|
151 |
$mform->hideIf('mailingmodemodule', 'source', 'noteq', 1);
|
|
|
152 |
$mform->hideIf('mailingdelaymodule', 'source', 'noteq', 1);
|
|
|
153 |
$mform->hideIf('mailingmodemoduleoption', 'source', 'noteq', 1);
|
|
|
154 |
$mform->hideIf('mailingmodemodulegroup', 'source', 'noteq', 1);
|
|
|
155 |
if (!empty($source[MAILING_SOURCE_COURSE]) && !empty($mailing->mailingmode)) {
|
|
|
156 |
$mform->setDefault('mailingmodemoduleoption', $mailing->mailingmode);
|
|
|
157 |
if (!empty($mailing->mailingdelay)) {
|
|
|
158 |
$mform->setDefault('mailingdelaymodule', $mailing->mailingdelay);
|
|
|
159 |
}
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
$mailing_mode[] =& $mform->createElement('radio', 'mailingmode', null, '', 'option');
|
|
|
163 |
$mailing_mode[] =& $mform->createElement('select', 'mailingdelay', null, $days);
|
|
|
164 |
$mailing_mode[] =& $mform->createElement(
|
|
|
165 |
'select', 'mailingmodeoption', null, [
|
|
|
166 |
MAILING_MODE_DAYSFROMINSCRIPTIONDATE => get_string('courseenroldate', 'mod_custommailing'),
|
|
|
167 |
MAILING_MODE_DAYSFROMLASTCONNECTION => get_string('courselastaccess', 'mod_custommailing'),
|
|
|
168 |
]
|
|
|
169 |
);
|
|
|
170 |
$mform->addGroup($mailing_mode, 'mailingmodegroup', get_string('sendmailing', 'mod_custommailing'), ' ', false);
|
|
|
171 |
$mform->addElement('radio', 'mailingmode', null, get_string('atcourseenrol', 'mod_custommailing'), MAILING_MODE_REGISTRATION);
|
|
|
172 |
$mform->setType('mailingmode', PARAM_RAW);
|
|
|
173 |
$mform->setDefault('mailingmode', 0);
|
|
|
174 |
$mform->hideIf('mailingmode', 'source', 'noteq', 2);
|
|
|
175 |
$mform->hideIf('mailingdelay', 'source', 'noteq', 2);
|
|
|
176 |
$mform->hideIf('mailingmodeoption', 'source', 'noteq', 2);
|
|
|
177 |
$mform->hideIf('mailingmodegroup', 'source', 'noteq', 2);
|
|
|
178 |
if (!empty($mailing->targetmodulestatus)) {
|
|
|
179 |
$mform->setDefault('mailingmodecompletion', $mailing->targetmodulestatus);
|
|
|
180 |
$mform->setDefault('mailingmode', $mailing->mailingmode);
|
|
|
181 |
$mform->setDefault('mailingmodeoption', $mailing->mailingmode);
|
|
|
182 |
if (!empty($mailing->mailingdelay)) {
|
|
|
183 |
$mform->setDefault('mailingdelay', $mailing->mailingdelay);
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
// Add retroactive mode
|
|
|
188 |
$mform->addElement('selectyesno', 'retroactive', get_string('retroactive', 'mod_custommailing'));
|
|
|
189 |
$mform->setType('retroactive', PARAM_BOOL);
|
|
|
190 |
$mform->setDefault('retroactive', 0);
|
|
|
191 |
$mform->addHelpButton('retroactive', 'retroactive', 'mod_custommailing');
|
|
|
192 |
if (!empty($this->_customdata['mailingid'])) {
|
|
|
193 |
$mform->disabledIf('retroactive', 'mailingid', 'noteq', 0);
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
// Add subject
|
|
|
197 |
$mform->addElement('text', 'mailingsubject', get_string('mailingsubject', 'mod_custommailing'));
|
|
|
198 |
$mform->setType('mailingsubject', PARAM_RAW_TRIMMED);
|
|
|
199 |
$mform->addRule('mailingsubject', get_string('required'), 'required');
|
|
|
200 |
|
|
|
201 |
// Add body
|
|
|
202 |
$mform->addElement('editor', 'mailingcontent', get_string('mailingcontent', 'mod_custommailing'), '', ['enable_filemanagement' => false]);
|
|
|
203 |
$mform->setType('mailingcontent', PARAM_RAW);
|
|
|
204 |
$mform->addRule('mailingcontent', get_string('required'), 'required');
|
|
|
205 |
$mform->addHelpButton('mailingcontent', 'mailingcontent', 'mod_custommailing');
|
|
|
206 |
|
|
|
207 |
//Todo v2 : starttime
|
|
|
208 |
// $start_time = [];
|
|
|
209 |
// $start_time[] =& $mform->createElement('select', 'starttimehour', '', $hours);
|
|
|
210 |
// $start_time[] =& $mform->createElement('static', '', null, ' : ');
|
|
|
211 |
// $start_time[] =& $mform->createElement('select', 'starttimeminute', '', $minutes);
|
|
|
212 |
// $mform->addGroup($start_time, 'starttime', get_string('starttime', 'mod_custommailing'), ' ', false);
|
|
|
213 |
// $mform->addRule('starttime', get_string('required'), 'required');
|
|
|
214 |
|
|
|
215 |
// Add status
|
|
|
216 |
$mform->addElement('selectyesno', 'mailingstatus', get_string('enabled', 'mod_custommailing'));
|
|
|
217 |
$mform->setType('mailingstatus', PARAM_BOOL);
|
|
|
218 |
$mform->setDefault('mailingstatus', 1);
|
|
|
219 |
|
|
|
220 |
// Add standard buttons, common to all modules.
|
|
|
221 |
if (!empty($this->_customdata['mailingid'])) {
|
|
|
222 |
$submitlabel = get_string('updatemailing', 'mod_custommailing');
|
|
|
223 |
} else {
|
|
|
224 |
$submitlabel = get_string('createmailing', 'mod_custommailing');
|
|
|
225 |
}
|
|
|
226 |
$this->add_action_buttons(true, $submitlabel);
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* @param array $data
|
|
|
231 |
* @param array $files
|
|
|
232 |
* @return array
|
|
|
233 |
* @throws coding_exception
|
|
|
234 |
* @throws moodle_exception
|
|
|
235 |
*/
|
|
|
236 |
public function validation($data, $files)
|
|
|
237 |
{
|
|
|
238 |
$errors = parent::validation($data, $files);
|
|
|
239 |
|
|
|
240 |
return $errors;
|
|
|
241 |
}
|
|
|
242 |
}
|