diff --git a/db/upgrade.php b/db/upgrade.php index 3f7025a..8291701 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -421,5 +421,24 @@ function xmldb_attendance_upgrade($oldversion=0) { // Attendance savepoint reached. upgrade_mod_savepoint(true, 2017050214, 'attendance'); } + + if ($oldversion < 2017050216) { + // 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, 2017050216, 'attendance'); + } return $result; } diff --git a/lib.php b/lib.php index f5c3cdc..c2af403 100644 --- 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); diff --git a/locallib.php b/locallib.php index 275eb01..dfb3c92 100644 --- a/locallib.php +++ b/locallib.php @@ -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} diff --git a/version.php b/version.php index 7ca75b5..5435e01 100644 --- a/version.php +++ b/version.php @@ -23,7 +23,7 @@ */ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2017050215; +$plugin->version = 2017050216; $plugin->requires = 2017042100; $plugin->release = '3.3.11'; $plugin->maturity = MATURITY_STABLE; diff --git a/warnings.php b/warnings.php index 0716575..d283386 100644 --- a/warnings.php +++ b/warnings.php @@ -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)) {