diff --git a/form.php b/form.php index aa6a258..f95a542 100644 --- a/form.php +++ b/form.php @@ -36,6 +36,49 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class report_editdates_form extends moodleform { + + /** + * Mod info instance set for the form. + * @var \course_modinfo|null + */ + protected $modinfo; + + /** + * Course. + * @var \stdClass + */ + protected $course; + + /** + * Selected activity type. + * @var string + */ + protected $activitytype; + + /** + * Get course mod info instance set for the form. + * @return course_modinfo | null + */ + public function get_modinfo(): ?course_modinfo { + return $this->modinfo; + } + + /** + * Course object. + * @return \stdClass + */ + public function get_course(): stdClass { + return $this->course; + } + + /** + * Selected activity type. + * @return string + */ + public function get_activitytype(): string { + return $this->activitytype; + } + /** * @see lib/moodleform#definition() */ @@ -43,19 +86,19 @@ public function definition() { global $CFG, $DB, $PAGE; $mform = $this->_form; - $modinfo = $this->_customdata['modinfo']; - $course = $this->_customdata['course']; - $activitytype = $this->_customdata['activitytype']; + $this->modinfo = $this->_customdata['modinfo']; + $this->course = $this->_customdata['course']; + $this->activitytype = $this->_customdata['activitytype']; $config = get_config('report_editdates'); $coursehasavailability = !empty($CFG->enableavailability); - $coursehascompletion = !empty($CFG->enablecompletion) && !empty($course->enablecompletion); + $coursehascompletion = !empty($CFG->enablecompletion) && !empty($this->course->enablecompletion); // Context instance of the course. - $coursecontext = context_course::instance($course->id); + $coursecontext = context_course::instance($this->course->id); // Store current activity type. - $mform->addElement('hidden', 'activitytype', $activitytype); + $mform->addElement('hidden', 'activitytype', $this->activitytype); $mform->setType('activitytype', PARAM_PLUGIN); // Invisible static element. Used as the holder for a validation message sometimes. @@ -71,11 +114,11 @@ public function definition() { $mform->addElement('date_time_selector', 'coursestartdate', get_string('startdate')); $mform->addHelpButton('coursestartdate', 'startdate'); - $mform->setDefault('coursestartdate', $course->startdate); + $mform->setDefault('coursestartdate', $this->course->startdate); $mform->addElement('date_time_selector', 'courseenddate', get_string('enddate'), array('optional' => true)); $mform->addHelpButton('courseenddate', 'enddate'); - $mform->setDefault('courseenddate', $course->enddate); + $mform->setDefault('courseenddate', $this->course->enddate); // If user is not capable, make it read only. if (!has_capability('moodle/course:update', $coursecontext)) { @@ -93,8 +136,8 @@ public function definition() { $prevsectionnum = -1; // Cycle through all the sections in the course. - $cms = $modinfo->get_cms(); - $sections = $modinfo->get_section_info_all(); + $cms = $this->modinfo->get_cms(); + $sections = $this->modinfo->get_section_info_all(); $timeline = array(); foreach ($sections as $sectionnum => $section) { $ismodadded = false; @@ -107,7 +150,7 @@ public function definition() { // New section, create header. if ($prevsectionnum != $sectionnum) { - $sectionname = get_section_name($course, $section); + $sectionname = get_section_name($this->course, $section); $headername = 'section' . $sectionnum . 'header'; $mform->addElement('header', $headername, $sectionname); $mform->setExpanded($headername, false); @@ -151,8 +194,8 @@ public function definition() { } // Cycle through each module in a section. - if (isset($modinfo->sections[$sectionnum])) { - foreach ($modinfo->sections[$sectionnum] as $cmid) { + if (isset($this->modinfo->sections[$sectionnum])) { + foreach ($this->modinfo->sections[$sectionnum] as $cmid) { $cm = $cms[$cmid]; // No need to display/continue if this module is not visible to user. @@ -161,7 +204,7 @@ public function definition() { } // If activity filter is on, then filter module by activity type. - if ($activitytype && ($cm->modname != $activitytype && $activitytype != "all")) { + if ($this->activitytype && ($cm->modname != $this->activitytype && $this->activitytype != "all")) { continue; } @@ -186,7 +229,7 @@ public function definition() { // Call get_settings method for the acitivity/module. // Get instance of the mod's date exractor class. - $mod = report_editdates_mod_date_extractor::make($cm->modname, $course); + $mod = report_editdates_mod_date_extractor::make($cm->modname, $this->course); if ($mod && ($cmdatesettings = $mod->get_settings($cm))) { // Added activity name on the form. foreach ($cmdatesettings as $cmdatetype => $cmdatesetting) { @@ -276,7 +319,7 @@ public function definition() { // Iterate though blocks array. foreach ($courseblocks as $blockid => $block) { - $blockdatextrator = report_editdates_block_date_extractor::make($block->blockname, $course); + $blockdatextrator = report_editdates_block_date_extractor::make($block->blockname, $this->course); if ($blockdatextrator) { // Create the block instance. $blockobj = block_instance($block->blockname, $block, $PAGE); @@ -321,16 +364,21 @@ public function definition() { $mform->addElement('static', 'timelineview', ''); $mform->addElement('html', self::render_timeline_view($timeline)); } + + $callbacks = get_plugins_with_function('report_editdates_form_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); + } + } } public function validation($data, $files) { global $CFG; $errors = parent::validation($data, $files); - $modinfo = $this->_customdata['modinfo']; - $course = $this->_customdata['course']; - $coursecontext = context_course::instance($course->id); - $moddatesettings = array(); $forceddatesettings = array(); foreach ($data as $key => $value) { @@ -365,7 +413,7 @@ public function validation($data, $files) { } } - $cms = $modinfo->get_cms(); + $cms = $this->modinfo->get_cms(); // Validating forced date settings. foreach ($forceddatesettings as $modid => $datesettings) { @@ -386,7 +434,7 @@ public function validation($data, $files) { $cm = $cms[$modid]; $moderrors = array(); - if ($mod = report_editdates_mod_date_extractor::make($cm->modname, $course)) { + if ($mod = report_editdates_mod_date_extractor::make($cm->modname, $this->course)) { $moderrors = $mod->validate_dates($cm, $datesettings); if (!empty($moderrors)) { foreach ($moderrors as $errorfield => $errorstr) { @@ -396,6 +444,16 @@ public function validation($data, $files) { } } + $callbacks = get_plugins_with_function('report_editdates_form_validation', 'lib.php'); + foreach ($callbacks as $type => $plugins) { + foreach ($plugins as $plugin => $pluginfunction) { + $pluginerrors = $pluginfunction($this, $data); + if (!empty($pluginerrors)) { + $errors = array_merge($errors, $pluginerrors); + } + } + } + if (!empty($errors)) { // If there are any validation errors, which may be hidden a long way down this // very big form, put a message at the top too. @@ -405,6 +463,15 @@ public function validation($data, $files) { return $errors; } + public function definition_after_data() { + $callbacks = get_plugins_with_function('report_editdates_form_definition_after_data', 'lib.php'); + foreach ($callbacks as $type => $plugins) { + foreach ($plugins as $plugin => $pluginfunction) { + $pluginfunction($this, $this->_form); + } + } + } + public function render_timeline_view($data) { $data = self::sort_timeline_data($data); $config = get_config('report_editdates'); diff --git a/index.php b/index.php index 8b9da01..9998a1c 100644 --- a/index.php +++ b/index.php @@ -86,8 +86,15 @@ redirect($returnurl); } else if ($data = $mform->get_data()) { - // Process submitted data. + // Modify submitted data. + $callbacks = get_plugins_with_function('report_editdates_form_post_actions', 'lib.php'); + foreach ($callbacks as $type => $plugins) { + foreach ($plugins as $plugin => $pluginfunction) { + $data = $pluginfunction($data, $course); + } + } + // Process submitted data. $moddatesettings = array(); $blockdatesettings = array(); $sectiondatesettings = array(); diff --git a/readme.md b/readme.md index 3bec416..32b1636 100644 --- a/readme.md +++ b/readme.md @@ -41,3 +41,15 @@ For other plugins, there is the option to put the class in the other plugin. You need to make a class called `mod_`_mymodname_`_report_editdates_integration`, which therefore goes in mod/_mymodname_/classes/report_editdates_integration.php. For blocks, the equivalent class is `block_`_myblock_`_report_editdates_integration`. + +## Available callbacks + +To be aligned with a core extension point for the activity form via core callbacks, +this plugin allows developers to extend dates form the same way. + +Supported callbacks: + +* report_editdates_form_elements +* report_editdates_form_definition_after_data +* report_editdates_form_validation +* report_editdates_form_post_actions \ No newline at end of file