Пример #1
0
 /**
  * This function handles the actions on a question and its answers
  *
  * @todo consider using $form_content instead of $_POST
  *
  * @author Patrick Cool <*****@*****.**>, Ghent University
  * @version January 2007
  */
 function handle_action($form_content)
 {
     $course_id = api_get_course_int_id();
     global $config;
     // Moving an answer up
     if ($_POST['move_up']) {
         foreach ($_POST['move_up'] as $key => &$value) {
             $id1 = $key;
             $content1 = $form_content['answers'][$id1];
             $id2 = $key - 1;
             $content2 = $form_content['answers'][$id2];
             $form_content['answers'][$id1] = $content2;
             $form_content['answers'][$id2] = $content1;
         }
     }
     // Moving an answer down
     if ($_POST['move_down']) {
         foreach ($_POST['move_down'] as $key => &$value) {
             $id1 = $key;
             $content1 = $form_content['answers'][$id1];
             $id2 = $key + 1;
             $content2 = $form_content['answers'][$id2];
             $form_content['answers'][$id1] = $content2;
             $form_content['answers'][$id2] = $content1;
         }
     }
     // Adding an answer
     if (isset($_POST['add_answer'])) {
         $form_content['answers'][] = '';
     }
     // Removing an answer
     if (isset($_POST['remove_answer'])) {
         $max_answer = count($form_content['answers']);
         unset($form_content['answers'][$max_answer - 1]);
     }
     // Saving a question
     if (isset($_POST['save_question'])) {
         $message = survey_manager::save_question($form_content);
         if ($message == 'QuestionAdded' || $message == 'QuestionUpdated') {
             $sql = 'SELECT COUNT(*) FROM ' . Database::get_course_table(TABLE_SURVEY_QUESTION) . ' WHERE c_id = ' . $course_id . ' AND survey_id = ' . intval($_GET['survey_id']);
             $res = Database::fetch_array(Database::query($sql));
             header('Location:survey.php?survey_id=' . intval($_GET['survey_id']) . '&message=' . $message);
             exit;
         } else {
             if ($message == 'PleaseEnterAQuestion' || $message == 'PleasFillAllAnswer' || $message == 'PleaseChooseACondition' || $message == 'ChooseDifferentCategories') {
                 $_SESSION['temp_user_message'] = $form_content['question'];
                 $_SESSION['temp_horizontalvertical'] = $form_content['horizontalvertical'];
                 $_SESSION['temp_sys_message'] = $message;
                 $_SESSION['temp_answers'] = $form_content['answers'];
                 $_SESSION['temp_values'] = $form_content['values'];
                 header('location:question.php?' . api_get_cidreq() . '&question_id=' . intval($_GET['question_id']) . '&survey_id=' . intval($_GET['survey_id']) . '&action=' . Security::remove_XSS($_GET['action']) . '&type=' . Security::remove_XSS($_GET['type']) . '');
                 exit;
             }
         }
     }
     /**
      * This solution is a little bit strange but I could not find a different solution.
      */
     if ($_POST['delete_answer']) {
         foreach ($_POST['delete_answer'] as $key => &$value) {
             unset($form_content['answers'][$key]);
             $deleted = $key;
         }
         foreach ($form_content['answers'] as $key => &$value) {
             if ($key > $deleted) {
                 $form_content['answers'][$key - 1] = $form_content['answers'][$key];
                 unset($form_content['answers'][$key]);
             }
         }
     }
     return $form_content;
 }