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 |
* Defines the custom messaging form
|
|
|
19 |
*
|
|
|
20 |
* @package block_messageteacher
|
|
|
21 |
* @author Mark Johnson <mark@barrenfrozenwasteland.com>
|
|
|
22 |
* @copyright 2013 Mark Johnson
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
namespace block_messageteacher;
|
|
|
27 |
|
|
|
28 |
use context;
|
|
|
29 |
use moodle_url;
|
|
|
30 |
|
|
|
31 |
defined('MOODLE_INTERNAL') || die();
|
|
|
32 |
|
|
|
33 |
require_once($CFG->libdir . '/formslib.php');
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Custom messaging form.
|
|
|
37 |
*
|
|
|
38 |
* @copyright 2013 Mark Johnson
|
|
|
39 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
40 |
*/
|
|
|
41 |
class message_form extends \core_form\dynamic_form {
|
|
|
42 |
|
|
|
43 |
protected function get_context_for_dynamic_submission(): context {
|
|
|
44 |
if ($contextid = $this->optional_param('contextid', null, PARAM_INT)) {
|
|
|
45 |
return \context::instance_by_id($contextid, MUST_EXIST);
|
|
|
46 |
} else {
|
|
|
47 |
$courseid = $this->optional_param('courseid', null, PARAM_INT);
|
|
|
48 |
return \context_course::instance($courseid);
|
|
|
49 |
}
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
public function set_data_for_dynamic_submission(): void {
|
|
|
53 |
global $DB;
|
|
|
54 |
|
|
|
55 |
$this->set_data([
|
|
|
56 |
'recipientid' => $this->optional_param('recipientid', null, PARAM_INT),
|
|
|
57 |
'referurl' => $this->optional_param('referurl', null, PARAM_URL),
|
|
|
58 |
'courseid' => $this->optional_param('courseid', null, PARAM_INT)
|
|
|
59 |
]);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
protected function check_access_for_dynamic_submission(): void {
|
|
|
63 |
require_capability('moodle/site:sendmessage', $this->get_context_for_dynamic_submission());
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
protected function get_page_url_for_dynamic_submission(): moodle_url {
|
|
|
67 |
return new moodle_url($this->optional_param('referurl', null, PARAM_URL));
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
public function process_dynamic_submission() {
|
|
|
71 |
return $this->process($this->get_data());
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Define the form.
|
|
|
76 |
*/
|
|
|
77 |
public function definition() {
|
|
|
78 |
$mform = $this->_form;
|
|
|
79 |
$mform->disable_form_change_checker();
|
|
|
80 |
$strrequired = get_string('required');
|
|
|
81 |
|
|
|
82 |
$mform->addElement('textarea',
|
|
|
83 |
'message',
|
|
|
84 |
get_string('messagetext', 'block_messageteacher'),
|
|
|
85 |
array('rows' => 6, 'cols' => 60));
|
|
|
86 |
$mform->setType('message', PARAM_TEXT);
|
|
|
87 |
|
|
|
88 |
$mform->addRule('message', $strrequired, 'required', null, 'client');
|
|
|
89 |
|
|
|
90 |
$mform->addElement('hidden', 'referurl');
|
|
|
91 |
$mform->setType('referurl', PARAM_URL);
|
|
|
92 |
|
|
|
93 |
$mform->addElement('hidden', 'recipientid');
|
|
|
94 |
$mform->setType('recipientid', PARAM_INT);
|
|
|
95 |
|
|
|
96 |
$mform->addElement('hidden', 'courseid');
|
|
|
97 |
$mform->setType('courseid', PARAM_INT);
|
|
|
98 |
|
|
|
99 |
if (empty($this->_ajaxformdata)) {
|
|
|
100 |
$mform->addElement('submit', 'send', get_string('send', 'block_messageteacher'));
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
public function definition_after_data() {
|
|
|
106 |
global $DB;
|
|
|
107 |
$mform = $this->_form;
|
|
|
108 |
$recipientid = $mform->getElementValue('recipientid');
|
|
|
109 |
$recipient = $DB->get_record('user', ['id' => $recipientid], '*', MUST_EXIST);
|
|
|
110 |
$header = $mform->createElement('header', 'messsageheader',
|
|
|
111 |
get_string('messageheader', 'block_messageteacher', fullname($recipient)));
|
|
|
112 |
$mform->insertElementBefore($header, 'message');
|
|
|
113 |
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Validate and send the message.
|
|
|
118 |
*
|
|
|
119 |
* @param \stdClass $data Form data
|
|
|
120 |
* @return true
|
|
|
121 |
*/
|
|
|
122 |
public function process($data) {
|
|
|
123 |
global $DB, $USER, $COURSE;
|
|
|
124 |
if (!$recipient = $DB->get_record('user', array('id' => $data->recipientid))) {
|
|
|
125 |
throw new no_recipient_exception($data->recipientid);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
$appendurl = get_config('block_messageteacher', 'appendurl');
|
|
|
129 |
if ($appendurl) {
|
|
|
130 |
$data->message .= "\n\n".get_string('sentfrom', 'block_messageteacher', $data->referurl);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
$eventdata = new \core\message\message();
|
|
|
134 |
$eventdata->component = 'moodle';
|
|
|
135 |
$eventdata->name = 'instantmessage';
|
|
|
136 |
$eventdata->userfrom = $USER;
|
|
|
137 |
$eventdata->userto = $recipient;
|
|
|
138 |
$eventdata->subject = get_string('messagefrom', 'block_messageteacher', fullname($USER));
|
|
|
139 |
$eventdata->fullmessage = $data->message;
|
|
|
140 |
$eventdata->fullmessageformat = FORMAT_PLAIN;
|
|
|
141 |
$eventdata->fullmessagehtml = '';
|
|
|
142 |
$eventdata->smallmessage = '';
|
|
|
143 |
$eventdata->notification = 0;
|
|
|
144 |
$eventdata->courseid = $COURSE->id;
|
|
|
145 |
|
|
|
146 |
if (!$id = message_send($eventdata)) {
|
|
|
147 |
throw new message_failed_exception();
|
|
|
148 |
}
|
|
|
149 |
return $id;
|
|
|
150 |
}
|
|
|
151 |
}
|