Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

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