Implement data privacy provider.
[moodle-mod_attendance.git] / coursesummary.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 $download = optional_param('download', '', PARAM_ALPHA);
34 $sort = optional_param('tsort', '', PARAM_ALPHA);
35 $fromcourse = optional_param('fromcourse', 0, PARAM_INT);
36
37 $admin = false;
38 if (empty($fromcourse)) {
39 $admin = true;
40 admin_externalpage_setup('managemodules');
41 } else {
42 require_login($fromcourse);
43 }
44
45 if (empty($category)) {
46 $context = context_system::instance();
47 $courses = array(); // Show all courses.
48 } else {
49 $context = context_coursecat::instance($category);
50 $coursecat = coursecat::get($category);
51 $courses = $coursecat->get_courses(array('recursive' => true, 'idonly' => true));
52 }
53 // Check permissions.
54 require_capability('mod/attendance:viewsummaryreports', $context);
55
56 $exportfilename = 'attendancecoursesummary.csv';
57
58 $PAGE->set_url('/mod/attendance/coursesummary.php', array('category' => $category));
59
60 $PAGE->set_heading($SITE->fullname);
61
62 $table = new flexible_table('attendancecoursesummary');
63 $table->define_baseurl($PAGE->url);
64
65 if (!$table->is_downloading($download, $exportfilename)) {
66 echo $OUTPUT->header();
67 $heading = get_string('coursesummary', 'mod_attendance');
68 if (!empty($category)) {
69 $heading .= " (".$coursecat->name.")";
70 }
71 echo $OUTPUT->heading($heading);
72 if ($admin) {
73 // Only show tabs if displaying via the admin page.
74 $tabmenu = attendance_print_settings_tabs('coursesummary');
75 echo $tabmenu;
76 }
77 $url = new moodle_url('/mod/attendance/coursesummary.php', array('category' => $category, 'fromcourse' => $fromcourse));
78
79 if ($admin) {
80 $options = coursecat::make_categories_list('mod/attendance:viewsummaryreports');
81 echo $OUTPUT->single_select($url, 'category', $options, $category);
82 }
83
84 }
85
86 $table->define_columns(array('course', 'percentage'));
87 $table->define_headers(array(get_string('course'),
88 get_string('averageattendance', 'attendance')));
89 $table->sortable(true);
90 $table->no_sorting('course');
91 $table->set_attribute('cellspacing', '0');
92 $table->set_attribute('class', 'generaltable generalbox');
93 $table->show_download_buttons_at(array(TABLE_P_BOTTOM));
94 $table->setup();
95
96 // Work out direction of sort required.
97 $sortcolumns = $table->get_sort_columns();
98 // Now do sorting if specified.
99
100 $orderby = ' ORDER BY percentage ASC';
101 if (!empty($sort)) {
102 $direction = ' DESC';
103 if (!empty($sortcolumns[$sort]) && $sortcolumns[$sort] == SORT_ASC) {
104 $direction = ' ASC';
105 }
106 $orderby = " ORDER BY $sort $direction";
107
108 }
109
110 $records = attendance_course_users_points($courses, $orderby);
111 foreach ($records as $record) {
112 if (!$table->is_downloading($download, $exportfilename)) {
113 $url = new moodle_url('/mod/attendance/index.php', array('id' => $record->courseid));
114 $name = html_writer::link($url, $record->coursename);
115 } else {
116 $name = $record->coursename;
117 }
118 $table->add_data(array($name, round($record->percentage * 100)."%"));
119 }
120 $table->finish_output();
121
122 if (!$table->is_downloading()) {
123 echo $OUTPUT->footer();
124 }