-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrate.php
253 lines (222 loc) · 7.82 KB
/
rate.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
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
<?php
use \Tsugi\Blob\BlobUtil;
require_once "peer_util.php";
use \Tsugi\Util\U;
use \Tsugi\Core\Cache;
use \Tsugi\Core\LTIX;
use \Tsugi\Core\Result;
use \Tsugi\UI\Table;
use \Tsugi\Grades\GradeUtil;
// Sanity checks
$LAUNCH = LTIX::requireData();
$p = $CFG->dbprefix;
if ( !isset($_REQUEST['user_id']) ) {
die('user_id required');
}
$user_id = $_REQUEST['user_id'];
// Model
$row = loadAssignment();
$assn_json = null;
$assn_id = false;
if ( $row !== false ) {
$assn_json = json_decode(upgradeSubmission($row['json']));
$assn_id = $row['assn_id'];
}
if ( $assn_id == false ) {
$_SESSION['error'] = 'This assignment is not yet set up';
header( 'Location: '.addSession($url_goback) ) ;
return;
}
if ( $assn_json->gallery == 'off' ) {
die('Gallery not enabled for assignment');
}
$submit_id = false;
$submit_json = null;
$submit_row = loadSubmission($assn_id, $user_id);
if ( $submit_row !== null ) {
$submit_id = $submit_row['submit_id'];
$submit_json = json_decode($submit_row['json']);
}
if ( $submit_json === null ) {
$_SESSION['error'] = 'Unable to load submission '.$user_id;
header( 'Location: '.addSession($url_goback) ) ;
return;
}
// Load the previous rating data
$row = $PDOX->rowDie("
SELECT rating FROM {$p}peer_grade
WHERE submit_id = :SID AND user_id = :UID",
array( ":SID" => $submit_id,
":UID" => $USER->id)
);
if ( $row === FALSE ) {
$previous_rating = 0;
} else {
$previous_rating = $row['rating'];
if ( $previous_rating < 1 ) $previous_rating = 0;
}
// Handle the flag data
if ( isset($_POST['doFlag']) && isset($_POST['submit_id']) ) {
$submit_id = $_POST['submit_id']+0;
$stmt = $PDOX->queryDie(
"INSERT INTO {$p}peer_flag
(submit_id, user_id, note, created_at, updated_at)
VALUES ( :SID, :UID, :NOTE, NOW(), NOW())
ON DUPLICATE KEY UPDATE note = :NOTE, updated_at = NOW()",
array(
':SID' => $submit_id,
':UID' => $USER->id,
':NOTE' => $_POST['note'])
);
$_SESSION['success'] = "Flagged for the instructor to examine, please continue grading.";
header( 'Location: '.addSession($url_stay.'?user_id='.$user_id) ) ;
return;
}
// Handle the rating data
if ( isset($_POST['rating']) && isset($_POST['submit_id'])
&& isset($_POST['user_id']) ) {
$session_peer_submit_id = isset($_SESSION['peer_submit_id']) ? $_SESSION['peer_submit_id'] : false;
unset($_SESSION['peer_submit_id']);
if ( $assn_json->rating < 1 ) {
$_SESSION['error'] = 'Rating is not enabled on this assignment';
header( 'Location: '.addSession($url_stay.'?user_id='.$user_id) ) ;
return;
}
if ( $session_peer_submit_id != $_POST['submit_id'] ) {
$_SESSION['error'] = 'Error in submission id';
header( 'Location: '.addSession($url_goback) ) ;
return;
}
if ( U::isEmpty($_POST['rating']) ) {
$_SESSION['error'] = 'Rating is required';
header( 'Location: '.addSession($url_stay.'?user_id='.$user_id) ) ;
return;
}
$rating = $_POST['rating']+0;
if ( $rating < 0 || $rating > $assn_json->rating ) {
$_SESSION['error'] = 'Rating must be between 0 and '.$assn_json->rating;
header( 'Location: '.addSession($url_stay.'?user_id='.$user_id) ) ;
return;
}
// Check to see if user_id is correct for this submit_id
$user_id = $_POST['user_id']+0;
$submit_row = loadSubmission($assn_id, $user_id);
if ( $submit_row === null || $submit_row['submit_id'] != $_POST['submit_id']) {
$_SESSION['error'] = 'Mis-match between user_id and session_id';
header( 'Location: '.addSession($url_goback) ) ;
return;
}
// Add / update the rating for the logged in user
$stmt = $PDOX->queryReturnError(
"INSERT INTO {$p}peer_grade
(submit_id, user_id, rating, created_at, updated_at)
VALUES ( :SID, :UID, :RATING, NOW(), NOW())
ON DUPLICATE KEY UPDATE rating = :RATING, updated_at = NOW()",
array(
':SID' => $submit_id,
':UID' => $USER->id,
':RATING' => $rating)
);
if ( ! $stmt->success ) {
$_SESSION['error'] = $stmt->errorImplode;
header( 'Location: '.addSession($url_goback) ) ;
return;
}
// Update the subission's overall rating
$stmt = $PDOX->queryDie(
"UPDATE {$p}peer_submit
SET rating = IF(rating IS NULL , :RATING, (rating + :DELTA) )
WHERE submit_id = :SID",
array(
':SID' => $submit_id,
':RATING' => $rating,
':DELTA' => ($rating - $previous_rating)
)
);
header( 'Location: '.addSession($url_goback) ) ;
return;
}
// View
$OUTPUT->header();
?>
<link href="<?= U::get_rest_parent() ?>/static/prism.css" rel="stylesheet"/>
<script>
let html_loads = [];
</script>
<?php
$OUTPUT->bodyStart();
$OUTPUT->topNav();
$OUTPUT->flashMessages();
echo('<div style="border: 1px solid black; padding:3px">');
echo("<p><h4>".$assn_json->title."</h4></p>\n");
echo('<p>'.htmlent_utf8($assn_json->description)."</p>\n");
echo('</div>');
showSubmission($assn_json, $submit_json, $assn_id, $user_id);
echo('<p>'.htmlent_utf8($assn_json->grading)."</p>\n");
?>
<form method="post">
<input type="hidden" value="<?php echo($submit_id); ?>" name="submit_id">
<input type="hidden" value="<?php echo($user_id); ?>" name="user_id">
<?php if ( $assn_json->rating > 0 ) { ?>
<p>
Rate this submission: <span id="jRate"></span>
<input type="hidden" min="0" max="<?php echo($assn_json->rating); ?>" name="rating"
id="rating-input"
value="<?= ($previous_rating > 0 ? $previous_rating : '') ?>" >
</p>
<input type="submit" value="Rate" class="btn btn-primary">
<?php } ?>
<input type="submit" name="doCancel" onclick="location='<?php echo(addSession($url_goback));?>'; return false;" value="Back" class="btn btn-default">
<?php if ( $assn_json->flag ) { ?>
<input type="submit" name="showFlag" onclick="$('#flagform').toggle(); return false;" value="Flag Content Or Technical Issue" class="btn btn-warning" style="float:right;">
<?php } ?>
</form>
<?php if ( $assn_json->flag ) { ?>
<form method="post" id="flagform" style="display:none">
<p>Please be considerate when flagging an item. Only use
flagging when instructor attention is needed.</p>
<input type="hidden" value="<?php echo($submit_id); ?>" name="submit_id">
<input type="hidden" value="<?php echo($user_id); ?>" name="user_id">
<input type="hidden" value="1" name="doFlag">
<textarea rows="5" cols="60" name="note"></textarea><br/>
<input type="submit" name="flagSubmit"
onclick="return confirm('Are you sure you want to bring this student submission to the attention of the instructor?');"
value="Submit To Instructor" class="btn btn-primary">
<input type="submit" name="doCancel" onclick="$('#flagform').toggle(); return false;" value="Cancel Flag" class="btn btn-default">
</form>
<?php } ?>
<?php
if ( $USER->instructor ) {
echo('<br/>Admin Info: ');
echo('<a href="rate-detail.php?user_id='.$user_id.'">Detail</a>');
}
$_SESSION['peer_submit_id'] = $submit_id; // Our CSRF touch
$OUTPUT->footerStart();
?>
<script src="<?= U::get_rest_parent() ?>/static/prism.js" type="text/javascript"></script>
<?php if ( $assn_json->rating > 0 ) { ?>
<script src="<?= $CFG->staticroot ?>/js/jRate.js" type="text/javascript"></script>
<script>
$("#jRate").jRate(
{
startColor: 'cyan',
endColor: 'blue',
width: 25,
height: 25,
strokeWidth: '10px',
min: 0,
max: 5,
max: <?= $assn_json->rating ?>,
precision: 1,
count: <?= $assn_json->rating ?>,
rating: <?= ($previous_rating > 0 ? $previous_rating : '0') ?>,
onChange: function(rating) {
$('#rating-input').val(rating);
}
}
);
</script>
<?php } ?>
<?php
load_htmls();
$OUTPUT->footerEnd();