PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/php/lib/platform/dokeos185/data_class/dokeos185_quiz_answer.class.php

https://bitbucket.org/chamilo/chamilo-migration-dev/
PHP | 257 lines | 155 code | 32 blank | 70 comment | 9 complexity | 6fe9b54322228fe18a58a5cf9dcc3ab1 MD5 | raw file
  1. <?php
  2. namespace migration;
  3. use common\libraries\Translation;
  4. use repository\RepositoryDataManager;
  5. use common\libraries\Utilities;
  6. use repository\content_object\assessment_multiple_choice_question\AssessmentMultipleChoiceQuestion;
  7. use repository\content_object\fill_in_blanks_question\FillInBlanksQuestion;
  8. use repository\content_object\assessment_matching_question\AssessmentMatchingQuestion;
  9. use repository\content_object\assessment_multiple_choice_question\AssessmentMultipleChoiceQuestionOption;
  10. use repository\content_object\assessment_matching_question\AssessmentMatchingQuestionOption;
  11. /**
  12. * $Id: dokeos185_quiz_answer.class.php 221 2009-11-13 14:36:41Z vanpouckesven $
  13. * @package migration.lib.platform.dokeos185
  14. */
  15. require_once dirname(__FILE__) . '/../dokeos185_course_data_migration_data_class.class.php';
  16. /**
  17. * This class presents a Dokeos185 quiz_answer
  18. *
  19. * @author Sven Vanpouckes
  20. */
  21. class Dokeos185QuizAnswer extends Dokeos185CourseDataMigrationDataClass
  22. {
  23. const CLASS_NAME = __CLASS__;
  24. const TABLE_NAME = 'quiz_answer';
  25. /**
  26. * Dokeos185QuizAnswer properties
  27. */
  28. const PROPERTY_ID = 'id';
  29. const PROPERTY_QUESTION_ID = 'question_id';
  30. const PROPERTY_ANSWER = 'answer';
  31. const PROPERTY_CORRECT = 'correct';
  32. const PROPERTY_COMMENT = 'comment';
  33. const PROPERTY_PONDERATION = 'ponderation';
  34. const PROPERTY_POSITION = 'position';
  35. const PROPERTY_HOTSPOT_COORDINATES = 'hotspot_coordinates';
  36. const PROPERTY_HOTSPOT_TYPE = 'hotspot_type';
  37. /**
  38. * Get the default properties
  39. * @return array The property names.
  40. */
  41. static function get_default_property_names()
  42. {
  43. return array(self :: PROPERTY_ID, self :: PROPERTY_QUESTION_ID, self :: PROPERTY_ANSWER, self :: PROPERTY_CORRECT, self :: PROPERTY_COMMENT, self :: PROPERTY_PONDERATION, self :: PROPERTY_POSITION, self :: PROPERTY_HOTSPOT_COORDINATES, self :: PROPERTY_HOTSPOT_TYPE);
  44. }
  45. /**
  46. * Returns the id of this Dokeos185QuizAnswer.
  47. * @return the id.
  48. */
  49. function get_id()
  50. {
  51. return $this->get_default_property(self :: PROPERTY_ID);
  52. }
  53. /**
  54. * Returns the question_id of this Dokeos185QuizAnswer.
  55. * @return the question_id.
  56. */
  57. function get_question_id()
  58. {
  59. return $this->get_default_property(self :: PROPERTY_QUESTION_ID);
  60. }
  61. /**
  62. * Returns the answer of this Dokeos185QuizAnswer.
  63. * @return the answer.
  64. */
  65. function get_answer()
  66. {
  67. return $this->get_default_property(self :: PROPERTY_ANSWER);
  68. }
  69. /**
  70. * Returns the correct of this Dokeos185QuizAnswer.
  71. * @return the correct.
  72. */
  73. function get_correct()
  74. {
  75. return $this->get_default_property(self :: PROPERTY_CORRECT);
  76. }
  77. /**
  78. * Returns the comment of this Dokeos185QuizAnswer.
  79. * @return the comment.
  80. */
  81. function get_comment()
  82. {
  83. return $this->get_default_property(self :: PROPERTY_COMMENT);
  84. }
  85. /**
  86. * Returns the ponderation of this Dokeos185QuizAnswer.
  87. * @return the ponderation.
  88. */
  89. function get_ponderation()
  90. {
  91. return $this->get_default_property(self :: PROPERTY_PONDERATION);
  92. }
  93. /**
  94. * Returns the position of this Dokeos185QuizAnswer.
  95. * @return the position.
  96. */
  97. function get_position()
  98. {
  99. return $this->get_default_property(self :: PROPERTY_POSITION);
  100. }
  101. /**
  102. * Returns the hotspot_coordinates of this Dokeos185QuizAnswer.
  103. * @return the hotspot_coordinates.
  104. */
  105. function get_hotspot_coordinates()
  106. {
  107. return $this->get_default_property(self :: PROPERTY_HOTSPOT_COORDINATES);
  108. }
  109. /**
  110. * Returns the hotspot_type of this Dokeos185QuizAnswer.
  111. * @return the hotspot_type.
  112. */
  113. function get_hotspot_type()
  114. {
  115. return $this->get_default_property(self :: PROPERTY_HOTSPOT_TYPE);
  116. }
  117. /**
  118. * Check if the object is valid
  119. */
  120. function is_valid()
  121. {
  122. $new_question_id = $this->get_id_reference($this->get_question_id(), $this->get_database_name() . '.quiz_question');
  123. if (!$this->get_id() || !$new_question_id || !$this->get_answer())
  124. {
  125. if(!$new_question_id)
  126. {
  127. $this->set_message(Translation :: get('QuestionNotMigratedMessage', array('QID' => $this->get_question_id(), 'AID' => $this->get_id())));
  128. }
  129. else
  130. {
  131. $this->set_message(Translation :: get('GeneralInvalidMessage', array('TYPE' => 'quiz_answer', 'ID' => $this->get_id())));
  132. }
  133. $this->create_failed_element($this->get_id());
  134. return false;
  135. }
  136. return true;
  137. }
  138. /**
  139. * Convert
  140. */
  141. function convert_data()
  142. {
  143. $new_question_id = $this->get_id_reference($this->get_question_id(), $this->get_database_name() . '.quiz_question');
  144. $question = RepositoryDataManager :: get_instance()->retrieve_content_object($new_question_id);
  145. switch ($question->get_type())
  146. {
  147. case AssessmentMultipleChoiceQuestion :: get_type_name():
  148. $this->convert_multiple_choice_question($question);
  149. break;
  150. case FillInBlanksQuestion :: get_type_name():
  151. $this->convert_fill_in_blanks_question($question);
  152. break;
  153. case AssessmentMatchingQuestion :: get_type_name():
  154. $this->convert_matching_question($question);
  155. break;
  156. }
  157. $this->set_message(Translation :: get('GeneralConvertedMessage', array('TYPE' => 'quiz_answer', 'OLD_ID' => $this->get_id(), 'NEW_ID' => $question->get_id())));
  158. }
  159. /**
  160. * Converts multiple choice questions
  161. * @param AssessmentMultipleChoiceQuestion $question
  162. */
  163. private function convert_multiple_choice_question(AssessmentMultipleChoiceQuestion $question)
  164. {
  165. $option = new AssessmentMultipleChoiceQuestionOption($this->get_answer(), $this->get_correct(), $this->get_ponderation(), $this->get_comment());
  166. $question->add_option($option);
  167. $question->update();
  168. }
  169. /**
  170. * Converts fill in the blanks questions
  171. * @param FillInBlanksQuestion $question
  172. */
  173. private function convert_fill_in_blanks_question(FillInBlanksQuestion $question)
  174. {
  175. $answer_text = $this->get_answer();
  176. $question->set_question_type(FillInBlanksQuestion :: TYPE_SIZED_TEXT);
  177. $split = explode('::', $answer_text);
  178. $scores = explode(',', $split[1]);
  179. $answer_text = $split[0];
  180. $pattern = '/\[[^\[\]]*\]/';
  181. $answers = preg_match_all($pattern, $answer_text, $matches);
  182. foreach ($matches[0] as $i => $answer)
  183. {
  184. $score = $scores[$i] ? $scores[$i] : 0;
  185. $answer = substr($answer, 1, -1);
  186. $new_answer = '[' . $answer . '=' . $score . ']';
  187. $answer_text = preg_replace('/\[' . $answer . '\]/', $new_answer, $answer_text, 1, $count);
  188. }
  189. if (is_null($answer_text) || $answer_text == '')
  190. {
  191. $answer_text = 'error: non-standard fill in the blanks answer [error]';
  192. }
  193. $question->set_answer_text($answer_text);
  194. $question->update();
  195. }
  196. /**
  197. * Converts matching questions
  198. * @param AssessmentMatchingQuestion $question
  199. */
  200. private function convert_matching_question(AssessmentMatchingQuestion $question)
  201. {
  202. if ($this->get_correct())
  203. {
  204. $option = new AssessmentMatchingQuestionOption($this->get_answer(), $this->get_correct() - 1, $this->get_ponderation());
  205. $question->add_option($option);
  206. }
  207. else
  208. {
  209. $matches = $question->get_matches();
  210. $matches[$this->get_position() - 1] = $this->get_answer();
  211. $question->set_matches($matches);
  212. }
  213. $question->update();
  214. }
  215. static function get_table_name()
  216. {
  217. return Utilities :: camelcase_to_underscores(substr(Utilities :: get_classname_from_namespace(__CLASS__), 9));
  218. ;
  219. }
  220. static function get_class_name()
  221. {
  222. return self :: CLASS_NAME;
  223. }
  224. }
  225. ?>