mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 20:42:22 +02:00
MDL-52534 course: Course module form can be extended by plugins via callbacks.
3 new plugin callbacks are available to extend any course module form. xx_coursemodule_standard_elements() xx_coursemodule_validation() xx_coursemodule_edit_post_actions() This allows you to add elements to the form, extend the validation and process the extra information once the module has been created/updated.
This commit is contained in:
parent
9331352cc1
commit
8995c2702f
@ -174,6 +174,23 @@ function add_moduleinfo($moduleinfo, $course, $mform = null) {
|
||||
return $moduleinfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for plugins to take action when a module is created or updated.
|
||||
*
|
||||
* @param stdClass $moduleinfo the module info
|
||||
* @param stdClass $course the course of the module
|
||||
*
|
||||
* @return stdClass moduleinfo updated by plugins.
|
||||
*/
|
||||
function plugin_extend_coursemodule_edit_post_actions($moduleinfo, $course) {
|
||||
$callbacks = get_plugins_with_function('coursemodule_edit_post_actions', 'lib.php');
|
||||
foreach ($callbacks as $type => $plugins) {
|
||||
foreach ($plugins as $plugin => $pluginfunction) {
|
||||
$moduleinfo = $pluginfunction($moduleinfo, $course);
|
||||
}
|
||||
}
|
||||
return $moduleinfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Common create/update module module actions that need to be processed as soon as a module is created/updaded.
|
||||
@ -328,6 +345,9 @@ function edit_module_post_actions($moduleinfo, $course) {
|
||||
require_once($CFG->libdir.'/plagiarismlib.php');
|
||||
plagiarism_save_form_elements($moduleinfo);
|
||||
|
||||
// Allow plugins to extend the course module form.
|
||||
$moduleinfo = plugin_extend_coursemodule_edit_post_actions($moduleinfo, $course);
|
||||
|
||||
return $moduleinfo;
|
||||
}
|
||||
|
||||
|
@ -31,6 +31,14 @@ abstract class moodleform_mod extends moodleform {
|
||||
* @var mixed
|
||||
*/
|
||||
protected $_cm;
|
||||
|
||||
/**
|
||||
* Current course.
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $_course;
|
||||
|
||||
/**
|
||||
* List of modform features
|
||||
*/
|
||||
@ -65,6 +73,7 @@ abstract class moodleform_mod extends moodleform {
|
||||
$this->_instance = $current->instance;
|
||||
$this->_section = $section;
|
||||
$this->_cm = $cm;
|
||||
$this->_course = $course;
|
||||
if ($this->_cm) {
|
||||
$this->context = context_module::instance($this->_cm->id);
|
||||
} else {
|
||||
@ -96,6 +105,63 @@ abstract class moodleform_mod extends moodleform {
|
||||
self::__construct($current, $section, $cm, $course);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current data for the form.
|
||||
* @return stdClass|null
|
||||
*/
|
||||
function get_current() {
|
||||
return $this->current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DB record for the current instance.
|
||||
* @return stdClass|null
|
||||
*/
|
||||
function get_instance() {
|
||||
return $this->_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the course section number (relative).
|
||||
* @return int
|
||||
*/
|
||||
function get_section() {
|
||||
return $this->_section;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the course id.
|
||||
* @return int
|
||||
*/
|
||||
function get_course() {
|
||||
return $this->_course;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the course module object.
|
||||
* @return stdClass|null
|
||||
*/
|
||||
function get_coursemodule() {
|
||||
return $this->_cm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the course context for new modules, or the module context for existing modules.
|
||||
* @return context
|
||||
*/
|
||||
function get_context() {
|
||||
return $this->context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the features this module supports.
|
||||
* @return stdClass
|
||||
*/
|
||||
function get_features() {
|
||||
return $this->_features;
|
||||
}
|
||||
|
||||
|
||||
protected function init_features() {
|
||||
global $CFG;
|
||||
|
||||
@ -359,6 +425,33 @@ abstract class moodleform_mod extends moodleform {
|
||||
\core_availability\frontend::report_validation_errors($data, $errors);
|
||||
}
|
||||
|
||||
$pluginerrors = $this->plugin_extend_coursemodule_validation($data);
|
||||
if (!empty($pluginerrors)) {
|
||||
$errors = array_merge($errors, $pluginerrors);
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend the validation function from any other plugin.
|
||||
*
|
||||
* @param stdClass $data The form data.
|
||||
* @return array $errors The list of errors keyed by element name.
|
||||
*/
|
||||
function plugin_extend_coursemodule_validation($data) {
|
||||
$errors = array();
|
||||
|
||||
$callbacks = get_plugins_with_function('coursemodule_validation', 'lib.php');
|
||||
foreach ($callbacks as $type => $plugins) {
|
||||
foreach ($plugins as $plugin => $pluginfunction) {
|
||||
// We have exposed all the important properties with public getters - the errors array should be pass by reference.
|
||||
$pluginerrors = $pluginfunction($this, $data);
|
||||
if (!empty($pluginerrors)) {
|
||||
$errors = array_merge($errors, $pluginerrors);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $errors;
|
||||
}
|
||||
|
||||
@ -608,6 +701,22 @@ abstract class moodleform_mod extends moodleform {
|
||||
}
|
||||
|
||||
$this->standard_hidden_coursemodule_elements();
|
||||
|
||||
$this->plugin_extend_coursemodule_standard_elements();
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugins can extend the coursemodule settings form.
|
||||
*/
|
||||
function plugin_extend_coursemodule_standard_elements() {
|
||||
$callbacks = get_plugins_with_function('coursemodule_standard_elements', 'lib.php');
|
||||
foreach ($callbacks as $type => $plugins) {
|
||||
foreach ($plugins as $plugin => $pluginfunction) {
|
||||
// We have exposed all the important properties with public getters - and the callback can manipulate the mform
|
||||
// directly.
|
||||
$pluginfunction($this, $this->_form);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user