> ------------------------------------------------------------------------------ See main.inc.php for release information MyPolls_AIP : classe to manage plugin admin pages --------------------------------------------------------------------------- */ include_once('mypolls_root.class.inc.php'); include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/translate.class.inc.php'); include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/tables.class.inc.php'); include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/genericjs.class.inc.php'); include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php'); class MyPolls_AIP extends MyPolls_root { protected $google_translate; protected $tabsheet; public function MyPolls_AIP($prefixeTable, $filelocation) { parent::__construct($prefixeTable, $filelocation); $this->load_config(); $this->init_events(); $this->tabsheet = new tabsheet(); $this->tabsheet->add('polls', l10n('mypolls_polls'), $this->page_link."&f_tabsheet=polls"); $this->tabsheet->add('config', l10n('mypolls_config'), $this->page_link."&f_tabsheet=config"); // don't create this object inside root classe otherwise header is modified // everywhere in admin pages $this->ajax = new Ajax(); // don't create the google_translate object everytime, otherwise google's js // code is always included in the header even if translation functionalities // aren't needed // translation is needed only if we are in a ajax request if((isset($_POST['fmypolls_gomodify_translation'])||isset($_POST['fmypolls_goadd_translation']))) { $this->google_translate = new translate(); } } /* initialize events call for the plugin */ public function init_events() { parent::init_events(); add_event_handler('loc_end_page_header', array(&$this->css, 'apply_CSS')); } /* display administration page */ public function manage() { global $template; $this->return_ajax_content(); $template->set_filename('plugin_admin_content', dirname(__FILE__)."/admin/mypolls_admin.tpl"); if(!isset($_REQUEST['f_tabsheet'])) { $_REQUEST['f_tabsheet']='polls'; } switch($_REQUEST['f_tabsheet']) { case 'polls': $this->display_polls_page(); break; case 'config': $this->display_config_page(); break; } $this->tabsheet->select($_REQUEST['f_tabsheet']); $this->tabsheet->assign(); $selected_tab=$this->tabsheet->get_selected(); $template->assign($this->tabsheet->get_titlename(), "[".$selected_tab['caption']."]"); $template_plugin["MYPOLLS_VERSION"] = "".$this->plugin_name." ".l10n('mypolls_version').MYPOLLS_VERSION; $template_plugin["MYPOLLS_PAGE"] = $_REQUEST['f_tabsheet']; $template->assign('plugin', $template_plugin); $template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content'); } /* return ajax content */ protected function return_ajax_content() { global $ajax, $template; if(isset($_REQUEST['ajaxfct'])) { //$this->debug("AJAXFCT:".$_REQUEST['ajaxfct']); $result="

An error has occured

"; switch($_REQUEST['ajaxfct']) { case 'poll_detail_list': $result=$this->ajax_poll_detail_list($_REQUEST['poll_id']); break; case 'poll_user_list': $result=$this->ajax_poll_user_list($_REQUEST['poll_id']); break; case 'poll_comment_list': $result=$this->ajax_poll_comment_list($_REQUEST['poll_id']); break; case 'poll_delete_comment': $result=$this->ajax_poll_delete_comment($_REQUEST['poll_id']); break; } //$template-> $this->ajax->return_result($result); } } /* manage display of config page & save config */ protected function display_config_page() { $languages=get_languages(); if(!$this->adviser_abort()) { if(isset($_POST['submit_save_config'])) { foreach($this->my_config as $key => $val) { if(is_array($val)) { foreach($languages as $key2 => $val2) { if(isset($_REQUEST[str2url('f_'.$key.'_'.$key2)])) { $this->my_config[$key][$key2] = htmlspecialchars(stripslashes($_REQUEST[str2url('f_'.$key.'_'.$key2)]), ENT_QUOTES); } } } else { if(isset($_REQUEST['f_'.$key])) { $this->my_config[$key] = $_REQUEST['f_'.$key]; } } } $this->display_result(l10n('mypolls_save_config'), $this->save_config()); } } $this->display_config(); } /* manage display of polls pages actions : - list - create - view - modify/delete */ protected function display_polls_page() { //default action if not specified into URL's page if(!isset($_REQUEST['action'])) { $_REQUEST['action']='list'; } if(!isset($_REQUEST['fmypolls_att_id'])) { $_REQUEST['fmypolls_att_id']=''; } if(($_REQUEST['action']!='create') and ($_REQUEST['fmypolls_att_id']=='') ) { $_REQUEST['action']='list'; } if(!isset($_REQUEST['mypolls_step'])) { $_REQUEST['mypolls_step']=0; } if(!isset($_REQUEST['mypolls_lang'])) { $_REQUEST['mypolls_lang']=""; } //$this->debug('action:'.$_REQUEST['action']); //$this->debug('fmypolls_att_id:'.$_REQUEST['fmypolls_att_id']); //$this->debug('step:'.$_REQUEST['mypolls_step']); switch($_REQUEST['action']) { case 'list': $this->display_list(); break; case 'create': if(isset($_POST['fmypolls_create_new_poll'])) { if($this->adviser_abort()) { $this->display_list(); break; } $poll_id=$this->do_create_poll(0); $this->display_result(l10n('mypolls_msg_create_new_poll'), $poll_id); $this->display_cm_page0($this->init_poll_array($poll_id), 'modify', $poll_id); } else { $this->display_cm_page0(); } break; case 'view': $this->display_view($_REQUEST['fmypolls_att_id'], $_REQUEST['mypolls_lang']); break; case 'modify': case 'adding': if(isset($_POST['fmypolls_modify_poll'])) { if($this->adviser_abort()) { $this->display_list(); break; } //modify a poll $result=$this->do_modify_poll(0, $_REQUEST['fmypolls_att_id']); $this->display_result(l10n('mypolls_msg_modify_poll'), $result); //go back to poll's main page in 'modify' mode $this->display_cm_page0($this->init_poll_array($_REQUEST['fmypolls_att_id']), 'modify', $_REQUEST['fmypolls_att_id']); } elseif(isset($_POST['fmypolls_delete_poll'])) { if($this->adviser_abort()) { $this->display_list(); break; } //delete a poll $result=$this->do_delete_poll($_REQUEST['fmypolls_att_id']); $this->display_result(l10n('mypolls_msg_delete_poll'), $result); //go back to list $this->display_list(); } elseif(isset($_POST['fmypolls_add_question'])) { if($this->adviser_abort()) { $this->display_list(); break; } //add a question on poll $result=$this->do_create_poll($_REQUEST['mypolls_step']); $this->display_result(l10n('mypolls_msg_add_question'), $result); //go back to poll's main page in 'modify' mode $this->display_cm_page0($this->init_poll_array($_REQUEST['fmypolls_att_id']), 'modify', $_REQUEST['fmypolls_att_id']); } elseif(isset($_POST['fmypolls_modify_question'])) { if($this->adviser_abort()) { $this->display_list(); break; } //modify a poll's question $result=$this->do_modify_poll($_REQUEST['mypolls_step'], $_REQUEST['fmypolls_att_id']); $this->display_result(l10n('mypolls_msg_modify_question'), $result); //go back to poll's main page in 'modify' mode $this->display_cm_page0($this->init_poll_array($_REQUEST['fmypolls_att_id']), 'modify', $_REQUEST['fmypolls_att_id']); } elseif(isset($_POST['fmypolls_delete_question'])) { if($this->adviser_abort()) { $this->display_list(); break; } //delete a poll's question $result=$this->do_delete_question($_REQUEST['mypolls_step'], $_REQUEST['fmypolls_att_id']); $this->display_result(l10n('mypolls_msg_delete_question'), $result); //go back to poll's main page in 'modify' mode $this->display_cm_page0($this->init_poll_array($_REQUEST['fmypolls_att_id']), 'modify', $_REQUEST['fmypolls_att_id']); } elseif(isset($_POST['fmypolls_add_translation'])) { if($this->adviser_abort()) { $this->display_list(); break; } //add a translation on poll $result=$this->do_translate_poll($_REQUEST['fmypolls_att_id'], 'adding'); $this->display_result(l10n('mypolls_msg_add_translation'), $result); //go back to poll's main page in 'modify' mode $this->display_cm_page0($this->init_poll_array($_REQUEST['fmypolls_att_id']), 'modify', $_REQUEST['fmypolls_att_id']); } elseif(isset($_POST['fmypolls_modify_translation'])) { if($this->adviser_abort()) { $this->display_list(); break; } //delete a poll's question $result=$this->do_translate_poll($_REQUEST['fmypolls_att_id'], 'modify'); $this->display_result(l10n('mypolls_msg_modify_translation'), $result); //go back to poll's main page in 'modify' mode $this->display_cm_page0($this->init_poll_array($_REQUEST['fmypolls_att_id']), 'modify', $_REQUEST['fmypolls_att_id']); } elseif(isset($_POST['fmypolls_delete_translation'])) { if($this->adviser_abort()) { $this->display_list(); break; } //delete a poll's question $result=$this->do_delete_poll_translation($_REQUEST['fmypolls_att_id'], $_REQUEST['fmypolls_trans_to']); $this->display_result(l10n('mypolls_msg_delete_translation'), $result); //go back to poll's main page in 'modify' mode $this->display_cm_page0($this->init_poll_array($_REQUEST['fmypolls_att_id']), 'modify', $_REQUEST['fmypolls_att_id']); } elseif(isset($_POST['fmypolls_gomodify_question'])) { $ansid=explode(".", $_REQUEST['mypolls_step']); //go to poll's question page in 'modify' mode $this->display_cm_pagen($_REQUEST['mypolls_step'], $_REQUEST['fmypolls_att_id'], $this->init_poll_question_array($_REQUEST['fmypolls_att_id'], $ansid[0]), 'modify'); } elseif(isset($_POST['fmypolls_goadd_question'])) { //go to poll's question page in 'adding' mode $this->display_cm_pagen($_REQUEST['mypolls_step'], $_REQUEST['fmypolls_att_id'], array(), 'adding'); } elseif(isset($_POST['fmypolls_gomodify_translation'])) { //go to poll's translation page in 'modify' mode $this->display_translation_page($_REQUEST['fmypolls_att_id'], $_REQUEST['fmypolls_translation_id'],'modify'); } elseif(isset($_POST['fmypolls_goadd_translation'])) { //go to poll's translation page in 'modify' mode $this->display_translation_page($_REQUEST['fmypolls_att_id'], '', 'adding'); } else { //go to poll's main page in 'modify' mode $this->display_cm_page0($this->init_poll_array($_REQUEST['fmypolls_att_id']), 'modify', $_REQUEST['fmypolls_att_id']); } break; } } /* display list page of polls and allow to : - view a poll - modify a poll - create a new poll */ protected function display_list() { global $template, $user; $template->set_filename('body_page', dirname($this->filelocation).'/admin/plugin_admin_list.tpl'); $polls_list=$this->get_polls_list(1,true); if(count($polls_list)>1) { $nb='n'; } else { $nb=count($polls_list); } $template_datas=array(); $template_datarows=array(); for($i=0;$i0) { $last_vote = $polls_list[$i]['last_vote']; } else { $last_vote = ""; } $template_datarows[]=array( 'ID' => $polls_list[$i]['id'], 'DATE' => substr($polls_list[$i]['date'],0,10), 'TITLE' => $polls_list[$i]['title'], 'NBQUESTIONS' => $polls_list[$i]['nb_questions'], 'VISIBLE' => l10n('mypolls_yesno_'.$polls_list[$i]['visible']), 'ENABLED' => l10n('mypolls_yesno_'.$polls_list[$i]['enabled']), 'TOTAL' => $polls_list[$i]['total_votes'], 'LAST_VOTE' => $last_vote, 'LNK_VIEW' => $this->page_link. '&f_tabsheet=polls&action=view&fmypolls_att_id='. $polls_list[$i]['id'], 'LNK_MODIFY' => $this->page_link. '&f_tabsheet=polls&action=modify&fmypolls_att_id='. $polls_list[$i]['id'] ); } $template_datas=array( "MYPOLLS_TITLE" => l10n('mypolls_title_list'), "MYPOLLS_NB_POLLS" => sprintf(l10n('mypolls_number_of_polls'.$nb), count($polls_list)), "LNK_CREATE" => $this->page_link.'&f_tabsheet=polls&action=create', "MYPOLLS_AJAX_URL_POLL_DETAIL" => $this->page_link.'&ajaxfct=poll_detail_list&poll_id=', "MYPOLLS_CHARSET" => substr($user['language'],6) ); $template->assign("datas", $template_datas); $template->assign("mypolls_nb_polls_rows", $template_datarows); $template->assign_var_from_handle('MYPOLLS_BODY_PAGE', 'body_page'); } /* display first page to create/modify a poll (poll's attributes) page can be acceded : - when poll is created (no default_values) - when poll is created but with an error (default_values are form's values) - when poll is modified (default_values are database values) - when poll is modified but with an error (default_values are form's values) $access_type = "create", "modify" */ protected function display_cm_page0($default_values=array(), $access_type = "create", $poll_id=0) { global $template, $user, $page; $template->set_filename('body_page', dirname($this->filelocation).'/admin/plugin_admin_create.tpl'); $template_datas=array(); if($access_type=='create') { $template_datas['MYPOLLS_ICN_HIDDEN']='visibility:hidden;'; $template_datas['MYPOLLS_NEXT_STEP']='0.1'; $template_datas['MYPOLLS_ENABLED']=' disabled'; } else { $template_datas['MYPOLLS_LANG_DISABLED']='disabled'; $template_datas['MYPOLLS_LNK_VIEW'] = $this->page_link.'&f_tabsheet=polls&action=view&fmypolls_att_id='.$poll_id; $template_datas['MYPOLLS_NEXT_STEP'] = ($default_values['counter_questions']+1).".".($default_values['nb_questions']+1); //check if number of questions/answers is the same for each language $sql="SELECT count(id_answer), lang FROM ".$this->tables['polls_answers_lang']." WHERE id_poll = '".$poll_id."' GROUP BY lang"; $result=pwg_query($sql); if($result) { $nb=-1; while($row=mysql_fetch_row($result)) { if($nb<0) { $nb=$row[0]; } elseif($nb!=$row[0]) { $nb=-2; array_push($page['errors'], l10n('mypolls_error_on_tranlsation_number')); break; } } } if(($default_values['nb_questions']<1)||($nb==-2)) { $template_datas['MYPOLLS_ENABLED']= ' disabled'; $default_values['enabled']='n'; $this->deactivate_poll($poll_id); } } $template_datas['MYPOLLS_ACTION']= $access_type; //no default value, so initialization if(count($default_values)==0) { $allowed_users=""; if($this->my_config['mypolls_allowed_users_default_guest']=='y') { $allowed_users.="guest/"; } if($this->my_config['mypolls_allowed_users_default_generic']=='y') { $allowed_users.="generic/"; } if($this->my_config['mypolls_allowed_users_default_normal']=='y') { $allowed_users.="normal/"; } $default_values=array( 'default_lang' => $user['language'], 'title' => '', 'description' => '', 'after_vote_text' => '', 'allowed_users' => $allowed_users, 'allowed_groups' => $this->my_config['mypolls_allowed_groups_default'], 'enabled' => 'n', 'visible' => $this->my_config['mypolls_visible_default'], 'allow_comment' => $this->my_config['mypolls_allow_comment_default'], 'show_comments' => $this->my_config['mypolls_show_user_comment_default'], 'display_type' => $this->my_config['mypolls_display_type_default'], 'color_bars' => $this->my_config['mypolls_color_bars'], 'public_results' => $this->my_config['mypolls_public_results'] ); } $template_datas['MYPOLLS_ATT_TITLE'] = $default_values['title']; $template_datas['MYPOLLS_ATT_DESCRIPTION'] = $default_values['description']; $template_datas['MYPOLLS_ATT_AFTER_VOTE_TEXT'] = $default_values['after_vote_text']; //users and groups access management $users = new users($default_values['allowed_users']); $template_datas['GENERIC_USERS_ACCESS'] = $users->html_form('fmypolls_att_allowed_users_'); $groups = new groups($default_values['allowed_groups']); $template_datas['GENERIC_GROUPS_ACCESS'] = $groups->html_form('fmypolls_att_allowed_groups_'); //poll's main language selection $template_language_list_values=array(); $template_language_list_labels=array(); $template_language_selected=$default_values['default_lang']; $languages=get_languages(); foreach($languages as $key => $val) { $template_language_list_values[]=$key; $template_language_list_labels[]=$val; } //poll's type of display (one page for all question / a page per question) $template_display_list_values=array(); $template_display_list_labels=array(); $template_display_selected=$default_values['display_type']; for($i=0;$idisplay_types);$i++) { $template_display_list_values[]=$this->display_types[$i]; $template_display_list_labels[]=l10n('mypolls_display_'.$this->display_types[$i]); } if($default_values['enabled']=='y') { $template_datas['MYPOLLS_ATT_ENABLED_YES'] = 'selected'; } else { $template_datas['MYPOLLS_ATT_ENABLED_NO'] = 'selected'; } if($default_values['visible']=='y') { $template_datas['MYPOLLS_ATT_VISIBLE_YES'] = 'selected'; } else { $template_datas['MYPOLLS_ATT_VISIBLE_NO'] = 'selected'; } if($default_values['allow_comment']=='y') { $template_datas['MYPOLLS_ATT_ALLOW_COMMENT_YES'] = 'selected'; } else { $template_datas['MYPOLLS_ATT_ALLOW_COMMENT_NO'] = 'selected'; } if($default_values['show_comments']=='y') { $template_datas['MYPOLLS_ATT_SHOW_USER_COMMENT_YES'] = 'selected'; } else { $template_datas['MYPOLLS_ATT_SHOW_USER_COMMENT_NO'] = 'selected'; } if($default_values['public_results']=='y') { $template_datas['MYPOLLS_ATT_PUBLIC_RESULTS_YES'] = 'selected'; } else { $template_datas['MYPOLLS_ATT_PUBLIC_RESULTS_NO'] = 'selected'; } $template_datas['fmypolls_att_color_bars'] = $default_values['color_bars']; if($access_type=='create') { $template_datas['mypolls_en_creation']='yes'; } else { $nbquestions = $default_values['nb_questions']." "; $poll_languages=$this->get_poll_languages($poll_id); if($default_values['nb_questions']>1) { $nbquestions.=l10n('mypolls_cm_questions'); } else { $nbquestions.=l10n('mypolls_cm_question'); } //modify poll, display specifics block for actions $template_datas['mypolls_en_modification']='yes'; $template_datas['ID'] = $poll_id; $template_datas['NBQUESTIONS'] = $nbquestions; $template_datas['DISABLE_ADD_TRANS'] = (count($poll_languages)==count($languages))?' disabled':''; // block : questions $template_questions_list_values=array(); $template_questions_list_labels=array(); $num=1; $questions=$this->get_poll_questions($poll_id); foreach($questions as $val) { if(strlen($val['description'])>37) { $text=substr($val['description'],0,34)."..."; } else { $text=$val['description']; } $template_questions_list_values[] = $val['question_num'].".".$num; $template_questions_list_labels[] = "[".$num."] ".$text; $num++; } // block : translations $template_translations_list_values=array(); $template_translations_list_labels=array(); foreach($poll_languages as $val) { if($val != $default_values['default_lang']) { $template_translations_list_values[]=$val; $template_translations_list_labels[]=$languages[$val]; } } $template->assign('mypolls_questions_list_values', $template_questions_list_values); $template->assign('mypolls_questions_list_labels', $template_questions_list_labels); $template->assign('mypolls_translations_list_values', $template_translations_list_values); $template->assign('mypolls_translations_list_labels', $template_translations_list_labels); } $template_datas["MYPOLLS_TITLE"] = l10n('mypolls_title_'.$access_type); $template->assign('mypolls_language_list_values', $template_language_list_values); $template->assign('mypolls_language_list_labels', $template_language_list_labels); $template->assign('mypolls_language_selected', $template_language_selected); $template->assign('mypolls_display_list_values', $template_display_list_values); $template->assign('mypolls_display_list_labels', $template_display_list_labels); $template->assign('mypolls_display_selected', $template_display_selected); $template->assign('datas', $template_datas); $template->assign_var_from_handle('MYPOLLS_BODY_PAGE', 'body_page'); } //display_cm_page0 /* display other pages to create/modify a poll (poll's questions) page can be acceded : - when poll is created (no default_values) - when poll is created but with an error (default_values are form's values) - when poll is modified (default_values are database values) - when poll is modified but with an error (default_values are form's values) $access_type = "create", "modify" */ protected function display_cm_pagen($step, $poll_id, $default_values=array(), $access_type = "create") { global $template; $template->set_filename('body_page', dirname($this->filelocation).'/admin/plugin_admin_create2.tpl'); //$step format : id.num $steps=explode(".", $step); $template_datas["MYPOLLS_TITLE"] = l10n('mypolls_title_'.$access_type)." - ".l10n('mypolls_cm_question')." ".$steps[1]; $template_datas['MYPOLLS_STEP_VALUE'] = $steps[0]; $template_datas['MYPOLLS_ATT_ID'] = $poll_id; if($access_type=='create') { $template_datas['MYPOLLS_ICN_HIDDEN'] = 'visibility:hidden;'; } else { $template_datas['MYPOLLS_LNK_VIEW'] = $this->page_link.'&f_tabsheet=polls&action=view&fmypolls_att_id='.$poll_id; $template_datas['MYPOLLS_LNK_MODIFY'] = $this->page_link.'&f_tabsheet=polls&action=modify&fmypolls_att_id='.$poll_id; } $template_datas['MYPOLLS_ACTION'] = $access_type; if(count($default_values)==0) { $default_values=array( 'default_lang' => $_REQUEST['fmypolls_att_default_lang'], 'description' => '', 'question_type' => $this->my_config['mypolls_question_type_default'], 'multi_max_answers' => $this->my_config['mypolls_multi_max_answers_default'], 'answ0' => '', 'answ1' => '', 'counter_answers' => '1' ); } $template_datas['MYPOLLS_ATT_DESCRIPTION'] = $default_values['description']; $template_datas['MYPOLLS_ATT_MULTI_MAX_ANSWERS'] = $default_values['multi_max_answers']; $template_datas['MYPOLLS_NEXT_QUESTION'] = $default_values['counter_answers']+1; //poll's main language selection $languages=get_languages(); $template_datas['MYPOLLS_ATT_LANG'] = $default_values['default_lang']; $template_language_list_values=array(); $template_language_list_labels=array(); $template_language_list_values[]=$default_values['default_lang']; $template_language_list_labels[]=$languages[$default_values['default_lang']]; $template_language_selected[]=$default_values['default_lang']; //poll's type of display (one page for all question / a page per question) $template_question_type_list_values=array(); $template_question_type_list_labels=array(); $template_question_type_selected=$default_values['question_type']; for($i=0;$iquestions_types);$i++) { $template_question_type_list_values[]=$this->questions_types[$i]; $template_question_type_list_labels[]=l10n('mypolls_question_'.$this->questions_types[$i]); } //poll's answers $init=""; foreach($default_values as $key => $val) { if(substr($key,0,4)=="answ") { $init.="add_answer('".$val."', ".substr($key,4).");"; } } $template_datas['MYPOLLS_INIT_ANSWERS_LIST'] = $init; if($access_type=='create') { $template_datas['mypolls_en_creation'] = 'yes'; } elseif($access_type=='modify') { //modify poll, display specifics block for actions $template_datas['mypolls_en_modification'] = 'yes'; } elseif($access_type=='adding') { //add a question (modify poll)), display specifics block for actions $template_datas['mypolls_adding_question'] = 'yes'; } $template->assign('datas', $template_datas); $template->assign('mypolls_language_list_values', $template_language_list_values); $template->assign('mypolls_language_list_labels', $template_language_list_labels); $template->assign('mypolls_language_selected', $template_language_selected); $template->assign('mypolls_question_type_list_values', $template_question_type_list_values); $template->assign('mypolls_question_type_list_labels', $template_question_type_list_labels); $template->assign('mypolls_question_type_selected', $template_question_type_selected); $template->assign_var_from_handle('MYPOLLS_BODY_PAGE', 'body_page'); } //display_cm_pagen /* display translation page */ protected function display_translation_page($poll_id, $lang="", $access_type="adding") { global $template, $user, $page; $template->set_filename('body_page', dirname($this->filelocation).'/admin/plugin_admin_translate.tpl'); if($access_type=="adding") { } else { $template_datas['MYPOLLS_LANG_DISABLED'] = ' disabled'; } $poll_langs=$this->get_poll_languages($poll_id); $poll_dlang=$this->get_poll_values($poll_id); $poll_tolang=$this->get_poll_values($poll_id, $lang); $languages=get_languages(); $template_langto_list_values=array(); $template_langto_list_labels=array(); $template_langto_selected=$lang; // initialize "to lang" list foreach($languages as $key => $val) { if($key!=$poll_dlang['default_lang']) { if((($access_type=="adding")&&(!in_array($key, $poll_langs)))||($access_type=="modify")) { $template_langto_list_values[]=$key; $template_langto_list_labels[]=$val; } } } $questions_dlang=$this->get_poll_questions($poll_id); $questions_tolang=$this->get_poll_questions($poll_id, "", $lang); $numq=1; $template_questions_list=array(); foreach($questions_dlang as $key => $val) { $template_answers_list=array(); $numa=1; $answers_dlang=$this->get_poll_answers($poll_id, $val['question_num']); $answers_tolang=$this->get_poll_answers($poll_id, $val['question_num'], $lang); foreach($answers_dlang as $key2 => $val2) { $template_answers_list[]=array( 'NUMANSWER' => $numa, 'IDANSWER' => $val['question_num']."_".$val2['answer_num'], //from lang 'ANSWER' => $val2['answer'], //to lang 'TTO_ANSWER' => isset($answers_tolang[$key2]['answer'])?$answers_tolang[$key2]['answer']:$val2['answer'] ); $numa++; } $template_questions_list[]=array( 'NUMQUESTION' => $numq, 'IDQUESTION' => $val['question_num'], //from lang 'DESCRIPTION' => $val['description'], //to lang 'TTO_DESCRIPTION' => isset($questions_tolang[$key]['description'])?$questions_tolang[$key]['description']:$val['description'], 'answers' => $template_answers_list ); $numq++; } if($access_type=='adding') { $template_datas['mypolls_add_translation'] = 'yes'; } elseif($access_type=='modify') { $template_datas['mypolls_modify_translation'] = 'yes'; } $template_datas["MYPOLLS_TITLE"] = l10n('mypolls_title_translation_'.$access_type); $template_datas['MYPOLLS_ATT_ID'] = $poll_id; $template_datas['MYPOLLS_ACTION'] = $access_type; $template_datas['MYPOLLS_LNK_VIEW'] = $this->page_link.'&f_tabsheet=polls&action=view&fmypolls_att_id='.$poll_id; $template_datas['MYPOLLS_LNK_MODIFY'] = $this->page_link.'&f_tabsheet=polls&action=modify&fmypolls_att_id='.$poll_id; $template_datas['MYPOLLS_CHARSET'] = substr($user['language'], 6); $template_datas['MYPOLLS_ATT_TITLE'] = $poll_dlang['title']; $template_datas['MYPOLLS_ATT_DESCRIPTION'] = $poll_dlang['description']; $template_datas['MYPOLLS_ATT_AFTER_VOTE_TEXT'] = $poll_dlang['after_vote_text']; $template_datas['MYPOLLS_TRANS_FROM'] = $poll_dlang['default_lang']; $template_datas['MYPOLLS_TRANS_FROM_TEXT'] = $languages[$poll_dlang['default_lang']]; $template_datas['MYPOLLS_ATT_TTO_TITLE'] = $poll_tolang['title']; $template_datas['MYPOLLS_ATT_TTO_DESCRIPTION'] = $poll_tolang['description']; $template_datas['MYPOLLS_ATT_TTO_AFTER_VOTE_TEXT'] = $poll_tolang['after_vote_text']; $template->assign('datas', $template_datas); $template->assign('mypolls_langto_list_values', $template_langto_list_values); $template->assign('mypolls_langto_list_labels', $template_langto_list_labels); $template->assign('mypolls_langto_selected', $template_langto_selected); $template->assign('questions', $template_questions_list); $template->assign_var_from_handle('MYPOLLS_BODY_PAGE', 'body_page'); }//display_translation_page /* display view page */ protected function display_view($poll_id, $lang="") { global $template; $template->set_filename('body_page', dirname($this->filelocation).'/admin/plugin_admin_view.tpl'); $poll_langs=$this->get_poll_languages($poll_id); $poll_values=$this->get_poll_values($poll_id, $lang); $languages=get_languages(); if($lang=="") { $lang=$poll_values['default_lang']; } $template_langs_list_values=array(); $template_langs_list_labels=array(); $template_langs_selected=$lang; // initialize "view langs" list foreach($poll_langs as $key => $val) { $template_langs_list_values[]=$val; $template_langs_list_labels[]=$languages[$val]; } $users=new users($poll_values['allowed_users']); $users->set_allowed('admin', false); $groups=new groups($poll_values['allowed_groups']); $this->display_results_block($poll_id, $lang, $poll_values); $template_datas['MYPOLLS_TITLE'] = l10n('mypolls_title_view'); $template_datas['MYPOLLS_ATT_ID'] = $poll_id; $template_datas['MYPOLLS_LNK_MODIFY'] = $this->page_link.'&f_tabsheet=polls&action=modify&fmypolls_att_id='.$poll_id; $template_datas['MYPOLLS_AJAX_URL_POLL_VOTER_LIST'] = $this->page_link.'&ajaxfct=poll_user_list&poll_id='.$poll_id.'&vpage='; $template_datas['MYPOLLS_AJAX_URL_POLL_COMMENT_LIST'] = $this->page_link.'&ajaxfct=poll_comment_list&poll_id='.$poll_id.'&vpage='; $template_datas['MYPOLLS_AJAX_URL_POLL_DELETE_COMMENT'] = $this->page_link.'&ajaxfct=poll_delete_comment&poll_id='.$poll_id.'&comment_id='; $template_datas['MYPOLLS_CHARSET'] = substr($lang, 6); $template_datas['MYPOLLS_ATT_TITLE'] = $poll_values['title']; $template_datas['MYPOLLS_ATT_DESCRIPTION'] = html_entity_decode($poll_values['description'], ENT_QUOTES); $template_datas['MYPOLLS_ATT_AFTER_VOTE_TEXT'] = html_entity_decode($poll_values['after_vote_text'], ENT_QUOTES); $template_datas['MYPOLLS_ATT_DATE'] = $poll_values['date']; $template_datas['MYPOLLS_ATT_ENABLED'] = l10n('mypolls_yesno_'.$poll_values['enabled']); $template_datas['MYPOLLS_ATT_VISIBLE'] = l10n('mypolls_yesno_'.$poll_values['visible']); $template_datas['MYPOLLS_ATT_ALLOW_COMMENT'] = l10n('mypolls_yesno_'.$poll_values['allow_comment']); $template_datas['MYPOLLS_ATT_SHOW_COMMENTS'] = l10n('mypolls_yesno_'.$poll_values['show_comments']); $template_datas['MYPOLLS_ATT_ALLOWED_USERS'] = $users->html_view(", ", l10n('mypolls_allowed_users_empty')); $template_datas['MYPOLLS_ATT_ALLOWED_GROUPS'] = $groups->html_view(", ", l10n('mypolls_allowed_groups_empty')); $template_datas['MYPOLLS_ATT_NB_QUESTIONS'] = $poll_values['nb_questions']; $template_datas['MYPOLLS_ATT_TOTAL_VOTES'] = $poll_values['total_votes']; $template_datas['MYPOLLS_ATT_LAST_VOTE'] = ($poll_values['last_vote']=="0000-00-00 00:00:00")?l10n('mypolls_result_no_vote'):$poll_values['last_vote']; $template_datas['MYPOLLS_ATT_VISIT_WITHOUT_VOTE'] = $poll_values['visit_without_vote']; $template_datas['MYPOLLS_ATT_VISIT_AFTER_VOTE'] = $poll_values['visit_after_vote']; $template_datas['MYPOLLS_ATT_DISPLAY_TYPE'] = strtolower(l10n('mypolls_display_'.$poll_values['display_type'])); $template_datas['MYPOLLS_ATT_PUBLIC_RESULTS'] = $poll_values['public_results']; $template->assign('datas', $template_datas); $template->assign('mypolls_langs_list_values', $template_langs_list_values); $template->assign('mypolls_langs_list_labels', $template_langs_list_labels); $template->assign('mypolls_langs_selected', $template_langs_selected); $template->assign_var_from_handle('MYPOLLS_BODY_PAGE', 'body_page'); } //display_view /* display config page */ protected function display_config() { global $template; $template->set_filename('body_page', dirname($this->filelocation).'/admin/plugin_admin_config.tpl'); //poll's type of display (one page for all question / a page per question) $template_display_list_values=array(); $template_display_list_labels=array(); $template_display_selected=$this->my_config['mypolls_display_type_default']; foreach($this->display_types as $val) { $template_display_list_values[]=$val; $template_display_list_labels[]=l10n('mypolls_display_'.$val); } //langs list & texts $template_language_list=array(); $template_language_list_values=array(); $template_language_list_labels=array(); $template_language_selected=str2url(get_default_language()); $languages=get_languages(); foreach($languages as $key => $val) { $template_language_list_values[]=str2url($key); $template_language_list_labels[]=$val; $template_language_list[]=array( 'LANG' => str2url($key), 'MENUBARTIT' => $this->my_config['mypolls_menubar_title'][$key], 'GENTEXUSER' => $this->my_config['mypolls_generic_text_users'][$key], 'GENTEXGUEST' => $this->my_config['mypolls_generic_text_guests'][$key], 'NOTPUBRESTEX' => $this->my_config['mypolls_not_public_results'][$key] ); } $template_datas["MYPOLLS_TITLE"] = l10n('mypolls_title_config'); $template_datas['MYPOLLS_CFG_COLOR_BARS'] = $this->my_config['mypolls_color_bars']; $template_datas['MYPOLLS_CFG_MAXBARWIDTH'] = $this->my_config['mypolls_maxbarwidth']; $template_datas['MYPOLLS_CFG_ADMINCOLOR'] = $this->my_config['mypolls_admincolor']; $template_datas['MYPOLLS_CFG_MOUSEOVERCOLOR'] = $this->my_config['mypolls_mouseovercolor']; $template_datas['MYPOLLS_CFG_NBUSER_PER_PAGE'] = $this->my_config['mypolls_nbuser_per_page']; $template_datas['MYPOLLS_CFG_NBCOMMENT_PER_PAGE'] = $this->my_config['mypolls_nbcomment_per_page']; $template->assign('datas', $template_datas); $template->assign('mypolls_language_list', $template_language_list); $template->assign('mypolls_language_list_values', $template_language_list_values); $template->assign('mypolls_language_list_labels', $template_language_list_labels); $template->assign('mypolls_language_selected', $template_language_selected); $template->assign('mypolls_display_list_values', $template_display_list_values); $template->assign('mypolls_display_list_labels', $template_display_list_labels); $template->assign('mypolls_display_selected', $template_display_selected); $template->assign_var_from_handle('MYPOLLS_BODY_PAGE', 'body_page'); } //display_config /* manage adviser profile return true if user is adviser */ protected function adviser_abort() { if(is_adviser()) { $this->display_result(l10n("mypolls_adviser_not_allowed"), false); return(true); } return(false); } /* deactivate a poll */ protected function deactivate_poll($poll_id) { $sql="UPDATE ".$this->tables['polls']." SET enabled = 'n' WHERE id = '".$poll_id."'"; $result=pwg_query($sql); return($result); } /* create a poll if $page = 0, create general nfo else create question */ protected function do_create_poll($step) { if($step==0) { //step 0 : create root caracteristics of poll $groups = new groups(); $users = new users(); foreach($_REQUEST as $key => $val) { if((substr($key, 0, 28)=='fmypolls_att_allowed_groups_') and ($val=="on")) { $groups->set_allowed(substr($key,28),true); } if((substr($key, 0, 27)=='fmypolls_att_allowed_users_') and ($val=="on")) { $users->set_allowed(substr($key,27),true); } } $values = array( 'allowed_users' => $users->get_alloweds('id'), 'allowed_groups' => $groups->get_alloweds('id'), 'visible' => $_REQUEST['fmypolls_att_visible'], 'enabled' => $_REQUEST['fmypolls_att_enabled'], 'allow_comment' => $_REQUEST['fmypolls_att_allow_comment'], 'show_comments' => $_REQUEST['fmypolls_att_show_comments'], 'color_bars' => $_REQUEST['fmypolls_att_color_bars'], 'display_type' => $_REQUEST['fmypolls_att_display_type'], 'default_lang' => $_REQUEST['fmypolls_att_default_lang'], 'public_results' => $_REQUEST['fmypolls_att_public_results'] ); $poll_id=$this->create_poll($values); if($poll_id>0) { $values = array( 'lang' => $_REQUEST['fmypolls_att_default_lang'], 'title' => htmlspecialchars(stripslashes($_REQUEST['fmypolls_att_title']), ENT_QUOTES), 'description' => htmlspecialchars(stripslashes($_REQUEST['fmypolls_att_description']), ENT_QUOTES), 'after_vote_text' => htmlspecialchars(stripslashes($_REQUEST['fmypolls_att_after_vote_text']), ENT_QUOTES), ); if($this->create_poll_lang($poll_id, $values)) { return($poll_id); } } } else { //step >0 : create questions $poll_id=$_REQUEST['fmypolls_att_id']; $values = array( 'id_poll' => $poll_id, 'question_num' => $step, 'question_type' => $_REQUEST['fmypolls_att_question_type'], 'multi_max_answers' => isset($_REQUEST['fmypolls_att_multi_max_answers']) ? $_REQUEST['fmypolls_att_multi_max_answers'] : '0' ); if($this->create_poll_questions($values)) { $values = array( 'id_poll' => $poll_id, 'id_question' => $step, 'lang' => $_REQUEST['fmypolls_att_default_lang'], 'description' => htmlspecialchars(stripslashes($_REQUEST['fmypolls_att_description']), ENT_QUOTES) ); if($this->create_poll_questions_lang($values)) { foreach($_REQUEST as $key => $val) { if((substr($key, 0, 17)=='fmypolls_att_answ') and ($val!='')) { $values = array( 'id_poll' => $poll_id, 'id_question' => $step, 'answer_num' => substr($key,17), '0' ); if($this->create_poll_answers($values)) { $values = array( 'id_poll' => $poll_id, 'id_question' => $step, 'id_answer' => substr($key,17), 'lang' => $_REQUEST['fmypolls_att_default_lang'], 'answer' => htmlspecialchars(stripslashes($val), ENT_QUOTES), 'nb_votes' => '0' ); if(!$this->create_poll_answers_lang($values)) { return(false); } } } } } return($poll_id); } } return(false); } //do_create_poll /* modify a poll if $page = 0, modify general nfo else modify question */ protected function do_modify_poll($step, $poll_id) { if($step==0) { //step 0 : modify root caracteristics of poll $groups = new groups(); $users = new users(); foreach($_REQUEST as $key => $val) { if((substr($key, 0, 28)=='fmypolls_att_allowed_groups_') and ($val=="on")) { $groups->set_allowed(substr($key,28),true); } if((substr($key, 0, 27)=='fmypolls_att_allowed_users_') and ($val=="on")) { $users->set_allowed(substr($key,27),true); } } $values = array( 'allowed_users' => $users->get_alloweds('id'), 'allowed_groups' => $groups->get_alloweds('id'), 'visible' => $_REQUEST['fmypolls_att_visible'], 'enabled' => $_REQUEST['fmypolls_att_enabled'], 'allow_comment' => $_REQUEST['fmypolls_att_allow_comment'], 'show_comments' => $_REQUEST['fmypolls_att_show_comments'], 'color_bars' => $_REQUEST['fmypolls_att_color_bars'], 'display_type' => $_REQUEST['fmypolls_att_display_type'], 'public_results' => $_REQUEST['fmypolls_att_public_results'] ); $result=$this->modify_poll($poll_id, $values); if($result>0) { $values = array( 'title' => htmlspecialchars(stripslashes($_REQUEST['fmypolls_att_title']), ENT_QUOTES), 'description' => htmlspecialchars(stripslashes($_REQUEST['fmypolls_att_description']), ENT_QUOTES), 'after_vote_text' => htmlspecialchars(stripslashes($_REQUEST['fmypolls_att_after_vote_text']), ENT_QUOTES) ); if($this->modify_poll_lang($poll_id, $_REQUEST['fmypolls_att_default_lang'], $values)) { return($poll_id); } } } else { //step >0 :modify questions $poll_id=$_REQUEST['fmypolls_att_id']; $values = array( 'question_type' => $_REQUEST['fmypolls_att_question_type'], 'multi_max_answers' => isset($_REQUEST['fmypolls_att_multi_max_answers']) ? $_REQUEST['fmypolls_att_multi_max_answers'] : '0' ); if($this->modify_poll_questions($poll_id, $step, $values)) { $values = array( 'description' => htmlspecialchars(stripslashes($_REQUEST['fmypolls_att_description']), ENT_QUOTES) ); if($this->modify_poll_questions_lang($poll_id, $step, $_REQUEST['fmypolls_att_default_lang'], $values)) { foreach($_REQUEST as $key => $val) { if((substr($key, 0, 17)=='fmypolls_att_answ')) { if($val=='') { if($this->delete_poll_answers($poll_id, $step, substr($key,17))) { if(!$this->delete_poll_answers_lang($poll_id, $step, substr($key,17))) { return(false); } } } else { $values = array( 'answer' => htmlspecialchars(stripslashes($val), ENT_QUOTES) ); if(!$this->modify_poll_answers_lang($poll_id, $step, substr($key,17), $_REQUEST['fmypolls_att_default_lang'], $values)) { return(false); } } } } } return($poll_id); } } return(false); } //do_modify_poll /* delete a poll's question */ protected function do_delete_question($step, $poll_id) { if($this->delete_poll_questions($poll_id, $step)) { if($this->delete_poll_questions_lang($poll_id, $step)) { if($this->delete_poll_answers($poll_id, $step)) { if($this->delete_poll_answers_lang($poll_id, $step)) { return($poll_id); } } } } return(false); } /* delete a poll */ protected function do_delete_poll($poll_id) { if($this->delete_poll($poll_id)) { if($this->delete_poll_lang($poll_id)) { if($this->do_delete_question("", $poll_id)) { return($poll_id); } } } return(false); } /* translate a poll (add / modify) nfo are from $_REQUEST global var */ protected function do_translate_poll($poll_id) { $sql="REPLACE INTO ".$this->tables['polls_lang']." VALUES ( '".$poll_id."', '".$_REQUEST['fmypolls_trans_to']."', '".htmlspecialchars(stripslashes($_REQUEST['fmypolls_att_tto_title']), ENT_QUOTES)."', '".htmlspecialchars(stripslashes($_REQUEST['fmypolls_att_tto_description']), ENT_QUOTES)."', '".htmlspecialchars(stripslashes($_REQUEST['fmypolls_att_tto_after_vote_text']), ENT_QUOTES)."' )"; if(pwg_query($sql)) { $poll_questions=$this->get_poll_questions($poll_id); foreach($poll_questions as $key => $val) { $sql="REPLACE INTO ".$this->tables['polls_questions_lang']." VALUES ( '".$poll_id."', '".$val['question_num']."', '".htmlspecialchars(stripslashes($_REQUEST['fmypolls_trans_to']), ENT_QUOTES)."', '".htmlspecialchars(stripslashes($_REQUEST['fmypolls_att_tto_qdescription_'.$val['question_num']]), ENT_QUOTES)."' )"; if(pwg_query($sql)) { $poll_answers=$this->get_poll_answers($poll_id, $val['question_num']); $poll_danswers=$this->get_poll_answers($poll_id, $val['question_num'],$_REQUEST['fmypolls_trans_to']); foreach($poll_answers as $key2 => $val2) { if(isset($poll_danswers[$key2])) { $nbvotes=$poll_danswers[$key2]['nb_votes']; } else { $nbvotes=0; } $sql="REPLACE INTO ".$this->tables['polls_answers_lang']." VALUES ( '".$poll_id."', '".$val['question_num']."', '".$val2['answer_num']."', '".$_REQUEST['fmypolls_trans_to']."', '".htmlspecialchars(stripslashes($_REQUEST['fmypolls_att_tto_adescription_'.$val['question_num']."_".$val2['answer_num']]), ENT_QUOTES)."', '".$nbvotes."' )"; if(!pwg_query($sql)) { return(false); } } } else { return(false); } } return(true); } return(false); } protected function do_delete_poll_translation($poll_id, $lang) { $sql="DELETE FROM ".$this->tables['polls_lang']." WHERE id_poll = '".$poll_id."' AND lang = '".$lang."' "; if(pwg_query($sql)) { $sql="DELETE FROM ".$this->tables['polls_questions_lang']." WHERE id_poll = '".$poll_id."' AND lang = '".$lang."' "; if(pwg_query($sql)) { $sql="DELETE FROM ".$this->tables['polls_answers_lang']." WHERE id_poll = '".$poll_id."' AND lang = '".$lang."' "; if(pwg_query($sql)) { return(true); } } } return(false); } /* --------------------------------------------------------------------------- function to manage database manipulation --------------------------------------------------------------------------- */ /* insert poll general info (no language needed) input : an array of general values return poll_id if OK, else -1 */ protected function create_poll($values) { $sql="INSERT INTO ".$this->tables['polls']." VALUES ( '0', '".date('Y-m-d H:i:s')."', '".$values['allowed_users']."', '".$values['allowed_groups']."', '".$values['visible']."', '".$values['enabled']."', '0', '0000-00-00 00:00:00', '0', '0', '".$values['allow_comment']."', '".$values['show_comments']."', '".$values['color_bars']."', '0', '".$values['display_type']."', '".$values['default_lang']."', '0', '0', '".$values['public_results']."' )"; $result=pwg_query($sql); if($result) { return(mysql_insert_id()); } return(false); } /* insert poll general info (langage needed) input : poll_id, an array(lang, title, description, after_vote_text) return 1 if OK, else -1 */ protected function create_poll_lang($poll_id, $values) { $sql="INSERT INTO ".$this->tables['polls_lang']." VALUES ( '".$poll_id."', '".$values['lang']."', '".$values['title']."', '".$values['description']."', '".$values['after_vote_text']."' )"; $result=pwg_query($sql); if(!$result) { return(false); } return($poll_id); } /* insert poll question info (no language needed) input : an array of general values return poll_id if OK, else -1 */ protected function create_poll_questions($values) { $sql="INSERT INTO ".$this->tables['polls_questions']." VALUES ( '".$values['id_poll']."', '".$values['question_num']."', '".$values['question_type']."', '".$values['multi_max_answers']."', '0', '0' )"; $result=pwg_query($sql); if($result) { $sql="UPDATE ".$this->tables['polls']." SET nb_questions = nb_questions+1, counter_questions = counter_questions+1 WHERE id = '".$values['id_poll']."'"; $result=pwg_query($sql); if($result) { return($values['id_poll']); } } return(false); } /* insert poll question info (language needed) input : an array of general values return poll_id if OK, else -1 */ protected function create_poll_questions_lang($values) { $sql="INSERT INTO ".$this->tables['polls_questions_lang']." VALUES ( '".$values['id_poll']."', '".$values['id_question']."', '".$values['lang']."', '".$values['description']."' )"; $result=pwg_query($sql); if(!$result) { return(false); } return($values['id_poll']); } /* insert poll answers info (no language needed) input : an array of general values return poll_id if OK, else -1 */ protected function create_poll_answers($values) { $sql="INSERT INTO ".$this->tables['polls_answers']." VALUES ( '".$values['id_poll']."', '".$values['id_question']."', '".$values['answer_num']."', '0', '0', '0' )"; $result=pwg_query($sql); if(!$result) { return(false); } //every answer created increment a counter on the question //needed to maintain properly answers's id when modify/delete/add one $sql="UPDATE ".$this->tables['polls_questions']." SET counter_answers = counter_answers+1 WHERE id_poll = '".$values['id_poll']."' AND question_num = '".$values['id_question']."' "; pwg_query($sql); return($values['id_poll']); } /* insert poll question info (language needed) input : an array of general values return poll_id if OK, else -1 */ protected function create_poll_answers_lang($values) { $sql="INSERT INTO ".$this->tables['polls_answers_lang']." VALUES ( '".$values['id_poll']."', '".$values['id_question']."', '".$values['id_answer']."', '".$values['lang']."', '".$values['answer']."', '0' )"; $result=pwg_query($sql); if(!$result) { return(false); } return($values['id_poll']); } /* initialise an array from database to prepare form input : poll_id return an array if OK, else -1 */ protected function init_poll_array($poll_id, $lang="") { $sql="SELECT ".$this->tables['polls'].".*, ".$this->tables['polls_lang'].".* FROM ".$this->tables['polls']." INNER JOIN ".$this->tables['polls_lang']." ON ".$this->tables['polls'].".id = ".$this->tables['polls_lang'].".id_poll AND ".$this->tables['polls'].".id = '".$poll_id."' "; 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); } /* initialise an array from database to prepare form input : poll_id, question_num return an array if OK, else -1 */ protected function init_poll_question_array($poll_id, $question_num, $lang="") { $sql="SELECT ".$this->tables['polls_questions'].".*, ".$this->tables['polls_questions_lang'].".*, ".$this->tables['polls_answers_lang'].".* FROM ".$this->tables['polls_questions']." INNER JOIN ".$this->tables['polls_questions_lang']." ON ".$this->tables['polls_questions'].".id_poll = ".$this->tables['polls_questions_lang'].".id_poll AND ".$this->tables['polls_questions'].".question_num = ".$this->tables['polls_questions_lang'].".id_question, ".$this->tables['polls'].", ".$this->tables['polls_answers_lang']." WHERE ".$this->tables['polls'].".id = '".$poll_id."' AND ".$this->tables['polls'].".id = ".$this->tables['polls_questions'].".id_poll AND ".$this->tables['polls_answers_lang'].".id_poll = ".$this->tables['polls_questions'].".id_poll AND ".$this->tables['polls_answers_lang'].".id_question = ".$this->tables['polls_questions'].".question_num AND ".$this->tables['polls_answers_lang'].".lang = ".$this->tables['polls_questions_lang'].".lang AND ".$this->tables['polls_questions'].".question_num = '".$question_num."' "; if($lang=="") { $sql.=" AND ".$this->tables['polls'].".default_lang = ".$this->tables['polls_questions_lang'].".lang "; } else { $sql.=" AND ".$this->tables['polls_lang'].".lang = '".$lang."' "; } $sql.=" ORDER BY id_answer ASC "; $result=pwg_query($sql); if($result) { $returned=array(); $answnum=0; while($row=mysql_fetch_array($result)) { if($answnum==0) { $returned['default_lang'] = $row['lang']; $returned['question_type'] = $row['question_type']; $returned['multi_max_answers'] = $row['multi_max_answers']; $returned['description'] = $row['description']; $returned['counter_answers'] = $row['counter_answers']; } $returned['answ'.$row['id_answer']] = $row['answer']; $answnum++; } return($returned); } return(false); } /* modify poll general info (no language needed) input : an array of general values return poll_id if OK, else -1 */ protected function modify_poll($poll_id, $values) { $sql="UPDATE ".$this->tables['polls']." SET allowed_users = '".$values['allowed_users']."', allowed_groups = '".$values['allowed_groups']."', visible = '".$values['visible']."', enabled = '".$values['enabled']."', allow_comment = '".$values['allow_comment']."', show_comments = '".$values['show_comments']."', color_bars = '".$values['color_bars']."', display_type = '".$values['display_type']."', public_results = '".$values['public_results']."' WHERE id = '".$poll_id."'"; $result=pwg_query($sql); if($result) { return($poll_id); } return(false); } /* modify poll general info (language needed) input : an array of general values return poll_id if OK, else -1 */ protected function modify_poll_lang($poll_id, $lang, $values) { $sql="UPDATE ".$this->tables['polls_lang']." SET title = '".$values['title']."', description = '".$values['description']."', after_vote_text = '".$values['after_vote_text']."' WHERE id_poll = '".$poll_id."' AND lang = '".$lang."'"; $result=pwg_query($sql); if($result) { return($poll_id); } return(false); } /* modify poll question info (no language needed) input : an array of general values return poll_id if OK, else -1 */ protected function modify_poll_questions($poll_id, $question_num, $values) { $sql="UPDATE ".$this->tables['polls_questions']." SET question_type = '".$values['question_type']."', multi_max_answers = '".$values['multi_max_answers']."' WHERE id_poll = '".$poll_id."' AND question_num = '".$question_num."' "; $result=pwg_query($sql); if($result) { return($poll_id); } return(false); } /* modify poll question info (language needed) return poll_id if OK, else -1 */ protected function modify_poll_questions_lang($poll_id, $id_question, $lang, $values) { $sql="UPDATE ".$this->tables['polls_questions_lang']." SET description = '".$values['description']."' WHERE id_poll = '".$poll_id."' AND id_question = '".$id_question."' AND lang = '".$lang."' "; $result=pwg_query($sql); if(!$result) { return(false); } return($poll_id); } /* modify poll question info (language needed) return poll_id if OK, else -1 */ protected function modify_poll_answers_lang($id_poll, $id_question, $id_answer, $lang, $values) { $sql="SELECT id_poll FROM ".$this->tables['polls_answers_lang']." WHERE id_poll = '".$id_poll."' AND id_question = '".$id_question."' AND id_answer = '".$id_answer."' AND lang = '".$lang."' "; $result=pwg_query($sql); if(mysql_num_rows($result)==0) { $values=array( 'id_poll' => $id_poll, 'id_question' => $id_question, 'id_answer' => $id_answer, 'answer_num' => $id_answer, 'lang' => $lang, 'answer' => $values['answer'] ); if($this->create_poll_answers($values)) { if($this->create_poll_answers_lang($values)) { return($id_poll); } }; return(false); } else { $sql="UPDATE ".$this->tables['polls_answers_lang']." SET answer = '".$values['answer']."' WHERE id_poll = '".$id_poll."' AND id_question = '".$id_question."' AND id_answer = '".$id_answer."' AND lang = '".$lang."' "; $result=pwg_query($sql); if(!$result) { return(false); } return($id_poll); } } protected function delete_poll($id_poll) { $sql="DELETE FROM ".$this->tables['polls']." WHERE id = '".$id_poll."' "; $result=pwg_query($sql); if($result) { return($id_poll); } return(false); } protected function delete_poll_lang($id_poll, $lang="") { $sql="DELETE FROM ".$this->tables['polls_lang']." WHERE id_poll = '".$id_poll."' "; if($lang!="") { $sql.=" AND lang = '".$lang."' "; } $result=pwg_query($sql); if($result) { return($id_poll); } return(false); } protected function delete_poll_questions($id_poll, $id_question="") { $sql="DELETE FROM ".$this->tables['polls_questions']." WHERE id_poll = '".$id_poll."' "; if($id_question!="") { $sql.=" AND question_num = '".$id_question."' "; } $result=pwg_query($sql); if($result) { $sql="UPDATE ".$this->tables['polls']; if($id_question=="") { $sql.=" SET nb_questions = 0 "; } else { $sql.=" SET nb_questions = nb_questions-1 "; } $sql.=" WHERE id = '".$id_poll."'"; $result=pwg_query($sql); if($result) { return($id_poll); } } return(false); } protected function delete_poll_questions_lang($id_poll, $id_question="", $lang="") { if($id_question=="") { $lang=""; } $sql="DELETE FROM ".$this->tables['polls_questions_lang']." WHERE id_poll = '".$id_poll."' "; if($id_question!="") { $sql.=" AND id_question = '".$id_question."' "; } if($lang!="") { $sql.=" AND lang = '".$lang."' "; } $result=pwg_query($sql); if($result) { return($id_poll); } return(false); } protected function delete_poll_answers($id_poll, $id_question="", $id_answer="") { if($id_question=="") { $id_answer=""; } $sql="DELETE FROM ".$this->tables['polls_answers']." WHERE id_poll = '".$id_poll."' "; if($id_question!="") { $sql.=" AND id_question = '".$id_question."' "; } if($id_answer!="") { $sql.=" AND answer_num = '".$id_answer."' "; } $result=pwg_query($sql); if($result) { return($id_poll); } return(false); } protected function delete_poll_answers_lang($id_poll, $id_question="", $id_answer="", $lang="") { if($id_question=="") { $id_answer=""; } if($id_answer=="") { $lang=""; } $sql="DELETE FROM ".$this->tables['polls_answers_lang']." WHERE id_poll = '".$id_poll."' "; if($id_question!="") { $sql.=" AND id_question = '".$id_question."' "; } if($id_answer!="") { $sql.=" AND id_answer = '".$id_answer."' "; } if($lang!="") { $sql.=" AND lang = '".$lang."' "; } $result=pwg_query($sql); if($result) { return($id_poll); } return(false); } /* return a list of user/ip making a vote on the poll list is paged with $my_config['mypolls_nbuser_per_page'] value an array is returned : array[0] : list array[1] : total number of pages */ protected function get_poll_users_vote($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 FROM ".$this->tables['polls_votes']." LEFT JOIN ".USERS_TABLE." ON ".$this->tables['polls_votes'].".user_id = ".USERS_TABLE.".id WHERE poll_id = '".$poll_id."' ORDER BY name LIMIT ".(($num_page-1)*$this->my_config['mypolls_nbuser_per_page']).", ".$this->my_config['mypolls_nbuser_per_page']; $result = pwg_query($sql); if($result) { while($row = mysql_fetch_row($result)) { array_push($returned[0], $row[0]); } //compute number of pages $sql="select FOUND_ROWS()"; $result=pwg_query($sql); if($result) { $row = mysql_fetch_row($result); $returned[1] = ceil($row[0]/$this->my_config['mypolls_nbuser_per_page']); } else { $returned[1] = -1; } } return($returned); } /* --------------------------------------------------------------------------- ajax functions --------------------------------------------------------------------------- */ /* return html formatted informations for a poll (need by poll's list view) */ protected function ajax_poll_detail_list($poll_id) { $local_tpl = new Template(MYPOLLS_PATH."admin/", ""); $local_tpl->set_filename('body_page', dirname($this->filelocation).'/admin/plugin_admin_detail.tpl'); $languages=get_languages(); $poll_values=$this->get_poll_values($poll_id); $poll_langs=$this->get_poll_languages($poll_id); $langs="".$languages[$poll_values['default_lang']].""; foreach($poll_langs as $key => $val) { if($val!=$poll_values['default_lang']) { $langs.=", ".$languages[$val]; } } $local_tpl->assign('datas', array( 'MYPOLLS_ATT_VISIT_WO_VOTE' => $poll_values['visit_without_vote'], 'MYPOLLS_ATT_VISIT_A_VOTE' => $poll_values['visit_after_vote'], 'MYPOLLS_NB_COMMENTS' => ($poll_values['allow_comment']=='y')?"(".$poll_values['nb_comments'].")":"", 'MYPOLLS_ATT_ALLOW_COMMENT' => l10n('mypolls_yesno_'.$poll_values['allow_comment']), 'MYPOLLS_ATT_SHOW_COMMENTS' => l10n('mypolls_yesno_'.$poll_values['show_comments']), 'MYPOLLS_ATT_DISPLAY_TYPE' => l10n('mypolls_display_'.$poll_values['display_type']), 'MYPOLLS_POLL_LANGS' => $langs, 'MYPOLLS_ATT_PUBLIC_RESULTS' => l10n('mypolls_yesno_'.$poll_values['public_results']) ) ); return($local_tpl->parse('body_page', true)); } /* return html formatted informations about poll's voting users */ protected function ajax_poll_user_list($poll_id) { if(!isset($_REQUEST['vpage'])) { $page=1; } else { $page=$_REQUEST['vpage']; } $voter_list=$this->get_poll_users_vote($poll_id, $page); $local_tpl = new Template(MYPOLLS_PATH."admin/", ""); $local_tpl->set_filename('body_page', dirname($this->filelocation).'/admin/plugin_admin_voter_list.tpl'); //users list $voter_list_rows=array(); foreach($voter_list[0] as $key => $val) { $voter_list_rows[]=array('USERNAME' => $val); } //make pages links if($voter_list[1]>1) { $plural="s"; } else { $plural=""; } $pages_links=l10n("mypolls_page".$plural."_label")." : "; if($voter_list[1]<=0) { $pages_links=""; } else { for($i=1;$i<=$voter_list[1];$i++) { if($i==$page) { $pages_links.=" $i "; } else { $pages_links.=" $i "; } } } $local_tpl->assign("datas", array('PAGES' => $pages_links)); $local_tpl->assign("voter_list_rows", $voter_list_rows); return($local_tpl->parse('body_page', true)); } /* delete a comment */ protected function ajax_poll_delete_comment($poll_id) { if(!isset($_REQUEST['comment_id'])) { return("
".l10n("mypolls_no_comment_to_delete")."
 
"); } if((!is_admin()) || (is_adviser())) { $returned="
".l10n("mypolls_adviser_not_allowed")."
 
"; } else { $sql="UPDATE ".$this->tables['polls_votes']." SET user_comment = '' WHERE poll_id = '".$poll_id."' AND user_id = '".$_REQUEST['comment_id']."' "; $result=pwg_query($sql); if(!$result) { $returned="
".l10n("mypolls_errors_deleting_comment")."
 
"; } else { $returned="
".l10n("mypolls_infos_deleted_comment")."
 
"; } } $returned.=$this->ajax_poll_comment_list($poll_id); return($returned); } } //class ?>