Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 89 additions & 22 deletions form.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,69 @@
* @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()
*/
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.
Expand All @@ -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)) {
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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.
Expand All @@ -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;
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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.
Expand All @@ -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');
Expand Down
9 changes: 8 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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