Browse Source

Merge pull request #100 from barrysspace/add_record_snapshot_errors_MDL27

Fix #75 - Add record snapshot errors mdl27
MOODLE_27_STABLE
Dan Marsden 10 years ago
parent
commit
8c0dfb29c6
  1. 100
      classes/event/status_removed.php
  2. 107
      locallib.php
  3. 19
      preferences.php

100
classes/event/status_removed.php

@ -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();
}
}

107
locallib.php

@ -795,32 +795,24 @@ class attendance {
array('subdirs' => false, 'maxfiles' => -1, 'maxbytes' => 0), array('subdirs' => false, 'maxfiles' => -1, 'maxbytes' => 0),
$sess->description); $sess->description);
$DB->set_field('attendance_sessions', 'description', $description, array('id' => $sess->id)); $DB->set_field('attendance_sessions', 'description', $description, array('id' => $sess->id));
}
$info_array = array(); $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); $info_array[] = construct_session_full_date_time($sess->sessdate, $sess->duration);
}
$i++; // Trigger a session added event.
}
// Trigger a report viewed event.
$event = \mod_attendance\event\session_added::create(array( $event = \mod_attendance\event\session_added::create(array(
'objectid' => $this->id, 'objectid' => $this->id,
'context' => $this->context, 'context' => $this->context,
'other' => array('info' => implode(',', $info_array)) 'other' => array('info' => implode(',', $info_array))
)); ));
$event->add_record_snapshot('course_modules', $this->cm); $event->add_record_snapshot('course_modules', $this->cm);
$event->add_record_snapshot('attendance', $this); $sess->description = $description;
$sess->lasttaken = 0;
$sess->lasttakenby = 0;
$sess->studentscanmark = 0;
$event->add_record_snapshot('attendance_sessions', $sess);
$event->trigger(); $event->trigger();
}
} }
public function update_session_from_form_data($formdata, $sessionid) { public function update_session_from_form_data($formdata, $sessionid) {
@ -846,7 +838,7 @@ class attendance {
'context' => $this->context, 'context' => $this->context,
'other' => array('info' => $info, 'sessionid' => $sessionid, 'action' => att_sessions_page_params::ACTION_UPDATE))); '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('course_modules', $this->cm);
$event->add_record_snapshot('attendance', $this); $event->add_record_snapshot('attendance_sessions', $sess);
$event->trigger(); $event->trigger();
} }
@ -883,11 +875,10 @@ class attendance {
} }
// Update the session to show that a register has been taken, or staff may overwrite records. // Update the session to show that a register has been taken, or staff may overwrite records.
$rec = new object(); $session = $this->get_session_info($mformdata->sessid);
$rec->id = $mformdata->sessid; $session->lasttaken = $now;
$rec->lasttaken = $now; $session->lasttakenby = $USER->id;
$rec->lasttakenby = $USER->id; $DB->update_record('attendance_sessions', $session);
$DB->update_record('attendance_sessions', $rec);
// Update the users grade. // Update the users grade.
$this->update_users_grade(array($USER->id)); $this->update_users_grade(array($USER->id));
@ -906,7 +897,8 @@ class attendance {
'context' => $this->context, 'context' => $this->context,
'other' => $params)); 'other' => $params));
$event->add_record_snapshot('course_modules', $this->cm); $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(); $event->trigger();
return true; return true;
@ -949,11 +941,10 @@ class attendance {
} }
} }
$rec = new stdClass(); $session = $this->get_session_info($this->pageparams->sessionid);
$rec->id = $this->pageparams->sessionid; $session->lasttaken = $now;
$rec->lasttaken = $now; $session->lasttakenby = $USER->id;
$rec->lasttakenby = $USER->id; $DB->update_record('attendance_sessions', $session);
$DB->update_record('attendance_sessions', $rec);
if ($this->grade != 0) { if ($this->grade != 0) {
$this->update_users_grade(array_keys($sesslog)); $this->update_users_grade(array_keys($sesslog));
@ -968,7 +959,7 @@ class attendance {
'context' => $this->context, 'context' => $this->context,
'other' => $params)); 'other' => $params));
$event->add_record_snapshot('course_modules', $this->cm); $event->add_record_snapshot('course_modules', $this->cm);
$event->add_record_snapshot('attendance', $this); $event->add_record_snapshot('attendance_sessions', $session);
$event->trigger(); $event->trigger();
$group = 0; $group = 0;
@ -1403,7 +1394,6 @@ class attendance {
'context' => $this->context, 'context' => $this->context,
'other' => array('info' => implode(', ', $sessionsids)))); 'other' => array('info' => implode(', ', $sessionsids))));
$event->add_record_snapshot('course_modules', $this->cm); $event->add_record_snapshot('course_modules', $this->cm);
$event->add_record_snapshot('attendance', $this);
$event->trigger(); $event->trigger();
} }
@ -1416,23 +1406,47 @@ class attendance {
$sess->duration = $duration; $sess->duration = $duration;
$sess->timemodified = $now; $sess->timemodified = $now;
$DB->update_record('attendance_sessions', $sess); $DB->update_record('attendance_sessions', $sess);
}
$sessions->close();
$event = \mod_attendance\event\session_duration_updated::create(array( $event = \mod_attendance\event\session_duration_updated::create(array(
'objectid' => $this->id, 'objectid' => $this->id,
'context' => $this->context, 'context' => $this->context,
'other' => array('info' => implode(', ', $sessionsids)))); 'other' => array('info' => implode(', ', $sessionsids))));
$event->add_record_snapshot('course_modules', $this->cm); $event->add_record_snapshot('course_modules', $this->cm);
$event->add_record_snapshot('attendance', $this); $event->add_record_snapshot('attendance_sessions', $sess);
$event->trigger(); $event->trigger();
} }
$sessions->close();
}
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; 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) { public function add_status($acronym, $description, $grade) {
global $DB; global $DB;
@ -1443,27 +1457,38 @@ class attendance {
$rec->acronym = $acronym; $rec->acronym = $acronym;
$rec->description = $description; $rec->description = $description;
$rec->grade = $grade; $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( $event = \mod_attendance\event\status_added::create(array(
'objectid' => $this->id, 'objectid' => $this->id,
'context' => $this->context, 'context' => $this->context,
'other' => array('acronym' => $acronym, 'description' => $description, 'grade' => $grade))); 'other' => array('acronym' => $acronym, 'description' => $description, 'grade' => $grade)));
$event->add_record_snapshot('course_modules', $this->cm); $event->add_record_snapshot('course_modules', $this->cm);
$event->add_record_snapshot('attendance', $this); $event->add_record_snapshot('attendance_statuses', $rec);
$event->trigger(); $event->trigger();
} else { } else {
print_error('cantaddstatus', 'attendance', $this->url_preferences()); 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 $status
* @param string $acronym
* @param string $description
* @param int $grade
* @param bool $visible
*/
public function update_status($status, $acronym, $description, $grade, $visible) {
global $DB; global $DB;
$updated = array(); $updated = array();
$status = new stdClass();
$status->id = $statusid;
if ($acronym) { if ($acronym) {
$status->acronym = $acronym; $status->acronym = $acronym;
$updated[] = $acronym; $updated[] = $acronym;
@ -1487,7 +1512,7 @@ class attendance {
'context' => $this->context, 'context' => $this->context,
'other' => array('acronym' => $acronym, 'description' => $description, 'grade' => $grade, 'updated' => implode(' ', $updated)))); 'other' => array('acronym' => $acronym, 'description' => $description, 'grade' => $grade, 'updated' => implode(' ', $updated))));
$event->add_record_snapshot('course_modules', $this->cm); $event->add_record_snapshot('course_modules', $this->cm);
$event->add_record_snapshot('attendance', $this); $event->add_record_snapshot('attendance_statuses', $status);
$event->trigger(); $event->trigger();
} }

19
preferences.php

@ -62,13 +62,14 @@ switch ($att->pageparams->action) {
} }
$confirm = optional_param('confirm', null, PARAM_INT); $confirm = optional_param('confirm', null, PARAM_INT);
$statuses = $att->get_statuses(false);
$status = $statuses[$att->pageparams->statusid];
if (isset($confirm)) { if (isset($confirm)) {
$att->remove_status($att->pageparams->statusid); $att->remove_status($status);
redirect($att->url_preferences(), get_string('statusdeleted', 'attendance')); 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 = get_string('deletecheckfull', '', get_string('variable', 'attendance'));
$message .= str_repeat(html_writer::empty_tag('br'), 2); $message .= str_repeat(html_writer::empty_tag('br'), 2);
$message .= $status->acronym.': '. $message .= $status->acronym.': '.
@ -80,18 +81,24 @@ switch ($att->pageparams->action) {
echo $OUTPUT->footer(); echo $OUTPUT->footer();
exit; exit;
case att_preferences_page_params::ACTION_HIDE: 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; break;
case att_preferences_page_params::ACTION_SHOW: 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; break;
case att_preferences_page_params::ACTION_SAVE: case att_preferences_page_params::ACTION_SAVE:
$acronym = required_param_array('acronym', PARAM_MULTILANG); $acronym = required_param_array('acronym', PARAM_MULTILANG);
$description = required_param_array('description', PARAM_MULTILANG); $description = required_param_array('description', PARAM_MULTILANG);
$grade = required_param_array('grade', PARAM_INT); $grade = required_param_array('grade', PARAM_INT);
$statuses = $att->get_statuses(false);
foreach ($acronym as $id => $v) { 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) { if ($att->grade > 0) {
att_update_all_users_grades($att->id, $att->course, $att->context, $cm); att_update_all_users_grades($att->id, $att->course, $att->context, $cm);

Loading…
Cancel
Save