-
Bug
-
Resolution: Fixed
-
Minor
-
2.9.1
-
MOODLE_29_STABLE
-
MOODLE_28_STABLE, MOODLE_29_STABLE
-
MDL-51110-master -
Steps to reproduce:
- 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
- Add a new badge and set the criteria for it to use activity completion and have any of the two forums completed
- As student, post in both forums
- 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;
|
- Discovered while testing
-
MDLSITE-4145 Learn.moodle is trying to re-award a previously awarded badge
-
- Resolved
-