if (!\core\plugininfo\qbank::is_plugin_enabled('qbank_customfields')) {
$this->customfieldpluginenabled = false;
}
Re: qbank_ type plugin: adding fields to question edit form
Near the top of the form there is this old-style callback https://github.com/moodle/moodle/blob/master/question/type/edit_question_form.php#L214-L226, but only if you are editing an existing question, and since it is right at the top, you cannot control where your new fields go in relation to the other fields.
Really, this callback should be customfields code is. Then the customfields (and probably the tags) bits of code should be moved into their respective plugins using that hook. The current callback should be deprecated.
I don't have time to do that right now, but would happily review a patch. At the very least, please could you make a tracker issue for this?
Re: qbank_ type plugin: adding fields to question edit form
I'ts have been a long time since I adeed something to core, and I'm not experienced with new coding style, but I can try. We could add a new abstract class available to be extended by qbank_ plugins. Something like 'question_edit_handler' . This handler would have methods for:
- instance_form_definition()
- instance_form_before_set_data()
- instance_form_definition_after_data()
- instance_form_save()
And many of those currently defined in question/bank/customfields/classes/customfield/question_handler.php
Then, where code now calls for customfieldhandler, calling any handler for any qbank_ plugin having it.
For instance, in question/type/edit_question_form.php calling
$plugins = \core_component::get_plugin_list_with_class('qbank', 'question_edit_handler', 'question_edit_handler.php');
foreach ($plugins as $componentname => $classname) {
$handlerclass = str_replace('_', '\', $componentname) . '\' . $classname;
$this->qbankedithandlers[$componentname] = new $handlerclass();
$this->qbankedithandlers[$componentname]->set_parent_context($this->categorycontext); // For question handler only.
$this->qbankedithandlers[$componentname]->instance_form_definition($mform, empty($this->question->id) ? 0 : $this->question->id);
}
In question/bank/editquestion/question.php, adding something like
$plugins = \core_component::get_plugin_list_with_class('qbank', 'question_edit_handler', 'question_edit_handler.php');
foreach ($plugins as $componentname => $classname) {
$handlerclass = str_replace('_', '\', $componentname) . '\' . $classname;
$qbankedithandlers[$componentname] = new $handlerclass();
$qbankedithandlers[$componentname]->instance_form_before_set_data($toform);
}
And afterwards,
$qbankedithandlers[$componentname]->instance_form_save($fromform);
I can get some time to actually implement code along these lines, taking care of loading/saving, editing form, backup/restore, export/import etc; if you really see such approach useful and deserves the time invested, with any hope to see it in core in 4.5 or so.
The main point is if we want qbank plugins to add new properties to questions themselves, individually, or remain as something that act on the bank, on questions listings. Please, let me now if you think an effort in this direction would we useful and is in line with the ideas of the group promoting new qbank developments.
Re: qbank_ type plugin: adding fields to question edit form
Question bank plugins should not use get_plugin_list_with_class. The way it is supposed to work is that anything the plugin wants to do is declared in the plugin_features class, which is a subclass of plugin_features_base. So, we should probably add a get_question_edit_handler method there, which by default returns null, but which can return an instance of a subclass of question_edit_handler_base.
As you say, as well as the direct interactions with the form, you need to handle saving/loading, backup/restore, import/export etc. Backup/restore is already supported. Import/export was completely missed initally (oops!) and is being handled in MDL-78520 - but work there has stalled. Still, the propsed data_mapper there might be the best approach for handling the data, and being consistent with the proposed implementation there would probably be good.
Re: qbank_ type plugin: adding fields to question edit form
I actually thought of adding a fourth feature to plugin_features class, but that was changing more files. But if we are talking about core, the concern is future use, simple, understandable and maintainable, not changes in current files.
I'll read MDL-78520 to see what to do with export/import.
Re: qbank_ type plugin: adding fields to question edit form
In a separate issuse, MDL-78520, you propose having a general way for other code to inspect what features qbank plugins support or expose. So, I have added a helper get_all_qbank_plugin_features() working like init_plugins() method in the view class.
I really hope you could review this ad help to push it into new releases. Please let me now any correction or improvement that you see appropriate.
Re: qbank_ type plugin: adding fields to question edit form
Hi, Hooks API has landed in moodle 4.4 Dev . In comments for MDL-80964 Tim suggest that would be sensible to implement this in the same manner. So, I've tried and is actually posiible and even cleaner (less modification of current core code). You may see an implementation of hooks for question_edit_form in issue MDL-81170.
Actually, this pathway allows any plugin type to extend the question edit form, adding new datafields, or managing the existing ones (freezing some, fort instance). The mechanism via plugin_features was exclusive for qbank type plugins, so less generic.
Please, have a look at MDL-81170 and vote for integration.
Re: qbank_ type plugin: adding fields to question edit form
So, I've updated the patch here to have question edit form hooks written and used in the same style as course form hooks. Please, see MDL-81170 for the newer version
Re: qbank_ type plugin: adding fields to question edit form
Hi, time for new 4.5 release is coming. Please, may I ask to review MDL-81170?. I've added a demo repository with two brances, main (follogin moodle main) and edit-question-hook contaning the proposed changes. Following Tim Hunt's recommendation, this proposal eliminates the especial entry points for question custom fields and are replaced by thesw new regular hooks.
If you whant to try, I'm including a patch file as attachment.
Re: qbank_ type plugin: adding fields to question edit form
Hi Enrique,
Issue reviewed.
Thanks,
Jordi