global $CFG, $THEME, $DB;
$dbman = $DB->get_manager(); // Loads ddl manager and xmldb classes.
++ $result = true;
++
if ($oldversion < 2013082901) {
$table = new xmldb_table('attendance_sessions');
$dbman->add_field($table, $field);
}
-- upgrade_mod_savepoint(true, 2013082901, 'attendance');
++ upgrade_mod_savepoint($result, 2013082901, 'attendance');
+ }
+
+ if ($oldversion < 2013082902) {
+ // Replace values that reference old module "attforblock" to "attendance".
+ $sql = "UPDATE {grade_items}
+ SET itemmodule = 'attendance'
+ WHERE itemmodule = 'attforblock'";
+
+ $DB->execute($sql);
+
+ $sql = "UPDATE {grade_items_history}
+ SET itemmodule = 'attendance'
+ WHERE itemmodule = 'attforblock'";
+
+ $DB->execute($sql);
+
+ /*
+ * The user's custom capabilities need to be preserved due to the module renaming.
+ * Capabilities with a modifierid = 0 value are installed by default.
+ * Only update the user's custom capabilities where modifierid is not zero.
+ */
+ $sql = $DB->sql_like('capability', '?').' AND modifierid <> 0';
+ $rs = $DB->get_recordset_select('role_capabilities', $sql, array('%mod/attforblock%'));
+ foreach ($rs as $cap) {
+ $renamedcapability = str_replace('mod/attforblock', 'mod/attendance', $cap->capability);
+ $exists = $DB->record_exists('role_capabilities', array('roleid' => $cap->roleid, 'capability' => $renamedcapability));
+ if (!$exists) {
+ $DB->update_record('role_capabilities', array('id' => $cap->id, 'capability' => $renamedcapability));
+ }
+ }
+
+ // Delete old role capabilities.
+ $sql = $DB->sql_like('capability', '?');
+ $DB->delete_records_select('role_capabilities', $sql, array('%mod/attforblock%'));
+
+ // Delete old capabilities.
+ $DB->delete_records_select('capabilities', 'component = ?', array('mod_attforblock'));
+
+ upgrade_plugin_savepoint($result, 2013082902, 'mod', 'attendance');
}
- // UPGRADES from attforblock are only supported for sites that are running attforblock version 2012120700.
return $result;
}
*/
public function get_user_statuses_stat($userid, array $filters = null) {
global $DB;
-
- // Need to start setting the parameters here for the filters to work.
$params = array(
- 'aid' => $this->id,
- 'cstartdate' => $this->course->startdate,
- 'uid' => $userid);
+ 'aid' => $this->id,
+ 'cstartdate' => $this->course->startdate,
+ 'uid' => $userid);
- $processed_filters = array();
- // We test for any valid filters sent.
- if (isset($filters['enddate'])) {
- $processed_filters[] = 'ats.sessdate <= :enddate';
- $params['enddate'] = $filters['enddate'];
+ $period = '';
+ if (!empty($this->pageparams->startdate) && !empty($this->pageparams->enddate)) {
+ $period = ' AND ats.sessdate >= :sdate AND ats.sessdate < :edate ';
+ $params['sdate'] = $this->pageparams->startdate;
+ $params['edate'] = $this->pageparams->enddate;
+ }
+
+ if (!array_key_exists($userid, $this->userstatusesstat)) {
+ if ($this->get_group_mode()) {
+ $qry = "SELECT al.statusid, count(al.statusid) AS stcnt
+ FROM {attendance_log} al
+ JOIN {attendance_sessions} ats ON al.sessionid = ats.id
+ LEFT JOIN {groups_members} gm ON gm.userid = al.studentid AND gm.groupid = ats.groupid
+ WHERE ats.attendanceid = :aid AND
+ ats.sessdate >= :cstartdate AND
+ al.studentid = :uid AND
+ (ats.groupid = 0 or gm.id is NOT NULL)".$period."
+ GROUP BY al.statusid";
+ } else {
+ $qry = "SELECT al.statusid, count(al.statusid) AS stcnt
+ FROM {attendance_log} al
+ JOIN {attendance_sessions} ats
+ ON al.sessionid = ats.id
+ WHERE ats.attendanceid = :aid AND
+ ats.sessdate >= :cstartdate AND
+ al.studentid = :uid".$period."
+ GROUP BY al.statusid";
+
+ }
-
+ }
// Make the filter array into a SQL string.
if (!empty($processed_filters)) {
if ($this->pageparams->startdate && $this->pageparams->enddate) {
$where = "ats.attendanceid = :aid AND ats.sessdate >= :csdate AND
ats.sessdate >= :sdate AND ats.sessdate < :edate";
- $where2 = "ats.attendanceid = :aid2 AND ats.sessdate >= :csdate2 AND
- ats.sessdate >= :sdate2 AND ats.sessdate < :edate2 AND ats.groupid $gsql";
} else {
$where = "ats.attendanceid = :aid AND ats.sessdate >= :csdate";
- $where2 = "ats.attendanceid = :aid2 AND ats.sessdate >= :csdate2 AND ats.groupid $gsql";
}
- $sql = "SELECT ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks, ats.studentscanmark
+ // We need to add this concatination so that moodle will use it as the array index that is a string.
+ // If the array's index is a number it will not merge entries.
+ // It would be better as a UNION query butunfortunatly MS SQL does not seem to support doing a DISTINCT on a the description field.
+ $id = $DB->sql_concat(':value', 'ats.id');
+ if ($this->get_group_mode()) {
- $sql = "SELECT $id, ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks
++ $sql = "SELECT $id, ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks, ats.studentscanmark
FROM {attendance_sessions} ats
- RIGHT JOIN {attendance_log} al
+ RIGHT JOIN {attendance_log} al
ON ats.id = al.sessionid AND al.studentid = :uid
- WHERE $where
- UNION
- SELECT ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks, ats.studentscanmark
+ LEFT JOIN {groups_members} gm ON gm.userid = al.studentid AND gm.groupid = ats.groupid
+ WHERE $where AND (ats.groupid = 0 or gm.id is NOT NULL)
+ ORDER BY ats.sessdate ASC";
+ } else {
- $sql = "SELECT $id, ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks
++ $sql = "SELECT $id, ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks, ats.studentscanmark
FROM {attendance_sessions} ats
- LEFT JOIN {attendance_log} al
- ON ats.id = al.sessionid AND al.studentid = :uid2
- WHERE $where2
- ORDER BY sessdate ASC";
+ RIGHT JOIN {attendance_log} al
+ ON ats.id = al.sessionid AND al.studentid = :uid
+ WHERE $where
+ ORDER BY ats.sessdate ASC";
+ }
$params = array(
'uid' => $userid,
'csdate' => $this->course->startdate,
'sdate' => $this->pageparams->startdate,
'edate' => $this->pageparams->enddate,
- 'uid2' => $userid,
- 'aid2' => $this->id,
- 'csdate2' => $this->course->startdate,
- 'sdate2' => $this->pageparams->startdate,
- 'edate2' => $this->pageparams->enddate);
- $params = array_merge($params, $gparams);
+ 'value' => 'c');
$sessions = $DB->get_records_sql($sql, $params);
- $sql = "SELECT $id, ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks
+ // All sessions for current groups.
+
+ $groups = array_keys(groups_get_all_groups($this->course->id, $userid));
+ $groups[] = 0;
+ list($gsql, $gparams) = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED, 'gid0');
+
+ if ($this->pageparams->startdate && $this->pageparams->enddate) {
+ $where = "ats.attendanceid = :aid AND ats.sessdate >= :csdate AND
+ ats.sessdate >= :sdate AND ats.sessdate < :edate AND ats.groupid $gsql";
+ } else {
+ $where = "ats.attendanceid = :aid AND ats.sessdate >= :csdate AND ats.groupid $gsql";
+ }
+
++ $sql = "SELECT $id, ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks, ats.studentscanmark
+ FROM {attendance_sessions} ats
+ LEFT JOIN {attendance_log} al
+ ON ats.id = al.sessionid AND al.studentid = :uid
+ WHERE $where
+ ORDER BY ats.sessdate ASC";
+
+ $params = array_merge($params, $gparams);
+ $sessions = array_merge($sessions, $DB->get_records_sql($sql, $params));
+
foreach ($sessions as $sess) {
if (empty($sess->description)) {
$sess->description = get_string('nodescription', 'attendance');
$sess->timemodified = $now;
$DB->update_record('attendance_sessions', $sess);
}
- $sessions->close();
- add_to_log($this->course->id, 'attendance', 'sessions duration updated', $this->url_manage(),
- get_string('sessionsids', 'attendance').implode(', ', $sessionsids), $this->cm->id);
--
++ $sessions->close();
$this->log('sessions duration updated', $this->url_manage(), get_string('sessionsids', 'attendance').implode(', ', $sessionsids));
}
$this->log('status updated', $this->url_preferences(), implode(' ', $updated));
}
++
++ /**
++ * wrapper around {@see add_to_log()}
++ *
++ * @param string $action to be logged
++ * @param moodle_url $url absolute url, if null will be used $this->url_manage()
++ * @param mixed $info additional info, usually id in a table
++ */
++ public function log($action, moodle_url $url = null, $info = null) {
++ if (is_null($url)) {
++ $url = $this->url_manage();
++ }
++
++ if (is_null($info)) {
++ $info = $this->id;
++ }
++
++ $logurl = att_log_convert_url($url);
++ add_to_log($this->course->id, 'attforblock', $action, $logurl, $info, $this->cm->id);
++ }
}
$table->size[] = '1px';
}
-
+ if ($reportdata->sessionslog) {
+ $table->head[] = get_string('remarks', 'attendance');
+ $table->align[] = 'center';
+ $table->size[] = '200px';
+ }
+
foreach ($reportdata->users as $user) {
$row = new html_table_row();
$row->cells[] = html_writer::checkbox('user'.$user->id, 'on', false);
}
- $table->data[] = $row;
- }
-
- if ($bulkmessagecapability) { // Require that the user can bulk message users.
- // Display check boxes that will allow the user to send a message to the students that have been checked.
- $output = html_writer::empty_tag('input', array('name' => 'sesskey', 'type' => 'hidden', 'value' => sesskey()));
- $output .= html_writer::empty_tag('input', array('name' => 'formaction', 'type' => 'hidden', 'value' => 'messageselect.php'));
- $output .= html_writer::empty_tag('input', array('name' => 'id', 'type' => 'hidden', 'value' => $GLOBALS['COURSE']->id));
- $output .= html_writer::empty_tag('input', array('name' => 'returnto', 'type' => 'hidden', 'value' => s(me())));
- $output .= html_writer::table($table);
- $output .= html_writer::tag('div',
- html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('messageselectadd'))),
- array('class' => 'buttons'));
- $url = new moodle_url('/user/action_redir.php');
- return html_writer::tag('form', $output, array('action' => $url->out(), 'method' => 'post'));
- } else {
- return html_writer::table($table);
- }
+ if ($reportdata->sessionslog) {
+ if (isset($sess) && isset($reportdata->sessionslog[$user->id][$sess->id]->remarks)) {
+ $row->cells[] = $reportdata->sessionslog[$user->id][$sess->id]->remarks;
+ } else {
+ $row->cells[] = '';
+ }
+ }
$table->data[] = $row;
}
-
- return html_writer::table($table).html_writer::tag('div', get_string('users').': '.count($reportdata->users));
+ // Calculate the sum of statuses for each user
+ $statrow = new html_table_row();
+ $statrow->cells[] = '';
+ $statrow->cells[] = get_string('summary');
+ foreach ($reportdata->sessions as $sess) {
+ foreach ($reportdata->users as $user) {
+ foreach($reportdata->statuses as $status) {
+ if ($reportdata->sessionslog[$user->id][$sess->id]->statusid == $status->id) $sessionstats[$status->id]++;
+ }
+ }
+
+ $statsoutput = '<br/>';
+ foreach($reportdata->statuses as $status) {
+ $statsoutput .= "$status->description:".$sessionstats[$status->id]." <br/>";
+ }
+ $statrow->cells[] = $statsoutput;
+
+ }
+ $table->data[] = $statrow;
- return html_writer::table($table);
++
+ if ($bulkmessagecapability) { // Require that the user can bulk message users.
+ // Display check boxes that will allow the user to send a message to the students that have been checked.
+ $output = html_writer::empty_tag('input', array('name' => 'sesskey', 'type' => 'hidden', 'value' => sesskey()));
+ $output .= html_writer::empty_tag('input', array('name' => 'formaction', 'type' => 'hidden', 'value' => 'messageselect.php'));
+ $output .= html_writer::empty_tag('input', array('name' => 'id', 'type' => 'hidden', 'value' => $GLOBALS['COURSE']->id));
+ $output .= html_writer::empty_tag('input', array('name' => 'returnto', 'type' => 'hidden', 'value' => s(me())));
+ $output .= html_writer::table($table);
+ $output .= html_writer::tag('div',
+ html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('messageselectadd'))),
+ array('class' => 'buttons'));
+ $url = new moodle_url('/user/action_redir.php');
++ html_writer::tag('div', get_string('users').': '.count($reportdata->users));
+ return html_writer::tag('form', $output, array('action' => $url->out(), 'method' => 'post'));
+ } else {
++ return html_writer::table($table).html_writer::tag('div', get_string('users').': '.count($reportdata->users));
+ }
++
}
protected function render_attendance_preferences_data(attendance_preferences_data $prefdata) {