Pass original structure position to usort. #3957
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Sensei_Course_Structure->sort_structure()
Testing instructions
New/Updated Hooks
Deprecated Code
Screenshot / Video