PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/php/lib/survey_manager/component/taker.class.php

https://bitbucket.org/chamilo/chamilo-app-survey-dev/
PHP | 239 lines | 163 code | 47 blank | 29 comment | 9 complexity | d5364dd84f1fac79af3c37df35b7e2a2 MD5 | raw file
  1. <?php
  2. namespace application\survey;
  3. use repository\content_object\survey\SurveyComplexDisplaySupport;
  4. use common\libraries\DynamicTabsRenderer;
  5. use common\libraries\Path;
  6. use repository\content_object\survey\SurveyDisplaySurveyViewerComponent;
  7. use repository\content_object\survey\Survey;
  8. use repository\RepositoryDataManager;
  9. use common\libraries\Breadcrumb;
  10. use common\libraries\Translation;
  11. use common\libraries\Theme;
  12. use common\libraries\Request;
  13. use repository\ComplexDisplay;
  14. use common\libraries\EqualityCondition;
  15. use common\libraries\AndCondition;
  16. use tracking\Tracker;
  17. use tracking\Event;
  18. use common\libraries\Display;
  19. class SurveyManagerTakerComponent extends SurveyManager implements SurveyComplexDisplaySupport
  20. {
  21. // private $survey_id;
  22. private $survey;
  23. private $publication_id;
  24. private $publication;
  25. private $answer_cache;
  26. /**
  27. * @var SurveyParticipantTracker
  28. */
  29. private $participant_tracker;
  30. function run()
  31. {
  32. $this->publication_id = Request :: get(SurveyManager :: PARAM_PUBLICATION_ID);
  33. if (! SurveyRights :: is_allowed_in_surveys_subtree(SurveyRights :: RIGHT_PARTICIPATE, $this->publication_id, SurveyRights :: TYPE_PUBLICATION, $this->get_user_id()))
  34. {
  35. Display :: not_allowed();
  36. }
  37. $this->publication = SurveyDataManager :: get_instance()->retrieve_survey_publication($this->publication_id);
  38. // $this->survey_id = Request :: get(SurveyDisplaySurveyViewerComponent :: PARAM_SURVEY_ID);
  39. if (! $this->publication->is_publication_period())
  40. {
  41. $this->redirect(Translation :: get('NotInPublicationPeriod'), (false), array(
  42. self :: PARAM_ACTION => self :: ACTION_BROWSE));
  43. }
  44. $this->survey = $this->publication->get_publication_object();
  45. ComplexDisplay :: launch(Survey :: get_type_name(), $this, false);
  46. }
  47. function add_additional_breadcrumbs(BreadcrumbTrail $breadcrumbtrail)
  48. {
  49. $breadcrumbtrail->add(new Breadcrumb($this->get_url(array(self :: PARAM_ACTION => self :: ACTION_BROWSE,
  50. DynamicTabsRenderer :: PARAM_SELECTED_TAB => SurveyManagerBrowserComponent :: TAB_PARTICIPATE)), Translation :: get('SurveyManagerBrowserComponent')));
  51. }
  52. function get_additional_parameters()
  53. {
  54. // return array(self :: PARAM_PUBLICATION_ID, SurveyDisplaySurveyViewerComponent :: PARAM_SURVEY_ID,
  55. // SurveyDisplaySurveyViewerComponent :: PARAM_CONTEXT_PATH);
  56. return array(self :: PARAM_PUBLICATION_ID,
  57. SurveyDisplaySurveyViewerComponent :: PARAM_CONTEXT_PATH);
  58. }
  59. //try out for interface SurveyTaker
  60. function started()
  61. {
  62. $conditions[] = new EqualityCondition(SurveyParticipantTracker :: PROPERTY_SURVEY_PUBLICATION_ID, $this->publication_id);
  63. $conditions[] = new EqualityCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $this->get_user_id());
  64. $condition = new AndCondition($conditions);
  65. $tracker_count = Tracker :: count_data(SurveyParticipantTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
  66. if ($tracker_count == 0)
  67. {
  68. $args = array();
  69. $args[SurveyParticipantTracker :: PROPERTY_SURVEY_PUBLICATION_ID] = $this->publication_id;
  70. $args[SurveyParticipantTracker :: PROPERTY_USER_ID] = $this->get_user_id();
  71. $args[SurveyParticipantTracker :: PROPERTY_START_TIME] = time();
  72. $args[SurveyParticipantTracker :: PROPERTY_STATUS] = SurveyParticipantTracker :: STATUS_STARTED;
  73. $args[SurveyParticipantTracker :: PROPERTY_CONTEXT_TEMPLATE_ID] = $this->survey->get_context_template_id();
  74. $args[SurveyParticipantTracker :: PROPERTY_PARENT_ID] = 0;
  75. $args[SurveyParticipantTracker :: PROPERTY_CONTEXT_ID] = 0;
  76. $trackers = Event :: trigger(SurveyParticipantTracker :: CREATE_PARTICIPANT_EVENT, SurveyManager :: APPLICATION_NAME, $args);
  77. $this->participant_tracker = $trackers[0];
  78. }
  79. else
  80. {
  81. $this->participant_tracker = Tracker :: get_data(SurveyParticipantTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition, 0, 1)->next_result();
  82. }
  83. }
  84. function finished($progress)
  85. {
  86. // $condition = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_SURVEY_PARTICIPANT_ID, $this->participant_tracker->get_id());
  87. //
  88. // $answer_count = Tracker :: count_data(SurveyQuestionAnswerTracker :: get_table_name(), SurveyManager :: APPLICATION_NAME, $condition);
  89. //
  90. // $survey = RepositoryDataManager :: get_instance()->retrieve_content_object($this->survey_id);
  91. // $survey->initialize($this->get_user_id());
  92. // $question_count = count($survey->get_question_context_paths());
  93. //
  94. // $progress = $answer_count / $question_count * 100;
  95. $this->participant_tracker->set_progress($progress);
  96. $this->participant_tracker->set_status(SurveyParticipantTracker :: STATUS_FINISHED);
  97. $this->participant_tracker->set_total_time(time());
  98. $this->participant_tracker->update();
  99. }
  100. function save_answer($complex_question_id, $answer, $context_path)
  101. {
  102. // dump('in save answer');
  103. // dump($context_path);
  104. // dump($answer);
  105. $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_SURVEY_PARTICIPANT_ID, $this->participant_tracker->get_id());
  106. $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_COMPLEX_QUESTION_ID, $complex_question_id);
  107. $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_PATH, $context_path);
  108. $condition = new AndCondition($conditions);
  109. $tracker = $trackers = Tracker :: get_data(SurveyQuestionAnswerTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition, 0, 1)->next_result();
  110. if ($tracker)
  111. {
  112. $tracker->set_answer($answer);
  113. $tracker->update();
  114. }
  115. else
  116. {
  117. $parameters = array();
  118. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_SURVEY_PARTICIPANT_ID] = $this->participant_tracker->get_id();
  119. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_COMPLEX_QUESTION_ID] = $complex_question_id;
  120. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_ANSWER] = $answer;
  121. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_PATH] = $context_path;
  122. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_PUBLICATION_ID] = $this->publication_id;
  123. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_USER_ID] = $this->get_user_id();
  124. // $survey = RepositoryDataManager :: get_instance()->retrieve_content_object($this->survey_id);
  125. if ($this->survey->has_context())
  126. {
  127. // $level_count = $this->survey->count_levels();
  128. $path_ids = explode('|', $context_path);
  129. $context_ids = explode('_', $path_ids[1]);
  130. $context_count = count($context_ids);
  131. $context_id = array_pop($context_ids);
  132. $context_template = $this->survey->get_context_template_for_level($context_count);
  133. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_TEMPLATE_ID] = $context_template->get_id();
  134. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_ID] = $context_id;
  135. }
  136. else
  137. {
  138. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_ID] = 0;
  139. $parameters[SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_TEMPLATE_ID] = 0;
  140. }
  141. Event :: trigger(SurveyQuestionAnswerTracker :: SAVE_QUESTION_ANSWER_EVENT, SurveyManager :: APPLICATION_NAME, $parameters);
  142. }
  143. }
  144. function get_answer($complex_question_id, $context_path)
  145. {
  146. // dump('in get answer');
  147. // dump($complex_question_id);
  148. if (! isset($this->answer_cache) || ! isset($this->answer_cache[$context_path]))
  149. {
  150. // dump($context_path);
  151. $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_SURVEY_PARTICIPANT_ID, $this->participant_tracker->get_id());
  152. $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_COMPLEX_QUESTION_ID, $complex_question_id);
  153. $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_PATH, $context_path);
  154. $condition = new AndCondition($conditions);
  155. $tracker = Tracker :: get_singular_data(SurveyQuestionAnswerTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
  156. if ($tracker instanceof Tracker)
  157. {
  158. $this->answer_cache[$context_path] = $tracker->get_answer();
  159. }
  160. else
  161. {
  162. // dump($context_path);
  163. $this->answer_cache[$context_path] = false;
  164. }
  165. }
  166. // dump($this->answer_cache);
  167. return $this->answer_cache[$context_path];
  168. }
  169. function get_publication_id()
  170. {
  171. // $this->publication_id = Request :: get(SurveyManager :: PARAM_PUBLICATION_ID);
  172. // $this->publication = SurveyDataManager :: get_instance()->retrieve_survey_publication($this->publication_id);
  173. return $this->publication->get_id();
  174. }
  175. function get_invitee_id()
  176. {
  177. return $this->get_user_id();
  178. }
  179. function get_go_back_url()
  180. {
  181. return $this->get_browse_survey_publications_url();
  182. }
  183. function get_root_content_object()
  184. {
  185. return $this->survey;
  186. }
  187. function is_allowed($right)
  188. {
  189. return true;
  190. }
  191. }
  192. ?>