/**
  * Process addition or modification of question
  *
  * @return array errors
  */
 protected function process_question_addition_or_modification()
 {
     if (!check_form_key($this->form_key_name)) {
         return array($this->user->lang('FORM_INVALID'));
     }
     $question_id = self::NEW_QUESTION_ID;
     if ($this->request->is_set_post('survey-submit-question-modify')) {
         $question_id = (int) $this->request->variable('question_to_modify', '');
         if (!$this->survey->question_exists($question_id)) {
             return array();
         }
     }
     $question = array('label' => '', 'example_answer' => '', 'type' => 0, 'random_choice_order' => 0, 'sum_type' => 0, 'sum_by' => '', 'average' => 0, 'cap' => 0);
     foreach ($question as $key => $value) {
         $question[$key] = $this->request->variable('question_' . $key, $question[$key], true);
     }
     $question = array_map('trim', $question);
     if ($question['label'] == '') {
         return array($this->user->lang('SURVEY_INVALID_QUESTION_NO_LABEL'));
     }
     if ($this->survey->get_question_id_from_label($question['label'], $question_id) != $question_id) {
         return array($this->user->lang('SURVEY_QUESTION_ALREADY_ADDED', $question['label']));
     }
     $question['random_choice_order'] = $question['random_choice_order'] ? 1 : 0;
     $question['average'] = $question['average'] ? 1 : 0;
     $question['cap'] = $question['cap'] != '' ? $question['cap'] : 0;
     if (!in_array($question['type'], survey::$QUESTION_TYPES)) {
         return array($this->user->lang('SURVEY_INVALID_QUESTION_TYPE'));
     }
     if (!in_array($question['sum_type'], survey::$QUESTION_SUM_TYPES)) {
         return array($this->user->lang('SURVEY_INVALID_QUESTION_SUM_TYPE'));
     }
     if ($question['sum_type'] == survey::$QUESTION_SUM_TYPES['MATCHING_TEXT'] && $question['sum_by'] == '') {
         return array($this->user->lang('SURVEY_INVALID_QUESTION_SUM_BY'));
     }
     if ($question['sum_type'] != survey::$QUESTION_SUM_TYPES['MATCHING_TEXT']) {
         $question['sum_by'] = '';
     }
     if ($question['sum_type'] == survey::$QUESTION_SUM_TYPES['NO_SUM']) {
         $question['average'] = 0;
         $question['cap'] = 0;
     }
     $choices_input = $this->request->variable('question_choices', '', true);
     $choices = array();
     if ($question['type'] == survey::$QUESTION_TYPES['DROP_DOWN_MENU'] || $question['type'] == survey::$QUESTION_TYPES['MULTIPLE_CHOICE']) {
         if ($choices_input == '') {
             return array($this->user->lang('SURVEY_INVALID_QUESTION_CHOICES'));
         }
         $choices = array_unique(explode(",", $choices_input));
     } else {
         $question['random_choice_order'] = 0;
     }
     $choices = array_map('trim', $choices);
     if ($question_id == self::NEW_QUESTION_ID) {
         $this->survey->add_question($question, $choices);
     } else {
         $this->survey->modify_question($question_id, $question, $choices);
     }
     return array();
 }
 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);
 }