Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-80024

Adhoc task \mod_forum\task\send_user_notifications does not consider user.emailstop flag

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • None
    • 4.1.6
    • Forum
    • None
    • MOODLE_401_STABLE

      Context

      When users records have emailstop = 1 it should prevent receiving any notification, also preventing to send them emails.

      Moodle 4.1.6+
      MySQL 5.7

      Due to some enrolment procedure problem, we are receiving users without email and they are created into Moodle with our integration system. But this is just contextual information to understand error messages from below. Meanwhile, we are just setting the emailstop = 1 for those users.

      Problem

      I have found in our production that a user with the emailstop = 1, an adhoc task \mod_forum\task\send_user_notifications is trying to send an email.

      The given record from mdl_task_adhoc:

      | 19042174 | mod_forum       | \mod_forum\task\send_user_notifications |  1699351472 |       480 | ["1774281"]      | 136385 |        0 |        NULL | NULL     | NULL |  1699350992 |
      

      And the log:

      Execute adhoc task: mod_forum\task\send_user_notifications
      Adhoc task id: 19042174
      Adhoc task custom data: ["1774281"]
      ... started 10:56:32. Current memory use 53.6 MB.
      Sending messages to XXXXXXX (136385)
      ++ Can not send email to user without email: 136385 ++
      * line 5981 of /lib/moodlelib.php: call to debugging()
      * line 103 of /message/output/email/message_output_email.php: call to email_to_user()
      * line 506 of /lib/classes/message/manager.php: call to message_output_email->send_message()
      * line 382 of /lib/classes/message/manager.php: call to core\message\manager::call_processors()
      * line 349 of /lib/classes/message/manager.php: call to core\message\manager::send_message_to_processors()
      * line 341 of /lib/messagelib.php: call to core\message\manager::send_message()
      * line 386 of /mod/forum/classes/task/send_user_notifications.php: call to message_send()
      * line 155 of /mod/forum/classes/task/send_user_notifications.php: call to mod_forum\task\send_user_notifications->send_post()
      * line 367 of /lib/cronlib.php: call to mod_forum\task\send_user_notifications->execute()
      * line 198 of /lib/cronlib.php: call to cron_run_inner_adhoc_task()
      * line 76 of /lib/cronlib.php: call to cron_run_adhoc_tasks()
      * line 178 of /admin/cli/cron.php: call to cron_run()
      ++ Error calling message processor email ++
      * line 507 of /lib/classes/message/manager.php: call to debugging()
      * line 382 of /lib/classes/message/manager.php: call to core\message\manager::call_processors()
      * line 349 of /lib/classes/message/manager.php: call to core\message\manager::send_message_to_processors()
      * line 341 of /lib/messagelib.php: call to core\message\manager::send_message()
      * line 386 of /mod/forum/classes/task/send_user_notifications.php: call to message_send()
      * line 155 of /mod/forum/classes/task/send_user_notifications.php: call to mod_forum\task\send_user_notifications->send_post()
      * line 367 of /lib/cronlib.php: call to mod_forum\task\send_user_notifications->execute()
      * line 198 of /lib/cronlib.php: call to cron_run_inner_adhoc_task()
      * line 76 of /lib/cronlib.php: call to cron_run_adhoc_tasks()
      * line 178 of /admin/cli/cron.php: call to cron_run()
        Failed to send post 1774281
      Sent 0 messages with 1 failures
      ... used 31 dbqueries
      ... used 0.13186693191528 seconds
      Adhoc task failed: mod_forum\task\send_user_notifications,error/Error sending posts.
      Backtrace:
      * line 367 of /lib/cronlib.php: call to mod_forum\task\send_user_notifications->execute()
      * line 198 of /lib/cronlib.php: call to cron_run_inner_adhoc_task()
      * line 76 of /lib/cronlib.php: call to cron_run_adhoc_tasks()
      * line 178 of /admin/cli/cron.php: call to cron_run()
      

      Expected

      No error should appear on this adhoc task, since the user is set to not to receive any notification, also emails, with emailstop = 1. So, this adhoc task should not try to send any email instead.

      Proposal

      Since in lib/messagelib.php, line 116, it is checked if emailstop is set for the target user in:

              // If recipient is internal user (noreply user), and emailstop is set then don't send any msg.
              if (!$usertoisrealuser && !empty($eventdata->userto->emailstop)) {
                  debugging('Attempt to send msg to internal (noreply) user', DEBUG_NORMAL);
                  return false;
              }
      

      I think we should consider independently emailstop regardless of the user: If, for some reason, user on Preferences > Notifications preferences decides to mark the checkbox to not to receive any notification (emailstop = 1), it should be respected.

      Therefore, the proposal should be just check if:

              if (!empty($eventdata->userto->emailstop)) {
                  debugging('Do not send emails to users with disabled notifications', DEBUG_NORMAL);
                  return false;
              }
      

      In fact, I can see these kind of lines scattered over there. Maybe a new API like core_user::has_disabled_notifications($userobjorid) could be useful to ensure everywhere is checked the same way. The result would be like this:

              if (core_user::has_diabled_notifications($eventdata->userto)) {
                  debugging('Do not send emails to users with disabled notifications', DEBUG_NORMAL);
                  return false;
              }
      

            Unassigned Unassigned
            jpahullo Jordi Pujol-Ahulló
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.