/** * Process all survey stuff in viewtopic * * @param \phpbb\event\data $event */ public function show_survey_viewtopic($event) { $forum_id = $event['forum_id']; $this->base_url = $event['base_url']; $this->topic_id = $event['topic_id']; $this->survey->set_env($forum_id, $event['topic_data']['topic_poster'], $event['topic_data']['topic_status'], $event['topic_data']['forum_status']); if (!$this->survey->load_survey($this->topic_id)) { // No survey for this topic return; } // Load Language file $this->user->add_lang_ext('kilianr/survey', 'survey'); // Now process all submits, if any $survey_errors = $this->process_submit($event); // If the survey is disabled, then return now (also if we just disabled it) if (!$this->survey->enabled) { return; } // Some frequently used data: $user_id = $this->user->data['user_id']; $can_see_everything = $this->survey->can_see_everything($user_id); $can_manage = $this->survey->can_manage($user_id); $can_edit_other_users = $this->survey->can_edit_other_users($user_id); $is_closed = $this->survey->is_closed(); $viewtopic_url = append_sid("{$this->phpbb_root_path}viewtopic.{$this->phpEx}?f={$forum_id}&t={$this->topic_id}"); $action_url = "{$viewtopic_url}&{$this->action_name}="; $can_add_new_entry = $this->survey->can_add_new_entry($user_id); if (!$this->survey->questions) { $survey_errors[] = $this->user->lang['SURVEY_NO_QUESTIONS']; } if (!$this->survey->entries) { $survey_errors[] = $this->user->lang['SURVEY_NO_ENTRIES']; } if ($is_closed) { $this->template->assign_var('SURVEY_IS_CLOSED_DESC', $this->user->lang('SURVEY_IS_CLOSED' . ($can_manage ? '_DESC_OWNER' : ''), $this->user->format_date($this->survey->settings['stop_time']))); } else { if ($this->survey->settings['stop_time']) { $this->template->assign_var('SURVEY_WILL_CLOSE_DESC', $this->user->lang('SURVEY_DESC_STOP', $this->user->format_date($this->survey->settings['stop_time']))); } } // Output settings $template_vars = array(); foreach ($this->survey->settings as $key => $value) { if ($key == 'start_time' || $key == 'stop_time' && $value != '') { $value = $this->user->format_date($value, $this->user->lang['SURVEY_DATEFORMAT']); } $template_vars['S_SURVEY_' . strtoupper($key)] = $value; } $this->template->assign_vars($template_vars); foreach (array('show_order', 'visibility', 'topic_poster_right') as $block_setting) { survey::assign_block_vars_for_selection($block_setting, $this->template, $this->user, $this->survey->settings); } // Output question types foreach (survey::$QUESTION_TYPES as $type) { $template_vars = array('NUM' => $type, 'SELECTED' => $this->question_to_load !== false && $this->survey->questions[$this->question_to_load]['type'] == $type ? true : false, 'DESC' => $this->user->lang('SURVEY_QUESTION_TYPE_DESC_' . $type)); $this->template->assign_block_vars('question_type', $template_vars); } // Output question sum types foreach (survey::$QUESTION_SUM_TYPES as $type) { $template_vars = array('NUM' => $type, 'SELECTED' => $this->question_to_load !== false && $this->survey->questions[$this->question_to_load]['sum_type'] == $type ? true : false, 'DESC' => $this->user->lang('SURVEY_QUESTION_SUM_TYPE_DESC_' . $type)); $this->template->assign_block_vars('question_sum_type', $template_vars); } // Output questions $entry_count = $this->survey->get_entry_count(); $can_see_sums = false; $can_see_averages = false; $some_cap_set = false; foreach ($this->survey->questions as $question_id => $question) { $template_vars = array(); foreach ($question as $key => $value) { if ($key != 'choices') { $template_vars[strtoupper($key)] = $value; } } $template_vars['LOADED'] = $this->question_to_load !== false && $this->question_to_load == $question_id ? true : false; $template_vars['DELETE_LINK'] = $action_url . 'question_deletion&question_to_delete=' . $question_id; $template_vars['SUM_STRING'] = $this->survey->get_sum_string($question_id); $template_vars['AVERAGE_STRING'] = $this->survey->get_average_string($question_id, $entry_count); $template_vars['CAP_REACHED'] = $this->survey->cap_reached($question_id); $template_vars['HAS_CHOICES'] = (bool) $question['choices']; if ($template_vars['SUM_STRING'] != '') { $can_see_sums = true; } if ($template_vars['AVERAGE_STRING'] != '') { $can_see_averages = true; } if ($this->survey->has_cap($question_id)) { $some_cap_set = true; } $this->template->assign_block_vars('questions', $template_vars); foreach ($question['choices'] as $choice) { $can_see_sums = true; $template_vars = array(); foreach ($choice as $key => $value) { $template_vars[strtoupper($key)] = $value; } $this->template->assign_block_vars('questions.choices', $template_vars); } } if ($entry_count == 0 || $this->survey->hide_everything() && !$can_see_everything) { $can_see_sums = $can_see_averages = false; } // Fetch User details $user_details = array(); $anonymous = false; foreach ($this->survey->entries as $entry) { if ($entry['user_id'] != ANONYMOUS) { $user_details[$entry['user_id']] = true; } else { $anonymous = true; } } $user_details[$user_id] = true; $sql = 'SELECT user_id, username, user_colour FROM ' . USERS_TABLE . ' WHERE ' . $this->db->sql_in_set('user_id', array_keys($user_details)); $result = $this->db->sql_query($sql); while ($row = $this->db->sql_fetchrow($result)) { $user_details[$row['user_id']] = $row; } $this->db->sql_freeresult($result); // Output entries $entries_modifyable = array(); $can_see_or_add_entries = false; $entries_to_assign = array(); $extra_rows = array(); $adduser_entry_template_vars = array(); $new_entry_template_vars = array(); $questions = $this->survey->questions; foreach ($questions as $question_id => $question) { if ($question['random_choice_order']) { $choice_ids = array_keys($question['choices']); shuffle($choice_ids); $randomized_choices = array(); foreach ($choice_ids as $choice_id) { $randomized_choices[$choice_id] = $question['choices'][$choice_id]; } $questions[$question_id]['choices'] = $randomized_choices; } } if ($can_add_new_entry) { $extra_rows[] = self::NEW_ENTRY_ID; } if ($can_edit_other_users) { $extra_rows[] = self::ADDUSER_ENTRY_ID; } foreach (array_merge($this->survey->entries, $extra_rows) as $entry) { $template_vars = array(); if ($entry == self::ADDUSER_ENTRY_ID) { $entry = array('entry_id' => self::ADDUSER_ENTRY_ID, 'answers' => array()); } else { if ($entry == self::NEW_ENTRY_ID) { $entry = array('entry_id' => self::NEW_ENTRY_ID, 'user_id' => $user_id, 'answers' => array()); } else { if ($entry['user_id'] != $user_id && $this->survey->hide_entries() && !$can_see_everything) { continue; } } } $can_see_or_add_entries = true; foreach ($entry as $key => $value) { if ($key != 'answers') { $template_vars[strtoupper($key)] = $value; } if ($key == 'entry_id') { $template_vars['IS_ADDUSER'] = $value == self::ADDUSER_ENTRY_ID ? true : false; $template_vars['IS_NEW'] = $value == self::NEW_ENTRY_ID ? true : false; $template_vars['DELETE_LINK'] = "{$action_url}entry_deletion&entry_to_delete={$value}"; } } if ($entry['entry_id'] != self::ADDUSER_ENTRY_ID) { $uid = $entry['user_id']; $user_detail = array(); if ($uid != ANONYMOUS) { $user_detail = $user_details[$uid]; $user_detail['is_self'] = $uid == $user_id; if ($uid == $user_id || $can_edit_other_users) { $entries_modifyable[] = $entry['entry_id']; } $user_detail['username_full'] = get_username_string('full', $uid, $user_detail['username'], $user_detail['user_colour']); } else { $user_detail['is_self'] = false; if ($can_edit_other_users) { $entries_modifyable[] = $entry['entry_id']; } $user_detail['username'] = $user_detail['username_full'] = $entry['entry_username'] != '' ? $entry['entry_username'] : $this->user->lang['GUEST']; } foreach ($user_detail as $key => $value) { $template_vars[strtoupper($key)] = $value; } } else { $template_vars['IS_SELF'] = false; $entries_modifyable[] = $entry['entry_id']; } $questions_to_assign = array(); $is_first_question = true; foreach ($questions as $question_id => $question) { $template_vars_question = array(); if (isset($entry['answers'][$question_id])) { $template_vars_question['VALUE'] = $entry['answers'][$question_id]; $template_vars_question['IS_SET'] = true; } else { $template_vars_questions['IS_SET'] = false; } if ($is_first_question) { $is_first_question = false; $template_vars['first_answer_text'] = isset($entry['answers'][$question_id]) ? $entry['answers'][$question_id] : ''; } $template_vars_question['S_INPUT_NAME'] = "answer_{$entry['entry_id']}_{$question_id}" . ($question['type'] == survey::$QUESTION_TYPES['MULTIPLE_CHOICE'] ? '[]' : ''); $template_vars_question['TYPE_STRING'] = array_search($question['type'], survey::$QUESTION_TYPES); $template_vars_question['CAP_EXEEDED'] = $this->survey->cap_exceeded($question_id); $template_vars_question['SELECT_MULTIPLE_HEIGHT'] = min(6, sizeof($question['choices'])); $choices_to_assign = array(); if (isset($entry['answers'][$question_id])) { $exploded_answers = explode(",", $entry['answers'][$question_id]); } foreach ($question['choices'] as $choice) { $template_vars_choices = array(); foreach ($choice as $key => $value) { $template_vars_choices[strtoupper($key)] = $value; } $template_vars_choices['SELECTED'] = isset($entry['answers'][$question_id]) && in_array($choice['text'], $exploded_answers) ? ' selected="selected"' : ''; $choices_to_assign[] = $template_vars_choices; } $template_vars_question['choices'] = $choices_to_assign; $questions_to_assign[] = $template_vars_question; } $template_vars['questions'] = $questions_to_assign; if ($entry['entry_id'] == self::ADDUSER_ENTRY_ID) { $adduser_entry_template_vars = $template_vars; } else { if ($entry['entry_id'] == self::NEW_ENTRY_ID) { $new_entry_template_vars = $template_vars; } else { $entries_to_assign[] = $template_vars; } } } $sort_by = false; $sort_order = $this->survey->settings['reverse_order'] ? SORT_DESC : SORT_ASC; switch ($this->survey->settings['show_order']) { case survey::$SHOW_ORDER_TYPES['ALPHABETICAL_USERNAME']: $sort_by = 'USERNAME'; break; case survey::$SHOW_ORDER_TYPES['RESPONSE_TIME']: $sort_by = 'ENTRY_ID'; break; case survey::$SHOW_ORDER_TYPES['ALPHABETICAL_FIRST_ANSWER']: $sort_by = 'first_answer_text'; break; } if ($sort_by && $this->survey->entries) { $only_sorting_row = array(); foreach ($entries_to_assign as $key => $row) { $only_sorting_row[$key] = $row[$sort_by]; } array_multisort($only_sorting_row, $sort_order, $entries_to_assign); } if ($new_entry_template_vars) { $entries_to_assign[] = $new_entry_template_vars; } if ($adduser_entry_template_vars) { $entries_to_assign[] = $adduser_entry_template_vars; } foreach ($entries_to_assign as $entry_to_assign) { if (isset($entry_to_assign['first_answer_text'])) { unset($entry_to_assign['first_answer_text']); } $questions_to_assign = $entry_to_assign['questions']; unset($entry_to_assign['questions']); $this->template->assign_block_vars('entries', $entry_to_assign); foreach ($questions_to_assign as $question_to_assign) { $assign_choices = false; if (isset($question_to_assign['choices'])) { $choices_to_assign = $question_to_assign['choices']; unset($question_to_assign['choices']); $assign_choices = true; } $this->template->assign_block_vars('entries.questions', $question_to_assign); if ($assign_choices) { foreach ($choices_to_assign as $choice_to_assign) { $this->template->assign_block_vars('entries.questions.choices', $choice_to_assign); } } } } if ($this->question_to_load !== false) { $template_vars = array(); foreach ($this->survey->questions[$this->question_to_load] as $key => $value) { if ($key == 'cap') { $template_vars['S_SURVEY_LOADED_QUESTION_' . strtoupper($key)] = $value != 0 ? $value : ''; } else { if ($key != 'choices') { $template_vars['S_SURVEY_LOADED_QUESTION_' . strtoupper($key)] = $value; } } } $choices_to_load = array(); foreach ($this->survey->questions[$this->question_to_load]['choices'] as $choice) { $choices_to_load[] = $choice['text']; } $template_vars['S_SURVEY_LOADED_QUESTION_CHOICES'] = implode(",", $choices_to_load); $this->template->assign_vars($template_vars); } $this->template->assign_vars(array('S_HAS_SURVEY' => true, 'S_SURVEY_ACTION' => $viewtopic_url, 'S_SURVEY_ACTION_NAME' => $this->action_name, 'S_SURVEY_EXT_ROOT_PATH' => $this->phpbb_root_path, 'S_SURVEY_HAS_QUESTIONS' => (bool) $this->survey->questions, 'S_SURVEY_HIDE_ENTRIES' => $this->survey->hide_entries(), 'S_SURVEY_HIDE_EVERYTHING' => $this->survey->hide_everything(), 'S_SURVEY_IS_CLOSED' => $is_closed, 'S_SURVEY_IS_MODERATOR' => $this->survey->is_moderator(), 'S_SURVEY_CAN_ADD_ENTRY' => $can_add_new_entry, 'S_SURVEY_CAN_EDIT_OTHER_USERS' => $can_edit_other_users, 'S_SURVEY_CAN_MANAGE' => $can_manage, 'S_SURVEY_CAN_MODIFY_OWN_ENTRY' => $this->survey->can_modify_entry($user_id), 'S_SURVEY_CAN_SEE_AVERAGES' => $can_see_averages, 'S_SURVEY_CAN_SEE_EVERYTHING' => $can_see_everything, 'S_SURVEY_CAN_SEE_OR_ADD_ENTRIES' => $can_see_or_add_entries, 'S_SURVEY_CAN_SEE_SUMS' => $can_see_sums, 'S_SURVEY_LOADED_QUESTION' => $this->question_to_load !== false ? true : false, 'S_SURVEY_LOADED_QUESTION_ID' => $this->question_to_load, 'S_SURVEY_MODIFYABLE_ENTRIES' => implode(",", $entries_modifyable), 'S_SURVEY_SHOW_USERNAMES' => !$this->survey->is_anonymized() || $can_see_everything, 'S_SURVEY_SOME_CAP_SET' => $some_cap_set, 'SURVEY_DESC' => $this->user->lang('SURVEY_DESC', $this->user->format_date($this->survey->settings['start_time'])), 'SURVEY_ERRORS' => $survey_errors ? implode('<br />', $survey_errors) : false, 'SURVEY_TOTAL_ENTRIES' => $this->user->lang('SURVEY_TOTAL_ENTRIES', $entry_count), 'U_SURVEY_CHANGE_OPEN' => $action_url . ($is_closed ? 'reopen' : 'close'), 'U_SURVEY_FIND_USERNAME' => append_sid("{$this->phpbb_root_path}memberlist.{$this->phpEx}", 'mode=searchuser&form=surveyform&field=answer_adduser_username&select_single=true'))); add_form_key($this->form_key_name); }