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

Course badge awarding can lead to duplicate 'awards', leading to scheduled task backing off

    XMLWordPrintable

    Details

    • Testing Instructions:
      Hide
      1. Enable badges and course completion on a site & course
      2. Add two new forum activities
      3. Set both forum activity completion criteria as requiring 1 post to complete
      4. As student, post in both forums (to complete the criteria) - repeat this with 3 different students
      5. Add a new badge and set the criteria for it to use activity completion and have any of the two forums completed
      6. Enable the badge
      7. VERIFY: the badge gets enabled and the 3 students are awarded the badge on enable (previously this step caused a db constaint issue)
      Show
      Enable badges and course completion on a site & course Add two new forum activities Set both forum activity completion criteria as requiring 1 post to complete As student, post in both forums (to complete the criteria) - repeat this with 3 different students Add a new badge and set the criteria for it to use activity completion and have any of the two forums completed Enable the badge VERIFY: the badge gets enabled and the 3 students are awarded the badge on enable (previously this step caused a db constaint issue)
    • Affected Branches:
      MOODLE_29_STABLE
    • Fixed Branches:
      MOODLE_28_STABLE, MOODLE_29_STABLE
    • Pull Master Branch:
      MDL-51110-master

      Description

      Steps to reproduce:

      1. Enable badges and course completion on a site & course
      2. Add two new forum activities
      3. Set both forum activity completion criteria as requiring 1 post to complete
      4. Add a new badge and set the criteria for it to use activity completion and have any of the two forums completed
      5. As student, post in both forums
      6. Run the scheduled task for badges cron {{ php admin/tool/task/cli/schedule_task.php --execute=\\core\\task
        badges_cron_task}}

      Expected result:
      The scheduled task runs succesfully

      Actual result:
      Task failed: Error writing to database

      This happens because of this (discovered on learn.moodle):

      ERROR:  duplicate key value violates unique constraint "mdl_badgissu_baduse_uix"
      DETAIL:  Key (badgeid, userid)=(432, 28881) already exists.
      

      After a lot of debugging this seems to because the toearn query was returning duplicate userids and the badge was attempted to be awarded twice. Then the exception causes the scheduled task to fail and get backed off.

      The following patch 'fixed' it - but I haven't worked out why duplicate userids are being returned yet:

      diff --git a/lib/badgeslib.php b/lib/badgeslib.php
      index c8af3f8..8e3d7e5 100644
      --- a/lib/badgeslib.php
      +++ b/lib/badgeslib.php
      @@ -472,7 +472,7 @@ class badge {
                           $wheresql = ' WHERE u.id ' . $earnedsql;
                       }
                       list($enrolledsql, $enrolledparams) = get_enrolled_sql($this->get_context(), 'moodle/badges:earnbadge', 0, true);
      -                $sql = "SELECT u.id
      +                $sql = "SELECT DISTINCT u.id
                               FROM {user} u
                               {$extrajoin}
                               JOIN ({$enrolledsql}) je ON je.id = u.id " . $wheresql . $extrawhere;
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                poltawski Dan Poltawski
                Reporter:
                poltawski Dan Poltawski
                Peer reviewer:
                Yuliya Bozhko
                Integrator:
                David Monllaó
                Tester:
                Ankit Agarwal
                Participants:
                Component watchers:
                Yuliya Bozhko, Amaia Anabitarte, Bas Brands, Carlos Escobedo, Sara Arjona (@sarjona), Víctor Déniz Falcón
              • Votes:
                1 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Fix Release Date:
                  14/Sep/15