Skip to content

Commit

Permalink
VID-862: Simplify completion condition naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Thies committed Sep 26, 2024
1 parent cdcf95f commit 9be9bce
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
12 changes: 5 additions & 7 deletions classes/completion/custom_completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'])) {
Expand All @@ -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),
];
Expand All @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
71 changes: 39 additions & 32 deletions mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
];
}

Expand All @@ -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')]));
}

/**
Expand All @@ -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();
Expand All @@ -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');
}
}

Expand All @@ -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');
Expand Down Expand Up @@ -390,7 +388,6 @@ public function data_preprocessing(&$defaultvalues) {
}
}


/**
* Completion suffix
*/
Expand All @@ -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();
}
}

0 comments on commit 9be9bce

Please sign in to comment.