예제 #1
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;
 }