Baca File Format Aiken (Moodle) dengan PHP

Format Aiken merupakan salah satu format file yang sangat sederhana yang dapat digunakan untuk membuat pertanyaan jenis pilihan ganda (multiple choice) di aplikasi Moodle (sebuah aplikasi e-learning open source). Format Aiken memiliki kelebihan yaitu dapat dibuat dengan bantuan aplikasi pengolah teks biasa seperti Notepad, WordPad, GEdit dan sebagainya. Kelemahannya memang tidak bisa mengimport pertanyaan yang memiliki gambar. Untuk lebih jelasnya mengenai format AIKEN ini dapat dilihat di dokumentasi resmi Moodle.

Pada tutorial ini, saya iseng-iseng bikin program PHP sederhana untuk membaca format Aiken tersebut dan memasukannya menjadi sebuah array. Sebenarnya program ini saya perlukan untuk melengkapi sistem pembelajaran online di D3 Unggulan, Fakultas Teknologi Informasi, Universitas Budi Luhur yang pernah saya buat. Dengan demikian, bank-bank soal yang sudah saya buat dengan format Aiken untuk keperluan kuliah online di sistem elearning S1 dapat digunakan dengan cara import langsung, tanpa perlu mengentri satu-satu lagi. :D

Berikut ini source code program tersebut.

  1. <html>
  2. <head><title>Moodle's Aiken Format Importer</title></head>
  3. <body>
  4. <h1>Choose file to import</h1>
  5. <form action="" method="post" enctype="multipart/form-data">
  6. <input type="file" name="file"/>
  7. <input type="submit" name="Import" value="Import"/>
  8. </form>
  9. <?php
  10. //upload
  11. if(isset($_POST['Import'])) {
  12. if (is_uploaded_file($_FILES['file']['tmp_name'])) {
  13. //read file
  14. $questions = array();
  15. $temp = array();
  16. $handle = fopen($_FILES['file']['tmp_name'], "r");
  17. if ($handle) {
  18. while ($baris = fgets($handle, 4096)) {
  19. if (empty($baris) or trim($baris)=="") {
  20. continue;
  21. }
  22. //echo strip_tags($baris)."<br/>";
  23. $temp[] = trim($baris);
  24. if (preg_match("/ANSWER:/", $baris)) {
  25. array_push($questions, $temp);
  26. $temp = array();
  27. }
  28. }
  29. } else {
  30. die ("Cannot read the file");
  31. }
  32. } else {
  33. die ("File not uploaded");
  34. }
  35. //
  36. $newquestions = array();
  37. foreach($questions as $question) {
  38. $tmp = array();
  39.  
  40. //get the first member
  41. $question_text = array_shift($question);
  42. //get the last member
  43. $answer = array_pop($question);
  44. $answer = substr($answer,-1);
  45. //the options
  46. $options = array();
  47. for($i=0; $i<count($question); $i++) {
  48. $options[$i]['text'] = htmlspecialchars(substr($question[$i],3));
  49.  
  50. $options[$i]['right_answer'] = false;
  51. if ($i== ord($answer)-65) {
  52. $options[$i]['right_answer'] = true;
  53. }
  54. }
  55.  
  56. $tmp['question'] = htmlspecialchars($question_text);
  57. $tmp['options'] = $options;
  58.  
  59. array_push($newquestions, $tmp);
  60. }
  61.  
  62. echo "<pre>"; print_r ($newquestions); echo"</pre>";
  63. }
  64. ?>
  65. </body>
  66. </html>

Dan berikut ini alternatif parsing file Format Aiken yang dibuat oleh Konglie Huang (terima kasih atas masukannya, begitu indahnya berbagi pengetahuan….)

  1. <?php
  2. /*
  3. SOURCE: http://docs.moodle.org/22/en/Aiken_format
  4.  
  5. a. The question must be all on one line.
  6. b. Each answer must start with a single uppercase letter,
  7. c. followed by a period '.' or a bracket ')', then a space.
  8. d. The answer line must immediately follow, starting with "ANSWER: " (NOTE the space after the colon)
  9. and then giving the appropriate letter.
  10.  
  11. NOTE: script dibawah mengabaikan syarat b
  12. */
  13. function parseAikenFormat($file)
  14. {
  15. $result = array();
  16.  
  17. if(!is_readable($file))
  18. return FALSE;
  19.  
  20. $lines = file($file);
  21. if(!is_array($lines))
  22. return FALSE;
  23.  
  24. $start = true;
  25. $question = array();
  26. foreach($lines as $line)
  27. {
  28. $line = trim($line);
  29. if($line == "") continue;
  30.  
  31. if($start)
  32. {
  33. $question = array(
  34. 'question' => $line, // syarat a
  35. 'options' => array()
  36. );
  37. $start = false;
  38. }
  39. else if(preg_match("/^ANSWER:\s{1}/", $line))
  40. {
  41. //syarat d, starting with "ANSWER: "
  42. $answer = trim(preg_replace("/^ANSWER:\s{1}/", '', $line));
  43. $question['answer'] = $answer;
  44. $result[] = $question;
  45. $start = true;
  46. }
  47. else
  48. {
  49. $options = explode(' ', $line, 2);
  50. list($opt, $optText) = $options;
  51. // syarat c '.' atau ')'
  52. $opt = trim(preg_replace("/\.|\)/", '', $opt));
  53. $question['options'][$opt] = trim($optText);
  54. }
  55. }
  56.  
  57. return $result;
  58. }
  59.  
  60. $aikenFile = "aiken-example.txt";
  61. $parsed = parseAikenFormat($aikenFile);
  62.  
  63. print_r($parsed);

Semoga contoh program PHP tersebut dapat bermanfaat bagi kita semua.

Comments
  1. 487 days ago
  2. 462 days ago
  3. 442 days ago
    • 441 days ago
      • 433 days ago
      • 433 days ago
  4. 425 days ago
  5. 196 days ago

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>