Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This file defines an adhoc task to send notifications.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_forum\task;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Adhoc task to send user forum notifications.
  31   *
  32   * @package    mod_forum
  33   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class send_user_notifications extends \core\task\adhoc_task {
  37  
  38      // Use the logging trait to get some nice, juicy, logging.
  39      use \core\task\logging_trait;
  40  
  41      /**
  42       * @var \stdClass   A shortcut to $USER.
  43       */
  44      protected $recipient;
  45  
  46      /**
  47       * @var \stdClass[] List of courses the messages are in, indexed by courseid.
  48       */
  49      protected $courses = [];
  50  
  51      /**
  52       * @var \stdClass[] List of forums the messages are in, indexed by courseid.
  53       */
  54      protected $forums = [];
  55  
  56      /**
  57       * @var int[] List of IDs for forums in each course.
  58       */
  59      protected $courseforums = [];
  60  
  61      /**
  62       * @var \stdClass[] List of discussions the messages are in, indexed by forumid.
  63       */
  64      protected $discussions = [];
  65  
  66      /**
  67       * @var \stdClass[] List of IDs for discussions in each forum.
  68       */
  69      protected $forumdiscussions = [];
  70  
  71      /**
  72       * @var \stdClass[] List of posts the messages are in, indexed by discussionid.
  73       */
  74      protected $posts = [];
  75  
  76      /**
  77       * @var bool[] Whether the user can view fullnames for each forum.
  78       */
  79      protected $viewfullnames = [];
  80  
  81      /**
  82       * @var bool[] Whether the user can post in each discussion.
  83       */
  84      protected $canpostto = [];
  85  
  86      /**
  87       * @var \renderer[] The renderers.
  88       */
  89      protected $renderers = [];
  90  
  91      /**
  92       * @var \core\message\inbound\address_manager The inbound message address manager.
  93       */
  94      protected $inboundmanager;
  95  
  96      /**
  97       * @var array List of users.
  98       */
  99      protected $users = [];
 100  
 101      /**
 102       * Send out messages.
 103       * @throws \moodle_exception
 104       */
 105      public function execute() {
 106          global $CFG;
 107  
 108          // Raise the time limit for each discussion.
 109          \core_php_time_limit::raise(120);
 110  
 111          $this->recipient = \core_user::get_user($this->get_userid());
 112  
 113          // Create the generic messageinboundgenerator.
 114          $this->inboundmanager = new \core\message\inbound\address_manager();
 115          $this->inboundmanager->set_handler('\mod_forum\message\inbound\reply_handler');
 116  
 117          $data = $this->get_custom_data();
 118  
 119          $this->prepare_data((array) $data);
 120  
 121          $failedposts = [];
 122          $markposts = [];
 123          $errorcount = 0;
 124          $sentcount = 0;
 125          $this->log_start("Sending messages to {$this->recipient->username} ({$this->recipient->id})");
 126          foreach ($this->courses as $course) {
 127              $coursecontext = \context_course::instance($course->id);
 128              if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
 129                  // The course is hidden and the user does not have access to it.
 130                  // Permissions may have changed since it was queued.
 131                  continue;
 132              }
 133              foreach ($this->courseforums[$course->id] as $forumid) {
 134                  $forum = $this->forums[$forumid];
 135  
 136                  $cm = get_fast_modinfo($course)->instances['forum'][$forumid];
 137                  $modcontext = \context_module::instance($cm->id);
 138  
 139                  foreach (array_values($this->forumdiscussions[$forumid]) as $discussionid) {
 140                      $discussion = $this->discussions[$discussionid];
 141  
 142                      if (!forum_user_can_see_discussion($forum, $discussion, $modcontext, $this->recipient)) {
 143                          // User cannot see this discussion.
 144                          // Permissions may have changed since it was queued.
 145                          continue;
 146                      }
 147  
 148                      if (!\mod_forum\subscriptions::is_subscribed($this->recipient->id, $forum, $discussionid, $cm)) {
 149                          // The user does not subscribe to this forum as a whole, or to this specific discussion.
 150                          continue;
 151                      }
 152  
 153                      foreach ($this->posts[$discussionid] as $post) {
 154                          if (!forum_user_can_see_post($forum, $discussion, $post, $this->recipient, $cm)) {
 155                              // User cannot see this post.
 156                              // Permissions may have changed since it was queued.
 157                              continue;
 158                          }
 159  
 160                          if ($this->send_post($course, $forum, $discussion, $post, $cm, $modcontext)) {
 161                              $this->log("Post {$post->id} sent", 1);
 162                              // Mark post as read if forum_usermarksread is set off.
 163                              if (!$CFG->forum_usermarksread) {
 164                                  $markposts[$post->id] = true;
 165                              }
 166                              $sentcount++;
 167                          } else {
 168                              $this->log("Failed to send post {$post->id}", 1);
 169                              $failedposts[] = $post->id;
 170                              $errorcount++;
 171                          }
 172                      }
 173                  }
 174              }
 175          }
 176  
 177          $this->log_finish("Sent {$sentcount} messages with {$errorcount} failures");
 178          if (!empty($markposts)) {
 179              if (get_user_preferences('forum_markasreadonnotification', 1, $this->recipient->id) == 1) {
 180                  $this->log_start("Marking posts as read");
 181                  $count = count($markposts);
 182                  forum_tp_mark_posts_read($this->recipient, array_keys($markposts));
 183                  $this->log_finish("Marked {$count} posts as read");
 184              }
 185          }
 186  
 187          if ($errorcount > 0 and $sentcount === 0) {
 188              // All messages errored. So fail.
 189              throw new \moodle_exception('Error sending posts.');
 190          } else if ($errorcount > 0) {
 191              // Requeue failed messages as a new task.
 192              $task = new send_user_notifications();
 193              $task->set_userid($this->recipient->id);
 194              $task->set_custom_data($failedposts);
 195              $task->set_component('mod_forum');
 196              $task->set_next_run_time(time() + MINSECS);
 197              $task->set_fail_delay(MINSECS);
 198              \core\task\manager::reschedule_or_queue_adhoc_task($task);
 199          }
 200      }
 201  
 202      /**
 203       * Prepare all data for this run.
 204       *
 205       * Take all post ids, and fetch the relevant authors, discussions, forums, and courses for them.
 206       *
 207       * @param   int[]   $postids The list of post IDs
 208       */
 209      protected function prepare_data(array $postids) {
 210          global $DB;
 211  
 212          if (empty($postids)) {
 213              return;
 214          }
 215  
 216          list($in, $params) = $DB->get_in_or_equal(array_values($postids));
 217          $sql = "SELECT p.*, f.id AS forum, f.course
 218                    FROM {forum_posts} p
 219              INNER JOIN {forum_discussions} d ON d.id = p.discussion
 220              INNER JOIN {forum} f ON f.id = d.forum
 221                   WHERE p.id {$in}";
 222  
 223          $posts = $DB->get_recordset_sql($sql, $params);
 224          $discussionids = [];
 225          $forumids = [];
 226          $courseids = [];
 227          $userids = [];
 228          foreach ($posts as $post) {
 229              $discussionids[] = $post->discussion;
 230              $forumids[] = $post->forum;
 231              $courseids[] = $post->course;
 232              $userids[] = $post->userid;
 233              unset($post->forum);
 234              if (!isset($this->posts[$post->discussion])) {
 235                  $this->posts[$post->discussion] = [];
 236              }
 237              $this->posts[$post->discussion][$post->id] = $post;
 238          }
 239          $posts->close();
 240  
 241          if (empty($discussionids)) {
 242              // All posts have been removed since the task was queued.
 243              return;
 244          }
 245  
 246          // Fetch all discussions.
 247          list($in, $params) = $DB->get_in_or_equal(array_values($discussionids));
 248          $this->discussions = $DB->get_records_select('forum_discussions', "id {$in}", $params);
 249          foreach ($this->discussions as $discussion) {
 250              if (empty($this->forumdiscussions[$discussion->forum])) {
 251                  $this->forumdiscussions[$discussion->forum] = [];
 252              }
 253              $this->forumdiscussions[$discussion->forum][] = $discussion->id;
 254          }
 255  
 256          // Fetch all forums.
 257          list($in, $params) = $DB->get_in_or_equal(array_values($forumids));
 258          $this->forums = $DB->get_records_select('forum', "id {$in}", $params);
 259          foreach ($this->forums as $forum) {
 260              if (empty($this->courseforums[$forum->course])) {
 261                  $this->courseforums[$forum->course] = [];
 262              }
 263              $this->courseforums[$forum->course][] = $forum->id;
 264          }
 265  
 266          // Fetch all courses.
 267          list($in, $params) = $DB->get_in_or_equal(array_values($courseids));
 268          $this->courses = $DB->get_records_select('course', "id $in", $params);
 269  
 270          // Fetch all authors.
 271          list($in, $params) = $DB->get_in_or_equal(array_values($userids));
 272          $users = $DB->get_recordset_select('user', "id $in", $params);
 273          foreach ($users as $user) {
 274              $this->minimise_user_record($user);
 275              $this->users[$user->id] = $user;
 276          }
 277          $users->close();
 278  
 279          // Fill subscription caches for each forum.
 280          // These are per-user.
 281          foreach (array_values($forumids) as $id) {
 282              \mod_forum\subscriptions::fill_subscription_cache($id);
 283              \mod_forum\subscriptions::fill_discussion_subscription_cache($id);
 284          }
 285      }
 286  
 287      /**
 288       * Send the specified post for the current user.
 289       *
 290       * @param   \stdClass   $course
 291       * @param   \stdClass   $forum
 292       * @param   \stdClass   $discussion
 293       * @param   \stdClass   $post
 294       * @param   \stdClass   $cm
 295       * @param   \context    $context
 296       */
 297      protected function send_post($course, $forum, $discussion, $post, $cm, $context) {
 298          global $CFG, $PAGE;
 299  
 300          $author = $this->get_post_author($post->userid, $course, $forum, $cm, $context);
 301          if (empty($author)) {
 302              return false;
 303          }
 304  
 305          // Prepare to actually send the post now, and build up the content.
 306          $cleanforumname = str_replace('"', "'", strip_tags(format_string($forum->name)));
 307  
 308          $shortname = format_string($course->shortname, true, [
 309                  'context' => \context_course::instance($course->id),
 310              ]);
 311  
 312          // Generate a reply-to address from using the Inbound Message handler.
 313          $replyaddress = $this->get_reply_address($course, $forum, $discussion, $post, $cm, $context);
 314  
 315          $data = new \mod_forum\output\forum_post_email(
 316              $course,
 317              $cm,
 318              $forum,
 319              $discussion,
 320              $post,
 321              $author,
 322              $this->recipient,
 323              $this->can_post($course, $forum, $discussion, $post, $cm, $context)
 324          );
 325          $data->viewfullnames = $this->can_view_fullnames($course, $forum, $discussion, $post, $cm, $context);
 326  
 327          // Not all of these variables are used in the default string but are made available to support custom subjects.
 328          $site = get_site();
 329          $a = (object) [
 330              'subject' => $data->get_subject(),
 331              'forumname' => $cleanforumname,
 332              'sitefullname' => format_string($site->fullname),
 333              'siteshortname' => format_string($site->shortname),
 334              'courseidnumber' => $data->get_courseidnumber(),
 335              'coursefullname' => $data->get_coursefullname(),
 336              'courseshortname' => $data->get_coursename(),
 337          ];
 338          $postsubject = html_to_text(get_string('postmailsubject', 'forum', $a), 0);
 339  
 340          // Message headers are stored against the message author.
 341          $author->customheaders = $this->get_message_headers($course, $forum, $discussion, $post, $a, $data);
 342  
 343          $eventdata = new \core\message\message();
 344          $eventdata->courseid            = $course->id;
 345          $eventdata->component           = 'mod_forum';
 346          $eventdata->name                = 'posts';
 347          $eventdata->userfrom            = $author;
 348          $eventdata->userto              = $this->recipient;
 349          $eventdata->subject             = $postsubject;
 350          $eventdata->fullmessage         = $this->get_renderer()->render($data);
 351          $eventdata->fullmessageformat   = FORMAT_PLAIN;
 352          $eventdata->fullmessagehtml     = $this->get_renderer(true)->render($data);
 353          $eventdata->notification        = 1;
 354          $eventdata->replyto             = $replyaddress;
 355          if (!empty($replyaddress)) {
 356              // Add extra text to email messages if they can reply back.
 357              $eventdata->set_additional_content('email', [
 358                      'fullmessage' => [
 359                          'footer' => "\n\n" . get_string('replytopostbyemail', 'mod_forum'),
 360                      ],
 361                      'fullmessagehtml' => [
 362                          'footer' => \html_writer::tag('p', get_string('replytopostbyemail', 'mod_forum')),
 363                      ]
 364                  ]);
 365          }
 366  
 367          $eventdata->smallmessage = get_string('smallmessage', 'forum', (object) [
 368                  'user' => fullname($author),
 369                  'forumname' => "$shortname: " . format_string($forum->name, true) . ": " . $discussion->name,
 370                  'message' => $post->message,
 371              ]);
 372  
 373          $contexturl = new \moodle_url('/mod/forum/discuss.php', ['d' => $discussion->id], "p{$post->id}");
 374          $eventdata->contexturl = $contexturl->out();
 375          $eventdata->contexturlname = $discussion->name;
 376          // User image.
 377          $userpicture = new \user_picture($author);
 378          $userpicture->size = 1; // Use f1 size.
 379          $userpicture->includetoken = $this->recipient->id; // Generate an out-of-session token for the user receiving the message.
 380          $eventdata->customdata = [
 381              'cmid' => $cm->id,
 382              'instance' => $forum->id,
 383              'discussionid' => $discussion->id,
 384              'postid' => $post->id,
 385              'notificationiconurl' => $userpicture->get_url($PAGE)->out(false),
 386              'actionbuttons' => [
 387                  'reply' => get_string_manager()->get_string('reply', 'forum', null, $eventdata->userto->lang),
 388              ],
 389          ];
 390  
 391          return message_send($eventdata);
 392      }
 393  
 394      /**
 395       * Fetch and initialise the post author.
 396       *
 397       * @param   int         $userid The id of the user to fetch
 398       * @param   \stdClass   $course
 399       * @param   \stdClass   $forum
 400       * @param   \stdClass   $cm
 401       * @param   \context    $context
 402       * @return  \stdClass
 403       */
 404      protected function get_post_author($userid, $course, $forum, $cm, $context) {
 405          if (!isset($this->users[$userid])) {
 406              // This user no longer exists.
 407              return false;
 408          }
 409  
 410          $user = $this->users[$userid];
 411  
 412          if (!isset($user->groups)) {
 413              // Initialise the groups list.
 414              $user->groups = [];
 415          }
 416  
 417          if (!isset($user->groups[$forum->id])) {
 418              $user->groups[$forum->id] = groups_get_all_groups($course->id, $user->id, $cm->groupingid);
 419          }
 420  
 421          // Clone the user object to prevent leaks between messages.
 422          return (object) (array) $user;
 423      }
 424  
 425      /**
 426       * Helper to fetch the required renderer, instantiating as required.
 427       *
 428       * @param   bool    $html Whether to fetch the HTML renderer
 429       * @return  \core_renderer
 430       */
 431      protected function get_renderer($html = false) {
 432          global $PAGE;
 433  
 434          $target = $html ? 'htmlemail' : 'textemail';
 435  
 436          if (!isset($this->renderers[$target])) {
 437              $this->renderers[$target] = $PAGE->get_renderer('mod_forum', 'email', $target);
 438          }
 439  
 440          return $this->renderers[$target];
 441      }
 442  
 443      /**
 444       * Get the list of message headers.
 445       *
 446       * @param   \stdClass   $course
 447       * @param   \stdClass   $forum
 448       * @param   \stdClass   $discussion
 449       * @param   \stdClass   $post
 450       * @param   \stdClass   $a The list of strings for this  post
 451       * @param   \core\message\message $message The message to be sent
 452       * @return  \stdClass
 453       */
 454      protected function get_message_headers($course, $forum, $discussion, $post, $a, $message) {
 455          $cleanforumname = str_replace('"', "'", strip_tags(format_string($forum->name)));
 456          $viewurl = new \moodle_url('/mod/forum/view.php', ['f' => $forum->id]);
 457  
 458          $headers = [
 459              // Headers to make emails easier to track.
 460              'List-Id: "' . $cleanforumname . '" ' . generate_email_messageid('moodleforum' . $forum->id),
 461              'List-Help: ' . $viewurl->out(),
 462              'Message-ID: ' . forum_get_email_message_id($post->id, $this->recipient->id),
 463              'X-Course-Id: ' . $course->id,
 464              'X-Course-Name: '. format_string($course->fullname, true),
 465  
 466              // Headers to help prevent auto-responders.
 467              'Precedence: Bulk',
 468              'X-Auto-Response-Suppress: All',
 469              'Auto-Submitted: auto-generated',
 470              'List-Unsubscribe: <' . $message->get_unsubscribediscussionlink() . '>',
 471          ];
 472  
 473          $rootid = forum_get_email_message_id($discussion->firstpost, $this->recipient->id);
 474  
 475          if ($post->parent) {
 476              // This post is a reply, so add reply header (RFC 2822).
 477              $parentid = forum_get_email_message_id($post->parent, $this->recipient->id);
 478              $headers[] = "In-Reply-To: $parentid";
 479  
 480              // If the post is deeply nested we also reference the parent message id and
 481              // the root message id (if different) to aid threading when parts of the email
 482              // conversation have been deleted (RFC1036).
 483              if ($post->parent != $discussion->firstpost) {
 484                  $headers[] = "References: $rootid $parentid";
 485              } else {
 486                  $headers[] = "References: $parentid";
 487              }
 488          }
 489  
 490          // MS Outlook / Office uses poorly documented and non standard headers, including
 491          // Thread-Topic which overrides the Subject and shouldn't contain Re: or Fwd: etc.
 492          $aclone = (object) (array) $a;
 493          $aclone->subject = $discussion->name;
 494          $threadtopic = html_to_text(get_string('postmailsubject', 'forum', $aclone), 0);
 495          $headers[] = "Thread-Topic: $threadtopic";
 496          $headers[] = "Thread-Index: " . substr($rootid, 1, 28);
 497  
 498          return $headers;
 499      }
 500  
 501      /**
 502       * Get a no-reply address for this user to reply to the current post.
 503       *
 504       * @param   \stdClass   $course
 505       * @param   \stdClass   $forum
 506       * @param   \stdClass   $discussion
 507       * @param   \stdClass   $post
 508       * @param   \stdClass   $cm
 509       * @param   \context    $context
 510       * @return  string
 511       */
 512      protected function get_reply_address($course, $forum, $discussion, $post, $cm, $context) {
 513          if ($this->can_post($course, $forum, $discussion, $post, $cm, $context)) {
 514              // Generate a reply-to address from using the Inbound Message handler.
 515              $this->inboundmanager->set_data($post->id);
 516              return $this->inboundmanager->generate($this->recipient->id);
 517          }
 518  
 519          // TODO Check if we can return a string.
 520          // This will be controlled by the event.
 521          return null;
 522      }
 523  
 524      /**
 525       * Check whether the user can post.
 526       *
 527       * @param   \stdClass   $course
 528       * @param   \stdClass   $forum
 529       * @param   \stdClass   $discussion
 530       * @param   \stdClass   $post
 531       * @param   \stdClass   $cm
 532       * @param   \context    $context
 533       * @return  bool
 534       */
 535      protected function can_post($course, $forum, $discussion, $post, $cm, $context) {
 536          if (!isset($this->canpostto[$discussion->id])) {
 537              $this->canpostto[$discussion->id] = forum_user_can_post($forum, $discussion, $this->recipient, $cm, $course, $context);
 538          }
 539          return $this->canpostto[$discussion->id];
 540      }
 541  
 542      /**
 543       * Check whether the user can view full names of other users.
 544       *
 545       * @param   \stdClass   $course
 546       * @param   \stdClass   $forum
 547       * @param   \stdClass   $discussion
 548       * @param   \stdClass   $post
 549       * @param   \stdClass   $cm
 550       * @param   \context    $context
 551       * @return  bool
 552       */
 553      protected function can_view_fullnames($course, $forum, $discussion, $post, $cm, $context) {
 554          if (!isset($this->viewfullnames[$forum->id])) {
 555              $this->viewfullnames[$forum->id] = has_capability('moodle/site:viewfullnames', $context, $this->recipient->id);
 556          }
 557  
 558          return $this->viewfullnames[$forum->id];
 559      }
 560  
 561      /**
 562       * Removes properties from user record that are not necessary for sending post notifications.
 563       *
 564       * @param   \stdClass   $user
 565       */
 566      protected function minimise_user_record(\stdClass $user) {
 567          // We store large amount of users in one huge array, make sure we do not store info there we do not actually
 568          // need in mail generation code or messaging.
 569          unset($user->institution);
 570          unset($user->department);
 571          unset($user->address);
 572          unset($user->city);
 573          unset($user->currentlogin);
 574          unset($user->description);
 575          unset($user->descriptionformat);
 576      }
 577  }