Browse Source

Re-organise form handling so to avoid cache issues and improve UX

MOODLE_35_STABLE
Dan Marsden 6 years ago
parent
commit
38d5e8cee2
  1. 113
      classes/output/mobile.php
  2. 4
      templates/mobile_user_form.mustache
  3. 7
      templates/mobile_view_page.mustache

113
classes/output/mobile.php

@ -46,6 +46,8 @@ class mobile {
$args = (object) $args; $args = (object) $args;
$cmid = $args->cmid; $cmid = $args->cmid;
$courseid = $args->courseid; $courseid = $args->courseid;
$takenstatus = empty($args->status) ? '' : $args->status;
$sessid = $args->sessid;
$groupid = empty($args->group) ? 0 : $args->group; // By default, group 0. $groupid = empty($args->group) ? 0 : $args->group; // By default, group 0.
// Capabilities check. // Capabilities check.
@ -64,7 +66,7 @@ class mobile {
$data['courseid'] = $courseid; $data['courseid'] = $courseid;
$data['attendance'] = $attendance; $data['attendance'] = $attendance;
$data['attendancefunction'] = 'mobile_take_attendance'; $data['attendancefunction'] = 'mobile_user_form';
$isteacher = false; $isteacher = false;
if (has_capability('mod/attendance:takeattendances', $context)) { if (has_capability('mod/attendance:takeattendances', $context)) {
$isteacher = true; $isteacher = true;
@ -73,17 +75,15 @@ class mobile {
// Add stats for this use to output. // Add stats for this use to output.
$pageparams = new \mod_attendance_view_page_params(); $pageparams = new \mod_attendance_view_page_params();
$pageparams->studentid = $USER->id; $pageparams->studentid = $USER->id;
if (!empty($takenstatus) && !empty($sessid)) {
$pageparams->sessionid = $sessid;
}
$pageparams->mode = \mod_attendance_view_page_params::MODE_THIS_COURSE; $pageparams->mode = \mod_attendance_view_page_params::MODE_THIS_COURSE;
$pageparams->view = 5; // Show all sessions for this course? $pageparams->view = 5; // Show all sessions for this course?
$att = new \mod_attendance_structure($attendance, $cm, $course, $context, $pageparams); $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['summary'] = $summary->get_all_sessions_summary_for($USER->id);
// Get list of sessions within the next 24hrs and in last 6hrs. // Get list of sessions within the next 24hrs and in last 6hrs.
// TODO: provide way of adjusting which sessions to show in app. // TODO: provide way of adjusting which sessions to show in app.
$time = time() - (6 * 60 * 60); $time = time() - (6 * 60 * 60);
@ -104,9 +104,9 @@ class mobile {
// This session isn't viewable to this student - probably a group session. // This session isn't viewable to this student - probably a group session.
continue; continue;
} }
list($canmark, $unused) = attendance_can_student_mark($sess, false); list($canmark, $reason) = attendance_can_student_mark($sess, false);
if ($isteacher || $canmark) {
if ($isteacher || $canmark) {
$html = array('time' => attendance_construct_session_time($sess->sessdate, $sess->duration)); $html = array('time' => attendance_construct_session_time($sess->sessdate, $sess->duration));
if (!empty($sess->groupid)) { if (!empty($sess->groupid)) {
// TODO In-efficient way to get group name - we should get all groups in one query. // TODO In-efficient way to get group name - we should get all groups in one query.
@ -114,10 +114,61 @@ class mobile {
$html['time'] .= ' ('.$groupname.')'; $html['time'] .= ' ('.$groupname.')';
} }
// Check if Status already recorded.
if (!$isteacher && !empty($userdata->sessionslog['c'.$sess->id]->statusid)) { if (!$isteacher && !empty($userdata->sessionslog['c'.$sess->id]->statusid)) {
$html['currentstatus'] = $userdata->statuses[$userdata->sessionslog['c'.$sess->id]->statusid]->description; $html['currentstatus'] = $userdata->statuses[$userdata->sessionslog['c'.$sess->id]->statusid]->description;
} else { } else {
// Status has not been recorded - If student, check auto-assign and form data.
$html['sessid'] = $sess->id; $html['sessid'] = $sess->id;
if (!$isteacher) {
if (!empty($sess->subnet) && !address_in_subnet(getremoteaddr(), $sess->subnet)) {
$data['messages'][]['string'] = 'subnetwrong'; // Lang string to show as a message.
$html['sessid'] = null; // Unset sessid as we cannot record session on this ip.
} else if ($sess->autoassignstatus && empty($sess->studentpassword)) {
$statusid = attendance_session_get_highest_status($att, $sess);
if (empty($statusid)) {
$data['messages'][]['string'] = 'attendance_no_status';
}
$take = new \stdClass();
$take->status = $statusid;
$take->sessid = $sess->id;
$success = $att->take_from_student($take);
if ($success) {
$html['currentstatus'] = $userdata->statuses[$statusid]->description;
$html['sessid'] = null; // Unset sessid as we have recorded session.
}
} else if (!empty($takenstatus)) {
$statuses = $att->get_statuses();
// Check if user has access to all statuses.
foreach ($statuses as $status) {
if ($status->studentavailability === '0') {
unset($statuses[$status->id]);
continue;
}
if (!empty($status->studentavailability) &&
time() > $sess->sessdate + ($status->studentavailability * 60)) {
unset($statuses[$status->id]);
continue;
}
}
if (empty($statuses[$takenstatus])) {
// This status has probably expired and is not available - they need to choose a new one.
$data['messages'][]['string'] = 'invalidstatus';
} else {
$take = new \stdClass();
$take->status = $takenstatus;
$take->sessid = $sess->id;
$success = $att->take_from_student($take);
if ($success) {
$html['currentstatus'] = $userdata->statuses[$takenstatus]->description;
$html['sessid'] = null; // Unset sessid as we have recorded session.
}
}
}
}
} }
$data['sessions'][] = $html; $data['sessions'][] = $html;
@ -125,6 +176,10 @@ class mobile {
} }
} }
$summary = new \mod_attendance_summary($att->id, array($USER->id), $att->pageparams->startdate,
$att->pageparams->enddate);
$data['summary'] = $summary->get_all_sessions_summary_for($USER->id);
return [ return [
'templates' => [ 'templates' => [
[ [
@ -142,7 +197,7 @@ class mobile {
* @param array $args Arguments from tool_mobile_get_content WS * @param array $args Arguments from tool_mobile_get_content WS
* @return array HTML, javascript and other data * @return array HTML, javascript and other data
*/ */
public static function mobile_take_attendance($args) { public static function mobile_user_form($args) {
global $OUTPUT, $DB, $CFG; global $OUTPUT, $DB, $CFG;
require_once($CFG->dirroot.'/mod/attendance/locallib.php'); require_once($CFG->dirroot.'/mod/attendance/locallib.php');
@ -151,7 +206,6 @@ class mobile {
$cmid = $args->cmid; $cmid = $args->cmid;
$courseid = $args->courseid; $courseid = $args->courseid;
$sessid = $args->sessid; $sessid = $args->sessid;
$takenstatus = empty($args->status) ? '' : $args->status;
// Capabilities check. // Capabilities check.
$cm = get_coursemodule_from_id('attendance', $cmid); $cm = get_coursemodule_from_id('attendance', $cmid);
@ -189,21 +243,9 @@ class mobile {
$data['messages'][]['string'] = 'subnetwrong'; // Lang string to show as a message. $data['messages'][]['string'] = 'subnetwrong'; // Lang string to show as a message.
$data['showstatuses'] = false; // Hide all statuses. $data['showstatuses'] = false; // Hide all statuses.
} else if ($attforsession->autoassignstatus && empty($attforsession->studentpassword)) { } else if ($attforsession->autoassignstatus && empty($attforsession->studentpassword)) {
$statusid = attendance_session_get_highest_status($att, $attforsession); // This shouldn't happen as the main function should handle this scenario.
if (empty($statusid)) { // Hide all status just in case the user manages to hit this page accidentally.
$data['messages'][]['string'] = 'attendance_no_status';
}
$take = new \stdClass();
$take->status = $statusid;
$take->sessid = $attforsession->id;
$success = $att->take_from_student($take);
if ($success) {
$data['messages'][]['string'] = 'attendancesuccess'; // Lang string to show as a message.
} else {
$data['messages'][]['string'] = 'attendance_already_submitted'; // Lang string to show as a message.
$data['showstatuses'] = false; // Hide all statuses. $data['showstatuses'] = false; // Hide all statuses.
}
} else { } else {
// Show user form for submitting a status. // Show user form for submitting a status.
$statuses = $att->get_statuses(); $statuses = $att->get_statuses();
@ -224,26 +266,6 @@ class mobile {
if (empty($data['statuses'])) { if (empty($data['statuses'])) {
$data['messages'][]['string'] = 'attendance_no_status'; $data['messages'][]['string'] = 'attendance_no_status';
$data['showstatuses'] = false; // Hide all statuses. $data['showstatuses'] = false; // Hide all statuses.
} else if (!empty($takenstatus)) {
// Check if user has submitted a status.
if (empty($statuses[$takenstatus])) {
// This status has probably expired and is not available - they need to choose a new one.
$data['messages'][]['string'] = 'invalidstatus';
} else {
$take = new \stdClass();
$take->status = $takenstatus;
$take->sessid = $attforsession->id;
$success = $att->take_from_student($take);
if ($success) {
$data['messages'][]['string'] = 'attendancesuccess'; // Lang string to show as a message.
$data['showstatuses'] = false; // Hide all statuses.
} else {
$data['messages'][]['string'] = 'attendance_already_submitted'; // Lang string to show as a message.
$data['showstatuses'] = false; // Hide all statuses.
}
}
} }
} }
if (!empty($data['messages'])) { if (!empty($data['messages'])) {
@ -254,7 +276,8 @@ class mobile {
'templates' => [ 'templates' => [
[ [
'id' => 'main', 'id' => 'main',
'html' => $OUTPUT->render_from_template('mod_attendance/mobile_take_attendance', $data), 'html' => $OUTPUT->render_from_template('mod_attendance/mobile_user_form', $data),
'cache-view' => false
], ],
], ],
'javascript' => '', 'javascript' => '',

4
templates/mobile_take_attendance.mustache → templates/mobile_user_form.mustache

@ -15,7 +15,7 @@
along with Moodle. If not, see <http://www.gnu.org/licenses/>. along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}} }}
{{! {{!
@template mod_attendance/mobile_take_attendance @template mod_attendance/mobile_user_form
The page to take attendance The page to take attendance
@ -60,7 +60,7 @@
</ion-item> </ion-item>
<%/statuses%> <%/statuses%>
</ion-list> </ion-list>
<button ion-button core-site-plugins-new-content component="mod_attendance" method="mobile_take_attendance" [args]="{cmid: <% cmid %>, courseid: <% courseid %>, sessid: <% sessid %>, status: status}"> <button ion-button core-site-plugins-new-content component="mod_attendance" method="mobile_view_activity" [args]="{cmid: <% cmid %>, courseid: <% courseid %>, sessid: <% sessid %>, status: status}">
{{ 'plugin.mod_attendance.submitattendance' | translate }} {{ 'plugin.mod_attendance.submitattendance' | translate }}
</button> </button>
<%/showstatuses%> <%/showstatuses%>

7
templates/mobile_view_page.mustache

@ -49,6 +49,13 @@
{{=<% %>=}} {{=<% %>=}}
<div> <div>
<core-course-module-description description="<% attendance.intro %>" component="mod_attendance" componentId="<% cmid %>"></core-course-module-description> <core-course-module-description description="<% attendance.intro %>" component="mod_attendance" componentId="<% cmid %>"></core-course-module-description>
<%#showmessage%>
<%#messages%>
<ion-item>
{{ 'plugin.mod_attendance.<% string %>' | translate }}
</ion-item>
<%/messages%>
<%/showmessage%>
<%#sessions%> <%#sessions%>
<ion-item> <ion-item>
<h2><% time %></h2> <h2><% time %></h2>

Loading…
Cancel
Save