fill_exe.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * This script contains a data filling procedure for an exercise
  4. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  5. *
  6. */
  7. /**
  8. * Initialisation section
  9. */
  10. /**
  11. * Loads the data and injects it into the Chamilo database, using the Chamilo
  12. * internal functions.
  13. * @return array List of user IDs for the users that have just been inserted
  14. */
  15. function fill_exe()
  16. {
  17. $qc = 5000; //how many questions to create per exercise
  18. $eol = PHP_EOL;
  19. $courses = array(); //declare only to avoid parsing notice
  20. $qst = array();
  21. require_once 'data_exe.php'; //fill the $users array
  22. $output = array();
  23. $output[] = array('title' => 'Exe Filling Report: ');
  24. $i = 1;
  25. $lib = api_get_path(SYS_CODE_PATH);
  26. require_once $lib.'exercice/exercise.class.php';
  27. require_once $lib.'exercice/question.class.php';
  28. require_once $lib.'exercice/answer.class.php';
  29. foreach ($courses as $i => $course) {
  30. //first check that the first item doesn't exist already
  31. $output[$i]['line-init'] = $course['title'];
  32. $res = CourseManager::create_course($course);
  33. $output[$i]['line-info'] = ($res ? $res : get_lang('NotInserted'));
  34. $i++;
  35. if (is_array($res)) {
  36. //now insert an exercise
  37. foreach ($course['exes'] as $exe) {
  38. $objExercise = new Exercise($res['real_id']);
  39. $objExercise->id = 0;
  40. $objExercise->course_id = $res['real_id'];
  41. $objExercise->exercise = $exe['title'];
  42. $objExercise->type = 1;
  43. $objExercise->attempts = $exe['attempts'];
  44. $objExercise->random = $exe['random'];
  45. $objExercise->active = 1;
  46. $objExercise->propagate_neg = 0;
  47. $objExercise->pass_percentage = $exe['pass_percentage'];
  48. $objExercise->session_id = 0;
  49. $objExercise->results_disabled = 0;
  50. $objExercise->expired_time = $exe['time'];
  51. $objExercise->review_answers = $exe['review_answers'];
  52. $objExercise->save();
  53. $id = $objExercise->id;
  54. if (!empty($id)) {
  55. $qi = 0;
  56. while ($qi < $qc) {
  57. foreach ($qst as $q) {
  58. error_log('Created '.$qi.' questions');
  59. $question = Question::getInstance($q['type']);
  60. $question->id = 0;
  61. $question->question = $q['title'].' '.$qi;
  62. $question->description = $q['desc'];
  63. $question->type = $q['type'];
  64. $question->course = $res;
  65. $r = $question->save($id);
  66. if ($r === false) {
  67. continue;
  68. }
  69. $qid = $question->id;
  70. $objExercise->addToList($qid);
  71. $objExercise->update_question_positions();
  72. $aid = 1;
  73. foreach ($q['answers'] as $asw) {
  74. $answer = new UniqueAnswer($qid);
  75. $answer->create_answer(
  76. $aid,
  77. $qid,
  78. $asw['title'],
  79. '',
  80. $asw['score'],
  81. $asw['correct'],
  82. $res['real_id']
  83. );
  84. $aid++;
  85. }
  86. $qi++;
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. return $output;
  94. }