添加 apoll 到 SVN

+YUCHENG HU+

git-svn-id: https://svn.code.sf.net/p/hawebs/svn@73 a2543c7e-f6e9-4f8a-8bff-1ffc34733512
This commit is contained in:
YuCheng Hu
2010-06-02 06:02:14 +00:00
parent 0e1e7b52b1
commit d431f4d177
6 changed files with 590 additions and 0 deletions
@@ -0,0 +1,126 @@
<?php
# @version $Id: modules/mod_apoll/helper.php
# @package: Apoll Vote
# ===================================================
# @author
# Name: Hristo Genev
# Email: [email protected]
# Url: http://www.afactory.org
# ===================================================
# @copyright Copyright (C) 2008 aFactory.org All rights reserved.
# @license see http://www.gnu.org/licenses/lgpl.html GNU/LGPL.
# You can use, redistribute this file and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation.
# License http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
// no direct access
defined('_JEXEC') or die('Restricted access');
class modApollHelper
{
function getApollOptions($apoll_id)
{
$db =& JFactory::getDBO();
$query = "SELECT o.id, o.text, o.ordering" .
" FROM ".$db->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;
}
}
?>
@@ -0,0 +1 @@
<html><body bgcolor="#FFFFFF"></body></html>
@@ -0,0 +1,188 @@
<?php
# @file: modules/mod_apoll/mod_apoll.php
# @package: aPoll
# ===================================================
# @author
# Name: Hristo Genev
# Email: [email protected]
# Url: http://www.afactory.org
# ===================================================
# @copyright Copyright (C) 2008 aFactory.org All rights reserved.
# @license see http://www.gnu.org/licenses/lgpl.html GNU/LGPL.
# You can use, redistribute this file and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation.
# License http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
// no direct access
defined('_JEXEC') or die('Restricted access');
// Include the syndicate functions only once
require_once (dirname(__FILE__).DS.'helper.php');
$tabclass_arr = array ('sectiontableentry2', 'sectiontableentry1');
$menu = &JSite::getMenu();
$items = $menu->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 ='<br />';}
if ( $apoll && $apoll->id ) {
$layout = JModuleHelper::getLayoutPath('mod_apoll');
$tabcnt = 0;
$options = modApollHelper::getApollOptions($apoll_id);
$itemid = modApollHelper::getItemid($apoll_id);
require($layout);
}
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE install SYSTEM "http://dev.joomla.org/xml/1.5/module-install.dtd">
<install type="module" version="1.5.0">
<name>Apoll</name>
<author>Hristo Genev</author>
<creationDate>October 2009</creationDate>
<copyright>Copyright (C) 2009</copyright>
<license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.afactory.org</authorUrl>
<version>2.1.005</version>
<description>Apoll module for voting</description>
<files>
<filename module="mod_apoll">mod_apoll.php</filename>
<filename module="mod_apoll">index.html</filename>
<filename module="mod_apoll">helper.php</filename>
<folder module="mod_apoll">tmpl</folder>
</files>
<languages folder="language">
<language tag="en-GB">en-GB.mod_apoll.ini</language>
</languages>
<params addpath="/administrator/components/com_apoll/elements">
<param name="moduleclass_sfx" type="text" default="" label="Module Class Suffix" description="PARAMMODULECLASSSUFFIX" />
<param type="spacer" />
<param name="id" type="apoll" default="" label="SELECT POLL" description="PARAMMODULEPOLL" />
<param name="ajax" type="radio" default="1" label="AJAX VOTE" description="AJAX VOTE DESC">
<option value="1">Yes</option>
<option value="0">NO</option>
</param>
<param name="show_poll_title" type="radio" default="1" label="SHOW POLL TITLE" description="SHOW POLL TITLE DESC">
<option value="1">Yes</option>
<option value="0">NO</option>
</param>
<param type="spacer" />
<param name="only_one_color" type="radio" default="1" label="ONLY ONE COLOR" description="ONLY ONE COLOR DESC">
<option value="1">Yes</option>
<option value="0">NO</option>
</param>
<param name="apoll_bars_color" type="text" default="cccccc" label="BARS COLOR" description="BARS COLOR DESC" size="7" />
<param name="apoll_bars_border_color" type="text" default="cccccc" label="BARS BORDER COLOR" description="BARS BORDER COLOR DESC" size="7" />
<param type="spacer" />
<param name="show_view_details" type="radio" default="1" label="SHOW VIEW DETAILS" description="SHOW VIEW DETAILS DESC">
<option value="1">Yes</option>
<option value="0">NO</option>
</param>
<param name="show_rel_article" type="radio" default="1" label="SHOW REL ARTICLE" description="SHOW REL ARTICLE DESC">
<option value="1">Yes</option>
<option value="0">NO</option>
</param>
<param name="rel_article" type="text" default="http://www.afactory.org" label="RELATED ARTICLE LINK" description="RELATED ARTICLE LINK DESC" size="40" />
<param name="rel_article_window" type="radio" default="_self" label="IN NEW WINDOW" description="ONE IN NEW WINDOW DESC">
<option value="_blank">Yes</option>
<option value="_self">NO</option>
</param>
<param type="spacer" />
<param name="show_total" type="radio" default="1" label="SHOW TOTAL" description="SHOW TOTAL DESC">
<option value="1">Yes</option>
<option value="0">NO</option>
</param>
<param name="show_msg" type="radio" default="1" label="SHOW MESSAGES" description="SHOW MESSAGES DESC">
<option value="1">Yes</option>
<option value="0">NO</option>
</param>
<param name="show_detailed_msg" type="radio" default="1" label="SHOW DETAILED MESSAGES" description="SHOW DETAILED MESSAGES DESC">
<option value="1">Yes</option>
<option value="0">NO</option>
</param>
<param name="msg_date_format" type="text" default="%d %b %Y - %H:%M " label="MSG DATE FORMAT" description="MSG DATE FORMAT DESC" size="25" />
</params>
<params group="advanced">
<param name="cache" type="list" default="1" label="Caching" description="Select whether to cache the content of this module">
<option value="1">Use global</option>
<option value="0">No caching</option>
</param>
<param name="cache_time" type="text" default="900" label="Cache Time" description="The time before the module is recached" />
</params>
</install>
@@ -0,0 +1,188 @@
<?php
# @file: modules/mod_apoll/tmpl/default.php
# @package: aPoll
# ===================================================
# @author
# Name: Hristo Genev
# Email: [email protected]
# Url: http://www.afactory.org
# ===================================================
# @copyright Copyright (C) 2008 aFactory.org All rights reserved.
# @license see http://www.gnu.org/licenses/lgpl.html GNU/LGPL.
# You can use, redistribute this file and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation.
# License http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
// no direct access
defined('_JEXEC') or die('Restricted access');
$document =& JFactory::getDocument();
$document->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;
}
");
?>
<div class="apoll<?php echo $params->get('moduleclass_sfx'); ?>" style="width:95%; border:none; padding:1px;">
<?php if ($params->get('show_poll_title')) : ?>
<h4 class="apoll_mod_title"><?php echo $apoll->title; ?></h4>
<?php endif; ?>
<div id="apolldiv_<?php echo $apoll->id;?>">
<?php if ($display_poll) { ?>
<form action="<?php echo JRoute::_( 'index.php' );?>" method="post" name="apoll_vote_<?php echo $apoll->id;?>" id="apoll_vote_<?php echo $apoll->id;?>">
<?php for ($i = 0, $n = count($options); $i < $n; $i ++) { ?>
<label for="voteid<?php echo $options[$i]->id;?>" class="<?php echo $tabclass_arr[$tabcnt].$params->get('moduleclass_sfx'); ?>" style="display:block; padding:2px;">
<input type="radio" name="voteid" id="voteid<?php echo $options[$i]->id;?>" value="<?php echo $options[$i]->id;?>" alt="<?php echo $options[$i]->id;?>" <?php echo $disabled; ?> />
<?php echo $options[$i]->text; ?>
</label>
<?php $tabcnt = 1 - $tabcnt; }
//show messages box
if($params->get('show_msg')) :
echo '<div id="mod_apoll_messages_'.$apoll->id.'" style="margin:5px;">'.JText::_($msg);
if($params->get('show_detailed_msg')) echo " ".$details;
echo '</div>';
endif;
?>
<div style="padding:2px;" id="apoll_buttons_<?php echo $apoll->id;?>" >
<input type="submit" id="submit_vote_<?php echo $apoll->id; ?>" name="task_button" class="button" value="<?php echo JText::_('Vote'); ?>" <?php echo $disabled; ?> />
</div>
<div id="apoll_loading_<?php echo $apoll->id;?>" style="display:none;"><?php echo JText::_('Processing ...'); ?>
</div>
<input type="hidden" name="option" value="com_apoll" />
<input type="hidden" name="id" value="<?php echo $apoll->id;?>" />
<?php if($params->get('ajax')) : ?>
<input type="hidden" name="format" value="raw" />
<input type="hidden" name="view" value="apoll" />
<?php else : ?>
<input type="hidden" name="task" value="vote" />
<?php endif;
echo "<div>".JHTML::_( 'form.token' )."</div>"; ?>
</form>
<?php if($params->get('ajax')) {
// add mootools
JHTML::_('behavior.mootools');
$updateValue = '';
for ($i=0; $i<count($results); $i++) {
if($params->get('only_one_color')) {
$background_color = $params->get('apoll_bars_color');
} else {
$background_color = "' + options.item($i).attributes[3].nodeValue + '";
}
$updateValue .= "<div style=\"width:100%\"><div style=\"padding: 3px;\">' + text.item($i).firstChild.nodeValue + ' - ' + options.item($i).attributes[1].nodeValue + '%</div><div class=\"apoll_module_bar_holder\" id=\"apoll_module_bar_holder".$i."\" style=\"width: 100%; height: 10px; padding:1px; border:1px solid #".$params->get('apoll_bars_border_color').";\"><div id=\"apoll_module_bar'+options.item($i).attributes[0].nodeValue+'\" style=\"background:#$background_color; width:' + options.item($i).attributes[1].nodeValue + '%; height:10px;\"></div></div></div>";
}
if($params->get('show_total')) {
$updateValue .= "<br /><b>".JText::_('Total votes')."</b>: ' + voters.item(0).firstChild.nodeValue + '";
}
$js = "
/* <![CDATA[ */
window.addEvent('domready', function()
{
$('apoll_vote_".$apoll->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; ?>
<div>
<div style="padding:3px;"><?php echo $row->text." - ".$percent; ?>%</div>
<div style="width:100%; height:10px; padding:1px; border:1px solid #<?php echo $params->get('apoll_bars_border_color'); ?>;">
<div style="width: <?php echo $width; ?>%; height:10px;background:#<?php echo $background_color; ?>;"></div>
</div>
</div>
<?php endforeach;
if($params->get('show_total'))
echo "<br /><b>".JText::_('Total votes')."</b>: ".$row->votes;
if($params->get('show_msg')) :
echo '<div id="mod_apoll_messages_'.$apoll->id.'" style="margin:5px;">'.JText::_($msg);
if($params->get('show_detailed_msg')) {
echo " ".$details;
}
echo '</div>';
endif;
} ?>
<!-- End of #apolldiv -->
</div>
<?php if (($params->get('show_view_details')) || ($params->get('rel_article_window'))) { ?>
<div id="apoll_links" style="padding-top:5px; ">
<?php if ($params->get('show_view_details')) : ?>
<a class="apoll_result_link" href="<?php echo JRoute::_('index.php?option=com_apoll&view=apoll&id='.$slug.$itemid); ?>"><?php echo JText::_('View details'); ?></a><br />
<?php endif; ?>
<?php if ($params->get('show_rel_article')) : ?>
<a class="apoll_result_link" target="<?php echo $params->get('rel_article_window'); ?>" href="<?php echo JRoute::_($params->get('rel_article')); ?>">
<?php echo JText::_('read the related article >>'); ?></a>
<?php endif; ?>
<?php } ?>
</div>
<!-- End of module .apoll -->
</div>
@@ -0,0 +1 @@
<html><body bgcolor="#FFFFFF"></body></html>