*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/**
* \file htdocs/opensurvey/public/studs.php
* \ingroup opensurvey
* \brief Page to list surveys
*/
define("NOLOGIN",1); // This means this output page does not require to be logged.
define("NOCSRFCHECK",1); // We accept to go on this page from external web site.
require_once('../../main.inc.php');
require_once(DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php");
require_once(DOL_DOCUMENT_ROOT."/core/lib/files.lib.php");
require_once(DOL_DOCUMENT_ROOT."/opensurvey/class/opensurveysondage.class.php");
require_once(DOL_DOCUMENT_ROOT."/opensurvey/fonctions.php");
// Init vars
$action=GETPOST('action');
$numsondage = $numsondageadmin = '';
if (GETPOST('sondage'))
{
if (strlen(GETPOST('sondage')) == 24) // recuperation du numero de sondage admin (24 car.) dans l'URL
{
$numsondageadmin=GETPOST("sondage",'alpha');
$numsondage=substr($numsondageadmin, 0, 16);
}
else
{
$numsondageadmin='';
$numsondage=GETPOST("sondage",'alpha');
}
}
$object=new Opensurveysondage($db);
$result=$object->fetch(0,$numsondage);
if ($result <= 0) dol_print_error('','Failed to get survey id '.$numsondage);
$nblignes=count($object->fetch_lines());
/*
* Actions
*/
$nbcolonnes = substr_count($object->sujet, ',') + 1;
$listofvoters=explode(',',$_SESSION["savevoter"]);
// Add comment
if (GETPOST('ajoutcomment'))
{
$error=0;
if (! GETPOST('comment'))
{
$error++;
setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Comment")),'errors');
}
if (! GETPOST('commentuser'))
{
$error++;
setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("User")),'errors');
}
if (! $error)
{
$comment = GETPOST("comment");
$comment_user = GETPOST('commentuser');
$sql = "INSERT INTO ".MAIN_DB_PREFIX."opensurvey_comments (id_sondage, comment, usercomment)";
$sql.= " VALUES ('".$db->escape($numsondage)."','".$db->escape($comment)."','".$db->escape($comment_user)."')";
$resql = $db->query($sql);
dol_syslog("sql=".$sql);
if (! $resql) dol_print_error($db);
}
}
// Add vote
if (isset($_POST["boutonp"]) || isset($_POST["boutonp_x"]))
{
//Si le nom est bien entré
if (GETPOST('nom'))
{
$nouveauchoix = '';
for ($i=0;$i<$nbcolonnes;$i++)
{
if (isset($_POST["choix$i"]) && $_POST["choix$i"] == '1')
{
$nouveauchoix.="1";
}
else if (isset($_POST["choix$i"]) && $_POST["choix$i"] == '2')
{
$nouveauchoix.="2";
}
else { // sinon c'est 0
$nouveauchoix.="0";
}
}
$nom=substr($_POST["nom"],0,64);
// Check if vote already exists
$sql = 'SELECT id_users, nom FROM '.MAIN_DB_PREFIX."opensurvey_user_studs WHERE id_sondage='".$db->escape($numsondage)."' AND nom = '".$db->escape($nom)."' ORDER BY id_users";
$resql = $db->query($sql);
$num_rows = $db->num_rows($resql);
if ($num_rows > 0)
{
setEventMessage($langs->trans("VoteNameAlreadyExists"),'errors');
$error++;
}
else
{
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'opensurvey_user_studs (nom, id_sondage, reponses)';
$sql.= " VALUES ('".$db->escape($nom)."', '".$db->escape($numsondage)."','".$db->escape($nouveauchoix)."')";
$resql=$db->query($sql);
if ($resql)
{
// Add voter to session
$_SESSION["savevoter"]=$nom.','.(empty($_SESSION["savevoter"])?'':$_SESSION["savevoter"]); // Save voter
$listofvoters=explode(',',$_SESSION["savevoter"]);
if (! empty($object->mailsonde))
{
include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
$cmailfile=new CMailFile("[".MAIN_APPLICATION_TITLE."] ".$langs->trans("Poll").': '.$object->titre, $object->mail_admin, $conf->global->MAIN_MAIL_EMAIL_FROM, $nom." has filled a line.\nYou can find your poll at the link:\n".getUrlSondage($numsondage));
$result=$cmailfile->sendfile();
if ($result)
{
}
else
{
}
}
}
else dol_print_error($db);
}
}
else
{
$err |= NAME_EMPTY;
}
}
// Update vote
$testmodifier = false;
$testligneamodifier = false;
$ligneamodifier = -1;
for ($i=0; $i<$nblignes; $i++)
{
if (isset($_POST['modifierligne'.$i]))
{
$ligneamodifier=$i;
$testligneamodifier=true;
}
//test pour voir si une ligne est a modifier
if (isset($_POST['validermodifier'.$i]))
{
$modifier=$i;
$testmodifier=true;
}
}
if ($testmodifier)
{
//var_dump($_POST);exit;
$nouveauchoix = '';
for ($i=0;$i<$nbcolonnes;$i++)
{
//var_dump($_POST["choix$i"]);
if (isset($_POST["choix$i"]) && $_POST["choix$i"] == '1')
{
$nouveauchoix.="1";
}
else if (isset($_POST["choix$i"]) && $_POST["choix$i"] == '2')
{
$nouveauchoix.="2";
}
else { // sinon c'est 0
$nouveauchoix.="0";
}
}
$idtomodify=$_POST["idtomodify".$modifier];
$sql = 'UPDATE '.MAIN_DB_PREFIX."opensurvey_user_studs";
$sql.= " SET reponses = '".$db->escape($nouveauchoix)."'";
$sql.= " WHERE id_users = '".$db->escape($idtomodify)."'";
dol_syslog("sql=".$sql);
$resql = $db->query($sql);
if (! $resql) dol_print_error($db);
}
// Delete comment
$idcomment=GETPOST('deletecomment','int');
if ($idcomment)
{
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'opensurvey_comments WHERE id_comment = '.$idcomment;
$resql = $db->query($sql);
}
/*
* View
*/
$form=new Form($db);
$object=new OpenSurveySondage($db);
$result=$object->fetch(0,$numsondage);
if ($result <= 0)
{
print $langs->trans("ErrorRecordNotFound");
llxFooterSurvey();
exit;
}
$arrayofjs=array();
$arrayofcss=array('/opensurvey/css/style.css');
llxHeaderSurvey($object->titre, "", 0, 0, $arrayofjs, $arrayofcss);
// Define format of choices
$toutsujet=explode(",",$object->sujet);
$listofanswers=array();
foreach ($toutsujet as $value)
{
$tmp=explode('@',$value);
$listofanswers[]=array('label'=>$tmp[0],'format'=>($tmp[1]?$tmp[1]:'checkbox'));
}
$toutsujet=str_replace("°","'",$toutsujet);
print '
'.$langs->trans("YouAreInivitedToVote").'
';
print $langs->trans("OpenSurveyHowTo").'
';
print ' '."\n";
//affichage du titre du sondage
$titre=str_replace("\\","",$object->titre);
print ''.$titre.'
'."\n";
//affichage du nom de l'auteur du sondage
print $langs->trans("InitiatorOfPoll") .' : '.$object->nom_admin.'
'."\n";
//affichage des commentaires du sondage
if ($object->commentaires)
{
print '
'.$langs->trans("Description") .' :
'."\n";
$commentaires=dol_nl2br($object->commentaires);
print $commentaires;
print '
'."\n";
}
print '
'."\n";
print '