Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass original structure position to usort. #3957

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from

Conversation

hansschuijff
Copy link
Contributor

@hansschuijff hansschuijff commented Feb 3, 2021

Fixes #3921

Changes proposed in this Pull Request

Although PR3951 prevented lesson pages to distort the lesson order, by basically skipping the sorting in sort_structure(), it hasn't solved the reason why the usort() call failed in the first place.

The reason is that the callback tries to retain the original order in $structure without having information about the order of this structure. Simply returning 0 doesn't tell usort to keep the original order, it says the order doesn't matter, so it can do what it wants.

I first thought that each comparison could better return -1, since that implies an actual difference between the compared values, but that only works if usort calls the entries in sequence and that is not what is does. Using vardumps I found It first compairs the first entry with the middle one and then that one with the last and compairs that middle one with every other entry and so on.

That means usort actually needs to be fed with an actual order-indication (0, 1 or -1) and thus the callback needs info about the original order of $structure to correctly determine that response in cases the order should remain unchanged.

So in order to fix that part of the usort, this pr proposes to save the original order in the structure and really compare the original order so usort doesn't fail in any scenario. It would even work without PR3951, although that PR is a more efficient solution for situations where no $order is passed to sort_structure.

  • remember the original order of $structure in Sensei_Course_Structure->sort_structure()
  • return the actual order result where lesson_order should remain as it was.

Testing instructions

  • We had 47 lessons without modules when this originally went wrong when saving a lesson.
  • The way I tested it is to use sort_structure() without the condition added in PR3951. The original order is kept in the same testcase as I used to test PR3951.

New/Updated Hooks

Deprecated Code

Screenshot / Video

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Course Lesson order is changed whenever a single lesson is added or updated - Restore Lesson Order
1 participant