-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcsvexport.php
executable file
·167 lines (149 loc) · 6.99 KB
/
csvexport.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
<?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/>.
/**
* This file is used to export exercise attempts in csv format.
*
* Called from gview.php (View All Grades).
* Changed the code 03/10/2019 to work with removing ...lib.php function get_typergradesfull.
* Adds the mode, lesson name, timelimit, required precision, required wpm to row one of the csv output file.
* Also changed to use the lesson name, with whitespace removed, as the filename.
*
* @package mod_mootyper
* @copyright 2011 Jaka Luthar ([email protected])
* @copyright 2016 onwards AL Rachels ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
use mod_mootyper\event\export_viewallgrades_to_csv;
// Changed to this newer format 03/10/2019.
require(__DIR__ . '/../../config.php');
require_once(__DIR__ . '/lib.php');
require_login(0, true, null, false);
/**
* The function for exporting results data from this MooTyper.
*
* @param array $array All the grade data for this MooTyper
* @param string $filename
* @param string $delimiter
* @return array, false if none.
*/
function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
$mootyperid = optional_param('mootyperid', 0, PARAM_INT); // Get the id for this MooTyper.
$id = optional_param('id', 0, PARAM_INT); // Get the course module id for this MooTyper.
$coursename = optional_param('coursename', '', PARAM_RAW); // Get the course name for this MooTyper.
$mtname = optional_param('mtname', '', PARAM_TEXT); // Get the activity name for this MooTyper.
$misexam = optional_param('isexam', 0, PARAM_INT); // Get the mode for this MooTyper.
$lsnname = optional_param('lsnname', '', PARAM_RAW); // Get the lesson name for this MooTyper.
$timelimit = optional_param('timelimit', 0, PARAM_INT); // Get the timelimit for this MooTyper.
$requiredgoal = optional_param('requiredgoal', 0, PARAM_INT); // Get the required precision goal for this MooTyper.
$requiredwpm = optional_param('requiredwpm', 0, PARAM_INT); // Get the required precision goal for this MooTyper.
$scale = optional_param('scale', 0, PARAM_INT); // Get the scale for this MooTyper.
$cm = get_coursemodule_from_id('mootyper', $id, 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
// Start building a row 1 entry of the course name, activity name, mode, lesson name, required precision, and required WPM.
$coursename = get_string('course')." = ".$coursename;
$mtname = get_string('activity')." = ".$mtname;
// Continue building a row 1 entry grades csv output, based on the mode.
switch ($misexam) {
case 0:
$mtmode = get_string('fmode', 'mootyper')." = ".get_string('flesson', 'mootyper');
break;
case 1:
$mtmode = get_string('fmode', 'mootyper')." = ".get_string('isexamtext', 'mootyper');
break;
case 2:
$mtmode = get_string('fmode', 'mootyper')." = ".get_string('practice', 'mootyper');
break;
default:
$mtmode = get_string('error', 'moodle');
}
// Create a spreadsheet csv filename based on the lesson name.
$filename = get_string('flesson', 'mootyper')."_".$lsnname.'_'.gmdate("Ymd_Hi").'GMT.csv';
// Get the lesson name, required precision, and required WPM for the csv spreadsheet row 1 entry.
$lsnname = get_string('flesson', 'mootyper')." = ".$lsnname;
$timelimit = get_string('timelimit', 'mootyper')." = ".$timelimit.":00 ".get_string('minutes');
$requiredgoal = get_string('requiredgoal', 'mootyper').' = '.$requiredgoal.'%';
$requiredwpm = get_string('requiredwpm', 'mootyper').' = '.$requiredwpm;
$scale = get_string('gradenoun').' = '.$scale;
// Trigger export_viewallgrades_to_csv event.
$params = [
'objectid' => $id,
'context' => $context,
'other' => [
'coursename' => $coursename,
'mtname' => $mtname,
'lesson' => $lsnname,
'filename' => $filename,
],
];
$event = export_viewallgrades_to_csv::create($params);
$event->trigger();
// Remove all whitespace from the filename. This will remove tabs too.
$filename = preg_replace('/\s+/', '', $filename);
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="'.$filename.'";');
header("Pragma: no-cache");
header("Expires: 0");
$f = fopen('php://output', 'w');
$details = [$coursename,
$mtname,
$mtmode,
$lsnname,
$timelimit,
$requiredgoal,
$requiredwpm,
$scale,
];
$headings = [get_string('student', 'mootyper'),
get_string('fexercise', 'mootyper'),
get_string('vmistakes', 'mootyper'),
get_string('timeinseconds', 'mootyper'),
get_string('hitsperminute', 'mootyper'),
get_string('fullhits', 'mootyper'),
get_string('precision', 'mootyper'),
get_string('wpm', 'mootyper'),
get_string('gradenoun'),
get_string('timetaken', 'mootyper'),
];
fputcsv($f, $details, $delimiter);
fputcsv($f, $headings, $delimiter);
foreach ($array as $gr) {
$fields = [$gr->firstname.' '.$gr->lastname,
$gr->exercisename,
$gr->mistakes.': '.$gr->mistakedetails,
format_time($gr->timeinseconds),
format_float($gr->hitsperminute),
$gr->fullhits,
format_float($gr->precisionfield).'%',
$gr->wpm,
$gr->grade,
date(get_config('mod_mootyper', 'dateformat'), $gr->timetaken),
];
fputcsv($f, $fields, $delimiter);
}
fclose($f);
}
$mid = optional_param('mootyperid', 0, PARAM_INT);
// Fourth item determines sort order of the data.
// 2 is lastname. 10 is exercise name, ($mid, 0, 0, 10, 0).
// The function get_typer_grades_adv needs further work on sorting.
$grds = get_typer_grades_adv($mid, 0, 0, 2, 0);
// Add suspicion mark to first name for each suspicious entry.
foreach ($grds as $gr) {
if ($gr->suspicion) {
$gr->firstname = '!!!!! '.$gr->firstname;
}
}
array_to_csv_download($grds, get_string('gradesfilename', 'mootyper'));