source: extensions/mypolls/mypolls_root.class.inc.php @ 6299

Last change on this file since 6299 was 3397, checked in by grum, 15 years ago

Add plugin MyPolls - this release is not published in PEM (functionnal but not tested yet...)

  • Property svn:executable set to *
File size: 19.5 KB
Line 
1<?php
2/* -----------------------------------------------------------------------------
3  Plugin     : MyPolls.2
4  Author     : Grum
5    email    : grum@piwigo.org
6    website  : http://photos.grum.dnsalias.com
7
8    << May the Little SpaceFrog be with you ! >>
9  ------------------------------------------------------------------------------
10  See main.inc.php for release information
11
12  MyPolls_root : common classe for admin and public classes
13
14  --------------------------------------------------------------------------- */ 
15  include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/common_plugin.class.inc.php');
16  include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/users_groups.class.inc.php');
17  include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/css.class.inc.php');
18  include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/ajax.class.inc.php');
19  global $ajax;
20
21
22  class MyPolls_root extends common_plugin
23  {
24    protected $questions_types;
25    protected $display_types;
26    protected $section_name;    //name of mypolls's section
27    protected $css;   //the css object
28    protected $ajax;
29
30    public function MyPolls_root($prefixeTable, $filelocation)
31    {
32      $this->plugin_name='MyPolls.2';
33      $this->plugin_name_files="mypolls";
34      parent::common_plugin($prefixeTable, $filelocation);
35      $this->section_name=$this->plugin_name_files;
36
37      $list=array('polls', 'polls_lang', 'polls_questions', 'polls_questions_lang',
38                  'polls_answers', 'polls_answers_lang', 'polls_votes');
39      $this->set_tables_list($list);
40
41      $this->questions_types=array('mono', 'multi', 'order');
42      $this->display_types=array('mono', 'multi');
43      $this->css = new css(dirname($this->filelocation).'/'.$this->plugin_name_files.".css");
44    }
45
46  public function init_events()
47  {
48    add_event_handler('blockmanager_register_blocks', array(&$this, 'register_blocks') );
49  }
50
51  public function register_blocks( $menu_ref_arr )
52  {
53    $menu = & $menu_ref_arr[0];
54    if ($menu->get_id() != 'menubar')
55      return;
56    $menu->register_block( new RegisteredBlock( 'mbMYPOLLS_menu', 'Polls', 'MYPOLLS'));
57  }
58
59
60    /*
61      surchage of common_plugin->save_config function
62    */
63    public function save_config()
64    {
65      if(parent::save_config())
66      {
67        $this->css->make_CSS($this->generate_CSS());
68        return(true);
69      }
70      return(false);
71    }
72
73    /*
74      surchage of common_plugin->save_config function
75    */
76    public function load_config()
77    {
78      parent::load_config();
79      if(!$this->css->css_file_exists())
80      {
81        $this->css->make_CSS($this->generate_CSS());
82      }
83    }
84
85
86    /*
87      intialize default values
88    */
89    public function init_config()
90    {
91      //global $user;
92      $this->my_config=array(
93        'mypolls_color_bars' => '6666ff', /**/
94        'mypolls_maxbarwidth' => '400', /**/
95        'mypolls_display_type_default' => 'mono', /**/
96        'mypolls_admincolor' => 'ffff66', /**/
97        'mypolls_mouseovercolor' => '303030', /**/
98        'mypolls_nbuser_per_page' => 15, /**/
99        'mypolls_nbcomment_per_page' => 15, /**/
100        /* not present in config panel, but needed by mypolls */
101        'mypolls_visible_default' => 'y',
102        'mypolls_allow_comment_default' => 'n',
103        'mypolls_show_user_comment_default' => 'n',
104        'mypolls_allowed_users_default_guest' => 'y',
105        'mypolls_allowed_users_default_generic' => 'y',
106        'mypolls_allowed_users_default_normal' => 'y',
107        'mypolls_allowed_groups_default' => '',
108        'mypolls_question_type_default' => 'mono',
109        'mypolls_multi_max_answers_default' => '3',
110        'mypolls_public_results' => 'y',
111        /* 'mypolls_default_language' => $user['language'], */
112        'mypolls_menubar_title' => array(),
113        'mypolls_generic_text_users' => array(),
114        'mypolls_generic_text_guests' => array(),
115        'mypolls_not_public_results' => array()
116      );
117
118      $languages=get_languages();
119      foreach($languages as $key => $val)
120      {
121        $this->my_config['mypolls_menubar_title'][$key]='MyPolls';
122        $this->my_config['mypolls_generic_text_users'][$key]='';
123        $this->my_config['mypolls_generic_text_guests'][$key]='';
124        $this->my_config['mypolls_not_public_results'][$key]='';
125      }
126    }
127
128    /*
129      generate the css code
130    */
131    protected function generate_CSS()
132    {
133      $text = "
134.graphbar1,
135.graphbarx {
136    border:0px;
137    height:8px;
138    display: block;
139    margin:0px;
140    padding:0px;
141    left:0;
142    position:relative;
143  }
144.graphbar1 { background-color:#".$this->my_config['mypolls_color_bars']."; top:-3px;  }
145.graphbarx { background-color:transparent; top:0px; height:1px; }
146
147.formtable P,
148.formtable UL,
149.formtable UL LI P
150{
151  text-align:left;
152  display:block;
153  margin-top:0px;
154  margin-bottom:0px;
155}
156
157.formtable UL LI
158{
159  margin-bottom:1em;
160}
161
162.formtable UL LI P,
163.formtable P
164{
165  color:#".$this->my_config['mypolls_admincolor'].";
166  padding-left:1.5em;
167}
168
169.TableRow:hover
170{
171  background-color:#".$this->my_config['mypolls_mouseovercolor'].";
172}
173
174.formtable UL.compact LI
175{
176  margin-bottom:0em;
177}
178
179.mypolls_page
180{
181  text-align:justify;
182  margin:8px;
183}
184
185.mypolls_page FORM P
186{
187  text-align:justify;
188  margin:0px;
189  margin-bottom:1em;
190}
191
192.littlefont { font-size:90%; }
193table.littlefont th { text-align:center; padding:3px;padding-left:9px;padding-right:9px; }
194table.littlefont td { text-align:left; padding:0px;padding-left:3px;padding-right:3px; }
195
196      ";
197      return($text);
198    }
199
200    /* -------------------------------------------------------------------------
201     return an array of poll's values
202      if no lang given, return nfo with poll's default language
203    ------------------------------------------------------------------------- */
204    protected function get_poll_values($poll_id, $lang="")
205    {
206      $sql="SELECT ".$this->tables['polls'].".*,
207                   ".$this->tables['polls_lang'].".title,
208                   ".$this->tables['polls_lang'].".description,
209                   ".$this->tables['polls_lang'].".after_vote_text
210            FROM ".$this->tables['polls'].",
211                 ".$this->tables['polls_lang']."
212            WHERE id = '$poll_id'
213              AND ".$this->tables['polls'].".id = ".$this->tables['polls_lang'].".id_poll ";
214      if($lang=="")
215      {
216        $sql.=" AND ".$this->tables['polls'].".default_lang = ".$this->tables['polls_lang'].".lang ";
217      }
218      else
219      {
220        $sql.=" AND ".$this->tables['polls_lang'].".lang = '".$lang."' ";
221      }
222
223      $result=pwg_query($sql);
224      if($result)
225      {
226        return(mysql_fetch_array($result));
227      }
228      return(false); 
229    }
230
231    /* -------------------------------------------------------------------------
232     return an array of poll's translation
233    ------------------------------------------------------------------------- */
234    protected function get_poll_languages($poll_id)
235    {
236      $sql="SELECT lang FROM ".$this->tables['polls_lang']." WHERE id_poll = '$poll_id'";
237      $result=pwg_query($sql);
238      if($result)
239      {
240        $returned=array();
241        while($row=mysql_fetch_row($result))
242        {
243          array_push($returned, $row[0]);
244        }
245        return($returned);
246      } 
247      return(false); 
248    }
249
250
251    /* -------------------------------------------------------------------------
252     returns poll's question
253      if no question id given, return an array of all question
254      if no lang given, return question with poll's default language
255    ------------------------------------------------------------------------- */
256    protected function get_poll_questions($poll_id, $id_question="", $lang="")
257    {
258      $sql="SELECT ".$this->tables['polls_questions_lang'].".description,
259                ".$this->tables['polls_questions'].".*
260            FROM ".$this->tables['polls_questions_lang'].",
261                 ".$this->tables['polls_questions'];
262      $sql_where=" WHERE ".$this->tables['polls_questions'].".id_poll = ".$poll_id."
263                   AND ".$this->tables['polls_questions_lang'].".id_poll = "
264                        .$this->tables['polls_questions'].".id_poll
265                   AND ".$this->tables['polls_questions_lang'].".id_question = "
266                        .$this->tables['polls_questions'].".question_num ";
267
268      if($id_question!="")
269      {
270        $sql_where.=" AND ".$this->tables['polls_questions'].".question_num = ".
271                      $id_question." ";
272      }
273
274      if($lang=="")
275      {
276        $sql.=" , ".$this->tables['polls']." ";
277        $sql_where.=" AND ".$this->tables['polls'].".default_lang =
278                          ".$this->tables['polls_questions_lang'].".lang
279                      AND ".$this->tables['polls'].".id =
280                          ".$this->tables['polls_questions'].".id_poll ";
281      }
282      else
283      {
284        $sql_where.=" AND ".$this->tables['polls_questions_lang'].".lang = '".$lang."' ";
285      }
286      $sql_order=" ORDER BY question_num asc ";
287      $result=pwg_query($sql.$sql_where.$sql_order);
288      if($result)
289      {
290        $returned=array();
291        while($row=mysql_fetch_assoc($result))
292        {
293          array_push($returned, $row);
294        }
295        return($returned);
296      } 
297      return(false); 
298    }
299
300    /* -------------------------------------------------------------------------
301     returns poll's question's answers
302      if no lang given, return answers with poll's default language
303    ------------------------------------------------------------------------- */
304    protected function get_poll_answers($poll_id, $id_question, $lang="", $order_by="", $array_by_answer_id=false)
305    {
306      $sql="SELECT ".$this->tables['polls_answers'].".*,
307                   ".$this->tables['polls_answers_lang'].".nb_votes as vote_lang,
308                   ".$this->tables['polls_answers_lang'].".answer
309            FROM ".$this->tables['polls_answers']." INNER JOIN
310                 ".$this->tables['polls_answers_lang']."
311              ON ".$this->tables['polls_answers'].".id_poll = ".$this->tables['polls_answers_lang'].".id_poll
312              AND ".$this->tables['polls_answers'].".id_question = ".$this->tables['polls_answers_lang'].".id_question
313              AND ".$this->tables['polls_answers'].".answer_num = ".$this->tables['polls_answers_lang'].".id_answer ";
314      $sql_where=" WHERE ".$this->tables['polls_answers'].".id_poll = '".$poll_id."'
315                     AND ".$this->tables['polls_answers'].".id_question = '".$id_question."' ";
316      if($order_by=="")
317      {
318        $sql_order=" ORDER BY ".$this->tables['polls_answers'].".answer_num ASC ";
319      }
320      else
321      {
322        $sql_order=" ORDER BY ".$order_by;
323      }
324
325      if($lang=="")
326      {
327        $sql.=" , ".$this->tables['polls']." ";
328        $sql_where.=" AND ".$this->tables['polls_answers'].".id_poll = ".$this->tables['polls'].".id
329                      AND ".$this->tables['polls_answers_lang'].".lang = ".$this->tables['polls'].".default_lang ";
330      }
331      else
332      {
333        $sql_where.=" AND ".$this->tables['polls_answers_lang'].".lang = '".$lang."' ";
334      }
335      $result=pwg_query($sql.$sql_where.$sql_order);
336      if($result)
337      {
338        $returned=array();
339        $num=0;
340        while($row=mysql_fetch_assoc($result))
341        {
342          if(!$array_by_answer_id)
343          { array_push($returned, $row); }
344          else
345          {
346            $num++;
347            $returned[$row['answer_num']] = $row;
348            $returned[$row['answer_num']]['order'] = $num;
349          }
350        }
351        return($returned);
352      }
353      return(false);
354    }
355
356
357    /* -------------------------------------------------------------------------
358       return an array of all polls
359    ------------------------------------------------------------------------- */
360    protected function get_polls_list($user_id=2, $view_all=true, $lang="")
361    {
362      $returned=array();
363
364
365      $sql="SELECT ".$this->tables['polls'].".*,
366                       ".$this->tables['polls_lang'].".title
367          FROM ".$this->tables['polls_lang'].",
368                     ".$this->tables['polls'];
369
370      $sql_where=" WHERE ".$this->tables['polls'].".id =
371                          ".$this->tables['polls_lang'].".id_poll
372                      AND ".$this->tables['polls_lang'].".lang = ";
373
374      if($lang=="")
375      {
376        $sql_where.=" ".$this->tables['polls'].".default_lang ";
377      }
378      else
379      {
380        $sql_where.=" '".$lang."' ";
381      }
382
383      if(!$view_all)
384      {
385        $sql.=" LEFT OUTER JOIN (SELECT poll_id, count(*) as as_voted
386                  FROM ".$this->tables['polls_votes']."
387                  WHERE user_id = '".$user_id."'
388                  GROUP BY poll_id, user_id ) as vote_list
389              ON id = poll_id ";
390        $sql_where.=" AND visible = 'y' AND enabled = 'y' AND nb_questions>0
391                      AND ((".$this->tables['polls'].".public_results = 'n'
392                            AND vote_list.as_voted = 0
393                           )
394                           OR (".$this->tables['polls'].".public_results = 'y'))";
395      }
396
397      $sql_order=" ORDER BY id desc";
398
399      $result=pwg_query($sql.$sql_where.$sql_order);
400      if($result)
401      {
402        while($row=mysql_fetch_assoc($result))
403        { 
404          array_push($returned, $row);
405        }
406      } 
407      return($returned); 
408    }
409
410
411    /*
412      return a list of user/ip making a comment on the poll
413      list is paged with $my_config['mypolls_nbcomment_per_page'] value
414
415      an array is returned :
416        array[0] : list
417        array[1] : total number of pages
418    */
419    protected function get_poll_users_comment($poll_id, $num_page=1)
420    {
421      $returned=array(array(), -1);
422      $sql="SELECT SQL_CALC_FOUND_ROWS IF(username IS NULL, user_id, CONCAT(' ',username)) AS name, date, user_comment, user_id
423        FROM ".$this->tables['polls_votes']." LEFT JOIN ".USERS_TABLE."
424          ON ".$this->tables['polls_votes'].".user_id = ".USERS_TABLE.".id
425        WHERE poll_id = $poll_id and user_comment != ''
426        ORDER BY date desc
427        LIMIT ".(($num_page-1)*$this->my_config['mypolls_nbcomment_per_page']).", ".$this->my_config['mypolls_nbcomment_per_page'];
428
429      $result = pwg_query($sql);
430      if($result)
431      {
432        while($row = mysql_fetch_assoc($result))
433        { array_push($returned[0], $row); }
434
435        $sql="select FOUND_ROWS()";
436        $result=pwg_query($sql);
437        if($result) 
438        { $row = mysql_fetch_row($result); $returned[1] = ceil($row[0]/$this->my_config['mypolls_nbcomment_per_page']); } 
439        else 
440        { $returned[1] = -1; }
441      }
442      return($returned);
443    }
444
445
446    /*
447      common fo&bo function to display results
448        //$poll_values is by ref, because it can be a huge array
449    */
450    protected function display_results_block($poll_id, $lang, &$poll_values)
451    {
452      global $template;
453
454      /*
455        function used with this templates
456            mypolls_public_results.tpl
457            mypolls_public_question.tpl
458            plugin_admin_view.tpl
459        only affect $questions values
460
461        $datas values are affected outside of this function
462      */
463
464     
465      $template_questions=array();
466
467      $questions_values=$this->get_poll_questions($poll_id, "", $lang);
468      $numq=1;
469      foreach($questions_values as $key => $val)
470      {
471        if($val['question_type']=='multi')
472        {
473          $show_maxi='&nbsp;'.sprintf(l10n('mypolls_answer_maxi'), $val['multi_max_answers']);
474        }
475        else
476        {
477          $show_maxi="";
478        }
479        if($val['question_type']=='order')
480        {
481          $nbvotes_label='';
482          $order_by=" nb_votes ASC";
483        }
484        else
485        {
486          $nbvotes_label=l10n('mypolls_result_nbanswers')." (".$val['nb_votes'].")";
487          $order_by=" nb_votes DESC";
488        }
489
490
491        $template_questions_answers=array();
492
493        $numa=1;
494        $answers_values=$this->get_poll_answers($poll_id, $val['question_num'], $lang, $order_by.", ans_order ASC");
495        foreach($answers_values as $key2 => $val2)
496        {
497          if(($val['nb_votes']>0)&&($poll_values['total_votes']>0))
498          {
499            if($val['question_type']=='order')
500            { 
501              $pct=100*($poll_values['total_votes']-$val2['frequency'])/$poll_values['total_votes'];
502            }
503            else
504            { 
505              $pct=100*$val2['nb_votes']/$val['nb_votes'];
506            }
507            $graph=ceil($pct*$this->my_config['mypolls_maxbarwidth']/100);
508          }
509          else
510          {
511            $pct=0;
512            $graph=0;
513          }
514          $template_questions_answers[]=array(
515            'NUMANSWER' => $numa,
516            'ANSWER' => html_entity_decode($val2['answer']),
517            'PCT' => number_format($pct, 2, '.', '').'% ',
518            'GRAPH' => $graph,
519            'MAXGRAPH' => $this->my_config['mypolls_maxbarwidth'],
520            'NBANSWSERS' => ($val['question_type']!='order')?$val2['nb_votes']:""
521          );
522
523          $numa++;
524        }
525
526        $template_questions[]=array(
527            'MAXGRAPH' => ceil($this->my_config['mypolls_maxbarwidth']*1.10),
528            'NUMQUESTION' => $numq,
529            'DESCRIPTION' => html_entity_decode($val['description']),
530            'QUESTION_TYPE' => strtolower(l10n('mypolls_question_'.$val['question_type']).$show_maxi),
531            'NBVOTES' => $nbvotes_label,
532            'answers' => $template_questions_answers
533        );
534
535        $numq++;
536      }
537      $template->assign('questions', $template_questions);
538
539      if($poll_values['color_bars']!=$this->my_config['mypolls_color_bars'])
540      {
541        $template->append('head_elements', '<style type="text/css">.graphbar1 { background-color:#'.$poll_values['color_bars'].'; }</style>');
542      }
543
544    } //display_results_block
545
546
547    /* ---------------------------------------------------------------------------
548      ajax functions
549    --------------------------------------------------------------------------- */
550
551    /*
552      return html formatted informations about poll's comments users
553    */
554    protected function ajax_poll_comment_list($poll_id)
555    {
556      global $user;
557
558      if(!isset($_REQUEST['vpage']))
559      {
560        $page=1;
561      }
562      else
563      {
564        $page=$_REQUEST['vpage'];
565      }
566
567      $comment_list=$this->get_poll_users_comment($poll_id, $page);
568
569      $local_tpl = new Template(MYPOLLS_PATH."templates/", "");
570      $local_tpl->set_filename('body_page',
571                    dirname($this->filelocation).'/templates/plugin_comment_list.tpl');
572
573
574      $template_datas=array();
575      $template_datarows=array();
576
577      //users list
578      foreach($comment_list[0] as $key => $val)
579      {
580        $template_datarows[] =array(
581          'USERNAME' => $val['name'],
582          'DATE' => $val['date'],
583          'COMMENT' => nl2br($val['user_comment']),
584          'display_delete' => (defined('IN_ADMIN') and IN_ADMIN)?'yes':'',
585          'ID' => (defined('IN_ADMIN') and IN_ADMIN)?$val['user_id']:'',
586          'PAGE' => (defined('IN_ADMIN') and IN_ADMIN)?$page :''
587        );
588      }
589
590      //make pages links
591      if($comment_list[1]>1)
592      {
593        $plural="s";
594      }
595      else
596      {
597        $plural="";
598      }
599      $pages_links=l10n("mypolls_page".$plural."_label")." : ";
600      if($comment_list[1]<=0)
601      {
602        $pages_links="";
603      }
604      else
605      {
606        for($i=1;$i<=$comment_list[1];$i++)
607        {
608          if($i==$page)
609          { $pages_links.=" $i "; } 
610          else
611          {
612            $pages_links.="<a style='cursor:pointer;' onclick='load_poll_comment_list($i);'> $i </a>";
613          }
614        }
615      }
616
617      $local_tpl->assign('datas', array("PAGES" => $pages_links));
618      $local_tpl->assign('comment_list_rows',  $template_datarows);
619      return($local_tpl->parse('body_page', true));
620    }
621
622  } //class
623
624?>
Note: See TracBrowser for help on using the repository browser.