This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
export.php
206 lines (175 loc) · 7.1 KB
/
export.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Export attendance sessions
*
* @package mod
* @subpackage attforblock
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__).'/../../config.php');
require_once(dirname(__FILE__).'/locallib.php');
require_once(dirname(__FILE__).'/export_form.php');
require_once(dirname(__FILE__).'/renderables.php');
require_once(dirname(__FILE__).'/renderhelpers.php');
$id = required_param('id', PARAM_INT);
$cm = get_coursemodule_from_id('attforblock', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$att = $DB->get_record('attforblock', array('id' => $cm->instance), '*', MUST_EXIST);
require_login($course, true, $cm);
$att = new attforblock($att, $cm, $course, $PAGE->context);
$att->perm->require_export_capability();
$PAGE->set_url($att->url_export());
$PAGE->set_title($course->shortname. ": ".$att->name);
$PAGE->set_heading($course->fullname);
$PAGE->set_cacheable(true);
$PAGE->set_button($OUTPUT->update_module_button($cm->id, 'attforblock'));
$PAGE->navbar->add(get_string('export', 'quiz'));
$formparams = array('course' => $course, 'cm' => $cm, 'modcontext' => $PAGE->context);
$mform = new mod_attforblock_export_form($att->url_export(), $formparams);
if ($mform->is_submitted()) {
$formdata = $mform->get_data();
$pageparams = new att_page_with_filter_controls();
$pageparams->init($cm);
$pageparams->group = $formdata->group;
$pageparams->set_current_sesstype($formdata->group ? $formdata->group : att_page_with_filter_controls::SESSTYPE_ALL);
if (isset($formdata->includeallsessions)) {
if (isset($formdata->includenottaken)) {
$pageparams->view = ATT_VIEW_ALL;
} else {
$pageparams->view = ATT_VIEW_ALLPAST;
$pageparams->curdate = time();
}
$pageparams->init_start_end_date();
} else {
$pageparams->startdate = $formdata->sessionstartdate;
$pageparams->enddate = $formdata->sessionenddate;
}
$att->pageparams = $pageparams;
$reportdata = new attforblock_report_data($att);
if ($reportdata->users) {
$filename = clean_filename($course->shortname.'_Attendances_'.userdate(time(), '%Y%m%d-%H%M'));
$group = $formdata->group ? $reportdata->groups[$formdata->group] : 0;
$data = new stdClass;
$data->tabhead = array();
$data->course = $att->course->fullname;
$data->group = $group ? $group->name : get_string('allparticipants');
if (isset($formdata->ident['id'])) {
$data->tabhead[] = get_string('studentid', 'attforblock');
}
if (isset($formdata->ident['uname'])) {
$data->tabhead[] = get_string('username');
}
$data->tabhead[] = get_string('lastname');
$data->tabhead[] = get_string('firstname');
if (count($reportdata->sessions) > 0) {
foreach($reportdata->sessions as $sess) {
$text = userdate($sess->sessdate, get_string('strftimedmyhm', 'attforblock'));
$text .= ' ';
$text .= $sess->groupid ? $reportdata->groups[$sess->groupid]->name : get_string('commonsession', 'attforblock');
$data->tabhead[] = $text;
}
} else {
print_error('sessionsnotfound', 'attforblock', $att->url_manage());
}
if ($reportdata->gradable)
$data->tabhead[] = get_string('grade');
$i = 0;
$data->table = array();
foreach($reportdata->users as $user) {
if (isset($formdata->ident['id'])) {
$data->table[$i][] = $user->id;
}
if (isset($formdata->ident['uname'])) {
$data->table[$i][] = $user->username;
}
$data->table[$i][] = $user->lastname;
$data->table[$i][] = $user->firstname;
$cellsgenerator = new user_sessions_cells_text_generator($reportdata, $user);
$data->table[$i] = array_merge($data->table[$i], $cellsgenerator->get_cells());
if ($reportdata->gradable)
$data->table[$i][] = $reportdata->grades[$user->id].' / '.$reportdata->maxgrades[$user->id];
$i++;
}
if ($formdata->format === 'text') {
ExportToCSV($data, $filename);
} else {
ExportToTableEd($data, $filename, $formdata->format);
}
exit;
} else {
print_error('studentsnotfound', 'attendance', $att->url_manage());
}
}
$output = $PAGE->get_renderer('mod_attforblock');
$tabs = new attforblock_tabs($att, attforblock_tabs::TAB_EXPORT);
echo $output->header();
echo $output->heading(get_string('attendanceforthecourse','attforblock').' :: ' .$course->fullname);
echo $output->render($tabs);
$mform->display();
echo $OUTPUT->footer();
function ExportToTableEd($data, $filename, $format) {
global $CFG;
if ($format === 'excel') {
require_once("$CFG->libdir/excellib.class.php");
$filename .= ".xls";
$workbook = new MoodleExcelWorkbook("-");
} else {
require_once("$CFG->libdir/odslib.class.php");
$filename .= ".ods";
$workbook = new MoodleODSWorkbook("-");
}
/// Sending HTTP headers
$workbook->send($filename);
/// Creating the first worksheet
$myxls =& $workbook->add_worksheet('Attendances');
/// format types
$formatbc =& $workbook->add_format();
$formatbc->set_bold(1);
$myxls->write(0, 0, get_string('course'), $formatbc);
$myxls->write(0, 1, $data->course);
$myxls->write(1, 0, get_string('group'), $formatbc);
$myxls->write(1, 1, $data->group);
$i = 3;
$j = 0;
foreach ($data->tabhead as $cell) {
$myxls->write($i, $j++, $cell, $formatbc);
}
$i++;
$j = 0;
foreach ($data->table as $row) {
foreach ($row as $cell) {
$myxls->write($i, $j++, $cell);
}
$i++;
$j = 0;
}
$workbook->close();
}
function ExportToCSV($data, $filename) {
$filename .= ".txt";
header("Content-Type: application/download\n");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Expires: 0");
header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
header("Pragma: public");
echo get_string('course')."\t".$data->course."\n";
echo get_string('group')."\t".$data->group."\n\n";
echo implode("\t", $data->tabhead)."\n";
foreach ($data->table as $row) {
echo implode("\t", $row)."\n";
}
}