Skip to content

Commit 82897a2

Browse files
committed
MD-1661: Add form extensibility callbacks (PR moodleou#51)
Add protected properties ($modinfo, $course, $activitytype) and public getters to report_editdates_form so other plugins can inspect form state. Refactor definition() and validation() to use instance properties. Add four plugin callback hooks: - report_editdates_form_elements: inject extra form elements - report_editdates_form_definition_after_data: called after form data set - report_editdates_form_validation: add extra validation rules - report_editdates_form_post_actions: modify submitted data before save Document new callbacks in readme.md. Ref: moodleou#51 (commit cd21173) Made-with: Cursor
1 parent 068bddd commit 82897a2

File tree

3 files changed

+123
-22
lines changed

3 files changed

+123
-22
lines changed

form.php

Lines changed: 101 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,69 @@
3636
*/
3737
class report_editdates_form extends moodleform {
3838

39+
// BEGIN LSU MD-1661: Add properties and public getters for form extensibility callbacks
40+
// See: https://github.com/moodleou/moodle-report_editdates/pull/51 (commit cd21173)
41+
/**
42+
* Mod info instance set for the form.
43+
* @var \course_modinfo|null
44+
*/
45+
protected $modinfo;
46+
47+
/**
48+
* Course.
49+
* @var \stdClass
50+
*/
51+
protected $course;
52+
53+
/**
54+
* Selected activity type.
55+
* @var string
56+
*/
57+
protected $activitytype;
58+
59+
/**
60+
* Get course mod info instance set for the form.
61+
* @return course_modinfo|null
62+
*/
63+
public function get_modinfo(): ?course_modinfo {
64+
return $this->modinfo;
65+
}
66+
67+
/**
68+
* Course object.
69+
* @return \stdClass
70+
*/
71+
public function get_course(): stdClass {
72+
return $this->course;
73+
}
74+
75+
/**
76+
* Selected activity type.
77+
* @return string
78+
*/
79+
public function get_activitytype(): string {
80+
return $this->activitytype;
81+
}
82+
// END LSU MD-1661: Add properties and public getters for form extensibility callbacks
83+
3984
#[\Override]
4085
public function definition() {
4186
global $CFG, $DB, $PAGE;
4287
$mform = $this->_form;
4388

44-
$modinfo = $this->_customdata['modinfo'];
45-
$course = $this->_customdata['course'];
46-
$activitytype = $this->_customdata['activitytype'];
89+
$this->modinfo = $this->_customdata['modinfo'];
90+
$this->course = $this->_customdata['course'];
91+
$this->activitytype = $this->_customdata['activitytype'];
4792
$config = get_config('report_editdates');
4893

4994
$coursehasavailability = !empty($CFG->enableavailability);
50-
$coursehascompletion = !empty($CFG->enablecompletion) && !empty($course->enablecompletion);
95+
$coursehascompletion = !empty($CFG->enablecompletion) && !empty($this->course->enablecompletion);
5196

5297
// Context instance of the course.
53-
$coursecontext = context_course::instance($course->id);
98+
$coursecontext = context_course::instance($this->course->id);
5499

55100
// Store current activity type.
56-
$mform->addElement('hidden', 'activitytype', $activitytype);
101+
$mform->addElement('hidden', 'activitytype', $this->activitytype);
57102
$mform->setType('activitytype', PARAM_PLUGIN);
58103

59104
// Invisible static element. Used as the holder for a validation message sometimes.
@@ -69,11 +114,11 @@ public function definition() {
69114

70115
$mform->addElement('date_time_selector', 'coursestartdate', get_string('startdate'));
71116
$mform->addHelpButton('coursestartdate', 'startdate');
72-
$mform->setDefault('coursestartdate', $course->startdate);
117+
$mform->setDefault('coursestartdate', $this->course->startdate);
73118

74119
$mform->addElement('date_time_selector', 'courseenddate', get_string('enddate'), ['optional' => true]);
75120
$mform->addHelpButton('courseenddate', 'enddate');
76-
$mform->setDefault('courseenddate', $course->enddate);
121+
$mform->setDefault('courseenddate', $this->course->enddate);
77122

78123
// If user is not capable, make it read only.
79124
if (!has_capability('moodle/course:update', $coursecontext)) {
@@ -91,8 +136,8 @@ public function definition() {
91136
$prevsectionnum = -1;
92137

93138
// Cycle through all the sections in the course.
94-
$cms = $modinfo->get_cms();
95-
$sections = $modinfo->get_section_info_all();
139+
$cms = $this->modinfo->get_cms();
140+
$sections = $this->modinfo->get_section_info_all();
96141
$timeline = [];
97142
foreach ($sections as $sectionnum => $section) {
98143
$ismodadded = false;
@@ -105,7 +150,7 @@ public function definition() {
105150

106151
// New section, create header.
107152
if ($prevsectionnum != $sectionnum) {
108-
$sectionname = get_section_name($course, $section);
153+
$sectionname = get_section_name($this->course, $section);
109154
$headername = 'section' . $sectionnum . 'header';
110155
$mform->addElement('header', $headername, $sectionname);
111156
$mform->setExpanded($headername, false);
@@ -163,8 +208,8 @@ public function definition() {
163208
}
164209

165210
// Cycle through each module in a section.
166-
if (isset($modinfo->sections[$sectionnum])) {
167-
foreach ($modinfo->sections[$sectionnum] as $cmid) {
211+
if (isset($this->modinfo->sections[$sectionnum])) {
212+
foreach ($this->modinfo->sections[$sectionnum] as $cmid) {
168213
$cm = $cms[$cmid];
169214

170215
// No need to display/continue if this module is not visible to user.
@@ -173,7 +218,7 @@ public function definition() {
173218
}
174219

175220
// If activity filter is on, then filter module by activity type.
176-
if ($activitytype && ($cm->modname != $activitytype && $activitytype != "all")) {
221+
if ($this->activitytype && ($cm->modname != $this->activitytype && $this->activitytype != "all")) {
177222
continue;
178223
}
179224

@@ -199,7 +244,7 @@ public function definition() {
199244

200245
// Call get_settings method for the acitivity/module.
201246
// Get instance of the mod's date exractor class.
202-
$mod = report_editdates_mod_date_extractor::make($cm->modname, $course);
247+
$mod = report_editdates_mod_date_extractor::make($cm->modname, $this->course);
203248
if ($mod && ($cmdatesettings = $mod->get_settings($cm))) {
204249
// Added activity name on the form.
205250
foreach ($cmdatesettings as $cmdatetype => $cmdatesetting) {
@@ -304,7 +349,7 @@ public function definition() {
304349

305350
// Iterate though blocks array.
306351
foreach ($courseblocks as $blockid => $block) {
307-
$blockdatextrator = report_editdates_block_date_extractor::make($block->blockname, $course);
352+
$blockdatextrator = report_editdates_block_date_extractor::make($block->blockname, $this->course);
308353
if ($blockdatextrator) {
309354
// Create the block instance.
310355
$blockobj = block_instance($block->blockname, $block, $PAGE);
@@ -354,17 +399,38 @@ public function definition() {
354399
$mform->addElement('static', 'timelineview', '');
355400
$mform->addElement('html', self::render_timeline_view($timeline));
356401
}
402+
403+
// BEGIN LSU MD-1661: Invoke report_editdates_form_elements callbacks for extensibility
404+
// See: https://github.com/moodleou/moodle-report_editdates/pull/51 (commit cd21173)
405+
$callbacks = get_plugins_with_function('report_editdates_form_elements', 'lib.php');
406+
foreach ($callbacks as $type => $plugins) {
407+
foreach ($plugins as $plugin => $pluginfunction) {
408+
$pluginfunction($this, $this->_form);
409+
}
410+
}
411+
// END LSU MD-1661: Invoke report_editdates_form_elements callbacks for extensibility
357412
}
358413

414+
// BEGIN LSU MD-1661: definition_after_data callback for form extensibility
415+
// See: https://github.com/moodleou/moodle-report_editdates/pull/51 (commit cd21173)
416+
/**
417+
* Invoke callbacks after form data is set.
418+
*/
419+
public function definition_after_data() {
420+
$callbacks = get_plugins_with_function('report_editdates_form_definition_after_data', 'lib.php');
421+
foreach ($callbacks as $type => $plugins) {
422+
foreach ($plugins as $plugin => $pluginfunction) {
423+
$pluginfunction($this, $this->_form);
424+
}
425+
}
426+
}
427+
// END LSU MD-1661: definition_after_data callback for form extensibility
428+
359429
#[\Override]
360430
public function validation($data, $files) {
361431
global $CFG;
362432
$errors = parent::validation($data, $files);
363433

364-
$modinfo = $this->_customdata['modinfo'];
365-
$course = $this->_customdata['course'];
366-
$coursecontext = context_course::instance($course->id);
367-
368434
$moddatesettings = [];
369435
$forceddatesettings = [];
370436
foreach ($data as $key => $value) {
@@ -399,7 +465,7 @@ public function validation($data, $files) {
399465
}
400466
}
401467

402-
$cms = $modinfo->get_cms();
468+
$cms = $this->modinfo->get_cms();
403469

404470
// Validating forced date settings.
405471
foreach ($forceddatesettings as $modid => $datesettings) {
@@ -420,7 +486,7 @@ public function validation($data, $files) {
420486
$cm = $cms[$modid];
421487
$moderrors = [];
422488

423-
if ($mod = report_editdates_mod_date_extractor::make($cm->modname, $course)) {
489+
if ($mod = report_editdates_mod_date_extractor::make($cm->modname, $this->course)) {
424490
$moderrors = $mod->validate_dates($cm, $datesettings);
425491
if (!empty($moderrors)) {
426492
foreach ($moderrors as $errorfield => $errorstr) {
@@ -430,6 +496,19 @@ public function validation($data, $files) {
430496
}
431497
}
432498

499+
// BEGIN LSU MD-1661: Invoke report_editdates_form_validation callbacks for extensibility
500+
// See: https://github.com/moodleou/moodle-report_editdates/pull/51 (commit cd21173)
501+
$callbacks = get_plugins_with_function('report_editdates_form_validation', 'lib.php');
502+
foreach ($callbacks as $type => $plugins) {
503+
foreach ($plugins as $plugin => $pluginfunction) {
504+
$pluginerrors = $pluginfunction($this, $data);
505+
if (!empty($pluginerrors)) {
506+
$errors = array_merge($errors, $pluginerrors);
507+
}
508+
}
509+
}
510+
// END LSU MD-1661: Invoke report_editdates_form_validation callbacks for extensibility
511+
433512
if (!empty($errors)) {
434513
// If there are any validation errors, which may be hidden a long way down this
435514
// very big form, put a message at the top too.

index.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@
9393
redirect($returnurl);
9494

9595
} else if ($data = $mform->get_data()) {
96+
// BEGIN LSU MD-1661: Invoke report_editdates_form_post_actions callbacks before processing
97+
// See: https://github.com/moodleou/moodle-report_editdates/pull/51 (commit cd21173)
98+
$callbacks = get_plugins_with_function('report_editdates_form_post_actions', 'lib.php');
99+
foreach ($callbacks as $type => $plugins) {
100+
foreach ($plugins as $plugin => $pluginfunction) {
101+
$data = $pluginfunction($data, $course);
102+
}
103+
}
104+
// END LSU MD-1661: Invoke report_editdates_form_post_actions callbacks before processing
105+
96106
// Process submitted data.
97107

98108
$moddatesettings = [];

readme.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,15 @@ For other plugins, there is the option to put the class in the other plugin.
4141
You need to make a class called `mod_`_mymodname_`_report_editdates_integration`,
4242
which therefore goes in mod/_mymodname_/classes/report_editdates_integration.php.
4343
For blocks, the equivalent class is `block_`_myblock_`_report_editdates_integration`.
44+
45+
## Available callbacks
46+
47+
To be aligned with a core extension point for the activity form via core callbacks,
48+
this plugin allows developers to extend dates form the same way.
49+
50+
Supported callbacks:
51+
52+
* report_editdates_form_elements
53+
* report_editdates_form_definition_after_data
54+
* report_editdates_form_validation
55+
* report_editdates_form_post_actions

0 commit comments

Comments
 (0)