diff --git a/add_form.php b/add_form.php index 791f7c4..b6eb35e 100644 --- a/add_form.php +++ b/add_form.php @@ -211,6 +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'); + if (isset($pluginconfig->autoassignstatus)) { + $mform->setDefault('autoassignstatus', $pluginconfig->autoassignstatus); + } if (isset($pluginconfig->studentscanmark_default)) { $mform->setDefault('studentscanmark', $pluginconfig->studentscanmark_default); } @@ -244,6 +249,9 @@ class mod_attendance_add_form extends moodleform { $mform->settype('studentscanmark', PARAM_INT); $mform->addElement('hidden', 'automark', '0'); $mform->setType('automark', PARAM_INT); + $mform->addElement('hidden', 'autoassignstatus', '0'); + $mform->setType('autoassignstatus', PARAM_INT); + $mform->addElement('hidden', 'subnet', ''); $mform->setType('subnet', PARAM_TEXT); } diff --git a/attendance.php b/attendance.php index c6290da..06a2756 100644 --- a/attendance.php +++ b/attendance.php @@ -75,6 +75,31 @@ if ($mform->is_cancelled()) { $url = new moodle_url('/mod/attendance/attendance.php', array('sessid' => $id, 'sesskey' => sesskey())); 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 = $highestavailablestatus->id; + } if (!empty($fromform->status)) { $success = $att->take_from_student($fromform); @@ -84,7 +109,7 @@ if ($mform->is_cancelled()) { // Redirect back to the view page for the block. redirect($url); } else { - print_error ('attendance_already_submitted', 'mod_attendance', $url); + print_error('attendance_already_submitted', 'mod_attendance', $url); } } diff --git a/backup/moodle2/backup_attendance_stepslib.php b/backup/moodle2/backup_attendance_stepslib.php index f27d83c..a861aec 100644 --- a/backup/moodle2/backup_attendance_stepslib.php +++ b/backup/moodle2/backup_attendance_stepslib.php @@ -56,8 +56,8 @@ class backup_attendance_activity_structure_step extends backup_activity_structur $sessions = new backup_nested_element('sessions'); $session = new backup_nested_element('session', array('id'), array( - 'groupid', 'sessdate', 'duration', 'lasttaken', 'lasttakenby', - 'timemodified', 'description', 'descriptionformat', 'studentscanmark', 'studentpassword', + 'groupid', 'sessdate', 'duration', 'lasttaken', 'lasttakenby', 'timemodified', + 'description', 'descriptionformat', 'studentscanmark', 'studentpassword', 'autoassignstatus', 'subnet', 'automark', 'automarkcompleted', 'statusset', 'absenteereport', 'caleventid')); // XML nodes declaration - user data. diff --git a/classes/structure.php b/classes/structure.php index e5e3d57..97d1b93 100644 --- a/classes/structure.php +++ b/classes/structure.php @@ -477,6 +477,9 @@ class mod_attendance_structure { if (!isset($sess->studentscanmark)) { $sess->studentscanmark = 0; } + if (!isset($sess->autoassignstatus)) { + $sess->autoassignstatus = 0; + } if (!isset($sess->studentpassword)) { $sess->studentpassword = ''; } @@ -515,15 +518,19 @@ class mod_attendance_structure { $sess->descriptionformat = $formdata->sdescription['format']; $sess->studentscanmark = 0; + $sess->autoassignstatus = 0; $sess->studentpassword = ''; $sess->subnet = ''; $sess->automark = 0; $sess->automarkcompleted = 0; - + if (!empty($formdata->autoassignstatus)) { + $sess->autoassignstatus = $formdata->autoassignstatus; + } if (!empty(get_config('attendance', 'studentscanmark')) && !empty($formdata->studentscanmark)) { $sess->studentscanmark = $formdata->studentscanmark; $sess->studentpassword = $formdata->studentpassword; + $sess->autoassignstatus = $formdata->autoassignstatus; if (!empty($formdata->usedefaultsubnet)) { $sess->subnet = $this->subnet; } else { @@ -537,6 +544,7 @@ class mod_attendance_structure { $sess->timemodified = time(); $DB->update_record('attendance_sessions', $sess); + if (empty($sess->caleventid)) { // This shouldn't really happen, but just in case to prevent fatal error. attendance_create_calendar_event($sess); @@ -1058,7 +1066,7 @@ class mod_attendance_structure { $id = $DB->sql_concat(':value', 'ats.id'); if ($this->get_group_mode()) { $sql = "SELECT $id, ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, - al.statusid, al.remarks, ats.studentscanmark + al.statusid, al.remarks, ats.studentscanmark, ats.autoassignstatus FROM {attendance_sessions} ats RIGHT JOIN {attendance_log} al ON ats.id = al.sessionid AND al.studentid = :uid @@ -1067,7 +1075,7 @@ class mod_attendance_structure { ORDER BY ats.sessdate ASC"; } else { $sql = "SELECT $id, ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, ats.statusset, - al.statusid, al.remarks, ats.studentscanmark + al.statusid, al.remarks, ats.studentscanmark, ats.autoassignstatus FROM {attendance_sessions} ats RIGHT JOIN {attendance_log} al ON ats.id = al.sessionid AND al.studentid = :uid @@ -1096,9 +1104,9 @@ class mod_attendance_structure { } else { $where = "ats.attendanceid = :aid AND ats.sessdate >= :csdate AND ats.groupid $gsql"; } - + //James $sql = "SELECT $id, ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, ats.statusset, - al.statusid, al.remarks, ats.studentscanmark + al.statusid, al.remarks, ats.studentscanmark, ats.autoassignstatus FROM {attendance_sessions} ats LEFT JOIN {attendance_log} al ON ats.id = al.sessionid AND al.studentid = :uid diff --git a/db/install.xml b/db/install.xml index 910eef3..8734fc1 100644 --- a/db/install.xml +++ b/db/install.xml @@ -37,6 +37,7 @@ + diff --git a/db/upgrade.php b/db/upgrade.php index ee0f872..9ba51a1 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -453,5 +453,17 @@ function xmldb_attendance_upgrade($oldversion=0) { upgrade_mod_savepoint(true, 2017050219, 'attendance'); } + if ($oldversion < 2017050220) { + $table = new xmldb_table('attendance_sessions'); + + $field = new xmldb_field('autoassignstatus'); + $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'studentscanmark'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + upgrade_mod_savepoint(true, 2017050220, 'attendance'); + } + return $result; } diff --git a/externallib.php b/externallib.php index 9d016ac..0c753e3 100644 --- a/externallib.php +++ b/externallib.php @@ -69,6 +69,7 @@ class mod_wsattendance_external extends external_api { 'descriptionformat' => new external_value(PARAM_INT, 'Session description format.'), 'studentscanmark' => new external_value(PARAM_INT, 'Students can mark their own presence.'), 'absenteereport' => new external_value(PARAM_INT, 'Session included in absetee reports.'), + 'autoassignstatus' => new external_value(PARAM_INT, 'Automatically assign a status to students.'), 'statusset' => new external_value(PARAM_INT, 'Session statusset.')); return $session; diff --git a/lang/en/attendance.php b/lang/en/attendance.php index 98ec2c2..97663b3 100644 --- a/lang/en/attendance.php +++ b/lang/en/attendance.php @@ -108,6 +108,8 @@ The sessions begin on the date of the base session and continue until the \'repe * Repeat every: This allows for a frequency setting. If your class will meet every week, select 1; if it will meet every other week, select 2; every 3rd week, select 3, etc. * Repeat until: Select the last day of class (the last day you want to take attendance). '; +$string['autoassignstatus'] = 'Automatically select highest status available'; +$string['autoassignstatus_help'] = 'If this is selected, students will automatically be assigned the highest available grade.'; $string['createonesession'] = 'Create one session for the course'; $string['csvdelimiter'] = 'CSV delimiter'; $string['date'] = 'Date'; diff --git a/locallib.php b/locallib.php index 8bc98f0..dc4e1bc 100644 --- a/locallib.php +++ b/locallib.php @@ -539,6 +539,9 @@ 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)) { @@ -581,6 +584,9 @@ function attendance_construct_sessions_data_for_add($formdata, mod_attendance_st $sess->subnet = $formdata->subnet; } $sess->automark = $formdata->automark; + if (isset($formdata->autoassignstatus)) { + $sess->autoassignstatus = 1; + } $sess->automarkcompleted = 0; if (!empty($formdata->randompassword)) { $sess->studentpassword = attendance_random_string(); @@ -612,6 +618,7 @@ function attendance_construct_sessions_data_for_add($formdata, mod_attendance_st $sess->descriptionformat = $formdata->sdescription['format']; $sess->timemodified = $now; $sess->studentscanmark = 0; + $sess->autoassignstatus = 0; $sess->subnet = ''; $sess->studentpassword = ''; $sess->automark = 0; @@ -621,6 +628,9 @@ function attendance_construct_sessions_data_for_add($formdata, mod_attendance_st if (isset($formdata->studentscanmark) && !empty($formdata->studentscanmark)) { // Students will be able to mark their own attendance. $sess->studentscanmark = 1; + if (isset($formdata->autoassignstatus) && !empty($formdata->autoassignstatus)) { + $sess->autoassignstatus = 1; + } if (!empty($formdata->randompassword)) { $sess->studentpassword = attendance_random_string(); } else if (!empty($formdata->studentpassword)) { diff --git a/sessions.php b/sessions.php index 73631fc..be68826 100644 --- a/sessions.php +++ b/sessions.php @@ -99,6 +99,10 @@ switch ($att->pageparams->action) { } if ($formdata = $mform->get_data()) { + error_log(var_export($formdata, true)); + if (empty($formdata->autoassignstatus)) { + $formdata->autoassignstatus = 0; + } $att->update_session_from_form_data($formdata, $sessionid); mod_attendance_notifyqueue::notify_success(get_string('sessionupdated', 'attendance')); diff --git a/settings.php b/settings.php index a8acb0f..e169122 100644 --- a/settings.php +++ b/settings.php @@ -118,6 +118,9 @@ if ($ADMIN->fulltree) { $settings->add(new admin_setting_configcheckbox('attendance/randompassword_default', get_string('randompassword', 'attendance'), '', 0)); + $settings->add(new admin_setting_configcheckbox('attendance/autoassignstatus', + get_string('autoassignstatus', 'attendance'), '', 0)); + $name = new lang_string('defaultwarningsettings', 'mod_attendance'); $description = new lang_string('defaultwarningsettings_help', 'mod_attendance'); $settings->add(new admin_setting_heading('defaultwarningsettings', $name, $description)); diff --git a/student_attendance_form.php b/student_attendance_form.php index 61c11bb..86676fe 100644 --- a/student_attendance_form.php +++ b/student_attendance_form.php @@ -82,21 +82,23 @@ class mod_attendance_student_attendance_form extends moodleform { $mform->setType('studentpassword', PARAM_TEXT); $mform->addRule('studentpassword', get_string('passwordrequired', 'attendance'), 'required'); } - // Create radio buttons for setting the attendance status. - $radioarray = array(); - foreach ($statuses as $status) { - $name = html_writer::span($status->description, 'statusdesc'); - $radioarray[] =& $mform->createElement('radio', 'status', '', $name, $status->id, array()); - } - if ($disabledduetotime) { - $warning = html_writer::span(get_string('somedisabledstatus', 'attendance'), 'somedisabledstatus'); - $radioarray[] =& $mform->createElement('static', '', '', $warning); - } + if (!$attforsession->autoassignstatus) { - // 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')); - $mform->addRule('statusarray', get_string('attendancenotset', 'attendance'), 'required', '', 'client', false, false); + // Create radio buttons for setting the attendance status. + $radioarray = array(); + foreach ($statuses as $status) { + $name = html_writer::span($status->description, 'statusdesc'); + $radioarray[] =& $mform->createElement('radio', 'status', '', $name, $status->id, array()); + } + 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')); + $mform->addRule('statusarray', get_string('attendancenotset', 'attendance'), 'required', '', 'client', false, false); + } $this->add_action_buttons(); } @@ -109,9 +111,11 @@ class mod_attendance_student_attendance_form extends moodleform { */ public function validation($data, $files) { $errors = array(); - // Check if this status is allowed to be set. - if (empty($data['status'])) { - $errors['statusarray'] = get_string('invalidstatus', 'attendance'); + if (!($this->_customdata['session']->autoassignstatus)) { + // Check if this status is allowed to be set. + if (empty($data['status'])) { + $errors['statusarray'] = get_string('invalidstatus', 'attendance'); + } } return $errors; diff --git a/update_form.php b/update_form.php index a1dfe53..e445522 100644 --- a/update_form.php +++ b/update_form.php @@ -68,6 +68,7 @@ class mod_attendance_update_form extends moodleform { 'sdescription' => $sess->description_editor, 'studentscanmark' => $sess->studentscanmark, 'studentpassword' => $sess->studentpassword, + 'autoassignstatus' => $sess->autoassignstatus, 'subnet' => $sess->subnet, 'automark' => $sess->automark, 'automarkcompleted' => 0); @@ -127,7 +128,8 @@ class mod_attendance_update_form extends moodleform { $mform->disabledif('studentpassword', 'studentscanmark', 'notchecked'); $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'); $mgroup = array(); $mgroup[] = & $mform->createElement('text', 'subnet', get_string('requiresubnet', 'attendance')); $mform->setDefault('subnet', $this->_customdata['att']->subnet); @@ -155,6 +157,8 @@ class mod_attendance_update_form extends moodleform { $mform->settype('automark', PARAM_INT); $mform->addElement('hidden', 'automarkcompleted', '0'); $mform->settype('automarkcompleted', PARAM_INT); + $mform->addElement('hidden', 'autoassignstatus', '0'); + $mform->setType('autoassignstatus', PARAM_INT); } $mform->setDefaults($data); diff --git a/version.php b/version.php index 7e5d60f..f3c80e3 100644 --- a/version.php +++ b/version.php @@ -23,7 +23,7 @@ */ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2017050219; +$plugin->version = 2017050220; $plugin->requires = 2017042100; $plugin->release = '3.3.13'; $plugin->maturity = MATURITY_STABLE;