From d431f4d1773ca41989289736b5f6cd02cb94aa4b Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Wed, 2 Jun 2010 06:02:14 +0000 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20apoll=20=E5=88=B0=20SVN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit +YUCHENG HU+ git-svn-id: https://svn.code.sf.net/p/hawebs/svn@73 a2543c7e-f6e9-4f8a-8bff-1ffc34733512 --- cms/joomla/trunk/modules/mod_apoll/helper.php | 126 ++++++++++++ cms/joomla/trunk/modules/mod_apoll/index.html | 1 + .../trunk/modules/mod_apoll/mod_apoll.php | 188 ++++++++++++++++++ .../trunk/modules/mod_apoll/mod_apoll.xml | 86 ++++++++ .../trunk/modules/mod_apoll/tmpl/default.php | 188 ++++++++++++++++++ .../trunk/modules/mod_apoll/tmpl/index.html | 1 + 6 files changed, 590 insertions(+) create mode 100644 cms/joomla/trunk/modules/mod_apoll/helper.php create mode 100644 cms/joomla/trunk/modules/mod_apoll/index.html create mode 100644 cms/joomla/trunk/modules/mod_apoll/mod_apoll.php create mode 100644 cms/joomla/trunk/modules/mod_apoll/mod_apoll.xml create mode 100644 cms/joomla/trunk/modules/mod_apoll/tmpl/default.php create mode 100644 cms/joomla/trunk/modules/mod_apoll/tmpl/index.html diff --git a/cms/joomla/trunk/modules/mod_apoll/helper.php b/cms/joomla/trunk/modules/mod_apoll/helper.php new file mode 100644 index 00000000..0f4879e3 --- /dev/null +++ b/cms/joomla/trunk/modules/mod_apoll/helper.php @@ -0,0 +1,126 @@ +nameQuote('#__apoll_options') . " AS o " . + " WHERE o.apoll_id = " . (int) $apoll_id . + " AND o.text <> ''" . + " ORDER BY o.ordering" + ; + + $db->setQuery($query); + + if (!($options = $db->loadObjectList())) { + echo "helper ".$db->stderr(); + return; + } + + return $options; + } + // checks if user has voted (if cookie is set) + function alreadyVoted($id) { + + global $mainframe; + + $cookieName = JUtility::getHash( $mainframe->getName() . 'apoll' . $id ); + $voted = JRequest::getVar( $cookieName, '0', 'COOKIE', 'INT'); + + return $voted; + } + + function userVoted($user_id, $apoll_id) { + $db =& JFactory::getDBO(); + $query = "SELECT date FROM ".$db->nameQuote('#__apoll_votes') + ." WHERE apoll_id=".(int) $apoll_id + ." AND user_id=".(int)$user_id + ; + $db->setQuery($query); + + return $userVoted=($db->loadResult())?1:0; + } + + function ipVoted($apoll_id) { + $db =& JFactory::getDBO(); + $ip = $_SERVER['REMOTE_ADDR']; +$query = "SELECT ip FROM ".$db->nameQuote('#__apoll_votes') + ."WHERE apoll_id=".(int) $apoll_id." + AND ip = INET_ATON('".$ip."')" + ; + $db->setQuery($query); + + return $ipVoted=($db->loadResult()) ? 1:0; + } + + function getResults($apoll_id) { + + $db =& JFactory::getDBO(); + $query = "SELECT o.*, COUNT(v.id) AS hits, + (SELECT COUNT(id) FROM ".$db->nameQuote('#__apoll_votes')." WHERE apoll_id=".$apoll_id.") AS votes + FROM ".$db->nameQuote('#__apoll_options')." AS o + LEFT JOIN ".$db->nameQuote('#__apoll_votes')." AS v + ON (o.id = v.option_id AND v.apoll_id = ".(int)$apoll_id . ") + WHERE o.apoll_id=".(int)$apoll_id ." + AND o.text <> '' + GROUP BY o.id + ORDER BY o.ordering"; + + $db->setQuery($query); + + return $db->loadObjectList(); + } + + function getActivePolls() { + $db =& JFactory::getDBO(); + $query = "SELECT id FROM ".$db->nameQuote('#__apoll_polls') . + " WHERE published = 1"; + $db->setQuery($query); + if ($ids = $db->loadResultArray()) { + return $ids; + } else { + return false; + } + } + + function getItemid($apoll_id) { + + $component =& JComponentHelper::getComponent('com_apoll'); + $menus =& JApplication::getMenu('site', array()); + $items = $menus->getItems('componentid', $component->id); + $match = null; + $itemid = null; + + if (isset($items)) { + foreach ($items as $item) { + if ((@$item->query['view'] == 'apoll') && (@$item->query['id'] == $apoll_id)) { + $itemid = $item->id; + break; + } + } + } + return '&Itemid='.$itemid; + } +} +?> diff --git a/cms/joomla/trunk/modules/mod_apoll/index.html b/cms/joomla/trunk/modules/mod_apoll/index.html new file mode 100644 index 00000000..fa6d84e8 --- /dev/null +++ b/cms/joomla/trunk/modules/mod_apoll/index.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/cms/joomla/trunk/modules/mod_apoll/mod_apoll.php b/cms/joomla/trunk/modules/mod_apoll/mod_apoll.php new file mode 100644 index 00000000..8703ba72 --- /dev/null +++ b/cms/joomla/trunk/modules/mod_apoll/mod_apoll.php @@ -0,0 +1,188 @@ +getItems('link', 'index.php?option=com_apoll&view=apoll'); +$itemid = isset($items[0]) ? '&Itemid='.$items[0]->id : ''; +$details = ""; + +$apoll_id = $params->get( 'id', 0 ); +//if Show random poll is selected +if (!$apoll_id) { + $ids = modApollHelper::getActivePolls(); + + if (count($ids) > 1) { + $apoll_id = $ids[array_rand($ids)]; + } else { + $apoll_id = $ids[0]; + } +} + +$results = modApollHelper::getResults($apoll_id); +//print_r($results); exit; +//get the component params +JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_apoll'.DS.'tables'); +$apoll =& JTable::getInstance('apoll', 'Table'); +$apoll->load( $apoll_id ); +$apollParams = new JParameter($apoll->params); +//merge them with the module params +$params->merge($apollParams, $params); + +$slug = ($apoll->alias=='')? $apoll->id : $apoll->id.":".$apoll->alias; +// check if cookie is set showing that the user has voted for this poll +$voted = modApollHelper::alreadyVoted($apoll_id); +// check if the registrated user has voted and if user has logged in +$user =& JFactory::getUser(); +$userVoted = modApollHelper::userVoted($user->id, $apoll_id); +$guest = $user->guest; +// check if this ip has been recorder +$ipVoted = modApollHelper::ipVoted($apoll_id); +//reset the var display_poll +$display_poll = 0; + +//check the start time and the end of poll +global $mainframe; +$date = &JFactory::getDate(); +$date->setOffset($mainframe->getCfg('offset')); +$now = $date->toFormat(); + +if(($now>$apoll->publish_up) && ($now<$apoll->publish_down)) { + $display_submit = 1; + + // if only registered users can vote + if ($params->get('only_registered')) { + //if the user is not a guest + if(!$guest) { + //if only one vote is allowed per logged user + if($params->get('one_vote_per_user')) { + //check if user has voted + if($userVoted) { + //display the poll with disabled options + $display_submit = 0; + $msg = "You have already voted."; + $details = JText::_("Only one vote per user is allowed."); + //user has not voted yet + } else { + //display the poll + $display_poll = 1; + $display_submit = 1; + $msg = ""; + } + // if loggedin user are allowed to vote unlimited times + } else { + // Check the cookie + if($voted) { + $display_poll = 0; + $display_submit = 0; + $msg = "You have already voted."; + $details = JText::sprintf("Only one vote each %u hours is allowed.", $apoll->lag/60); + + //hm check the ip please but only if allowed to do that + } elseif ($params->get('ip_check')) { + if($ipVoted) { + //display the poll with disabled options + $display_poll = 0; + $display_submit = 0; + $msg = "You have already voted."; + $details = JText::_("Only one vote per IP is allowed."); + //if user's ip has not been logged + } + //if user has not voted + } else { + //display the poll + $display_poll = 1; + $display_submit = 1; + $msg = ""; + } + } + //if the user has not logged in + } else { + //display message please log in + $display_poll = 1; + $display_submit = 0; + $msg = "Please register to vote"; + } + // if anybody can vote + } else { + //if user has voted, according to the cookie check + if($voted) { + //display the poll with disabled options or the results + $display_poll = ($params->get('if_voted'))? 1:0; + $display_submit = 0; + $msg = "You have already voted."; + $details = JText::sprintf("Only one vote each %u hours is allowed.", $apoll->lag/60); + //if user has not voted according to the cookie + } else { + //hm check the ip please but only if allowed to do that + if ($params->get('ip_check')) { + if($ipVoted) { + //display the poll with disabled options + $display_poll = ($params->get('if_voted'))? 1:0; + $display_submit = 0; + $msg = "You have already voted."; + $details = JText::_("Only one vote per IP is allowed."); + //if user's ip has not been logged + } else { + //display the poll + $display_poll = 1; + $display_submit = 1; + $msg = ""; + } + //if we are not allowed to do the ip check + } else { + //display the poll + $display_poll = 1; + $display_submit = 1; + $msg = ""; + } + } + } + +} else { + $display_submit = 0; + $msg = "The voting for this poll has not started yet."." "; + $publish_up = JFactory::getDate($apoll->publish_up); + $details = JText::_("It will start on:")." ".$publish_up->toFormat($params->get('msg_date_format')); +} +//if deadline has passed change the msg +if($now>$apoll->publish_down) { + $display_poll = 0; + $msg = "The voting for this poll has ended"." "; + $publish_down = JFactory::getDate($apoll->publish_down); + $details = JText::_("on:")." ".$publish_down->toFormat($params->get('msg_date_format')); +} + +$disabled = ($display_submit)? '':'disabled="disabled"'; +//if show messages is set to no, reset all messages +//if(!$params->get('show_msg')) {$msg ='
';} + +if ( $apoll && $apoll->id ) { + $layout = JModuleHelper::getLayoutPath('mod_apoll'); + $tabcnt = 0; + $options = modApollHelper::getApollOptions($apoll_id); + $itemid = modApollHelper::getItemid($apoll_id); + + require($layout); + +} \ No newline at end of file diff --git a/cms/joomla/trunk/modules/mod_apoll/mod_apoll.xml b/cms/joomla/trunk/modules/mod_apoll/mod_apoll.xml new file mode 100644 index 00000000..abddaaad --- /dev/null +++ b/cms/joomla/trunk/modules/mod_apoll/mod_apoll.xml @@ -0,0 +1,86 @@ + + + + Apoll + Hristo Genev + October 2009 + Copyright (C) 2009 + http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL + harrygg@gmail.com + http://www.afactory.org + 2.1.005 + Apoll module for voting + + + mod_apoll.php + index.html + helper.php + tmpl + + + en-GB.mod_apoll.ini + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/cms/joomla/trunk/modules/mod_apoll/tmpl/default.php b/cms/joomla/trunk/modules/mod_apoll/tmpl/default.php new file mode 100644 index 00000000..ccf51f55 --- /dev/null +++ b/cms/joomla/trunk/modules/mod_apoll/tmpl/default.php @@ -0,0 +1,188 @@ +addStyleDeclaration("div#apoll_loading_".$apoll->id." { + background: url(media/system/images/mootree_loader.gif) 0% 50% no-repeat; + width:100%; + height:20px; + padding: 4px 0 0 20px; +} +"); +?> + +
+ +get('show_poll_title')) : ?> +

title; ?>

+ + +
+ + +
+ + + get('show_msg')) : + echo '
'.JText::_($msg); + if($params->get('show_detailed_msg')) echo " ".$details; + echo '
'; + endif; + ?> +
+ /> +
+ + + + +get('ajax')) : ?> + + + + +".JHTML::_( 'form.token' )."
"; ?> + + +get('ajax')) { +// add mootools +JHTML::_('behavior.mootools'); + +$updateValue = ''; + +for ($i=0; $iget('only_one_color')) { + $background_color = $params->get('apoll_bars_color'); + } else { + $background_color = "' + options.item($i).attributes[3].nodeValue + '"; + } + + $updateValue .= "
' + text.item($i).firstChild.nodeValue + ' - ' + options.item($i).attributes[1].nodeValue + '%
get('apoll_bars_border_color').";\">
"; +} + + if($params->get('show_total')) { + $updateValue .= "
".JText::_('Total votes').": ' + voters.item(0).firstChild.nodeValue + '"; + } +$js = " +/* id."').addEvent('submit', function(e) { + // Prevent the submit event + new Event(e).stop(); + + var options = $('apoll_vote_".$apoll->id."').getElements('input[name=voteid]'); + + var nothing_selected = 1; + options.each(function(item, index){ + if(item.checked==1) {nothing_selected = 0;} + }); + if (nothing_selected) { + alert('".JText::_('Please select an option')."'); + return false; + } else { + + $('submit_vote_".$apoll->id."').disabled = 1; + $('apoll_loading_".$apoll->id."').setStyle('display',''); + + // Update the page + this.send({ + onComplete: function(response, responseXML) + { + // get the XML nodes + var root = responseXML.documentElement; + var options = root.getElementsByTagName('option'); + var text = root.getElementsByTagName('text'); + //var percentages = root.getElementsByTagName('percentages'); + var voters = root.getElementsByTagName('voters'); + + // prepare the XHTML + var updateValue = '".$updateValue."'; + + // update the page element 'update' + //$('apolldiv_".$apoll->id."').removeClass('ajax-loading'); + $('apolldiv_".$apoll->id."').empty().setHTML(updateValue); + var apollSlide = new Fx.Slide('apoll_comp_form'); + apollSlide.slideOut(); + }}); + } + }); + });/* ]]> */"; + + +$document->addScriptDeclaration( $js ); + + +} +//If user has voted + } else { + + foreach ($results as $row) : + $percent = ($row->votes)? round((100*$row->hits)/$row->votes, 1):0; + $width = ($percent)? $percent:2; + if($params->get('only_one_color')) + $background_color = $params->get('apoll_bars_color'); + else + $background_color = $row->color; ?> + +
+
text." - ".$percent; ?>%
+
+
+
+
+get('show_total')) + echo "
".JText::_('Total votes').": ".$row->votes; + + if($params->get('show_msg')) : + echo '
'.JText::_($msg); + if($params->get('show_detailed_msg')) { + echo " ".$details; + } + echo '
'; + endif; + } ?> + + +
+get('show_view_details')) || ($params->get('rel_article_window'))) { ?> + + + \ No newline at end of file diff --git a/cms/joomla/trunk/modules/mod_apoll/tmpl/index.html b/cms/joomla/trunk/modules/mod_apoll/tmpl/index.html new file mode 100644 index 00000000..fa6d84e8 --- /dev/null +++ b/cms/joomla/trunk/modules/mod_apoll/tmpl/index.html @@ -0,0 +1 @@ + \ No newline at end of file