/php/lib/survey_manager/component/taker.class.php
PHP | 239 lines | 163 code | 47 blank | 29 comment | 9 complexity | d5364dd84f1fac79af3c37df35b7e2a2 MD5 | raw file
- <?php
- namespace application\survey;
-
- use repository\content_object\survey\SurveyComplexDisplaySupport;
-
- use common\libraries\DynamicTabsRenderer;
-
- use common\libraries\Path;
- use repository\content_object\survey\SurveyDisplaySurveyViewerComponent;
- use repository\content_object\survey\Survey;
- use repository\RepositoryDataManager;
- use common\libraries\Breadcrumb;
- use common\libraries\Translation;
- use common\libraries\Theme;
- use common\libraries\Request;
- use repository\ComplexDisplay;
- use common\libraries\EqualityCondition;
- use common\libraries\AndCondition;
- use tracking\Tracker;
- use tracking\Event;
- use common\libraries\Display;
-
- class SurveyManagerTakerComponent extends SurveyManager implements SurveyComplexDisplaySupport
- {
- // private $survey_id;
- private $survey;
- private $publication_id;
- private $publication;
-
- private $answer_cache;
-
- /**
- * @var SurveyParticipantTracker
- */
- private $participant_tracker;
-
- function run()
- {
-
- $this->publication_id = Request :: get(SurveyManager :: PARAM_PUBLICATION_ID);
-
- if (! SurveyRights :: is_allowed_in_surveys_subtree(SurveyRights :: RIGHT_PARTICIPATE, $this->publication_id, SurveyRights :: TYPE_PUBLICATION, $this->get_user_id()))
- {
- Display :: not_allowed();
- }
-
- $this->publication = SurveyDataManager :: get_instance()->retrieve_survey_publication($this->publication_id);
-
- // $this->survey_id = Request :: get(SurveyDisplaySurveyViewerComponent :: PARAM_SURVEY_ID);
-
-
- if (! $this->publication->is_publication_period())
- {
- $this->redirect(Translation :: get('NotInPublicationPeriod'), (false), array(
- self :: PARAM_ACTION => self :: ACTION_BROWSE));
- }
-
- $this->survey = $this->publication->get_publication_object();
-
- ComplexDisplay :: launch(Survey :: get_type_name(), $this, false);
- }
-
- function add_additional_breadcrumbs(BreadcrumbTrail $breadcrumbtrail)
- {
- $breadcrumbtrail->add(new Breadcrumb($this->get_url(array(self :: PARAM_ACTION => self :: ACTION_BROWSE,
- DynamicTabsRenderer :: PARAM_SELECTED_TAB => SurveyManagerBrowserComponent :: TAB_PARTICIPATE)), Translation :: get('SurveyManagerBrowserComponent')));
- }
-
- function get_additional_parameters()
- {
- // return array(self :: PARAM_PUBLICATION_ID, SurveyDisplaySurveyViewerComponent :: PARAM_SURVEY_ID,
- // SurveyDisplaySurveyViewerComponent :: PARAM_CONTEXT_PATH);
- return array(self :: PARAM_PUBLICATION_ID,
- SurveyDisplaySurveyViewerComponent :: PARAM_CONTEXT_PATH);
- }
-
- //try out for interface SurveyTaker
-
-
- function started()
- {
- $conditions[] = new EqualityCondition(SurveyParticipantTracker :: PROPERTY_SURVEY_PUBLICATION_ID, $this->publication_id);
- $conditions[] = new EqualityCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $this->get_user_id());
- $condition = new AndCondition($conditions);
-
- $tracker_count = Tracker :: count_data(SurveyParticipantTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
-
- if ($tracker_count == 0)
- {
-
- $args = array();
- $args[SurveyParticipantTracker :: PROPERTY_SURVEY_PUBLICATION_ID] = $this->publication_id;
- $args[SurveyParticipantTracker :: PROPERTY_USER_ID] = $this->get_user_id();
- $args[SurveyParticipantTracker :: PROPERTY_START_TIME] = time();
- $args[SurveyParticipantTracker :: PROPERTY_STATUS] = SurveyParticipantTracker :: STATUS_STARTED;
- $args[SurveyParticipantTracker :: PROPERTY_CONTEXT_TEMPLATE_ID] = $this->survey->get_context_template_id();
- $args[SurveyParticipantTracker :: PROPERTY_PARENT_ID] = 0;
- $args[SurveyParticipantTracker :: PROPERTY_CONTEXT_ID] = 0;
- $trackers = Event :: trigger(SurveyParticipantTracker :: CREATE_PARTICIPANT_EVENT, SurveyManager :: APPLICATION_NAME, $args);
- $this->participant_tracker = $trackers[0];
- }
- else
- {
- $this->participant_tracker = Tracker :: get_data(SurveyParticipantTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition, 0, 1)->next_result();
- }
- }
-
- function finished($progress)
- {
-
- // $condition = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_SURVEY_PARTICIPANT_ID, $this->participant_tracker->get_id());
- //
- // $answer_count = Tracker :: count_data(SurveyQuestionAnswerTracker :: get_table_name(), SurveyManager :: APPLICATION_NAME, $condition);
- //
- // $survey = RepositoryDataManager :: get_instance()->retrieve_content_object($this->survey_id);
- // $survey->initialize($this->get_user_id());
- // $question_count = count($survey->get_question_context_paths());
- //
- // $progress = $answer_count / $question_count * 100;
-
-
- $this->participant_tracker->set_progress($progress);
- $this->participant_tracker->set_status(SurveyParticipantTracker :: STATUS_FINISHED);
- $this->participant_tracker->set_total_time(time());
- $this->participant_tracker->update();
- }
-
- function save_answer($complex_question_id, $answer, $context_path)
- {
-
- // dump('in save answer');
- // dump($context_path);
- // dump($answer);
-
-
- $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_SURVEY_PARTICIPANT_ID, $this->participant_tracker->get_id());
- $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_COMPLEX_QUESTION_ID, $complex_question_id);
- $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_PATH, $context_path);
- $condition = new AndCondition($conditions);
- $tracker = $trackers = Tracker :: get_data(SurveyQuestionAnswerTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition, 0, 1)->next_result();
-
- if ($tracker)
- {
- $tracker->set_answer($answer);
- $tracker->update();
- }
- else
- {
- $parameters = array();
- $parameters[SurveyQuestionAnswerTracker :: PROPERTY_SURVEY_PARTICIPANT_ID] = $this->participant_tracker->get_id();
- $parameters[SurveyQuestionAnswerTracker :: PROPERTY_COMPLEX_QUESTION_ID] = $complex_question_id;
- $parameters[SurveyQuestionAnswerTracker :: PROPERTY_ANSWER] = $answer;
- $parameters[SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_PATH] = $context_path;
- $parameters[SurveyQuestionAnswerTracker :: PROPERTY_PUBLICATION_ID] = $this->publication_id;
- $parameters[SurveyQuestionAnswerTracker :: PROPERTY_USER_ID] = $this->get_user_id();
-
- // $survey = RepositoryDataManager :: get_instance()->retrieve_content_object($this->survey_id);
-
-
- if ($this->survey->has_context())
- {
- // $level_count = $this->survey->count_levels();
- $path_ids = explode('|', $context_path);
- $context_ids = explode('_', $path_ids[1]);
- $context_count = count($context_ids);
- $context_id = array_pop($context_ids);
- $context_template = $this->survey->get_context_template_for_level($context_count);
- $parameters[SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_TEMPLATE_ID] = $context_template->get_id();
- $parameters[SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_ID] = $context_id;
-
- }
- else
- {
- $parameters[SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_ID] = 0;
- $parameters[SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_TEMPLATE_ID] = 0;
- }
- Event :: trigger(SurveyQuestionAnswerTracker :: SAVE_QUESTION_ANSWER_EVENT, SurveyManager :: APPLICATION_NAME, $parameters);
- }
- }
-
- function get_answer($complex_question_id, $context_path)
- {
- // dump('in get answer');
- // dump($complex_question_id);
-
-
- if (! isset($this->answer_cache) || ! isset($this->answer_cache[$context_path]))
- {
- // dump($context_path);
- $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_SURVEY_PARTICIPANT_ID, $this->participant_tracker->get_id());
- $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_COMPLEX_QUESTION_ID, $complex_question_id);
- $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_CONTEXT_PATH, $context_path);
- $condition = new AndCondition($conditions);
-
- $tracker = Tracker :: get_singular_data(SurveyQuestionAnswerTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
-
- if ($tracker instanceof Tracker)
- {
- $this->answer_cache[$context_path] = $tracker->get_answer();
- }
- else
- {
- // dump($context_path);
- $this->answer_cache[$context_path] = false;
- }
- }
- // dump($this->answer_cache);
- return $this->answer_cache[$context_path];
-
- }
-
- function get_publication_id()
- {
- // $this->publication_id = Request :: get(SurveyManager :: PARAM_PUBLICATION_ID);
- // $this->publication = SurveyDataManager :: get_instance()->retrieve_survey_publication($this->publication_id);
- return $this->publication->get_id();
- }
-
- function get_invitee_id()
- {
- return $this->get_user_id();
- }
-
- function get_go_back_url()
- {
- return $this->get_browse_survey_publications_url();
- }
-
- function get_root_content_object()
- {
- return $this->survey;
- }
-
- function is_allowed($right)
- {
- return true;
- }
- }
- ?>