Move subnet from attendance level to session level
authorDan Marsden <dan@danmarsden.com>
Wed, 17 May 2017 23:07:38 +0000 (11:07 +1200)
committerDan Marsden <dan@danmarsden.com>
Wed, 17 May 2017 23:07:38 +0000 (11:07 +1200)
retain attendance level setting as "default" setting for creating
new sessions.

add_form.php
attendance.php
backup/moodle2/backup_attendance_stepslib.php
classes/structure.php
db/install.xml
db/upgrade.php
lang/en/attendance.php
locallib.php
mod_form.php
update_form.php
version.php

index 4157c2f..ff847dd 100644 (file)
@@ -143,10 +143,17 @@ class mod_attendance_add_form extends moodleform {
             if (isset($pluginconfig->randompassword_default)) {
                 $mform->setDefault('randompassword', $pluginconfig->randompassword_default);
             }
+            $mform->addElement('text', 'subnet', get_string('requiresubnet', 'attendance'));
+            $mform->setType('subnet', PARAM_TEXT);
+            $mform->addHelpButton('subnet', 'requiresubnet', 'attendance');
+            $mform->disabledif('subnet', 'studentscanmark', 'notchecked');
+            $mform->setDefault('subnet', $this->_customdata['att']->subnet);
 
         } else {
             $mform->addElement('hidden', 'studentscanmark', '0');
             $mform->settype('studentscanmark', PARAM_INT);
+            $mform->addElement('hidden', 'subnet', '');
+            $mform->setType('subnet', PARAM_TEXT);
         }
 
         $mform->addElement('editor', 'sdescription', get_string('description', 'attendance'), array('rows' => 1, 'columns' => 80),
index f8825ab..c6290da 100644 (file)
@@ -46,7 +46,7 @@ if (!attendance_can_student_mark($attforsession)) {
 }
 
 // Check if subnet is set and if the user is in the allowed range.
-if (!empty($attendance->subnet) && !address_in_subnet(getremoteaddr(), $attendance->subnet)) {
+if (!empty($attforsession->subnet) && !address_in_subnet(getremoteaddr(), $attforsession->subnet)) {
     notice(get_string('subnetwrong', 'attendance'));
     exit; // Notice calls this anyway.
 }
index 507eb2b..9be2593 100644 (file)
@@ -44,17 +44,17 @@ class backup_attendance_activity_structure_step extends backup_activity_structur
 
         // XML nodes declaration - non-user data.
         $attendance = new backup_nested_element('attendance', array('id'), array(
-            'name', 'grade', 'showsessiondetails', 'sessiondetailspos'));
+            'name', 'intro', 'introformat', 'grade', 'showsessiondetails', 'sessiondetailspos'));
 
         $statuses = new backup_nested_element('statuses');
         $status  = new backup_nested_element('status', array('id'), array(
-            'acronym', 'description', 'grade', 'visible', 'deleted', 'setnumber'));
+            'acronym', 'description', 'grade', 'studentavailability', 'visible', 'deleted', 'setnumber'));
 
         $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',
-            'statusset', 'caleventid'));
+            'subnet', 'statusset', 'caleventid'));
 
         // XML nodes declaration - user data.
         $logs = new backup_nested_element('logs');
index b634c4a..6a16719 100644 (file)
@@ -475,11 +475,13 @@ class mod_attendance_structure {
 
         $sess->studentscanmark = 0;
         $sess->studentpassword = '';
+        $sess->subnet = '';
 
         if (!empty(get_config('attendance', 'studentscanmark')) &&
             !empty($formdata->studentscanmark)) {
             $sess->studentscanmark = $formdata->studentscanmark;
             $sess->studentpassword = $formdata->studentpassword;
+            $sess->subnet = $formdata->subnet;
         }
 
         $sess->timemodified = time();
index 411d2b9..95dac93 100644 (file)
@@ -13,7 +13,7 @@
         <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="The time the settings for this attendance instance were last modified."/>
         <FIELD NAME="intro" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="This field is a requirement for activity modules."/>
         <FIELD NAME="introformat" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="This field is a requirement for activity modules."/>
-        <FIELD NAME="subnet" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Restrict ability for students to mark by subnet."/>
+        <FIELD NAME="subnet" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Default subnet used when creating sessions."/>
         <FIELD NAME="sessiondetailspos" TYPE="char" LENGTH="5" NOTNULL="true" DEFAULT="left" SEQUENCE="false" COMMENT="Position for the session detail columns related to summary columns."/>
         <FIELD NAME="showsessiondetails" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false" COMMENT="Define if session details should be shown in reports."/>
       </FIELDS>
@@ -38,6 +38,7 @@
         <FIELD NAME="descriptionformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
         <FIELD NAME="studentscanmark" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
         <FIELD NAME="studentpassword" TYPE="char" LENGTH="50" NOTNULL="false" DEFAULT="" SEQUENCE="false"/>
+        <FIELD NAME="subnet" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Restrict ability for students to mark by subnet."/>
         <FIELD NAME="statusset" TYPE="int" LENGTH="5" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Which set of statuses to use"/>
         <FIELD NAME="caleventid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
       </FIELDS>
index 42cf604..5aed323 100644 (file)
@@ -260,5 +260,33 @@ function xmldb_attendance_upgrade($oldversion=0) {
         upgrade_mod_savepoint(true, 2017051101, 'attendance');
     }
 
+    if ($oldversion < 2017051103) {
+        $table = new xmldb_table('attendance_sessions');
+        $newfield = $table->add_field('subnet', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'studentpassword');
+        if (!$dbman->field_exists($table, $newfield)) {
+            $dbman->add_field($table, $newfield);
+        }
+        upgrade_mod_savepoint(true, 2017051103, 'attendance');
+    }
+
+    if ($oldversion < 2017051104) {
+        // The meaning of the subnet in the attendance table has changed - it is now the "default" value - find all existing
+        // Attendance with subnet set and set the session subnet for these.
+        $attendances = $DB->get_recordset_select('attendance', 'subnet IS NOT NULL');
+        foreach ($attendances as $attendance) {
+            if (!empty($attendance->subnet)) {
+                // Get all sessions for this attendance.
+                $sessions = $DB->get_recordset('attendance_sessions', array('attendanceid' => $attendance->id));
+                foreach ($sessions as $session) {
+                    $session->subnet = $attendance->subnet;
+                    $DB->update_record('attendance_sessions', $session);
+                }
+                $sessions->close();
+            }
+        }
+        $attendances->close();
+
+        upgrade_mod_savepoint(true, 2017051104, 'attendance');
+    }
     return $result;
 }
index 61c1b21..61a3bf0 100644 (file)
@@ -374,6 +374,8 @@ $string['extrarestrictions'] = 'Extra restrictions';
 $string['requiresubnet'] = 'Students can only record own attendance from these computers.';
 $string['subnetwrong'] = 'Attendance can only be recorded from certain locations, and this computer is not on the allowed list.';
 $string['requiresubnet_help'] = 'Attendance recording may be restricted to particular subnets by specifying a comma-separated list of partial or full IP addresses.';
+$string['defaultsubnet'] = 'Default subnet range';
+$string['defaultsubnet_help'] = 'Attendance recording may be restricted to particular subnets by specifying a comma-separated list of partial or full IP addresses. This is the default value used when creating new sessions.';
 $string['defaultsettings'] = 'Default attendance settings';
 $string['defaultsettings_help'] = 'These settings define the defaults for all new attendances';
 $string['defaultstatus'] = 'Default status set';
index 1af86d2..b6eb971 100644 (file)
@@ -560,6 +560,7 @@ function attendance_construct_sessions_data_for_add($formdata) {
                     $sess->timemodified = $now;
                     if (isset($formdata->studentscanmark)) { // Students will be able to mark their own attendance.
                         $sess->studentscanmark = 1;
+                        $sess->subnet = $formdata->subnet;
                         if (!empty($formdata->randompassword)) {
                             $sess->studentpassword = attendance_random_string();
                         } else {
@@ -567,6 +568,7 @@ function attendance_construct_sessions_data_for_add($formdata) {
                         }
                     } else {
                         $sess->studentpassword = '';
+                        $sess->subnet = '';
                     }
                     $sess->statusset = $formdata->statusset;
 
@@ -587,6 +589,7 @@ function attendance_construct_sessions_data_for_add($formdata) {
         $sess->descriptionformat = $formdata->sdescription['format'];
         $sess->timemodified = $now;
         $sess->studentscanmark = 0;
+        $sess->subnet = '';
         $sess->studentpassword = '';
 
         if (isset($formdata->studentscanmark) && !empty($formdata->studentscanmark)) {
@@ -597,6 +600,7 @@ function attendance_construct_sessions_data_for_add($formdata) {
             } else {
                 $sess->studentpassword = $formdata->studentpassword;
             }
+            $sess->subnet = $formdata->subnet;
         }
         $sess->statusset = $formdata->statusset;
 
index dc1e9e7..64bb8f6 100644 (file)
@@ -61,9 +61,9 @@ class mod_attendance_mod_form extends moodleform_mod {
 
         $mform->addElement('header', 'security', get_string('extrarestrictions', 'attendance'));
         // IP address.
-        $mform->addElement('text', 'subnet', get_string('requiresubnet', 'attendance'), array('size' => '164'));
+        $mform->addElement('text', 'subnet', get_string('defaultsubnet', 'attendance'), array('size' => '164'));
         $mform->setType('subnet', PARAM_TEXT);
-        $mform->addHelpButton('subnet', 'requiresubnet', 'attendance');
+        $mform->addHelpButton('subnet', 'defaultsubnet', 'attendance');
         $mform->setDefault('subnet', $attendanceconfig->subnet);
 
         $this->add_action_buttons();
index 24c752a..f149b46 100644 (file)
@@ -66,7 +66,8 @@ class mod_attendance_update_form extends moodleform {
                                    'endhour' => $endhour, 'endminute' => $endminute),
                 'sdescription' => $sess->description_editor,
                 'studentscanmark' => $sess->studentscanmark,
-                'studentpassword' => $sess->studentpassword);
+                'studentpassword' => $sess->studentpassword,
+                'subnet' => $sess->subnet);
 
         $mform->addElement('header', 'general', get_string('changesession', 'attendance'));
 
@@ -99,9 +100,16 @@ class mod_attendance_update_form extends moodleform {
             $mform->setType('studentpassword', PARAM_TEXT);
             $mform->addHelpButton('studentpassword', 'passwordgrp', 'attendance');
             $mform->disabledif('studentpassword', 'studentscanmark', 'notchecked');
+
+            $mform->addElement('text', 'subnet', get_string('requiresubnet', 'attendance'));
+            $mform->setType('subnet', PARAM_TEXT);
+            $mform->addHelpButton('subnet', 'requiresubnet', 'attendance');
+            $mform->disabledif('subnet', 'studentscanmark', 'notchecked');
         } else {
             $mform->addElement('hidden', 'studentscanmark', '0');
             $mform->settype('studentscanmark', PARAM_INT);
+            $mform->addElement('hidden', 'subnet', '0');
+            $mform->settype('subnet', PARAM_TEXT);
         }
 
         $mform->addElement('editor', 'sdescription', get_string('description', 'attendance'),
index 3a0058c..5a0db02 100644 (file)
@@ -23,7 +23,7 @@
  */
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version  = 2017051102;
+$plugin->version  = 2017051104;
 $plugin->requires = 2017042100;
 $plugin->release = '3.3.5';
 $plugin->maturity  = MATURITY_ALPHA;