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

mod_assign due tasks fail if user inactive

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Minor Minor
    • None
    • 4.5.2
    • Assignment
    • None
    • MOODLE_405_STABLE

      If an inactive (eg. suspended, nologin etc.) user shall be notified by the 'recently' invented tasks (MDL-79734)

      queue_all_assignment_due_digest_notification_tasks
      queue_assignment_due_soon_notification_tasks_for_users
      queue_assignment_overdue_notification_tasks_for_users.php

      the tasks fail with an error message like this:

       

      Adhoc task failed: mod_assign\task\queue_assignment_due_soon_notification_tasks_for_users,Gesperrtes Nutzerkonto
      Backtrace:
      * line 249 of /lib/classes/task/manager.php: call to core\user::require_active_user()
      * line 45 of /mod/assign/classes/task/queue_assignment_due_soon_notification_tasks_for_users.php: call to core\task\manager::queue_adhoc_task()
      * line 519 of /lib/classes/cron.php: call to mod_assign\task\queue_assignment_due_soon_notification_tasks_for_users->execute()
      * line 302 of /lib/classes/cron.php: call to core\cron::run_inner_adhoc_task()
      * line 128 of /lib/classes/cron.php: call to core\cron::run_adhoc_tasks()
      * line 186 of /admin/cli/cron.php: call to core\cron::run_main_process()
       
      

       

      Additionally the admins are notified about the failing tasks.

      Inactive users of course are not able to complete outstanding moodle activities. They are assumably inactive for some reason and these tasks therefore should not fail.

      The tasks fail when moodles task manager calls core_user::require_active_user which throws an exception if the user is considered inactive. To prevent these task failures it is necessary that users objects are checked for their activity state by mod_assign before moodles task manager is called. The following patch (against Moodle 4.5.2) will check the activity in the notification_helper class of mod_assign by calling core_user::require_active_user (and sorting out inactive users) before the tasks call \core\task\manager::queue_adhoc_task.

       

      diff -Naur orig/mod/assign/classes/notification_helper.php patched.official/mod/assign/classes/notification_helper.php
      --- orig/mod/assign/classes/notification_helper.php     2025-02-11 08:25:36.015495977 +0100
      +++ patched.official/mod/assign/classes/notification_helper.php 2025-02-28 10:47:16.571446128 +0100
      @@ -32,6 +32,8 @@
        */
       class notification_helper {
       
      +    use \core\task\logging_trait;
      +
           /**
            * @var int Due soon time interval of 48 hours.
            */
      @@ -211,7 +213,7 @@
            * @param string $type The notification type.
            * @return array The users after all filtering has been applied.
            */
      -    public static function get_users_within_assignment(int $assignmentid, string $type): array {
      +    public static function get_users_within_assignment(int $assignmentid, string $type, bool $onlyactive=true): array {
               // Get assignment data.
               $assignmentobj = self::get_assignment_data($assignmentid);
       
      @@ -294,6 +296,18 @@
                   if ($checksent && self::has_user_been_sent_a_notification_already($user->id, json_encode($match), $type)) {
                       unset($users[$key]);
                   }
      +
      +            // Check if user is active user (moodle task manager requires active user later on)
      +            // Prevent failing task (and failure notification being send to the admins)
      +            // TODO check if user is inactive earlier (asap) and exclude them from being handled; log something useful
      +            if ($onlyactive) {
      +                try {
      +                    \core_user::require_active_user($user, true, true);
      +                } catch (moodle_exception $e) {
      +                    $this->log($e->getMessage() . "Inactive user will not be notified.")
      +                    unset($users[$key]);
      +                }
      +            }
               }
       
               return $users;
       
      

       

      Please consider the patch more illustrative than the final solution. I did not check for other moodle versions that are surely affected as well.

      Reproducation of the error:

      • have a user enrolled in course with an activity mod_assign and a due date that triggers the notification of that user by one of the mod_assign tasks above.
      • 'inactivate' the user by suspending or change the authmethod to nologin etc.
      • start the desired task

       

       

            Unassigned Unassigned
            misc Dr. Michael Schneider
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

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