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

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

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

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

		$q2_1 = (isset($_POST['q2_1']) ? escapeValue($_POST['q2_1']) : 'NULL');
		$q2_2 = (isset($_POST['q2_2']) ? escapeValue($_POST['q2_2']) : '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');
		//$q4_liste_disciplines = (isset($_POST['q4_liste_disciplines']) ? escapeValue($_POST['q4_liste_disciplines']) : '');
		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(serialize($_POST['q5_activites'])) : '');

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

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

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

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

		// if not exists, insert only entered values
		if ($nbRowsExists == 0)
		{
			$requete = 'INSERT INTO isoactif_form_p (
			fp_q1,
			fp_q2_1,
			fp_q2_2,
			fp_q3_1,
			fp_q3_liste_disciplines,
			fp_q3_2,
			fp_q4,
			fp_q4_liste_disciplines,
			fp_q5,
			fp_q5_activites,
			fp_q6,
			fp_q6_activites,
			fp_q7_1,
			fp_q7_2,
			fp_q8,
			fp_q8_initiatives,
			fp_q9,
			fp_q10,
			fp_commentaires,
			fp_u_id_fk
			)
			VALUES (
			' . $q1 . ',
			' . $q2_1 . ',
			' . $q2_2 . ',
			' . $q3_1 . ',
			\'' . $q3_liste_disciplines . '\',
			' . $q3_2 . ',
			' . $q4 . ',
			\'' . $q4_liste_disciplines . '\',
			' . $q5 . ',
			\'' . $q5_activites . '\',
			' . $q6 . ',
			\'' . $q6_activites . '\',
			' . $q7_1 . ',
			' . $q7_2 . ',
			' . $q8 . ',
			\'' . $q8_initiatives . '\',
			' . $q9 . ',
			' . $q10 . ',
			\'\',
			' . escapeValue($_SESSION['usagerEcoleIsoActif']->u_id) . ')';
		}
		else // If exists, update
		{
			$requete = 'UPDATE isoactif_form_p SET
			fp_q1 = ' . $q1 . ',
			fp_q2_1 = ' . $q2_1 . ',
			fp_q2_2 = ' . $q2_2 . ',
			fp_q3_1 = ' . $q3_1 . ',
			fp_q3_liste_disciplines = \'' . $q3_liste_disciplines . '\',
			fp_q3_2 = ' . $q3_2 . ',
			fp_q4 = ' . $q4 . ',
			fp_q4_liste_disciplines = \'' . $q4_liste_disciplines . '\',
			fp_q5 = ' . $q5 . ',
			fp_q5_activites = \'' . $q5_activites . '\',
			fp_q6 = ' . $q6 . ',
			fp_q6_activites = \'' . $q6_activites . '\',
			fp_q7_1 = ' . $q7_1 . ',
			fp_q7_2 = ' . $q7_2 . ',
			fp_q8 = ' . $q8 . ',
			fp_q8_initiatives = \'' . $q8_initiatives . '\',
			fp_q9 = ' . $q9 . ',
			fp_q10 = ' . $q10 . ',
			fp_commentaires = \'\'
			WHERE
			fp_u_id_fk = ' . escapeValue($_SESSION['usagerEcoleIsoActif']->u_id);
		}

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

		/*
		print("<pre>" . print_r($_POST, true) . "</pre>");
		echo($requete);
		die();
		exit;
		*/

		$connexion->close();
		header ("Location: ../index.php?formP&saved");
	}
?>
