Merge branch 'MDL-63303_master-deleteduserfix' of https://github.com/markn86/moodle
[moodle.git] / admin / user / user_bulk_message.php
blobadf90ffb356ee3a50d9f67d431db5594296c418b
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_CLEANHTML);
8 $confirm = optional_param('confirm', 0, PARAM_BOOL);
10 admin_externalpage_setup('userbulk');
11 require_capability('moodle/site:manageallmessaging', context_system::instance());
13 $return = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php';
15 if (empty($SESSION->bulk_users)) {
16 redirect($return);
19 if (empty($CFG->messaging)) {
20 print_error('messagingdisable', 'error');
23 //TODO: add support for large number of users
25 if ($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);
34 $rs->close();
35 redirect($return);
38 $msgform = new user_message_form('user_bulk_message.php');
40 if ($msgform->is_cancelled()) {
41 redirect($return);
43 } else if ($formdata = $msgform->get_data()) {
44 $options = new stdClass();
45 $options->para = false;
46 $options->newlines = true;
47 $options->smiley = false;
49 $msg = format_text($formdata->messagebody['text'], $formdata->messagebody['format'], $options);
51 list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
52 $userlist = $DB->get_records_select_menu('user', "id $in", $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname');
53 $usernames = implode(', ', $userlist);
54 echo $OUTPUT->header();
55 echo $OUTPUT->heading(get_string('confirmation', 'admin'));
56 echo $OUTPUT->box($msg, 'boxwidthnarrow boxaligncenter generalbox', 'preview'); //TODO: clean once we start using proper text formats here
58 $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
59 $formcancel = new single_button(new moodle_url('user_bulk.php'), get_string('no'), 'get');
60 echo $OUTPUT->confirm(get_string('confirmmessage', 'bulkusers', $usernames), $formcontinue, $formcancel);
61 echo $OUTPUT->footer();
62 die;
65 echo $OUTPUT->header();
66 $msgform->display();
67 echo $OUTPUT->footer();