Repository navigation
Expand file tree
/
Copy pathdownloads.php
More file actions
390 lines (374 loc) · 17.3 KB
/
Copy pathdownloads.php
File metadata and controls
390 lines (374 loc) · 17.3 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/**
* Exports the simplescheduler data in spreadsheet format.
*
* @package mod
* @subpackage simplescheduler
* @copyright 2013 Nathan White and others (see README.txt)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/************************************ ODS (OpenOffice Sheet) download generator ******************************/
if ($action == 'downloadods'){
require_once($CFG->libdir."/odslib.class.php");
/// Calculate file name
$downloadfilename = clean_filename("{$course->shortname}_{$simplescheduler->name}.ods");
/// Creating a workbook
$workbook = new MoodleODSWorkbook("-");
}
/************************************ Excel download generator ***********************************************/
if ($action == 'downloadexcel'){
require_once($CFG->libdir."/excellib.class.php");
/// Calculate file name
$downloadfilename = clean_filename(shorten_text("{$course->shortname}_{$simplescheduler->name}", 20).".xls");
/// Creating a workbook
$workbook = new MoodleExcelWorkbook("-");
}
if($action == 'downloadexcel' || $action == 'downloadods'){
/// Sending HTTP headers
$workbook->send($downloadfilename);
/// Prepare data
$sql = "
SELECT DISTINCT
u.id,
u.firstname,
u.lastname,
u.email,
u.department
FROM
{simplescheduler_slots} s,
{user} u
WHERE
s.teacherid = u.id AND
simpleschedulerid = ?
";
$teachers = $DB->get_records_sql($sql, array($simplescheduler->id));
$slots = $DB->get_records('simplescheduler_slots', array('simpleschedulerid' => $simplescheduler->id), 'starttime', 'id, starttime, duration, exclusivity, teacherid, hideuntil');
if ($subaction == 'singlesheet'){
/// Adding the worksheet
$myxls['singlesheet'] =& $workbook->add_worksheet($COURSE->shortname.': '.format_string($simplescheduler->name));
$myxls['singlesheet']->write_string(0,0,get_string('date', 'simplescheduler'));
$myxls['singlesheet']->write_string(0,1,get_string('starttime', 'simplescheduler'));
$myxls['singlesheet']->write_string(0,2,get_string('endtime', 'simplescheduler'));
$myxls['singlesheet']->write_string(0,3,get_string('slottype', 'simplescheduler'));
$myxls['singlesheet']->write_string(0,4,simplescheduler_get_teacher_name($simplescheduler));
$myxls['singlesheet']->write_string(0,5,get_string('students', 'simplescheduler'));
$myxls['singlesheet']->set_column(0,0,26);
$myxls['singlesheet']->set_column(1,2,15);
$myxls['singlesheet']->set_column(3,3,10);
$myxls['singlesheet']->set_column(4,4,20);
$myxls['singlesheet']->set_column(5,5,60);
$f = $workbook->add_format(array('bold' => 1));
$myxls['singlesheet']->set_row(0,13,$f);
}
elseif ($subaction == 'byteacher') {
/// Adding the worksheets
if ($teachers){
foreach($teachers as $teacher){
$myxls[$teacher->id] =& $workbook->add_worksheet(fullname($teacher));
/// Print names of all the fields
$myxls[$teacher->id]->write_string(0,0,get_string('date', 'simplescheduler'));
$myxls[$teacher->id]->write_string(0,1,get_string('starttime', 'simplescheduler'));
$myxls[$teacher->id]->write_string(0,2,get_string('endtime', 'simplescheduler'));
$myxls[$teacher->id]->write_string(0,3,get_string('slottype', 'simplescheduler'));
$myxls[$teacher->id]->write_string(0,4,get_string('students', 'simplescheduler'));
$myxls[$teacher->id]->set_column(0,0,26);
$myxls[$teacher->id]->set_column(1,2,15);
$myxls[$teacher->id]->set_column(3,3,10);
$myxls[$teacher->id]->set_column(4,4,60);
$f = $workbook->add_format(array('bold' => 1));
$myxls[$teacher->id]->set_row(0,13,$f);
}
}
}
/// Print all the lines of data.
$i = array();
if (!empty($slots)) {
foreach ($slots as $slot) {
switch($subaction){
case 'byteacher':
$sheetname = $slot->teacherid ;
break;
default :
$sheetname = $subaction;
}
$appointments = $DB->get_records('simplescheduler_appointment', array('slotid' => $slot->id));
/// fill slot data
$datestart = simplescheduler_userdate($slot->starttime,1);
$timestart = simplescheduler_usertime($slot->starttime,1);
$timeend = simplescheduler_usertime($slot->starttime + $slot->duration * 60,1);
$i[$sheetname] = @$i[$sheetname] + 1;
$myxls[$sheetname]->write_string($i[$sheetname],0,$datestart);
$myxls[$sheetname]->write_string($i[$sheetname],1,$timestart);
$myxls[$sheetname]->write_string($i[$sheetname],2,$timeend);
switch($slot->exclusivity){
case 0 :
$myxls[$sheetname]->write_string($i[$sheetname], 3, get_string('unlimited', 'simplescheduler'));
break;
case 1 :
$myxls[$sheetname]->write_string($i[$sheetname], 3, get_string('exclusive', 'simplescheduler'));
break;
default :
$remaining = ($slot->exclusivity - count($appointments));
$myxls[$sheetname]->write_string($i[$sheetname], 3, get_string('limited', 'simplescheduler',$remaining));
}
$j = 4;
if ($subaction == 'singlesheet'){
$myxls[$sheetname]->write_string($i[$sheetname], $j, fullname($teachers[$slot->teacherid]));
$j++;
}
if (!empty($appointments)) {
$appointedlist = '';
foreach($appointments as $appointment){
$user = $DB->get_record('user', array('id' => $appointment->studentid), 'id,firstname,lastname');
$user->lastname = strtoupper($user->lastname);
$appointedlist[] = fullname($user);
}
$myxls[$sheetname]->write_string($i[$sheetname], $j, implode(',', $appointedlist));
}
}
}
/// Close the workbook
$workbook->close();
exit;
}
/********************************************* csv generator : get parms ************************************/
if ($action == 'dodownloadcsv'){
?>
<center>
<?php
echo $OUTPUT->heading(get_string('csvparms', 'simplescheduler'));
echo $OUTPUT->box_start()
?>
<form name="csvparms" method="POST" action="view.php" target="_blank">
<input type="hidden" name="id" value="<?php p($cm->id) ?>" />
<input type="hidden" name="what" value="downloadcsv" />
<input type="hidden" name="page" value="<?php p($page) ?>" />
<input type="hidden" name="subaction" value="<?php p($subaction) ?>" />
<table>
<tr>
<td align="right" valign="top"><b><?php print_string('csvrecordseparator','simplescheduler') ?>:</b></td>
<td align="left" valign="top">
<select name="csvrecordseparator">
<option value="CR">[CR] (\r) - OLD MAC</option>
<option value="CRLF" >[CR][LF] (\r\n) - DOS/WINDOWS</option>
<option value="LF" selected="selected">[LF] (\n) - LINUX/UNIX</option>
</select>
</td>
</tr>
<tr>
<td align="right" valign="top"><b><?php print_string('csvfieldseparator','simplescheduler') ?>:</b></td>
<td align="left" valign="top">
<select name="csvfieldseparator">
<option value="TAB">[TAB]</option>
<option value=";">;</option>
<option value=",">,</option>
</select>
</td>
</tr>
<tr>
<td align="right" valign="top"><b><?php print_string('csvencoding','simplescheduler') ?>:</b></td>
<td align="left" valign="top">
<select name="csvencoding">
<option value="UTF-16">UTF-16</option>
<option value="UTF-8" selected="selected" >UTF-8</option>
<option value="UTF-7">UTF-7</option>
<option value="ASCII">ASCII</option>
<option value="EUC-JP">EUC-JP</option>
<option value="SJIS">SJIS</option>
<option value="eucJP-win">eucJP-win</option>
<option value="SJIS-win">SJIS-win</option>
<option value="ISO-2022-JP">ISO-2022-JP</option>
<option value="JIS">JIS</option>
<option value="ISO-8859-1">ISO-8859-1</option>
<option value="ISO-8859-2">ISO-8859-2</option>
<option value="ISO-8859-3">ISO-8859-3</option>
<option value="ISO-8859-4">ISO-8859-4</option>
<option value="ISO-8859-5">ISO-8859-5</option>
<option value="ISO-8859-6">ISO-8859-6</option>
<option value="ISO-8859-7">ISO-8859-7</option>
<option value="ISO-8859-8">ISO-8859-8</option>
<option value="ISO-8859-9">ISO-8859-9</option>
<option value="ISO-8859-10">ISO-8859-10</option>
<option value="ISO-8859-13">ISO-8859-13</option>
<option value="ISO-8859-14">ISO-8859-14</option>
<option value="ISO-8859-15">ISO-8859-15</option>
<option value="BASE64">BASE64</option>
</select>
</td>
</tr>
<tr>
<td align="center" valign="top" colspan="2">
<input type="submit" name="go_btn" value="<?php print_string('continue') ?>" />
</td>
</tr>
</table>
</form>
<?php
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
exit;
}
/********************************************* csv generator : generate **********************************/
if ($action == 'downloadcsv'){
$ENDLINES = array( 'LF' => "\n", 'CRLF' => "\r\n", 'CR' => "\r");
$csvrecordseparator = $ENDLINES[required_param('csvrecordseparator', PARAM_TEXT)];
$csvfieldseparator = required_param('csvfieldseparator', PARAM_TEXT);
if ($csvfieldseparator == 'TAB'){
$csvfieldseparator = "\t";
}
$csvencoding = required_param('csvencoding', PARAM_CLEAN);
$downloadfilename = clean_filename(shorten_text("{$course->shortname}_{$simplescheduler->name}", 20).'.csv');
/// sending headers
header("Content-Type:text/csv\n\n");
header("Content-Disposition: attachment; filename=$downloadfilename");
/// Prepare data
$sql = "
SELECT DISTINCT
u.id,
u.firstname,
u.lastname,
u.email,
u.department
FROM
{simplescheduler_slots} s,
{user} u
WHERE
s.teacherid = u.id AND
simpleschedulerid = ?
";
$teachers = $DB->get_records_sql($sql, array($simplescheduler->id));
$stream = '';
$slots = $DB->get_records('simplescheduler_slots', array('simpleschedulerid' => $simplescheduler->id), 'starttime', 'id, starttime, duration, exclusivity, teacherid, hideuntil');
if ($subaction == 'slots'){
/// Making title line
$stream .= get_string('date', 'simplescheduler') . $csvfieldseparator;
$stream .= get_string('starttime', 'simplescheduler') . $csvfieldseparator;
$stream .= get_string('endtime', 'simplescheduler') . $csvfieldseparator;
$stream .= get_string('slottype', 'simplescheduler') . $csvfieldseparator;
$stream .= get_string('students', 'simplescheduler') .$csvrecordseparator;
/// Print all the lines of data.
if (!empty($slots)) {
foreach ($slots as $slot) {
$appointments = $DB->get_records('simplescheduler_appointment', array('slotid'=>$slot->id));
/// fill slot data
$datestart = simplescheduler_userdate($slot->starttime,1);
$timestart = simplescheduler_usertime($slot->starttime,1);
$timeend = simplescheduler_usertime($slot->starttime + $slot->duration * 60,1);
$stream .= $datestart . $csvfieldseparator;
$stream .= $timestart . $csvfieldseparator;
$stream .= $timeend . $csvfieldseparator;
switch($slot->exclusivity){
case 0 :
$stream .= get_string('unlimited', 'simplescheduler') . $csvfieldseparator;
break;
case 1 :
$stream .= get_string('exclusive', 'simplescheduler') . $csvfieldseparator;
break;
default :
$stream .= get_string('limited', 'simplescheduler').' '.($slot->exclusivity - count($appointments)) . $csvfieldseparator;
}
if (!empty($appointments)) {
$appointedlist = '';
foreach($appointments as $appointment){
$user = $DB->get_record('user', array('id' => $appointment->studentid), 'id,firstname,lastname');
$user->lastname = strtoupper($user->lastname);
$appointedlist[] = fullname($user);
}
$stream .= implode(',', $appointedlist);
}
$stream .= $csvrecordseparator;
}
}
}
echo mb_convert_encoding($stream, $csvencoding, 'UTF-8');
exit;
}
/*********************************************** download selection **********************************/
else{
$strdownloadexcelsingle = get_string('strdownloadexcelsingle', 'simplescheduler');
$strdownloadexcelteachers = get_string('strdownloadexcelteachers', 'simplescheduler', format_string(simplescheduler_get_teacher_name($simplescheduler)));
$strdownloadodssingle = get_string('strdownloadodssingle', 'simplescheduler');
$strdownloadodsteachers = get_string('strdownloadodsteachers', 'simplescheduler', format_string(simplescheduler_get_teacher_name($simplescheduler)));
$strdownloadcsvslots = get_string('strdownloadcsvslots', 'simplescheduler');
$strdownloadcsvgrades = get_string('strdownloadcsvgrades', 'simplescheduler');
?>
<center>
<?php echo $OUTPUT->heading(get_string('downloads', 'simplescheduler')) ?>
<hr width="60%" class="separator"/>
<table>
<tr>
<td>
<form action="view.php" method="post" name="deleteallform" target="_blank">
<input type="hidden" name="what" value="downloadexcel" />
<input type="hidden" name="subaction" value="singlesheet" />
<input type="hidden" name="id" value="<?php p($cm->id) ?>" />
<input type="submit" name="go_btn" value="<?php echo $strdownloadexcelsingle ?>" style="width:240px"/>
</form>
</td>
</tr>
<tr>
<td>
<form action="view.php" method="post" name="deleteallform" target="_blank">
<input type="hidden" name="what" value="downloadexcel" />
<input type="hidden" name="subaction" value="byteacher" />
<input type="hidden" name="id" value="<?php p($cm->id) ?>" />
<input type="submit" name="go_btn" value="<?php echo $strdownloadexcelteachers ?>" style="width:240px"/>
</form>
</td>
</tr>
<tr>
<td>
<form action="view.php" method="post" name="deleteallform" target="_blank">
<input type="hidden" name="what" value="downloadods" />
<input type="hidden" name="subaction" value="singlesheet" />
<input type="hidden" name="id" value="<?php p($cm->id) ?>" />
<input type="submit" name="go_btn" value="<?php echo $strdownloadodssingle ?>" style="width:240px"/>
</form>
</td>
</tr>
<tr>
<td>
<form action="view.php" method="post" name="deleteallform" target="_blank">
<input type="hidden" name="what" value="downloadods" />
<input type="hidden" name="subaction" value="byteacher" />
<input type="hidden" name="id" value="<?php p($cm->id) ?>" />
<input type="submit" name="go_btn" value="<?php echo $strdownloadodsteachers ?>" style="width:240px"/>
</form>
</td>
</tr>
<tr>
<td>
<form action="view.php" method="post" name="deleteallform">
<input type="hidden" name="what" value="dodownloadcsv" />
<input type="hidden" name="subaction" value="slots" />
<input type="hidden" name="id" value="<?php p($cm->id) ?>" />
<input type="submit" name="go_btn" value="<?php echo $strdownloadcsvslots ?>" style="width:240px"/>
</form>
</td>
</tr>
<?php
if ($simplescheduler->scale != 0){
?>
<tr>
<td>
<form action="view.php" method="post" name="deleteallform">
<input type="hidden" name="what" value="dodownloadcsv" />
<input type="hidden" name="subaction" value="grades" />
<input type="hidden" name="id" value="<?php p($cm->id) ?>" />
<input type="submit" name="go_btn" value="<?php echo $strdownloadcsvgrades ?>" style="width:240px"/>
</form>
</td>
</tr>
<tr>
<td>
<br/><?php print_string('exportinstructions','simplescheduler') ?>
</td>
</tr>
<?php
}
?>
</table>
</center>
<?php
}
?>