diff --git a/classes/output/mobile.php b/classes/output/mobile.php index 033b3fd..e788d54 100644 --- a/classes/output/mobile.php +++ b/classes/output/mobile.php @@ -46,6 +46,8 @@ class mobile { $args = (object) $args; $cmid = $args->cmid; $courseid = $args->courseid; + $takenstatus = empty($args->status) ? '' : $args->status; + $sessid = $args->sessid; $groupid = empty($args->group) ? 0 : $args->group; // By default, group 0. // Capabilities check. @@ -64,7 +66,7 @@ class mobile { $data['courseid'] = $courseid; $data['attendance'] = $attendance; - $data['attendancefunction'] = 'mobile_take_attendance'; + $data['attendancefunction'] = 'mobile_user_form'; $isteacher = false; if (has_capability('mod/attendance:takeattendances', $context)) { $isteacher = true; @@ -73,17 +75,15 @@ class mobile { // Add stats for this use to output. $pageparams = new \mod_attendance_view_page_params(); - $pageparams->studentid = $USER->id; + if (!empty($takenstatus) && !empty($sessid)) { + $pageparams->sessionid = $sessid; + } $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['summary'] = $summary->get_all_sessions_summary_for($USER->id); - // Get list of sessions within the next 24hrs and in last 6hrs. // TODO: provide way of adjusting which sessions to show in app. $time = time() - (6 * 60 * 60); @@ -104,9 +104,9 @@ class mobile { // This session isn't viewable to this student - probably a group session. continue; } - list($canmark, $unused) = attendance_can_student_mark($sess, false); - if ($isteacher || $canmark) { + list($canmark, $reason) = attendance_can_student_mark($sess, false); + if ($isteacher || $canmark) { $html = array('time' => attendance_construct_session_time($sess->sessdate, $sess->duration)); if (!empty($sess->groupid)) { // 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.')'; } + // Check if Status already recorded. if (!$isteacher && !empty($userdata->sessionslog['c'.$sess->id]->statusid)) { $html['currentstatus'] = $userdata->statuses[$userdata->sessionslog['c'.$sess->id]->statusid]->description; } else { + // Status has not been recorded - If student, check auto-assign and form data. $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; @@ -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 [ 'templates' => [ [ @@ -142,7 +197,7 @@ class mobile { * @param array $args Arguments from tool_mobile_get_content WS * @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; require_once($CFG->dirroot.'/mod/attendance/locallib.php'); @@ -151,7 +206,6 @@ class mobile { $cmid = $args->cmid; $courseid = $args->courseid; $sessid = $args->sessid; - $takenstatus = empty($args->status) ? '' : $args->status; // Capabilities check. $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['showstatuses'] = false; // Hide all statuses. } else if ($attforsession->autoassignstatus && empty($attforsession->studentpassword)) { - $statusid = attendance_session_get_highest_status($att, $attforsession); - if (empty($statusid)) { - $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. - } + // This shouldn't happen as the main function should handle this scenario. + // Hide all status just in case the user manages to hit this page accidentally. + $data['showstatuses'] = false; // Hide all statuses. } else { // Show user form for submitting a status. $statuses = $att->get_statuses(); @@ -224,26 +266,6 @@ class mobile { if (empty($data['statuses'])) { $data['messages'][]['string'] = 'attendance_no_status'; $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'])) { @@ -254,7 +276,8 @@ class mobile { 'templates' => [ [ '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' => '', diff --git a/templates/mobile_take_attendance.mustache b/templates/mobile_user_form.mustache similarity index 91% rename from templates/mobile_take_attendance.mustache rename to templates/mobile_user_form.mustache index 336c108..86f6ee5 100644 --- a/templates/mobile_take_attendance.mustache +++ b/templates/mobile_user_form.mustache @@ -15,7 +15,7 @@ along with Moodle. If not, see . }} {{! - @template mod_attendance/mobile_take_attendance + @template mod_attendance/mobile_user_form The page to take attendance @@ -60,7 +60,7 @@ <%/statuses%> - <%/showstatuses%> diff --git a/templates/mobile_view_page.mustache b/templates/mobile_view_page.mustache index c3d5fe2..1e4a0dd 100644 --- a/templates/mobile_view_page.mustache +++ b/templates/mobile_view_page.mustache @@ -49,6 +49,13 @@ {{=<% %>=}}
+ <%#showmessage%> + <%#messages%> + + {{ 'plugin.mod_attendance.<% string %>' | translate }} + + <%/messages%> + <%/showmessage%> <%#sessions%>

<% time %>