PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/php/ajax/save_answer.class.php

https://bitbucket.org/chamilo/chamilo-app-survey-dev/
PHP | 183 lines | 121 code | 27 blank | 35 comment | 7 complexity | a640471a6c4d125089457f86d2c8cac2 MD5 | raw file
  1. <?php
  2. namespace application\survey;
  3. use common\libraries\Security;
  4. use common\libraries\Utilities;
  5. use common\libraries\AjaxManager;
  6. use common\libraries\JsonAjaxResult;
  7. use common\libraries\Request;
  8. use common\libraries\StringUtilities;
  9. use common\libraries\Path;
  10. use repository\content_object\survey_page\SurveyPage;
  11. use common\libraries\EqualityCondition;
  12. use common\libraries\AndCondition;
  13. use tracking\Tracker;
  14. use tracking\Event;
  15. use repository\RepositoryDataManager;
  16. class SurveyAjaxSaveAnswer extends AjaxManager
  17. {
  18. const PARAM_SURVEY_PUBLICATION_ID = 'survey_publication';
  19. const PARAM_ANSWER = 'answer';
  20. const PARAM_CONTEXT_PATH = 'context_path';
  21. const PARAM_SUCCES = 'succes';
  22. private $publication_id;
  23. /* (non-PHPdoc)
  24. * @see common\libraries.AjaxManager::required_parameters()
  25. */
  26. function required_parameters()
  27. {
  28. return array(self :: PARAM_SURVEY_PUBLICATION_ID, self :: PARAM_ANSWER, self :: PARAM_CONTEXT_PATH);
  29. }
  30. /* (non-PHPdoc)
  31. * @see common\libraries.AjaxManager::run()
  32. */
  33. function run()
  34. {
  35. $this->publication_id = $this->get_parameter(self :: PARAM_SURVEY_PUBLICATION_ID);
  36. $context_path = $this->get_parameter(self :: PARAM_CONTEXT_PATH);
  37. $ids = explode('_', $context_path);
  38. $complex_question_id = array_pop($ids);
  39. $answers = $this->get_parameter(self :: PARAM_ANSWER);
  40. // $answers = json_decode($answers, true);
  41. // dump($answers);
  42. // exit;
  43. if (count($answers) > 0)
  44. {
  45. $answer[$complex_question_id] = $answers;
  46. }
  47. // foreach ($answers as $key => $value)
  48. // {
  49. // $value = Security :: remove_XSS($value);
  50. // $split_key = explode('_', $key);
  51. //// dump( $split_key);
  52. // $count = count($split_key);
  53. //// dump($count);
  54. // if (!StringUtilities :: is_null_or_empty($value, true))
  55. // {
  56. // $answer_index = $split_key[1];
  57. //// dump($answer_index);
  58. // if ($count == 3)
  59. // {
  60. // $sub_index = $split_key[2];
  61. // $answer[$answer_index][$sub_index] = $value;
  62. // }
  63. // else
  64. // {
  65. //// $answer[$answer_index] = $value;
  66. // $answer[$key] = $value;
  67. // }
  68. // }
  69. //
  70. // }
  71. // dump($answer);
  72. if (count($answers) > 0)
  73. {
  74. $this->set_participant_tracker();
  75. $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_SURVEY_PARTICIPANT_ID, $this->participant_tracker->get_id());
  76. $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_COMPLEX_QUESTION_ID, $complex_question_id);
  77. $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_PATH, $context_path);
  78. $condition = new AndCondition($conditions);
  79. $tracker = Tracker :: get_singular_data(SurveyQuestionAnswerTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
  80. if ($tracker)
  81. {
  82. $tracker->set_answer($answers);
  83. $succes = $tracker->update();
  84. }
  85. else
  86. {
  87. $parameters = array();
  88. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_SURVEY_PARTICIPANT_ID] = $this->participant_tracker->get_id();
  89. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_COMPLEX_QUESTION_ID] = $complex_question_id;
  90. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_ANSWER] = $answers;
  91. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_PATH] = $context_path;
  92. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_PUBLICATION_ID] = $this->publication_id;
  93. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_USER_ID] = $this->get_user_id();
  94. $publication = SurveyDataManager :: get_instance()->retrieve_survey_publication($this->publication_id);
  95. $survey = $publication->get_publication_object();
  96. if ($survey->has_context())
  97. {
  98. $level_count = $survey->count_levels();
  99. $path_ids = explode('|', $context_path);
  100. $context_ids = explode('_', $path_ids[1]);
  101. $context_count = count($context_ids);
  102. $context_id = array_pop($context_ids);
  103. $context_template = $survey->get_context_template_for_level($context_count);
  104. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_TEMPLATE_ID] = $context_template->get_id();
  105. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_ID] = $context_id;
  106. }
  107. else
  108. {
  109. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_ID] = 0;
  110. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_TEMPLATE_ID] = 0;
  111. }
  112. $succes = Event :: trigger(SurveyQuestionAnswerTracker :: SAVE_QUESTION_ANSWER_EVENT, SurveyManager :: APPLICATION_NAME, $parameters);
  113. }
  114. if ($succes)
  115. {
  116. $result = new JsonAjaxResult(200);
  117. $result->display();
  118. }
  119. else
  120. {
  121. JsonAjaxResult :: general_error();
  122. }
  123. }
  124. else
  125. {
  126. $result = new JsonAjaxResult(204);
  127. $result->display();
  128. }
  129. }
  130. function set_participant_tracker()
  131. {
  132. $conditions[] = new EqualityCondition(SurveyParticipantTracker :: PROPERTY_SURVEY_PUBLICATION_ID, $this->publication_id);
  133. $conditions[] = new EqualityCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $this->get_user_id());
  134. $condition = new AndCondition($conditions);
  135. $tracker_count = Tracker :: count_data(SurveyParticipantTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
  136. if ($tracker_count == 0)
  137. {
  138. $args = array();
  139. $args[SurveyParticipantTracker :: PROPERTY_SURVEY_PUBLICATION_ID] = $this->publication_id;
  140. $args[SurveyParticipantTracker :: PROPERTY_USER_ID] = $this->get_user_id();
  141. $args[SurveyParticipantTracker :: PROPERTY_START_TIME] = time();
  142. $args[SurveyParticipantTracker :: PROPERTY_STATUS] = SurveyParticipantTracker :: STATUS_STARTED;
  143. $args[SurveyParticipantTracker :: PROPERTY_CONTEXT_TEMPLATE_ID] = 0;
  144. $args[SurveyParticipantTracker :: PROPERTY_PARENT_ID] = 0;
  145. $args[SurveyParticipantTracker :: PROPERTY_CONTEXT_ID] = 0;
  146. // $args[SurveyParticipantTracker :: PROPERTY_CONTEXT_NAME] = 'NOCONTEXT';
  147. $trackers = Event :: trigger(SurveyParticipantTracker :: CREATE_PARTICIPANT_EVENT, SurveyManager :: APPLICATION_NAME, $args);
  148. $this->participant_tracker = $trackers[0];
  149. }
  150. else
  151. {
  152. $this->participant_tracker = Tracker :: get_data(SurveyParticipantTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition, 0, 1)->next_result();
  153. }
  154. }
  155. }
  156. ?>