-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextdownload.php
More file actions
105 lines (90 loc) · 3.65 KB
/
textdownload.php
File metadata and controls
105 lines (90 loc) · 3.65 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
<?php
// This file is part of Stateful
//
// Stateful 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.
//
// Stateful 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 Stateful. If not, see <http://www.gnu.org/licenses/>.
/**
* This script serves text files generated on demand by rendering CASText
* of a given question with a given seed. For generated data transfer needs.
*
* @copyright 2022 Aalto University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../config.php');
require_once(__DIR__ . '/../stack/vle_specific.php');
require_once(__DIR__ . '/stacklib.php');
global $CFG;
require_once($CFG->libdir . '/questionlib.php');
require_login();
// Start by checking that we have what we need.
if (!(isset($_GET['qaid']) && isset($_GET['id']) && isset($_GET['name']))) {
header('HTTP/1.0 404 Not Found');
header('Content-Type: text/plain;charset=UTF-8');
echo 'Incomplete request';
die();
}
// Extract the details we need for this action.
$qaid = $_GET['qaid'];
$tdid = $_GET['id'];
$name = $_GET['name'];
// Check that they are of the correct type.
if (!is_numeric($qaid) || !is_numeric($tdid)) {
header('HTTP/1.0 404 Not Found');
header('Content-Type: text/plain;charset=UTF-8');
echo 'Incomplete request';
die();
}
// So what we are doing is that we need to instanttiate the question
// of that attempt to have correct seed and then we need to render
// that specific td-file and serve it out with a specific name.
$dm = new question_engine_data_mapper();
$qa = $dm->load_question_attempt($qaid);
$question = $qa->get_question();
// The process of initting is not exactly complete here, so we push
// in some details normally coming from elsewhere.
$sidelinedstate = new qbehaviour_stateful_state_storage($qa,
$question->get_state_variable_identifiers());
$question->set_state($sidelinedstate);
$question->castextprocessor = new stateful_castext2_default_processor($qa);
$question->apply_attempt_state($qa->get_last_step());
if (!stack_user_can_view_question($question)) {
header('HTTP/1.0 403 Forbidden');
header('Content-Type: text/plain;charset=UTF-8');
echo 'This question is not accessible for the active user';
die();
}
// Unlock session during instantiation.
\core\session\manager::write_close();
// Make sure that the cache is good, as this is one of those places where
// the identifier for the cached item comes from outside we cannot
// cannot directly ask for it as that would allow people to force the cache
// to be regenerated.
// This will generate the cache if it is missing, which is highly unlikely.
$question->get_compiled('random');
if (!isset($question->compiledcache['td-' . $tdid])) {
header('HTTP/1.0 404 Not Found');
header('Content-Type: text/plain;charset=UTF-8');
echo 'No such textdownload object in this question';
die();
}
// Render it.
$content = $question->render('td-' . $tdid);
// Now pick some sensible headers.
header('HTTP/1.0 200 OK');
header("Content-Disposition: attachment; filename=\"$name\"");
if (strripos($name, '.csv') === strlen($name) - 4) {
header('Content-Type: text/csv;charset=UTF-8');
} else {
header('Content-Type: text/plain;charset=UTF-8');
}
echo($content);