--- /dev/null
+<?php
+/***************************************************************************
+ * Copyright (C) 2003-2007 Polytechnique.org *
+ * http://opensource.polytechnique.org/ *
+ * *
+ * 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 2 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, write to the Free Software *
+ * Foundation, Inc., *
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
+ ***************************************************************************/
+
+// {{{ class SurveyReq
+
+class SurveyReq extends Validate
+{
+ // {{{ properties
+ public $title;
+ public $description;
+ public $end;
+ public $mode;
+ public $promos;
+ public $questions;
+ // }}}
+ // {{{ constructor
+
+ public function __construct($_title, $_description, $_end, $_mode, $_promos, $_questions, $_uid)
+ {
+ parent::__construct($_uid, false, 'surveys');
+ $this->title = $_title;
+ $this->description = $_description;
+ $this->end = $_end;
+ $this->mode = $_mode;
+ $this->promos = $_promos;
+ $this->questions = $_questions;
+ }
+
+ // }}}
+ // {{{ function formu()
+
+ public function formu()
+ {
+ return 'include/form.valid.surveys.tpl';
+ }
+
+ // }}}
+ // {{{ function _mail_subj
+
+ protected function _mail_subj()
+ {
+ return "[Polytechnique.org/SONDAGES] Proposition de sondage";
+ }
+
+ // }}}
+ // {{{ function _mail_body
+
+ protected function _mail_body($isok)
+ {
+ if ($isok) {
+ return " Le sondage que tu avais proposé vient d'être validé.";
+ } else {
+ return " Le sondage que tu avais proposé a été refusé.";
+ }
+ }
+
+ // }}}
+ // {{{ function updateReq()
+
+ public function updateReq($_title, $_description, $_end, $_mode, $_promos, $_questions)
+ {
+ $this->title = $_title;
+ $this->description = $_description;
+ $this->end = $_end;
+ $this->mode = $_mode;
+ $this->promos = $_promos;
+ $this->questions = $_questions;
+ return $this->update();
+ }
+
+ // }}}
+ // {{{ function commit()
+
+ public function commit()
+ {
+ $sql = 'INSERT INTO survey_surveys
+ SET questions={?},
+ title={?},
+ description={?},
+ author_id={?},
+ end={?},
+ mode={?},
+ promos={?}';
+ return XDB::execute($sql, serialize($this->questions), $this->title, $this->description, $this->uid, $this->end, $this->mode, $this->promos);
+ }
+
+ // }}}
+}
+
+// }}}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>
}
$id = intval($id);
if ($id == -1) {
- return $this->show_error($page, "Un identifiant de sondage doit être précisé.", '');
+ return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey');
}
require_once dirname(__FILE__).'/survey/survey.inc.php';
$survey = Survey::retrieveSurvey($id); // retrieves the survey object structure
if ($survey == null || !$survey->isValid()) {
- return $this->show_error($page, "Sondage ".$id." introuvable.", '');
+ return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey');
} elseif ($survey->isEnded()) {
- return $this->show_error($page, "Le sondage ".$survey->getTitle()." est terminé.");
+ return $this->show_error($page, "Le sondage ".$survey->getTitle()." est terminé.", 'survey');
}
if (!$this->check_surveyPerms($page, $survey)) {
return;
if (!$survey->isMode(Survey::MODE_ALL)) { // if survey is restriced to alumni
$uid = S::v('uid');
if ($survey->hasVoted($uid)) { // checks whether the user has already voted
- return $this->show_error($page, "Tu as déjà voté à ce sondage.");
+ return $this->show_error($page, "Tu as déjà voté à ce sondage.", 'survey');
}
}
$survey->vote($uid, Post::v('survey'.$id)); // performs vote
- $this->show_success($page, "Ta réponse a bien été prise en compte. Merci d'avoir participé à ce sondage.", '');
+ $this->show_success($page, "Ta réponse a bien été prise en compte. Merci d'avoir participé à ce sondage.", 'survey');
} else { // offers to fill in the survey
if ($survey->isMode(Survey::MODE_ALL) || !$survey->hasVoted(S::v('uid'))) {
$page->assign('survey_votemode', true);
{
$id = intval($id);
if ($id == -1) {
- return $this->show_error($page, "Un identifiant de sondage doit être précisé.", '');
+ return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey');
}
require_once dirname(__FILE__).'/survey/survey.inc.php';
$survey = Survey::retrieveSurvey($id); // retrieves the survey object structure
if ($survey == null || !$survey->isValid()) {
- return $this->show_error($page, "Sondage ".$id." introuvable.", '');
+ return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey');
} elseif (!$survey->isEnded()) {
- return $this->show_error($page, "Le sondage ".$survey->getTitle()." n'est pas encore terminé.");
+ return $this->show_error($page, "Le sondage ".$survey->getTitle()." n'est pas encore terminé.", 'survey');
}
if (!$this->check_surveyPerms($page, $survey)) {
return;
$id = intval($id);
$survey = Survey::retrieveSurvey($id); // retrieves all survey object structure
if ($survey == null) {
- $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
+ $this->show_error($page, "Sondage ".$id." introuvable.", 'survey/admin');
}
$page->assign('survey_adminmode', true);
$this->show_survey($page, $survey);
// }}}
// {{{ function handler_adminEdit() : edits a survey in admin mode
- function handler_adminEdit(&$page, $id = -1)
+ function handler_adminEdit(&$page, $id = -1, $req = -1)
{
- if ($id == -1) {
- return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin');
+ if ($id == -1 || ($id == 'req' && $req == -1)) {
+ return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey/admin');
}
- $id = intval($id);
require_once dirname(__FILE__).'/survey/survey.inc.php';
- $survey = Survey::retrieveSurvey($id); // retrieves the survey in database
$this->clear_session(); // cleans session (in case there would have been a problem before)
- $this->store_session($survey, $id);
+ if ($id == 'req') {
+ $survey = Survey::retrieveSurveyReq($req);
+ if ($survey == null) {
+ return $this->show_error($page, "Sondage introuvable.", 'survey/admin');
+ }
+ $this->store_session($survey, $req, true);
+ } else {
+ $id = intval($id);
+ $survey = Survey::retrieveSurvey($id); // retrieves the survey in database
+ if ($survey == null) {
+ return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey/admin');
+ }
+ $this->store_session($survey, $id);
+ }
$this->handler_edit($page, 'show'); // calls handler_edit, but in admin mode since 'survey_id' is in session
}
// }}}
return $this->handler_admin(&$page, $id);
}
if ($id == -1) {
- return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin');
+ return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey/admin');
}
$id = intval($id);
require_once dirname(__FILE__).'/survey/survey.inc.php';
$surveyInfo = Survey::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure)
if ($surveyInfo == null) {
- return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
+ return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey/admin');
}
if (Post::has('survey_submit')) { // needs a confirmation before validation
if (Survey::validateSurvey($id)) { // validates the survey (in the database)
- $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été validé, les votes sont maintenant ouverts.", 'admin');
+ $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été validé, les votes sont maintenant ouverts.", 'survey/admin');
} else {
- $this->show_error($page, '', 'admin');
+ $this->show_error($page, '', 'survey/admin');
}
} else { // asks for a confirmation
$this->show_confirm($page, "Êtes-vous certain de vouloir valider le sondage \"".$surveyInfo['title']."\" ? "
return $this->handler_admin(&$page, $id);
}
if ($id == -1) {
- return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin');
+ return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey/admin');
}
$id = intval($id);
require_once dirname(__FILE__).'/survey/survey.inc.php';
$surveyInfo = Survey::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure)
if ($surveyInfo == null) {
- return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
+ return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey/admin');
}
if (Post::has('survey_submit')) { // needs a confirmation before suppression
if (Survey::deleteSurvey($id)) { // deletes survey in database
- $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été supprimé, ainsi que tous les votes le concernant.", 'admin');
+ $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été supprimé, ainsi que tous les votes le concernant.", 'survey/admin');
} else {
- $this->show_error($page, '', 'admin');
+ $this->show_error($page, '', 'survey/admin');
}
} else { // asks for a confirmation
$this->show_confirm($page, "Êtes-vous certain de vouloir supprimer le sondage \"".$surveyInfo['title']."\" ?", 'admin/del', array('id' => $id));
$survey = unserialize(S::v('survey'));
$args = Post::v('survey_question');
if (!$survey->editQuestion($qid, $args)) { // update the survey object structure
- return $this->show_error($page, '', 'edit');
+ return $this->show_error($page, '', 'survey/edit');
}
$this->show_survey($page, $survey);
$this->store_session($survey);
$survey = unserialize(S::v('survey'));
$current = $survey->toArray($qid); // gets the current parameters of the question
if ($current == null) {
- return $this->show_error($page, '', 'edit');
+ return $this->show_error($page, '', 'survey/edit');
}
$this->show_form($page, $action, $qid, $current['type'], $current);
} // }}}
if (Post::has('survey_submit')) { // if the form has been submitted, adds the question
$survey = unserialize(S::v('survey'));
if (!$survey->addQuestion($qid, $survey->factory(Post::v('survey_type'), Post::v('survey_question')))) {
- return $this->show_error($page, '', 'edit');
+ return $this->show_error($page, '', 'survey/edit');
}
$this->show_survey($page, $survey);
$this->store_session($survey);
if (Post::has('survey_submit')) { // if a confirmation has been sent, deletes the question
$survey = unserialize(S::v('survey'));
if (!$survey->delQuestion(Post::v('survey_qid'))) { // deletes the node in the survey object structure
- return $this->show_error($page, '', 'edit');
+ return $this->show_error($page, '', 'survey/edit');
}
$this->show_survey($page, $survey);
$this->store_session($survey);
$survey = unserialize(S::v('survey'));
$current = $survey->toArray($qid); // needed to get the title of the question to delete (more user-friendly than an id)
if ($current == null) {
- return $this->show_error($page, '', 'edit');
+ return $this->show_error($page, '', 'survey/edit');
}
$this->show_confirm($page, 'Êtes-vous certain de vouloir supprimer la question intitulé "'.$current['question'].'" ? '
.'Attention, cela supprimera en même temps toutes les questions qui dépendent de celle-ci.',
if (Post::has('survey_submit')) { // needs a confirmation before storing the proposition
$survey = unserialize(S::v('survey'));
if (S::has('survey_id')) { // if 'survey_id' is in session, we are modifying an existing survey (in admin mode) instead of proposing a new one
+ $link = (S::has('survey_validate'))? 'admin/validate' : 'survey/admin';
if ($survey->updateSurvey()) { // updates the database according the new survey object structure
- $this->show_success($page, "Les modifications sur le sondage ont bien été enregistrées.", 'admin');
+ $this->show_success($page, "Les modifications sur le sondage ont bien été enregistrées.", $link);
} else {
- $this->show_error($page, '', 'admin');
+ $this->show_error($page, '', $link);
}
} else { // if no 'survey_id' is in session, we are indeed proposing a new survey
if ($survey->proposeSurvey()) { // stores the survey object structure in database
$this->show_success($page, "Votre proposition de sondage a bien été enregistrée,
- elle est en attent de validation par un administrateur du site.", '');
+ elle est en attent de validation par un administrateur du site.", 'survey');
} else {
- $this->show_error($page);
+ $this->show_error($page, '', 'survey');
}
}
$this->clear_session();
$survey = unserialize(S::v('survey'));
$errors = $survey->checkSyntax();
if (!is_null($errors)) {
- $this->show_error($page, "", 'edit', $errors);
+ $this->show_error($page, "", 'survey/edit', $errors);
} else {
if (S::has('survey_id')) {
$this->show_confirm($page, "Veuillez confirmer l'enregistrement des modifications apportées à ce sondage", 'edit', array('action' => 'valid'));
} // }}}
} elseif ($action == 'cancel') { // {{{ cancels the creation/modification of a survey
if (Post::has('survey_submit')) { // needs a confirmation
- if (S::has('survey_id')) { // only possible when modifying a survey in admin mode, still this should be considered again,
- $this->clear_session(); // maybe some name with "admin" in it, "survey_adminid" or anything that might not be confusing.
- return $this->handler_admin($page); // in this case, shows the admin index
+ if (S::has('survey_id')) { // only possible when modifying a survey in admin mode
+ if (S::has('survey_validate')) { // if a link has been supplied, uses it
+ $this->clear_session();
+ return $this->show_success($page, "Les modifications effectuées ont été annulées", 'admin/validate');
+ } else { // else shows the admin index
+ $this->clear_session();
+ return $this->handler_admin($page);
+ }
} else {
- $this->clear_session(); // cleans session
+ $this->clear_session();
return $this->handler_index($page); // else shows the 'normal' index
}
} else { // asks for a confirmation if it has not been sent
$this->show_confirm(&$page, "Êtes-vous certain de vouloir annuler totalement l'édition de ce sondage ? Attention, "
- ."toutes les données éditées jusque lâ seront définitivement perdues.",
+ ."toutes les données éditées jusque là seront définitivement perdues.",
'edit', array('action' => $action));
}
} // }}}
{
S::kill('survey');
S::kill('survey_id');
+ S::kill('survey_validate');
}
// }}}
// {{{ function store_session() : serializes and stores survey (and survey_id) in session
- function store_session($survey, $survey_id = -1)
+ function store_session($survey, $survey_id = -1, $survey_validate = false)
{
$_SESSION['survey'] = serialize($survey);
if ($survey_id != -1) {
$_SESSION['survey_id'] = $survey_id;
}
+ if ($survey_validate) {
+ $_SESSION['survey_validate'] = true;
+ }
}
// }}}
$platal->force_login($page);
}
if (!$survey->checkPromo(S::v('promo'))) { // checks promotion
- $this->show_error($page, "Tu n'as pas accès à ce sondage car il est réservé à d'autres promotions.");
+ $this->show_error($page, "Tu n'as pas accès à ce sondage car il est réservé à d'autres promotions.", 'survey');
return false;
}
}
{
$page->changeTpl('survey/error.tpl');
$page->assign('survey_message', $message);
- $page->assign('survey_link', './survey/'.$link); // 'return' link to let the user leave the page
+ $page->assign('survey_link', $link); // 'return' link to let the user leave the page
if (!is_null($errArray)) {
$page->assign('survey_errors', $errArray);
}
{
$page->changeTpl('survey/success.tpl');
$page->assign('survey_message', $message);
- $page->assign('survey_link', './survey/'.$link); // 'return' link to let the user leave the page
+ $page->assign('survey_link', $link); // 'return' link to let the user leave the page
}
// }}}
}
public static function retrieveList($type, $tpl = true)
{
switch ($type) {
- case 'w':
- case 'waiting' :
- $where = 'valid=0';
- break;
case 'c':
case 'current':
- $where = 'valid=1 AND end > NOW()';
+ $where = 'end > NOW()';
break;
case 'o':
case 'old':
- $where = 'valid=1 AND end <= NOW()';
+ $where = 'end <= NOW()';
break;
default:
return null;
// {{{ static function retrieveSurvey() : gets a survey in database (and unserialize the survey object structure)
public static function retrieveSurvey($sid)
{
- $sql = 'SELECT questions, title, description, end, mode, promos, valid
+ $sql = 'SELECT questions, title, description, end, mode, promos
FROM survey_surveys
WHERE id={?}';
$res = XDB::query($sql, $sid);
if (is_null($data) || !is_array($data)) {
return null;
}
- $survey = new Survey($data, $sid, (boolean) $data['valid'], unserialize($data['questions']));
+ $survey = new Survey($data, $sid, true, unserialize($data['questions']));
return $survey;
}
// }}}
// {{{ static function retrieveSurveyInfo() : gets information about a survey (title, description, end date, restrictions) but does not unserialize the survey object structure
public static function retrieveSurveyInfo($sid)
{
- $sql = 'SELECT title, description, end, mode, promos, valid
+ $sql = 'SELECT title, description, end, mode, promos
FROM survey_surveys
WHERE id={?}';
$res = XDB::query($sql, $sid);
}
// }}}
+ // {{{ static function retrieveSurveyReq() : gets a survey request to validate
+ public static function retrieveSurveyReq($id)
+ {
+ require_once 'validations.inc.php';
+ $surveyreq = Validate::get_request_by_id($id);
+ if ($surveyreq == null) {
+ return null;
+ }
+ $data = array('title' => $surveyreq->title,
+ 'description' => $surveyreq->description,
+ 'end' => $surveyreq->end,
+ 'mode' => $surveyreq->mode,
+ 'promos' => $surveyreq->promos);
+ $survey = new Survey($data, $id, false, $surveyreq->questions);
+ return $survey;
+ }
+ // }}}
+
// {{{ function proposeSurvey() : stores a proposition of survey in database (before validation)
public function proposeSurvey()
{
- $sql = 'INSERT INTO survey_surveys
- SET questions={?},
- title={?},
- description={?},
- author_id={?},
- end={?},
- mode={?},
- promos={?},
- valid=0;';
- return XDB::execute($sql, serialize($this->questions), $this->title, $this->description, S::v('uid'), $this->end, $this->mode, $this->promos);
+ require_once 'validations.inc.php';
+ $surveyreq = new SurveyReq($this->title, $this->description, $this->end, $this->mode, $this->promos, $this->questions, S::v('uid'));
+ return $surveyreq->submit();
}
// }}}
// {{{ function updateSurvey() : updates a survey in database (before validation)
public function updateSurvey()
{
- if ($this->id == -1) {
- return false;
+ if ($this->valid) {
+ $sql = 'UPDATE survey_surveys
+ SET questions={?},
+ title={?},
+ description={?},
+ end={?},
+ mode={?},
+ promos={?}
+ WHERE id={?};';
+ return XDB::execute($sql, serialize($this->questions), $this->title, $this->description, $this->end, $this->mode, $this->promos, $this->id);
+ } else {
+ require_once 'validations.inc.php';
+ $surveyreq = Validate::get_request_by_id($this->id);
+ if ($surveyreq == null) {
+ return false;
+ }
+ return $surveyreq->updateReq($this->title, $this->description, $this->end, $this->mode, $this->promos, $this->questions);
}
- $sql = 'UPDATE survey_surveys
- SET questions={?},
- title={?},
- description={?},
- end={?},
- mode={?},
- promos={?}
- WHERE id={?};';
- return XDB::execute($sql, serialize($this->questions), $this->title, $this->description, $this->end, $this->mode, $this->promos, $this->id);
- }
- // }}}
-
- // {{{ static function validateSurvey() : validates a survey
- public static function validateSurvey($sid)
- {
- $sql = 'UPDATE survey_surveys
- SET valid=1
- WHERE id={?};';
- return XDB::execute($sql, $sid);
}
// }}}