1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Poll
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Poll block
29: *
30: * @file Poll.php
31: * @author Magento Core Team <core@magentocommerce.com>
32: */
33:
34: class Mage_Poll_Block_ActivePoll extends Mage_Core_Block_Template
35: {
36: /**
37: * Poll templates
38: *
39: * @var array
40: */
41: protected $_templates;
42:
43: /**
44: * Current Poll Id
45: *
46: * @var int
47: */
48: protected $_pollId = null;
49:
50: /**
51: * Already voted by current visitor Poll Ids array
52: *
53: * @var array|null
54: */
55: protected $_votedIds = null;
56:
57: /**
58: * Poll model
59: *
60: * @var Mage_Poll_Model_Poll
61: */
62: protected $_pollModel;
63:
64: public function __construct()
65: {
66: parent::__construct();
67: $this->_pollModel = Mage::getModel('poll/poll');
68: }
69:
70: /**
71: * Set current Poll Id
72: *
73: * @param int $pollId
74: * @return Mage_Poll_Block_ActivePoll
75: */
76: public function setPollId($pollId)
77: {
78: $this->_pollId = $pollId;
79: return $this;
80: }
81:
82: /**
83: * Get current Poll Id
84: *
85: * @return int|null
86: */
87: public function getPollId()
88: {
89: return $this->_pollId;
90: }
91:
92: /**
93: * Retrieve already voted Poll Ids
94: *
95: * @return array|null
96: */
97: public function getVotedPollsIds()
98: {
99: if ($this->_votedIds === null) {
100: $this->_votedIds = $this->_pollModel->getVotedPollsIds();
101: }
102: return $this->_votedIds;
103: }
104:
105: /**
106: * Get Ids of all active Polls
107: *
108: * @return array
109: */
110: public function getActivePollsIds()
111: {
112: return $this->_pollModel
113: ->setExcludeFilter($this->getVotedPollsIds())
114: ->setStoreFilter(Mage::app()->getStore()->getId())
115: ->getAllIds();
116: }
117:
118: /**
119: * Get Poll Id to show
120: *
121: * @return int
122: */
123: public function getPollToShow()
124: {
125: if ($this->getPollId()) {
126: return $this->getPollId();
127: }
128: // get last voted poll (from session only)
129: $pollId = Mage::getSingleton('core/session')->getJustVotedPoll();
130: if (empty($pollId)) {
131: // get random not voted yet poll
132: $votedIds = $this->getVotedPollsIds();
133: $pollId = $this->_pollModel
134: ->setExcludeFilter($votedIds)
135: ->setStoreFilter(Mage::app()->getStore()->getId())
136: ->getRandomId();
137: }
138: $this->setPollId($pollId);
139:
140: return $pollId;
141: }
142:
143: /**
144: * Get Poll related data
145: *
146: * @param int $pollId
147: * @return array|bool
148: */
149: public function getPollData($pollId)
150: {
151: if (empty($pollId)) {
152: return false;
153: }
154: $poll = $this->_pollModel->load($pollId);
155:
156: $pollAnswers = Mage::getModel('poll/poll_answer')
157: ->getResourceCollection()
158: ->addPollFilter($pollId)
159: ->load()
160: ->countPercent($poll);
161:
162: // correct rounded percents to be always equal 100
163: $percentsSorted = array();
164: $answersArr = array();
165: foreach ($pollAnswers as $key => $answer) {
166: $percentsSorted[$key] = $answer->getPercent();
167: $answersArr[$key] = $answer;
168: }
169: asort($percentsSorted);
170: $total = 0;
171: foreach ($percentsSorted as $key => $value) {
172: $total += $value;
173: }
174: // change the max value only
175: if ($total > 0 && $total !== 100) {
176: $answersArr[$key]->setPercent($value + 100 - $total);
177: }
178:
179: return array(
180: 'poll' => $poll,
181: 'poll_answers' => $pollAnswers,
182: 'action' => Mage::getUrl('poll/vote/add', array('poll_id' => $pollId, '_secure' => true))
183: );
184: }
185:
186:
187: /**
188: * Add poll template
189: *
190: * @param string $template
191: * @param string $type
192: * @return Mage_Poll_Block_ActivePoll
193: */
194: public function setPollTemplate($template, $type)
195: {
196: $this->_templates[$type] = $template;
197: return $this;
198: }
199:
200: /**
201: * Render block HTML
202: *
203: * @return string
204: */
205: protected function _toHtml()
206: {
207: /** @var $coreSessionModel Mage_Core_Model_Session */
208: $coreSessionModel = Mage::getSingleton('core/session');
209: $justVotedPollId = $coreSessionModel->getJustVotedPoll();
210: if ($justVotedPollId && !$this->_pollModel->isVoted($justVotedPollId)) {
211: $this->_pollModel->setVoted($justVotedPollId);
212: }
213:
214: $pollId = $this->getPollToShow();
215: $data = $this->getPollData($pollId);
216: $this->assign($data);
217:
218: $coreSessionModel->setJustVotedPoll(false);
219:
220: if ($this->_pollModel->isVoted($pollId) === true || $justVotedPollId) {
221: $this->setTemplate($this->_templates['results']);
222: } else {
223: $this->setTemplate($this->_templates['poll']);
224: }
225: return parent::_toHtml();
226: }
227:
228:
229: /**
230: * Get cache key informative items that must be preserved in cache placeholders
231: * for block to be rerendered by placeholder
232: *
233: * @return array
234: */
235: public function getCacheKeyInfo()
236: {
237: $items = array(
238: 'templates' => serialize($this->_templates)
239: );
240:
241: $items = parent::getCacheKeyInfo() + $items;
242:
243: return $items;
244: }
245:
246: }
247: