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 ='