コード例 #1
0
function save_user_survey($user_id, $survey_id, $posted_values)
{
    try {
        $questions_resultset = query_questions($survey_id);
        while ($question_row = mysqli_fetch_assoc($questions_resultset)) {
            //get user answers if any
            $user_answers = isset($posted_values['qid_' . $question_row['question_id']]) ? $posted_values['qid_' . $question_row['question_id']] : null;
            //get list of user answers option ids if any
            $selection_list = "";
            if (empty($user_answers)) {
                $selection_list = "0";
            } else {
                while (list($field, $value) = each($user_answers)) {
                    $control_type = trim(substr($field, 0, 3));
                    //note:  can't make switch to work???
                    if ($control_type == "txt" || $control_type == "chk") {
                        $selection_list .= substr($field, 4) . ",";
                    } else {
                        $selection_list .= $value . ",";
                    }
                }
                $selection_list = substr($selection_list, 0, strlen($selection_list) - 1);
            }
            //echo $selection_list."<br>";
            //clean database of any answers not in the new user selection list - prevents unchanged selections to be deleted unnecessarily
            delete_user_answers($user_id, $survey_id, $question_row['question_id'], $selection_list);
            //update or insert any changes posted by user
            if (isset($posted_values['qid_' . $question_row['question_id']])) {
                $new_selections = $posted_values['qid_' . $question_row['question_id']];
                while (list($field, $value) = each($new_selections)) {
                    $control_type = trim(substr($field, 0, 3));
                    switch ($control_type) {
                        case "txt":
                            $answer_exists = check_answer_exist($user_id, $survey_id, $question_row['question_id'], substr($field, 4));
                            if ($answer_exists) {
                                //if user_id, survey_id, question_id, option_id exist in user_answers, then execute update statement
                                update_user_answers($user_id, $survey_id, $question_row['question_id'], substr($field, 4), $value);
                            } else {
                                //else execute insert statement with answer_text populated with &value
                                insert_user_answers($user_id, $survey_id, $question_row['question_id'], substr($field, 4), $value);
                            }
                            break;
                        case "chk":
                            $answer_exists = check_answer_exist($user_id, $survey_id, $question_row['question_id'], substr($field, 4));
                            //if user_id, survey_id, question_id, option_id exist in user_answers, then do nothing
                            if (!$answer_exists) {
                                insert_user_answers($user_id, $survey_id, $question_row['question_id'], substr($field, 4), null);
                            }
                            break;
                        case "rad":
                            $answer_exists = check_answer_exist($user_id, $survey_id, $question_row['question_id'], $value);
                            if (!$answer_exists) {
                                insert_user_answers($user_id, $survey_id, $question_row['question_id'], $value, null);
                            }
                            break;
                    }
                }
            }
        }
        //end of looping questions
        $save_result['success'] = true;
        return $save_result;
        /*		switch ($posted_values['save']){
        			case "Save":
        						echo "Survey saved successfully.  You can return anytime to complete the survey!";
        						break;
        			case "Submit":
        						echo "Survey successfully submitted.  Thank you!";
        						break;
        		}
        */
    } catch (Exception $e) {
        $save_result['success'] = false;
        return $save_result;
        /*		echo "There was a problem submitting your request: ",  $e->getMessage(), "\n";
        		echo "Please try again later.";
        */
    }
    //end of try catch
}
コード例 #2
0
            $validation_results = validate_user_survey($survey_id, $_POST);
            if (empty($validation_results)) {
                $commit_results = save_user_survey($user_id, $survey_id, $_POST);
                $commit_results['action'] = "Submit";
                $queryurl = urlencode(serialize($commit_results));
                header("location: confirmation.php?action=" . $queryurl);
                exit;
            } else {
                $validationSuccess = false;
            }
            break;
    }
    //end of switch POST
}
// end of if
$questions = query_questions($survey_id);
$num_of_questions = mysqli_num_rows($questions);
if ($num_of_questions > 0) {
    for ($x = 1; $x <= $num_of_questions; $x++) {
        echo "<div class='container'>";
        $question_row = mysqli_fetch_assoc($questions);
        //set mandatory flag
        $mandatory_label = $question_row['mandatory_flag'] == "Y" ? " <span class='required' > *</span>" : "";
        echo "Question " . $x . " of " . $num_of_questions . ":  " . $question_row['question'] . $mandatory_label;
        $options_user_answers = query_options_user_answers($user_id, $question_row['question_id']);
        while ($options_row = mysqli_fetch_assoc($options_user_answers)) {
            $column_class = $question_row['option_columns'] > 1 ? "twocolumn" : "onecolumn";
            switch ($options_row['control_type']) {
                case "text":
                    if (!empty($_POST) && $_POST['save'] == "Submit") {
                        if (!empty($_POST['qid_' . $question_row['question_id']]['txt-' . $options_row['option_id']])) {