Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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