FMCorz / moodle-block_xp

A gamification plugin for Moodle allowing students to gain experience points and level up.
https://levelup.plus/?ref=github
149 stars 42 forks source link

Point rules / level descriptions not working properly on course backup / restore #154

Closed tsbartel closed 1 year ago

tsbartel commented 1 year ago

When restoring a course with block_xp configuration into an existing course (selecting "Restore into this course" -> "Delete the contents of this course and then restore"), the information on point rules is duplicated. More precisely: The point rules (both grade & event) from the backup file are added to the ruleset of the existing course.

Repeated restoration attempts of a course can cause very bloated rulesets which is especially annoying with the grade rulesets as these cannot be manually deleted.

Minimal course backup file for demonstration: backup-moodle2-course-65-testcourse_xp-20230412-1758-nu.mbz (Moodle 3.9.20, block_xp 3.13.3, local_xp 1.13.1)

Steps to reproduce:

  1. Import the above course backup file into a new course.
  2. Check that the event ruleset is complete (2 condition blocks on events which grant 15 xp each, 1 nested condition block on completions granting 25 xp on each completed activity).
  3. Restore the course from the provided backup file as described in the beginning of this issue: a. Restore into this course b. Delete the contents of this course and then restore
  4. Check that the event ruleset is now multiplied.

Optional steps: 2.5 Make some arbitrary changes to the ruleset before restoring to make the new ruleset distinguishably from the old ruleset.

  1. Repeat the process multiple times to observe linear growth of the ruleset with each restore.
FMCorz commented 1 year ago

Thank you @tsbartel for reporting this. I'm actually glad you identified the issue, we were aware that at times rule were duplicated but did not know when, or how. It turns out it's to do with merging restores, so thank you!

We patched the bug and included a bunch of unit tests to validate that the behaviour, as we had to manually delete the entries it was important to validate that the deletion works, but only when it should.

We've also patched the restore code to always prevent multiple grade rules.

This work has also highlighted potential issues with the restore of XP+, which works slightly differently as it is a local plugin. We will document the behaviour in our documentation, but please note that a merge restore (with or witout delete) will never restore XP+ data if the setting "Overwrite course configuration" is not set to "Yes", and it unfortunately defaults to "No". The reason being that Moodle does not invoke local plugins (and others) during course restore if this setting is disabled. I'm not entirely sure why that is.

Thanks again for reporting this!

tsbartel commented 1 year ago

You're very welcome and I'm very thankful for you being on top of this issue so fast. Very glad that you took the time to highlight potential issues with local_xp as well - we observed that behaviour but were not able to pinpoint it sufficiently for opening an issue yet. If I understand you correctly, this is rooted in the way Moodle does restores, though, so maybe that's just tough luck. (Idea: Could you invoke local_xp restoration in block_xp instead, as the latter is a dependency of the former? Of course wrapped in a check whether local_xp is both installed and settings are present in the restore file...)

Our institution is very content with the xp plugin suite and I want to extend our thanks to you and the other maintainers for providing a great gamification subsystem for Moodle.

Cheers from Germany

FMCorz commented 1 year ago

You have a point, perhaps it would best if block_xp was responsible for backup and restores, deferring to local_xp where needed. It does something along those lines for the privacy API, so perhaps backups could work the same way. Having said that, the backup code is a mysterious jungle and I don't want to get ahead of myself assuming it is easily possible.

Thanks a lot for your kind words! XP is 9 years old now, who would have thought? Not me! :-)