From 722bcecf824ec02d0eb98e90bca45511e627da1e Mon Sep 17 00:00:00 2001 From: Lyannic Date: Thu, 13 Jun 2024 15:21:44 +0200 Subject: [PATCH] Conditionally add suffix to completion rule elements Use unsiffxed completionpass identifier for help button Add PHPdoc for get_completion_element_name --- mod_form.php | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/mod_form.php b/mod_form.php index 0255943..de8660d 100644 --- a/mod_form.php +++ b/mod_form.php @@ -244,20 +244,41 @@ public function add_completion_rules() : array { $items = array(); $group = array(); + $completionpasselement = $this->get_completion_rule_element_name('completionpass'); $group[] = $mform->createElement( 'advcheckbox', - 'completionpass', + $completionpasselement, null, get_string('completionpass', 'mumie'), array('group' => 'cpass') ); - $mform->disabledIf('completionpass', 'completionusegrade', 'notchecked'); - $mform->addGroup($group, 'completionpassgroup', get_string('completionpass', 'mumie'), '   ', false); - $mform->addHelpButton('completionpassgroup', 'completionpass', 'mumie'); - $items[] = 'completionpassgroup'; + $completionusegradeelement = $this->get_completion_rule_element_name('completionusegrade'); + $mform->disabledIf($completionpasselement, $completionusegradeelement, 'notchecked'); + $completionpassgroupelement = $this->get_completion_rule_element_name('completionpassgroup'); + $mform->addGroup($group, $completionpassgroupelement, get_string('completionpass', 'mumie'), '   ', false); + $mform->addHelpButton($completionpassgroupelement, 'completionpass', 'mumie'); + $items[] = $completionpassgroupelement; return $items; } + /** + * Get the completion rule's element name. + * + * Conditionally add suffix for Moodle >= 4.3. + * + * @param string $rawname The raw name of the completion rule. + * @return string The properly suffixed element name. + */ + private function get_completion_rule_element_name($rawname) : string { + global $CFG; + if ($CFG->branch < 403) { + $suffix = ''; + } else { + $suffix = $this->get_suffix(); + } + return $rawname . $suffix; + } + /** * Disable all options for grades if the user has chosen to link a course instead of a problem. */ @@ -477,7 +498,8 @@ function ($server) use ($data) { * @return bool True if one or more rules is enabled, false if none are. */ public function completion_rule_enabled($data) : bool { - return !empty($data['completionpass']); + $completionpasselement = $this->get_completion_rule_element_name('completionpass'); + return !empty($data[$completionpasselement]); }