Skip to content

Commit

Permalink
VID-690: Update Dash session data source
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Thies committed Aug 15, 2023
1 parent c1d7043 commit 4a2e11a
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 17 deletions.
64 changes: 64 additions & 0 deletions classes/local/block_dash/attribute/completion_status_attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Transforms completion status into human readable form
*
* @package mod_videotime
* @copyright 2020 bdecent gmbh <https://bdecent.de>
* @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 <https://bdecent.de>
* @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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
4 changes: 2 additions & 2 deletions classes/local/block_dash/attribute/last_session_attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
4 changes: 3 additions & 1 deletion classes/local/block_dash/attribute/views_attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
);
}
Expand Down
17 changes: 13 additions & 4 deletions classes/local/block_dash/videotime_sessions_data_source.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');

Expand Down
27 changes: 22 additions & 5 deletions classes/local/dash_framework/structure/videotime_session_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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');
}

/**
Expand All @@ -83,25 +84,41 @@ 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)', [
new percent_attribute()
]),
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;
}
}
2 changes: 1 addition & 1 deletion classes/local/dash_framework/structure/videotime_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])])
]),
Expand Down
2 changes: 0 additions & 2 deletions classes/vimeo_embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
*/
class vimeo_embed implements \renderable, \templatable {

protected $cm = null;

/**
* @var $cm Course module
*/
Expand Down
7 changes: 7 additions & 0 deletions lang/en/videotime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';

0 comments on commit 4a2e11a

Please sign in to comment.