Skip to content

Filter report by the user groups #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion classes/lib/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public static function timespent($courseid, $userid, $rawformat=false) {
* @param bool $filter
* @return array
*/
public static function get_average($courseid, $duration = null, bool $filter = false) {
public static function get_average($courseid, $duration = null, bool $filter = false, array $groups = []) {
global $DB, $CFG, $SESSION;

$params = ['courseid' => $courseid];
Expand Down Expand Up @@ -333,9 +333,25 @@ public static function get_average($courseid, $duration = null, bool $filter = f
$sqltotal = "SELECT SUM(timespent)
FROM {block_dedication}
WHERE courseid = :courseid" . $sqlextra;

$sqlusers = "SELECT count(DISTINCT userid)
FROM {block_dedication}
WHERE courseid = :courseid" . $sqlextra;

if (!empty($groups)) {

[$sqlgroups, $paramsmembers] = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED);
$sqlmembers = "SELECT id, userid from {groups_members} WHERE groupid {$sqlgroups}";
$userids = $DB->get_records_sql_menu($sqlmembers, $paramsmembers);
$userids = array_values($userids);

[$sqlmembers, $paramsmembers] = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
$sqltotal .= " AND userid {$sqlmembers}";
$sqlusers .= " AND userid {$sqlmembers}";

$params = array_merge($params, $paramsmembers);
}

$totaldedication = $DB->get_field_sql($sqltotal, $params);
$totalusers = $DB->get_field_sql($sqlusers, $params);
}
Expand Down
12 changes: 11 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,22 @@
$PAGE->set_title("$course->fullname: ".get_string('sessionduration', 'block_dedication'));
$PAGE->set_heading($course->fullname);

// Fetch the current groupid.
$groups = groups_get_user_groups($courseid);
$groups = array_pop($groups);

echo $OUTPUT->header();
$average = \block_dedication\lib\utils::get_average($course->id);
$average = \block_dedication\lib\utils::get_average($course->id, null, false, $groups);

echo $OUTPUT->heading(get_string('timespentincourse', 'block_dedication'));
echo html_writer::div(get_string('totaltimespent', 'block_dedication', $average['total']));
echo html_writer::div(get_string('averagetimespent', 'block_dedication', $average['average']));
if (!empty($groups)) {
[$groupssql, $groupsparams] = $DB->get_in_or_equal($groups);
$groupsnamessql = "SELECT CONCAT(name) FROM {groups} WHERE id {$groupssql}";
$groupsnames = $DB->get_field_sql($groupsnamessql, $groupsparams);
echo html_writer::div(get_string('groups', 'block_dedication', $groupsnames));
}

$config = get_config('block_dedication');

Expand Down
1 change: 1 addition & 0 deletions lang/en/block_dedication.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
$string['perioddiff'] = '<strong>Elapsed time:</strong> {$a}';
$string['totaltimespent'] = '<strong>Total of all time spent in course:</strong> {$a}';
$string['averagetimespent'] = '<strong>Average time spent in course:</strong> {$a}';
$string['groups'] = '<strong>Group(s):</strong> {$a}';
$string['sessionstart'] = 'Session start';
$string['userdedication'] = 'Detailed course dedication of <em>{$a}</em>.';
$string['timespentincourse'] = 'Time spent in course';
Expand Down
Loading