1<?php
2require_once('../../config.php');
3require_once($CFG->libdir.'/adminlib.php');
4require_once($CFG->dirroot.'/message/lib.php');
5require_once('user_message_form.php');
6
7$msg     = optional_param('msg', '', PARAM_RAW);
8$confirm = optional_param('confirm', 0, PARAM_BOOL);
9
10admin_externalpage_setup('userbulk');
11require_capability('moodle/site:manageallmessaging', context_system::instance());
12
13$return = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php';
14
15if (empty($SESSION->bulk_users)) {
16    redirect($return);
17}
18
19if (empty($CFG->messaging)) {
20    print_error('messagingdisable', 'error');
21}
22
23//TODO: add support for large number of users
24
25if ($confirm and !empty($msg) and confirm_sesskey()) {
26    list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
27    $rs = $DB->get_recordset_select('user', "id $in", $params);
28    foreach ($rs as $user) {
29        //TODO we should probably support all text formats here or only FORMAT_MOODLE
30        //For now bulk messaging is still using the html editor and its supplying html
31        //so we have to use html format for it to be displayed correctly
32        message_post_message($USER, $user, $msg, FORMAT_HTML);
33    }
34    $rs->close();
35    redirect($return);
36}
37
38$msgform = new user_message_form('user_bulk_message.php');
39
40if ($msgform->is_cancelled()) {
41    redirect($return);
42
43} else if ($formdata = $msgform->get_data()) {
44    $options = new stdClass();
45    $options->para     = false;
46    $options->newlines = true;
47    $options->smiley   = false;
48    $options->trusted = trusttext_trusted(\context_system::instance());
49
50    $msg = format_text($formdata->messagebody['text'], $formdata->messagebody['format'], $options);
51
52    list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
53    $userlist = $DB->get_records_select_menu('user', "id $in", $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname');
54    $usernames = implode(', ', $userlist);
55    echo $OUTPUT->header();
56    echo $OUTPUT->heading(get_string('confirmation', 'admin'));
57    echo $OUTPUT->box($msg, 'boxwidthnarrow boxaligncenter generalbox', 'preview'); //TODO: clean once we start using proper text formats here
58
59    $formcontinue = new single_button(new moodle_url('user_bulk_message.php', array('confirm' => 1, 'msg' => $msg)), get_string('yes')); //TODO: clean once we start using proper text formats here
60    $formcancel = new single_button(new moodle_url('user_bulk.php'), get_string('no'), 'get');
61    echo $OUTPUT->confirm(get_string('confirmmessage', 'bulkusers', $usernames), $formcontinue, $formcancel);
62    echo $OUTPUT->footer();
63    die;
64}
65
66echo $OUTPUT->header();
67$msgform->display();
68echo $OUTPUT->footer();
69