>
------------------------------------------------------------------------------
See main.inc.php for release information
MyPolls_root : common classe for admin and public classes
--------------------------------------------------------------------------- */
include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/common_plugin.class.inc.php');
include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/users_groups.class.inc.php');
include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/css.class.inc.php');
include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/ajax.class.inc.php');
global $ajax;
class MyPolls_root extends common_plugin
{
protected $questions_types;
protected $display_types;
protected $section_name; //name of mypolls's section
protected $css; //the css object
protected $ajax;
public function MyPolls_root($prefixeTable, $filelocation)
{
$this->plugin_name='MyPolls.2';
$this->plugin_name_files="mypolls";
parent::common_plugin($prefixeTable, $filelocation);
$this->section_name=$this->plugin_name_files;
$list=array('polls', 'polls_lang', 'polls_questions', 'polls_questions_lang',
'polls_answers', 'polls_answers_lang', 'polls_votes');
$this->set_tables_list($list);
$this->questions_types=array('mono', 'multi', 'order');
$this->display_types=array('mono', 'multi');
$this->css = new css(dirname($this->filelocation).'/'.$this->plugin_name_files.".css");
}
public function init_events()
{
add_event_handler('blockmanager_register_blocks', array(&$this, 'register_blocks') );
}
public function register_blocks( $menu_ref_arr )
{
$menu = & $menu_ref_arr[0];
if ($menu->get_id() != 'menubar')
return;
$menu->register_block( new RegisteredBlock( 'mbMYPOLLS_menu', 'Polls', 'MYPOLLS'));
}
/*
surchage of common_plugin->save_config function
*/
public function save_config()
{
if(parent::save_config())
{
$this->css->make_CSS($this->generate_CSS());
return(true);
}
return(false);
}
/*
surchage of common_plugin->save_config function
*/
public function load_config()
{
parent::load_config();
if(!$this->css->css_file_exists())
{
$this->css->make_CSS($this->generate_CSS());
}
}
/*
intialize default values
*/
public function init_config()
{
//global $user;
$this->my_config=array(
'mypolls_color_bars' => '6666ff', /**/
'mypolls_maxbarwidth' => '400', /**/
'mypolls_display_type_default' => 'mono', /**/
'mypolls_admincolor' => 'ffff66', /**/
'mypolls_mouseovercolor' => '303030', /**/
'mypolls_nbuser_per_page' => 15, /**/
'mypolls_nbcomment_per_page' => 15, /**/
/* not present in config panel, but needed by mypolls */
'mypolls_visible_default' => 'y',
'mypolls_allow_comment_default' => 'n',
'mypolls_show_user_comment_default' => 'n',
'mypolls_allowed_users_default_guest' => 'y',
'mypolls_allowed_users_default_generic' => 'y',
'mypolls_allowed_users_default_normal' => 'y',
'mypolls_allowed_groups_default' => '',
'mypolls_question_type_default' => 'mono',
'mypolls_multi_max_answers_default' => '3',
'mypolls_public_results' => 'y',
/* 'mypolls_default_language' => $user['language'], */
'mypolls_menubar_title' => array(),
'mypolls_generic_text_users' => array(),
'mypolls_generic_text_guests' => array(),
'mypolls_not_public_results' => array()
);
$languages=get_languages();
foreach($languages as $key => $val)
{
$this->my_config['mypolls_menubar_title'][$key]='MyPolls';
$this->my_config['mypolls_generic_text_users'][$key]='';
$this->my_config['mypolls_generic_text_guests'][$key]='';
$this->my_config['mypolls_not_public_results'][$key]='';
}
}
/*
generate the css code
*/
protected function generate_CSS()
{
$text = "
.graphbar1,
.graphbarx {
border:0px;
height:8px;
display: block;
margin:0px;
padding:0px;
left:0;
position:relative;
}
.graphbar1 { background-color:#".$this->my_config['mypolls_color_bars']."; top:-3px; }
.graphbarx { background-color:transparent; top:0px; height:1px; }
.formtable P,
.formtable UL,
.formtable UL LI P
{
text-align:left;
display:block;
margin-top:0px;
margin-bottom:0px;
}
.formtable UL LI
{
margin-bottom:1em;
}
.formtable UL LI P,
.formtable P
{
color:#".$this->my_config['mypolls_admincolor'].";
padding-left:1.5em;
}
.TableRow:hover
{
background-color:#".$this->my_config['mypolls_mouseovercolor'].";
}
.formtable UL.compact LI
{
margin-bottom:0em;
}
.mypolls_page
{
text-align:justify;
margin:8px;
}
.mypolls_page FORM P
{
text-align:justify;
margin:0px;
margin-bottom:1em;
}
.littlefont { font-size:90%; }
table.littlefont th { text-align:center; padding:3px;padding-left:9px;padding-right:9px; }
table.littlefont td { text-align:left; padding:0px;padding-left:3px;padding-right:3px; }
";
return($text);
}
/* -------------------------------------------------------------------------
return an array of poll's values
if no lang given, return nfo with poll's default language
------------------------------------------------------------------------- */
protected function get_poll_values($poll_id, $lang="")
{
$sql="SELECT ".$this->tables['polls'].".*,
".$this->tables['polls_lang'].".title,
".$this->tables['polls_lang'].".description,
".$this->tables['polls_lang'].".after_vote_text
FROM ".$this->tables['polls'].",
".$this->tables['polls_lang']."
WHERE id = '$poll_id'
AND ".$this->tables['polls'].".id = ".$this->tables['polls_lang'].".id_poll ";
if($lang=="")
{
$sql.=" AND ".$this->tables['polls'].".default_lang = ".$this->tables['polls_lang'].".lang ";
}
else
{
$sql.=" AND ".$this->tables['polls_lang'].".lang = '".$lang."' ";
}
$result=pwg_query($sql);
if($result)
{
return(mysql_fetch_array($result));
}
return(false);
}
/* -------------------------------------------------------------------------
return an array of poll's translation
------------------------------------------------------------------------- */
protected function get_poll_languages($poll_id)
{
$sql="SELECT lang FROM ".$this->tables['polls_lang']." WHERE id_poll = '$poll_id'";
$result=pwg_query($sql);
if($result)
{
$returned=array();
while($row=mysql_fetch_row($result))
{
array_push($returned, $row[0]);
}
return($returned);
}
return(false);
}
/* -------------------------------------------------------------------------
returns poll's question
if no question id given, return an array of all question
if no lang given, return question with poll's default language
------------------------------------------------------------------------- */
protected function get_poll_questions($poll_id, $id_question="", $lang="")
{
$sql="SELECT ".$this->tables['polls_questions_lang'].".description,
".$this->tables['polls_questions'].".*
FROM ".$this->tables['polls_questions_lang'].",
".$this->tables['polls_questions'];
$sql_where=" WHERE ".$this->tables['polls_questions'].".id_poll = ".$poll_id."
AND ".$this->tables['polls_questions_lang'].".id_poll = "
.$this->tables['polls_questions'].".id_poll
AND ".$this->tables['polls_questions_lang'].".id_question = "
.$this->tables['polls_questions'].".question_num ";
if($id_question!="")
{
$sql_where.=" AND ".$this->tables['polls_questions'].".question_num = ".
$id_question." ";
}
if($lang=="")
{
$sql.=" , ".$this->tables['polls']." ";
$sql_where.=" AND ".$this->tables['polls'].".default_lang =
".$this->tables['polls_questions_lang'].".lang
AND ".$this->tables['polls'].".id =
".$this->tables['polls_questions'].".id_poll ";
}
else
{
$sql_where.=" AND ".$this->tables['polls_questions_lang'].".lang = '".$lang."' ";
}
$sql_order=" ORDER BY question_num asc ";
$result=pwg_query($sql.$sql_where.$sql_order);
if($result)
{
$returned=array();
while($row=mysql_fetch_assoc($result))
{
array_push($returned, $row);
}
return($returned);
}
return(false);
}
/* -------------------------------------------------------------------------
returns poll's question's answers
if no lang given, return answers with poll's default language
------------------------------------------------------------------------- */
protected function get_poll_answers($poll_id, $id_question, $lang="", $order_by="", $array_by_answer_id=false)
{
$sql="SELECT ".$this->tables['polls_answers'].".*,
".$this->tables['polls_answers_lang'].".nb_votes as vote_lang,
".$this->tables['polls_answers_lang'].".answer
FROM ".$this->tables['polls_answers']." INNER JOIN
".$this->tables['polls_answers_lang']."
ON ".$this->tables['polls_answers'].".id_poll = ".$this->tables['polls_answers_lang'].".id_poll
AND ".$this->tables['polls_answers'].".id_question = ".$this->tables['polls_answers_lang'].".id_question
AND ".$this->tables['polls_answers'].".answer_num = ".$this->tables['polls_answers_lang'].".id_answer ";
$sql_where=" WHERE ".$this->tables['polls_answers'].".id_poll = '".$poll_id."'
AND ".$this->tables['polls_answers'].".id_question = '".$id_question."' ";
if($order_by=="")
{
$sql_order=" ORDER BY ".$this->tables['polls_answers'].".answer_num ASC ";
}
else
{
$sql_order=" ORDER BY ".$order_by;
}
if($lang=="")
{
$sql.=" , ".$this->tables['polls']." ";
$sql_where.=" AND ".$this->tables['polls_answers'].".id_poll = ".$this->tables['polls'].".id
AND ".$this->tables['polls_answers_lang'].".lang = ".$this->tables['polls'].".default_lang ";
}
else
{
$sql_where.=" AND ".$this->tables['polls_answers_lang'].".lang = '".$lang."' ";
}
$result=pwg_query($sql.$sql_where.$sql_order);
if($result)
{
$returned=array();
$num=0;
while($row=mysql_fetch_assoc($result))
{
if(!$array_by_answer_id)
{ array_push($returned, $row); }
else
{
$num++;
$returned[$row['answer_num']] = $row;
$returned[$row['answer_num']]['order'] = $num;
}
}
return($returned);
}
return(false);
}
/* -------------------------------------------------------------------------
return an array of all polls
------------------------------------------------------------------------- */
protected function get_polls_list($user_id=2, $view_all=true, $lang="")
{
$returned=array();
$sql="SELECT ".$this->tables['polls'].".*,
".$this->tables['polls_lang'].".title
FROM ".$this->tables['polls_lang'].",
".$this->tables['polls'];
$sql_where=" WHERE ".$this->tables['polls'].".id =
".$this->tables['polls_lang'].".id_poll
AND ".$this->tables['polls_lang'].".lang = ";
if($lang=="")
{
$sql_where.=" ".$this->tables['polls'].".default_lang ";
}
else
{
$sql_where.=" '".$lang."' ";
}
if(!$view_all)
{
$sql.=" LEFT OUTER JOIN (SELECT poll_id, count(*) as as_voted
FROM ".$this->tables['polls_votes']."
WHERE user_id = '".$user_id."'
GROUP BY poll_id, user_id ) as vote_list
ON id = poll_id ";
$sql_where.=" AND visible = 'y' AND enabled = 'y' AND nb_questions>0
AND ((".$this->tables['polls'].".public_results = 'n'
AND vote_list.as_voted = 0
)
OR (".$this->tables['polls'].".public_results = 'y'))";
}
$sql_order=" ORDER BY id desc";
$result=pwg_query($sql.$sql_where.$sql_order);
if($result)
{
while($row=mysql_fetch_assoc($result))
{
array_push($returned, $row);
}
}
return($returned);
}
/*
return a list of user/ip making a comment on the poll
list is paged with $my_config['mypolls_nbcomment_per_page'] value
an array is returned :
array[0] : list
array[1] : total number of pages
*/
protected function get_poll_users_comment($poll_id, $num_page=1)
{
$returned=array(array(), -1);
$sql="SELECT SQL_CALC_FOUND_ROWS IF(username IS NULL, user_id, CONCAT(' ',username)) AS name, date, user_comment, user_id
FROM ".$this->tables['polls_votes']." LEFT JOIN ".USERS_TABLE."
ON ".$this->tables['polls_votes'].".user_id = ".USERS_TABLE.".id
WHERE poll_id = $poll_id and user_comment != ''
ORDER BY date desc
LIMIT ".(($num_page-1)*$this->my_config['mypolls_nbcomment_per_page']).", ".$this->my_config['mypolls_nbcomment_per_page'];
$result = pwg_query($sql);
if($result)
{
while($row = mysql_fetch_assoc($result))
{ array_push($returned[0], $row); }
$sql="select FOUND_ROWS()";
$result=pwg_query($sql);
if($result)
{ $row = mysql_fetch_row($result); $returned[1] = ceil($row[0]/$this->my_config['mypolls_nbcomment_per_page']); }
else
{ $returned[1] = -1; }
}
return($returned);
}
/*
common fo&bo function to display results
//$poll_values is by ref, because it can be a huge array
*/
protected function display_results_block($poll_id, $lang, &$poll_values)
{
global $template;
/*
function used with this templates
mypolls_public_results.tpl
mypolls_public_question.tpl
plugin_admin_view.tpl
only affect $questions values
$datas values are affected outside of this function
*/
$template_questions=array();
$questions_values=$this->get_poll_questions($poll_id, "", $lang);
$numq=1;
foreach($questions_values as $key => $val)
{
if($val['question_type']=='multi')
{
$show_maxi=' '.sprintf(l10n('mypolls_answer_maxi'), $val['multi_max_answers']);
}
else
{
$show_maxi="";
}
if($val['question_type']=='order')
{
$nbvotes_label='';
$order_by=" nb_votes ASC";
}
else
{
$nbvotes_label=l10n('mypolls_result_nbanswers')." (".$val['nb_votes'].")";
$order_by=" nb_votes DESC";
}
$template_questions_answers=array();
$numa=1;
$answers_values=$this->get_poll_answers($poll_id, $val['question_num'], $lang, $order_by.", ans_order ASC");
foreach($answers_values as $key2 => $val2)
{
if(($val['nb_votes']>0)&&($poll_values['total_votes']>0))
{
if($val['question_type']=='order')
{
$pct=100*($poll_values['total_votes']-$val2['frequency'])/$poll_values['total_votes'];
}
else
{
$pct=100*$val2['nb_votes']/$val['nb_votes'];
}
$graph=ceil($pct*$this->my_config['mypolls_maxbarwidth']/100);
}
else
{
$pct=0;
$graph=0;
}
$template_questions_answers[]=array(
'NUMANSWER' => $numa,
'ANSWER' => html_entity_decode($val2['answer']),
'PCT' => number_format($pct, 2, '.', '').'% ',
'GRAPH' => $graph,
'MAXGRAPH' => $this->my_config['mypolls_maxbarwidth'],
'NBANSWSERS' => ($val['question_type']!='order')?$val2['nb_votes']:""
);
$numa++;
}
$template_questions[]=array(
'MAXGRAPH' => ceil($this->my_config['mypolls_maxbarwidth']*1.10),
'NUMQUESTION' => $numq,
'DESCRIPTION' => html_entity_decode($val['description']),
'QUESTION_TYPE' => strtolower(l10n('mypolls_question_'.$val['question_type']).$show_maxi),
'NBVOTES' => $nbvotes_label,
'answers' => $template_questions_answers
);
$numq++;
}
$template->assign('questions', $template_questions);
if($poll_values['color_bars']!=$this->my_config['mypolls_color_bars'])
{
$template->append('head_elements', '');
}
} //display_results_block
/* ---------------------------------------------------------------------------
ajax functions
--------------------------------------------------------------------------- */
/*
return html formatted informations about poll's comments users
*/
protected function ajax_poll_comment_list($poll_id)
{
global $user;
if(!isset($_REQUEST['vpage']))
{
$page=1;
}
else
{
$page=$_REQUEST['vpage'];
}
$comment_list=$this->get_poll_users_comment($poll_id, $page);
$local_tpl = new Template(MYPOLLS_PATH."templates/", "");
$local_tpl->set_filename('body_page',
dirname($this->filelocation).'/templates/plugin_comment_list.tpl');
$template_datas=array();
$template_datarows=array();
//users list
foreach($comment_list[0] as $key => $val)
{
$template_datarows[] =array(
'USERNAME' => $val['name'],
'DATE' => $val['date'],
'COMMENT' => nl2br($val['user_comment']),
'display_delete' => (defined('IN_ADMIN') and IN_ADMIN)?'yes':'',
'ID' => (defined('IN_ADMIN') and IN_ADMIN)?$val['user_id']:'',
'PAGE' => (defined('IN_ADMIN') and IN_ADMIN)?$page :''
);
}
//make pages links
if($comment_list[1]>1)
{
$plural="s";
}
else
{
$plural="";
}
$pages_links=l10n("mypolls_page".$plural."_label")." : ";
if($comment_list[1]<=0)
{
$pages_links="";
}
else
{
for($i=1;$i<=$comment_list[1];$i++)
{
if($i==$page)
{ $pages_links.=" $i "; }
else
{
$pages_links.=" $i ";
}
}
}
$local_tpl->assign('datas', array("PAGES" => $pages_links));
$local_tpl->assign('comment_list_rows', $template_datarows);
return($local_tpl->parse('body_page', true));
}
} //class
?>