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 * Export attendance sessions
20 * @package mod_attendance
21 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(dirname(__FILE__
).'/../../config.php');
26 require_once(dirname(__FILE__
).'/locallib.php');
27 require_once(dirname(__FILE__
).'/export_form.php');
28 require_once(dirname(__FILE__
).'/renderables.php');
29 require_once(dirname(__FILE__
).'/renderhelpers.php');
31 $id = required_param('id', PARAM_INT
);
33 $cm = get_coursemodule_from_id('attendance', $id, 0, false
, MUST_EXIST
);
34 $course = $DB->get_record('course', array('id' => $cm->course
), '*', MUST_EXIST
);
35 $att = $DB->get_record('attendance', array('id' => $cm->instance
), '*', MUST_EXIST
);
37 require_login($course, true
, $cm);
39 $context = context_module
::instance($cm->id
);
40 require_capability('mod/attendance:export', $context);
42 $att = new mod_attendance_structure($att, $cm, $course, $context);
44 $PAGE->set_url($att->url_export());
45 $PAGE->set_title($course->shortname
. ": ".$att->name
);
46 $PAGE->set_heading($course->fullname
);
47 $PAGE->set_cacheable(true
);
48 $PAGE->set_button($OUTPUT->update_module_button($cm->id
, 'attendance'));
49 $PAGE->navbar
->add(get_string('export', 'attendance'));
51 $formparams = array('course' => $course, 'cm' => $cm, 'modcontext' => $context);
52 $mform = new mod_attendance_export_form($att->url_export(), $formparams);
54 if ($formdata = $mform->get_data()) {
56 $pageparams = new mod_attendance_page_with_filter_controls();
57 $pageparams->init($cm);
58 $pageparams->page
= 0;
59 $pageparams->group
= $formdata->group
;
60 $pageparams->set_current_sesstype($formdata->group ?
$formdata->group
: mod_attendance_page_with_filter_controls
::SESSTYPE_ALL
);
61 if (isset($formdata->includeallsessions
)) {
62 if (isset($formdata->includenottaken
)) {
63 $pageparams->view
= ATT_VIEW_ALL
;
65 $pageparams->view
= ATT_VIEW_ALLPAST
;
66 $pageparams->curdate
= time();
68 $pageparams->init_start_end_date();
70 $pageparams->startdate
= $formdata->sessionstartdate
;
71 $pageparams->enddate
= $formdata->sessionenddate
;
73 if ($formdata->selectedusers
) {
74 $pageparams->userids
= $formdata->users
;
76 $att->pageparams
= $pageparams;
78 $reportdata = new attendance_report_data($att);
79 if ($reportdata->users
) {
80 $filename = clean_filename($course->shortname
.'_Attendances_'.userdate(time(), '%Y%m%d-%H%M'));
82 $group = $formdata->group ?
$reportdata->groups
[$formdata->group
] : 0;
84 $data->tabhead
= array();
85 $data->course
= $att->course
->fullname
;
86 $data->group
= $group ?
$group->name
: get_string('allparticipants');
88 if (isset($formdata->ident
['id'])) {
89 $data->tabhead
[] = get_string('studentid', 'attendance');
91 if (isset($formdata->ident
['uname'])) {
92 $data->tabhead
[] = get_string('username');
95 $optional = array('idnumber', 'institution', 'department');
96 foreach ($optional as $opt) {
97 if (isset($formdata->ident
[$opt])) {
98 $data->tabhead
[] = get_string($opt);
102 $data->tabhead
[] = get_string('lastname');
103 $data->tabhead
[] = get_string('firstname');
104 $groupmode = groups_get_activity_groupmode($cm, $course);
105 if (!empty($groupmode)) {
106 $data->tabhead
[] = get_string('groups');
109 if (count($reportdata->sessions
) > 0) {
110 foreach ($reportdata->sessions
as $sess) {
111 $text = userdate($sess->sessdate
, get_string('strftimedmyhm', 'attendance'));
113 if (!empty($sess->groupid
) && empty($reportdata->groups
[$sess->groupid
])) {
114 $text .= get_string('deletedgroup', 'attendance');
116 $text .= $sess->groupid ?
$reportdata->groups
[$sess->groupid
]->name
: get_string('commonsession', 'attendance');
118 $data->tabhead
[] = $text;
119 if (isset($formdata->includeremarks
)) {
120 $data->tabhead
[] = ''; // Space for the remarks.
124 print_error('sessionsnotfound', 'attendance', $att->url_manage());
126 $data->tabhead
[] = get_string('takensessions', 'attendance');
127 $data->tabhead
[] = get_string('points', 'attendance');
128 $data->tabhead
[] = get_string('percentage', 'attendance');
131 $data->table
= array();
132 foreach ($reportdata->users
as $user) {
133 if (isset($formdata->ident
['id'])) {
134 $data->table
[$i][] = $user->id
;
136 if (isset($formdata->ident
['uname'])) {
137 $data->table
[$i][] = $user->username
;
140 $optionalrow = array('idnumber', 'institution', 'department');
141 foreach ($optionalrow as $opt) {
142 if (isset($formdata->ident
[$opt])) {
143 $data->table
[$i][] = $user->$opt;
147 $data->table
[$i][] = $user->lastname
;
148 $data->table
[$i][] = $user->firstname
;
149 if (!empty($groupmode)) {
151 $groupsraw = groups_get_all_groups($course->id
, $user->id
, 0, 'g.name');
153 foreach ($groupsraw as $group) {
154 $groups[] = $group->name
;;
156 $data->table
[$i][] = implode(', ', $groups);
158 $cellsgenerator = new user_sessions_cells_text_generator($reportdata, $user);
159 $data->table
[$i] = array_merge($data->table
[$i], $cellsgenerator->get_cells(isset($formdata->includeremarks
)));
161 $usersummary = $reportdata->summary
->get_taken_sessions_summary_for($user->id
);
162 $data->table
[$i][] = $usersummary->numtakensessions
;
163 $data->table
[$i][] = format_float($usersummary->takensessionspoints
, 1, true
, true
) . ' / ' .
164 format_float($usersummary->takensessionsmaxpoints
, 1, true
, true
);
165 $data->table
[$i][] = format_float($usersummary->takensessionspercentage
* 100);
170 if ($formdata->format
=== 'text') {
171 exporttocsv($data, $filename);
173 exporttotableed($data, $filename, $formdata->format
);
177 print_error('studentsnotfound', 'attendance', $att->url_manage());
181 $output = $PAGE->get_renderer('mod_attendance');
182 $tabs = new attendance_tabs($att, attendance_tabs
::TAB_EXPORT
);
183 echo $output->header();
184 echo $output->heading(get_string('attendanceforthecourse', 'attendance').' :: ' .format_string($course->fullname
));
185 echo $output->render($tabs);
189 echo $OUTPUT->footer();
192 function exporttotableed($data, $filename, $format) {
195 if ($format === 'excel') {
196 require_once("$CFG->libdir/excellib.class.php");
198 $workbook = new MoodleExcelWorkbook("-");
200 require_once("$CFG->libdir/odslib.class.php");
202 $workbook = new MoodleODSWorkbook("-");
204 // Sending HTTP headers.
205 $workbook->send($filename);
206 // Creating the first worksheet.
207 $myxls = $workbook->add_worksheet('Attendances');
209 $formatbc = $workbook->add_format();
210 $formatbc->set_bold(1);
212 $myxls->write(0, 0, get_string('course'), $formatbc);
213 $myxls->write(0, 1, $data->course
);
214 $myxls->write(1, 0, get_string('group'), $formatbc);
215 $myxls->write(1, 1, $data->group
);
219 foreach ($data->tabhead
as $cell) {
220 // Merge cells if the heading would be empty (remarks column).
222 $myxls->merge_cells($i, $j - 1, $i, $j);
224 $myxls->write($i, $j, $cell, $formatbc);
230 foreach ($data->table
as $row) {
231 foreach ($row as $cell) {
232 $myxls->write($i, $j++
, $cell);
240 function exporttocsv($data, $filename) {
243 header("Content-Type: application/download\n");
244 header("Content-Disposition: attachment; filename=\"$filename\"");
245 header("Expires: 0");
246 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
247 header("Pragma: public");
249 echo get_string('course')."\t".$data->course
."\n";
250 echo get_string('group')."\t".$data->group
."\n\n";
252 echo implode("\t", $data->tabhead
)."\n";
253 foreach ($data->table
as $row) {
254 echo implode("\t", $row)."\n";