> ------------------------------------------------------------------------------ See main.inc.php for release information MyPolls_PIP : classe to manage plugin public pages --------------------------------------------------------------------------- */ include_once('mypolls_root.class.inc.php'); include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/public_integration.class.inc.php'); class MyPolls_PIP extends MyPolls_root { protected $section_page; public function MyPolls_PIP($prefixeTable, $filelocation) { parent::__construct($prefixeTable, $filelocation); $this->load_config(); // don't create this object inside root classe otherwise header is modified // everywhere in admin pages $this->ajax = new Ajax(); $this->section_page = new public_integration($this->section_name); $this->init_events(); } /* load language file */ public function load_lang() { global $lang; load_language('plugin.lang', MYPOLLS_PATH); // ajax is managed here ; this permit to use user&language properties inside // ajax content $this->return_ajax_content(); } /* initialize events call for the plugin */ public function init_events() { parent::init_events(); $this->section_page->set_callback_page_function(array(&$this, 'manage_page')); $this->section_page->init_events(); add_event_handler('blockmanager_apply', array(&$this, 'blockmanager_apply') ); add_event_handler('loading_lang', array(&$this, 'load_lang')); add_event_handler('loc_end_page_header', array(&$this->css, 'apply_CSS')); } /* ------------------------------------------------------------------------- FUNCTIONS TO MANAGE POLL'S DISPLAY ------------------------------------------------------------------------- */ /* display mypolls page ; no parameters, all is in $_REQUEST this function is a called by a menubar's callback only when it is necessary */ protected function display_page($poll_id) { global $template, $user; $poll_values=$this->get_poll_values($poll_id, $user['language']); if(!$poll_values) { page_not_found('This page does not exist', 'index.php?'); return(false); } $users = new users($poll_values['allowed_users']); $groups = new groups($poll_values['allowed_groups']); $datas = array(); $user_groups=$this->get_user_groups($user['id']); //looks if user/group is allowed to access page if((!$users->is_allowed($user['status']) || (($poll_values['allowed_groups']!='') && !$groups->are_allowed($user_groups))) && !is_admin()) { page_not_found('User not allowed', 'index.php?'); } if($user['id']!=2) { $datas['GENERIC_TEXT']=$this->my_config['mypolls_generic_text_users'][$user['language']]; } else { $datas['GENERIC_TEXT']=$this->my_config['mypolls_generic_text_guests'][$user['language']]; } $datas['TITLE']=$poll_values['title']; //displayed nfo even if results are not public $datas['DESCRIPTION']=html_entity_decode($poll_values['description'], ENT_QUOTES); if(!$this->get_user_already_vote($poll_id, $user['id'], $_SERVER['REMOTE_ADDR'])) { // no vote for this poll by user, display poll in 'vote' mode //count visit on page $this->count_visit($poll_id, 'before'); $first_question=array('mono' => 0, 'multi' => 1); $template_file=dirname($this->filelocation).'/templates/mypolls_public_question.tpl'; $datas['ATT_ID']=$poll_id; $datas['AJAX_URL_POLL_DETAIL']='./index.php?/'.$this->section_name.'/'.$poll_id.'&ajaxfct=poll_question_blocks'; $datas['LANG']=$user['language']; $datas['FIRST_QUESTION']=$first_question[$poll_values['display_type']]; $datas['NBQUESTIONS']=$poll_values['nb_questions']; } else { //user/ip have already voted $template_file=dirname($this->filelocation).'/templates/mypolls_public_results.tpl'; $datas['AFTER_VOTE_TEXT']=html_entity_decode($poll_values['after_vote_text'], ENT_QUOTES); if($poll_values['public_results']=='n') { $not_public_text=html_entity_decode($this->my_config['mypolls_not_public_results'][$user['language']]); if($not_public_text=="") { $not_public_text=l10n('mypolls_not_public_results'); } $datas['NOT_PUBLIC_RESULT_TEXT']=$not_public_text; } else { if($poll_values['show_comments']=='y') { $datas["AJAX_URL_POLL_COMMENT_LIST"]='./index.php?/'.$this->section_name.'/'.$poll_id.'&ajaxfct=poll_comment_list&vpage='; } $this->display_results_block($poll_id, $user['language'], $poll_values); } //count visit on page $this->count_visit($poll_id, 'after'); } $template->set_filename('body_page', $template_file); $template->assign('TITLE', $datas['TITLE']); $template->assign('datas', $datas); // add icon in title bar if (is_admin()) { $this->make_icons(PHPWG_ROOT_PATH.$this->page_link."&f_tabsheet=polls&action=modify&fmypolls_att_id=".$poll_id, 'mypolls_msg_modify_poll','preferences.png', 'edit', 'icon_category_edit'); } $this->make_icons(make_index_url(),'return to homepage', 'home.png', 'home', 'icon_home'); //remote icons from the title bar unset($template->smarty->_tpl_vars["U_MODE_CREATED"]); unset($template->smarty->_tpl_vars["U_MODE_POSTED"]); $template->assign_var_from_handle('PLUGIN_INDEX_CONTENT_BEGIN', 'body_page'); } protected function make_icons($url, $title, $icon, $alt, $id="") { global $template; //method to make icon change with template // assume yoga method by default switch($template->smarty->_tpl_vars["themeconf"]['template']) { case 'gally': $template->concat( 'PLUGIN_INDEX_ACTIONS', '
  • '); break; default: $template->concat( 'PLUGIN_INDEX_ACTIONS', '
  • '.l10n($alt).'
  • '); } } /* analyse uri request, do vote and load the page */ public function manage_page() { global $page, $user; $this->load_lang(); if($page['section'] == $this->section_name) { $poll_id=$this->extract_poll_id_from_uri(); if((isset($_POST['submit_results']))&&(!$this->get_user_already_vote($poll_id, $user['user_id'], $_SERVER['REMOTE_ADDR']))) { if(!isset($_REQUEST['fmypolls_comment'])) { $_REQUEST['fmypolls_comment']=''; } $this->do_vote($poll_id, $_REQUEST['fmypolls_results'], $_REQUEST['fmypolls_comment'], $user['user_id'], $_SERVER['REMOTE_ADDR'], $user['language']); } $this->display_page($poll_id); } } /* initialise menubar's menu called by menubar object when making menu */ public function blockmanager_apply( $menu_ref_arr ) { global $user, $template; $menu = & $menu_ref_arr[0]; if ( ($block = $menu->get_block( 'mbMYPOLLS_menu' ) ) != null ) { if($user['id']==2) { $vote_id = $_SERVER['REMOTE_ADDR']; } else { $vote_id = $user['id']; } $polls_list=$this->get_polls_list($vote_id, is_admin(), $user['language']); $menu_list=array(); if(is_admin()) { array_push($menu_list, array( 'nfo' => "", 'text' => l10n('mypolls_admin_add'), 'link' => PHPWG_ROOT_PATH.$this->page_link.'&f_tabsheet=polls&action=create', 'edit' => "" ) ); } foreach($polls_list as $key => $val) { $nfo=""; if($val['enabled']=='n') { $nfo.='['.l10n('mypolls_menu_not_enabled').']'; } if($val['visible']=='n') { $nfo.='['.l10n('mypolls_menu_not_visible').']'; } if($val['public_results']=='n') { $nfo.='['.l10n('mypolls_menu_not_publicr').']'; } array_push($menu_list, array( 'nfo' => $nfo, 'text' => $val['title'], 'link' => './index.php?/'.$this->section_name.'/'.$val['id'], 'edit' => (is_admin())?PHPWG_ROOT_PATH.$this->page_link."&f_tabsheet=polls&action=modify&fmypolls_att_id=".$val['id']:"" ) ); } $block->set_title($this->my_config['mypolls_menubar_title'][$user['language']]); $block->template = dirname(__FILE__).'/templates/mypolls_public_menu.tpl'; $block->data = $menu_list; } } protected function extract_poll_id_from_uri() { $poll_id=explode($this->section_name.'/', $_SERVER['REQUEST_URI']); $poll_id=explode('&', $poll_id[1]); return($poll_id[0]); } /* --------------------------------------------------------------------------- FUNCTIONS TO MANAGE POLL'S VOTES --------------------------------------------------------------------------- */ /* analyse formated string "fq_N_a_M_P" return an array(N,M,P) */ protected function analyse_answer($answer) { $tmp=explode("_",$answer); if(!isset($tmp[4])) { $tmp[4]=1; } $return=array($tmp[1],$tmp[3],$tmp[4]); return($return); } /* do vote return true if Ok, else a number -1 : database problem -2 : user has already vote -3 : ip has already vote */ protected function do_vote($poll_id, $vote_results, $vote_comment, $user_id, $ip, $lang) { $vote_resultsa=unserialize(stripslashes($vote_results)); if($user_id==2) { $vote_id = $ip; } else { $vote_id = $user_id; } $sql='INSERT INTO '.$this->tables['polls_votes']." VALUES ('".$poll_id."', '".$vote_id."', '".date('Y-m-d H:i:s')."', '".htmlspecialchars(stripslashes($vote_comment), ENT_QUOTES)."')"; if(pwg_query($sql)) { $sql="UPDATE ".$this->tables['polls']." SET total_votes = total_votes + 1, last_vote = '".date('Y-m-d H:i:s')."', visit_without_vote = visit_without_vote - 1"; if($vote_comment!="") { $sql.=", nb_comments = nb_comments + 1 "; } $sql.=" WHERE id = '".$poll_id."' "; if(pwg_query($sql)) { foreach($vote_resultsa as $key => $val) { $nb_votes=0; //first, updating "nb_votes, used for ordering result and apply frequency foreach($val as $key2 => $val2) { $vote_result=$this->analyse_answer($val2); $sql="UPDATE ".$this->tables['polls_answers']." SET nb_votes = nb_votes + ".$vote_result[2]." WHERE id_poll = '".$poll_id."' AND id_question = '".$vote_result[0]."' AND answer_num = '".$vote_result[1]."'; "; $result=pwg_query($sql); $sql="UPDATE ".$this->tables['polls_answers_lang']." SET nb_votes = nb_votes + ".$vote_result[2]." WHERE id_poll = '".$poll_id."' AND id_question = '".$vote_result[0]."' AND id_answer = '".$vote_result[1]."' AND lang = '".$lang."' "; $result=pwg_query($sql); $nb_votes+=$vote_result[2]; } //"nb_votes" updated, reload order and apply frequency $question_num=$vote_result[0]; $poll_answers=$this->get_poll_answers($poll_id, $question_num, $lang, " nb_votes ASC, ans_order ASC", true); $num=0; foreach($val as $key2 => $val2) { $vote_result=$this->analyse_answer($val2); if($vote_result[2]!=$poll_answers[$vote_result[1]]['order']) { $sql="UPDATE ".$this->tables['polls_answers']." SET frequency = frequency + 1, ans_order = '".$poll_answers[$vote_result[1]]['order']."' WHERE id_poll = '".$poll_id."' AND id_question = '".$vote_result[0]."' AND answer_num = '".$vote_result[1]."'; "; $result=pwg_query($sql); } else { $sql="UPDATE ".$this->tables['polls_answers']." SET ans_order = '".$poll_answers[$vote_result[1]]['order']."' WHERE id_poll = '".$poll_id."' AND id_question = '".$vote_result[0]."' AND answer_num = '".$vote_result[1]."'; "; $result=pwg_query($sql); } } $sql="UPDATE ".$this->tables['polls_questions']." SET nb_votes = nb_votes + ".$nb_votes." WHERE id_poll = '".$poll_id."' AND question_num = '".$vote_result[0]."' ;"; $result=pwg_query($sql); } } } } /* looks if user/ip has already make a vote return true if yes, else false */ protected function get_user_already_vote($poll_id, $user_id, $ip) { $sql="SELECT user_id FROM ".$this->tables['polls_votes']." WHERE poll_id = '".$poll_id."' AND user_id = '".$ip."' UNION SELECT user_id FROM ".$this->tables['polls_votes']." WHERE poll_id = '".$poll_id."' AND user_id = '".$user_id."'"; $result=pwg_query($sql); if($result) { if(mysql_num_rows($result)>0) { return(true); } } return(false); } /* count visit on vote's page $step= 'before' = before vote 'after' = after vote */ protected function count_visit($poll_id, $step) { $field=array('before' => 'visit_without_vote', 'after' => 'visit_after_vote'); $sql="UPDATE ".$this->tables['polls']." SET ".$field[$step]." = ".$field[$step]." + 1 WHERE id = '".$poll_id."'"; pwg_query($sql); } /* return user's group as an array */ protected function get_user_groups($user_id) { $returned=array(); $sql="SELECT group_id FROM ".USER_GROUP_TABLE." WHERE user_id = ".$user_id." "; $result=pwg_query($sql); if($result) { while($row=mysql_fetch_assoc($result)) { array_push($returned, $row['group_id']); } } return($returned); } /* --------------------------------------------------------------------------- ajax functions --------------------------------------------------------------------------- */ /* return ajax content if request is ajax else return nothing (script allowed to continue) */ public function return_ajax_content() { global $user, $ajax; if(isset($_REQUEST['ajaxfct'])) { if($_REQUEST['ajaxfct'] == 'poll_question_blocks' || $_REQUEST['ajaxfct'] == 'poll_comment_list') { switch($_REQUEST['ajaxfct']) { case 'poll_question_blocks': $result=$this->ajax_poll_question_blocks($this->extract_poll_id_from_uri(), $_REQUEST['question'], $_REQUEST['lang']); break; case 'poll_comment_list': $result=$this->ajax_poll_comment_list($this->extract_poll_id_from_uri(), $_REQUEST['vpage']); break; } $this->ajax->return_result($result); } } } protected function ajax_poll_question_blocks($poll_id, $question_num, $lang) { $local_tpl = new Template(MYPOLLS_PATH."templates/", ""); $local_tpl->set_filename('body_page', dirname($this->filelocation).'/templates/mypolls_public_question2.tpl'); $poll_values=$this->get_poll_values($poll_id, $lang); $poll_questions=$this->get_poll_questions($poll_id, "", $lang); $datas=array(); $numq=1; foreach($poll_questions as $key => $val) { if(($question_num==0)||($numq==$question_num)) { $ansnfo=""; switch($val['question_type']) { case 'multi': $ansnfo=sprintf(l10n('mypolls_answer_maxi'), $val['multi_max_answers']); break; case 'order': $ansnfo=l10n("mypolls_answer_order"); break; } $datas['question'][$numq]=array( 'NUMQ' => $numq, 'NBQ' => $poll_values['nb_questions'], 'DESCRIPTION' => html_entity_decode($val['description']), 'ANSNFO' => $ansnfo ); $poll_answers=$this->get_poll_answers($poll_id, $val['question_num'], $lang); foreach($poll_answers as $key2 => $val2) { $inputfield=""; switch($val['question_type']) { case 'mono': $inputfield=" "; break; case 'multi': $inputfield=""; break; case 'order': $inputfield=""; break; } $datas['question'][$numq]['answers'][$val2['answer_num']]=array( 'INPUTFIELD' => $inputfield, 'ANSWER' => html_entity_decode($val2['answer']) ); } } $numq++; } if(($question_num==$poll_values['nb_questions'])||($question_num==0)) { $datas['LASTQUESTION']='y'; $datas['ALLOWCOMMENT']=$poll_values['allow_comment']; } else { $datas['NEXTQUESTION']=$question_num+1; } $local_tpl->assign("datas", $datas); return($local_tpl->parse('body_page', true)); } } //class ?>