Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
VoteModule.php
1 <?php
2 
3 /**
4  * Gentics Portal.Node PHP
5  * Author & Copyright (c) by Gentics Software GmbH
6  * sales@gentics.com
7  * http://www.gentics.com
8  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
9  * You must not use this software without a valid license agreement.
10  *
11  * VoteModule
12  * This module represent voting system which manage by questions and answers and return statistics of answers
13  *
14  *
15 **/
16 class VoteModule extends CWebModule
17 {
18  public $debug = false;
19  public $_assetsUrl;
20 
21  public function getLoginUrl()
22  {
23  return Yii::app()->getModule('user')->loginUrl;
24  }
25 
26  public function init()
27  {
28  $this->setImport(array(
29  'vote.models.*'
30  ));
31  }
32 
33  /**
34  * Translates a message to the specified language.
35  *
36  * @param string $str message
37  * @param array $params params
38  * @param string $dic dictionary
39  *
40  * @return string
41  */
42  public static function t($str = '', $params = array(), $dic = 'core')
43  {
44  return Yii::t("VoteModule." . $dic, $str, $params);
45  }
46 
47  /**
48  * Get current user id
49  *
50  * @return string
51  **/
52  public function getUserId()
53  {
54  return Yii::app()->user->isGuest ? Yii::app()->session->getSessionId() : Yii::app()->user->id;
55  }
56 
57  /**
58  * Checks if user already answered for question
59  *
60  * @return boolean
61  **/
62  public function isUserVoted($questionId, $userId = null)
63  {
64  if($userId == null) {
65  $userId = $this->getUserId();
66  }
67  return Yii::app()->user->getState('vote-voted-'.$questionId) === true;
68  }
69 
70  /**
71  * Set status of question as answered
72  *
73  * @return void
74  **/
75  public function setUserVoted($questionId, $userId = null)
76  {
77  if($userId == null) {
78  $userId = $this->getUserId();
79  }
80  Yii::app()->user->setState('vote-voted-'.$questionId, true);
81  }
82 
83  /**
84  * Unset status of question as answered
85  *
86  * @return void
87  **/
88  public function setUserUnvoted($questionId, $userId = null)
89  {
90  if($userId == null) {
91  $userId = $this->getUserId();
92  }
93  Yii::app()->user->setState('vote-voted-'.$questionId, false);
94  }
95 
96  public function isAdmin()
97  {
98  return true;
99  }
100 
101  /**
102  * Publish module assets and return url
103  *
104  * @return string
105  */
106  public function getAssetsUrl()
107  {
108  if (!$this->_assetsUrl) {
109  $this->_assetsUrl = Yii::app()->assetManager->publish(Yii::getPathOfAlias('vote.assets'), true, -1, $this->debug);
110  }
111  return $this->_assetsUrl;
112  }
113 }