Improve auto-marking when no password.
authorDan Marsden <dan@danmarsden.com>
Thu, 7 Dec 2017 21:51:39 +0000 (10:51 +1300)
committerDan Marsden <dan@danmarsden.com>
Thu, 7 Dec 2017 22:13:56 +0000 (11:13 +1300)
also fix some coding guideline issues.

add_form.php
attendance.php
lang/en/attendance.php
locallib.php
renderhelpers.php
sessions.php
student_attendance_form.php
update_form.php

index 8bb0fd0..fff97c9 100644 (file)
@@ -211,8 +211,11 @@ class mod_attendance_add_form extends moodleform {
             $mform->disabledif('studentpassword', 'randompassword', 'checked');
             $mform->disabledif('studentpassword', 'automark', 'eq', ATTENDANCE_AUTOMARK_ALL);
             $mform->disabledif('randompassword', 'automark', 'eq', ATTENDANCE_AUTOMARK_ALL);
+
             $mform->addElement('checkbox', 'autoassignstatus', '', get_string('autoassignstatus', 'attendance'));
             $mform->addHelpButton('autoassignstatus', 'autoassignstatus', 'attendance');
+            $mform->disabledif('autoassignstatus', 'studentscanmark', 'notchecked');
+
             if (isset($pluginconfig->autoassignstatus)) {
                 $mform->setDefault('autoassignstatus', $pluginconfig->autoassignstatus);
             }
index 06a2756..1818972 100644 (file)
@@ -57,6 +57,26 @@ $att = new mod_attendance_structure($attendance, $cm, $course, $PAGE->context, $
 // Require that a session key is passed to this page.
 require_sesskey();
 
+// Check to see if autoassignstatus is in use and no password required.
+if ($attforsession->autoassignstatus && empty($attforsession->studentpassword)) {
+    $statusid = attendance_session_get_highest_status($att, $attforsession);
+    $url = new moodle_url('/mod/attendance/view.php', array('id' => $cm->id));
+    if (empty($statusid)) {
+        print_error('attendance_no_status', 'mod_attendance', $url);
+    }
+    $take = new stdClass();
+    $take->status = $statusid;
+    $take->sessid = $attforsession->id;
+    $success = $att->take_from_student($take);
+
+    if ($success) {
+        // Redirect back to the view page.
+        redirect($url, get_string('studentmarked', 'attendance'));
+    } else {
+        print_error('attendance_already_submitted', 'mod_attendance', $url);
+    }
+}
+
 // Create the form.
 $mform = new mod_attendance_student_attendance_form(null,
         array('course' => $course, 'cm' => $cm, 'modcontext' => $PAGE->context, 'session' => $attforsession, 'attendance' => $att));
@@ -76,29 +96,11 @@ if ($mform->is_cancelled()) {
         redirect($url, get_string('incorrectpassword', 'mod_attendance'), null, \core\output\notification::NOTIFY_ERROR);
     }
     if ($attforsession->autoassignstatus) {
-        // Find the status to set here.
-        $statuses = $att->get_statuses();
-        $highestavailablegrade = 0;
-        $highestavailablestatus;
-        foreach ($statuses as $status) {
-            if ($status->studentavailability === '0') {
-                // This status is never available to students.
-                continue;
-            }
-            if (!empty($status->studentavailability)) {
-                $toolateforstatus = (($attforsession->sessdate + ($status->studentavailability * 60)) < time());
-                if ($toolateforstatus) {
-                    continue;
-                }
-            }
-            // This status is available to the student.
-            if ($status->grade > $highestavailablegrade) {
-                // This is the most favourable grade so far; save it.
-                $highestavailablegrade = $status->grade;
-                $highestavailablestatus = $status;
-            }
+        $fromform->status = attendance_session_get_highest_status($att, $attforsession);
+        if (empty($fromform->status)) {
+            $url = new moodle_url('/mod/attendance/view.php', array('id' => $cm->id));
+            print_error('attendance_no_status', 'mod_attendance', $url);
         }
-        $fromform->status = $highestavailablestatus->id;
     }
 
     if (!empty($fromform->status)) {
@@ -106,8 +108,8 @@ if ($mform->is_cancelled()) {
 
         $url = new moodle_url('/mod/attendance/view.php', array('id' => $cm->id));
         if ($success) {
-            // Redirect back to the view page for the block.
-            redirect($url);
+            // Redirect back to the view page.
+            redirect($url, get_string('studentmarked', 'attendance'));
         } else {
             print_error('attendance_already_submitted', 'mod_attendance', $url);
         }
@@ -126,3 +128,4 @@ $output = $PAGE->get_renderer('mod_attendance');
 echo $output->header();
 $mform->display();
 echo $output->footer();
+
index 97663b3..f9e0370 100644 (file)
@@ -501,4 +501,6 @@ $string['week'] = 'week(s)';
 $string['weeks'] = 'Weeks';
 $string['youcantdo'] = 'You can\'t do anything';
 $string['includeabsentee'] = 'Include session when calculating absentee report';
-$string['includeabsentee_help'] = 'If checked this session will be included in the absentee report calculations.';
\ No newline at end of file
+$string['includeabsentee_help'] = 'If checked this session will be included in the absentee report calculations.';
+$string['attendance_no_status'] = 'No valid status was available - you may be too late to record attendance.';
+$string['studentmarked'] = 'Your attendance in this session has been recorded.';
\ No newline at end of file
index dc4e1bc..b364d86 100644 (file)
@@ -539,9 +539,6 @@ function attendance_construct_sessions_data_for_add($formdata, mod_attendance_st
     if (empty(get_config('attendance', 'studentscanmark'))) {
         $formdata->studentscanmark = 0;
     }
-    if (empty(get_config('attendance', 'autoassignstatus'))) {
-        $formdata->autoassignstatus = 0;
-    }
 
     $sessions = array();
     if (isset($formdata->addmultiply)) {
@@ -833,4 +830,40 @@ function attendance_template_variables($record) {
         $record->$field = preg_replace($patterns, $replacements, $record->$field);
     }
     return $record;
+}
+
+/**
+ * Find highest available status for a user.
+ *
+ * @param mod_attendance_structure $att attendance structure
+ * @param stdclass $attforsession attendance_session record.
+ * @return bool/int
+ */
+function attendance_session_get_highest_status(mod_attendance_structure $att, $attforsession) {
+    // Find the status to set here.
+    $statuses = $att->get_statuses();
+    $highestavailablegrade = 0;
+    $highestavailablestatus = new stdClass();
+    foreach ($statuses as $status) {
+        if ($status->studentavailability === '0') {
+            // This status is never available to students.
+            continue;
+        }
+        if (!empty($status->studentavailability)) {
+            $toolateforstatus = (($attforsession->sessdate + ($status->studentavailability * 60)) < time());
+            if ($toolateforstatus) {
+                continue;
+            }
+        }
+        // This status is available to the student.
+        if ($status->grade > $highestavailablegrade) {
+            // This is the most favourable grade so far; save it.
+            $highestavailablegrade = $status->grade;
+            $highestavailablestatus = $status;
+        }
+    }
+    if (empty($highestavailablestatus)) {
+        return false;
+    }
+    return $highestavailablestatus->id;
 }
\ No newline at end of file
index 216d459..71f4f9c 100644 (file)
@@ -327,7 +327,7 @@ function attendance_strftimehm($time) {
     // Some Lang packs use %p to suffix with AM/PM but not all strftime support this.
     // Check if %p is in use and make sure it's being respected.
     if (stripos($format, '%p')) {
-        // Check if $userdate did something with %p by checking userdate against the same format without %p
+        // Check if $userdate did something with %p by checking userdate against the same format without %p.
         $formatwithoutp = str_ireplace('%p', '', $format);
         if (userdate($time, $formatwithoutp) == $userdate) {
             // The date is the same with and without %p - we have a problem.
index be68826..18ebfb0 100644 (file)
@@ -99,7 +99,6 @@ switch ($att->pageparams->action) {
         }
 
         if ($formdata = $mform->get_data()) {
-            error_log(var_export($formdata, true));
             if (empty($formdata->autoassignstatus)) {
                 $formdata->autoassignstatus = 0;
             }
index 86676fe..210ef74 100644 (file)
@@ -93,7 +93,7 @@ class mod_attendance_student_attendance_form extends moodleform {
             if ($disabledduetotime) {
                 $warning = html_writer::span(get_string('somedisabledstatus', 'attendance'), 'somedisabledstatus');
                 $radioarray[] =& $mform->createElement('static', '', '', $warning);
-             }
+            }
             // Add the radio buttons as a control with the user's name in front.
             $radiogroup = $mform->addGroup($radioarray, 'statusarray', $USER->firstname.' '.$USER->lastname.':', array(''), false);
             $radiogroup->setAttributes(array('class' => 'statusgroup'));
index e445522..a2ab016 100644 (file)
@@ -130,6 +130,8 @@ class mod_attendance_update_form extends moodleform {
             $mform->disabledif('randompassword', 'automark', 'eq', ATTENDANCE_AUTOMARK_ALL);
             $mform->addElement('checkbox', 'autoassignstatus', '', get_string('autoassignstatus', 'attendance'));
             $mform->addHelpButton('autoassignstatus', 'autoassignstatus', 'attendance');
+            $mform->disabledif('autoassignstatus', 'studentscanmark', 'notchecked');
+
             $mgroup = array();
             $mgroup[] = & $mform->createElement('text', 'subnet', get_string('requiresubnet', 'attendance'));
             $mform->setDefault('subnet', $this->_customdata['att']->subnet);