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);
 }
Esempio n. 2
0
 /**
  * Process modification of own entry
  *
  * @return array errors
  */
 protected function process_entry_modification()
 {
     if (!check_form_key($this->form_key_name)) {
         return array($this->user->lang('FORM_INVALID'));
     }
     if (!$this->request->is_set_post('entries_to_modify')) {
         return array();
     }
     $entry_ids = array_unique(explode(",", $this->request->variable('entries_to_modify', '')));
     $entry_user_id = $real_user_id = $this->user->data['user_id'];
     $errors = array();
     foreach ($entry_ids as $entry_id) {
         $changed = $filled_out = false;
         if ($entry_id == self::ADDUSER_ENTRY_ID) {
             $username = utf8_normalize_nfc($this->request->variable('answer_adduser_username', '', true));
             if ($username == '') {
                 continue;
             }
             $adduser_id = array();
             if (user_get_id_name($adduser_id, $username) == 'NO_USERS') {
                 $errors[] = $this->user->lang('NO_USER');
                 continue;
             }
             $entry_user_id = $adduser_id[0];
             if (!$this->survey->can_add_new_entry($real_user_id, $entry_user_id)) {
                 $errors[] = $this->user->lang('NO_AUTH_OPERATION');
                 continue;
             }
         } else {
             $entry_id = (int) $entry_id;
             if ($entry_id == self::NEW_ENTRY_ID && !$this->survey->can_add_new_entry($real_user_id)) {
                 $errors[] = $this->user->lang('NO_AUTH_OPERATION');
                 continue;
             } else {
                 if ($entry_id != self::NEW_ENTRY_ID && !$this->survey->entry_exists($entry_id)) {
                     continue;
                 } else {
                     if ($entry_id != self::NEW_ENTRY_ID && !$this->survey->can_modify_entry($real_user_id, $this->survey->entries[$entry_id]['user_id'])) {
                         $errors[] = $this->user->lang('NO_AUTH_OPERATION');
                         continue;
                     }
                 }
             }
         }
         $answers = array();
         $abort = false;
         foreach ($this->survey->questions as $question_id => $question) {
             $answers[$question_id] = $this->request->is_set_post("answer_{$entry_id}_{$question_id}") ? $this->request->variable("answer_{$entry_id}_{$question_id}", '', true) : '';
             if ($question['type'] == survey::$QUESTION_TYPES['DROP_DOWN_MENU']) {
                 if (isset($question['choices'][$answers[$question_id]])) {
                     $answers[$question_id] = $question['choices'][$answers[$question_id]]['text'];
                 } else {
                     $answers[$question_id] = '';
                 }
             } else {
                 if ($question['type'] == survey::$QUESTION_TYPES['MULTIPLE_CHOICE']) {
                     $answers_choice_array = array_unique($this->request->variable("answer_{$entry_id}_{$question_id}", array(0)));
                     $answers[$question_id] = array();
                     foreach ($answers_choice_array as $choice_id) {
                         if (isset($question['choices'][$choice_id])) {
                             $answers[$question_id][] = $question['choices'][$choice_id]['text'];
                         }
                     }
                     $answers[$question_id] = implode(",", $answers[$question_id]);
                 }
             }
             $old_exists = $entry_id != self::ADDUSER_ENTRY_ID && $entry_id != self::NEW_ENTRY_ID && isset($this->survey->entries[$entry_id]['answers'][$question_id]);
             $old_value = $old_exists ? $this->survey->entries[$entry_id]['answers'][$question_id] : 0;
             if ($answers[$question_id] != '') {
                 if (!$this->survey->check_answer($answers[$question_id], $question_id)) {
                     $errors[] = $this->user->lang('SURVEY_INVALID_ANSWER');
                     $abort = true;
                     continue;
                 }
                 $filled_out = true;
                 if ($this->survey->has_cap($question_id) && !$this->survey->can_manage($real_user_id)) {
                     $diff = $this->survey->modify_sum_entry($question_id, false, true, $answers[$question_id], $old_exists, $old_value);
                     if ($diff != 0 && $this->survey->cap_exceeded($question_id, $diff)) {
                         $errors[] = $this->user->lang('SURVEY_CAP_EXEEDED', $this->survey->questions[$question_id]['label']);
                         $abort = true;
                         continue;
                     }
                 }
                 if (!$old_exists || $old_value != $answers[$question_id]) {
                     $changed = true;
                 }
             } else {
                 if ($old_exists && $old_value != '') {
                     $changed = true;
                 }
             }
         }
         if ($abort) {
             continue;
         }
         if ($filled_out) {
             if ($entry_id == self::ADDUSER_ENTRY_ID || $entry_id == self::NEW_ENTRY_ID) {
                 $this->survey->add_entry($entry_user_id, $answers);
             } else {
                 if ($changed) {
                     $this->survey->modify_entry($entry_id, $answers);
                 }
             }
         } else {
             if ($entry_id != self::ADDUSER_ENTRY_ID && $entry_id != self::NEW_ENTRY_ID) {
                 $this->survey->delete_entry($entry_id);
             }
         }
     }
     return $errors;
 }