|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the trefoil application. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Miguel Angel Gabriel <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Trefoil\Plugins\Optional; |
|
12
|
|
|
|
|
13
|
|
|
use Easybook\Events\BaseEvent; |
|
14
|
|
|
use Easybook\Events\EasybookEvents; |
|
15
|
|
|
use Easybook\Events\ParseEvent; |
|
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
17
|
|
|
use Trefoil\Helpers\QuizActivity; |
|
18
|
|
|
use Trefoil\Helpers\QuizActivityParser; |
|
19
|
|
|
use Trefoil\Helpers\QuizItem; |
|
20
|
|
|
use Trefoil\Helpers\QuizQuestionnaire; |
|
21
|
|
|
use Trefoil\Helpers\QuizQuestionnaireParser; |
|
22
|
|
|
use Trefoil\Util\SimpleReport; |
|
23
|
|
|
use Trefoil\Plugins\BasePlugin; |
|
24
|
|
|
|
|
25
|
|
|
class EbookQuizPlugin extends BasePlugin implements EventSubscriberInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* The name of the book element that will receive the rendered solutions |
|
29
|
|
|
*/ |
|
30
|
|
|
const QUIZ_SOLUTIONS_ELEMENT = 'ebook-quiz-solutions'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* |
|
34
|
|
|
* @var Array|QuizItem |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $quizItems = array(); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Whether or not the solutions item has been generated |
|
40
|
|
|
* |
|
41
|
|
|
* @var bool |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $generated = false; |
|
44
|
|
|
|
|
45
|
1 |
|
public static function getSubscribedEvents() |
|
46
|
|
|
{ |
|
47
|
|
|
return array( |
|
48
|
1 |
|
EasybookEvents::PRE_PARSE => array('onItemPreParse', +100), |
|
49
|
1 |
|
EasybookEvents::POST_PARSE => array('onItemPostParse', -1100), // after ParserPlugin |
|
50
|
1 |
|
EasybookEvents::POST_PUBLISH => 'onPostPublish'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
public function onItemPreParse(ParseEvent $event) |
|
54
|
|
|
{ |
|
55
|
1 |
|
$this->init($event); |
|
56
|
|
|
|
|
57
|
1 |
|
if (self::QUIZ_SOLUTIONS_ELEMENT == $this->item['config']['element']) { |
|
58
|
|
|
// prepare to render the solutions into this element |
|
59
|
1 |
|
$this->prepareSolutions(); |
|
60
|
1 |
|
} |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
public function onItemPostParse(ParseEvent $event) |
|
64
|
|
|
{ |
|
65
|
1 |
|
$this->init($event); |
|
66
|
|
|
|
|
67
|
1 |
|
if (self::QUIZ_SOLUTIONS_ELEMENT != $this->item['config']['element']) { |
|
68
|
|
|
// a normal item that can contain quiz elements |
|
69
|
1 |
|
$this->processItem(); |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
// reload changed item |
|
73
|
1 |
|
$event->setItem($this->item); |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public function onPostPublish(BaseEvent $event) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$this->init($event); |
|
79
|
|
|
|
|
80
|
1 |
|
$this->checkCompatibility(); |
|
81
|
|
|
|
|
82
|
|
|
// create the processing report |
|
83
|
1 |
|
$this->createReport(); |
|
84
|
1 |
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
protected function checkCompatibility() |
|
87
|
|
|
{ |
|
88
|
1 |
|
$plugins = $this->getEditionOption('plugins.enabled'); |
|
89
|
|
|
|
|
90
|
1 |
|
if (in_array('KindleTweaks', $plugins)) { |
|
91
|
|
|
$this->writeLn('"KindleTweaks" plugin is enabled. Please disable it to avoid incompatibilities.', "error"); |
|
92
|
|
|
} |
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Parse the quiz elements in the current item and render them. |
|
97
|
|
|
* |
|
98
|
|
|
* The quiz elements are stored for later rendering of solutions. |
|
99
|
|
|
*/ |
|
100
|
1 |
|
protected function processItem() |
|
101
|
|
|
{ |
|
102
|
1 |
|
$quizElements = array( |
|
103
|
1 |
|
'activity', // backwards compatibility |
|
104
|
1 |
|
'questions', // backwards compatibility |
|
105
|
1 |
|
'quiz-activity', |
|
106
|
|
|
'quiz-questionnaire' |
|
107
|
1 |
|
); |
|
108
|
|
|
|
|
109
|
|
|
// capture quiz elements |
|
110
|
1 |
|
$regExp = '/'; |
|
111
|
1 |
|
$regExp .= '(?<div>'; |
|
112
|
1 |
|
$regExp .= '<div[^(class|>)]*'; |
|
113
|
1 |
|
$regExp .= sprintf('class="(?<type>%s)"', implode('|', $quizElements)); |
|
114
|
|
|
|
|
115
|
|
|
// can have other divs embedded (i.e. figures or tables already decorated) |
|
116
|
1 |
|
$regExp .= '('; |
|
117
|
1 |
|
$regExp .= '(<div.*>.*<\/div>)|'; // a single div |
|
118
|
1 |
|
$regExp .= '.'; // anything |
|
119
|
1 |
|
$regExp .= ')*'; |
|
120
|
|
|
|
|
121
|
1 |
|
$regExp .= '<\/div>)'; |
|
122
|
1 |
|
$regExp .= '/Ums'; // Ungreedy, multiline, dotall |
|
123
|
|
|
|
|
124
|
|
|
// PHP 5.3 compat |
|
125
|
1 |
|
$me = $this; |
|
126
|
|
|
|
|
127
|
1 |
|
$content = preg_replace_callback( |
|
128
|
1 |
|
$regExp, |
|
129
|
1 |
|
function ($matches) use ($me) { |
|
130
|
1 |
|
$html = ''; |
|
131
|
1 |
|
switch ($matches['type']) { |
|
132
|
1 |
|
case 'activity': |
|
133
|
1 |
|
case 'quiz-activity': |
|
134
|
1 |
|
$html = $me->internalProcessActivityType($matches['div']); |
|
135
|
1 |
|
break; |
|
136
|
1 |
|
case 'questions': |
|
137
|
1 |
|
case 'quiz-questionnaire': |
|
138
|
1 |
|
$html = $me->internalProcessQuestionnaireType($matches['div']); |
|
139
|
1 |
|
break; |
|
140
|
1 |
|
} |
|
141
|
|
|
|
|
142
|
1 |
|
return $html; |
|
143
|
1 |
|
}, |
|
144
|
1 |
|
$this->item['content'] |
|
145
|
1 |
|
); |
|
146
|
|
|
|
|
147
|
1 |
|
$this->item['content'] = $content; |
|
148
|
1 |
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Parse the quiz element of type QuizActivity and return a rendered version. |
|
152
|
|
|
* |
|
153
|
|
|
* @param $sourceHtml |
|
154
|
|
|
* |
|
155
|
|
|
* @return string |
|
156
|
|
|
* |
|
157
|
|
|
* @internal Should be protected but made public for PHP 5.3 compat |
|
158
|
|
|
*/ |
|
159
|
1 |
|
public function internalProcessActivityType($sourceHtml) |
|
160
|
|
|
{ |
|
161
|
1 |
|
$validYes = $this->getEditionOption('plugins.options.EbookQuiz.ynb.yes'); |
|
162
|
1 |
|
$validNo = $this->getEditionOption('plugins.options.EbookQuiz.ynb.no'); |
|
163
|
1 |
|
$validBoth = $this->getEditionOption('plugins.options.EbookQuiz.ynb.both'); |
|
164
|
|
|
|
|
165
|
1 |
|
$parser = new QuizActivityParser($sourceHtml); |
|
166
|
1 |
|
if ($validYes) { |
|
167
|
1 |
|
$parser->setResponsesValidAsYes($validYes); |
|
168
|
1 |
|
} |
|
169
|
1 |
|
if ($validNo) { |
|
170
|
1 |
|
$parser->setResponsesValidAsNo($validNo); |
|
171
|
1 |
|
} |
|
172
|
1 |
|
if ($validBoth) { |
|
173
|
1 |
|
$parser->setResponsesValidAsBoth($validBoth); |
|
174
|
1 |
|
} |
|
175
|
|
|
|
|
176
|
1 |
|
$activity = $parser->parse(); |
|
177
|
|
|
|
|
178
|
|
|
// save it for rendering the solutions later |
|
179
|
1 |
|
$this->saveQuizItem($activity); |
|
180
|
|
|
|
|
181
|
|
|
// render it |
|
182
|
1 |
|
$variables = array('activity' => $activity); |
|
183
|
1 |
|
$html = $this->app['twig']->render('ebook-quiz-activity.twig', $variables); |
|
184
|
|
|
|
|
185
|
1 |
|
return $html; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Parse the quiz element of type QuizQuestionnaire and return a rendered version. |
|
190
|
|
|
* |
|
191
|
|
|
* @param $sourceHtml |
|
192
|
|
|
* |
|
193
|
|
|
* @return string |
|
194
|
|
|
* |
|
195
|
|
|
* @internal Should be protected but made public for PHP 5.3 compat |
|
196
|
|
|
*/ |
|
197
|
1 |
|
public function internalProcessQuestionnaireType($sourceHtml) |
|
198
|
|
|
{ |
|
199
|
1 |
|
$parser = new QuizQuestionnaireParser($sourceHtml); |
|
200
|
|
|
|
|
201
|
1 |
|
$questionnaire = $parser->parse(); |
|
202
|
|
|
|
|
203
|
|
|
// save it for rendering the solutions later |
|
204
|
1 |
|
$this->saveQuizItem($questionnaire); |
|
205
|
|
|
|
|
206
|
|
|
// render it |
|
207
|
1 |
|
$variables = array('questionnaire' => $questionnaire); |
|
208
|
1 |
|
$html = $this->app['twig']->render('ebook-quiz-questionnaire.twig', $variables); |
|
209
|
|
|
|
|
210
|
1 |
|
return $html; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Save a quiz item xref for reporting. |
|
215
|
|
|
* |
|
216
|
|
|
* @param QuizItem $quizItem |
|
217
|
|
|
*/ |
|
218
|
1 |
|
protected function saveQuizItem(QuizItem $quizItem) |
|
219
|
|
|
{ |
|
220
|
|
|
// save the xref to this item |
|
221
|
1 |
|
$quizItem->setXref($this->item['config']['content']); |
|
222
|
|
|
|
|
223
|
|
|
// assign a name for grouping items |
|
224
|
1 |
|
$name = $this->item['title']; |
|
225
|
1 |
|
if ($this->item['label']) { |
|
226
|
1 |
|
$name = $this->item['label'] . ' - ' . $name; |
|
227
|
1 |
|
} |
|
228
|
|
|
|
|
229
|
1 |
|
if (!isset($this->quizItems[$name])) { |
|
230
|
1 |
|
$this->quizItems[$name] = array(); |
|
231
|
1 |
|
} |
|
232
|
|
|
|
|
233
|
1 |
|
$this->quizItems[$name][] = $quizItem; |
|
234
|
1 |
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Save the information for the "solutions" book element to be rendered. |
|
238
|
|
|
* |
|
239
|
|
|
* The rendering will be done by the publisher at decoration time (or when |
|
240
|
|
|
* the book is assembled, depending on the implementation). |
|
241
|
|
|
*/ |
|
242
|
1 |
|
protected function prepareSolutions() |
|
243
|
|
|
{ |
|
244
|
1 |
|
$this->app['publishing.quiz.items'] = $this->quizItems; |
|
245
|
|
|
|
|
246
|
1 |
|
$this->generated = true; |
|
247
|
1 |
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Writes the report with the summary of processing done. |
|
251
|
|
|
*/ |
|
252
|
1 |
|
protected function createReport() |
|
253
|
|
|
{ |
|
254
|
1 |
|
if (!$this->generated) { |
|
255
|
|
|
$this->writeLn( |
|
256
|
|
|
sprintf("No Quiz has been generated, check for missing '%s' contents element.", self::QUIZ_SOLUTIONS_ELEMENT), |
|
257
|
|
|
"error" |
|
258
|
|
|
); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
1 |
|
$report = new SimpleReport(); |
|
262
|
1 |
|
$report->setTitle('EbookQuizPlugin'); |
|
263
|
1 |
|
$report->setSubtitle('Quiz Items'); |
|
264
|
|
|
|
|
265
|
1 |
|
$report->setHeaders(array('Item title', 'X-Ref', 'Id', 'Type', 'Heading', 'Questions')); |
|
266
|
|
|
|
|
267
|
1 |
|
$report->setColumnsWidth(array(40, 20, 15, 15, 40, 9)); |
|
268
|
1 |
|
$report->setColumnsAlignment(array('', '', '', '', '', 'right')); |
|
269
|
|
|
|
|
270
|
1 |
|
foreach ($this->quizItems as $item => $quizItems) { |
|
271
|
1 |
|
$auxItem = $item; |
|
272
|
1 |
|
$auxXref = isset($quizItems[0]) ? $quizItems[0]->getXref() : ''; |
|
273
|
|
|
|
|
274
|
|
|
/** @var QuizItem $quizItem */ |
|
275
|
1 |
|
foreach ($quizItems as $quizItem) { |
|
276
|
|
|
|
|
277
|
1 |
|
switch ($quizItem->getType()) { |
|
278
|
1 |
|
case QuizActivity::QUIZ_ACTIVITY_TYPE_ABC: |
|
279
|
1 |
|
case QuizActivity::QUIZ_ACTIVITY_TYPE_YNB: |
|
280
|
|
|
/** @var QuizActivity $activity */ |
|
281
|
1 |
|
$activity = $quizItem; |
|
282
|
1 |
|
$count = count($activity->getQuestions()); |
|
283
|
1 |
|
break; |
|
284
|
1 |
|
case QuizQuestionnaire::QUIZ_QUESTIONNAIRE: |
|
285
|
|
|
/** @var QuizQuestionnaire $questionnaire */ |
|
286
|
1 |
|
$questionnaire = $quizItem; |
|
287
|
1 |
|
$count = count($questionnaire->getQuestions()); |
|
288
|
1 |
|
break; |
|
289
|
|
|
default: |
|
290
|
|
|
$count = null; |
|
291
|
1 |
|
} |
|
292
|
|
|
|
|
293
|
1 |
|
$report->addline( |
|
294
|
1 |
|
array(substr($auxItem, 0, 40), |
|
295
|
1 |
|
$auxXref, |
|
296
|
1 |
|
$quizItem->getId(), |
|
297
|
1 |
|
$quizItem->getType(), |
|
298
|
1 |
|
substr($quizItem->getHeading(), 0, 40), |
|
299
|
1 |
|
$count) |
|
300
|
1 |
|
); |
|
301
|
1 |
|
$auxItem = ''; |
|
302
|
1 |
|
$auxXref = ''; |
|
303
|
1 |
|
} |
|
304
|
1 |
|
} |
|
305
|
|
|
|
|
306
|
1 |
|
$outputDir = $this->app['publishing.dir.output']; |
|
307
|
1 |
|
$reportFile = $outputDir . '/report-EbookQuizPlugin.txt'; |
|
308
|
|
|
|
|
309
|
1 |
|
file_put_contents($reportFile, $report->getText()); |
|
310
|
|
|
|
|
311
|
1 |
|
} |
|
312
|
|
|
|
|
313
|
|
|
} |
|
314
|
|
|
|