Repository navigation
Expand file tree
/
Copy pathzanichelli.php
More file actions
114 lines (102 loc) · 3.43 KB
/
Copy pathzanichelli.php
File metadata and controls
114 lines (102 loc) · 3.43 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
<?php
namespace LucaTheHacker;
class Zanichelli
{
/**
* @var mixed
*/
private $json;
/*
* Loads the JSON in the class
*
* @param string $data Text to load
* */
public function loadJson(string $data): void
{
$this->json = json_decode($data, true);
}
/*
* Returns the solution of an exercise
*
* @param array $exercise
* @return array
* */
private function solveExercise(array $exercise): array
{
// Decodes the list containing all the QA
$options = json_decode(base64_decode($exercise['list']), true);
// Search for exercise type
if (strpos($exercise['question'], '<gap_ref idref="') !== false) {
$solutions = $exercise['question'];
foreach ($options as $option) {
foreach ($option['values'] as $value) {
if ($value['correct'] || $value['correct'] === null) {
$solutions = str_replace('<gap_ref idref="_' . ($option['id'] ?? '?') . '"/>', $value['text'], $solutions);
}
}
}
} elseif (strpos($exercise['question'], '<error_ref idref="') !== false) {
$solutions = $exercise['question'];
foreach ($options as $option) {
foreach ($option['values'] as $value) {
if ($value['correct'] || $value['correct'] === null) {
$solutions = str_replace('<error_ref idref="_' . ($option['id'] ?? '?') . '"/>', $value['text'], $solutions);
}
}
}
} else {
$solutions = [];
foreach ($options as $option) {
if ($option['correct']) $solutions[] = rtrim($option['text']??$option['question']);
}
}
return [
'question' => rtrim($exercise['question']),
'solutions' => $solutions,
];
}
/*
* Solves the whole quiz
*
* @return array
* */
public function solve(): array
{
$results = [];
foreach ($this->json['exercises'] as $exercise) {
$result = $this->solveExercise($exercise['json']);
// Question cleaner
$result['question'] = str_replace('<br/>', PHP_EOL, $result['question']);
$result['question'] = preg_replace('<(gap_ref|error_ref) idref="_[0-9]*"/>', 'INSERT HERE', $result['question']);
// Solutions cleaner
if (!is_array($result['solutions'])) {
$result['solutions'] = str_replace('<br/>', PHP_EOL, $result['solutions']);
}
$results[] = $result;
}
return $results;
}
/*
* Returns info about the quiz
*
* @return array
* */
public function getInfos(): array
{
return [
'ID' => $this->json['_id'],
'title' => $this->json['publicTitle'],
'book' => [
'ID' => $this->json['bookId'],
'Title' => $this->json['bookTitle'],
],
'course' => $this->json['courseTitle'],
'subjects' => $this->json['subjects'],
'owner' => [
'ID' => $this->json['owner'],
'name' => $this->json['creator']['firstName'] . ' ' . $this->json['creator']['lastName'],
'email' => $this->json['exercises'][0]['owner'] ?? '',
],
];
}
}