Implement data privacy provider.
[moodle-mod_attendance.git] / absentee.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17 /**
18 * Attendance course summary report.
19 *
20 * @package mod_attendance
21 * @copyright 2017 onwards Dan Marsden http://danmarsden.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25 require_once('../../config.php');
26 require_once($CFG->libdir.'/adminlib.php');
27 require_once($CFG->dirroot.'/mod/attendance/lib.php');
28 require_once($CFG->dirroot.'/mod/attendance/locallib.php');
29 require_once($CFG->libdir.'/tablelib.php');
30 require_once($CFG->libdir.'/coursecatlib.php');
31
32 $category = optional_param('category', 0, PARAM_INT);
33 $attendancecm = optional_param('id', 0, PARAM_INT);
34 $download = optional_param('download', '', PARAM_ALPHA);
35 $sort = optional_param('tsort', 'timesent', PARAM_ALPHA);
36
37 if (!empty($category)) {
38 $context = context_coursecat::instance($category);
39 $coursecat = coursecat::get($category);
40 $courses = $coursecat->get_courses(array('recursive' => true, 'idonly' => true));
41 $PAGE->set_category_by_id($category);
42 require_login();
43 } else if (!empty($attendancecm)) {
44 $cm = get_coursemodule_from_id('attendance', $attendancecm, 0, false, MUST_EXIST);
45 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
46 $att = $DB->get_record('attendance', array('id' => $cm->instance), '*', MUST_EXIST);
47 $courses = array($course->id);
48 $context = context_module::instance($cm->id);
49 require_login($course, false, $cm);
50 } else {
51 admin_externalpage_setup('managemodules');
52 $context = context_system::instance();
53 $courses = array(); // Show all courses.
54 }
55 // Check permissions.
56 require_capability('mod/attendance:viewreports', $context);
57
58 $exportfilename = 'attendance-absentee.csv';
59
60 $PAGE->set_url('/mod/attendance/absentee.php', array('category' => $category, 'id' => $attendancecm));
61
62 $PAGE->set_heading($SITE->fullname);
63
64 $table = new flexible_table('attendanceabsentee');
65 $table->define_baseurl($PAGE->url);
66
67 if (!$table->is_downloading($download, $exportfilename)) {
68 if (!empty($attendancecm)) {
69 $pageparams = new mod_attendance_sessions_page_params();
70 $att = new mod_attendance_structure($att, $cm, $course, $context, $pageparams);
71 $output = $PAGE->get_renderer('mod_attendance');
72 $tabs = new attendance_tabs($att, attendance_tabs::TAB_ABSENTEE);
73 echo $output->header();
74 echo $output->heading(get_string('attendanceforthecourse', 'attendance').' :: ' .format_string($course->fullname));
75 echo $output->render($tabs);
76 } else {
77 echo $OUTPUT->header();
78 echo $OUTPUT->heading(get_string('absenteereport', 'mod_attendance'));
79 if (empty($category)) {
80 // Only show tabs if displaying via the admin page.
81 $tabmenu = attendance_print_settings_tabs('absentee');
82 echo $tabmenu;
83 }
84 }
85
86 }
87
88 $table->define_columns(array('coursename', 'aname', 'userid', 'numtakensessions', 'percent', 'timesent'));
89 $table->define_headers(array(get_string('course'),
90 get_string('pluginname', 'attendance'),
91 get_string('user'),
92 get_string('takensessions', 'attendance'),
93 get_string('averageattendance', 'attendance'),
94 get_string('triggered', 'attendance')));
95 $table->sortable(true);
96 $table->set_attribute('cellspacing', '0');
97 $table->set_attribute('class', 'generaltable generalbox');
98 $table->show_download_buttons_at(array(TABLE_P_BOTTOM));
99 $table->setup();
100
101 // Work out direction of sort required.
102 $sortcolumns = $table->get_sort_columns();
103 // Now do sorting if specified.
104
105 $orderby = ' ORDER BY percent ASC';
106 if (!empty($sort)) {
107 $direction = ' DESC';
108 if (!empty($sortcolumns[$sort]) && $sortcolumns[$sort] == SORT_ASC) {
109 $direction = ' ASC';
110 }
111 $orderby = " ORDER BY $sort $direction";
112
113 }
114
115 $records = attendance_get_users_to_notify($courses, $orderby);
116 foreach ($records as $record) {
117 if (!$table->is_downloading($download, $exportfilename)) {
118 $url = new moodle_url('/mod/attendance/index.php', array('id' => $record->courseid));
119 $name = html_writer::link($url, $record->coursename);
120 } else {
121 $name = $record->coursename;
122 }
123 $url = new moodle_url('/mod/attendance/view.php', array('studentid' => $record->userid,
124 'id' => $record->cmid, 'view' => ATT_VIEW_ALL));
125 $attendancename = html_writer::link($url, $record->aname);
126
127 $username = html_writer::link($url, fullname($record));
128 $percent = round($record->percent * 100)."%";
129 $timesent = "-";
130 if (!empty($record->timesent)) {
131 $timesent = userdate($record->timesent);
132 }
133
134 $table->add_data(array($name, $attendancename, $username, $record->numtakensessions, $percent, $timesent));
135 }
136 $table->finish_output();
137
138 if (!$table->is_downloading()) {
139 echo $OUTPUT->footer();
140 }