From 9be9bce758160df1f573b585689c3b8b2808a6a0 Mon Sep 17 00:00:00 2001 From: Daniel Thies Date: Thu, 26 Sep 2024 12:06:16 -0500 Subject: [PATCH] VID-862: Simplify completion condition naming --- classes/completion/custom_completion.php | 12 ++-- lib.php | 4 +- mod_form.php | 71 +++++++++++++----------- 3 files changed, 46 insertions(+), 41 deletions(-) diff --git a/classes/completion/custom_completion.php b/classes/completion/custom_completion.php index 0b2a373e..6e652242 100644 --- a/classes/completion/custom_completion.php +++ b/classes/completion/custom_completion.php @@ -48,13 +48,13 @@ public function get_state(string $rule): int { $sessions = \videotimeplugin_pro\module_sessions::get($this->cm->id, $this->userid); switch ($rule) { - case 'completion_on_view_time_second': + case 'completion_on_view_time': $status = $sessions->get_total_time() >= $videotime->completion_on_view_time_second; break; case 'completion_on_finish': $status = $sessions->is_finished(); break; - case 'completion_on_percent_value': + case 'completion_on_percent': $status = $videotime->completion_on_percent && (($sessions->get_percent() * 100) >= $videotime->completion_on_percent_value); break; @@ -106,8 +106,8 @@ public static function get_defined_custom_rules(): array { * @return array */ public function get_custom_rule_descriptions(): array { - $timespent = format_time($this->cm->customdata['customcompletionrules']['completion_on_view_time_second'] ?? 0); - $percentspent = $this->cm->customdata['customcompletionrules']['completion_on_percent_value'] ?? 0; + $timespent = format_time($this->cm->customdata['customcompletionrules']['completion_on_view_time'] ?? 0); + $percentspent = $this->cm->customdata['customcompletionrules']['completion_on_percent'] ?? 0; // Only return general description if we are hiding the details. if (!empty($this->cm->customdata['customcompletionrules']['completion_hide_detail'])) { @@ -117,7 +117,7 @@ public function get_custom_rule_descriptions(): array { } return [ - 'completion_on_view_time_second' => get_string('completiondetail:_on_view_time', 'videotime', $timespent), + 'completion_on_view_time' => get_string('completiondetail:_on_view_time', 'videotime', $timespent), 'completion_on_finish' => get_string('completiondetail:_on_finish', 'videotime'), 'completion_on_percent_value' => get_string('completiondetail:_on_percent', 'videotime', $percentspent), ]; @@ -131,9 +131,7 @@ public function get_custom_rule_descriptions(): array { public function get_sort_order(): array { return [ 'completionview', - 'completion_on_view_time_second', 'completion_on_finish', - 'completion_on_percent_value', 'completion_hide_detail', 'completionpassgrade', 'completionusegrade', diff --git a/lib.php b/lib.php index ad1dfb56..846de2c9 100644 --- a/lib.php +++ b/lib.php @@ -639,11 +639,11 @@ function videotime_get_coursemodule_info($coursemodule) { $result->customdata['customcompletionrules']['completion_hide_detail'] = $instance->completion_hide_detail; } else { if ($instance->completion_on_view_time) { - $result->customdata['customcompletionrules']['completion_on_view_time_second'] + $result->customdata['customcompletionrules']['completion_on_view_time'] = $instance->completion_on_view_time_second; } if ($instance->completion_on_percent) { - $result->customdata['customcompletionrules']['completion_on_percent_value'] + $result->customdata['customcompletionrules']['completion_on_percent'] = $instance->completion_on_percent_value; } $result->customdata['customcompletionrules']['completion_on_finish'] = $instance->completion_on_finish; diff --git a/mod_form.php b/mod_form.php index 4f9cdfa1..93afcb7c 100644 --- a/mod_form.php +++ b/mod_form.php @@ -237,56 +237,54 @@ public function add_completion_rules() { $mform =& $this->_form; if (videotime_has_pro()) { - $suffix = $this->get_suffix(); - // Completion on view and seconds. $group = []; $group[] =& $mform->createElement( 'advcheckbox', - 'completion_on_view_time' . $suffix, + $this->get_suffixed_name('completion_on_view_time'), '', get_string('completion_on_view', 'videotime') . ': ' ); - $group[] =& $mform->createElement('text', 'completion_on_view_time_second', '', ['size' => 3]); + $group[] =& $mform->createElement('text', $this->get_suffixed_name('completion_on_view_time_second'), '', ['size' => 3]); $group[] =& $mform->createElement('static', 'seconds', '', get_string('seconds', 'videotime')); - $mform->setType('completion_on_view_time_second', PARAM_INT); - $mform->addGroup($group, 'completion_on_view' . $suffix, '', [' '], false); - $mform->disabledIf('completion_on_view_time_second', 'completion_on_view_time', 'notchecked'); + $mform->setType($this->get_suffixed_name('completion_on_view_time_second'), PARAM_INT); + $mform->addGroup($group, ('completion_on_view_time_group'), '', [' '], false); + $mform->addElement('text', 'seconds', '', ['size' => 3]); $group = []; $group[] =& $mform->createElement( 'advcheckbox', - 'completion_on_percent' . $suffix, + $this->get_suffixed_name('completion_on_percent'), '', get_string('completion_on_percent', 'videotime') . ': ' ); - $group[] =& $mform->createElement('text', 'completion_on_percent_value', '', ['size' => 3]); + $group[] =& $mform->createElement('text', $this->get_suffixed_name('completion_on_percent_value'), '', ['size' => 3]); $group[] =& $mform->createElement('static', 'percent_label', '', '%'); - $mform->setType('completion_on_percent_value', PARAM_INT); - $mform->addGroup($group, 'completion_on_percent' . $suffix, '', [' '], false); - $mform->disabledIf('completion_on_percent_value', 'completion_on_percent', 'notchecked'); + $mform->setType($this->get_suffixed_name('completion_on_percent_value'), PARAM_INT); + $mform->addGroup($group, $this->get_suffixed_name('completion_on_percent'), '', [' '], false); + $mform->disabledIf($this->get_suffixed_name('completion_on_percent_value'), $this->get_suffixed_name('completion_on_percent'), 'notchecked'); $mform->addElement( 'advcheckbox', - 'completion_on_finish' . $suffix, + $this->get_suffixed_name('completion_on_finish'), '', get_string('completion_on_finish', 'videotime') ); - $mform->setType('completion_on_finish' . $suffix, PARAM_BOOL); + $mform->setType($this->get_suffixed_name('completion_on_finish'), PARAM_BOOL); $mform->addElement( 'advcheckbox', - 'completion_hide_detail' . $suffix, + $this->get_suffixed_name('completion_hide_detail'), '', get_string('completion_hide_detail', 'videotime') ); - $mform->setType('completion_hide_detail', PARAM_BOOL); + $mform->setType($this->get_suffixed_name('completion_hide_detail'), PARAM_BOOL); return [ - 'completion_on_finish' . $suffix, - 'completion_hide_detail' . $suffix, - 'completion_on_view' . $suffix, - 'completion_on_percent' . $suffix, + $this->get_suffixed_name('completion_on_finish'), + $this->get_suffixed_name('completion_hide_detail'), + $this->get_suffixed_name('completion_on_view_time'), + $this->get_suffixed_name('completion_on_percent'), ]; } @@ -300,11 +298,10 @@ public function add_completion_rules() { * @return bool True if one or more rules is enabled, false if none are. */ public function completion_rule_enabled($data) { - $suffix = $this->get_suffix(); return ( - (!empty($data['completion_on_view_time' . $suffix]) && $data['completion_on_view_time_second'] != 0)) || - !empty($data['completion_on_finish' . $suffix] || - (!empty($data['completion_on_percent'] . $suffix) && $data['completion_on_percent_value'])); + (!empty($data[$this->get_suffixed_name('completion_on_view_time')]) && $data[$this->get_suffixed_name('completion_on_view_time_second')] != 0)) || + !empty($data[$this->get_suffixed_name('completion_on_finish')] || + (!empty($data[$this->get_suffixed_name('completion_on_percent')]) && $data[$this->get_suffixed_name('completion_on_percent_value')])); } /** @@ -318,7 +315,6 @@ public function validation($data, $files) { global $USER; $errors = parent::validation($data, $files); - $suffix = $this->get_suffix(); if (!isset($data['vimeo_url']) || empty($data['vimeo_url'])) { $fs = get_file_storage(); @@ -335,16 +331,16 @@ public function validation($data, $files) { } // Make sure seconds are set if completion on view time is enabled. - if (isset($data['completion_on_view_time' . $suffix]) && $data['completion_on_view_time' . $suffix]) { - if (isset($data['completion_on_view_time_second']) && !$data['completion_on_view_time_second']) { - $errors['completion_on_view_time_second'] = get_string('required'); + if (isset($data[$this->get_suffixed_name('completion_on_view_time')]) && $data[$this->get_suffixed_name('completion_on_view_time')]) { + if (isset($data[$this->get_suffixed_name('completion_on_view_time_second')]) && !$data[$this->get_suffixed_name('completion_on_view_time_second')]) { + $errors[$this->get_suffixed_name('completion_on_view_time_second')] = get_string('required'); } } // Make sure percent value is set if completion on percent is enabled. - if (isset($data['completion_on_percent' . $suffix]) && $data['completion_on_percent' . $suffix]) { - if (isset($data['completion_on_percent_value']) && !$data['completion_on_percent_value']) { - $errors['completion_on_percent_value'] = get_string('required'); + if (isset($data[$this->get_suffixed_name('completion_on_percent')]) && $data[$this->get_suffixed_name('completion_on_percent')]) { + if (isset($data[$this->get_suffixed_name('completion_on_percent_value')]) && !$data[$this->get_suffixed_name('completion_on_percent_value')]) { + $errors[$this->get_suffixed_name('completion_on_percent_value')] = get_string('required'); } } @@ -361,6 +357,8 @@ public function validation($data, $files) { * @param array $defaultvalues */ public function data_preprocessing(&$defaultvalues) { + parent::data_preprocessing($defaultvalues); + // Editing existing instance. if ($this->current->instance) { $draftitemid = file_get_submitted_draft_itemid('video_description'); @@ -390,7 +388,6 @@ public function data_preprocessing(&$defaultvalues) { } } - /** * Completion suffix */ @@ -400,4 +397,14 @@ public function get_suffix(): string { } return ''; } + + /** + * Completion condition with suffix + * + * @param string name Name without suffix + * @return string + */ + public function get_suffixed_name($name): string { + return $name . $this->get_suffix(); + } }