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

Add callbacks to add or keep section number #117

Conversation

tuanngocnguyen
Copy link

@tuanngocnguyen tuanngocnguyen commented Mar 14, 2024

Hi @Syxton ,

I have created a PR for this issue:

#115

I have added 2 callbacks for course format to determine if a user can select "Add new section"/"Keep original section"

Would you please have a look and let me know your opinion?

Thanks

Copy link

@TomoTsuyuki TomoTsuyuki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making PR for the issue.
It looks ok but some variable/function name are confusing.

@@ -45,6 +45,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class section_select_form extends moodleform {
/** Int array of allowed values */
protected $allowedvalues = [];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the variable name does not really mean the values.
maybe $targetsections or something is better?

Copy link
Author

@tuanngocnguyen tuanngocnguyen Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @TomoTsuyuki

Thanks for your review, I have changed it to $allowedsections, only contains section numbers which are allowed to be chosen.

* @param array $attributes the attributes
* @return void
*/
private function is_allowed(bool $allowcondition, int $optionnumber, array &$attributes): void {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function name sounds like for checking value and return bool if condition is ok.
maybe set_radioption or something?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed this to set_radio_option

* @param string $callbackname the name of the callback function
* @return mixed|null none determined return type
*/
private static function run_course_format_callback(int $courseid, string $format, string$callbackname) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a space between string and $callbackname?

Copy link
Author

@tuanngocnguyen tuanngocnguyen Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a space

$this->is_allowed($canaddnewsection, $targetsectionnum + 1, $attributes);
$radioarray[] = $mform->createElement('radio', 'targetsectionnum', '',
get_string('newsection', 'block_massaction'), $targetsectionnum + 1, $attributes);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FOUND 1 ERROR AFFECTING 1 LINE
140 | ERROR | [x] Functions must not contain multiple empty lines in a row; found 2 empty lines

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the redundant empty line

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @TomoTsuyuki for your review

I have updated the patch

@tuanngocnguyen tuanngocnguyen force-pushed the issue115_limitation_to_add_course_section branch 3 times, most recently from 33c3af5 to 3506e74 Compare March 17, 2024 23:10
@tuanngocnguyen
Copy link
Author

Hi @Syxton ,

Hope you are doing fine.

Would you please help with the review once you have time and let me know if there is any issue I need to address?

Regards

Copy link
Owner

@Syxton Syxton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tuanngocnguyen I have looked over these changes and they seem like they are good. @PhMemmel Do you have any concerns with moving this forward?

@PhMemmel
Copy link
Collaborator

@Syxton Won't be able to have a look for at least 2 weeks, sorry. If you want to wait, I will have a look and provide feedback.

@PhMemmel
Copy link
Collaborator

Just one thought when speaking of callbacks... Without having a closer look at the code ... would it be maybe better to use the new hook api for that implementation? So all kinds of Plugin could hook into this and we do not need to look for callbacks in course formats?

@tuanngocnguyen
Copy link
Author

tuanngocnguyen commented Mar 27, 2024

Hi @Syxton @PhMemmel ,

Thanks for your comment.

I am happy to wait for your review.

I think the limitation feature should be applied to course format.

If other plugins can implement the hooks/callbacks, we may go into a case where a course has multiple blocks with the feature. One allows to add new section, one disallows to add new section. I may lead to unpredictable behavior.
On the other hand, a course has only one format, so we will not run into this issue.

Please let me know your opinion.

Regards

@PhMemmel
Copy link
Collaborator

PhMemmel commented Apr 9, 2024

Hi everyone,

I now took some time to come back at this and thought a little bit more about this approach:

  • IMO it is not a good approach and not good style to create dependencies between plugins. Unfortunately, I could not find the place in the moodle dev docs where this is mentioned, but I'm pretty sure I've also read it there.
  • I admit, these kinds of callbacks are at least loosely coupled, but I consider it as very bad style that a course format main class contains some callback functions that are not related to the course format code or moodle core apis at all, but instead are hooked in by a plugin distantly related to the course format.
  • Also, block_massaction needs to contain course format related code when it is not necessary
  • Callbacks are being reworked and removed from the whole moodle code and be replaced by the hooks API, so I suggest to use the hooks API for this kind of scenario. This makes these scenarios much more controlled and clean.
  • The hooks API also allows other plugins to hook into block_massaction, so we do not have code for a super specific use case of special course formats, but instead we offer a hook for every plugin to use (if wanted).

Because of this I have reworked the current callback from #99 and included an alternative for this PR using a hook, see #118.
It's about 95% done IMO, it basically just lacks some unit tests which test the hook functionality.

To show, how this hook can be used I also pushed a moodle branch demonstrating the hook usage for format_topics: moodle/moodle@main...PhMemmel:moodle:Hook_example_block_massaction

@Syxton and also @tuanngocnguyen and @TomoTsuyuki: Let me know what you are thinking of this. From my point of view this would be the better approach for on the one hand addressing your (very specific) use cases and on the other hand keep the interactions with other plugins well structured and controlled.

Regarding your concerns @tuanngocnguyen about the course format having to be the only plugin controlling the section restrictrions: I designed the hook in a way so that every plugin which hooks in can disable additional sections and options, but never can add them again. So your course format callback should be good.
Also, the admin of a site can have a look in the hooks overview page in the website administration and see which hook listeners are registered, so it's easy to determine if there are other plugins hooking in. If you still do not feel well about this, it would also be possible to make the hook stoppable and to specify a priority to make sure your course format plugin will be the first.

@Syxton As my PR is more or less an API change we would have to think about how to deal with branches and releases. Also, this PR currently uses dependency injection features which is requiring moodle 4.4. If we need to be downwards compatible to moodle 4.3 this is easily reworkable however, makes it hard to unit test though.

@Syxton
Copy link
Owner

Syxton commented Apr 9, 2024

@PhMemmel I do like the hooks approach more due to the push towards hooks and their flexibility/control. I will create a branch for our current release through 4.3, and then we can push #118 onto master (the 4.4+ branch) moving forward once completed.

@Syxton Syxton force-pushed the master branch 2 times, most recently from b7ea080 to d3a086a Compare April 9, 2024 16:20
@PhMemmel
Copy link
Collaborator

@Syxton Glad to hear and thank you very much for the feedback. Will wait for some additional feedback from @tuanngocnguyen @TomoTsuyuki, especially if my implementation addresses all of their use cases.

After that I'll finalize my PR, will finally put it against master branch which then will be our new branch for Moodle 4.4 onwards.

@tuanngocnguyen
Copy link
Author

tuanngocnguyen commented Apr 11, 2024

Hi @PhMemmel @Syxton ,

Thanks for your work on the issue.
The PR looks good to me. However, we are still using Moodle 4.1 and PHP 7.4, there are some incompatible issues:

class "\core\di" does not exist in Moodle 4.1
Read only properties are only allowed since PHP 8.1
Constructor property promotion is only allowed since PHP 8.0

As Moodle 4.1 is LTS, would it still be possible for our PR to be integrated into a separated branch for the moodle version ? Please let me know if you require additional work for the MOODLE_401_STABLE branch?

Regards

@tuanngocnguyen tuanngocnguyen changed the base branch from master to MOODLE_401_STABLE April 11, 2024 01:35
@tuanngocnguyen tuanngocnguyen force-pushed the issue115_limitation_to_add_course_section branch from 3506e74 to 975d7cf Compare April 11, 2024 01:40
@tuanngocnguyen
Copy link
Author

I have rebased the PR to MOODLE_401_STABLE

@PhMemmel
Copy link
Collaborator

Thank you @tuanngocnguyen for the feedback.

I feared you would answer this :)

Trying to be as objective as possible while considering all concerns I personally come to the conclusion that it would be best overall to not integrate this PR but instead use the resources on the review of the hook rework/approach.

@tuanngocnguyen As we will not develop the current code any further it probably should be no big effort for you to integrate your PR in a temporary fork. As soon as you update, you will be able to switch to the new master branch and implement the new hook.

@Syxton @TomoTsuyuki @tuanngocnguyen Is this fine for everyone?

@Syxton
Copy link
Owner

Syxton commented Apr 15, 2024

@PhMemmel I agree. I think that it is late in the game to be adding features to the 401 branch. Moodle LTS general support ended in December of '23 with only security support till 2025. I can only speak for myself that my attention is spread very thin, so I think it best to instead of adding a feature for 4.1, we focus on the long term solution with hooks and @TomoTsuyuki & @tuanngocnguyen can keep their fork with this PR until they are able to upgrade their Moodle to the hooks capable version.

@tuanngocnguyen
Copy link
Author

Hi @PhMemmel @Syxton

Thanks for your comments.
We will go with a fork for this PR.

Regards

@tuanngocnguyen
Copy link
Author

Thanks all for your work.

I am closing this WR.

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.

4 participants