Repository navigation
Expand file tree
/
Copy pathassignments.php
More file actions
101 lines (96 loc) · 3.59 KB
/
Copy pathassignments.php
File metadata and controls
101 lines (96 loc) · 3.59 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
<?php
/**
* Built-in WebGrader assignments.
*
* Select via Settings → Exercise, or on first launch with LTI custom:
*
* "custom": [ { "key": "exercise", "value": "HeadingsAndParagraphs" } ]
*
* Each key maps to a directory under assignments/ containing assignment.json.
*/
/**
* Catalog of built-in exercises: key => label.
* Prefer this over $assignments from register.php — register is loaded inside
* a function, so a require_once of this file may not define $assignments there.
*/
function webgrader_assignment_catalog() {
return array(
'HeadingsAndParagraphs' => 'HTML: Headings and Paragraphs',
'LinksAndImages' => 'HTML: Links and Images',
'SimpleList' => 'HTML: A Simple List',
'ValidatedHtmlPage' => 'HTML: Validated HTML Page',
'ColoringParagraphs' => 'CSS: Coloring Paragraphs',
'FourCorners' => 'CSS: Four Corners',
'PuttingTheCascade' => 'CSS: Putting the Cascade in CSS',
'HighlightingWithSpan' => 'CSS: Highlighting with Span',
'LinkStates' => 'CSS: Link States',
'FixAccessibility' => 'A11Y: Fix Accessibility',
'HelloWorld' => 'JavaScript: Hello World',
'AddTwoAndSquare' => 'JavaScript: Add Two and Square',
);
}
$assignments = webgrader_assignment_catalog();
/**
* Relative path under this tool to an assignment directory, or null.
*/
function webgrader_builtin_relpath($key) {
$map = array(
'HeadingsAndParagraphs' => 'assignments/html/headings-and-paragraphs',
'LinksAndImages' => 'assignments/html/links-and-images',
'SimpleList' => 'assignments/html/simple-list',
'ValidatedHtmlPage' => 'assignments/html/validated-html-page',
'FixAccessibility' => 'assignments/a11y/fix-accessibility',
'ColoringParagraphs' => 'assignments/css/coloring-paragraphs',
'FourCorners' => 'assignments/css/four-corners',
'PuttingTheCascade' => 'assignments/css/putting-the-cascade',
'HighlightingWithSpan' => 'assignments/html/highlighting-with-span',
'LinkStates' => 'assignments/css/link-states',
'HelloWorld' => 'assignments/javascript/hello-world',
'AddTwoAndSquare' => 'assignments/javascript/add-two-and-square',
);
return isset($map[$key]) ? $map[$key] : null;
}
/**
* Load a built-in assignment by catalog key, or null.
*/
function webgrader_builtin_exercise($key) {
$assignments = webgrader_assignment_catalog();
if (!$key || !is_string($key) || !isset($assignments[$key])) {
return null;
}
if (!preg_match('/^[A-Za-z][A-Za-z0-9_]*$/', $key)) {
return null;
}
$rel = webgrader_builtin_relpath($key);
if (!$rel) {
return null;
}
$path = __DIR__ . '/' . $rel . '/assignment.json';
if (!is_file($path) || !is_readable($path)) {
return null;
}
$raw = file_get_contents($path);
$exercise = json_decode($raw, true);
if (!is_array($exercise)) {
return null;
}
$exercise['builtin'] = $key;
$exercise['builtin_rev'] = md5_file($path);
if (!isset($exercise['source']) || !is_array($exercise['source'])) {
$exercise['source'] = array();
}
$exercise['source']['assignment_id'] = isset($exercise['id']) ? $exercise['id'] : $key;
$exercise['source']['path'] = $rel . '/assignment.json';
return $exercise;
}
/**
* Fingerprint of a built-in assignment.json file.
*/
function webgrader_builtin_rev($key) {
$rel = webgrader_builtin_relpath($key);
if (!$rel) {
return null;
}
$path = __DIR__ . '/' . $rel . '/assignment.json';
return is_readable($path) ? md5_file($path) : null;
}