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 27, 2024
1 parent cdcf95f commit d0602a4
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 52 deletions.
20 changes: 10 additions & 10 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 @@ -93,9 +93,9 @@ public static function get_defined_custom_rules(): array {
}

return [
'completion_on_view_time_second',
'completion_on_view_time',
'completion_on_finish',
'completion_on_percent_value',
'completion_on_percent',
'completion_hide_detail',
];
}
Expand All @@ -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,9 +117,9 @@ 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),
'completion_on_percent' => get_string('completiondetail:_on_percent', 'videotime', $percentspent),
];
}

Expand All @@ -131,9 +131,9 @@ 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_on_percent',
'completion_on_view_time',
'completion_hide_detail',
'completionpassgrade',
'completionusegrade',
Expand Down
15 changes: 7 additions & 8 deletions lang/en/videotime.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<li>Enter Client ID and Client Secret <a href="{$a->configure_url}">here</a></li></ol>';
$string['confirmation'] = 'Confirmation';
$string['controls'] = 'Controls';
$string['coverage_help'] = 'Amount of video seen at least once by user';
$string['create_vimeo_app'] = 'Create Vimeo App';
$string['currentwatchtime'] = 'Current watch time';
$string['datasource:videotime_sessions_data_source'] = 'Video Time sessions';
Expand All @@ -98,6 +99,7 @@
$string['enabletabs_help'] = 'Display tabs with video which may contain additional information or interaction';
$string['estimated_request_time'] = 'Estimated time remaining';
$string['failed'] = "Failed";
$string['firstaccess_help'] = 'When user first played video';
$string['firstsession'] = 'First session';
$string['force'] = 'Force setting';
$string['force_help'] = 'If checked this default will override the instance setting.';
Expand All @@ -111,6 +113,7 @@
$string['insert_video_metadata'] = 'Insert metadata from video (may override activity settings)';
$string['invalid_session_state'] = 'Invalid session state.';
$string['label_mode'] = 'Label mode';
$string['lastaccess_help'] = 'When user last played video';
$string['lastsession'] = 'Last session';
$string['long'] = 'Long';
$string['managevideotimepluginplugins'] = 'Manage Video Time extensions';
Expand Down Expand Up @@ -285,6 +288,7 @@
$string['timeleft'] = "Time Left";
$string['timestarted'] = 'Date started';
$string['todo'] = 'TODO';
$string['totaltime_help'] = 'Time video was playing at normal speed';
$string['totaluniquevisitors'] = 'Total unique visitors';
$string['totalvideotime'] = 'Total video time: {$a->time}';
$string['totalviews'] = 'Total views';
Expand Down Expand Up @@ -316,7 +320,7 @@
$string['viewpercentgrade'] = 'Set grade equal to view percentage.';
$string['viewpercentgrade_help'] = 'Create grade item for this video. Student will receive a grade equal to their view percentage of the video.';
$string['views'] = 'Views';
$string['views_help'] = 'Number of times the activity has been viewed.';
$string['views_help'] = 'Number of separate times activity page was loaded to view video';
$string['vimeo_overview'] = 'Overview and setup';
$string['vimeo_url'] = 'Video URL';
$string['vimeo_url_help'] = 'Full URL of Vimeo video, Youtube video, or an external video or audio file.';
Expand All @@ -331,11 +335,6 @@
$string['watch_time'] = 'Watch time';
$string['watch_time_help'] = 'How long the student has watched the video in total (in 5s steps).';
$string['watchedtime'] = "Time watched";
$string['with_play_button'] = 'with play button';
$string['views_help'] = 'Number of separate times activity page was loaded to view video';
$string['watchtime_help'] = 'The farthest position seen by the user';
$string['watchpercent_help'] = 'The highest percentage of the video seend';
$string['firstaccess_help'] = 'When user first played video';
$string['lastaccess_help'] = 'When user last played video';
$string['totaltime_help'] = 'Time video was playing at normal speed';
$string['coverage_help'] = 'Amount of video seen at least once by user';
$string['watchtime_help'] = 'The farthest position seen by the user';
$string['with_play_button'] = 'with play button';
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
131 changes: 99 additions & 32 deletions mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,56 +237,67 @@ 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') . ':&nbsp;'
);
$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, $this->get_suffixed_name('completion_on_view_time_group'), '', [' '], false);
$mform->disabledIf(
$this->get_suffixed_name('completion_on_view_time_second'),
$this->get_suffixed_name('completion_on_view_time'),
'notchecked'
);

$group = [];
$group[] =& $mform->createElement(
'advcheckbox',
'completion_on_percent' . $suffix,
$this->get_suffixed_name('completion_on_percent'),
'',
get_string('completion_on_percent', 'videotime') . ':&nbsp;'
);
$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_group'), '', [' '], 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_group'),
$this->get_suffixed_name('completion_on_percent_group'),
];
}

Expand All @@ -300,11 +311,14 @@ 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 +332,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 +348,28 @@ 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 +386,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 +417,6 @@ public function data_preprocessing(&$defaultvalues) {
}
}


/**
* Completion suffix
*/
Expand All @@ -400,4 +426,45 @@ 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();
}

/**
* Allows module to modify the data returned by form get_data().
* This method is also called in the bulk activity completion form.
*
* Only available on moodleform_mod.
*
* @param stdClass $data the form data to be modified.
*/
public function data_postprocessing($data) {
parent::data_postprocessing($data);
// Turn off completion settings if the checkboxes aren't ticked.
$suffix = $this->get_suffix();
if (!empty($data->completionunlocked)) {
$suffix = $this->get_suffix();
$completion = $data->{'completion' . $suffix};
$autocompletion = !empty($completion) && $completion == COMPLETION_TRACKING_AUTOMATIC;
if (empty($data->{'completion_on_finish' . $suffix}) || !$autocompletion) {
$data->{'completion_on_finish' . $suffix} = 0;
}
if (empty($data->{'completion_on_percent_value' . $suffix}) || !$autocompletion) {
$data->{'completion_on_percent' . $suffix} = 0;
}
if (empty($data->{'completion_on_view_time_second' . $suffix}) || !$autocompletion) {
$data->{'completion_on_view_time' . $suffix} = 0;
}
if (empty($data->{'completion_hide_detail' . $suffix}) || !$autocompletion) {
$data->{'completion_hide_detail' . $suffix} = 0;
}
}
}
}

0 comments on commit d0602a4

Please sign in to comment.