Use attendance-id instead of cmid in warning table.
authorDan Marsden <dan@danmarsden.com>
Mon, 21 Aug 2017 22:37:59 +0000 (10:37 +1200)
committerDan Marsden <dan@danmarsden.com>
Mon, 21 Aug 2017 22:37:59 +0000 (10:37 +1200)
db/upgrade.php
lib.php
locallib.php
version.php
warnings.php

index 68f9f5a..ce1be8e 100644 (file)
@@ -434,5 +434,24 @@ function xmldb_attendance_upgrade($oldversion=0) {
         // Attendance savepoint reached.
         upgrade_mod_savepoint(true, 2017071802, 'attendance');
     }
+
+    if ($oldversion < 2017082200) {
+        // Warnings idnumber field should use attendanceid instead of cmid.
+        $sql = "SELECT cm.id, cm.instance
+                  FROM {course_modules} cm
+                  JOIN {modules} md ON md.id = cm.module AND md.name = 'attendance'";
+        $idnumbers = $DB->get_records_sql_menu($sql);
+        $warnings = $DB->get_recordset('attendance_warning');
+        foreach ($warnings as $warning) {
+            if (!empty($warning->idnumber) && !empty($idnumbers[$warning->idnumber])) {
+                $warning->idnumber = $idnumbers[$warning->idnumber];
+                $DB->update_record("attendance_warning", $warning);
+            }
+        }
+        $warnings->close();
+
+        // Attendance savepoint reached.
+        upgrade_mod_savepoint(true, 2017082200, 'attendance');
+    }
     return $result;
 }
diff --git a/lib.php b/lib.php
index f5c3cdc..c2af403 100644 (file)
--- a/lib.php
+++ b/lib.php
@@ -72,9 +72,9 @@ function att_add_default_statuses($attid) {
 /**
  * Add default set of warnings to the new attendance.
  *
- * @param int $attid - id of attendance instance.
+ * @param int $id - id of attendance instance.
  */
-function attendance_add_default_warnings($cmid) {
+function attendance_add_default_warnings($id) {
     global $DB, $CFG;
     require_once($CFG->dirroot.'/mod/attendance/locallib.php');
 
@@ -82,7 +82,7 @@ function attendance_add_default_warnings($cmid) {
         array('idnumber' => 0), 'id');
     foreach ($warnings as $n) {
         $rec = $n;
-        $rec->idnumber = $cmid;
+        $rec->idnumber = $id;
         $DB->insert_record('attendance_warning', $rec);
     }
     $warnings->close();
@@ -103,7 +103,7 @@ function attendance_add_instance($attendance) {
 
     att_add_default_statuses($attendance->id);
 
-    attendance_add_default_warnings($attendance->coursemodule);
+    attendance_add_default_warnings($attendance->id);
 
     attendance_grade_item_update($attendance);
 
index 275eb01..dfb3c92 100644 (file)
@@ -755,7 +755,7 @@ function attendance_get_users_to_notify($courseids = array(), $orderby = '', $al
                    JOIN {attendance_log} atl ON (atl.sessionid = ats.id)
                    JOIN {user} u ON (u.id = atl.studentid)
                    JOIN {attendance_statuses} stg ON (stg.id = atl.statusid AND stg.deleted = 0 AND stg.visible = 1)
-                   JOIN {attendance_warning} n ON n.idnumber = cm.id
+                   JOIN {attendance_warning} n ON n.idnumber = a.id
                    LEFT JOIN {attendance_warning_done} ns ON ns.notifyid = n.id AND ns.userid = atl.studentid
                    JOIN (SELECT attendanceid, setnumber, MAX(grade) AS maxgrade
                            FROM {attendance_statuses}
index 0eefc08..dec509c 100644 (file)
@@ -23,7 +23,7 @@
  */
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version  = 2017072100;
+$plugin->version  = 2017082200;
 $plugin->requires = 2017042100;
 $plugin->release = '3.3.11';
 $plugin->maturity  = MATURITY_ALPHA;
index 0716575..d283386 100644 (file)
@@ -79,7 +79,7 @@ if ($data = $mform->get_data()) {
         if (empty($id)) {
             $notify->idnumber = 0;
         } else {
-            $notify->idnumber = $cm->id;
+            $notify->idnumber = $att->id;
         }
 
         $notify->warningpercent = $data->warningpercent;
@@ -105,7 +105,7 @@ if ($data = $mform->get_data()) {
 
     } else {
         $notify = $DB->get_record('attendance_warning', array('id' => $data->notid));
-        if (!empty($id) && $data->idnumber != $id) {
+        if (!empty($id) && $data->idnumber != $att->id) {
             // Someone is trying to update a record for a different attendance.
             print_error('invalidcoursemodule');
         } else {
@@ -146,7 +146,7 @@ if ($action == 'delete' && !empty($notid)) {
         $params = array('id' => $notid);
         if (!empty($id)) {
             // Add id/level to array.
-            $params['idnumber'] = $cm->id;
+            $params['idnumber'] = $id;
         }
         $DB->delete_records('attendance_warning', $params);
         echo $OUTPUT->notification(get_string('warningdeleted', 'mod_attendance'), 'success');
@@ -173,7 +173,7 @@ if ($action == 'update' && !empty($notid)) {
             'warningpercent');
     } else {
         $existingnotifications = $DB->get_records('attendance_warning',
-            array('idnumber' => $cm->id),
+            array('idnumber' => $att->id),
             'warningpercent');
     }
     if (!empty($existingnotifications)) {