Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of eMailTest plugin for Moodle - https://moodle.org/
3
//
4
// eMailTest 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
// eMailTest 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 eMailTest.  If not, see <https://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Main form for eMailTest.
19
 *
20
 * @package    local_mailtest
21
 * @copyright  2015-2024 TNG Consulting Inc. - www.tngcosulting.ca
22
 * @author     Michael Milette
23
 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
defined('MOODLE_INTERNAL') || die;
27
require_once($CFG->libdir . '/formslib.php');
28
 
29
/**
30
 * Form to prompt administrator for the recipient's email address.
31
 * @copyright  2015-2024 TNG Consulting Inc. - www.tngcosulting.ca
32
 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class mailtest_form extends moodleform {
35
    /**
36
     * Define the form.
37
     */
38
    public function definition() {
39
        global $USER, $CFG;
40
        $mform = $this->_form;
41
 
42
        // Header.
43
 
44
        $mform->addElement('html', '<p>' . get_string('pluginname_help', 'local_mailtest') . '</p>');
45
 
46
        // Send method.
47
 
48
        if (empty($CFG->smtphosts)) {
49
            $sendmethod = get_string('phpmethod', 'local_mailtest');
50
        } else {
51
            $sendmethod = get_string('smtpmethod', 'local_mailtest', $CFG->smtphosts);
52
        }
53
        if ($CFG->branch >= 32) {
54
            $sendmethod .= ' (<a href="../../admin/settings.php?section=outgoingmailconfig#admin-smtphosts">' .
55
                    get_string('change', 'admin') . '</a>)';
56
        } else {
57
            $sendmethod .= ' (<a href="../../admin/settings.php?section=messagesettingemail">' .
58
                    get_string('change', 'admin') . '</a>)';
59
        }
60
        $mform->addElement('static', 'sendmethod', get_string('sendmethod', 'local_mailtest'), $sendmethod);
61
 
62
        // Sender.
63
 
64
        $senderarray = [];
65
        $a = new stdClass();
66
        $a->label = get_string('change', 'admin');
67
 
68
        // Current user's email address.
69
        $a->email = $USER->email;
70
        if ($CFG->branch >= 32) {
71
            $a->url = '../../user/editadvanced.php?course=1#id_email';
72
        } else {
73
            $a->url = '../../user/editadvanced.php?course=1#fitem_id_email';
74
        }
75
        $a->type = get_string('youremail', 'local_mailtest');
76
        $senderarray[] = $mform->createElement('radio', 'sender', '', get_string('from', 'local_mailtest', $a)
77
            . local_mailtest_checkdns(explode('@', $a->email)[1]), $a->email);
78
        if (!validate_email($a->email)) {
79
            $senderarray[] = $mform->CreateElement(
80
                'static',
81
                'error',
82
                '',
83
                html_writer::span(get_string('invalidemail'), 'statuswarning')
84
            );
85
        }
86
 
87
        // Support email address.
88
        $primaryadmin = get_admin();
89
        $a->email = empty($CFG->supportemail) ? $primaryadmin->email : $CFG->supportemail;
90
        $a->url = '../../admin/settings.php?section=supportcontact';
91
        $a->type = get_string('supportemail', 'admin');
92
        $senderarray[] = $mform->createElement('radio', 'sender', '', get_string('from', 'local_mailtest', $a)
93
            . local_mailtest_checkdns(explode('@', $a->email)[1]), $a->email);
94
        if (!validate_email($a->email)) {
95
            $senderarray[] = $mform->CreateElement(
96
                'static',
97
                'error',
98
                '',
99
                html_writer::span(get_string('invalidemail'), 'statuswarning')
100
            );
101
        }
102
 
103
        // No Reply address.
104
        $a->email = empty($CFG->noreplyaddress) ? 'noreply@' . get_host_from_url($CFG->wwwroot) : $CFG->noreplyaddress;
105
        if ($CFG->branch >= 32) {
106
            $a->url = '../../admin/settings.php?section=outgoingmailconfig#admin-noreplyaddress';
107
            $a->type = get_string('noreplyaddress', 'admin');
108
        } else {
109
            $a->url = '../../admin/settings.php?section=messagesettingemail#noreplyaddress';
110
            $a->type = get_string('noreplyaddress', 'message_email');
111
        }
112
        $senderarray[] = $mform->createElement('radio', 'sender', '', get_string('from', 'local_mailtest', $a)
113
            . local_mailtest_checkdns(explode('@', $a->email)[1]), $a->email);
114
        if (!validate_email($a->email)) {
115
            $senderarray[] = $mform->CreateElement(
116
                'static',
117
                'error',
118
                '',
119
                html_writer::span(get_string('invalidemail'), 'statuswarning')
120
            );
121
        }
122
 
123
        // Primary admin email address.
124
        $primaryadmin = get_admin();
125
        $a->email = $primaryadmin->email;
126
        $a->url = '../../user/editadvanced.php?id=' . $primaryadmin->id;
127
        $a->type = get_string('primaryadminemail', 'local_mailtest');
128
        $senderarray[] = $mform->createElement('radio', 'sender', '', get_string('from', 'local_mailtest', $a)
129
            . local_mailtest_checkdns(explode('@', $a->email)[1]), $a->email);
130
        if (!validate_email($a->email)) {
131
            $senderarray[] = $mform->CreateElement(
132
                'static',
133
                'error',
134
                '',
135
                html_writer::span(get_string('invalidemail'), 'statuswarning')
136
            );
137
        }
138
 
139
        // Add group of sender radio buttons to form.
140
        $mform->setDefault('sender', $this->_customdata['fromdefault']);
141
        $mform->addGroup($senderarray, 'senderar', get_string('fromemail', 'local_mailtest'), ['<br />'], false);
142
        $mform->setType('sender', PARAM_EMAIL);
143
        $mform->addRule('senderar', get_string('required'), 'required');
144
 
145
        // Recipient.
146
 
147
        $mform->addElement('text', 'recipient', get_string('toemail', 'local_mailtest'), 'maxlength="100" size="25" ');
148
        $mform->setType('recipient', PARAM_EMAIL);
149
        $mform->addRule('recipient', get_string('required'), 'required');
150
 
151
        // Divert all emails.
152
 
153
        if (empty($CFG->divertallemailsto)) {
154
            $divertstatus = get_string('functiondisabled');
155
        } else {
156
            $divertstatus = get_string('divertedto', 'local_mailtest', $CFG->divertallemailsto);
157
        }
158
        if ($CFG->branch >= 310) {
159
            // Only configurable in UI as of Moodle 3.10.
160
            $divertstatus .= ' (<a href="../../admin/settings.php?section=outgoingmailconfig#admin-divertallemailsto">' .
161
                    get_string('change', 'admin') . '</a>)';
162
        }
163
        if (!empty($CFG->divertallemailsto)) {
164
            $divertstatus .= local_mailtest_checkdns(explode('@', $CFG->divertallemailsto)[1]);
165
        }
166
        $mform->addElement('static', 'divertemails', get_string('divertallemails', 'local_mailtest'), $divertstatus);
167
 
168
        // Always show communications log - even on success.
169
 
170
        $mform->addElement('checkbox', 'alwaysshowlog', '', get_string('alwaysshowlog', 'local_mailtest'));
171
        $mform->setDefault('alwaysshowlog', ($CFG->debugdisplay && isset($CFG->debugsmtp) && $CFG->debugsmtp));
172
 
173
        // Buttons.
174
 
175
        if ($disabled = !empty($CFG->noemailever)) {
176
            $mform->addElement(
177
                'static',
178
                'noemailever',
179
                html_writer::div(get_string('messagingdisable', 'error'), 'alert alert-danger'),
180
                html_writer::div(get_string('noemaileverset', 'message_airnotifier'), 'alert alert-danger')
181
            );
182
        }
183
        $buttonarray = [];
184
        if (!$disabled) {
185
            $buttonarray[] = $mform->createElement('submit', 'send', get_string('sendtest', 'local_mailtest'));
186
        }
187
        $buttonarray[] = $mform->createElement('cancel');
188
 
189
        $mform->addGroup($buttonarray, 'buttonar', '', [' '], false);
190
        $mform->closeHeaderBefore('buttonar');
191
    }
192
 
193
    /**
194
     * Validate submitted form data, recipient in this case, and returns list of errors if it fails.
195
     *
196
     * @param      array  $data   The data fields submitted from the form.
197
     * @param      array  $files  Files submitted from the form (not used)
198
     *
199
     * @return     array  List of errors to be displayed on the form if validation fails.
200
     */
201
    public function validation($data, $files) {
202
        $errors = parent::validation($data, $files);
203
 
204
        if (empty($data['recipient'])) {
205
            $errors['recipient'] = get_string('err_email', 'form');
206
        }
207
 
208
        return $errors;
209
    }
210
}