Some cleaning in poll creation

This commit is contained in:
Olivier PEREZ 2014-12-25 00:55:52 +01:00
parent 2f3831d997
commit 9ed0043569
10 changed files with 141 additions and 161 deletions

View File

@ -37,7 +37,7 @@ $editingVoteId = 0;
/*----------*/
$logService = new LogService(LOG_FILE);
$pollService = new PollService($connect);
$pollService = new PollService($connect, $logService);
$adminPollService = new AdminPollService($connect, $pollService, $logService);
$inputService = new InputService();

View File

@ -21,12 +21,12 @@ namespace Framadate;
class Form
{
public $titre;
public $commentaires;
public $nom;
public $adresse;
public $formatsondage;
public $champdatefin;
public $title;
public $description;
public $admin_name;
public $admin_mail;
public $format;
public $end_date;
public $choix_sondage;
/**

View File

@ -21,7 +21,7 @@ class LogService {
* @param $message string some message
*/
function log($tag, $message) {
error_log('[' . $tag . '] ' . $message . "\n", 3, $this->output);
error_log(date('H:i:s d/m/Y:') . '[' . $tag . '] ' . $message . "\n", 3, $this->output);
}
}

View File

@ -18,12 +18,18 @@
*/
namespace Framadate\Services;
use Framadate\Form;
use Framadate\FramaDB;
use Framadate\Utils;
class PollService {
private $connect;
private $logService;
function __construct($connect) {
function __construct(FramaDB $connect, LogService $logService) {
$this->connect = $connect;
$this->logService = $logService;
}
/**
@ -54,11 +60,13 @@ class PollService {
public function updateVote($poll_id, $vote_id, $choices) {
$choices = implode($choices);
return $this->connect->updateVote($poll_id, $vote_id, $choices);
}
function addVote($poll_id, $name, $choices) {
$choices = implode($choices);
return $this->connect->insertVote($poll_id, $name, $choices);
}
@ -71,7 +79,7 @@ class PollService {
$result = [];
foreach ($votes as $vote) {
$choices = str_split($vote->reponses);
foreach ($choices as $i=>$choice) {
foreach ($choices as $i => $choice) {
if (empty($result[$i])) {
$result[$i] = 0;
}
@ -80,6 +88,7 @@ class PollService {
}
}
}
return $result;
}
@ -93,6 +102,7 @@ class PollService {
$splitted[] = $obj;
}
return $splitted;
}
@ -106,6 +116,71 @@ class PollService {
$splitted[] = $obj;
}
return $splitted;
}
/**
* @param Form $form
* @return string
*/
function createPoll(Form $form) {
// Generate poll IDs
$poll_id = $this->random(16);
$admin_poll_id = $poll_id . $this->random(8);
// Insert poll + slots
$this->connect->beginTransaction();
$sql = 'INSERT INTO sondage
(poll_id, admin_poll_id, title, comment, admin_name, admin_mail, end_date, format, editable, receiveNewVotes)
VALUES (?,?,?,?,?,?,FROM_UNIXTIME(?),?,?,?)';
$prepared = $this->connect->prepare($sql);
$prepared->execute(array($poll_id, $admin_poll_id, $form->title, $form->description, $form->admin_name, $form->admin_mail, $form->end_date, $form->format, $form->editable, $form->receiveNewVotes));
$prepared = $this->connect->prepare('INSERT INTO sujet_studs (id_sondage, sujet) VALUES (?, ?)');
foreach ($form->getChoices() as $choice) {
// We prepared the slots (joined by comas)
$joinedSlots = '';
$first = true;
foreach ($choice->getSlots() as $slot) {
if ($first) {
$joinedSlots = $slot;
$first = false;
} else {
$joinedSlots .= ',' . $slot;
}
}
// We execute the insertion
if (empty($joinedSlots)) {
$prepared->execute(array($poll_id, $choice->getName()));
} else {
$prepared->execute(array($poll_id, $choice->getName() . '@' . $joinedSlots));
}
}
$this->connect->commit();
$this->logService->log('CREATE_POLL', ' id:' . $poll_id . ', format:' . $form->format . ', admin:' . $form->admin_name . ', mail:' . $form->admin_mail);
return [$poll_id, $admin_poll_id];
}
private function random($car) {
// TODO Better random ?
$string = '';
$chaine = 'abcdefghijklmnopqrstuvwxyz123456789';
srand((double)microtime() * 1000000);
for ($i = 0; $i < $car; $i++) {
$string .= $chaine[rand() % strlen($chaine)];
}
return $string;
}
}

View File

@ -16,7 +16,7 @@
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
namespace Framadate;
use Framadate\Utils;
include_once __DIR__ . '/app/inc/init.php';

View File

@ -20,8 +20,6 @@ namespace Framadate;
include_once __DIR__ . '/app/inc/init.php';
include_once('creation_sondage.php');
if (file_exists('bandeaux_local.php')) {
include_once('bandeaux_local.php');
} else {

View File

@ -16,11 +16,19 @@
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
namespace Framadate;
use Framadate\Services\LogService;
use Framadate\Services\PollService;
use Framadate\Services\MailService;
use Framadate\Utils;
use Framadate\Choice;
include_once __DIR__ . '/app/inc/init.php';
include_once('creation_sondage.php');
/* Service */
/*---------*/
$logService = new LogService(LOG_FILE);
$pollService = new PollService($connect, $logService);
$mailService = new MailService($config['use_smtp']);
if (is_readable('bandeaux_local.php')) {
include_once('bandeaux_local.php');
@ -29,7 +37,7 @@ if (is_readable('bandeaux_local.php')) {
}
// Step 1/4 : error if $_SESSION from info_sondage are not valid
if (!isset($_SESSION['form']->titre) || !isset($_SESSION['form']->nom) || (($config['use_smtp']) ? !isset($_SESSION['form']->adresse) : false)) {
if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) || ($config['use_smtp'] && !isset($_SESSION['form']->admin_mail))) {
Utils::print_header ( _("Error!") );
bandeau_titre(_("Error!"));
@ -56,34 +64,46 @@ if (!isset($_SESSION['form']->titre) || !isset($_SESSION['form']->nom) || (($con
$time = mktime(0,0,0, $registredate[1], $registredate[0], $registredate[2]);
if ($time > time() + (24*60*60))
{
$_SESSION['form']->champdatefin=$time;
$_SESSION['form']->end_date=$time;
}
}
}
if(empty($_SESSION['form']->champdatefin))
{
if(empty($_SESSION['form']->end_date)) {
// By default, expiration date is 6 months after last day
$_SESSION['form']->champdatefin=end($temp_results)+(86400 * $config['default_poll_duration']);
$_SESSION['form']->end_date=end($temp_results)+(86400 * $config['default_poll_duration']);
}
// Insert poll in database
$admin_poll_id = ajouter_sondage(
$_SESSION['form']->titre,
$_SESSION['form']->commentaires,
$_SESSION['form']->nom,
$_SESSION['form']->adresse,
$_SESSION['form']->formatsondage,
$_SESSION['form']->editable,
$_SESSION['form']->champdatefin,
$_SESSION['form']->receiveNewVotes,
$_SESSION['form']->getChoices()
);
$ids = $pollService->createPoll($_SESSION['form']);
$poll_id = $ids[0];
$admin_poll_id = $ids[1];
// Send confirmation by mail if enabled
if ($config['use_smtp'] === true) {
$message = _("This is the message you have to send to the people you want to poll. \nNow, you have to send this message to everyone you want to poll.");
$message .= "\n\n";
$message .= stripslashes(html_entity_decode($_SESSION['form']->admin_name, ENT_QUOTES, "UTF-8")) . ' ' . _("hast just created a poll called") . ' : "' . stripslashes(htmlspecialchars_decode($_SESSION['form']->title, ENT_QUOTES)) . "\".\n";
$message .= _('Thanks for filling the poll at the link above') . " :\n\n%s\n\n" . _('Thanks for your confidence.') . "\n" . NOMAPPLICATION;
$message_admin = _("This message should NOT be sent to the polled people. It is private for the poll's creator.\n\nYou can now modify it at the link above");
$message_admin .= " :\n\n" . "%s \n\n" . _('Thanks for your confidence.') . "\n" . NOMAPPLICATION;
$message = sprintf($message, Utils::getUrlSondage($poll_id));
$message_admin = sprintf($message_admin, Utils::getUrlSondage($admin_poll_id, true));
if ($mailService->isValidEmail($_SESSION['form']->admin_mail)) {
$mailService->send($_SESSION['form']->admin_mail, '[' . NOMAPPLICATION . '][' . _('Author\'s message') . '] ' . _('Poll') . ' : ' . stripslashes(htmlspecialchars_decode($_SESSION['form']->title, ENT_QUOTES)), $message_admin);
$mailService->send($_SESSION['form']->admin_mail, '[' . NOMAPPLICATION . '][' . _('For sending to the polled users') . '] ' . _('Poll') . ' : ' . stripslashes(htmlspecialchars_decode($_SESSION['form']->title, ENT_QUOTES)), $message);
}
}
// Clean Form data in $_SESSION
unset($_SESSION['form']);
// Delete old polls
// TODO Create a PurgeService
Utils::cleaningOldPolls($connect, 'admin/logs_studs.txt');
// Redirect to poll administration
@ -118,7 +138,7 @@ if (!isset($_SESSION['form']->titre) || !isset($_SESSION['form']->nom) || (($con
}
//le format du sondage est DATE
$_SESSION['form']->formatsondage = 'D';
$_SESSION['form']->format = 'D';
// Step 3/4 : Confirm poll creation
if (!empty($_POST['choixheures']) && !isset($_SESSION['form']->totalchoixjour)) {

View File

@ -1,114 +0,0 @@
<?php
/**
* This software is governed by the CeCILL-B license. If a copy of this license
* is not distributed with this file, you can obtain one at
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
*
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
* Authors of Framadate/OpenSondate: Framasoft (https://github.com/framasoft)
*
* =============================
*
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
*
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
// TODO Move this file into a class into app/classes/Framadate
namespace Framadate;
include_once __DIR__ . '/app/inc/init.php';
/**
* Generer une chaine de caractere unique et aleatoire
*/
function random($car)
{
// TODO Better random ?
$string = '';
$chaine = 'abcdefghijklmnopqrstuvwxyz123456789';
srand((double)microtime()*1000000);
for($i=0; $i<$car; $i++) {
$string .= $chaine[rand()%strlen($chaine)];
}
return $string;
}
function ajouter_sondage($title, $comment, $adminName, $adminMail, $format, $editable, $endDate, $receiveNewVotes, $choices)
{
global $connect;
global $config;
// Generate poll ids
$poll_id = random(16);
$admin_poll_id = $poll_id.random(8);
// Insert poll + slots
$connect->beginTransaction();
$sql = 'INSERT INTO sondage
(poll_id, admin_poll_id, title, comment, admin_name, admin_mail, end_date, format, editable, receiveNewVotes)
VALUES (?,?,?,?,?,?,FROM_UNIXTIME(?),?,?,?)';
$prepared = $connect->prepare($sql);
$prepared->execute(array($poll_id, $admin_poll_id, $title, $comment, $adminName, $adminMail, $endDate, $format, $editable, $receiveNewVotes));
$prepared = $connect->prepare('INSERT INTO sujet_studs (id_sondage, sujet) VALUES (?, ?)');
foreach ($choices as $choice) {
// We prepared the slots (joined by comas)
$joinedSlots = '';
$first = true;
foreach ($choice->getSlots() as $slot) {
// We prepared the slots (joined by comas)
$joinedSlots = '';
$first = true;
foreach ($choice->getSlots() as $slot) {
if ($first) {
$joinedSlots = $slot;
$first = false;
} else {
$joinedSlots .= ',' . $slot;
}
}
// We execute the insertion
if (empty($joinedSlots)) {
$prepared->execute(array($poll_id, $choice->getName()));
} else {
$prepared->execute(array($poll_id, $choice->getName().'@'.$joinedSlots));
}
}
}
$connect->commit();
// Send confirmation by mail if enabled
if($config['use_smtp'] === true){
$message = _("This is the message you have to send to the people you want to poll. \nNow, you have to send this message to everyone you want to poll.");
$message .= "\n\n";
$message .= stripslashes(html_entity_decode($adminName, ENT_QUOTES, "UTF-8"))." " . _("hast just created a poll called") . " : \"".stripslashes(htmlspecialchars_decode($title,ENT_QUOTES))."\".\n";
$message .= _("Thanks for filling the poll at the link above") . " :\n\n%s\n\n" . _("Thanks for your confidence.") . "\n".NOMAPPLICATION;
$message_admin = _("This message should NOT be sent to the polled people. It is private for the poll's creator.\n\nYou can now modify it at the link above");
$message_admin .= " :\n\n"."%s \n\n" . _("Thanks for your confidence.") . "\n".NOMAPPLICATION;
$message = sprintf($message, Utils::getUrlSondage($poll_id));
$message_admin = sprintf($message_admin, Utils::getUrlSondage($admin_poll_id, true));
if (Utils::isValidEmail($_SESSION['adresse'])) {
Utils::sendEmail( $adminMail, "[".NOMAPPLICATION."][" . _("Author's message") . "] " . _("Poll") . " : ".stripslashes(htmlspecialchars_decode($title,ENT_QUOTES)), $message_admin, $_SESSION['adresse'] );
Utils::sendEmail( $adminMail, "[".NOMAPPLICATION."][" . _("For sending to the polled users") . "] " . _("Poll") . " : ".stripslashes(htmlspecialchars_decode($title,ENT_QUOTES)), $message, $_SESSION['adresse'] );
}
}
error_log(date('H:i:s d/m/Y:') . ' CREATION: '.$poll_id."\t".$format."\t".$adminName."\t".$adminMail."\n", 3, 'admin/logs_studs.txt');
return $admin_poll_id;
}

View File

@ -58,10 +58,10 @@ $erreur_injection_commentaires = false;
#tests
if (!empty($_POST['poursuivre'])){
$_SESSION['form']->titre = $titre;
$_SESSION['form']->nom = $nom;
$_SESSION['form']->adresse = $adresse;
$_SESSION['form']->commentaires = $commentaires;
$_SESSION['form']->title = $titre;
$_SESSION['form']->admin_name = $nom;
$_SESSION['form']->admin_mail = $adresse;
$_SESSION['form']->description = $commentaires;
$_SESSION['form']->editable = ($editable !== null) ? true : false;
$_SESSION['form']->receiveNewVotes = ($receiveNewVotes !== null) ? true : false;
@ -140,7 +140,7 @@ $errors = array(
)
);
if (!$_SESSION['form']->titre && !empty($_POST['poursuivre'])) {
if (!$_SESSION['form']->title && !empty($_POST['poursuivre'])) {
$errors['title']['aria'] = 'aria-describeby="poll_title_error" '; $errors['title']['class'] = ' has-error';
$errors['title']['msg'] = '<div class="alert alert-danger" ><p id="poll_title_error">' . _("Enter a title") . '</p></div>';
} elseif ($erreur_injection_titre) {
@ -153,7 +153,7 @@ if ($erreur_injection_commentaires) {
$errors['description']['msg'] = '<div class="alert alert-danger"><p id="poll_comment_error">' . _("Characters < > and \" are not permitted") . '</p></div>';
}
if (!$_SESSION['form']->nom && !empty($_POST['poursuivre'])) {
if (!$_SESSION['form']->admin_name && !empty($_POST['poursuivre'])) {
$errors['name']['aria'] = 'aria-describeby="poll_name_error" '; $errors['name']['class'] = ' has-error';
$errors['name']['msg'] = '<div class="alert alert-danger"><p id="poll_name_error">' . _("Enter a name") . '</p></div>';
} elseif ($erreur_injection_nom) {
@ -161,7 +161,7 @@ if (!$_SESSION['form']->nom && !empty($_POST['poursuivre'])) {
$errors['name']['msg'] = '<div class="alert alert-danger"><p id="poll_name_error">' . _("Characters < > and \" are not permitted") . '</p></div>';
}
if (!$_SESSION['form']->adresse && !empty($_POST['poursuivre'])) {
if (!$_SESSION['form']->admin_mail && !empty($_POST['poursuivre'])) {
$errors['email']['aria'] = 'aria-describeby="poll_name_error" '; $errors['email']['class'] = ' has-error';
$errors['email']['msg'] = '<div class="alert alert-danger"><p id="poll_email_error">' . _("Enter an email address") . '</p></div>';
} elseif ($erreur_adresse && !empty($_POST['poursuivre'])) {
@ -175,15 +175,15 @@ if (!$_SESSION['form']->adresse && !empty($_POST['poursuivre'])) {
// REMOTE_USER ?
if (USE_REMOTE_USER && isset($_SERVER['REMOTE_USER'])) {
$input_name = '<input type="hidden" name="nom" value="'.$_SESSION['form']->nom.'" />'.stripslashes($_SESSION['form']->nom);
$input_name = '<input type="hidden" name="nom" value="'.$_SESSION['form']->admin_name.'" />'.stripslashes($_SESSION['form']->admin_name);
} else {
$input_name = '<input id="yourname" type="text" name="nom" class="form-control" '.$errors['name']['aria'].' value="'.stripslashes($_SESSION['form']->nom).'" />';
$input_name = '<input id="yourname" type="text" name="nom" class="form-control" '.$errors['name']['aria'].' value="'.stripslashes($_SESSION['form']->admin_name).'" />';
}
if (USE_REMOTE_USER && isset($_SERVER['REMOTE_USER'])) {
$input_email = '<input type="hidden" name="adresse" value="'.$_SESSION['form']->adresse.'">'.$_SESSION['form']->adresse;
$input_email = '<input type="hidden" name="adresse" value="'.$_SESSION['form']->admin_mail.'">'.$_SESSION['form']->admin_mail;
} else {
$input_email = '<input id="email" type="text" name="adresse" class="form-control" '.$errors['email']['aria'].' value="'.$_SESSION['form']->adresse.'" />';
$input_email = '<input id="email" type="text" name="adresse" class="form-control" '.$errors['email']['aria'].' value="'.$_SESSION['form']->admin_mail.'" />';
}
// Checkbox checked ?
@ -208,14 +208,14 @@ echo '
<div class="form-group'.$errors['title']['class'].'">
<label for="poll_title" class="col-sm-4 control-label">' . _("Poll title") . ' *</label>
<div class="col-sm-8">
<input id="poll_title" type="text" name="titre" class="form-control" '.$errors['title']['aria'].' value="'.stripslashes($_SESSION['form']->titre).'" />
<input id="poll_title" type="text" name="titre" class="form-control" '.$errors['title']['aria'].' value="'.stripslashes($_SESSION['form']->title).'" />
</div>
</div>
'.$errors['title']['msg'].'
<div class="form-group'.$errors['description']['class'].'">
<label for="poll_comments" class="col-sm-4 control-label">'. _("Description") .'</label>
<div class="col-sm-8">
<textarea id="poll_comments" name="commentaires" class="form-control" '.$errors['description']['aria'].' rows="5">'.stripslashes($_SESSION['form']->commentaires).'</textarea>
<textarea id="poll_comments" name="commentaires" class="form-control" '.$errors['description']['aria'].' rows="5">'.stripslashes($_SESSION['form']->description).'</textarea>
</div>
</div>
'.$errors['description']['msg'].'

View File

@ -16,6 +16,7 @@
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
use Framadate\Services\LogService;
use Framadate\Services\PollService;
use Framadate\Services\InputService;
use Framadate\Services\MailService;
@ -34,8 +35,8 @@ $editingVoteId = 0;
/* Services */
/*----------*/
$pollService = new PollService($connect);
$logService = new LogService(LOG_FILE);
$pollService = new PollService($connect, $logService);
$inputService = new InputService();
$mailService = new MailService($config['use_smtp']);