diff --git a/classes/local/block_dash/attribute/completion_status_attribute.php b/classes/local/block_dash/attribute/completion_status_attribute.php new file mode 100644 index 00000000..1c5acf78 --- /dev/null +++ b/classes/local/block_dash/attribute/completion_status_attribute.php @@ -0,0 +1,64 @@ +. + +/** + * Transforms completion status into human readable form + * + * @package mod_videotime + * @copyright 2020 bdecent gmbh + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_videotime\local\block_dash\attribute; + +use block_dash\local\data_grid\field\attribute\abstract_field_attribute; +use mod_videotime\videotime_instance; + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->libdir.'/completionlib.php'); + +/** + * Transforms data to average view time. + * + * @package mod_videotime + * @copyright 2020 bdecent gmbh + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class completion_status_attribute extends abstract_field_attribute { + + /** + * After records are relieved from database each field has a chance to transform the data. + * Example: Convert unix timestamp into a human readable date format + * + * @param int $data + * @param \stdClass $record Entire row + * @return mixed + * @throws \moodle_exception + */ + public function transform_data($data, \stdClass $record) { + global $DB; + if ($data == COMPLETION_COMPLETE) { + return get_string("completed", "mod_videotime"); + } else if ($data == COMPLETION_COMPLETE_PASS) { + return get_string("passed", "mod_videotime"); + } else if ($data == COMPLETION_COMPLETE_FAIL) { + return get_string("failed", "mod_videotime"); + } else { + return get_string("incomplete", "mod_videotime"); + } + } +} diff --git a/classes/local/block_dash/attribute/first_session_attribute.php b/classes/local/block_dash/attribute/first_session_attribute.php index 5237dfd1..7aece919 100644 --- a/classes/local/block_dash/attribute/first_session_attribute.php +++ b/classes/local/block_dash/attribute/first_session_attribute.php @@ -48,8 +48,8 @@ public function transform_data($data, \stdClass $record) { $instance = videotime_instance::instance_by_id($data); - return $DB->get_field_sql('SELECT MIN(vts.timestarted) + return $DB->get_field_sql('SELECT MIN(vts.timecreated) FROM {videotimeplugin_pro_session} vts - WHERE vts.module_id = ? AND vts.timestarted > 0', [$instance->get_cm()->id]); + WHERE vts.module_id = ? AND vts.timecreated > 0', [$instance->get_cm()->id]); } } diff --git a/classes/local/block_dash/attribute/last_session_attribute.php b/classes/local/block_dash/attribute/last_session_attribute.php index a90dd5a1..12065047 100644 --- a/classes/local/block_dash/attribute/last_session_attribute.php +++ b/classes/local/block_dash/attribute/last_session_attribute.php @@ -48,8 +48,8 @@ public function transform_data($data, \stdClass $record) { $instance = videotime_instance::instance_by_id($data); - return $DB->get_field_sql('SELECT MAX(vts.timestarted) + return $DB->get_field_sql('SELECT MAX(vts.timecreated) FROM {videotimeplugin_pro_session} vts - WHERE vts.module_id = ? AND vts.timestarted > 0', [$instance->get_cm()->id]); + WHERE vts.module_id = ? AND vts.timecreated > 0', [$instance->get_cm()->id]); } } diff --git a/classes/local/block_dash/attribute/views_attribute.php b/classes/local/block_dash/attribute/views_attribute.php index ea26383e..862fd59d 100644 --- a/classes/local/block_dash/attribute/views_attribute.php +++ b/classes/local/block_dash/attribute/views_attribute.php @@ -49,7 +49,9 @@ public function transform_data($data, \stdClass $record) { $instance = videotime_instance::instance_by_id($data); return $DB->get_field_sql( - 'SELECT COUNT(*) FROM {videotimeplugin_pro_session} vts WHERE vts.module_id = ?', + 'SELECT COUNT(DISTINCT uuid) + SUM(CASE WHEN uuid IS NULL THEN 1 ELSE 0 END) + FROM {videotimeplugin_pro_session} vts + WHERE vts.module_id = ?', [$instance->get_cm()->id] ); } diff --git a/classes/local/block_dash/videotime_sessions_data_source.php b/classes/local/block_dash/videotime_sessions_data_source.php index 1798facb..47393def 100644 --- a/classes/local/block_dash/videotime_sessions_data_source.php +++ b/classes/local/block_dash/videotime_sessions_data_source.php @@ -26,6 +26,7 @@ use block_dash\local\dash_framework\query_builder\builder; use block_dash\local\dash_framework\query_builder\join; +use block_dash\local\dash_framework\query_builder\where; use block_dash\local\dash_framework\structure\user_table; use block_dash\local\data_grid\filter\bool_filter; use block_dash\local\data_grid\filter\course_condition; @@ -79,14 +80,22 @@ public function get_query_template(): builder { $builder = new builder(); $builder - ->select('vts.id', 'vts_id') + ->select("CONCAT(vt.id, '-', u.id)", 'vtn_id') ->from('videotime', 'vt') + ->join('videotime', 'vts', 'id', 'vt.id') ->join('course_modules', 'cm', 'instance', 'vt.id', join::TYPE_INNER_JOIN, ['cm.module' => $module]) - ->join('videotime_pro_session', 'vts', 'module_id', 'cm.id') - ->join('user', 'u', 'id', 'vts.user_id') + ->join('videotimeplugin_pro_session', 'vtn', 'module_id', 'cm.id') + ->join('user', 'u', 'id', 'vtn.user_id') ->join('course', 'c', 'id', 'vt.course') ->join('course_categories', 'cc', 'id', 'c.category') - ->groupby('vt.id')->groupby('vts.user_id'); + ->join('videotime_vimeo_video', 'vvv', 'link', 'vt.vimeo_url', join::TYPE_LEFT_JOIN) + ->join('course_modules_completion', 'cmc', 'coursemoduleid', 'cm.id', join::TYPE_LEFT_JOIN, ['cmc.userid' => 'u.id']) + ->groupby('vt.id') + ->groupby('vts.id') + ->groupby('u.id') + ->groupby('c.id') + ->groupby('vvv.duration') + ->groupby('vtn.user_id'); $filterpreferences = $this->get_preferences('filters'); diff --git a/classes/local/dash_framework/structure/videotime_session_table.php b/classes/local/dash_framework/structure/videotime_session_table.php index 5ea70c10..53bd2e7b 100644 --- a/classes/local/dash_framework/structure/videotime_session_table.php +++ b/classes/local/dash_framework/structure/videotime_session_table.php @@ -48,6 +48,7 @@ use mod_videotime\local\block_dash\attribute\video_created_attribute; use mod_videotime\local\block_dash\attribute\video_preview_attribute; use mod_videotime\local\block_dash\attribute\views_attribute; +use mod_videotime\local\block_dash\attribute\completion_status_attribute; use moodle_url; defined('MOODLE_INTERNAL') || die(); @@ -65,7 +66,7 @@ class videotime_session_table extends table { * Build a new table. */ public function __construct() { - parent::__construct('videotimeplugin_pro_session', 'vts'); + parent::__construct('videotime', 'vts'); } /** @@ -83,17 +84,17 @@ public function get_title(): string { * @return field_interface[] */ public function get_fields(): array { - return [ + $fields = [ new field('id', new lang_string('pluginname', 'videotime'), $this, null, [ new identifier_attribute() ]), new field('time', new lang_string('watch_time', 'videotime'), $this, 'SUM(time)', [ new time_attribute() ]), - new field('state', new lang_string('state_finished', 'videotime'), $this, 'MAX(state)', [ + new field('state', new lang_string('state_finished', 'videotime'), $this, 'MAX(vtn.state)', [ new bool_attribute() ]), - new field('timestarted', new lang_string('timestarted', 'videotime'), $this, 'MIN(vts.timestarted)', [ + new field('timestarted', new lang_string('timestarted', 'videotime'), $this, 'MIN(vtn.timecreated)', [ new date_attribute() ]), new field('percent_watch', new lang_string('watch_percent', 'videotime'), $this, 'MAX(percent_watch)', [ @@ -101,7 +102,23 @@ public function get_fields(): array { ]), new field('current_watch_time', new lang_string('currentwatchtime', 'videotime'), $this, 'MAX(current_watch_time)', [ new time_attribute() - ]) + ]), ]; + + if (videotime_has_repository()) { + $addfields = [ + new field('watched_time', new lang_string('watchedtime', 'videotime'), $this, 'MAX(current_watch_time)', [ + new time_attribute() + ]), + new field('time_left', new lang_string('timeleft', 'videotime'), $this, 'MIN(vvv.duration - current_watch_time)', [ + new time_attribute() + ]), + new field('status', new lang_string('activitystatus', 'videotime'), $this, 'MAX(cmc.completionstate)', [ + new completion_status_attribute() + ]) + ]; + $fields = array_merge($fields, $addfields); + } + return $fields; } } diff --git a/classes/local/dash_framework/structure/videotime_table.php b/classes/local/dash_framework/structure/videotime_table.php index 37961d5c..3ae41bbf 100644 --- a/classes/local/dash_framework/structure/videotime_table.php +++ b/classes/local/dash_framework/structure/videotime_table.php @@ -87,7 +87,7 @@ public function get_fields(): array { new field('id', new lang_string('pluginname', 'videotime'), $this, null, [ new identifier_attribute() ]), - new field('name', new lang_string('activity_name', 'videotime'), $this), + new field('name', new lang_string('activity_name', 'videotime'), $this, 'vt.name'), new field('url', new lang_string('videotimeurl', 'videotime'), $this, 'vt.id', [ new moodle_url_attribute(['url' => new moodle_url('/mod/videotime/view.php', ['v' => 'vt_id'])]) ]), diff --git a/classes/vimeo_embed.php b/classes/vimeo_embed.php index 4d55056b..c51faf03 100644 --- a/classes/vimeo_embed.php +++ b/classes/vimeo_embed.php @@ -42,8 +42,6 @@ */ class vimeo_embed implements \renderable, \templatable { - protected $cm = null; - /** * @var $cm Course module */ diff --git a/lang/en/videotime.php b/lang/en/videotime.php index 6d00c75e..cb2bb550 100644 --- a/lang/en/videotime.php +++ b/lang/en/videotime.php @@ -73,6 +73,9 @@ $string['confirmation'] = 'Confirmation'; $string['create_vimeo_app'] = 'Create Vimeo App'; $string['currentwatchtime'] = 'Current watch time'; +$string['watchedtime'] = "Time watched"; +$string['timeleft'] = "Time Left"; +$string['activitystatus'] = "Activity completion status"; $string['datasource:videotime_sessions_data_source'] = 'Video Time sessions'; $string['datasource:videotime_stats_data_source'] = 'Video Time stats'; $string['default'] = 'Default'; @@ -326,5 +329,9 @@ $string['completiondetail:_on_view_time'] = 'View for time {$a}'; $string['completiondetail:_on_finish'] = 'Finish video'; $string['completiondetail:_on_percent'] = 'Finish watching {$a} percent'; +$string['completed'] = "Completed"; +$string['passed'] = "Passed"; +$string['failed'] = "Failed"; +$string['incomplete'] = "Incomplete"; $string['videoopen'] = 'Allow viewing from'; $string['videoclose'] = 'Allow viewing until';