2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Attendance course summary report.
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
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');
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
);
38 if (empty($fromcourse)) {
40 admin_externalpage_setup('managemodules');
42 require_login($fromcourse);
45 if (empty($category)) {
46 $context = context_system
::instance();
47 $courses = array(); // Show all courses.
49 $context = context_coursecat
::instance($category);
50 $coursecat = coursecat
::get($category);
51 $courses = $coursecat->get_courses(array('recursive' => true
, 'idonly' => true
));
54 require_capability('mod/attendance:viewsummaryreports', $context);
56 $exportfilename = 'attendancecoursesummary.csv';
58 $PAGE->set_url('/mod/attendance/coursesummary.php', array('category' => $category));
60 $PAGE->set_heading($SITE->fullname
);
62 $table = new flexible_table('attendancecoursesummary');
63 $table->define_baseurl($PAGE->url
);
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
.")";
71 echo $OUTPUT->heading($heading);
73 // Only show tabs if displaying via the admin page.
74 $tabmenu = attendance_print_settings_tabs('coursesummary');
77 $url = new moodle_url('/mod/attendance/coursesummary.php', array('category' => $category, 'fromcourse' => $fromcourse));
80 $options = coursecat
::make_categories_list('mod/attendance:viewsummaryreports');
81 echo $OUTPUT->single_select($url, 'category', $options, $category);
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
));
96 // Work out direction of sort required.
97 $sortcolumns = $table->get_sort_columns();
98 // Now do sorting if specified.
100 $orderby = ' ORDER BY percentage ASC';
102 $direction = ' DESC';
103 if (!empty($sortcolumns[$sort]) && $sortcolumns[$sort] == SORT_ASC
) {
106 $orderby = " ORDER BY $sort $direction";
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
);
116 $name = $record->coursename
;
118 $table->add_data(array($name, round($record->percentage
* 100)."%"));
120 $table->finish_output();
122 if (!$table->is_downloading()) {
123 echo $OUTPUT->footer();