Feature Wiki

Information about planned and released features

Tabs

Template for Automatic Question Generation

1 Requirements

This template shall allow the automated generation of questions out of a given context. The issue at stake is that on the one hand a tutor or the person who is responsible for the course does not have to type in all questions manually. On the other hand the automatic and random question generation will help to avoid redundancy concearning the content of questions. So every student or course attandee will receive an individual test-questionnaire.

2 Status

  • Scheduled for: Not scheduled yet (will be set by Jour Fixe)
  • Funding:no Funding
  • Maintainer: Björn Heyser, Databay
  • Implementation of the feature is done by (Karlsruhe Institute of Technology (KIT), Lukas Block)
  • Contract settled: (No)
  • Tested by / status: (name, e-mail), (status information set after implementation)

3 Additional Information

Contact the following persons if you want to know more about this feature, its implementation or funding:

  • Information about concept: (Carina Benz, carina.benz@student.kit.edu; Lukas Block, uafok@student.kit.edu;Mathias Miemiec, ucdhr@student.kit.edu)
  • Information about funding: (name, e-mail)
  • Information about implementation: (Carina Benz, carina.benz@student.kit.edu;Lukas Block, uafok@student.kit.edu; Mathias Miemiec, ucdhr@student.kit.edu)

Seminar-Paper concerning develoment of attached template and general information (German).
 
The template allows user to generate automatically clozes out of a given text. Gaps are determined by regular expressions. Thereby not single combination of letters are blanked but coherent contexts derived from Word-Pools belonging to a specific content.

4 Discussion

JF 31 Mar 2014: In general we think that this may be a valuable feature. Please contact the module maintainer Björn Heyser (mail: bheyser (at) databay.de) to discuss a possible integration into the trunk and re-schedule the topic on the JF agenda afterwards.

5 Implementation

Class: assVTQuestion

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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
<?php 
 
include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
 
/**
* Class to provide the core functionality for the Question-Plugin
* @author Lukas Block
* @version 2014-03-19
*/

class assVTQuestion extends assQuestion{
 
//Pointer to the plugin
private $plugin;
//array which holds the texts
private $texts;
//array with the keys "pool_fis", "case_sensitive", "points_per_gap", "nr_of_gaps" as settings
private $settings;
 
/**
* Constructor
* @Override
*/

function __construct($title = "", $comment = "", $author = "", $owner = -1) {
parent::__construct($title, $comment, $author, $owner, "");
}
 
/**
* Setter for the linked word pools
* @param the database ids of the word pools as an int array
*/

public function setPoolFis($pool_fis) {
$this->settings["pool_fis"] = $pool_fis;
}
 
/**
* Getter for the linked word pools
* @return the database ids of the word pools as an int array
*/

public function getPoolFis() {
return $this->settings["pool_fis"];
}
 
/**
* Getter for the number of gaps
* @return normal int for the number of gaps
*/

public function getNrOfGaps() {
return $this->settings["nr_of_gaps"];
}
 
/**
* Setter for the number of gaps
* @param normal int for the number of gaps
*/

public function setNrOfGaps($nr_of_gaps) {
$this->settings["nr_of_gaps"] = $nr_of_gaps;
}
 
/**
* Setter for the points per gap
* @param normal int for points
*/

public function setPointsPerGap($points_per_gap) {
$this->settings["points_per_gap"] = $points_per_gap;
}
 
/**
* Getter for the points per gap
* @return normal int for points
*/

public function getPointsPerGap() {
return $this->settings["points_per_gap"];
}
 
/**
* Setter wheather correction is case sensitive or not
* @param false for case sensitive correction, true otherwise
*/

public function setCaseSensitive($case_sensitive) {
$this->settings["case_sensitive"] = $case_sensitive;
}
 
/**
* Getter wheather the correction is case sensitive or not
* @return false for case sensitive correction, true otherwise
*/

public function getCaseSensitive() {
return $this->settings["case_sensitive"];
}
 
/**
* Getter for the array which holds the different texts
* @return string array, each field equals one text
*/

public function getTexts() {
return $this->texts;
}
 
/**
* Setter for the array which holds the different texts
* @param string array, each field equals one text
*/

public function setTexts($texts) {
$this->texts = $texts;
}
 
/**
* Getter to get all settings
* @return array with the keys "pool_fis", "case_sensitive", "points_per_gap", "nr_of_gaps" (for types look above)
*/

public function getSettings() {
return $this->settings;
}
 
/**
* Setter to set all settings
* @param array with the keys "pool_fis", "case_sensitive", "points_per_gap", "nr_of_gaps" (for types look above)
*/

public function setSettings($settings) {
$this->settings = $settings;
}
/**
* Initialize plugin instance
* Required by Ilias
* @Override
*/

public function getPlugin()
{
if($this->plugin == null)
{
include_once "./Services/Component/classes/class.ilPlugin.php";
$this->plugin = ilPlugin::getPluginObject(IL_COMP_MODULE, "TestQuestionPool", "qst", "assVTQuestion");
}
return $this->plugin;
}
 
 
/**
* Returns true, if the question is complete for use
* @Override
*/

public function isComplete()
{
if(($this->title) and ($this->author) and ($this->getMaximumPoints() > 0) and ($this->checkNrOfGaps() == -1))
{
return true;
}
else
{
return false;
}
}
 
/**
* Function to check wheather all text have enough gaps
* @return the number (not the id) of the text that has too less gaps. If all texts have enough gaps -1 is returned
*/

function checkNrOfGaps() {
$i = 0;
foreach($this->getTexts() as $key => $value) {
$array = $this->setGaps($value, $this->getWords());
if(sizeof($array["solution"]) < $this->getNrOfGaps()) {
return $i;
}
$i++;
}
return -1;
}
 
/**
* Saves a assVTQuestion object to a database
* @Override
*/

function saveToDb($original_id = "")
{
global $ilDB;
 
$this->saveQuestionDataToDb($original_id);
 
 
//Save Texts
//First delete all old texts
$affectedRows = $ilDB->manipulateF("DELETE FROM il_qpl_qst_assVTQuestion_text WHERE question_fi = %s",
array("integer"),
array($this->getId())
);
//Now insert new texts
foreach($this->texts as $key => $text)
{
$next_id = $ilDB->nextId('il_qpl_qst_assVTQuestion_text');
$affectedRows = $ilDB->insert("il_qpl_qst_assVTQuestion_text", array(
"text_id" => array("integer", $next_id),
"question_fi" => array("integer", $this->getId()),
"question_text" => array("clob", $text),
));
}
 
//Save Settings
//First delete all old settings
$affectedRows = $ilDB->manipulateF("DELETE FROM il_qpl_qst_assVTQuestion_setting WHERE question_fi = %s",
array("integer"),
array($this->getId())
);
//Now insert new settings
$next_id = $ilDB->nextId('il_qpl_qst_assvtquestion_setting');
$affectedRows = $ilDB->insert("il_qpl_qst_assvtquestion_setting", array(
"setting_id" => array("integer", $next_id),
"question_fi" => array("integer", $this->getId()),
"pool_fis" => array("clob", serialize($this->settings["pool_fis"])),
"points_per_gap" => array("integer", $this->getPointsPerGap()),
"nr_of_gaps" => array("integer", $this->getNrOfGaps()),
"case_sensitive" => array("integer", $this->getCaseSensitive())
));
 
//Call parent
parent::saveToDb($original_id);
}
 
/**
* Loads a assVTQuestion object from a database
* @Override
*/

public function loadFromDb($question_id)
{
global $ilDB;
 
//Get data out of database
$result = $ilDB->queryF("SELECT qpl_questions.* FROM qpl_questions WHERE question_id = %s",
array('integer'),
array($question_id)
);
//Insert it into object
if($result->numRows() == 1)
{
$data = $ilDB->fetchAssoc($result);
$this->setId($question_id);
$this->setTitle($data["title"]);
$this->setComment($data["description"]);
 
$this->setObjId($data["obj_fi"]);
$this->setAuthor($data["author"]);
$this->setOwner($data["owner"]);
$this->setPoints($data["points"]);
$this->setOriginalId($data["original_id"]);
 
include_once("./Services/RTE/classes/class.ilRTE.php");
$this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
$this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
}
 
//Load texts from il_qpl_qst_assVTQuestion_text
$vt_result = $ilDB->queryF("SELECT il_qpl_qst_assVTQuestion_text.* FROM il_qpl_qst_assVTQuestion_text WHERE question_fi = %s",
array('integer'),
array($question_id)
);
$this->texts = array();
for($i = 0; $i < $vt_result->numRows(); $i++) {
$data = $ilDB->fetchAssoc($vt_result);
$this->texts[$i] = $data["question_text"];
}
 
//Load settings from il_qpl_qst_assvtquestion_setting
$this->settings["pool_fis"] = array();
$svt_result = $ilDB->queryF("SELECT il_qpl_qst_assvtquestion_setting.* FROM il_qpl_qst_assvtquestion_setting WHERE question_fi = %s",
array('integer'),
array($question_id)
);
if($svt_result->numRows() == 1)
{
$data = $ilDB->fetchAssoc($svt_result);
$this->settings["points_per_gap"] = $data["points_per_gap"];
$this->settings["nr_of_gaps"] = $data["nr_of_gaps"];
$this->settings["case_sensitive"] = $data["case_sensitive"];
$this->settings["pool_fis"] = unserialize($data["pool_fis"]);
}
 
//Call parent
parent::loadFromDb($question_id);
}
 
/**
* Duplicates an assVTQuestion
* @Override
*/

function duplicate($for_test = true, $title = "", $author = "", $owner = "")
{
 
if ($this->id <= 0)
{
//The question has not been saved. It cannot be duplicated
return;
}
 
$this_id = $this->getId();
$clone = $this;
include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
$original_id = assQuestion::_getOriginalId($this->id);
$clone->id = -1;
 
if ($title)
{
$clone->setTitle($title);
}
 
if ($author)
{
$clone->setAuthor($author);
}
if ($owner)
{
$clone->setOwner($owner);
}
 
//$for_test = if duplicated for a special test
if ($for_test)
{
$clone->saveToDb($original_id);
}
else
{
$clone->saveToDb();
}
 
//Duplicate texts and settings is not needed here because performed in saveToDb()
 
//Copy question page content
$clone->copyPageOfQuestion($this_id);
//Copy XHTML media objects
$clone->copyXHTMLMediaObjectsOfQuestion($this_id);
 
return $clone->id;
}
 
/**
* Copies an assVTQuestion object
* Called when coping question to another question pool
* @Override
*/

function copyObject($target_questionpool, $title = "")
{
if($this->id <= 0)
{
//The question has not been saved. It cannot be duplicated
return false;
}
//Duplicate the question in database
$clone = $this;
include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
$original_id = assQuestion::_getOriginalId($this->id);
$clone->id = -1;
 
 
$clone->setObjId($target_questionpool);
if($title)
{
$clone->setTitle($title);
}
 
//Everything important is performed in here
$clone->saveToDb();
 
//Copy question page content
$clone->copyPageOfQuestion($original_id);
//Copy XHTML media objects
$clone->copyXHTMLMediaObjectsOfQuestion($original_id);
 
return $clone->id;
}
 
/**
* Returns the maximum points, a learner can reach answering the question
* @Override
*/

public function getMaximumPoints()
{
return ($this->getNrOfGaps() * $this->getPointsPerGap());
}
 
/**
* Purges a question up if an error occured
* @Override
*/

function purgeQuestions() {
//Do nothing...
return true;
}
 
/**
* Called to calculate the points for a special learner ($active_id) and its current pass ($pass)
* @Override
*/

function calculateReachedPoints($active_id, $pass = NULL, $returndetails = false)
{
//Make sure to have a pass
if(is_null($pass))
{
$pass = $this->getSolutionMaxPass($active_id);
}
 
//Returns an array containing the solution, whole row with every column
$solutions =& $this->getSolutionValues($active_id, $pass);
$bestPractice = $this->getBestPractice($solutions[0]["value1"]);
$entries = unserialize($solutions[0]["value2"]);
 
$points = 0;
 
//Foreach answer in a gap of the participant check, wheather it fits to the right solution or not
foreach($entries as $key => $entry) {
$best = $bestPractice[$key];
//If no case sensitive correction wished put both (solution & answers) to lower characters
if($this->getCaseSensitive() > 0) {
$best = strtolower($best);
$entry = strtolower($entry);
}
//Increase points if work
if($entry == $best) {
$points += $this->getPointsPerGap();
}
}
return $points;
}
 
/**
* Sets the gaps, specified by the selected words to an text, does not take care about the number of gaps, sets all gaps which can found
* @param $text The text where the gaps should be inserted
* @param $words string array containing the words to make the gaps of
* @return array with the text (key = "text") with gaps in the form of #:{GAP_NUMBER}:# and an array with the solutions (key = "solution")
*/

function setGaps($text, $words) {
$i = 0;
$solution = array();
$text_neu = $text;
 
 
foreach($words as $key => $value) {
//Variable to count how often a regex is found...
$intf = 0;
do {
$text = $text_neu;
$found = preg_match($value, $text, $matches);
if($found > 0) {
//Insert gap
$replace = str_replace($matches[1], "#:" . $i . ":#", $matches[0]);
$text_neu = preg_replace("/" . $matches[0] . "/" ,$replace ,$text , 1);
//Add word to solution array
$solution[$i] = $matches[1];
$i++;
$intf++;
}
} while ($text != $text_neu);
}
 
//Put text and solution array together and return them
$result["text"] = $text;
$result["solution"] = $solution;
return $result;
}
 
/**
* Function to get all the words from the selcted pools
* @return array with all the words
*/

function getWords() {
global $ilDB;
 
$all_words = array();
foreach($this->settings["pool_fis"] as $key => $value) {
//Get the pool content from the db
$svt_result = $ilDB->queryF("SELECT word_fis FROM il_qpl_qst_assVTQuestion_pool WHERE pool_id = %s",
array('integer'),
array($value)
);
//If pool exists
if($svt_result->numRows()) {
$data = $ilDB->fetchAssoc($svt_result);
//Get all the word ids
$words = unserialize($data["word_fis"]);
foreach($words as $key2 => $value2) {
//Get the word
$word_db = $ilDB->queryF("SELECT word FROM il_qpl_qst_assVTQuestion_word WHERE word_id = %s",
array('integer'),
array($value2)
);
$data2 = $ilDB->fetchAssoc($word_db);
//Put it into the array
array_push($all_words, $data2["word"]);
}
}
}
//Delete duplicates
$all_words = array_unique($all_words);
return $all_words;
}
 
/**
* Function to get the best practice for a given solution id
* @param the id of the solution in the table
* @return an array with the best practice of the words in an array
*/

function getBestPractice($solution_id) {
global $ilDB;
 
$words_db = $ilDB->queryF("SELECT words FROM il_qpl_qst_assVTQuestion_solution WHERE solution_id = %s",
array('integer'),
array($solution_id)
);
$data = $ilDB->fetchAssoc($words_db);
return unserialize($data["words"]);
}
 
/**
* Function to get the solution text including the gaps
* @param the id of the solution in the table
* @return the text including the gaps
*/

function getSolutionText($solution_id) {
global $ilDB;
 
$words_db = $ilDB->queryF("SELECT question_text FROM il_qpl_qst_assVTQuestion_solution WHERE solution_id = %s",
array('integer'),
array($solution_id)
);
$data = $ilDB->fetchAssoc($words_db);
return $data["question_text"];
}
 
/**
* Function to generate and store the question-text and the best pratice array in database and delete overhead of gaps
* @return the solution id in the il_qpl_qst_assVTQuestion_solution table
*/

public function generateQuestion() {
global $ilDB;
 
//Randomly select one text out of all given
$text_index = rand(0, count($this->texts)-1);
$q_text = $this->texts[$text_index];
 
//Get a text with all possible gaps and the best practice solution
$q_array = $this->setGaps($q_text, $this->getWords());
$q_text = $q_array["text"];
$q_solution = $q_array["solution"];
 
//Select gaps if there are to many in the text
$number_of_set_gaps = count($q_solution);
//Delete gaps as long as there are to many
while($number_of_set_gaps > $this->getNrOfGaps()) {
$word_index = rand(0, count($q_solution)-1);
if(!(strpos($q_text, "#:" . $word_index . ":#") === false)) {
//Delete gap from text and solution and decrease number of set gaps
$q_text = str_replace("#:" . $word_index . ":#", $q_solution[$word_index], $q_text);
unset($q_solution[$word_index]);
$number_of_set_gaps--;
}
}
 
//Save to database
$solution_id = $ilDB->nextId('il_qpl_qst_assVTQuestion_solution');
$affectedRows = $ilDB->insert("il_qpl_qst_assVTQuestion_solution", array(
"solution_id" => array("integer", $solution_id),
"question_fi" => array("integer", $this->getId()),
"question_text" => array("clob", $q_text),
"words" => array("clob", serialize($q_solution))
));
 
return $solution_id;
}
 
 
/**
* Saves the learners input of the question to the database
* @Override
*/

function saveWorkingData($active_id, $pass = NULL)
{
global $ilDB;
 
if(is_null($pass))
{
include_once "./Modules/Test/classes/class.ilObjTest.php";
$pass = ilObjTest::_getPass($active_id);
}
//Check if values are avaiable
$entered_values = FALSE;
$entries = NULL;
foreach($_POST["data"] as $key => $value)
{
//Has the user filled in anything?
if(strlen($value)) $entered_values = TRUE;
//Add to entries with right key!!
$entries[$key] = $value;
}
 
//Delete old solution
$result = $ilDB->queryF("SELECT solution_id FROM tst_solutions WHERE active_fi = %s AND pass = %s AND question_fi = %s ",
array('integer', 'integer', 'integer'),
array($active_id, $pass, $this->getId())
);
if($result->numRows())
{
while($row = $ilDB->fetchAssoc($result))
{
$affectedRows = $ilDB->manipulateF("DELETE FROM tst_solutions WHERE solution_id = %s",
array('integer'),
array($row['solution_id'])
);
}
}
//Set new solution
$next_id = $ilDB->nextId('tst_solutions');
$affectedRows = $ilDB->insert("tst_solutions", array(
"solution_id" => array("integer", $next_id),
"active_fi" => array("integer", $active_id),
"question_fi" => array("integer", $this->getId()),
"value1" => array("clob", $_POST["solution_id"]),
"value2" => array("clob", serialize($entries)),
"pass" => array("integer", $pass),
"tstamp" => array("integer", time())
));
 
//Write to ilias log
if($entered_values)
{
include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
if(ilObjAssessmentFolder::_enabledAssessmentLogging())
{
$this->logAction($this->lng->txtlng("assessment", "log_user_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
}
}
else
{
include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
if(ilObjAssessmentFolder::_enabledAssessmentLogging())
{
$this->logAction($this->lng->txtlng("assessment", "log_user_not_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
}
}
 
return true;
}
 
 
/**
* Reworks the allready saved working data if neccessary
* @Override
*/

protected function reworkWorkingData($active_id, $pass, $obligationsAnswered)
{
//Do nothing...
}
 
/**
* Returns the question type of the question
* @Override
*/

public function getQuestionType()
{
return "assVTQuestion";
}
 
/**
* Deletes datasets from answers tables
* @Override
*/

function deleteAnswers($question_id)
{
global $ilDB;
 
$affectedRows = $ilDB->manipulateF("DELETE FROM il_qpl_qst_assvtquestion_solution WHERE question_fi = %s",
array('integer'),
array($question_id)
);
}
 
/**
* Collects all text in the question which could contain media objects which were created with the Rich Text Editor
* @Override
*/

function getRTETextWithMediaObjects()
{
$text = parent::getRTETextWithMediaObjects();
return $text;
}
 
/**
* Creates an Excel worksheet for the detailed cumulated results of this question
* @Override
* @todo nothing impemented yet
*/

public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
{
//Momentarily not implement
}
 
 
/**
* Creates a question from a QTI file
* Receives parameters from a QTI parser and creates a valid ILIAS question object
* @Override
* @todo nothign implement yet
*/

function fromXML(&$item, &$questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
{
//Momentarily not implemented
}
 
 
/**
* Returns a QTI xml representation of the question and sets the internal domxml variable with the DOM XML representation of the QTI xml representation
* @Override
* @todo nothing impmented yet
*/

function toXML($a_include_header = true, $a_include_binary = true, $a_shuffle = false, $test_output = false, $force_image_references = false)
{
//Momentarily not implemented
}
 
}
 
?>

Class: assVTQuestionGUI

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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
<?php
 
/**
* Class to create the GUI for the VTQuestionGUI
* @author Lukas Block
* ...
* @version $Id: class.assVTQuestionGUI.php 1318 2013-08-21 10:19:37Z aifbkitstuds $
* @ingroup ModulesTestQuestionPool
* @ilctrl_iscalledby assVTQuestionGUI: ilObjQuestionPoolGUI, ilObjTestGUI, ilQuestionEditGUI, ilTestExpressPageObjectGUI
*/

 
 
include_once "./Modules/TestQuestionPool/classes/class.assQuestionGUI.php";
include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
 
class assVTQuestionGUI extends assQuestionGUI {
 
/**
* Constructor
* @Override
*/

public function __construct($id = -1) {
parent::__construct();
include_once "./Services/Component/classes/class.ilPlugin.php";
$pl = ilPlugin::getPluginObject(IL_COMP_MODULE, "TestQuestionPool", "qst", "assVTQuestion");
$pl->includeClass("class.assVTQuestion.php");
$this->object = new assVTQuestion();
if ($id >= 0)
{
$this->object->loadFromDb($id);
}
}
 
/**
* Manipulate command
* @Override
*/

function getCommand($cmd)
{
//preg_match done because edit pool delivers the pool id after the "_"
if(preg_match("/editPool_([0-9a-zA-z]+)/", $cmd, $matches))
{
$cmd = "editPool";
} else if(preg_match("/deletePool_([0-9a-zA-Z]+)/", $cmd, $matches))
{
$cmd = "deletePool";
} else if (preg_match("/saveNewPool/", $cmd, $matches)) {
$cmd = "addPool";
}
return $cmd;
}
 
/**
* Proceed submitted changes in the test author panel
* @Override
*/

public function writePostData($always = false) {
$hasErrors = (!$always) ? $this->editQuestion(true) : false;
if(!$hasErrors)
{
//Set the "normal" contents
$this->object->setTitle($_POST["title"]);
$this->object->setAuthor($_POST["author"]);
$this->object->setComment($_POST["comment"]);
$this->object->setPointsPerGap($_POST["points_per_gap"]);
$this->object->setNrOfGaps($_POST["nr_of_gaps"]);
include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
$this->object->setEstimatedWorkingTime(
$_POST["Estimated"]["hh"],
$_POST["Estimated"]["mm"],
$_POST["Estimated"]["ss"]
);
//Set the word pools
if(isset($_POST["pool_fis"])) {
$pool_fis = array();
foreach($_POST["pool_fis"] as $key => $value) {
array_push($pool_fis, $value);
}
$this->object->setPoolFis($pool_fis);
}
//Set case sensitivity
if(isset($_POST["case"])) {
$this->object->setCaseSensitive(true);
} else {
$this->object->setCaseSensitive(false);
}
//Set the texts
$newTexts = array();
foreach($_POST["txt"] as $key => $value) {
$value = strip_tags($value);
$newTexts[] = $value;
}
$this->object->setTexts($newTexts);
return 0;
}
else
{
return 1;
}
}
 
/**
* Function to show the best solution and the solution of the participant
* @Override
*/

public function getSolutionOutput(
$active_id,
$pass = NULL,
$graphicalOutput = FALSE,
$result_output = FALSE,
$show_question_only = TRUE,
$show_feedback = FALSE,
$show_correct_solution = FALSE,
$show_manual_scoring = FALSE,
$show_question_text = TRUE
)
{
//Get the solution of the user for the active pass or from the last pass if allowed
$user_solution = null;
 
if($show_question_text && $result_output) {
return $this->getPreview();
} else {
//Last pass
if($active_id)
{
//Get infos for current pass
$solutions = NULL;
include_once "./Modules/Test/classes/class.ilObjTest.php";
if(is_null($pass)) $pass = ilObjTest::_getPass($active_id);
 
$user_solution["active_id"] = $active_id;
$user_solution["pass"] = $pass;
$solutions =& $this->object->getSolutionValues($active_id, $pass);
 
$solution_id = $solutions[0]["value1"];
$entries = unserialize($solutions[0]["value2"]);
 
$q_text = $this->object->getSolutionText($solution_id);
$q_text = $this->object->prepareTextareaOutput($q_text, TRUE);
 
//Build up GUI
foreach($entries as $key => $entry) {
$anzeige = "";
$bestArray = $this->object->getBestPractice($solution_id);
$best = $bestArray[$key];
if($show_correct_solution) {
$anzeige = $best;
} else {
$anzeige = $entry;
if($result_output) {
$anzeige = $anzeige . " / " . $best;
}
}
 
if($this->object->getCaseSensitive() > 0) {
$best = strtolower($best);
$entry = strtolower($entry);
}
 
//Include images
if ($show_correct_solution){
$icon = "";
} else if ($entry == $best) {
$icon = '<img src="' . ilUtil::getImagePath('icon_ok.png') . '" title="" alt="" />';
}else {
$icon = '<img src="' . ilUtil::getImagePath('icon_not_ok.png') . '" title="" alt="" />';
}
 
//Set up GUI in gaps
$q_text = str_replace("#:" . $key . ":#", '<span class="solutionbox">' . $anzeige . '</span>' . $icon, $q_text);
}
}
 
//Generate the question output from the template
$pl = $this->object->getPlugin();
$template = $pl->getTemplate("tpl.il_as_qpl_assVTQuestion_output.html");
 
$template->setVariable("QUESTIONTEXT", $q_text);
 
$questionoutput = $template->get();
$pageoutput = $this->outQuestionPage("", $is_postponed, $active_id, $questionoutput);
return $pageoutput;
}
}
 
/**
* Saves the feedback
* @Override
*/

public function saveFeedback() {
parent::saveFeedback();
}
 
/**
* Sets and adds the tabs for the questions
* @Override
*/

public function setQuestionTabs() {
global $rbacsystem, $ilTabs;
 
//Initlialize Tabs
$this->ctrl->setParameterByClass("ilpageobjectgui", "q_id", $_GET["q_id"]);
include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
$q_type = $this->object->getQuestionType();
 
if(strlen($q_type))
{
$classname = $q_type . "GUI";
$this->ctrl->setParameterByClass(strtolower($classname), "sel_question_types", $q_type);
$this->ctrl->setParameterByClass(strtolower($classname), "q_id", $_GET["q_id"]);
}
 
if($_GET["q_id"])
{
//check permission
if($rbacsystem->checkAccess('write', $_GET["ref_id"]))
{
// edit page
$ilTabs->addTarget("edit_content",
$this->ctrl->getLinkTargetByClass("ilPageObjectGUI", "edit"),
array("edit", "insert", "exec_pg"),
"", "", $force_active);
}
 
// Preview
$ilTabs->addTarget("preview",
$this->ctrl->getLinkTargetByClass("ilPageObjectGUI", "preview"),
array("preview"),
"ilPageObjectGUI", "", $force_active);
}
 
//Set force_active here to false, because used to force the word_pools tab to be active if shown...
$force_active = false;
if($rbacsystem->checkAccess('write', $_GET["ref_id"]))
{
$url = "";
 
if($classname) $url = $this->ctrl->getLinkTargetByClass($classname, "editQuestion");
//Check whether "editPool" or "deletePool" command has been proceeded
$commands = $_POST["cmd"];
if(is_array($commands))
{
foreach($commands as $key => $value)
{
if(preg_match("/editPool_([0-9a-zA-z]+)/", $this->ctrl->getCmd(), $matches))
{
$force_active = true;
} else if(preg_match("/deletePool_([0-9a-zA-z]+)/", $this->ctrl->getCmd(), $matches))
{
$force_active = true;
}
}
}
//Edit question properties
$ilTabs->addTarget("edit_properties",
$url,
array(
"editQuestion", "save", "cancel", "addSuggestedSolution",
"cancelExplorer", "linkChilds", "removeSuggestedSolution",
"parseQuestion", "saveEdit", "suggestRange"
),
$classname, "");
 
//Pool editor
if($_GET["q_id"])
{
//The first is responsible for the naming of the tab
//The second parameter is the function which is called, clicking it
$ilTabs->addTarget("qpl_qst_assVTQuestion_word_pools",
$this->ctrl->getLinkTargetByClass($classname, "showPoolList"),
array("showPoolList", "savePools", "addPool", "saveNewPool", "savePoolChanges", "savePoolChangesAndReturn"),
$classname, "", $force_active);
}
}
 
 
//Assessment of questions sub menu entry
if($_GET["q_id"])
{
$ilTabs->addTarget("statistics",
$this->ctrl->getLinkTargetByClass($classname, "assessment"),
array("assessment"),
$classname, "");
}
 
if(($_GET["calling_test"] > 0) || ($_GET["test_ref_id"] > 0))
{
$ref_id = $_GET["calling_test"];
if(strlen($ref_id) == 0) $ref_id = $_GET["test_ref_id"];
$ilTabs->setBackTarget($this->lng->txt("backtocallingtest"), "ilias.php?baseClass=ilObjTestGUI&cmd=questions&ref_id=$ref_id");
}
else
{
$ilTabs->setBackTarget($this->lng->txt("qpl"), $this->ctrl->getLinkTargetByClass("ilobjquestionpoolgui", "questions"));
}
}
 
/**
* Represents and builds up the word list word pools tab
* @param a possible message to be shown like "Saving successfull"
*/

function showPoolList($message = "") {
global $ilDB;
 
//Create Form...
include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
$form = new ilPropertyFormGUI();
$form->setFormAction($this->ctrl->getFormAction($this));
$form->setTitle($this->object->getPlugin()->txt("word_pools"));
$form->setMultipart(FALSE);
$form->setTableWidth("100%");
$form->setId("assvtquestion_pool");
 
//Add buttons
$form->addCommandButton("addPool", $this->object->getPlugin()->txt("add_pool"));
 
include_once("./Customizing/global/plugins/Modules/TestQuestionPool/Questions/assVTQuestion/classes/class.ilVTPoolGUI.php");
$all_pools = $ilDB->queryF("SELECT * FROM il_qpl_qst_assVTQuestion_pool", array(), array());
for($i = 0; $i < $all_pools->numRows(); $i++) {
$data = $ilDB->fetchAssoc($all_pools);
$word_fis = unserialize($data["word_fis"]);
$words = array();
foreach($word_fis as $key => $value) {
$word_db = $ilDB->queryF("SELECT word FROM il_qpl_qst_assVTQuestion_word WHERE word_id=%s", array("integer"), array($value));
$word_data = $ilDB->fetchAssoc($word_db);
array_push($words, $word_data["word"]);
}
$pool = new ilVTPoolGUI($data["title"], "", $data["pool_id"], $this->object->getPlugin(), $words);
$pool->setRequired(FALSE);
$form->addItem($pool);
}
 
$gui = $message . $form->getHTML();
 
$this->tpl->setVariable("ADM_CONTENT", $gui);
include_once "./Services/YUI/classes/class.ilYuiUtil.php";
ilYuiUtil::initDomEvent();
ilYuiUtil::initDragDropList();
}
 
/**
* Called to add a new word pool
*/

function addPool() {
 
$save = strcmp($this->ctrl->getCmd(), "saveNewPool") == 0;
$checkAgain = !$save;
 
$form = $this->buildAddForm();
if ($save)
{
$form->setValuesByPost();
$errors = !$form->checkInput();
$form->setValuesByPost(); //Again, because checkInput now performs the whole stripSlashes handling and we need this if we don't want to have duplication of backslashes
if ($errors) {
$checkAgain = true;
}
}
if((!$save || $checkAgain)) {
$this->tpl->setVariable("ADM_CONTENT", $form->getHTML());
include_once "./Services/YUI/classes/class.ilYuiUtil.php";
ilYuiUtil::initDomEvent();
ilYuiUtil::initDragDropList();
} else {
$this->saveNewPool($_POST["title"]);
}
}
 
/**
* Creates the form to enter the name of the new word pool
*/

function buildAddForm() {
//Create Form...
include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
$form = new ilPropertyFormGUI();
$form->setFormAction($this->ctrl->getFormAction($this));
$form->setTitle($this->object->getPlugin()->txt("create_new_pool"));
$form->setMultipart(FALSE);
$form->setTableWidth("100%");
$form->setId("assvtquestion_pool");
 
//Title
$title = new ilTextInputGUI($this->object->getPlugin()->txt("pool_title"), "title");
$title->setValue("");
$title->setRequired(TRUE);
$form->addItem($title);
 
//Add buttons
$form->addCommandButton("saveNewPool", $this->object->getPlugin()->txt("save_new_pool"));
$form->addCommandButton("showPoolList", $this->object->getPlugin()->txt("cancel"));
 
return $form;
}
 
/**
* Creates a new pool from the submitted data
* Can only be called from addPool because of $_POST
* @param the title of the new pool
*/

function saveNewPool($title) {
global $ilDB;
//insert new entry
//Get actual time
$date = new DateTime();
$next_id = $ilDB->nextId('il_qpl_qst_assVTQuestion_pool');
$emptyArray = array();
$ilDB->insert("il_qpl_qst_assVTQuestion_pool", array(
"pool_id" => array("integer", $next_id),
"author" => array("clob", $this->object->getAuthor()),
"owner" => array("clob", $this->object->getOwner()),
"title" => array("clob", $title),
"tstamp" => array("integer", $date->getTimestamp()),
"word_fis" => array("clob", serialize($emptyArray)),
));
//Go back to the list...
$this->showPoolList($this->getMessage("success", "info", "pool_xyz_has_been_created", "xyz", $title));
}
 
/**
* Called to edit a pool
*/

function editPool() {
global $ilDB;
 
preg_match("/editPool_([0-9a-zA-z]+)/", $this->ctrl->getCmd(), $matches);
//$poolId represents either pool id (load from db) or the word "post" => load from post, check and save
$poolId = $matches[1];
$post = ((strpos($poolId, "post") !== false) ? TRUE : FALSE);
$return = ((strpos($poolId, "return") !== false) ? TRUE : FALSE);
if($post) {
//Load from post data
$poolId = $_POST["pool_id"];
$poolTitle = $_POST["title"];
$words = $_POST["word"];
} else {
//Load from db
$db_pool = $ilDB->queryF("SELECT * FROM il_qpl_qst_assVTQuestion_pool WHERE pool_id = %s", array("integer"), array($poolId));
$data = $ilDB->fetchAssoc($db_pool);
$poolTitle = $data["title"];
$word_fis = unserialize($data["word_fis"]);
$words = array();
foreach($word_fis as $key => $value) {
$db_word = $ilDB->queryF("SELECT word FROM il_qpl_qst_assVTQuestion_word WHERE word_id = %s", array("integer"), array($value));
$word_data = $ilDB->fetchAssoc($db_word);
array_push($words, $word_data["word"]);
}
}
$form = $this->buildEditForm($poolId, $poolTitle, $words);
if($post) {
$form->setValuesByPost();
$wordNr = $this->checkRegex($words);
$wordsOk = ($wordNr == -1);
if($form->checkInput() && $wordsOk) {
//Form okay save and eventually return
$this->savePoolChanges($poolId, $poolTitle, $words);
if(!$return) {
//no return to pool list
$form = $this->buildEditForm($poolId, $poolTitle, $words);
$this->showEditForm($form, $this->getMessage("success", "info", "pool_changes_saved"));
} else {
//return to pool list
$this->showPoolList($this->getMessage("success", "info", "pool_changes_saved"));
}
} else {
$message = "";
//Form not okay, show Form again
if(!$wordsOk) {
$message = $this->getMessage("failure", "info", "word_xyz_is_not_a_regular_expression_with_brace", "xyz", ($wordNr+1));
}
$form = $this->buildEditForm($poolId, $poolTitle, $words);
$this->showEditForm($form, $message);
}
} else {
//Form is shown first time...
$this->showEditForm($form);
}
}
 
/**
* Checks an array of words if they are in a proper regex format also containing the ()
* @words array of words to check
* @return the number of the word (not the id) which is no regular regex, if all words are correct -1
*/

function checkRegex($words) {
$i = 0;
foreach($words as $key => $value) {
if(!$this->isEmptyWord($value)) {
//Set to let preg_match throw an error not a warning
set_error_handler(create_function(
'$errno, $errstr, $errfile, $errline',
'throw new ErrorException($errstr, 0, $errno, $errfile, $errline);'
));
//Check if regular expression is valid...
try {
preg_match($value, "Empty string");
} catch (Exception $e) {
restore_error_handler();
return $i;
}
restore_error_handler();
//Check if braces exist
if(!preg_match("/\(.+\)/", $value)) {
return $i;
}
$i++;
}
}
restore_error_handler();
return -1;
}
 
/**
* Checks if an word ist empty
* @param $word The word to be checked
* @return true, if the word is empty, otherwise false
*/

function isEmptyWord($word) {
if(preg_match("/[a-zA-Z0-9]+/", $word)) {
return false;
} else {
return true;
}
}
 
/**
* Builds the edit-Form of a word pool - called from editPool and SavePoolChanges
* @param $poolId the id of the pool to build
* @param $poolTitle the title of the pool to build
* @param $words the words the pool consists of
* @return the form in HTML format
*/

function buildEditForm($poolId, $poolTitle, $words) {
//Create Form...
include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
$form = new ilPropertyFormGUI();
$form->setFormAction($this->ctrl->getFormAction($this));
$form->setTitle($this->object->getPlugin()->txt("edit_pool"));
$form->setMultipart(FALSE);
$form->setTableWidth("100%");
$form->setId("assvtquestion_pool");
 
//Add buttons
$form->addCommandButton("editPool_post", $this->object->getPlugin()->txt("save_pool"));
$form->addCommandButton("editPool_post_return", $this->object->getPlugin()->txt("save_pool_and_return"));
$form->addCommandButton("showPoolList", $this->object->getPlugin()->txt("cancel"));
 
//Add the pool id to be submitted to work on the pool
$hidden = new ilHiddenInputGUI("pool_id", "pool_id");
$hidden->setValue($poolId);
$form->addItem($hidden);
 
// title
$title = new ilTextInputGUI($this->object->getPlugin()->txt("pool_title"), "title");
$title->setValue($poolTitle);
$title->setRequired(TRUE);
$form->addItem($title);
 
//Show all the words
include_once("./Customizing/global/plugins/Modules/TestQuestionPool/Questions/assVTQuestion/classes/class.ilVTTextInputGUI.php");
$no = 0;
foreach($words as $key => $value) {
if(!$this->isEmptyWord($value)) {
$word = new ilVTTextInputGUI($this->object->getPlugin()->txt("word") . " " . ($key+1), "word[]", $key);
$word->setValue($value);
$word->setRequired(FALSE);
$form->addItem($word);
$no++;
}
}
//If there are no words in the pool, provide at least one empty field
if($no == 0) {
$word = new ilVTTextInputGUI($this->object->getPlugin()->txt("word") . " 1", "word[]", 0);
$word->setValue("");
$word->setRequired(FALSE);
$form->addItem($word);
}
 
return $form;
}
 
/**
* Shows the edit-Form of a word pool
* @param $form the form to show
* @param $messages the possible message which should be shown
*/

function showEditForm($form, $messages = "") {
$gui = $messages . $form->getHTML() . '<br><br><i style="font-size:10pt;">' . $this->object->getPlugin()->txt("annotation_regex") .'</i>';
$this->tpl->setVariable("ADM_CONTENT", $gui);
include_once "./Services/YUI/classes/class.ilYuiUtil.php";
ilYuiUtil::initDomEvent();
ilYuiUtil::initDragDropList();
$this->tpl->addJavascript($this->translateJs($this->object->getPlugin()->getDirectory() . "/templates/js/js_raw/vtpoolwizard.js"));
}
 
/**
* Called from savePoolChanges and savePoolChangesAndReturn.
* Because of $_POST can just be called when these functions are also called.
* @param $poolId the id of the pool to save the changes
* @param $poolTitle the new title of the pool
* @param $words all words of the pool
* @return the new pool id
*/

function savePoolChanges($poolId, $poolTitle, $words) {
global $ilDB;
 
//Delete old words
$db_pool = $ilDB->queryF("SELECT * FROM il_qpl_qst_assVTQuestion_pool WHERE pool_id = %s", array("integer"), array($poolId));
$data = $ilDB->fetchAssoc($db_pool);
$delete_words = unserialize($data["word_fis"]);
foreach($delete_words as $key => $value) {
$ilDB->manipulateF("DELETE FROM il_qpl_qst_assVTQuestion_word WHERE word_id = %s",
array("integer"),
array($value));
}
//Write new ones
$newWordsId = array();
$date = new DateTime();
foreach($words as $key => $value) {
//Check if not empty
if(preg_match("/[a-zA-Z0-9]+/", $value, $matches)) {
$next_id = $ilDB->nextId('il_qpl_qst_assvtquestion_word');
$affectedRows = $ilDB->insert("il_qpl_qst_assvtquestion_word", array(
"word_id" => array("integer", $next_id),
"author" => array("clob", $this->object->getAuthor()),
"tstamp" => array("integer", $date->getTimestamp()),
"word" => array("clob", $value)
));
array_push($newWordsId, $next_id);
}
}
 
//Save changes to the pool itself...
//Delete old entry
$ilDB->manipulateF("DELETE FROM il_qpl_qst_assVTQuestion_pool WHERE pool_id = %s",
array("integer"),
array($poolId));
//Insert new entry
//Keep id because of questions
$ilDB->insert("il_qpl_qst_assVTQuestion_pool", array(
"pool_id" => array("integer", $poolId),
"author" => array("clob", $data["author"]),
"owner" => array("clob", $data["owner"]),
"title" => array("clob", $poolTitle),
"tstamp" => array("integer", $date->getTimestamp()),
"word_fis" => array("clob", serialize($newWordsId)),
));
}
 
/**
* Called to delete a word pool
*/

function deletePool() {
global $ilDB;
 
preg_match("/deletePool_([0-9a-zA-Z]+)/", $this->ctrl->getCmd(), $matches);
$poolId = $matches[1];
$confirmed = (($poolId == "confirmed") ? TRUE : FALSE);
if(!$confirmed) {
$db_pool = $ilDB->queryF("SELECT title FROM il_qpl_qst_assVTQuestion_pool WHERE pool_id = %s", array("integer"), array($poolId));
$data = $ilDB->fetchAssoc($db_pool);
$this->showDeleteForm($poolId, $data["title"]);
} else {
$poolId = $_POST["pool_id"];
$pool = $ilDB->queryF("SELECT word_fis FROM il_qpl_qst_assVTQuestion_pool WHERE pool_id=%s", array("integer"), array($poolId));
$data = $ilDB->fetchAssoc($pool);
//Delete words
$words = unserialize($data["word_fis"]);
foreach($words as $key => $value) {
$ilDB->manipulateF("DELETE FROM il_qpl_qst_assVTQuestion_word WHERE word_id = %s",
array("integer"),
array($value));
}
//Delete pool
$ilDB->manipulateF("DELETE FROM il_qpl_qst_assVTQuestion_pool WHERE pool_id = %s",
array("integer"),
array($poolId));
 
//Show list
$this->showPoolList($this->getMessage("success", "info", "pools_deleted"));
}
}
 
/**
* Function to create and show the form, which asks to delete the pool
* @param $poolId the id of the word pool to delete
* @param $poolTitle the title of the word pool to delete
*/

function showDeleteForm($poolId, $poolTitle) {
//Create Form...
include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
$form = new ilPropertyFormGUI();
$form->setFormAction($this->ctrl->getFormAction($this));
$form->setTitle($this->object->getPlugin()->txt("delete_pool"));
$form->setMultipart(FALSE);
$form->setTableWidth("100%");
$form->setId("assvtquestion_pool");
 
//Add buttons
$form->addCommandButton("deletePool_confirmed", $this->object->getPlugin()->txt("delete_pool"));
$form->addCommandButton("showPoolList", $this->object->getPlugin()->txt("cancel"));
 
//Add the pool id to be submitted to work on the pool
$hidden = new ilHiddenInputGUI("pool_id", "pool_id");
$hidden->setValue($poolId);
$form->addItem($hidden);
 
include_once("./Customizing/global/plugins/Modules/TestQuestionPool/Questions/assVTQuestion/classes/class.ilVTDeleteGUI.php");
$pool = new ilVTDeleteGUI($poolTitle, "", $poolId, $this->object->getPlugin());
$pool->setRequired(FALSE);
$form->addItem($pool);
 
$gui = $this->getMessage("question", "decision", "really_delete_pools") . $form->getHTML();
 
$this->tpl->setVariable("ADM_CONTENT", $gui);
include_once "./Services/YUI/classes/class.ilYuiUtil.php";
ilYuiUtil::initDomEvent();
ilYuiUtil::initDragDropList();
}
 
/**
* Function to create an info-, error-, failure,- oder success-Message
* @param $a_type = "info" or "question" or "failure" or "success"
* @param $title The title of the message
* @param $txt The message text
* @param $search a string for which should be searched and can be replaced
* @param $replace the string which replaces the word for what to search
* @return the message as an HTML-string
*/

function getMessage($a_type, $title, $txt, $search = "", $replace = "") {
//$a_type = info, question, failure, success
$mtpl = new ilTemplate("tpl.message.html", true, true, "Services/Utilities");
$mtpl->setCurrentBlock($a_type."_message");
$m_text = $this->object->getPlugin()->txt($txt);
if(($search != "") && ($replace != "")) {
$m_text = str_replace($search, $replace, $m_text);
}
$mtpl->setVariable("TEXT", $m_text);
$mtpl->setVariable("MESSAGE_HEADING", $this->object->getPlugin()->txt($title));
$mtpl->setVariable("ALT_IMAGE", "");
$mtpl->setVariable("SRC_IMAGE", ilUtil::getImagePath("mess_".$a_type.".png"));
$mtpl->parseCurrentBlock();
return $mtpl->get();
}
 
 
/**
* Returns feedback for test-participant if enabled in test
* @Override
*/

public function getSpecificFeedbackOutput($active_id, $pass)
{
if($this->object->calculateReachedPoints($active_id, $pass) >= $this->object->getMaximumPoints()) {
return $this->object->getPlugin()->txt("feedback_all_right");
} else {
return $this->object->getPlugin()->txt("feedback_some_mistakes");
}
}
 
/**
* Checks the questions for the number of gaps
* @return error-string if not enough gaps otherwise ""
*/

function checkInput()
{
$key = $this->object->checkNrOfGaps();
if($key > -1) {
return $this->getMessage("info", "info", "txt_xyz_has_insufficient_gaps", "xyz", $key+1);
}
return "";
}
 
/**
* Creates and shows the main edit-question-form
* @Override
*/

public function editQuestion() {
global $lng;
global $ilDB;
 
$save = ((strcmp($this->ctrl->getCmd(), "save") == 0) || (strcmp($this->ctrl->getCmd(), "saveEdit") == 0)) ? TRUE : FALSE;
$this->getQuestionTemplate();
 
include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
$form = new ilPropertyFormGUI();
$form->setFormAction($this->ctrl->getFormAction($this));
$form->setTitle($this->outQuestionType());
$form->setMultipart(FALSE);
$form->setTableWidth("100%");
$form->setId("assvtquestion");
 
// title
$title = new ilTextInputGUI($this->lng->txt("title"), "title");
$title->setValue($this->object->getTitle());
$title->setRequired(TRUE);
$form->addItem($title);
 
if (!$this->getSelfAssessmentEditingMode())
{
// author
$author = new ilTextInputGUI($this->lng->txt("author"), "author");
$author->setValue($this->object->getAuthor());
$author->setRequired(TRUE);
$form->addItem($author);
 
// description
$description = new ilTextInputGUI($this->lng->txt("description"), "comment");
$description->setValue($this->object->getComment());
$description->setRequired(FALSE);
$form->addItem($description);
}
else
{
// author as hidden field
$hi = new ilHiddenInputGUI("author");
$author = ilUtil::prepareFormOutput($this->object->getAuthor());
if (trim($author) == "")
{
$author = "-";
}
$hi->setValue($author);
$form->addItem($hi);
 
}
 
if (!$this->getSelfAssessmentEditingMode())
{
// duration
$duration = new ilDurationInputGUI($this->lng->txt("working_time"), "Estimated");
$duration->setShowHours(TRUE);
$duration->setShowMinutes(TRUE);
$duration->setShowSeconds(TRUE);
$ewt = $this->object->getEstimatedWorkingTime();
$duration->setHours($ewt["h"]);
$duration->setMinutes($ewt["m"]);
$duration->setSeconds($ewt["s"]);
$duration->setRequired(FALSE);
$form->addItem($duration);
}
else
{
// number of tries
if (strlen($this->object->getNrOfTries()))
{
$nr_tries = $this->object->getNrOfTries();
}
else
{
$nr_tries = $this->getDefaultNrOfTries();
}
if ($nr_tries <= 0)
{
$nr_tries = 1;
}
$ni = new ilNumberInputGUI($this->lng->txt("qst_nr_of_tries"), "nr_of_tries");
$ni->setValue($nr_tries);
$ni->setMinValue(1);
$ni->setSize(5);
$ni->setMaxLength(5);
$ni->setRequired(true);
$form->addItem($ni);
}
 
// points
$points = new ilNumberInputGUI($this->object->getPlugin()->txt("points_per_gap"), "points_per_gap");
$points->setValue($this->object->getPointsPerGap());
$points->setRequired(TRUE);
$points->setSize(3);
$points->setMinValue(0.0);
$form->addItem($points);
 
//number of gaps
$nr_of_gaps = new ilNumberInputGUI($this->object->getPlugin()->txt("nr_of_gaps"), "nr_of_gaps");
$nr_of_gaps->setValue($this->object->getNrOfGaps());
$nr_of_gaps->setRequired(TRUE);
$nr_of_gaps->setSize(3);
$nr_of_gaps->setMinValue(1);
$form->addItem($nr_of_gaps);
 
//Word-Pools
$pools = new ilCheckboxGroupInputGUI($this->object->getPlugin()->txt("word_pools"), "pool_fis");
$pools->setRequired(true);
$all_pools = $ilDB->queryF("SELECT * FROM il_qpl_qst_assVTQuestion_pool", array(), array());
$values = array();
for($i = 0; $i < $all_pools->numRows(); $i++) {
$data = $ilDB->fetchAssoc($all_pools);
$pools->addOption(new ilCheckboxOption($data["title"], $data["pool_id"], ''));
if(in_array($data["pool_id"], $this->object->getPoolFis())) {
array_push($values, $data["pool_id"]);
}
}
$pools->setValue($values);
$form->addItem($pools);
 
//Case sensitivity
$case = new ilCheckboxGroupInputGUI($this->object->getPlugin()->txt("case_sensitivity"), "case");
$case->setRequired(false);
$checked = array();
$case->addOption(new ilCheckboxOption($this->object->getPlugin()->txt("case_insensitive"), 'caseSens', ''));
if($this->object->getCaseSensitive() > 0) {
array_push($checked, 'caseSens');
}
$case->setValue($checked);
$form->addItem($case);
 
if ($this->object->getId())
{
$hidden = new ilHiddenInputGUI("", "ID");
$hidden->setValue($this->object->getId());
$form->addItem($hidden);
}
 
$this->addQuestionFormCommandButtons($form);
 
$errors = false;
 
if ($save)
{
$form->setValuesByPost();
$errors = !$form->checkInput();
$form->setValuesByPost(); // again, because checkInput now performs the whole stripSlashes handling and we need this if we don't want to have duplication of backslashes
if ($errors) $checkonly = false;
}
 
//Construct texts
include_once("./Customizing/global/plugins/Modules/TestQuestionPool/Questions/assVTQuestion/classes/class.ilVTTextAreaInputGUI.php");
$texts = $this->object->getTexts();
if(sizeof($texts) == 0) {
$texts[] = "";
}
$texts_gui = "";
foreach($texts as $key => $value) {
//Enter text
$txt = new ilVTTextAreaInputGUI($this->object->getPlugin()->txt("text") . " " . ($key+1), "txt[]", $key);
$txt->setRows(10);
$txt->setCols(75);
$txt->setValue($value);
$txt->setRequired(FALSE);
$form->addItem($txt);
}
 
$gui = $this->checkInput() . $form->getHTML();
 
if (!$checkonly) $this->tpl->setVariable("QUESTION_DATA", $gui);
 
include_once "./Services/YUI/classes/class.ilYuiUtil.php";
ilYuiUtil::initConnection();
ilYUIUtil::initDomEvent();
$this->tpl->addJavascript($this->translateJs($this->object->getPlugin()->getDirectory() . "/templates/js/js_raw/vtquestionwizard.js"));
return $errors;
}
 
/**
* Function to translate the plain text written in a .js file, writes other file one directory up
* @param the path to translate
* @return the path to the translated file (normally one directory up)
*/

function translateJs($pathToFile) {
$info = new SplFileInfo($pathToFile);
$path = $info->getPath();
$file = $info->getFilename();
$content = file_get_contents($path . "/" . $file);
$matches;
$found = preg_match_all("/#:#(.+)#:#/", $content, $matches);
for($i = 0; $i < $found; $i++) {
$content = str_replace($matches[0][$i], $this->object->getPlugin()->txt($matches[1][$i]), $content);
}
$newPath = str_replace(basename($path), "", $path) . $file;
file_put_contents($newPath, $content);
return $newPath;
}
 
/**
* Shows question in preview-mode while editing test
* @Override
*/

function getPreview($show_question_only = FALSE)
{
$pl = $this->object->getPlugin();
$template = $pl->getTemplate("tpl.il_as_qpl_assVTQuestion_output.html");
$txts = $this->object->getTexts();
//Randomly select one text
$txt_index = rand(0, count($txts)-1);
 
$template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($txts[$txt_index], TRUE));
$questionoutput = $template->get();
//Putoutput into ilias page
if(!$show_question_only)
{
// get page object output
$questionoutput = $this->getILIASPage($questionoutput);
}
return $questionoutput;
}
 
/**
* Creates the output for the test-participant
* @Override
*/

function outQuestionForTest($formaction, $active_id, $pass = NULL, $is_postponed = FALSE, $use_post_solutions = FALSE, $show_feedback = FALSE)
{
$test_output = $this->getTestOutput($active_id, $pass, $is_postponed, $use_post_solutions, $show_feedback);
//Put question text into the template
$this->tpl->setVariable("QUESTION_OUTPUT", $test_output);
//Set the URL for the following question to the "Continue"-Button
$this->tpl->setVariable("FORMACTION", $formaction);
}
 
/**
* Helper-function to create the test-ouput for the participant
* @param the same as outQuestionForTest except of $formaction
* @return the question text in HTML-format
*/

function getTestOutput($active_id, $pass = NULL, $is_postponed = FALSE, $use_post_solutions = FALSE, $show_feedback = FALSE)
{
//Get the solution of the user for the active pass or from the last pass if allowed
$user_solution = null;
 
if($active_id)
{
//Get infos for current pass
$solutions = NULL;
include_once "./Modules/Test/classes/class.ilObjTest.php";
if(is_null($pass)) $pass = ilObjTest::_getPass($active_id);
 
$user_solution["active_id"] = $active_id;
$user_solution["pass"] = $pass;
$solutions =& $this->object->getSolutionValues($active_id, $pass);
 
//If value1 is empty, the question has not been shown yet -> Generate new question
if($solutions[0]["value1"] == "") {
$solution_id = $this->object->generateQuestion();
$entries = $this->object->getBestPractice($solution_id);
//Clear the best practice
foreach($entries as $key => $value) {
$entries[$key] = "";
}
} else {
//Loading the old questions
$solution_id = $solutions[0]["value1"];
$entries = unserialize($solutions[0]["value2"]);
}
$q_text = $this->object->getSolutionText($solution_id);
$q_text = $this->object->prepareTextareaOutput($q_text, TRUE);
 
foreach($entries as $key => $value) {
$best = $this->object->getBestPractice($solution_id);
$q_text = str_replace("#:" . $key . ":#", '<input type="text" maxlength="255" size="' . strlen($best[$key]) . '" name="data[' . $key . ']" value="' . $value . '" />', $q_text);
}
 
//Append hidden field for solution_id
$q_text = $q_text . '<input type="hidden" id="" name="solution_id" value="' . $solution_id . '"/>';
}
 
//Generate the question output
$pl = $this->object->getPlugin();
$template = $pl->getTemplate("tpl.il_as_qpl_assVTQuestion_output.html");
 
$template->setVariable("QUESTIONTEXT", $q_text);
 
$questionoutput = $template->get();
$pageoutput = $this->outQuestionPage("", $is_postponed, $active_id, $questionoutput);
return $pageoutput;
}
 
}
 
?>

All relevant data concerning development:

PHP-Documentation

Last edited: 31. Mar 2014, 13:59, Killing, Alexander [alex]