forked from roelmann/moodle-block_course_contacts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_form.php
executable file
·90 lines (76 loc) · 3.8 KB
/
email_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
<?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/>.
/**
* Block Course_Contacts email_form file.
*
* @package block_course_contacts
* @author Mark Ward
* 2016 Richard Oelmann
* @copyright Mark Ward
* @credits 2016 R. Oelmann
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->libdir . '/formslib.php');
class simple_email_form extends moodleform {
public function definition() {
global $CFG, $USER, $COURSE, $OUTPUT;
$mform =& $this->_form;
$mailto = $this->_customdata['mailto'];
$touid = $this->_customdata['touid'];
$context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
$mform->addElement('header', 'simpleemail_topsection',
get_string('sendanemail', 'block_course_contacts'));
$mform->addElement('hidden', 'mailto', $mailto);
$mform->addElement('hidden', 'cid', $COURSE->id);
$mform->addElement('static', 'emailinfo', '',
str_replace('{recipient}', strtolower($mailto),
get_string('emailinfo', 'block_course_contacts')));
$mform->addElement('html', '<br />');
$mform->addElement('static', 'from', get_string('from', 'block_course_contacts'), strtolower($USER->email));
$mform->addElement('static', 'to', get_string('to', 'block_course_contacts'), strtolower($mailto));
$mform->addElement('text', 'subject', get_string('subject', 'block_course_contacts'));
$mform->setType('subject', PARAM_TEXT);
$mform->setDefault('subject', $COURSE->fullname);
$mform->addRule('subject', null, 'required');
$mform->addElement('editor', 'message', get_string('message', 'block_course_contacts'));
$mform->addRule('message', null, 'required');
if (!empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey) && $CFG->block_co_co_recaptcha) {
$mform->addElement('recaptcha', 'recaptcha_element', get_string('recaptcha', 'auth'));
}
$buttons = array();
$buttons[] =& $mform->createElement('submit', 'send', get_string('send', 'block_course_contacts'));
$buttons[] =& $mform->createElement('cancel', 'cancel', get_string('cancel'));
$mform->addGroup($buttons, 'buttons', '', array(' '), false);
}
public function validation($data, $files) {
global $CFG, $DB;
$errors = parent::validation($data, $files);
if (!empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey) && $CFG->block_co_co_recaptcha) {
$recaptchaelement = $this->_form->getElement('recaptcha_element');
if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
$challengefield = $this->_form->_submitValues['recaptcha_challenge_field'];
$responsefield = $this->_form->_submitValues['recaptcha_response_field'];
if (true !== ($result = $recaptchaelement->verify($challengefield, $responsefield))) {
$errors['recaptcha'] = $result;
}
} else {
$errors['recaptcha'] = get_string('missingrecaptchachallengefield');
}
}
return $errors;
}
}