From 0ae1216f1dcff06dc7a7ea02e14a851efcae6763 Mon Sep 17 00:00:00 2001 From: Dan Marsden Date: Mon, 3 Sep 2018 15:22:17 +1200 Subject: [PATCH] Display user summary in mobile app. --- classes/output/mobile.php | 86 +++++++++++++++++++++++++++++ classes/summary.php | 21 ++++++- db/mobile.php | 51 +++++++++++++++++ export.php | 3 +- lib.php | 3 +- renderer.php | 19 +++---- renderhelpers.php | 15 ++--- templates/mobile_view_page.mustache | 74 +++++++++++++++++++++++++ 8 files changed, 246 insertions(+), 26 deletions(-) create mode 100644 classes/output/mobile.php create mode 100644 db/mobile.php create mode 100644 templates/mobile_view_page.mustache diff --git a/classes/output/mobile.php b/classes/output/mobile.php new file mode 100644 index 0000000..82a08d0 --- /dev/null +++ b/classes/output/mobile.php @@ -0,0 +1,86 @@ +. + +/** + * Contains the mobile output class for the attendance + * + * @package mod_attendance + * @copyright 2018 Dan Marsden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_attendance\output; + +defined('MOODLE_INTERNAL') || die(); +/** + * Mobile output class for the attendance. + * + * @copyright 2018 Dan Marsden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mobile { + /** + * Returns the initial page when viewing the activity for the mobile app. + * + * @param array $args Arguments from tool_mobile_get_content WS + * @return array HTML, javascript and other data + */ + public static function mobile_view_activity($args) { + global $OUTPUT, $DB, $USER, $PAGE, $CFG; + + require_once($CFG->dirroot.'/mod/attendance/locallib.php'); + + $args = (object) $args; + $cmid = $args->cmid; + $courseid = $args->courseid; + $groupid = empty($args->group) ? 0 : $args->group; // By default, group 0. + + // Capabilities check. + $cm = get_coursemodule_from_id('attendance', $cmid); + + require_login($courseid, false , $cm, true, true); + + $context = \context_module::instance($cm->id); + require_capability('mod/attendance:view', $context); + + $attendance = $DB->get_record('attendance', array('id' => $cm->instance), '*', MUST_EXIST); + $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); + + $pageparams = new \mod_attendance_view_page_params(); + + $pageparams->studentid = $USER->id; + $pageparams->mode = \mod_attendance_view_page_params::MODE_THIS_COURSE; + $pageparams->view = 5; // Show all sessions for this course? + + $att = new \mod_attendance_structure($attendance, $cm, $course, $context, $pageparams); + + $summary = new \mod_attendance_summary($att->id, array($USER->id), $att->pageparams->startdate, + $att->pageparams->enddate); + $data = array('attendance' => $attendance, + 'summary' => $summary->get_all_sessions_summary_for($USER->id)); + + return [ + 'templates' => [ + [ + 'id' => 'main', + 'html' => $OUTPUT->render_from_template('mod_attendance/mobile_view_page', $data), + ], + ], + 'javascript' => '', + 'otherdata' => '' + ]; + } +} \ No newline at end of file diff --git a/classes/summary.php b/classes/summary.php index bec15cf..2e83fac 100644 --- a/classes/summary.php +++ b/classes/summary.php @@ -137,6 +137,11 @@ class mod_attendance_summary { $usersummary->userstakensessionsbyacronym = array(); } + $usersummary->pointssessionscompleted = format_float($usersummary->takensessionspoints, 1, true, true) . ' / ' . + format_float($usersummary->takensessionsmaxpoints, 1, true, true); + + $usersummary->percentagesessionscompleted = format_float($usersummary->takensessionspercentage * 100) . '%'; + return $usersummary; } @@ -168,11 +173,25 @@ class mod_attendance_summary { } $usersummary->allsessionspercentage = attendance_calc_fraction($usersummary->takensessionspoints, $usersummary->allsessionsmaxpoints); + $usersummary->allsessionspercentage = format_float($usersummary->allsessionspercentage * 100) . '%'; $deltapoints = $usersummary->allsessionsmaxpoints - $usersummary->takensessionsmaxpoints; + $usersummary->maxpossiblepoints = $usersummary->takensessionspoints + $deltapoints; - $usersummary->maxpossiblepercentage = attendance_calc_fraction($usersummary->maxpossiblepoints, + $usersummary->maxpossiblepoints = format_float($usersummary->maxpossiblepoints, 1, true, true) . ' / ' . + format_float($usersummary->allsessionsmaxpoints, 1, true, true); + + $usersummary->maxpossiblepercentage = attendance_calc_fraction(($usersummary->takensessionspoints + $deltapoints), $usersummary->allsessionsmaxpoints); + $usersummary->maxpossiblepercentage = format_float($usersummary->maxpossiblepercentage * 100) . '%'; + + $usersummary->pointssessionscompleted = format_float($usersummary->takensessionspoints, 1, true, true) . ' / ' . + format_float($usersummary->takensessionsmaxpoints, 1, true, true); + + $usersummary->percentagesessionscompleted = format_float($usersummary->takensessionspercentage * 100) . '%'; + + $usersummary->pointsallsessions = format_float($usersummary->takensessionspoints, 1, true, true) . ' / ' . + format_float($usersummary->allsessionsmaxpoints, 1, true, true); return $usersummary; } diff --git a/db/mobile.php b/db/mobile.php new file mode 100644 index 0000000..290fb34 --- /dev/null +++ b/db/mobile.php @@ -0,0 +1,51 @@ +. + +/** + * Defines mobile handlers. + * + * @package mod_attendance + * @copyright 2018 Dan Marsdenb + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$addons = [ + 'mod_attendance' => [ + 'handlers' => [ + 'view' => [ + 'displaydata' => [ + 'icon' => $CFG->wwwroot . '/mod/attendance/pix/icon.png', + 'class' => '', + ], + 'delegate' => 'CoreCourseModuleDelegate', + 'method' => 'mobile_view_activity' + ] + ], + 'lang' => [ // Language strings that are used in all the handlers. + ['pluginname', 'attendance'], + ['sessionscompleted', 'attendance'], + ['pointssessionscompleted', 'attendance'], + ['percentagesessionscompleted', 'attendance'], + ['sessionstotal', 'attendance'], + ['pointsallsessions', 'attendance'], + ['percentageallsessions', 'attendance'], + ['maxpossiblepoints', 'attendance'], + ['maxpossiblepercentage', 'attendance'], + ] + ] +]; \ No newline at end of file diff --git a/export.php b/export.php index 9a8e594..fabb0d5 100644 --- a/export.php +++ b/export.php @@ -181,8 +181,7 @@ if ($formdata = $mform->get_data()) { } $data->table[$i][] = $usersummary->numtakensessions; - $data->table[$i][] = format_float($usersummary->takensessionspoints, 1, true, true) . ' / ' . - format_float($usersummary->takensessionsmaxpoints, 1, true, true); + $data->table[$i][] = $usersummary->pointssessionscompleted; $data->table[$i][] = format_float($usersummary->takensessionspercentage * 100); $i++; diff --git a/lib.php b/lib.php index 110a420..0660b0d 100644 --- a/lib.php +++ b/lib.php @@ -285,8 +285,7 @@ function attendance_user_outline($course, $user, $mod, $attendance) { $summary = new mod_attendance_summary($attendance->id, $user->id); $usersummary = $summary->get_all_sessions_summary_for($user->id); - $result->info = format_float($usersummary->takensessionspoints, 1, true, true) . ' / ' . - format_float($usersummary->allsessionsmaxpoints, 1, true, true); + $result->info = $usersummary->pointsallsessions; } return $result; diff --git a/renderer.php b/renderer.php index 7e21ced..a2d4db1 100644 --- a/renderer.php +++ b/renderer.php @@ -978,12 +978,11 @@ class mod_attendance_renderer extends plugin_renderer_base { $usersummary = $userdata->summary[$ca->attid]->get_all_sessions_summary_for($userdata->user->id); $row->cells[] = $usersummary->numtakensessions; - $row->cells[] = format_float($usersummary->takensessionspoints, 1, true, true) . ' / ' . - format_float($usersummary->takensessionsmaxpoints, 1, true, true); + $row->cells[] = $usersummary->pointssessionscompleted; if (empty($usersummary->numtakensessions)) { $row->cells[] = '-'; } else { - $row->cells[] = format_float($usersummary->takensessionspercentage * 100) . '%'; + $row->cells[] = $usersummary->percentagesessionscompleted; } } @@ -1438,24 +1437,20 @@ class mod_attendance_renderer extends plugin_renderer_base { $contrast = $startwithcontrast; $row = new html_table_row(); $row->cells[] = $this->build_data_cell($usersummary->numtakensessions, $contrast); - $text = format_float($usersummary->takensessionspoints, 1, true, true) . ' / ' . - format_float($usersummary->takensessionsmaxpoints, 1, true, true); - $row->cells[] = $this->build_data_cell($text, $contrast); + $row->cells[] = $this->build_data_cell($usersummary->pointssessionscompleted, $contrast); $row->cells[] = $this->build_data_cell(format_float($usersummary->takensessionspercentage * 100) . '%', $contrast); if ($reportdata->pageparams->view == ATT_VIEW_SUMMARY) { $contrast = !$contrast; $row->cells[] = $this->build_data_cell($usersummary->numallsessions, $contrast); - $text = format_float($usersummary->takensessionspoints, 1, true, true) . ' / ' . - format_float($usersummary->allsessionsmaxpoints, 1, true, true); + $text = $usersummary->pointsallsessions; $row->cells[] = $this->build_data_cell($text, $contrast); - $row->cells[] = $this->build_data_cell(format_float($usersummary->allsessionspercentage * 100) . '%', $contrast); + $row->cells[] = $this->build_data_cell($usersummary->allsessionspercentage, $contrast); $contrast = !$contrast; - $text = format_float($usersummary->maxpossiblepoints, 1, true, true) . ' / ' . - format_float($usersummary->allsessionsmaxpoints, 1, true, true); + $text = $usersummary->maxpossiblepoints; $row->cells[] = $this->build_data_cell($text, $contrast); - $row->cells[] = $this->build_data_cell(format_float($usersummary->maxpossiblepercentage * 100) . '%', $contrast); + $row->cells[] = $this->build_data_cell($usersummary->maxpossiblepercentage, $contrast); } $rows[] = $row; diff --git a/renderhelpers.php b/renderhelpers.php index 71f4f9c..a851218 100644 --- a/renderhelpers.php +++ b/renderhelpers.php @@ -394,14 +394,13 @@ function construct_user_data_stat($usersummary, $view) { $row = new html_table_row(); $row->attributes['class'] = 'normal'; $row->cells[] = get_string('pointssessionscompleted', 'attendance') . ':'; - $row->cells[] = format_float($usersummary->takensessionspoints, 1, true, true) . ' / ' . - format_float($usersummary->takensessionsmaxpoints, 1, true, true); + $row->cells[] = $usersummary->pointssessionscompleted; $stattable->data[] = $row; $row = new html_table_row(); $row->attributes['class'] = 'normal'; $row->cells[] = get_string('percentagesessionscompleted', 'attendance') . ':'; - $row->cells[] = format_float($usersummary->takensessionspercentage * 100) . '%'; + $row->cells[] = $usersummary->percentagesessionscompleted; $stattable->data[] = $row; if ($view == ATT_VIEW_ALL) { @@ -414,27 +413,25 @@ function construct_user_data_stat($usersummary, $view) { $row = new html_table_row(); $row->attributes['class'] = 'highlight'; $row->cells[] = get_string('pointsallsessions', 'attendance') . ':'; - $row->cells[] = format_float($usersummary->takensessionspoints, 1, true, true) . ' / ' . - format_float($usersummary->allsessionsmaxpoints, 1, true, true); + $row->cells[] = $usersummary->pointsallsessions; $stattable->data[] = $row; $row = new html_table_row(); $row->attributes['class'] = 'highlight'; $row->cells[] = get_string('percentageallsessions', 'attendance') . ':'; - $row->cells[] = format_float($usersummary->allsessionspercentage * 100) . '%'; + $row->cells[] = $usersummary->allsessionspercentage; $stattable->data[] = $row; $row = new html_table_row(); $row->attributes['class'] = 'normal'; $row->cells[] = get_string('maxpossiblepoints', 'attendance') . ':'; - $row->cells[] = format_float($usersummary->maxpossiblepoints, 1, true, true) . ' / ' . - format_float($usersummary->allsessionsmaxpoints, 1, true, true); + $row->cells[] = $usersummary->maxpossiblepoints; $stattable->data[] = $row; $row = new html_table_row(); $row->attributes['class'] = 'normal'; $row->cells[] = get_string('maxpossiblepercentage', 'attendance') . ':'; - $row->cells[] = format_float($usersummary->maxpossiblepercentage * 100) . '%'; + $row->cells[] = $usersummary->maxpossiblepercentage; $stattable->data[] = $row; } diff --git a/templates/mobile_view_page.mustache b/templates/mobile_view_page.mustache new file mode 100644 index 0000000..6f919a8 --- /dev/null +++ b/templates/mobile_view_page.mustache @@ -0,0 +1,74 @@ +{{=<% %>=}} +
+ + + + + + {{ 'plugin.mod_attendance.sessionscompleted' | translate }} + + + <% summary.numtakensessions %> + + + + + {{ 'plugin.mod_attendance.pointssessionscompleted' | translate }} + + + <% summary.pointssessionscompleted %> + + + + + {{ 'plugin.mod_attendance.percentagesessionscompleted' | translate }} + + + <% summary.percentagesessionscompleted %> + + + + + + {{ 'plugin.mod_attendance.sessionstotal' | translate }} + + + <% summary.numallsessions %> + + + + + {{ 'plugin.mod_attendance.pointsallsessions' | translate }} + + + <% summary.percentagesessionscompleted %> + + + + + {{ 'plugin.mod_attendance.percentageallsessions' | translate }} + + + <% summary.allsessionspercentage %> + + + + + {{ 'plugin.mod_attendance.maxpossiblepoints' | translate }} + + + <% summary.maxpossiblepoints %> + + + + + {{ 'plugin.mod_attendance.maxpossiblepercentage' | translate }} + + + <% summary.maxpossiblepercentage %> + + + + + +
\ No newline at end of file