Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
PoolWidget.php
1 <?php
2 
3 /**
4  * Widget for pooling questions. Show question and available answers.
5  *
6  **/
7 class PoolWidget extends CWidget
8 {
9  /**
10  * Unique question identification string
11  *
12  * @var string
13  **/
14  public $id;
15 
16  /**
17  * Id of author of the question
18  *
19  * @var string
20  **/
21  public $author_id;
22  /**
23  * Question
24  *
25  * @var string
26  **/
27  public $question;
28 
29  /**
30  * Array of answers with options.
31  * Format: 'key1' => array(
32  * array(
33  * 'order' => 1, //optional
34  * 'text' => 'Yes I do',
35  * ),
36  * 'key2' => array(
37  * 'order' => 2, //optional
38  * 'text' => 'No I dont',
39  * )
40  * )
41  *
42  * @var array
43  **/
44  public $answers;
45 
46  /**
47  * If allowed to selectmultiply answers
48  *
49  * @var boolean
50  **/
51  public $multiply = false;
52 
53  /**
54  * Defines a default page, where the user will be forwarded if he clicks the “save”-button (default: same page)
55  *
56  * @var string
57  **/
58  public $forwardTo;
59 
60  /**
61  * Give the ability to hide/display a “show results”-link without giving an answer
62  *
63  * @var string
64  **/
65  public $resultsLink;
66 
67  /**
68  * If show link to voting results without vouting
69  *
70  * @public boolean
71  **/
72  public $showResultsLink = false;
73  /**
74  * If single-answer-selection: don’t display a “save”-button; save on click on answer
75  *
76  * @var boolean
77  **/
78  public $dynamic;
79 
80  /**
81  * Defines if anonymous answers can be given
82  *
83  * @var boolean
84  **/
85  public $allowAnonymous;
86 
87  /**
88  * Array of variables and values, that should be added to the form and saved with the given answer
89  *
90  * @var array
91  **/
92  public $additionalFields = array();
93 
94  /**
95  * Submit form via ajax
96  *
97  * @public string
98  **/
99  public $ajax;
100 
101  /**
102  * If show widget for voted user
103  *
104  * @public boolean
105  **/
106  public $votedShow = true;
107 
108  /**
109  * Here stored ResultsWidget options. They are used for ajax reloading
110  *
111  * @public string
112  **/
114 
115  public function init()
116  {
117  if($this->forwardTo === null){
118  $this->forwardTo = Yii::app()->request->requestUri;
119  }
120  }
121 
122  public function run()
123  {
124  if(!$this->votedShow && Yii::app()->getModule('vote')->isUserVoted($this->id)) {
125  $this->render('PoolWidget_hidden');
126  return;
127  }
128  //find question with answers by id
129  $question = Question::model()->with('answers')->findByPk($this->id);
130  //create or update question if needs
131  if(!$question) {
132  $question = new Question();
133  $question->id = $this->id;
134  $question->text = $this->question;
135  $question->author_id = $this->author_id;
136  if(!$question->save()){
137  throw new Exception(print_r($question->getErrors(),true));
138  }
139  } elseif($question->text != $this->question || $question->author_id != $this->author_id) {
140  $question->text = $this->question;
141  $question->author_id = $this->author_id;
142  $question->update(array('text', 'author_id'));
143  }
144  //create or update answers if needs
145  $answerByKey = array();
146  foreach ($question->answers as $answ) {
147  $answerByKey[$answ->answer_key] = $answ;
148  }
149  $activeAnswers = array();
150  foreach ($this->answers as $answerKey => $answer) {
151  if(!is_string($answerKey)) {
152  throw new Exception(VoteModule::t('Answer id should be string type. Answer: "{answer}". Id: {id}', array('{answer}' => $answer['text'], '{id}' => $answerKey)));
153  }
154  if(isset($answerByKey[$answerKey])) {
155  $existedAnswer = $answerByKey[$answerKey];
156  unset($answerByKey[$answerKey]);
157  if($existedAnswer->active != 1 || $existedAnswer->order != $answer['order'] || $existedAnswer->text != $answer['text']) {
158  $existedAnswer->active = 1;
159  $existedAnswer->order = $answer['order'];
160  $existedAnswer->text = $answer['text'];
161  $existedAnswer->update(array('active', 'order', 'text'));
162  }
163  $activeAnswers[] = $existedAnswer;
164  } else {
165  $newAnswer = new Answer();
166  $newAnswer->question_id = $question->id;
167  $newAnswer->answer_key = $answerKey;
168  $newAnswer->order = $answer['order'];
169  $newAnswer->text = $answer['text'];
170  if(!$newAnswer->save()){
171  throw new Exception(print_r($newAnswer->getErrors(),true));
172  }
173  $activeAnswers[] = $newAnswer;
174  }
175  }
176  //Make not used answers not active
177  foreach ($answerByKey as $answer) {
178  if ($answer->active != 0) {
179  $answer->active = 0;
180  $answer->update(array('active'));
181  }
182  }
183  usort($activeAnswers, function($a, $b){ return $a->order - $b->order;});
184  $answersData = array();
185  foreach ($activeAnswers as $answ) {
186  $answersData[$answ->id] = $answ->text;
187  }
188  $this->render('PoolWidget', array('question' => $question, 'answers' => $answersData));
189  }
190 
191  public function getSecureResultOptions()
192  {
193  return Yii::app()->securityManager->hashData(serialize($this->resultOptions));
194  }
195 }