forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.php
126 lines (103 loc) · 4.65 KB
/
send.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?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/>.
/**
* @author Luis Rodrigues and Martin Dougiamas
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package message
*/
require('../config.php');
require('lib.php');
require('send_form.php');
require_login();
if (isguestuser()) {
redirect($CFG->wwwroot);
}
if (empty($CFG->messaging)) {
print_error('disabled', 'message');
}
if (has_capability('moodle/site:sendmessage', get_context_instance(CONTEXT_SYSTEM))) {
$PAGE->set_generaltype('popup');
$PAGE->set_title('send');
$PAGE->requires->js('message/message.js');
/// Script parameters
$userid = required_param('id', PARAM_INT);
$PAGE->set_url(new moodle_url($CFG->wwwroot.'/message/send.php', array('id'=>$userid)));
/// Check the user we are talking to is valid
if (! $user = $DB->get_record('user', array('id'=>$userid))) {
print_error('invaliduserid');
}
/// Check that the user is not blocking us!!
if ($contact = $DB->get_record('message_contacts', array('userid'=>$user->id, 'contactid'=>$USER->id))) {
if ($contact->blocked and !has_capability('moodle/site:readallmessages', get_context_instance(CONTEXT_SYSTEM))) {
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('userisblockingyou', 'message'), 1);
echo $OUTPUT->footer();
exit;
}
}
$userpreferences = get_user_preferences(NULL, NULL, $user->id);
if (!empty($userpreferences['message_blocknoncontacts'])) { // User is blocking non-contacts
if (empty($contact)) { // We are not a contact!
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('userisblockingyounoncontact', 'message'), 1);
echo $OUTPUT->footer();
exit;
}
}
$mform = new send_form();
$defaultmessage = new stdClass;
$defaultmessage->id = $userid;
$defaultmessage->message = '';
if (can_use_html_editor() && get_user_preferences('message_usehtmleditor', 0)) {
$defaultmessage->messageformat = FORMAT_HTML;
} else {
$defaultmessage->messageformat = FORMAT_MOODLE;
}
$mform->set_data($defaultmessage);
echo $OUTPUT->header();
if ($data = $mform->get_data()) { /// Current user has just sent a message
if (!confirm_sesskey()) {
print_error('invalidsesskey');
}
/// Save it to the database...
$messageid = message_post_message($USER, $user, $data->message, $data->messageformat, 'direct');
/// Format the message as HTML
$options = new stdClass;
$options->para = false;
$options->newlines = true;
$message = format_text($data->message, $data->messageformat, $options);
$time = userdate(time(), get_string('strftimedatetimeshort'));
$message = '<div class="message me"><span class="author">'.fullname($USER).'</span> '.
'<span class="time">['.$time.']</span>: '.
'<span class="content">'.$message.'</span></div>';
//$PAGE->requires->js_function_call('parent.messages.document.write', Array($message));
$PAGE->requires->js_function_call('add_message', Array($message));
$PAGE->requires->js_function_call('parent.messages.scroll', Array(1,5000000));
add_to_log(SITEID, 'message', 'write', 'history.php?user1='.$user->id.'&user2='.$USER->id.'#m'.$messageid, $user->id);
echo $OUTPUT->notification(get_string('mailsent', 'message'), 'notifysuccess');
$mform->reset_message();
}
$mform->display();
echo $OUTPUT->box_start('noframesjslink');
$accesslink = new html_link();
$accesslink->url = new moodle_url($CFG->wwwroot.'/message/discussion.php', array('id'=>$userid, 'noframesjs'=>1));
$accesslink->text = get_string('noframesjs', 'message');
$accesslink->add_action(new breakout_of_frame_action());
echo $OUTPUT->link($accesslink);
echo $OUTPUT->box_end();
$PAGE->requires->js_function_call('set_focus', Array('id_message_editor'));
echo $OUTPUT->footer();
}