示例#1
0
 /**
  * Displays survey data in posting.php
  *
  * @param unknown $event
  */
 public function posting_display_template($event)
 {
     if (!$this->survey->can_create_survey($event['forum_id'])) {
         return;
     }
     // Check for first post
     if (isset($event['post_data']['topic_first_post_id']) && (!isset($event['post_data']['post_id']) || $event['post_data']['topic_first_post_id'] != $event['post_data']['post_id'])) {
         return;
     }
     $this->user->add_lang_ext('kilianr/survey', 'survey');
     if (isset($event['topic_id']) && $event['topic_id']) {
         $this->survey->load_survey($event['topic_id']);
     }
     $this->template->assign_vars(array('S_SURVEY_ALLOWED' => true, 'S_TOPIC_HAS_SURVEY' => $this->survey->enabled, 'S_TOPIC_HAS_INACTIVE_SURVEY' => ($this->survey->entries || $this->survey->questions) && !$this->survey->enabled));
 }
 public function convert_old_survey_data()
 {
     global $auth, $user;
     if (!isset($this->config['survey_version'])) {
         return;
     }
     if (!function_exists('user_get_id_name')) {
         include "{$this->phpbb_root_path}includes/functions_user.{$this->php_ext}";
     }
     $user->add_lang_ext('kilianr/survey', 'survey');
     $sql = 'SELECT topic_id FROM ' . TOPICS_TABLE . ' WHERE topic_survey = 1';
     $result = $this->db->sql_query($sql);
     while ($row = $this->db->sql_fetchrow($result)) {
         $topic_id = $row['topic_id'];
         $sql = "SELECT * FROM {$this->table_prefix}survey WHERE topic_id = '{$topic_id}'";
         $result2 = $this->db->sql_query($sql);
         $old_settings = $this->db->sql_fetchrow($result2);
         $this->db->sql_freeresult($result2);
         if ($old_settings) {
             $survey = new survey($this->db, $this->config, $user, $auth, $this->table_prefix . 'surveys', $this->table_prefix . 'surveys_questions', $this->table_prefix . 'surveys_q_choices', $this->table_prefix . 'surveys_entries', $this->table_prefix . 'surveys_answers');
             $survey->enable($topic_id);
             $survey->initialize($topic_id);
             $survey->load_survey($topic_id);
             // Convert the settings
             $settings = array('caption' => $old_settings['survey_caption'] ? $old_settings['survey_caption'] : $user->lang('SURVEY'), 'show_order' => min(max((int) $old_settings['show_order'], 0), 2), 'reverse_order' => $old_settings['show_order'] == 3 ? 1 : 0, 'allow_change_answer' => $old_settings['allow_change_answers'] == 1 ? 1 : 0, 'allow_multiple_answer' => $old_settings['allow_change_answers'] == 2 ? 1 : 0, 'visibility' => $old_settings['hide_survey_results'] ? 3 : ($old_settings['hide_names_of_respondents'] ? 1 : 0), 'start_time' => $old_settings['survey_start'], 'stop_time' => $old_settings['survey_length'] == 0 ? null : $old_settings['survey_start'] + $old_settings['survey_length']);
             $survey->change_config($settings);
             // Convert the questions
             $questions_skip = array();
             $questions_label = array_map('trim', explode('|', $old_settings['questions']));
             $questions_type = explode('|', $old_settings['question_types']);
             $questions_choices = explode('|', htmlspecialchars_decode($old_settings['question_selections']));
             $questions_sum_type = explode('|', $old_settings['question_sums']);
             $questions_sum_by = array_map('trim', explode('|', $old_settings['question_selected_text']));
             $questions_cap = explode('|', $old_settings['question_response_caps']);
             $num_questions = min(sizeof($questions_label), sizeof($questions_type), sizeof($questions_choices), sizeof($questions_sum_type), sizeof($questions_sum_by), sizeof($questions_cap));
             for ($i = 0; $i < $num_questions; $i++) {
                 $questions_skip[$i] = false;
                 if ($questions_label[$i] == '' || $survey->get_question_id_from_label($questions_label[$i], -1) != -1) {
                     $questions_skip[$i] = true;
                     continue;
                 }
                 $new_type = min(max((int) $questions_type[$i], 0), 5);
                 $new_type = $new_type == 2 ? 0 : $new_type;
                 $new_type = $new_type == 3 ? 4 : $new_type;
                 $question = array('label' => $questions_label[$i], 'example_answer' => '', 'type' => $new_type, 'random_choice_order' => 0, 'sum_type' => min(max((int) $questions_sum_type[$i] == 4 ? 2 : (int) $questions_sum_type[$i], 0), 3), 'sum_by' => $questions_sum_type[$i] == 3 ? $questions_sum_by[$i] : '', 'average' => $questions_sum_type[$i] == 4 ? 1 : 0, 'cap' => (int) $questions_cap[$i]);
                 $choices = array();
                 if ($new_type == 4 || $new_type == 5) {
                     if ($questions_choices[$i] == '') {
                         $questions_skip[$i] = true;
                         continue;
                     }
                     foreach (array_map('trim', array_unique(explode(";", $questions_choices[$i]))) as $choice) {
                         if ($choice == '') {
                             continue;
                         }
                         $choices[] = htmlspecialchars(str_replace(',', '', $choice));
                     }
                 }
                 $questions_type[$i] = $new_type;
                 $survey->add_question($question, $choices);
                 $questions_id[$i] = $survey->get_question_id_from_label($questions_label[$i], -1);
             }
             // Convert the answers
             $sql = "SELECT user_id, answers FROM {$this->table_prefix}survey_answers WHERE survey_id = {$old_settings['survey_id']} ORDER BY response_order";
             $result2 = $this->db->sql_query($sql);
             while ($row2 = $this->db->sql_fetchrow($result2)) {
                 $username = array();
                 if (user_get_id_name($row2['user_id'], $username) == 'NO_USERS') {
                     continue;
                 }
                 $raw_answers = explode('|', $row2['answers']);
                 $answers = array();
                 $i = -1;
                 foreach ($raw_answers as $answer) {
                     ++$i;
                     if ($i >= $num_questions || $questions_skip[$i]) {
                         continue;
                     }
                     if ($questions_type[$i] == 5) {
                         $answer = implode(',', array_map('trim', explode('&&', str_replace(',', '', $answer))));
                     }
                     if ($questions_type[$i] == 4) {
                         $answer = str_replace(',', '', trim($answer));
                     }
                     if (!$survey->check_answer($answer, $questions_id[$i])) {
                         continue;
                     }
                     $answers[$questions_id[$i]] = $answer;
                 }
                 $survey->add_entry($row2['user_id'], $answers);
             }
             $this->db->sql_freeresult($result2);
             unset($survey);
         }
     }
     $this->db->sql_freeresult($result);
 }
示例#3
0
 /**
  * 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}&amp;{$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&amp;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&amp;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&amp;form=surveyform&amp;field=answer_adduser_username&amp;select_single=true')));
     add_form_key($this->form_key_name);
 }