<?php
	require 'user-class.php';
	session_start();

	if (isset($_SESSION['usagerEcoleIsoActif']) && $_SESSION['usagerEcoleIsoActif']->u_form == 'S')
	{
		require 'connexion.php';

		$requeteExists = $requeteInsert = "SELECT fs_id FROM isoactif_form_s WHERE fs_u_id_fk = " . escapeValue($_SESSION['usagerEcoleIsoActif']->u_id);
		if (!$resultatExists = $connexion->query($requeteExists)) { die('There was an error running the query : Check if form S exists'); }
		$nbRowsExists = $resultatExists->num_rows;

		$q1 = (isset($_POST['q1']) ? escapeValue($_POST['q1']) : 'NULL');
		$q2 = (isset($_POST['q2']) ? escapeValue($_POST['q2']) : 'NULL');

		$q3_1 = (isset($_POST['q3_1']) ? escapeValue($_POST['q3_1']) : 'NULL');
		$q3_liste_disciplines = (isset($_POST['q3_1_di']) ? escapeValue(serialize($_POST['q3_1_di'])) : '');
		$q3_2 = (isset($_POST['q3_2']) ? escapeValue($_POST['q3_2']) : 'NULL');

		$q4 = (isset($_POST['q4']) ? escapeValue($_POST['q4']) : 'NULL');

		if (isset($_POST['q4_liste_disciplines']))
			$q4_liste_disciplines = escapeValue($_POST['q4_liste_disciplines']);
		else
			$q4_liste_disciplines = '';

		$q5 = (isset($_POST['q5']) ? escapeValue($_POST['q5']) : 'NULL');
		$q5_activites = (isset($_POST['q5_activites']) ? escapeValue($_POST['q5_activites']) : '');

		$q6_1 = (isset($_POST['q6_1']) ? escapeValue($_POST['q6_1']) : 'NULL');
		$q6_programme = (isset($_POST['q6_programme']) ? escapeValue($_POST['q6_programme']) : '');
		$q6_boni = (isset($_POST['q6_boni']) ? escapeValue($_POST['q6_boni']) : 'NULL');
		$q6_2 = (isset($_POST['q6_2']) ? escapeValue($_POST['q6_2']) : 'NULL');

		$q7 = (isset($_POST['q7']) ? escapeValue($_POST['q7']) : 'NULL');

		$q8 = (isset($_POST['q8']) ? escapeValue($_POST['q8']) : 'NULL');
		$q8_boni = (isset($_POST['q8_boni']) ? escapeValue($_POST['q8_boni']) : 'NULL');

		$q9 = (isset($_POST['q9']) ? escapeValue($_POST['q9']) : 'NULL');
		$q9_boni = (isset($_POST['q9_boni']) ? escapeValue($_POST['q9_boni']) : 'NULL');

		$q10 = (isset($_POST['q10']) ? escapeValue($_POST['q10']) : 'NULL');

		// if not exists, insert only entered values
		if ($nbRowsExists == 0)
		{
			$requete = 'INSERT INTO isoactif_form_s (
			fs_q1,
			fs_q2,
			fs_q3_1,
			fs_q3_liste_disciplines,
			fs_q3_2,
			fs_q4,
			fs_q4_liste_disciplines,
			fs_q5,
			fs_q5_activites,
			fs_q6_1,
			fs_q6_boni,
			fs_q6_programme,
			fs_q6_2,
			fs_q7,
			fs_q8,
			fs_q8_boni,
			fs_q9,
			fs_q9_boni,
			fs_q10,
			fs_commentaires,
			fs_u_id_fk
			)
			VALUES (
			' . $q1 . ',
			' . $q2 . ',
			' . $q3_1 . ',
			\'' . $q3_liste_disciplines . '\',
			' . $q3_2 . ',
			' . $q4 . ',
			\'' . $q4_liste_disciplines . '\',
			' . $q5 . ',
			\'' . $q5_activites . '\',
			' . $q6_1 . ',
			' . $q6_boni . ',
			\'' . $q6_programme . '\',
			' . $q6_2 . ',
			' . $q7 . ',
			' . $q8 . ',
			' . $q8_boni . ',
			' . $q9 . ',
			' . $q9_boni . ',
			' . $q10 . ',
			\'\',
			' . escapeValue($_SESSION['usagerEcoleIsoActif']->u_id) . ')';
		}
		else // If exists, update
		{
			$requete = 'UPDATE isoactif_form_s SET
			fs_q1 = ' . $q1 . ',
			fs_q2 = ' . $q2 . ',
			fs_q3_1 = ' . $q3_1 . ',
			fs_q3_liste_disciplines = \'' . $q3_liste_disciplines . '\',
			fs_q3_2 = ' . $q3_2 . ',
			fs_q4 = ' . $q4 . ',
			fs_q5 = ' . $q5 . ',
			fs_q5_activites = \'' . $q5_activites . '\',
			fs_q6_1 = ' . $q6_1 . ',
			fs_q6_boni = ' . $q6_boni . ',
			fs_q6_programme = \'' . $q6_programme . '\',
			fs_q6_2 = ' . $q6_2 . ',
			fs_q7 = ' . $q7 . ',
			fs_q8 = ' . $q8 . ',
			fs_q8_boni = ' . $q8_boni . ',
			fs_q9 = ' . $q9 . ',
			fs_q9_boni = ' . $q9_boni . ',
			fs_q10 = ' . $q10 . ',
			fs_commentaires = \'\'
			WHERE
			fs_u_id_fk = ' . escapeValue($_SESSION['usagerEcoleIsoActif']->u_id);
		}

		if (!$resultat = $connexion->query($requete)) { die('There was an error running the query : Insert/Update form S<br /><br />' . $connexion->error); }

		$connexion->close();
		//print("<pre>" . print_r($_POST, true) . "</pre>");
		//die;
		//exit;
		header ("Location: ../index.php?formS&saved");
	}
?>
