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