Search moodle.org's
Developer Documentation

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
  • Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 37 and 311] [Versions 38 and 311] [Versions 39 and 311]

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