[ Index ]
 

Code source de GeekLog 1.4.1

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/plugins/polls/ -> functions.inc (source)

   1  <?php
   2  
   3  /* Reminder: always indent with 4 spaces (no tabs). */
   4  // +---------------------------------------------------------------------------+
   5  // | Polls Plugin 1.1                                                          |
   6  // +---------------------------------------------------------------------------+
   7  // | functions.inc                                                             |
   8  // |                                                                           |
   9  // | This file does two things: 1) it implements the necessary Geeklog Plugin  |
  10  // | API method and 2) implements all the common code needed by the Polls      |
  11  // | plugin' PHP files.                                                        |
  12  // +---------------------------------------------------------------------------+
  13  // | Copyright (C) 2000-2006 by the following authors:                         |
  14  // |                                                                           |
  15  // | Authors: Tony Bibbs       - tony AT tonybibbs DOT com                     |
  16  // |          Tom Willett      - twillett AT users DOT sourceforge DOT net     |
  17  // |          Blaine Lang      - langmail AT sympatico DOT ca                  |
  18  // |          Dirk Haun        - dirk AT haun-online DOT de                    |
  19  // +---------------------------------------------------------------------------+
  20  // |                                                                           |
  21  // | This program is free software; you can redistribute it and/or             |
  22  // | modify it under the terms of the GNU General Public License               |
  23  // | as published by the Free Software Foundation; either version 2            |
  24  // | of the License, or (at your option) any later version.                    |
  25  // |                                                                           |
  26  // | This program is distributed in the hope that it will be useful,           |
  27  // | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
  28  // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
  29  // | GNU General Public License for more details.                              |
  30  // |                                                                           |
  31  // | You should have received a copy of the GNU General Public License         |
  32  // | along with this program; if not, write to the Free Software Foundation,   |
  33  // | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
  34  // |                                                                           |
  35  // +---------------------------------------------------------------------------+
  36  //
  37  // $Id: functions.inc,v 1.57 2006/12/02 16:32:16 dhaun Exp $
  38  
  39  if (strpos ($_SERVER['PHP_SELF'], 'functions.inc') !== false) {
  40      die ('This file can not be used on its own.');
  41  }
  42  
  43  $langfile = $_CONF['path'] . 'plugins/polls/language/'
  44            . $_CONF['language'] . '.php';
  45  
  46  if (file_exists ($langfile)) {
  47      require_once ($langfile);
  48  } else {
  49      require_once ($_CONF['path'] . 'plugins/polls/language/english.php');
  50  }
  51  
  52  require_once ($_CONF['path'] . 'plugins/polls/config.php');
  53  
  54  
  55  // +---------------------------------------------------------------------------+
  56  // | Geeklog Plugin API Implementations                                        |
  57  // +---------------------------------------------------------------------------+
  58  
  59  /**
  60  * Returns the items for this plugin that should appear on the main menu
  61  *
  62  * NOTE: this MUST return the url/value pairs in the following format
  63  * $<arrayname>[<label>] = <url>
  64  *
  65  */
  66  function plugin_getmenuitems_polls ()
  67  {
  68      global $_CONF, $_USER, $_PO_CONF, $LANG_POLLS;
  69  
  70      $anon = (empty ($_USER['uid']) || ($_USER['uid'] <= 1)) ? true : false;
  71      if (($_PO_CONF['hidepollsmenu'] == 1) || ($anon &&
  72                  ($_CONF['loginrequired'] || $_PO_CONF['pollsloginrequired']))) {
  73          return false;
  74      }
  75  
  76      $menuitems[$LANG_POLLS['polls']] = $_CONF['site_url'] . '/polls/index.php';
  77  
  78      return $menuitems;
  79  }
  80  
  81  /**
  82  * Geeklog is checking to see if this plugin supports comments, tell it yes.
  83  *
  84  * NOTE: to support comments you must used the same date/time based ID for your
  85  * widget.  In other words, to make primary keys for your plugin you should call
  86  * makesid().  Comments are keyed off of that...it is a limitation on how Geeklog
  87  * does comments.
  88  *
  89  */
  90  function plugin_commentsupport_polls()
  91  {
  92      // polls will use comments
  93      return true;
  94  }
  95  
  96  /**
  97   * Poll saves a comment
  98   *
  99   * @param   string  $type   Plugin to save comment
 100   * @param   string  $title  comment title
 101   * @param   string  $comment comment text
 102   * @param   string  $id     Item id to which $cid belongs
 103   * @param   int     $pid    comment parent
 104   * @param   string  $postmode 'html' or 'text'
 105   * @return  mixed   false for failure, HTML string (redirect?) for success
 106   */
 107  function plugin_savecomment_polls($title, $comment, $id, $pid, $postmode)
 108  {
 109      global $_CONF, $_TABLES, $LANG03, $_USER;
 110  
 111      $retval = '';
 112  
 113      $commentcode = DB_getItem ($_TABLES['pollquestions'], 'commentcode',
 114                                 "qid = '$id'");
 115      if ($commentcode < 0) {
 116          return COM_refresh ($_CONF['site_url'] . '/index.php');
 117      }
 118  
 119      $ret = CMT_saveComment ($title, $comment, $id, $pid, 'polls', $postmode);
 120      if ($ret > 0) { // failure //FIXME: some failures should not return to comment form
 121          $retval .= COM_siteHeader('menu', $LANG03[1])
 122                  . CMT_commentForm ($title, $comment, $id, $pid, 'polls',
 123                                     $LANG03[14], $postmode)
 124                  . COM_siteFooter();
 125      } else { // success
 126          $retval = COM_refresh ($_CONF['site_url']
 127                                  . "/polls/index.php?qid=$id&aid=-1");
 128      }
 129  
 130      return $retval;
 131  }
 132  
 133  /**
 134   * polls: delete a comment
 135   *
 136   * @param   int     $cid    Comment to be deleted
 137   * @param   string  $id     Item id to which $cid belongs
 138   * @return  mixed   false for failure, HTML string (redirect?) for success
 139   */
 140  function plugin_deletecomment_polls($cid, $id)
 141  {
 142      global $_CONF, $_TABLES, $_USER;
 143  
 144      $retval = '';
 145  
 146      $has_editPermissions = SEC_hasRights ('polls.edit');
 147      $result = DB_query ("SELECT owner_id,group_id,perm_owner,perm_group,perm_members,perm_anon FROM {$_TABLES['pollquestions']} WHERE qid = '{$id}'");
 148      $A = DB_fetchArray ($result);
 149  
 150      if ($has_editPermissions && SEC_hasAccess ($A['owner_id'],
 151              $A['group_id'], $A['perm_owner'], $A['perm_group'],
 152              $A['perm_members'], $A['perm_anon']) == 3) {
 153          CMT_deleteComment($cid, $id, 'polls');
 154          $retval .= COM_refresh ($_CONF['site_url']
 155                                   . "/polls/index.php?qid=$id&aid=-1");
 156      } else {
 157          COM_errorLog ("User {$_USER['username']} (IP: {$_SERVER['REMOTE_ADDR']}) "
 158                      . "tried to illegally delete comment $cid from poll $id");
 159          $retval .= COM_refresh ($_CONF['site_url'] . '/index.php');
 160      }
 161  
 162      return $retval;
 163  }
 164  
 165  
 166  /**
 167  * Helper function: count number of polls and total number of votes
 168  *
 169  * @return   array(number of polls, number of votes);
 170  *
 171  */
 172  function POLLS_countPollsAndVotes ()
 173  {
 174      global $_TABLES;
 175  
 176      $total_polls = 0;
 177      $total_answers = 0;
 178  
 179      $result = DB_query ("SELECT COUNT(*) AS count FROM {$_TABLES['pollquestions']}" . COM_getPermSQL ());
 180      $A = DB_fetchArray($result);
 181      $total_polls = $A['count'];
 182  
 183      $result = DB_query ("SELECT qid FROM {$_TABLES['pollquestions']}" . COM_getPermSQL ());
 184      $nrows = DB_numRows ($result);
 185      if ($nrows > 0) {
 186          $questions = '';
 187          for ($i = 1; $i <= $nrows; $i++) {
 188              $A = DB_fetchArray($result);
 189              if ($i > 1) {
 190                  $questions .= ',';
 191              }
 192              $questions .= "'" . $A['qid'] . "'";
 193          }
 194          $result = DB_query ("SELECT SUM(votes) FROM {$_TABLES['pollanswers']} WHERE qid IN ({$questions})");
 195          $A = DB_fetchArray($result, true);
 196          $total_answers = $A[0];
 197      }
 198  
 199      return array ($total_polls, $total_answers);
 200  }
 201  
 202  /**
 203  * Shows the statistics for the Polls plugin on stats.php.
 204  * If $showsitestats is 1 then we are to only print the overall stats in the
 205  * 'site statistics box' otherwise we show the detailed stats
 206  *
 207  * @param    int     showsitestate   Flag to let us know which stats to get
 208  */
 209  function plugin_showstats_polls ($showsitestats)
 210  {
 211      global $_CONF, $_TABLES, $LANG_POLLS;
 212  
 213      require_once ($_CONF['path_system'] . 'lib-admin.php');
 214  
 215      $retval = '';
 216  
 217      $result = DB_query ("SELECT qid,question,voters from {$_TABLES['pollquestions']} WHERE (voters > 0)" . COM_getPermSQL ('AND') . " ORDER BY voters DESC LIMIT 10");
 218      $nrows  = DB_numRows ($result);
 219  
 220      if ($nrows > 0) {
 221          $header_arr = array(
 222              array('text'         => $LANG_POLLS['stats_questions'],
 223                    'field'        => 'qid',
 224                    'header_class' => 'stats-header-title'
 225              ),
 226              array('text'         => $LANG_POLLS['stats_votes'],
 227                    'field'        => 'voters',
 228                    'header_class' => 'stats-header-count',
 229                    'field_class'  => 'stats-list-count'
 230              ),
 231          );
 232          $data_arr = array();
 233          $text_arr = array('has_menu' => false,
 234                            'title'    => $LANG_POLLS['stats_top10'],
 235          );
 236          for ($i = 0; $i < $nrows; $i++) {
 237              $A = DB_fetchArray ($result);
 238              $url = $_CONF['site_url'] . '/polls/index.php?qid=' . $A['qid']
 239                   . '&amp;aid=-1';
 240              $qid = '<a href="' . $url . '">' . $A['question'] . '</a>';
 241              $voters = COM_NumberFormat ($A['voters']);
 242              $data_arr[] = array ('qid' => $qid, 'voters' => $voters);
 243          }
 244          $retval .= ADMIN_simpleList ('', $header_arr, $text_arr, $data_arr);
 245      } else {
 246          $retval .= COM_startBlock ($LANG_POLLS['stats_top10']);
 247          $retval .= $LANG_POLLS['stats_none'];
 248          $retval .= COM_endBlock ();
 249      }
 250  
 251      return $retval;
 252  }
 253  
 254  /**
 255  * New stats plugin API function for proper integration with the site stats
 256  *
 257  * @return   array(item text, item count);
 258  *
 259  */
 260  function plugin_statssummary_polls ()
 261  {
 262      global $LANG_POLLS;
 263  
 264      list($total_polls, $total_answers) = POLLS_countPollsAndVotes ();
 265  
 266      $item_count = COM_numberFormat ($total_polls)
 267                  . ' (' . COM_numberFormat ($total_answers) . ')';
 268  
 269      return array ($LANG_POLLS['stats_summary'], $item_count);
 270  }
 271  
 272  
 273  /**
 274  * This will put an option for polls in the command and control block on
 275  * moderation.php
 276  *
 277  */
 278  function plugin_cclabel_polls()
 279  {
 280      global $_CONF, $LANG_POLLS;
 281  
 282      if (SEC_hasRights ('polls.edit')) {
 283          return array ($LANG_POLLS['polls'],
 284                  $_CONF['site_admin_url'] . '/plugins/polls/index.php',
 285                  plugin_geticon_polls ());
 286      }
 287  
 288      return false;
 289  }
 290  
 291  
 292  /**
 293  * returns the administrative option for this plugin
 294  *
 295  */
 296  function plugin_getadminoption_polls()
 297  {
 298      global $_CONF, $_TABLES, $LANG_POLLS;
 299  
 300      if (SEC_hasRights ('polls.edit')) {
 301          $result = DB_query ("SELECT COUNT(*) AS cnt FROM {$_TABLES['pollquestions']}" . COM_getPermSQL ());
 302          $A = DB_fetchArray ($result);
 303          $total_pages = $A['cnt'];
 304  
 305          return array ($LANG_POLLS['polls'],
 306                        $_CONF['site_admin_url'] . '/plugins/polls/index.php',
 307                        $total_pages);
 308      }
 309  }
 310  
 311  
 312  /**
 313  * A user is about to be deleted. Update ownership of any polls owned
 314  * by that user or delete them.
 315  *
 316  * @param   uid   int   User id of deleted user
 317  *
 318  */
 319  function plugin_user_delete_polls ($uid)
 320  {
 321      global $_TABLES, $_PO_CONF;
 322  
 323      if (DB_count ($_TABLES['pollquestions'], 'owner_id', $uid) == 0) {
 324          // there are no polls owned by this user
 325          return;
 326      }
 327  
 328      if ($_PO_CONF['delete_polls'] == 1) {
 329          // delete the polls
 330          $result = DB_query ("SELECT qid FROM {$_TABLES['pollquestions']} WHERE owner_id = $uid");
 331          $numPolls = DB_numRows ($result);
 332          for ($i = 0; $i < $numPolls; $i++) {
 333              list($qid) = DB_fetchArray ($result);
 334              DB_delete ($_TABLES['pollanswers'], 'qid', $qid);
 335              DB_delete ($_TABLES['pollvoters'], 'qid', $qid);
 336              DB_query ("DELETE FROM {$_TABLES['comments']} WHERE sid = '$qid' AND type = 'polls'");
 337          }
 338          DB_delete ($_TABLES['pollquestions'], 'owner_id', $uid);
 339      } else {
 340          // assign ownership to a user from the Root group
 341          $rootgroup = DB_getItem ($_TABLES['groups'], 'grp_id',
 342                                   "grp_name = 'Root'");
 343          $result = DB_query ("SELECT DISTINCT ug_uid FROM {$_TABLES['group_assignments']} WHERE ug_main_grp_id = $rootgroup ORDER BY ug_uid LIMIT 1");
 344          list($rootuser) = DB_fetchArray ($result);
 345  
 346          DB_query ("UPDATE {$_TABLES['pollquestions']} SET owner_id = $rootuser WHERE owner_id = $uid");
 347      }
 348  }
 349  
 350  /**
 351  * Return the current version of code.
 352  * Used in the Plugin Editor to show the registered version and code version
 353  */
 354  function plugin_chkVersion_polls()
 355  {
 356      global $_PO_CONF;
 357  
 358      return $_PO_CONF['version'];
 359  }
 360  
 361  /**
 362  * Geeklog informs us that we're about to be enabled or disabled
 363  *
 364  * @param    boolean     $enable     true = we're being enabled, false = disabled
 365  * @return   void
 366  *
 367  */
 368  function plugin_enablestatechange_polls ($enable)
 369  {
 370      global $_TABLES;
 371  
 372      $is_enabled = $enable ? 1 : 0;
 373  
 374      // toggle Poll block
 375      DB_query ("UPDATE {$_TABLES['blocks']} SET is_enabled = $is_enabled WHERE (type = 'phpblock') AND (phpblockfn = 'phpblock_polls')");
 376  }
 377  
 378  /**
 379  * Removes the datastructures for this plugin from the Geeklog database
 380  *
 381  * This may get called by the install routine to undo anything done to this
 382  * point.  To do that, $steps will have a list of steps to undo
 383  *
 384  * @steps   Array    Holds all the steps that have been completed by the install
 385  *
 386  */
 387  function plugin_uninstall_polls($steps = '')
 388  {
 389      global $_TABLES;
 390  
 391      // Uninstalls the polls plugin
 392  
 393      if (empty($steps) OR $steps['createtable'] == 1) {
 394          // Remove the pollquestions and pollanswers tables
 395          COM_errorLog('Dropping polls answers table',1);
 396          DB_query("DROP TABLE {$_TABLES['pollanswers']}");
 397          COM_errorLog('...success',1);
 398          COM_errorLog('Dropping polls questions table',1);
 399          DB_query("DROP TABLE {$_TABLES['pollquestions']}");
 400          COM_errorLog('...success',1);
 401          COM_errorLog('Dropping pollvoters table',1);
 402          DB_query("DROP TABLE {$_TABLES['pollvoters']}");
 403          COM_errorLog('...success',1);
 404      }
 405  
 406      // Remove polls block
 407      DB_delete ($_TABLES['blocks'], array ('type',     'phpblockfn'),
 408                                     array ('phpblock', 'phpblock_polls'));
 409  
 410      // Remove security for this plugin
 411  
 412      // Remove the polls admin group
 413      $grp_id = DB_getItem($_TABLES['groups'], 'grp_id', "grp_name = 'Polls Admin'");
 414  
 415      if (empty($steps) OR $steps['insertgroup'] == 1) {
 416          COM_errorLog('Attempting to remove the Polls Admin Group', 1);
 417          DB_query("DELETE FROM {$_TABLES['groups']} WHERE grp_id = $grp_id");
 418          COM_errorLog('...success',1);
 419      }
 420  
 421      // Remove related features
 422      $edit_id = DB_getItem($_TABLES['features'], 'ft_id', "ft_name = 'polls.edit'");
 423  
 424      if (empty($steps) OR $steps['addededittogroup'] == 1) {
 425          // Remove access to those features
 426          COM_errorLog('Attempting to remove rights to polls.edit from all groups',1);
 427          DB_query("DELETE FROM {$_TABLES['access']} WHERE acc_ft_id = $edit_id");
 428          COM_errorLog('...success',1);
 429      }
 430  
 431      if (empty($steps) OR $steps['addedrootuserstogroup'] == 1) {
 432          // Remove root users from the group
 433          COM_errorLog('Attempting to remove root users from admin of polls pages');
 434          DB_query("DELETE FROM {$_TABLES['group_assignments']} WHERE ug_main_grp_id = $grp_id");
 435          COM_errorLog('...success',1);
 436      }
 437  
 438      if (empty($steps) OR $steps['insertedfeatureedit'] == 1) {
 439          COM_errorLog('Attempting to remove the polls.edit feature',1);
 440          DB_query("DELETE FROM {$_TABLES['features']} WHERE ft_id = $edit_id");
 441          COM_errorLog('...success',1);
 442      }
 443  
 444      // Unregister the plugin with Geeklog
 445      // Always attempt to remove these entries or lib-common.php would still
 446      // try and read our functions.inc file ...
 447      COM_errorLog('Attempting to unregister the polls plugin from Geeklog',1);
 448      DB_query("DELETE FROM {$_TABLES['plugins']} WHERE pi_name = 'polls'");
 449      COM_errorLog('...success',1);
 450  
 451      COM_errorLog('leaving plugin_uninstall_polls',1);
 452  
 453      return true;
 454  }
 455  
 456  /**
 457  * Get path for the template files.
 458  *
 459  * @param    string  $path   subdirectory within the base template path
 460  * @return   string          full path to template directory
 461  *
 462  */
 463  function polls_templatePath ($path = '')
 464  {
 465      global $_CONF;
 466  
 467      if (empty ($path)) {
 468          $layout_path = $_CONF['path_layout'] . polls;
 469      } else {
 470          $layout_path = $_CONF['path_layout'] . polls . '/' . $path;
 471      }
 472  
 473      if (is_dir ($layout_path)) {
 474          $retval = $layout_path;
 475      } else {
 476          $retval = $_CONF['path'] . 'plugins/polls/templates';
 477          if (!empty ($path)) {
 478              $retval .= '/' . $path;
 479          }
 480      }
 481  
 482      return $retval;
 483  }
 484  
 485  
 486  /**
 487  * Shows a poll form
 488  *
 489  * Shows an HTML formatted poll for the given question ID
 490  *
 491  * @param      string      $qid      ID for poll question
 492  * @see function COM_pollResults
 493  * @see function COM_showPoll
 494  * @return       string  HTML Formatted Poll
 495  *
 496  */
 497  
 498  function POLLS_pollVote( $qid )
 499  {
 500      global $_CONF, $_TABLES, $LANG_POLLS, $LANG01;
 501  
 502      $retval = '';
 503  
 504      $question = DB_query( "SELECT question,voters,commentcode FROM {$_TABLES['pollquestions']} WHERE qid='$qid'" . COM_getPermSql( 'AND' ));
 505      $nquestion = DB_numRows( $question );
 506  
 507      if( $nquestion == 0 )
 508      {
 509          return $retval;
 510      }
 511  
 512      if( !isset( $_COOKIE[$qid] ) && !POLLS_ipAlreadyVoted( $qid ))
 513      {
 514          $Q = DB_fetchArray( $question );
 515          if( $nquestion == 1 )
 516          {
 517              $answers = DB_query( "SELECT answer,aid FROM {$_TABLES['pollanswers']} WHERE qid='$qid' ORDER BY aid" );
 518              $nanswers = DB_numRows( $answers );
 519  
 520              if( $nanswers > 0 )
 521              {
 522                  $poll = new Template( $_CONF['path'] . 'plugins/polls/templates/' );
 523                  $poll->set_file( array( 'panswer' => 'pollanswer.thtml',
 524                                          'block' => 'pollblock.thtml',
 525                                          'comments' => 'pollcomments.thtml' ));
 526                  $poll->set_var( 'site_url', $_CONF['site_url'] );
 527                  $poll->set_var( 'layout_url', $_CONF['layout_url'] );
 528  
 529                  $poll->set_var( 'poll_question', $Q['question'] );
 530                  $poll->set_var( 'poll_id', $qid );
 531                  $poll->set_var( 'num_votes', COM_numberFormat( $Q['voters'] ));
 532                  $poll->set_var( 'poll_vote_url', $_CONF['site_url']
 533                          . '/polls/index.php');
 534                  $poll->set_var( 'poll_results_url', $_CONF['site_url']
 535                          . '/polls/index.php?qid=' . $qid . '&amp;aid=-1');
 536  
 537                  $poll->set_var( 'lang_vote', $LANG_POLLS['vote'] );
 538                  $poll->set_var( 'lang_votes', $LANG_POLLS['votes'] );
 539                  $poll->set_var( 'lang_results', $LANG_POLLS['results'] );
 540  
 541                  for( $i = 1; $i <= $nanswers; $i++ )
 542                  {
 543                      $A = DB_fetchArray( $answers );
 544                      $poll->set_var( 'answer_id', $A['aid'] );
 545                      $poll->set_var( 'answer_text', $A['answer'] );
 546                      $poll->parse( 'poll_answers', 'panswer', true );
 547                  }
 548  
 549                  if( $Q['commentcode'] >= 0 )
 550                  {
 551                      $num_comments = DB_count( $_TABLES['comments'],
 552                              array( 'sid', 'type' ), array( $qid, 'polls' ));
 553                      $poll->set_var( 'num_comments',
 554                              COM_numberFormat( $num_comments ));
 555                      $poll->set_var( 'lang_comments', $LANG01[3] );
 556                      $poll->set_var( 'poll_comments_url', $_CONF['site_url'] .
 557                          '/polls/index.php?qid=' . $qid . '&amp;aid=-1#comments');
 558                      $poll->parse( 'poll_comments', 'comments', true );
 559                  }
 560                  else
 561                  {
 562                      $poll->set_var( 'poll_comments', '' );
 563                      $poll->set_var( 'poll_comments_url', '' );
 564                  }
 565  
 566                  $retval =  $poll->finish( $poll->parse( 'output', 'block' )) . LB;
 567              }
 568          }
 569      }
 570      else
 571      {
 572          $retval .= POLLS_pollResults( $qid );
 573      }
 574  
 575      return $retval;
 576  }
 577  
 578  /**
 579  * This shows a poll
 580  *
 581  * This will determine if a user needs to see the poll form OR the poll
 582  * result.
 583  *
 584  * @param        int        $sise       Size in pixels of poll results
 585  * @param        string     $qid        Question ID to show (optional)
 586  * @see function COM_pollVote
 587  * @see function COM_pollResults
 588  * @return    String  HTML Formated string of Poll
 589  *
 590  */
 591  
 592  function POLLS_showPoll( $size, $qid='' )
 593  {
 594      global $_CONF, $_PO_CONF, $_TABLES;
 595  
 596      $retval = '';
 597  
 598      DB_query( "DELETE FROM {$_TABLES['pollvoters']} WHERE date < unix_timestamp() - {$_PO_CONF['polladdresstime']}" );
 599  
 600      if( !empty( $qid ))
 601      {
 602          if( !isset( $_COOKIE[$qid] ) && !POLLS_ipAlreadyVoted( $qid ))
 603          {
 604              $retval .= POLLS_pollVote( $qid );
 605          }
 606          else
 607          {
 608              $retval .= POLLS_pollResults( $qid, $size );
 609          }
 610      }
 611      else
 612      {
 613          $result = DB_query( "SELECT qid from {$_TABLES['pollquestions']} WHERE display = 1 ORDER BY date DESC" );
 614          $nrows = DB_numRows( $result );
 615          
 616          $title = DB_getItem( $_TABLES['blocks'], 'title', "name='poll_block'" );
 617  
 618          if( $nrows > 0 )
 619          {
 620              for( $i = 1; $i <= $nrows; $i++ )
 621              {
 622                  $Q = DB_fetchArray( $result );
 623                  $qid = $Q['qid'];
 624  
 625                  if( $size < 120 ) // assume we're in the poll block
 626                  {
 627                      if( $i > 1 )
 628                      {
 629                          $retval .= COM_startBlock( $title, '',
 630                               COM_getBlockTemplate( 'poll_block', 'header' ));
 631                      }
 632                  }
 633                  else // assume we're in polls/index.php
 634                  {
 635                      $retval .= COM_startBlock( $title );
 636                  }
 637  
 638                  if( !isset( $_COOKIE[$qid] ) && !POLLS_ipAlreadyVoted( $qid ))
 639                  {
 640                      $retval .= POLLS_pollVote( $qid );
 641                  }
 642                  else
 643                  {
 644                      $retval .= POLLS_pollResults( $qid, $size );
 645                  }
 646                  
 647                  if( $size < 120 )
 648                  {
 649                      if ( $i < $nrows )
 650                      {
 651                          $retval .= COM_endBlock( COM_getBlockTemplate( 
 652                               'poll_block', 'footer' ));
 653                      }
 654                  }
 655                  else
 656                  {
 657                      $retval .= COM_endBlock();
 658                  }
 659                  
 660              }
 661          }
 662      }
 663  
 664      return $retval;
 665  }
 666  
 667  /**
 668  * Shows the results of a poll
 669  *
 670  * Shows the poll results for a given poll question
 671  *
 672  * @param        string      $qid        ID for poll question to show
 673  * @param        int         $scale      Size in pixels to scale formatted results to
 674  * @param        string      $order      'ASC' or 'DESC' for Comment ordering (SQL statment ordering)
 675  * @param        string      $mode       Comment Mode possible values 'nocomment', 'flat', 'nested', 'threaded'
 676  * @see COM_pollVote
 677  * @see COM_showPoll
 678  * @return     string   HTML Formated Poll Results
 679  *
 680  */
 681  function POLLS_pollResults( $qid, $scale=400, $order='', $mode='' )
 682  {
 683      global $_CONF, $_TABLES, $_PO_CONF, $LANG01, $LANG_POLLS, $_COM_VERBOSE;
 684  
 685      $retval = '';
 686  
 687      $question = DB_query( "SELECT question,voters,commentcode,owner_id,group_id,perm_owner,perm_group,perm_members,perm_anon FROM {$_TABLES['pollquestions']} WHERE qid='$qid'" );
 688      $Q = DB_fetchArray( $question );
 689  
 690      if( SEC_hasAccess( $Q['owner_id'], $Q['group_id'], $Q['perm_owner'], $Q['perm_group'], $Q['perm_members'], $Q['perm_anon']) == 0 )
 691      {
 692          return $retval;
 693      }
 694  
 695      $nquestion = DB_numRows( $question );
 696      if( $nquestion == 1 )
 697      {
 698          if( $_PO_CONF['answerorder'] == 'voteorder' )
 699          {
 700              $order = "votes DESC";
 701          }
 702          else
 703          {
 704              $order = "aid";
 705          }
 706  
 707          $answers = DB_query( "SELECT votes,answer,remark FROM {$_TABLES['pollanswers']} WHERE qid='$qid' ORDER BY $order" );
 708  
 709          $nanswers = DB_numRows( $answers );
 710  
 711          if( $_COM_VERBOSE )
 712          {
 713              COM_errorLog( "got $answers answers in COM_pollResults", 1 );
 714          }
 715  
 716          if( $nanswers > 0 )
 717          {
 718  
 719              $poll = new Template( $_CONF['path'] . 'plugins/polls/templates/' );
 720              $poll->set_file( array( 'result' => 'pollresult.thtml',
 721                                      'comments' => 'pollcomments.thtml',
 722                                      'votes_bar' => 'pollvotes_bar.thtml',
 723                                      'votes_num' => 'pollvotes_num.thtml' ));
 724              $poll->set_var( 'site_url', $_CONF['site_url'] );
 725              $poll->set_var( 'layout_url', $_CONF['layout_url'] );
 726  
 727              $poll->set_var( 'poll_question', $Q['question'] );
 728              $poll->set_var( 'poll_id', $qid );
 729              $poll->set_var( 'num_votes', COM_numberFormat( $Q['voters'] ));
 730  
 731              $poll->set_var( 'lang_votes', $LANG_POLLS['votes'] );
 732  
 733              for( $i = 1; $i <= $nanswers; $i++ )
 734              {
 735                  $A = DB_fetchArray( $answers );
 736  
 737                  if( $Q['voters'] == 0 )
 738                  {
 739                      $percent = 0;
 740                  }
 741                  else
 742                  {
 743                      $percent = $A['votes'] / $Q['voters'];
 744                  }
 745                  $poll->set_var( 'cssida', 1);
 746                  $poll->set_var( 'cssidb', 2);
 747                  $poll->set_var( 'answer_text', $A['answer'] );
 748                  $poll->set_var( 'remark_text', $A['remark'] );
 749                  $poll->set_var( 'answer_counter', $i );
 750                  $poll->set_var( 'answer_odd', (( $i - 1 ) % 2 ));
 751                  $poll->set_var( 'answer_num', COM_numberFormat( $A['votes'] ));
 752                  $poll->set_var( 'answer_percent',
 753                                  sprintf( '%.2f', $percent * 100 ));
 754                  if( $scale < 120 )
 755                  {
 756                      $poll->parse( 'poll_votes', 'votes_num', true );
 757                  }
 758                  else
 759                  {
 760                      $width = $percent * $scale;
 761                      $poll->set_var( 'bar_width', $width );
 762                      $poll->parse( 'poll_votes', 'votes_bar', true );
 763                  }
 764              }
 765  
 766              if( $Q['commentcode'] >= 0 )
 767              {
 768                  $num_comments = DB_count( $_TABLES['comments'],
 769                          array( 'sid', 'type' ), array( $qid, 'polls' ));
 770                  $poll->set_var( 'num_comments',
 771                          COM_numberFormat( $num_comments ));
 772                  $poll->set_var( 'lang_comments', $LANG01[3] );
 773                  $poll->set_var( 'poll_comments_url', $_CONF['site_url'] .
 774                          '/polls/index.php?qid=' . $qid . '&amp;aid=-1#comments');
 775                  $poll->parse( 'poll_comments', 'comments', true );
 776              }
 777              else
 778              {
 779                  $poll->set_var( 'poll_comments_url', '' );
 780                  $poll->set_var( 'poll_comments', '' );
 781              }
 782  
 783              $poll->set_var( 'lang_pollquestions', $LANG_POLLS['pollquestions'] );
 784  
 785              $retval .= $poll->finish( $poll->parse( 'output', 'result' ));
 786  
 787              if( $scale > 399 && $Q['commentcode'] >= 0 )
 788              {
 789                  $delete_option = ( SEC_hasRights( 'polls.edit' ) &&
 790                      SEC_hasAccess( $Q['owner_id'], $Q['group_id'],
 791                      $Q['perm_owner'], $Q['perm_group'], $Q['perm_members'],
 792                      $Q['perm_anon'] ) == 3 ? true : false );
 793                  require_once $_CONF['path_system'] . 'lib-comment.php';
 794                  $retval .= CMT_userComments( $qid, $Q['question'], 'polls',
 795                                               $order, $mode, 0, 1, false, $delete_option );
 796              }
 797          }
 798      }
 799  
 800      return $retval;
 801  }
 802  
 803  /**
 804  * Check if we already have a vote from this IP address
 805  *
 806  * @param    string  $qid    Poll ID
 807  * @param    string  $ip     (optional) IP address
 808  * @return   boolean         true: IP already voted; false: didn't
 809  *
 810  */
 811  function POLLS_ipAlreadyVoted( $qid, $ip = '' )
 812  {
 813      global $_TABLES;
 814  
 815      $retval = false;
 816  
 817      if( empty( $ip ))
 818      {
 819          $ip = $_SERVER['REMOTE_ADDR'];
 820      }
 821  
 822      if( DB_count( $_TABLES['pollvoters'], array( 'ipaddress', 'qid' ),
 823                                            array( $ip,         $qid )) > 0 )
 824      {
 825          $retval = true;
 826      }
 827  
 828      return $retval;
 829  }
 830  
 831  function phpblock_polls()
 832  {
 833      $retval = POLLS_showPoll( 60 );
 834  
 835      return $retval;
 836  }
 837  
 838  
 839  /**
 840  * Returns the URL of the plugin's icon
 841  *
 842  * @return   string      URL of the icon
 843  *
 844  */
 845  function plugin_geticon_polls ()
 846  {
 847      global $_CONF;
 848  
 849      return $_CONF['site_url'] . '/polls/images/polls.png';
 850  }
 851  
 852  /**
 853  * Plugin should display [a] comment[s]
 854  *
 855  * @param   string  $id     Unique idenifier for item comment belongs to
 856  * @param   int     $cid    Comment id to display (possibly including sub-comments)
 857  * @param   string  $title  Page/comment title
 858  * @param   string  $order  'ASC' or 'DSC' or blank                            
 859  * @param   string  $format 'threaded', 'nested', or 'flat'                    
 860  * @param   int     $page   Page number of comments to display                 
 861  * @param   boolean $view   True to view comment (by cid), false to display (by $pid)
 862  * @return  mixed   results of calling the plugin_displaycomment_ function     
 863  */
 864  function plugin_displaycomment_polls ($id, $cid, $title, $order, $format, $page, $view)
 865  {
 866      global $_TABLES, $LANG_ACCESS;
 867  
 868      $retval = '';
 869  
 870      $sql = "SELECT COUNT(*) AS count, owner_id, group_id, perm_owner, perm_group, perm_members, perm_anon FROM {$_TABLES['pollquestions']} WHERE (qid = '$id')" . COM_getPermSQL('AND') . ' GROUP BY qid';
 871      $result = DB_query ($sql);
 872      $A = DB_fetchArray ($result);
 873      $allowed = $A['count'];
 874  
 875      if ($allowed == 1) {
 876          $delete_option = (SEC_hasRights ('polls.edit') &&
 877                  (SEC_hasAccess ($A['owner_id'], $A['group_id'],
 878                      $A['perm_owner'], $A['perm_group'], $A['perm_members'],
 879                      $A['perm_anon']) == 3));
 880          $retval .= CMT_userComments ($id, $title, 'polls', $order, $format,
 881                                       $cid, $page, $view, $delete_option);
 882      } else {
 883          $retval .= COM_startBlock ($LANG_ACCESS['accessdenied'], '',
 884                          COM_getBlockTemplate ('_msg_block', 'header'))
 885                  . $LANG_ACCESS['storydenialmsg']
 886                  . COM_endBlock (COM_getBlockTemplate ('_msg_block', 'footer'));
 887      }
 888  
 889      return $retval;
 890  }
 891  
 892  function plugin_getListField_polls($fieldname, $fieldvalue, $A, $icon_arr)
 893  {
 894      global $_CONF, $LANG25, $LANG_ACCESS;
 895  
 896      $retval = '';
 897  
 898      $access = SEC_hasAccess ($A['owner_id'], $A['group_id'],
 899                               $A['perm_owner'], $A['perm_group'],
 900                               $A['perm_members'], $A['perm_anon']);
 901      if ($access > 0) {
 902          switch($fieldname) {
 903              case 'edit':
 904                  if ($access == 3) {
 905                      $retval = "<a href=\"{$_CONF['site_admin_url']}/plugins/polls/index.php?mode=edit&amp;qid={$A['qid']}\">{$icon_arr['edit']}</a>";
 906                  }
 907                  break;
 908              case 'unixdate':
 909                  $retval = strftime ($_CONF['daytime'], $A['unixdate']);
 910                  break;
 911              case 'question':
 912                  $retval = "<a href=\"{$_CONF['site_url']}/polls/index.php?qid={$A['qid']}&amp;aid=-1\">$fieldvalue</a>";
 913                  break;
 914              case 'access':
 915                  if ($access == 3) {
 916                      $access = $LANG_ACCESS['edit'];
 917                  } else {
 918                      $access = $LANG_ACCESS['readonly'];
 919                  }
 920                  $retval = $access;
 921                  break;
 922              case 'display':
 923                  if ($A['display'] == 1) {
 924                      $retval = $LANG25[25];
 925                  } else {
 926                      $retval = $LANG25[26];
 927                  }
 928                  break;
 929              case 'voters':
 930                  $retval = COM_numberFormat ($A['voters']);
 931                  break;
 932              default:
 933                  $retval = $fieldvalue;
 934                  break;
 935          }
 936      } else {
 937          $retval = false;
 938      }
 939  
 940      return $retval;
 941  }
 942  
 943  /**
 944  * Set template variables
 945  *
 946  * @param    string  $templatename   name of template, e.g. 'header'
 947  * @param    ref     $template       reference of actual template
 948  * @return   void
 949  *
 950  * Note: A plugin should use its name as a prefix for the names of its
 951  * template variables, e.g. 'polls_xxx' and 'lang_polls_xxx'.
 952  * 'button_polls' is an exception, as such a variable existed for header.thtml
 953  * in Geeklog 1.3.11 and earlier, where the Polls were an integral part
 954  * of Geeklog. It is added here for backward-compatibility.
 955  *
 956  */
 957  function plugin_templatesetvars_polls ($templatename, &$template)
 958  {
 959      global $LANG_POLLS;
 960  
 961      if ($templatename == 'header') {
 962          $template->set_var ('button_polls', $LANG_POLLS['polls']);
 963      }
 964  }
 965  
 966  /**
 967  * Update the Polls plugin
 968  *
 969  * @return   int     Number of message to display (true = generic success msg)
 970  *
 971  */
 972  function plugin_upgrade_polls () 
 973  {
 974      global $_TABLES, $_PO_CONF;
 975  
 976      // the plugin needs this function so complain when it doesn't exist
 977      if (!function_exists ('SEC_getGroupDropdown')) {
 978          return 3002;
 979      }
 980  
 981      $current_version = DB_getItem ($_TABLES['plugins'], 'pi_version',
 982                                     "pi_name = 'polls'");
 983      if ($current_version == '1.0') {
 984          $sql = "ALTER TABLE {$_TABLES['pollanswers']} ADD remark VARCHAR(255) NULL AFTER votes;";  
 985          $result = DB_query ($sql);
 986      }
 987  
 988      DB_query ("UPDATE {$_TABLES['plugins']} SET pi_version = '{$_PO_CONF['version']}', pi_gl_version = '" . VERSION . "' WHERE pi_name = 'polls'");
 989  
 990      return true;
 991  }
 992  
 993  ?>


Généré le : Wed Nov 21 12:27:40 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics