-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy patha01b.php
469 lines (466 loc) · 15.8 KB
/
a01b.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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?php
use \Tsugi\Core\LTIX;
use \Tsugi\Util\LTI;
use \Tsugi\Util\Net;
require_once $CFG->dirroot."/core/blob/blob_util.php";
error_reporting(0);
$oldgrade = $RESULT->grade;
$grade = 0;
$possgrade = 0;
if ( isset($_FILES['html_01']) ) {
$fdes = $_FILES['html_01'];
$filename = isset($fdes['name']) ? basename($fdes['name']) : false;
// Check to see if they left off a file
if( $fdes['error'] == 4) {
$_SESSION['error'] = 'Missing file, make sure to select all files before pressing submit';
header( 'Location: '.addSession('index.php') ) ;
return;
}
$data = uploadFileToString($fdes, false);
if ( $data === false ) {
$_SESSION['error'] = 'Could not retrieve file data';
header( 'Location: '.addSession('index.php') ) ;
return;
}
if ( count($data) > 250000 ) {
$_SESSION['error'] = 'Please upload a file less than 250K';
header( 'Location: '.addSession('index.php') ) ;
return;
}
// Put the data into session to allow us to process this in the GET request
$_SESSION['html_data'] = $data;
header( 'Location: '.addSession('index.php') ) ;
return;
}
if ( $LINK->grade > 0 ) {
echo('<p class="alert alert-info">Your current grade on this assignment is: '.($LINK->grade*100.0).'%</p>'."\n");
}
if ( $dueDate->message ) {
echo('<p style="color:red;">'.$dueDate->message.'</p>'."\n");
}
?>
<p>
<form name="myform" enctype="multipart/form-data" method="post" action="<?= addSession('index.php') ?>">
Please upload your file containing the HTML.
<p><input name="html_01" type="file"></p>
<input type="submit">
</form>
</p>
<?php
if ( ! isset($_SESSION['html_data']) ) return;
$data = $_SESSION['html_data'];
unset($_SESSION['html_data']);
echo("<pre>\n");
// echo("Input HTML\n");
// echo(htmlentities($data));
// echo("\n");
// First validate using
// https://github.com/validator/validator/wiki/Service:-Input:-POST-body
$validator = 'https://validator.w3.org/nu/?out=json&parser=html5';
echo("Calling the validator $validator ... \n");
$return = Net::doBody($validator, "POST", $data, 'Content-type: text/html; charset=utf-8');
$json = json_decode($return);
$val_error=false;
foreach($json->messages as $item)
{
if($item->type == "error")
{
echo "Found error";
// echo("Validator Output:\n");
// echo(htmlentities(jsonIndent($return)));
$val_error=true;
break;
}
}
if ($val_error){
echo "Your code did not validate. Please return to the W3 validator at validator.w3.org to check your code.";
exit;
}
else{
echo "Your code validated!<br/>";
$dom = new DOMDocument;
@$dom->loadHTML($data);
print("Checking for specific components.<br/>");
$possgrade+=2; //html + lang
try {
$nodes = $dom->getElementsByTagName('html');
if ($nodes->length==1){
print("Found html tag! <br/>");
$grade+=1;
print("...is English language specified?");
foreach ($nodes as $p) //go to each section 1 by 1
{
if ($p->getAttribute('lang')==="en"){
print("...Found it!!<br>");
$grade+=1;
}
else{
print("***Didn't find it!!<br>");
}
}
}
else
print("***Did NOT find html tag!<br>");
}catch(Exception $ex){
error_log("***Did not find html tag!<br>");
}
echo ($grade .' out of ' . $possgrade .'<br/><br/>');
$possgrade+=3; //head + meta + title
try {
$nodes = $dom->getElementsByTagName('head');
if ($nodes->length==1){
print("Found head tag!\n");
$grade+=1;
print("...looking for meta charset...");
try {
$nodes = $dom->getElementsByTagName('meta');
foreach ($nodes as $p) //go to each section 1 by 1
{
if ($p->getAttribute('charset')!=null){
print("...Found it!!<br>");
$grade+=1;
}
else{
print("\n***Didn't find it!!<br>");
}
}
} catch(Exception $ex){
error_log("***Did not find meta tag!<br>");
}
print("...looking for title...");
try {
$nodes = $dom->getElementsByTagName('title');
if ($nodes->length==1){
print("...Found it!!<br>");
$grade+=1;
}
else{
print("\n***Didn't find it!!<br>");
}
}catch(Exception $ex){
error_log("***Did not find title tag!<br>");
}
}
}catch(Exception $ex){
error_log("***Did not find head tag!<br>");
}
echo ($grade .' out of ' . $possgrade .'<br/><br/>');
$possgrade+=1; //body tag
try {
$nodes = $dom->getElementsByTagName('body');
if ($nodes->length==1){
print("Found body tag!<br>");
$grade+=1;
}
else
print("***Did NOT find body tag or found more than one!<br>");
}catch(Exception $ex){
error_log("***Did not find body tag!");
}
echo ($grade .' out of ' . $possgrade .'<br/><br/>');
$possgrade+=1; //header tag
try {
$nodes = $dom->getElementsByTagName('header');
if ($nodes->length==1){
print("Found header tag!<br>");
$grade+=1;
}
else{
print("***Did NOT find header tag or found more than one!<br>");
}
}catch(Exception $ex){
error_log("***Did not find header tag!");
}
echo ($grade .' out of ' . $possgrade .'<br/><br/>');
$possgrade+=1; //h1 tag
try {
$nodes = $dom->getElementsByTagName('h1');
if ($nodes->length==1){
foreach ($nodes as $node) {
$h1 = $node;
}
if (trim(strtolower($h1->nodeValue) == 'colleen van lent')) {
print(htmlentities('<h1> tag text not changed from example page. <br>') .'<br>');
}
else {
print(htmlentities('<h1> tag formatted properly') . '<br>');
$grade += 1;
}
}
else{
print("...***Did NOT find h1 tag or found more than one!\n" .'<br>');
}
}catch(Exception $ex){
error_log("***Did not find h1 tag!");
}
echo ($grade .' out of ' . $possgrade .'<br/><br/>');
$possgrade+=1; //h2 tags
try {
$nodes = $dom->getElementsByTagName('h2');
if ($nodes->length === 3) {
print(htmlentities('Found three <h2> tags') .'<br>');
$grade += 1;
}
else {
print(htmlentities('***Found more or less than three <h2> tags') .'<br>');
}
}
catch(Exception $ex) {
error_log(htmlentities('***Did not find any <h2> tags'));
}
echo ($grade .' out of ' . $possgrade .'<br/><br/>');
$possgrade+=1; //h2 nav
try {
$nodes = $dom->getElementsByTagName('nav');
if ($nodes->length==1){
print("Found nav tag!<br>");
$grade+=1;
}
else{
print("...***Did NOT find nav tag or found more than one!\n <br>");
}
}catch(Exception $ex){
error_log("***Did not find nav tag!");
}
echo ($grade .' out of ' . $possgrade .'<br/><br/>');
$possgrade+=1; //h2 sections
try {
$nodes = $dom->getElementsByTagName('section');
if ($nodes->length==3){
print("Found three section tags!<br>");
$grade+=1;
}
else
print("***Did NOT find three sections tags\n <br>");
}catch(Exception $ex){
error_log("***Did not find 3 section tags!");
}
echo ($grade .' out of ' . $possgrade .'<br/><br/>');
print("Searching for four links in the nav...<br>");
$possgrade+=1; //h2 links
try {
$nav = $dom->getElementsByTagName('nav');
$nav_links_all = array();
foreach ($nav as $navlinks) {
$navlinks = $navlinks->childNodes;
$count=0;
foreach ($navlinks as $link) {
$nav_links_all[] = $link;
if ($link->tagName === "a") {
$count+=1;
print("Found ". $count . '....');
}
}
if ($count==4){
print("Found them all!<br>");
if (trim(strtolower($nav_links_all[6]->nodeValue)) !== 'four') {
print(htmlentities("\n Fourth <a> tag's text was changed") .'<br>');
$grade += 1;
echo($grade . " out of ". $possgrade);
}
else {
print(htmlentities('***Fourth <a> tag text was not changed') .'<br>');
}
}
else
print("\n****Did not find four links in the nav section <br>");
}
} catch(Exception $ex) {
print("***Did not find links in the navigation");
$navlinks = "";
}
echo ($grade .' out of ' . $possgrade .'<br/><br/>');
$possgrade+=1; //ul tag + li tags
try {
$nodes = $dom->getElementsByTagName('ul');
$list_items = array();
if ($nodes->length == 1) {
foreach ($nodes as $node) {
$items = $node->childNodes;
$count = 0;
foreach ($items as $item) {
$list_items[] = $item;
if ($item->tagName === 'li') {
$count += 1;
}
}
}
if ($count == 4) {
print('Found four list items<br>');
$lcount = 0;
foreach ($list_items as $item) {
if (trim(strtolower($item->nodeValue)) == 'apples' || trim(strtolower($item->nodeValue)) == 'pizza' ||
trim(strtolower($item->nodeValue)) == 'crab' || trim(strtolower($item->nodeValue)) == 'chocolate cake') {
$lcount += 1;
}
}
if ($lcount) {
print('***' . $lcount . ' list ' . ($lcount == 1 ? 'item is' : 'items are') . ' the same as the example page<br>');
}
else {
print('All list items formatted properly<br>');
$grade += 1;
}
}
else {
print("\n ***Found less or more than four list items<br>");
}
}
else {
print(htmlentities("\n ***Found more than one <ul> tag") .'<br>');
}
} catch(Exception $ex) {
error_log(htmlentities('***Did not find a <ul> tag') .'<br>');
}
echo ($grade .' out of ' . $possgrade .'<br/><br/>');
$possgrade+=1; //progress tags
try {
$nodes = $dom->getElementsByTagName('progress');
if ($nodes->length == 3) {
print("Found three progress tags" . '<br>');
$progress = array();
foreach($nodes as $node) {
$progress[] = $node;
}
$p = $progress[2]->parentNode;
$p = explode('(', $p->nodeValue);
if (substr($p[1], 0, 3) == '67%') {
print(htmlentities("\n ***Value of third <progress> tag not changed") .'<br>');
}
else {
print(htmlentities('<progress> tags formatted properly') .'<br>');
$grade += 1;
}
}
} catch(Exception $ex) {
error_log(htmlentities('***Did not find <progress> tags'));
}
echo ($grade .' out of ' . $possgrade .'<br/><br/>');
$possgrade+=1; //details + summary tag
try {
$nodes = $dom->getElementsByTagName('details');
if ($nodes->length == 1) {
$details = array();
foreach ($nodes as $node) {
$children = $node->childNodes;
foreach ($children as $child) {
$details[] = $child;
}
}
if ($details[0]->tagName !== 'summary') {
print(htmlentities("\n ***Missing <summary> tag") .'<br>');
}
if ($details[1]->tagName !== 'p') {
print(htmlentities("\n ***Missing <p> tag in <details>") .'<br>');
}
elseif (trim(strtolower($details[1]->nodeValue)) == 'i grew up in ashtabula ohio. i lived near
lake erie and i really miss the sunsets over the water.') {
print(htmlentities('***Content of <p> tag in <details> was not changed') .'<br>');
}
else {
print(htmlentities('<details> tag properly formatted.') .'<br>');
$grade += 1;
}
}
else {
print(htmlentities('***Did not find <details> tag or found more than one') .'<br>');
}
} catch(Exception $ex) {
error_log(htmlentities('Did not find <details> tag.'));
}
echo ($grade .' out of ' . $possgrade .'<br/><br/>');
$possgrade+=3; //footer, link tags
try {
$nodes = $dom->getElementsByTagName('footer');
if ($nodes->length==1){
print("Found footer tag!\n" . '<br>');
$grade += 1;
$footer = array();
foreach ($nodes as $node) {
$items = $node->childNodes;
foreach ($items as $item) {
$footer[] = $item;
}
}
if ($footer[0]->tagName == 'p') {
$footer_p = array();
$children = $footer[0]->childNodes;
foreach ($children as $child) {
$footer_p[] = $child;
}
if ($footer_p[1]->tagName == 'img') {
print(htmlentities('Found <img> tag in footer <p> tag') .'<br>');
if ($footer_p[1]->getAttribute('src') !== 'http://www.intro-webdesign.com/images/newlogo.png') {
print(htmlentities('***<img> tag has incorrect src attribute') .'<br>');
}
elseif (!$footer_p[1]->getAttribute('alt')) {
print(htmlentities('<img> tag is missing alt attribute') .'<br>');
}
else {
print(htmlentities('<img> tag properly formatted') .'<br>');
$grade += 1;
}
}
else {
print(htmlentities('***<img> tag is not first element within <p> tag of footer') .'<br>');
}
if ($footer_p[2]->wholeText) {
$text = explode('by ', $footer_p[2]->wholeText);
$text = explode(' ', $text[1]);
if (strtolower($text[1]) == 'name') {
print('***Name in footer not changed from example page' .'<br>');
}
}
else {
print(htmlentities('<p> tag text missing from footer'));
}
if ($footer_p[3]->tagName == 'a') {
print(htmlentities('Found <a> tag in footer <p> tag') .'<br>');
if ($footer_p[3]->getAttribute('href') !== 'http://www.intro-webdesign.com') {
print(htmlentities('***Wrong href attribute for <a> tag in the <p> tag of the footer') .'<br>');
}
else {
print(htmlentities('<a> tag in <p> tag of footer properly formatted') .'<br>');
$grade += 1;
}
}
else {
print(htmlentities('***No <a> tag found in <p> tag of footer') .'<br>');
}
}
else {
print(htmlentities('Missing <p> tag from footer') .'<br>');
}
}
else
print("***Did NOT find footer tag or found more than one!\n" .'<br>');
} catch(Exception $ex){
error_log("***Did not find footer tag!");
}
echo ($grade .' out of ' . $possgrade .'<br/><br/>');
echo ("\n\nYour score is " . $grade/$possgrade . "\n\n");
}
$gradetosend = $grade/$possgrade;
$scorestr = "Your answer is correct, score saved.";
if ( $oldgrade > $gradetosend ) {
$scorestr = "New score of $gradetosend is < than previous grade of $oldgrade, previous grade kept";
$gradetosend = $oldgrade;
}
// Use LTIX to send the grade back to the LMS.
$debug_log = array();
$retval = LTIX::gradeSend($gradetosend, false, $debug_log);
$_SESSION['debug_log'] = $debug_log;
if ( $retval === true ) {
$_SESSION['success'] = $scorestr;
} else if ( is_string($retval) ) {
$_SESSION['error'] = "Grade not sent: ".$retval;
} else {
echo("<pre>\n");
var_dump($retval);
echo("</pre>\n");
die();
}
exit;
// Redirect to ourself
header('Location: '.addSession('index.php'));
return;