From 19bb08468607a9e0c11dfefdf5150f471a5e97db Mon Sep 17 00:00:00 2001 From: Nick Phillips Date: Fri, 9 Aug 2019 14:52:52 +1200 Subject: [PATCH] Add updated event for per-student reports from view.php, and use it, moving from internal viewed event to the one Dan integrated at the same time. Need to re-look at event validation and possibly-optional params per Dan's changes to mode param. --- classes/event/session_report_updated.php | 61 ++++++++++++++++++++++++ classes/event/session_report_viewed.php | 12 +++-- classes/structure.php | 2 +- lang/en/attendance.php | 1 + renderables.php | 15 ------ view.php | 31 ++++++++---- 6 files changed, 91 insertions(+), 31 deletions(-) create mode 100644 classes/event/session_report_updated.php diff --git a/classes/event/session_report_updated.php b/classes/event/session_report_updated.php new file mode 100644 index 0000000..408a369 --- /dev/null +++ b/classes/event/session_report_updated.php @@ -0,0 +1,61 @@ +. + +/** + * This file contains an event for when a student's attendance report is viewed. + * + * @package mod_attendance + * @copyright 2014 onwards Dan Marsden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_attendance\event; +defined('MOODLE_INTERNAL') || die(); + +/** + * Event for when a student's attendance report is updated. + * + * @property-read array $other { + * Extra information about event properties. + * + * string studentid Id of student whose attendances were updated. + * string mode Mode of the report updated. + * } + * @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 session_report_updated extends \mod_attendance\event\session_report_viewed { + + /** + * Init method. + */ + protected function init() { + $this->data['crud'] = 'u'; + $this->data['edulevel'] = self::LEVEL_TEACHING; + // objecttable and objectid can't be meaningfully specified + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventstudentattendancesessionsupdated', 'mod_attendance'); + } +} diff --git a/classes/event/session_report_viewed.php b/classes/event/session_report_viewed.php index ba3cf0a..4fe8940 100644 --- a/classes/event/session_report_viewed.php +++ b/classes/event/session_report_viewed.php @@ -55,7 +55,7 @@ class session_report_viewed extends \core\event\base { * @return string */ public function get_description() { - return 'User with id ' . $this->userid . ' viewed attendance sessions for student with id ' . + return 'User with id ' . $this->userid . ' ' . $this->action . ' attendance sessions for student with id ' . $this->relateduserid; } @@ -80,6 +80,8 @@ class session_report_viewed extends \core\event\base { 'studentid' => $this->relateduserid, 'mode' => $mode, 'view' => $this->other['view'], + 'groupby' => $this->other['groupby'], + 'sesscourses' => $this->other['sesscourses'], 'curdate' => $this->other['curdate'])); } @@ -89,7 +91,7 @@ class session_report_viewed extends \core\event\base { * @return array of parameters to be passed to legacy add_to_log() function. */ protected function get_legacy_logdata() { - return array($this->courseid, 'attendance', 'student sessions viewed', $this->get_url(), + return array($this->courseid, 'attendance', 'student sessions ' . $this->action, $this->get_url(), 'student id ' . $this->relateduserid, $this->contextinstanceid); } @@ -119,17 +121,17 @@ class session_report_viewed extends \core\event\base { */ protected function validate_data() { if (!isset($this->relateduserid)) { - throw new \coding_exception('The event mod_attendance\\event\\session_report_viewed must specify relateduserid.'); + throw new \coding_exception('The event ' . $this->eventname . ' must specify relateduserid.'); } // View params can be left out as defaults will be the same when log event is viewed as when // it was stored. // filter params are important, but stored in session so default effectively unknown, // hence required here. if (!isset($this->other['view'])) { - throw new \coding_exception('The event mod_attendance\\event\\session_report_viewed must specify view.'); + throw new \coding_exception('The event ' . $this->eventname . ' must specify view.'); } if (!isset($this->other['curdate'])) { - throw new \coding_exception('The event mod_attendance\\event\\session_report_viewed must specify curdate.'); + throw new \coding_exception('The event ' . $this->eventname . ' must specify curdate.'); } parent::validate_data(); } diff --git a/classes/structure.php b/classes/structure.php index a767318..c5d349f 100644 --- a/classes/structure.php +++ b/classes/structure.php @@ -114,7 +114,7 @@ class mod_attendance_structure { * @param stdClass $dbrecord Attandance instance data from {attendance} table * @param stdClass $cm Course module record as returned by {@see get_coursemodule_from_id()} * @param stdClass $course Course record from {course} table - * @param stdClass $context The context of the workshop instance + * @param stdClass $context The context of the attendance instance * @param stdClass $pageparams */ public function __construct(stdClass $dbrecord, stdClass $cm, stdClass $course, stdClass $context=null, $pageparams=null) { diff --git a/lang/en/attendance.php b/lang/en/attendance.php index d2bc83c..19b3bcb 100644 --- a/lang/en/attendance.php +++ b/lang/en/attendance.php @@ -236,6 +236,7 @@ $string['eventsessionupdated'] = 'Session updated'; $string['eventstatusadded'] = 'Status added'; $string['eventstatusupdated'] = 'Status updated'; $string['eventstudentattendancesessionsviewed'] = 'Session report viewed'; +$string['eventstudentattendancesessionsupdated'] = 'Session report updated'; $string['eventtaken'] = 'Attendance taken'; $string['eventtakenbystudent'] = 'Attendance taken by student'; $string['export'] = 'Export'; diff --git a/renderables.php b/renderables.php index 90ce3ce..d324fdd 100644 --- a/renderables.php +++ b/renderables.php @@ -571,8 +571,6 @@ class attendance_user_data implements renderable { // TODO: WARNING - $formdata is unclean - comes from direct $_POST - ideally needs a rewrite but we do some cleaning below. // This whole function could do with a nice clean up. - // XXX - replace this, see below - //$statuses = implode(',', array_keys( (array)$this->get_statuses() )); $now = time(); $sesslog = array(); $formdata = (array)$formdata; @@ -671,20 +669,7 @@ class attendance_user_data implements renderable { } } } - - // Create url for link in log screen. - $params = $this->pageparams->get_significant_params(); - // XXX - TODO - // Waiting for event for viewing user report(s) before creating derived event for editing. - /* $event = \mod_attendance\event\attendance_taken::create(array( */ - /* 'objectid' => $this->id, */ - /* 'context' => $this->context, */ - /* 'other' => $params)); */ - /* $event->add_record_snapshot('course_modules', $this->cm); */ - /* $event->add_record_snapshot('attendance_sessions', $session); */ - /* $event->trigger(); */ } - } /** diff --git a/view.php b/view.php index 40aafbf..efed400 100644 --- a/view.php +++ b/view.php @@ -84,27 +84,38 @@ $filterparams = array( 'enddate' => $userdata->pageparams->enddate ); $params = array_merge($userdata->pageparams->get_significant_params(), $filterparams); + +$header = new mod_attendance_header($att); + if (empty($userdata->pageparams->studentid)) { $relateduserid = $USER->id; } else { $relateduserid = $userdata->pageparams->studentid; } -// Trigger viewed event. -$event = \mod_attendance\event\session_report_viewed::create(array( - 'relateduserid' => $relateduserid, - 'context' => $context, - 'other' => $params)); -$event->add_record_snapshot('course_modules', $cm); -$event->trigger(); - -$header = new mod_attendance_header($att); - if (($formdata = data_submitted()) && confirm_sesskey()) { $userdata->take_sessions_from_form_data($formdata); + // Trigger updated event + $event = \mod_attendance\event\session_report_updated::create(array( + 'relateduserid' => $relateduserid, + 'context' => $context, + 'other' => $params)); + $event->add_record_snapshot('course_modules', $cm); + //$event->add_record_snapshot('user', $); + $event->trigger(); + redirect($url, get_string('attendancesuccess', 'attendance')); } +else { + // Trigger viewed event + $event = \mod_attendance\event\session_report_viewed::create(array( + 'relateduserid' => $relateduserid, + 'context' => $context, + 'other' => $params)); + $event->add_record_snapshot('course_modules', $cm); + $event->trigger(); +} $PAGE->set_title($course->shortname. ": ".$att->name); $PAGE->set_heading($course->fullname);