Home

Parse File Aiken (moodle)

Mari berdiskusi bersama kami di Group Facebook Kurung Kurawal

Script berikut berguna untuk melakukan parsing terhadap file (plain text) dengan format aiken yang digunakan oleh moodle. Diharapkan script ini dapat menangani kondisi dimana terdapat kata “ANSWER: ” pada soal ataupun salah satu pilihan jawabannya. Script ini dibuat tanpa bermaksud menyinggung siapapun, hanya ingin berbagi saja.

Contoh file aiken,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
What is the correct answer to this question?
A. Is it this one?
B. Maybe this answer?
C. Possibly this one?
D. Must be this one!
ANSWER: D
 
Which ANSWER: LMS has the most quiz import formats?
A) Moodle
B) ATutor
C) Claroline
D) Blackboard ANSWER:
E) WebCT
F) ANSWER: Ilias
ANSWER: A

Script parser file aiken yang digunakan adalah

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
<?php
/*
SOURCE: http://docs.moodle.org/22/en/Aiken_format
 
a. The question must be all on one line.
b. Each answer must start with a single uppercase letter,
c. followed by a period '.' or a bracket ')', then a space.
d. The answer line must immediately follow, starting with "ANSWER: " (NOTE the space after the colon)
and then giving the appropriate letter.
 
NOTE: script dibawah mengabaikan syarat b
*/
function parseAikenFormat($file)
{
        $result = array();
 
        if(!is_readable($file))
                return FALSE;
 
        $lines = file($file);
        if(!is_array($lines))
                return FALSE;
 
        $start = true;
        $question = array();
        foreach($lines as $line)
        {
                $line = trim($line);
                if($line == "") continue;
 
                if($start)
                {
                        $question = array(
                                'question' => $line, // syarat a
                                'options' => array()
                        );
                        $start = false;
                }
                else if(preg_match("/^ANSWER:\s{1}/", $line))
                {
                         //syarat d, starting with "ANSWER: "
                        $answer = trim(preg_replace("/^ANSWER:\s{1}/", '', $line));
                        $question['answer'] = $answer;
                        $result[] = $question;
                        $start = true;
                }
                else
                {
                        $options = explode(' ', $line, 2);
                        list($opt, $optText) = $options;
                        // syarat c '.' atau ')'
                        $opt = trim(preg_replace("/\.|\)/", '', $opt)); 
                        $question['options'][$opt] = trim($optText);
                }
        }
 
        return $result;
}
 
$aikenFile = "aiken-example.txt";
$parsed = parseAikenFormat($aikenFile);
 
print_r($parsed);

Script ini hanya bertujuan untuk “membaca file aiken” dan menjadikannya format array php, hasil dari parsing tersebut harus diproses lebih lanjut, misalnya, ditampilkan, disimpan dalam database, atau lainnya. Demikian, semoga script ini dapat berguna.