-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.php
More file actions
182 lines (159 loc) · 5.72 KB
/
index.php
File metadata and controls
182 lines (159 loc) · 5.72 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
<?php
require_once "../config.php";
// The Tsugi PHP API Documentation is available at:
// http://do1.dr-chuck.com/tsugi/phpdoc/
use \Tsugi\Core\LTIX;
use \Tsugi\Util\Net;
use \Tsugi\Core\Settings;
use \Tsugi\UI\SettingsForm;
// No parameter means we require CONTEXT, USER, and LINK
$LAUNCH = LTIX::requireData();
// If settings were updated
if ( SettingsForm::handleSettingsPost() ) {
header( 'Location: '.addSession('index.php') ) ;
return;
}
// Handle Post Data
$p = $CFG->dbprefix;
$old_code = $LAUNCH->link->getJsonKey('code', '');
$old_code = $LAUNCH->link->settingsGet('code', $old_code);
$send_grade = $LAUNCH->link->settingsGet('grade');
$match = $LAUNCH->link->settingsGet('match');
$ip = Net::getIP();
if ( isset($_POST['clear']) && $USER->instructor ) {
$PDOX->queryDie("DELETE FROM {$p}attend WHERE link_id = :LI",
array(':LI' => $LINK->id)
);
$_SESSION['success'] = 'Data cleared';
header( 'Location: '.addSession('index.php') ) ;
return;
}
if ( isset($_POST['code']) ) { // Student
if ( $old_code != $_POST['code'] ) {
$_SESSION['error'] = __('Code incorrect');
header( 'Location: '.addSession('index.php') ) ;
return;
}
if ( strlen($match) > 0 && substr($match, 0, 1) == '/' ) {
if (!preg_match($match, $ip) ) {
$_SESSION['error'] = __('IP Address '.$ip.' does not match (regex).');
header( 'Location: '.addSession('index.php') ) ;
return;
}
}
if ( strlen($match) > 0 && substr($match, 0, 1) != '/' ) {
if ( strpos($match, $ip) === false ) {
$_SESSION['error'] = __('IP Address '.$ip.' does not match.');
header( 'Location: '.addSession('index.php') ) ;
return;
}
}
// Passed all the tests..
$PDOX->queryDie("INSERT INTO {$p}attend
(link_id, user_id, ipaddr, attend, updated_at)
VALUES ( :LI, :UI, :IP, NOW(), NOW() )
ON DUPLICATE KEY UPDATE updated_at = NOW()",
array(
':LI' => $LINK->id,
':UI' => $USER->id,
':IP' => Net::getIP()
)
);
if ( $send_grade && isset($LAUNCH->link) && $LAUNCH->link ) {
if ($LAUNCH->result && $LAUNCH->result->id && $RESULT->grade < 1.0 ) {
$RESULT->gradeSend(1.0, false);
}
}
$_SESSION['success'] = __('Attendance Recorded...');
header( 'Location: '.addSession('index.php') ) ;
return;
}
// Prepare for view
if ( $USER->instructor ) {
$rows = $PDOX->allRowsDie("SELECT A.user_id,attend,A.ipaddr, displayname, email
FROM {$p}attend AS A
JOIN {$p}lti_user AS U ON U.user_id = A.user_id
WHERE link_id = :LI ORDER BY attend DESC, user_id",
array(':LI' => $LINK->id)
);
} else {
$rows = false;
}
$menu = false;
if ( $USER->instructor ) {
$menu = new \Tsugi\UI\MenuSet();
$menu->addRight(__('Settings'), '#', /* push */ false, SettingsForm::attr());
}
// Render view
$OUTPUT->header();
$OUTPUT->bodyStart();
$OUTPUT->topNav($menu);
if ( $USER->instructor ) {
echo('<div style="float:right;">');
echo('<form method="post" style="display: inline">');
echo('<input type="submit" class="btn btn-warning" name="clear" value="'.__('Clear data').'">');
echo("</form>\n");
echo('</div>');
$OUTPUT->welcomeUserCourse();
echo('<br clear="all">');
SettingsForm::start();
echo("<p>Configure the LTI Tool<p>\n");
SettingsForm::text('code',__('Code'));
SettingsForm::checkbox('grade',__('Send a grade'));
SettingsForm::text('match',__('Limit access by IP address. This can be a prefix of an IP address like "142.16.41" or if it starts with a "/" it can be a regular expression (PHP syntax)'));
echo("<p>Your current IP address is ".htmlentities(Net::getIP())."</p>\n");
SettingsForm::done();
SettingsForm::end();
}
$OUTPUT->flashMessages();
echo("<!-- Classic single-file version of the tool -->\n");
// Ask the user for the code
if ( $USER->instructor ) {
echo("<p>");
if ( strlen($old_code) < 1 ) {
echo(__("Use the settings link to configure the attendance code."));
} else {
echo(__("You can use the settings link to change the attendance code."));
}
echo("</p>\n");
} else {
echo('<form method="post">');
echo(__("Enter code:")."\n");
echo('<input type="text" name="code" value=""> ');
echo('<input type="submit" class="btn btn-normal" name="set" value="'.__('Record attendance').'"><br/>');
echo("\n</form>\n");
}
// Check the regex
if ( $USER->instructor && strlen($match) > 0 && substr($match, 0, 1) == '/' ) {
@preg_match($match, $ip);
if ( preg_last_error() != PREG_NO_ERROR ) {
echo('<p style="color:red;">Syntax error in regular expression '.htmlentities($match)."</p>\n");
}
}
if ( $rows ) {
echo('<table border="1">'."\n");
echo("<tr><th>".__("User")."</th><th>".__("Attendance")."</th><th>".__("IP Address")."</th></tr>\n");
foreach ( $rows as $row ) {
echo "<tr><td>";
echo('<a href="#" onclick="alert(\'');
echo($row['user_id']);
if ( strlen($row['email']) > 0 || strlen($row['displayname']) > 0 ) echo(' | ');
if ( strlen($row['email']) > 0 ) {
echo(htmlentities($row['email'] ?? ''));
if ( strlen($row['displayname']) > 0 ) echo(' | ');
}
if ( strlen($row['displayname']) > 0 ) {
echo(htmlentities($row['displayname'] ?? ''));
}
echo('\'); return false;">');
echo(' ******* ');
echo('</a>');
echo("</td><td>");
echo($row['attend']);
echo("</td><td>");
echo(htmlentities($row['ipaddr'] ?? ''));
echo("</td></tr>\n");
}
echo("</table>\n");
}
$OUTPUT->footer();