Take into account alias visibility setting. (Closes #1136)
[platal.git] / modules / survey.php
index b28aea7..15ee732 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2009 Polytechnique.org                              *
+ *  Copyright (C) 2003-2010 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'),
         );
     }
     // }}}
@@ -109,7 +109,8 @@ class SurveyModule extends PLModule
             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 {
@@ -364,7 +365,7 @@ class SurveyModule extends PLModule
     function handler_ajax(&$page, $type)
     {
         $this->load('survey.inc.php');
-        header('Content-Type: text/html; charset="UTF-8"');
+        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,18 +397,34 @@ 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)
     {
         $this->load('survey.inc.php');
-        if (!$survey->isMode(Survey::MODE_ALL)) { // if the survey is reserved to alumni
-            if (!S::logged()) {
-                return false;
-            }
-            if (!$survey->checkPromo(S::v('promo'))) { // checks promotion
-                $page->kill("Tu n'as pas accès à ce sondage car il est réservé à d'autres promotions.");
+        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;
             }
         }
-        return true;
+        if ($allowed) {
+            return true;
+        }
+        if (!$silent) {
+            $page->kill("Tu n'as pas accès à ce sondage car il est réservé à d'autres promotions.");
+        }
+        return false;
     }
     // }}}