Happy New Year!
[platal.git] / modules / survey.php
index 73a892a..3c53625 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -25,15 +25,15 @@ class SurveyModule extends PLModule
     function handlers()
     {
         return array(
-            'survey'              => $this->make_hook('index', AUTH_PUBLIC),
-            'survey/vote'         => $this->make_hook('vote', AUTH_PUBLIC),
-            'survey/result'       => $this->make_hook('result', AUTH_PUBLIC),
-            'survey/edit'         => $this->make_hook('edit', AUTH_COOKIE),
-            'survey/ajax'         => $this->make_hook('ajax', AUTH_COOKIE),
-            'survey/admin'        => $this->make_hook('admin', AUTH_MDP, 'admin'),
-            'survey/admin/edit'   => $this->make_hook('adminEdit', AUTH_MDP, 'admin'),
+            'survey'              => $this->make_hook('index',         AUTH_PUBLIC),
+            'survey/vote'         => $this->make_hook('vote',          AUTH_PUBLIC),
+            'survey/result'       => $this->make_hook('result',        AUTH_PUBLIC),
+            'survey/edit'         => $this->make_hook('edit',          AUTH_COOKIE),
+            'survey/ajax'         => $this->make_hook('ajax',          AUTH_COOKIE),
+            'survey/admin'        => $this->make_hook('admin',         AUTH_MDP, 'admin'),
+            'survey/admin/edit'   => $this->make_hook('adminEdit',     AUTH_MDP, 'admin'),
             'survey/admin/valid'  => $this->make_hook('adminValidate', AUTH_MDP, 'admin'),
-            'survey/admin/del'    => $this->make_hook('adminDelete', AUTH_MDP, 'admin'),
+            'survey/admin/del'    => $this->make_hook('adminDelete',   AUTH_MDP, 'admin'),
         );
     }
     // }}}
@@ -41,7 +41,7 @@ class SurveyModule extends PLModule
     // {{{ function handler_index() : lists all available surveys
     function handler_index(&$page, $action = null)
     {
-        require_once dirname(__FILE__).'/survey/survey.inc.php';
+        $this->load('survey.inc.php');
         $page->changeTpl('survey/index.tpl');
         $page->assign('survey_current', Survey::retrieveList('c'));
         $page->assign('survey_old', Survey::retrieveList('o'));
@@ -59,7 +59,7 @@ class SurveyModule extends PLModule
         if ($id == -1) {
             return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey');
         }
-        require_once dirname(__FILE__).'/survey/survey.inc.php';
+        $this->load('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.", 'survey');
@@ -67,9 +67,13 @@ class SurveyModule extends PLModule
             return $this->show_error($page, "Le sondage ".$survey->getTitle()." est terminé.", 'survey');
         }
         if (!$this->check_surveyPerms($page, $survey)) {
-            return;
+            return PL_DO_AUTH;
         }
         if (Post::has('survey_submit')) { // checks if the survey has already been filled in
+            // admins can see the survey but not vote
+            if (!$this->check_surveyPerms($page, $survey, false, false)) {
+                return PL_DO_AUTH;
+            }
             $uid = 0;
             if (!$survey->isMode(Survey::MODE_ALL)) { // if survey is restriced to alumni
                 $uid = S::v('uid');
@@ -98,18 +102,19 @@ class SurveyModule extends PLModule
         if ($id == -1) {
             return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey');
         }
-        require_once dirname(__FILE__).'/survey/survey.inc.php';
+        $this->load('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.", 'survey');
-        } elseif (!$survey->isEnded()) {
+        } elseif (!$survey->isEnded() && !$survey->canSeeEarlyResults(S::user())) {
             return $this->show_error($page, "Le sondage ".$survey->getTitle()." n'est pas encore terminé.", 'survey');
         }
-        if (!$this->check_surveyPerms($page, $survey)) {
-            return;
+        if (!$survey->canSeeEarlyResults(S::user()) && !$this->check_surveyPerms($page, $survey)) {
+            return PL_DO_AUTH;
         }
         if ($show == 'csv') {
-            header('Content-Type: text/csv; charset="UTF-8"');
+            pl_content_headers("text/csv");
+            header('Content-Disposition: attachment; filename="'.addslashes($survey->getTitle()).'.csv"');
             echo $survey->toCSV();
             exit;
         } else {
@@ -122,7 +127,7 @@ class SurveyModule extends PLModule
     // {{{ function handler_admin() : index of admin mode
     function handler_admin(&$page, $id = -1)
     {
-        require_once dirname(__FILE__).'/survey/survey.inc.php';
+        $this->load('survey.inc.php');
         $this->clear_session();
         if ($id == -1) {
             $page->changeTpl('survey/admin.tpl');
@@ -148,7 +153,7 @@ class SurveyModule extends PLModule
         if ($id == -1 || ($id == 'req' && $req == -1)) {
             return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey/admin');
         }
-        require_once dirname(__FILE__).'/survey/survey.inc.php';
+        $this->load('survey.inc.php');
         $this->clear_session(); // cleans session (in case there would have been a problem before)
         if ($id == 'req') {
             $survey = Survey::retrieveSurveyReq($req);
@@ -180,7 +185,7 @@ class SurveyModule extends PLModule
             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';
+        $this->load('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.", 'survey/admin');
@@ -209,7 +214,7 @@ class SurveyModule extends PLModule
             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';
+        $this->load('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.", 'survey/admin');
@@ -229,7 +234,7 @@ class SurveyModule extends PLModule
     // {{{ function handler_edit() : edits a survey (in normal mode unless called by handler_adminEdit() )
     function handler_edit(&$page, $action = 'show', $qid = 'root')
     {
-        require_once dirname(__FILE__).'/survey/survey.inc.php';
+        $this->load('survey.inc.php');
         $action = Post::v('survey_action', $action);
         $qid    = Post::v('survey_qid', $qid);
         if (Post::has('survey_cancel')) { // after cancelling changes, shows the survey
@@ -331,7 +336,7 @@ class SurveyModule extends PLModule
                     $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'));
+                        $this->show_confirm($page, "Veuillez confirmer l'enregistrement des modifications apportées à ce sondage.", 'edit', array('action' => 'valid'));
                     } else {
                         $this->show_confirm($page, "Veuillez confirmer l'envoi de cette proposition de sondage.", 'edit', array('action' => 'valid'));
                     }
@@ -363,8 +368,8 @@ class SurveyModule extends PLModule
     // {{{ function handler_ajax() : some ajax in editing a new question (for now, there may be a little more later)
     function handler_ajax(&$page, $type)
     {
-        require_once dirname(__FILE__).'/survey/survey.inc.php';
-        header('Content-Type: text/html; charset="UTF-8"');
+        $this->load('survey.inc.php');
+        pl_content_headers("text/html");
         if (Survey::isType($type)) { // when type has been chosen, the form is updated to fit exactly the type of question chosen
             $page->changeTpl('survey/edit_new.tpl', NO_SKIN);
             $page->assign('survey_types', Survey::getTypes());
@@ -396,21 +401,40 @@ class SurveyModule extends PLModule
     // }}}
 
     // {{{ function check_surveyPerms() : checks the particular surveys access permissions
-    function check_surveyPerms(&$page, $survey)
+    function check_surveyPerms(&$page, $survey, $silent = false, $admin_allowed = true)
     {
-        require_once dirname(__FILE__).'/survey/survey.inc.php';
-        if (!$survey->isMode(Survey::MODE_ALL)) { // if the survey is reserved to alumni
-            global $globals;
-            if (!call_user_func(array($globals->session, 'doAuth'))) { // checks authentification
-                global $platal;
-                $platal->force_login($page);
+        $this->load('survey.inc.php');
+        if ($survey->isMode(Survey::MODE_ALL)) { // if the survey is not reserved to alumni
+            return true;
+        }
+        if (!S::logged()) {
+            return false;
+        }
+        $profile = S::user()->profile();
+        if (!$profile) {
+            return false;
+        }
+        // checks promotion
+        $allowed = false;
+        foreach ($profile->yearspromo() as $p) {
+            if ($survey->checkPromo($p)) {
+                $allowed = true;
+                break;
             }
-            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.", 'survey');
-                return false;
+        }
+        if ($allowed) {
+            return true;
+        }
+        if (S::admin() && $admin_allowed) {
+            if (!$silent) {
+                $page->trigWarning('Tu as accès à ce sondage car tu es administrateur du site.');
             }
+            return true;
         }
-        return true;
+        if (!$silent) {
+            $page->kill("Tu n'as pas accès à ce sondage car il est réservé à d'autres promotions.");
+        }
+        return false;
     }
     // }}}
 
@@ -462,6 +486,7 @@ class SurveyModule extends PLModule
         if (!is_null($errArray)) {
             $page->assign('survey_errors', $errArray);
         }
+
     }
     // }}}