00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Poll_Block_ActivePoll extends Mage_Core_Block_Template
00035 {
00036 protected $_templates, $_voted;
00037
00038 public function __construct()
00039 {
00040 parent::__construct();
00041
00042 $pollModel = Mage::getModel('poll/poll');
00043
00044 $pollId = Mage::getSingleton('core/session')->getJustVotedPoll();
00045 if (empty($pollId)) {
00046
00047 $votedIds = $pollModel->getVotedPollsIds();
00048 $pollId = $pollModel->setExcludeFilter($votedIds)
00049 ->setStoreFilter(Mage::app()->getStore()->getId())
00050 ->getRandomId();
00051 }
00052 if (empty($pollId)) {
00053 return false;
00054 }
00055 $poll = $pollModel->load($pollId);
00056
00057 $pollAnswers = Mage::getModel('poll/poll_answer')
00058 ->getResourceCollection()
00059 ->addPollFilter($pollId)
00060 ->load()
00061 ->countPercent($poll);
00062
00063
00064 $percentsSorted = array();
00065 $answersArr = array();
00066 foreach ($pollAnswers as $key => $answer) {
00067 $percentsSorted[$key] = $answer->getPercent();
00068 $answersArr[$key] = $answer;
00069 }
00070 asort($percentsSorted);
00071 $total = 0;
00072 foreach ($percentsSorted as $key => $value) {
00073 $total += $value;
00074 }
00075
00076 if ($total > 0 && $total !== 100) {
00077 $answersArr[$key]->setPercent($value + 100 - $total);
00078 }
00079
00080 $this->assign('poll', $poll)
00081 ->assign('poll_answers', $pollAnswers)
00082 ->assign('action', Mage::getUrl('poll/vote/add', array('poll_id' => $pollId)));
00083
00084 $this->_voted = Mage::getModel('poll/poll')->isVoted($pollId);
00085 Mage::getSingleton('core/session')->setJustVotedPoll(false);
00086 }
00087
00088 public function setPollTemplate($template, $type)
00089 {
00090 $this->_templates[$type] = $template;
00091 return $this;
00092 }
00093
00094 protected function _toHtml()
00095 {
00096 if( $this->_voted === true ) {
00097 $this->setTemplate($this->_templates['results']);
00098 } else {
00099 $this->setTemplate($this->_templates['poll']);
00100 }
00101 return parent::_toHtml();
00102 }
00103 }