Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
PoolController.php
1 <?php
2 
3 /**
4  *
5  **/
6 class PoolController extends CController
7 {
8 
9  /**
10  * This action recieve user answers for pool question
11  *
12  * @return void
13  * @author Me
14  **/
15  public function actionSubmit()
16  {
17  if(!isset($_POST['question'], $_POST['answer'], $_POST['additionalFields'])) {
18  throw new CHttpException(400, 'Bad fields');
19  }
20 
21  if(is_array($_POST['answer'])) {
22  $answers = Answer::model()->findAllByAttributes(array('question_id' => $_POST['question'], 'id' => $_POST['answer']));
23  if(empty($answers)){
24  throw new CHttpException(400);
25  }
26  foreach($answers as $answ)
27  {
28  $userAnswer = new UserAnswer();
29  $userAnswer->answer_id = $answ->id;
30  $userAnswer->user_id = $this->module->getUserId();
31  $userAnswer->additional = $_POST['additionalFields'];
32  if(!$userAnswer->save()) {
33  throw new Exception(print_r($userAnswer->getErrors(), true));
34  }
35  }
36  } else {
37  $answer = Answer::model()->findByAttributes(array('question_id' => $_POST['question'], 'id' => $_POST['answer']));
38  if(empty($answer)){
39  throw new CHttpException(400);
40  }
41  $userAnswer = new UserAnswer();
42  $userAnswer->answer_id = $answer->id;
43  $userAnswer->user_id = $this->module->getUserId();
44  $userAnswer->additional = $_POST['additionalFields'];
45  if(!$userAnswer->save()) {
46  throw new Exception(print_r($userAnswer->getErrors(), true));
47  }
48  }
49  $this->module->setUserVoted($_POST['question']);
50  if(isset($_POST['forwardTo'])) {
51  $this->redirect($_POST['forwardTo']);
52  }
53 
54  }
55 
56  public function actionVoteResults()
57  {
58  if(isset($_GET['options'])){
59  $options = $_GET['options'];
60  $options = unserialize(Yii::app()->securityManager->validateData($options));
61  if($options !== false) {
62  $this->renderpartial('voteResults', array('options' => $options));
63  return;
64  }
65  }
66 
67  throw new CHttpException(400, 'Bad options');
68  }
69 
70  public function actionReset($id)
71  {
72  $this->module->setUserUnvoted($id);
73  if(isset($_SERVER['HTTP_REFERER'])) {
74  $this->redirect($_SERVER['HTTP_REFERER']);
75  }
76  }
77 
78  public function actionDownloadCsv($id)
79  {
80  if(!$this->module->isAdmin()){
81  throw new CHttpException(400, 'You have no permission');
82  };
83  $question = Question::model()
84  ->with(array('answers' => array('condition' => 'active = true', 'with' => array('userAnswersCount'))))
85  ->findByPk($id);
86  $answers = $question->answers;
87  usort($answers, function($a, $b){ return $a->order - $b->order;});
88  $csv = "Question| Answer| Votes\n";
89  $totalAnswers = 0;
90  foreach($answers as $answ) {
91  $csv .= "{$question->text}| {$answ->text}| {$answ->userAnswersCount}\n";
92  }
93  Yii::app()->request->sendFile('report.csv', $csv, null, false);
94  die;
95  }
96 }