Fix #75 - add_record_snapshot() gives debug messages on various pages. Add status_re...
authorBarry Oosthuizen <barry.oosthuizen@nottingham.ac.uk>
Fri, 13 Feb 2015 13:50:21 +0000 (13:50 +0000)
committerBarry Oosthuizen <barry.oosthuizen@nottingham.ac.uk>
Tue, 26 May 2015 16:18:57 +0000 (17:18 +0100)
classes/event/status_removed.php [new file with mode: 0644]
locallib.php
preferences.php

diff --git a/classes/event/status_removed.php b/classes/event/status_removed.php
new file mode 100644 (file)
index 0000000..96373da
--- /dev/null
@@ -0,0 +1,100 @@
+<?php
+// This file is part of the Attendance module for Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * This file contains an event for when an attendance status is updated.
+ *
+ * @package    mod_attendance
+ * @copyright  2015 onwards, University of Nottingham
+ * @author     Barry Oosthuizen <barry.oosthuizen@nottingham.ac.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_attendance\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event for when an attendance status is removed.
+ *
+ * @property-read array $other {
+ *      Extra information about event properties.
+ *
+ *      @string mode Mode of the report viewed.
+ * }
+ * @package    mod_attendance
+ * @since      Moodle 2.7
+ * @copyright  2013 onwards Dan Marsden
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class status_removed extends \core\event\base {
+
+    /**
+     * Init method.
+     */
+    protected function init() {
+        $this->data['crud'] = 'd';
+        $this->data['edulevel'] = self::LEVEL_TEACHING;
+        $this->data['objecttable'] = 'attendance_statuses';
+    }
+
+    /**
+     * Returns non-localised description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return 'User with id ' . $this->userid . ' deleted attendance status "' . $this->data['other']['acronym'] . ' - ' . $this->data['other']['description'] . '" with instanceid ' .
+            $this->objectid . '';
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('statusdeleted', 'mod_attendance');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/attendance/preferences.php', array('id' => $this->contextinstanceid));
+    }
+
+    /**
+     * Replace add_to_log() statement.
+     *
+     * @return array of parameters to be passed to legacy add_to_log() function.
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, 'attendance', 'status removed', $this->get_url(),
+            $this->other['acronym'] . ' - ' . $this->other['description'], $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+    }
+}
index 3e2e29c..4f2ab83 100644 (file)
@@ -794,32 +794,24 @@ class attendance {
                         array('subdirs' => false, 'maxfiles' => -1, 'maxbytes' => 0),
                         $sess->description);
             $DB->set_field('attendance_sessions', 'description', $description, array('id' => $sess->id));
-        }
 
-        $info_array = array();
-        $maxlog = 7; // Only log first 10 sessions and last session in the log info. as we can only store 255 chars.
-        $i = 0;
-        foreach ($sessions as $sess) {
-            if ($i > $maxlog) {
-                $lastsession = end($sessions);
-                $info_array[] = '...';
-                $info_array[] = construct_session_full_date_time($lastsession->sessdate, $lastsession->duration);
-                break;
-            } else {
-                $info_array[] = construct_session_full_date_time($sess->sessdate, $sess->duration);
-            }
-            $i++;
-        }
-        // Trigger a report viewed event.
-        $event = \mod_attendance\event\session_added::create(array(
-            'objectid' => $this->id,
-            'context' => $this->context,
-            'other' => array('info' => implode(',', $info_array))
-        ));
-        $event->add_record_snapshot('course_modules', $this->cm);
-        $event->add_record_snapshot('attendance', $this);
-        $event->trigger();
+            $info_array = array();
+            $info_array[] = construct_session_full_date_time($sess->sessdate, $sess->duration);
 
+            // Trigger a session added event.
+            $event = \mod_attendance\event\session_added::create(array(
+                        'objectid' => $this->id,
+                        'context' => $this->context,
+                        'other' => array('info' => implode(',', $info_array))
+            ));
+            $event->add_record_snapshot('course_modules', $this->cm);
+            $sess->description = $description;
+            $sess->lasttaken = 0;
+            $sess->lasttakenby = 0;
+            $sess->studentscanmark = 0;
+            $event->add_record_snapshot('attendance_sessions', $sess);
+            $event->trigger();
+        }
     }
 
     public function update_session_from_form_data($formdata, $sessionid) {
@@ -845,7 +837,7 @@ class attendance {
             'context' => $this->context,
             'other' => array('info' => $info, 'sessionid' => $sessionid, 'action' => att_sessions_page_params::ACTION_UPDATE)));
         $event->add_record_snapshot('course_modules', $this->cm);
-        $event->add_record_snapshot('attendance', $this);
+        $event->add_record_snapshot('attendance_sessions', $sess);
         $event->trigger();
     }
 
@@ -882,11 +874,10 @@ class attendance {
         }
 
         // Update the session to show that a register has been taken, or staff may overwrite records.
-        $rec = new object();
-        $rec->id = $mformdata->sessid;
-        $rec->lasttaken = $now;
-        $rec->lasttakenby = $USER->id;
-        $DB->update_record('attendance_sessions', $rec);
+        $session = $this->get_session_info($mformdata->sessid);
+        $session->lasttaken = $now;
+        $session->lasttakenby = $USER->id;
+        $DB->update_record('attendance_sessions', $session);
 
         // Update the users grade.
         $this->update_users_grade(array($USER->id));
@@ -894,18 +885,19 @@ class attendance {
         /* create url for link in log screen
          * need to set grouptype to 0 to allow take attendance page to be called
          * from report/log page */
-         
+
         $params = array(
                 'sessionid' => $this->pageparams->sessionid,
                 'grouptype' => 0);
-               
+
         // Log the change.
         $event = \mod_attendance\event\attendance_taken_by_student::create(array(
             'objectid' => $this->id,
             'context' => $this->context,
             'other' => $params));
         $event->add_record_snapshot('course_modules', $this->cm);
-        $event->add_record_snapshot('attendance', $this);
+        $event->add_record_snapshot('attendance_sessions', $session);
+        $event->add_record_snapshot('attendance_log', $record);
         $event->trigger();
 
         return true;
@@ -948,11 +940,10 @@ class attendance {
             }
         }
 
-        $rec = new stdClass();
-        $rec->id = $this->pageparams->sessionid;
-        $rec->lasttaken = $now;
-        $rec->lasttakenby = $USER->id;
-        $DB->update_record('attendance_sessions', $rec);
+        $session = $this->get_session_info($this->pageparams->sessionid);
+        $session->lasttaken = $now;
+        $session->lasttakenby = $USER->id;
+        $DB->update_record('attendance_sessions', $session);
 
         if ($this->grade != 0) {
             $this->update_users_grade(array_keys($sesslog));
@@ -967,7 +958,7 @@ class attendance {
             'context' => $this->context,
             'other' => $params));
         $event->add_record_snapshot('course_modules', $this->cm);
-        $event->add_record_snapshot('attendance', $this);
+        $event->add_record_snapshot('attendance_sessions', $session);
         $event->trigger();
 
         $group = 0;
@@ -1402,7 +1393,6 @@ class attendance {
             'context' => $this->context,
             'other' => array('info' => implode(', ', $sessionsids))));
         $event->add_record_snapshot('course_modules', $this->cm);
-        $event->add_record_snapshot('attendance', $this);
         $event->trigger();
     }
 
@@ -1415,23 +1405,47 @@ class attendance {
             $sess->duration = $duration;
             $sess->timemodified = $now;
             $DB->update_record('attendance_sessions', $sess);
+            $event = \mod_attendance\event\session_duration_updated::create(array(
+                'objectid' => $this->id,
+                'context' => $this->context,
+                'other' => array('info' => implode(', ', $sessionsids))));
+            $event->add_record_snapshot('course_modules', $this->cm);
+            $event->add_record_snapshot('attendance_sessions', $sess);
+            $event->trigger();
         }
         $sessions->close();
-        $event = \mod_attendance\event\session_duration_updated::create(array(
-            'objectid' => $this->id,
-            'context' => $this->context,
-            'other' => array('info' => implode(', ', $sessionsids))));
-        $event->add_record_snapshot('course_modules', $this->cm);
-        $event->add_record_snapshot('attendance', $this);
-        $event->trigger();
     }
 
-    public function remove_status($statusid) {
+    /**
+     * Remove a status variable from an attendance instance
+     * 
+     * @global moodle_database $DB
+     * @param stdClass $status
+     */
+    public function remove_status($status) {
         global $DB;
 
-        $DB->set_field('attendance_statuses', 'deleted', 1, array('id' => $statusid));
+        $DB->set_field('attendance_statuses', 'deleted', 1, array('id' => $status->id));
+        $event = \mod_attendance\event\status_removed::create(array(
+            'objectid' => $status->id,
+            'context' => $this->context, 
+            'other' => array(
+                'acronym' => $status->acronym,
+                'description' => $status->description
+            )));
+        $event->add_record_snapshot('course_modules', $this->cm);
+        $event->add_record_snapshot('attendance_statuses', $status);
+        $event->trigger();
     }
 
+    /**
+     * Add an attendance status variable
+     * 
+     * @global moodle_database $DB
+     * @param string $acronym
+     * @param string $description
+     * @param int $grade
+     */
     public function add_status($acronym, $description, $grade) {
         global $DB;
 
@@ -1442,27 +1456,38 @@ class attendance {
             $rec->acronym = $acronym;
             $rec->description = $description;
             $rec->grade = $grade;
-            $DB->insert_record('attendance_statuses', $rec);
+            $rec->deleted = 0;
+            $rec->visible = 1;
+            $id = $DB->insert_record('attendance_statuses', $rec);
+            $rec->id = $id;
 
             $event = \mod_attendance\event\status_added::create(array(
                 'objectid' => $this->id,
                 'context' => $this->context,
                 'other' => array('acronym' => $acronym, 'description' => $description, 'grade' => $grade)));
             $event->add_record_snapshot('course_modules', $this->cm);
-            $event->add_record_snapshot('attendance', $this);
+            $event->add_record_snapshot('attendance_statuses', $rec);
             $event->trigger();
         } else {
             print_error('cantaddstatus', 'attendance', $this->url_preferences());
         }
     }
 
-    public function update_status($statusid, $acronym, $description, $grade, $visible) {
+    /**
+     * Update status variable for a particular Attendance module instance
+     * 
+     * @global moodle_database $DB
+     * @param stdClass $statusid
+     * @param string $acronym
+     * @param string $description
+     * @param int $grade
+     * @param bool $visible
+     */
+    public function update_status($status, $acronym, $description, $grade, $visible) {
         global $DB;
 
         $updated = array();
 
-        $status = new stdClass();
-        $status->id = $statusid;
         if ($acronym) {
             $status->acronym = $acronym;
             $updated[] = $acronym;
@@ -1486,10 +1511,10 @@ class attendance {
             'context' => $this->context,
             'other' => array('acronym' => $acronym, 'description' => $description, 'grade' => $grade, 'updated' => implode(' ', $updated))));
         $event->add_record_snapshot('course_modules', $this->cm);
-        $event->add_record_snapshot('attendance', $this);
+        $event->add_record_snapshot('attendance_statuses', $status);
         $event->trigger();
     }
-    
+
 }
 
 function att_get_statuses($attid, $onlyvisible=true) {
index 8597fdd..ce30721 100644 (file)
@@ -62,13 +62,14 @@ switch ($att->pageparams->action) {
         }
 
         $confirm    = optional_param('confirm', null, PARAM_INT);
+        $statuses = $att->get_statuses(false);
+        $status = $statuses[$att->pageparams->statusid];
+
         if (isset($confirm)) {
-            $att->remove_status($att->pageparams->statusid);
+            $att->remove_status($status);
             redirect($att->url_preferences(), get_string('statusdeleted', 'attendance'));
         }
 
-        $statuses = $att->get_statuses();
-        $status = $statuses[$att->pageparams->statusid];
         $message = get_string('deletecheckfull', '', get_string('variable', 'attendance'));
         $message .= str_repeat(html_writer::empty_tag('br'), 2);
         $message .= $status->acronym.': '.
@@ -80,18 +81,24 @@ switch ($att->pageparams->action) {
         echo $OUTPUT->footer();
         exit;
     case att_preferences_page_params::ACTION_HIDE:
-        $att->update_status($att->pageparams->statusid, null, null, null, 0);
+        $statuses = $att->get_statuses();
+        $status = $statuses[$att->pageparams->statusid];
+        $att->update_status($status, null, null, null, 0);
         break;
     case att_preferences_page_params::ACTION_SHOW:
-        $att->update_status($att->pageparams->statusid, null, null, null, 1);
+        $statuses = $att->get_statuses(false);
+        $status = $statuses[$att->pageparams->statusid];
+        $att->update_status($status, null, null, null, 1);
         break;
     case att_preferences_page_params::ACTION_SAVE:
         $acronym        = required_param_array('acronym', PARAM_MULTILANG);
         $description    = required_param_array('description', PARAM_MULTILANG);
         $grade          = required_param_array('grade', PARAM_INT);
+        $statuses = $att->get_statuses(false);
 
         foreach ($acronym as $id => $v) {
-            $att->update_status($id, $acronym[$id], $description[$id], $grade[$id], null);
+            $status = $statuses[$id];
+            $att->update_status($status, $acronym[$id], $description[$id], $grade[$id], null);
         }
         if ($att->grade > 0) {
             att_update_all_users_grades($att->id, $att->course, $att->context, $cm);