File manager - Edit - /home/missmand/public_html/learning/old/main/couple/form.php
Back
<?php /* For licensing terms, see /license.txt */ /** * Model CRUD for couple_tutor_form and related tables * @author Batiste Roger <batiste.roger@live.fr> * @package chamilo.tuteur */ require_once(api_get_path(SYS_PATH) . 'main/couple/tool.php'); define('TABLE_TUTOR_FORM', 'couple_tutor_form'); // Name of each form define('TABLE_TUTOR_FORM_QUESTION', 'couple_tutor_form_question'); // Questions / form define('TABLE_TUTOR_FORM_STATE', 'couple_tutor_form_user'); // Has user answered the form? Should it be displayed to him? define('TABLE_TUTOR_FORM_ANSWER', 'couple_tutor_form_question_user'); // Answers to questions /** * Read forms to display to user * @param type $student_user_id (if not sent, all user ids are considered) * @return type */ function form_read($student_user_id = null) { // Chercher dans _form_status les elements à afficher /* $sql = "SELECT form.id as id, state.form_id as form_id, form.title as title, state.state as state FROM " . TABLE_TUTOR_FORM_STATE . " state JOIN " . TABLE_TUTOR_FORM . " form WHERE state.form_id = form.id AND user_id = '$student_user_id' " . "ORDER BY state.state DESC"; */ $sql = "SELECT id, title FROM " . TABLE_TUTOR_FORM; $result = Database::query($sql); $forms = array(); while ($row = Database::fetch_assoc($result)) { $form_id = $row['id']; $sql = "SELECT * FROM " . TABLE_TUTOR_FORM_STATE . " WHERE form_id = $form_id "; if (!empty($student_user_id)) { $sql .= "AND user_id = '$student_user_id'"; } $sql .= " ORDER BY state DESC"; $res_state = db_query($sql); $state = isset($res_state[0]['state']) ? $res_state[0]['state'] : 0; $row['id'] = $form_id; $row['state'] = $state; $forms[] = $row; } return $forms; } function form_read_with_questions ($student_user_id = null) { $forms = form_read($student_user_id); foreach ($forms as $key => $form) { $form_id = $form['id']; $sql = "SELECT * FROM " . TABLE_TUTOR_FORM_QUESTION . " WHERE form_id = $form_id"; $result = db_query($sql); $forms[$key]['questions'] = $result; } return $forms; } /** * Read answers to questions * @param type $user_id * @param type $form_id * @return type */ function form_read_form ($user_id, $form_id) { $sql = "SELECT * FROM " . TABLE_TUTOR_FORM_QUESTION . " WHERE form_id = $form_id"; $result_1 = Database::query($sql); $questions = array(); while ($row = Database::fetch_assoc($result_1)) { $question_id = $row['id']; $question_question = $row['question']; $sql = "SELECT answer FROM " . TABLE_TUTOR_FORM_ANSWER . " WHERE question_id = '$question_id' AND user_id = '$user_id'"; $result = Database::query($sql); $question_row_one = Database::fetch_assoc($result); $questions[$question_id] = array( 'id' => $question_id, 'question' => $question_question, 'answer' => isset($question_row_one['answer']) ? trim($question_row_one['answer']) : '' ); } return $questions; } /** Submit a form * Method POST used, be careful!!! * @param type $user_id * @param type $form_id * @param type $boolean_submit */ function form_save ($user_id, $form_id, $boolean_submit = false) { $questions = form_read_form($user_id, $form_id); foreach ($questions as $id => $question) { if (!isset($_POST[$id])) error_throw("Id '$id' is not set. Maybe the question isn't answered", "couple/form.php:form_read_form"); // Try to find the row with that question's answer $sql = "SELECT id FROM " . TABLE_TUTOR_FORM_ANSWER . " WHERE user_id = '$user_id' AND question_id = '$id'"; $result = Database::query($sql); if (Database::num_rows($result) > 0) { // Update $row = Database::fetch_assoc($result); $row_id = $row['id']; $sql = "UPDATE " . TABLE_TUTOR_FORM_ANSWER . " SET answer = '" . trim(Database::escape_string($_POST[$id])) . "' WHERE id = '$row_id'"; Database::query($sql); } else { // Create $row = Database::fetch_assoc($result); $row_id = $row['id']; $sql = "INSERT INTO " . TABLE_TUTOR_FORM_ANSWER . " SET answer = '" . trim(Database::escape_string($_POST[$id])) . "', user_id = '$user_id', question_id='$id'"; Database::query($sql); } // Finally, if submit, change state for user / form if ($boolean_submit) form_set_state($user_id, $form_id, 2); } } /** * Setteur pour TABLE_TUTOR_FORM_STATE.state * @param type $user_id * @param type $form_id * @param type $state */ function form_set_state ($user_id, $form_id, $state) { $sql = "SELECT * FROM " . TABLE_TUTOR_FORM_STATE . " WHERE user_id = '$user_id' AND form_id = '$form_id'"; $result = Database::query($sql); if (Database::num_rows($result) > 0) { $sql = "UPDATE " . TABLE_TUTOR_FORM_STATE . " SET state = '$state' WHERE user_id = '$user_id' AND form_id = '$form_id'"; return Database::query($sql); } else { $sql = "INSERT INTO " . TABLE_TUTOR_FORM_STATE . " SET state = '$state', user_id = '$user_id', form_id = '$form_id'"; return Database::query($sql); } } /** * Create a form (Only tutors and admins should be able to call this method) * @param type $title * @param type $questions */ function form_create ($title, $questions) { // Check params if (empty($title) || empty($questions) || sizeof($questions) < 1) error_throw ("Wrong paramters : $title, " . print_r($questions, true), "form.php:form_create"); // Insert title $sql_form = "INSERT INTO " . TABLE_TUTOR_FORM . " SET title = '" . Database::escape_string(trim($title)) . "';"; $result_form = db_query($sql_form, false); $form_id = mysql_insert_id(); if ($result_form) { // Insert all questions $sql_questions = "INSERT INTO " . TABLE_TUTOR_FORM_QUESTION . " (form_id, question) VALUES "; foreach ($questions as $nb => $question) { if ($nb != 1) $sql_questions .= ', '; $sql_questions .= "('$form_id', '" . Database::escape_string(trim($question)) . "')"; } $sql_questions .= ";"; return db_query($sql_questions, false); } error_throw("Error in $sql_form, query did not end properly", "form.php:form_create"); } /** * Deletes a form and all its related tables * @param type $id */ function form_delete_form ($id) { // Delete in form, questions, answers and state $sql = "DELETE FROM " . TABLE_TUTOR_FORM . " WHERE id = '$id'"; Database::query($sql); $tables = array(TABLE_TUTOR_FORM_QUESTION, TABLE_TUTOR_FORM_STATE); foreach ($tables as $table) { $sql = "DELETE FROM $table WHERE form_id = '$id'"; Database::query($sql); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings