[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/plugins/serendipity_event_karma/ -> serendipity_event_karma.php (source)

   1  <?php # $Id: serendipity_event_karma.php 1686 2007-04-25 10:00:36Z garvinhicking $
   2  
   3  
   4  if (IN_serendipity !== true) {
   5      die ("Don't hack!");
   6  }
   7  
   8  // Probe for a language include with constants. Still include defines later on, if some constants were missing
   9  $probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
  10  if (file_exists($probelang)) {
  11      include $probelang;
  12  }
  13  
  14  include dirname(__FILE__) . '/lang_en.inc.php';
  15  
  16  @define('PLUGIN_KARMA_VERSION', '1.3');
  17  
  18  class serendipity_event_karma extends serendipity_event
  19  {
  20      var $karmaVote    = '';
  21      var $karmaId      = '';
  22      var $karmaTimeOut = '';
  23      var $karmaVoting  = '';
  24      var $title        = PLUGIN_KARMA_NAME;
  25  
  26      function introspect(&$propbag)
  27      {
  28          global $serendipity;
  29  
  30          $propbag->add('name',          PLUGIN_KARMA_NAME);
  31          $propbag->add('description',   PLUGIN_KARMA_BLAHBLAH);
  32          $propbag->add('stackable',     false);
  33          $propbag->add('author',        'Garvin Hicking');
  34          $propbag->add('version',       '1.9');
  35          $propbag->add('requirements',  array(
  36              'serendipity' => '0.8',
  37              'smarty'      => '2.6.7',
  38              'php'         => '4.1.0'
  39          ));
  40          $propbag->add('event_hooks',   array('frontend_configure' => true, 'entry_display' => true, 'css' => true, 'event_additional_statistics' => true));
  41          $propbag->add('groups', array('STATISTICS'));
  42          $propbag->add('configuration', array('karma_active', 'visits_active', 'exits_active', 'max_entrytime', 'max_votetime', 'extended_only', 'max_karmatime', 'logging'));
  43      }
  44  
  45      function introspect_config_item($name, &$propbag)
  46      {
  47          switch($name) {
  48              case 'max_entrytime':
  49                  $propbag->add('type', 'string');
  50                  $propbag->add('name', PLUGIN_KARMA_ENTRYTIME);
  51                  $propbag->add('description', PLUGIN_KARMA_ENTRYTIME_BLAHBLAH);
  52                  $propbag->add('default', 1440);
  53                  break;
  54  
  55              case 'max_votetime':
  56                  $propbag->add('type', 'string');
  57                  $propbag->add('name', PLUGIN_KARMA_VOTINGTIME);
  58                  $propbag->add('description', PLUGIN_KARMA_VOTINGTIME_BLAHBLAH);
  59                  $propbag->add('default', 5);
  60                  break;
  61  
  62              case 'max_karmatime':
  63                  $propbag->add('type', 'string');
  64                  $propbag->add('name', PLUGIN_KARMA_MAXKARMA);
  65                  $propbag->add('description', PLUGIN_KARMA_MAXKARMA_BLAHBLAH);
  66                  $propbag->add('default', 7);
  67                  break;
  68  
  69              case 'karma_active':
  70                  $propbag->add('type', 'boolean');
  71                  $propbag->add('name', PLUGIN_KARMA_ACTIVE);
  72                  $propbag->add('description', PLUGIN_KARMA_ACTIVE_BLAHBLAH);
  73                  $propbag->add('default', 'true');
  74                  break;
  75  
  76              case 'visits_active':
  77                  $propbag->add('type', 'boolean');
  78                  $propbag->add('name', PLUGIN_KARMA_VISITS);
  79                  $propbag->add('description', PLUGIN_KARMA_VISITS_BLAHBLAH);
  80                  $propbag->add('default', 'true');
  81                  break;
  82  
  83              case 'exits_active':
  84                  $propbag->add('type', 'boolean');
  85                  $propbag->add('name', SHOWS_TOP_EXIT);
  86                  $propbag->add('description', '');
  87                  $propbag->add('default', 'false');
  88                  break;
  89  
  90              case 'extended_only':
  91                  $propbag->add('type', 'boolean');
  92                  $propbag->add('name', PLUGIN_KARMA_EXTENDEDONLY);
  93                  $propbag->add('description', PLUGIN_KARMA_EXTENDEDONLY_BLAHBLAH);
  94                  $propbag->add('default', 'false');
  95                  break;
  96  
  97              case 'logging':
  98                  $propbag->add('type', 'boolean');
  99                  $propbag->add('name', PLUGIN_KARMA_LOGGING);
 100                  $propbag->add('description', PLUGIN_KARMA_LOGGING_BLAHBLAH);
 101                  $propbag->add('default', 'false');
 102                  break;
 103  
 104              default:
 105                      return false;
 106          }
 107          return true;
 108      }
 109  
 110      function checkScheme() {
 111          global $serendipity;
 112  
 113          $version = $this->get_config('version', '0.9');
 114  
 115          if ($version == '1.1') {
 116              $q   = "ALTER TABLE {$serendipity['dbPrefix']}karma ADD visits INT(11) default 0";
 117              $sql = serendipity_db_schema_import($q);
 118              $this->set_config('version', PLUGIN_KARMA_VERSION);
 119          } elseif ($version == '1.0') {
 120              $q   = "ALTER TABLE {$serendipity['dbPrefix']}karma ADD visits INT(11) default 0";
 121              $sql = serendipity_db_schema_import($q);
 122  
 123              $q   = "CREATE TABLE {$serendipity['dbPrefix']}karmalog (
 124                          entryid int(11) default null,
 125                          points int(4) default null,
 126                          ip varchar(15),
 127                          user_agent varchar(255),
 128                          votetime int(11) default null
 129                      )";
 130              $sql = serendipity_db_schema_import($q);
 131              $this->set_config('version', PLUGIN_KARMA_VERSION);
 132          } elseif ($version != PLUGIN_KARMA_VERSION) {
 133              $q   = "CREATE TABLE {$serendipity['dbPrefix']}karma (
 134                          entryid int(11) default null,
 135                          points int(4) default null,
 136                          votes int(4) default null,
 137                          lastvote int(10) {UNSIGNED} NULL,
 138                          visits int(11) default null
 139                      )";
 140              $sql = serendipity_db_schema_import($q);
 141  
 142              $q   = "CREATE TABLE {$serendipity['dbPrefix']}karmalog (
 143                          entryid int(11) default null,
 144                          points int(4) default null,
 145                          ip varchar(15),
 146                          user_agent varchar(255),
 147                          votetime int(11) default null
 148                      )";
 149              $sql = serendipity_db_schema_import($q);
 150  
 151              $q   = "CREATE INDEX kfetch ON {$serendipity['dbPrefix']}karma (entryid, lastvote);";
 152              $sql = serendipity_db_schema_import($q);
 153  
 154              $q   = "CREATE INDEX kentryid ON {$serendipity['dbPrefix']}karma (entryid);";
 155              $sql = serendipity_db_schema_import($q);
 156              $this->set_config('version', PLUGIN_KARMA_VERSION);
 157          }
 158  
 159          return true;
 160      }
 161  
 162      function generate_content(&$title)
 163      {
 164          $title       = $this->title;
 165      }
 166  
 167      function prepareExits($entries, $get = false) {
 168          static $exits = null;
 169          global $serendipity;
 170  
 171          if ($exits === null) {
 172              $q = 'SELECT entry_id, SUM(count) AS exits
 173                      FROM ' . $serendipity['dbPrefix'] . 'exits
 174                     WHERE entry_id IN (' . implode(', ', $entries) . ') GROUP BY entry_id';
 175  
 176              $sql = serendipity_db_query($q);
 177              $exits = array();
 178              if (is_array($sql)) {
 179                  foreach($sql AS $idx => $row) {
 180                      $exits[$row['entry_id']] = (int)$row['exits'];
 181                  }
 182              }
 183          }
 184  
 185          if ($get) {
 186              return $exits[$entries];
 187          }
 188  
 189          return true;
 190      }
 191  
 192      function getExits($entryid, $get_prepared = false) {
 193          global $serendipity;
 194          static $karma_exits = null;
 195  
 196          if ($karma_exits === null) {
 197              $karma_exits = ' <span class="serendipity_karmaVoting_exits_sep">|</span> <span class="serendipity_karmaVoting_exits">' . TOP_EXITS . '</span> <span class="serendipity_karmaVoting_exits_num">(%d)</span>';
 198          }
 199  
 200          if ($get_prepared) {
 201              $points = $this->prepareExits($entryid, true);
 202          } else {
 203              $res = serendipity_db_query("SELECT sum(count) AS exits FROM {$serendipity['dbPrefix']}exits WHERE entry_id = " . (int)$entryid . " GROUP BY entry_id", true, 'assoc');
 204              if (is_array($res) && isset($res['exits'])) {
 205                  $points = $res['exits'];
 206              } else {
 207                  $points = 0;
 208              }
 209          }
 210  
 211          return sprintf($karma_exits, $points);
 212      }
 213  
 214      function event_hook($event, &$bag, &$eventData, $addData = null) {
 215          global $serendipity;
 216  
 217          $hooks = &$bag->get('event_hooks');
 218  
 219          if (isset($hooks[$event])) {
 220              switch($event) {
 221                  case 'frontend_configure':
 222                      if (!isset($serendipity['COOKIE']['karmaVote'])) {
 223                          serendipity_setCookie('karmaVote', serialize(array()));
 224                      }
 225  
 226                      if (isset($serendipity['GET']['id'])) {
 227                          $entryid = (int)serendipity_db_escape_string($serendipity['GET']['id']);
 228                      } elseif (preg_match(PAT_COMMENTSUB, $_SERVER['REQUEST_URI'], $matches)) {
 229                          $entryid = (int)$matches[1];
 230                      } else {
 231                          $entryid = false;
 232                      }
 233  
 234                      if ($entryid && empty($serendipity['GET']['adminAction'])) {
 235                          $track_clicks  = serendipity_db_bool($this->get_config('visits_active', true));
 236                          if ($track_clicks) {
 237                              $sql = serendipity_db_query('UPDATE ' . $serendipity['dbPrefix'] . 'karma SET visits = visits + 1 WHERE entryid = ' . $entryid, true);
 238                              if (serendipity_db_affected_rows() < 1) {
 239                                  serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}karma (entryid, points, votes, lastvote, visits) VALUES ('$entryid', 0, 0, 0, 1)");
 240                              }
 241                          }
 242                      }
 243  
 244                      if (!isset($serendipity['GET']['karmaId']) || !isset($serendipity['GET']['karmaVote'])) {
 245                          return;
 246                      }
 247  
 248                      $this->karmaId     = (int)$serendipity['GET']['karmaId'];
 249                      $this->karmaVoting = (int)$serendipity['GET']['karmaVote'];
 250  
 251                      if (!isset($serendipity['COOKIE']['karmaVote'])) {
 252                          $this->karmaVote = 'nocookie';
 253                          return;
 254                      }
 255  
 256                      $karma   = unserialize($serendipity['COOKIE']['karmaVote']);
 257  
 258                      if (!is_array($karma) || !is_numeric($this->karmaVoting) || !is_numeric($this->karmaId) || $this->karmaVoting > 2 || $this->karmaVoting < -2) {
 259                          $this->karmaVote = 'invalid1';
 260                          return;
 261                      }
 262  
 263                      if (!empty($karma[$this->karmaId])) {
 264                          $this->karmaVote = 'alreadyvoted';
 265                          return ;
 266                      }
 267  
 268                      if (stristr($_SERVER['HTTP_USER_AGENT'], 'google')) {
 269                          // We don't want googlebots hitting the karma-voting
 270                          $this->karmaVote = 'invalid1';
 271                          return ;
 272                      }
 273  
 274                      // Voting takes place here.
 275                      $q = 'SELECT *
 276                              FROM ' . $serendipity['dbPrefix'] . 'entries AS e
 277                   LEFT OUTER JOIN ' . $serendipity['dbPrefix'] . 'karma   AS k
 278                                ON e.id = k.entryid
 279                             WHERE e.id = ' . serendipity_db_escape_string($this->karmaId) . ' LIMIT 1';
 280                      $row = serendipity_db_query($q, true);
 281  
 282                      if (!isset($row) || !is_array($row)) {
 283                          $this->karmaVote = 'invalid2';
 284                          return;
 285                      }
 286  
 287                      $now = time();
 288                      if ($row['votes'] === '0' || $row['votes'] > 0) {
 289                          // Votes for this entry already exist. Do some checking.
 290                          $max_entrytime = $this->get_config('max_entrytime', 1440) * 60;
 291                          $max_votetime  = $this->get_config('max_votetime', 5)     * 60;
 292                          $max_karmatime = $this->get_config('max_karmatime', 7)    * 24 * 60 * 60;
 293  
 294                          if ($row['timestamp'] < ($now - $max_karmatime)) {
 295                              $this->karmaVote = 'timeout2';
 296                              return;
 297                          }
 298  
 299                          if (($row['timestamp'] > ($now - $max_entrytime)) || ($row['lastvote'] + $max_votetime < $now) || $row['lastvote'] == 0) {
 300                              // Update votes
 301                              $q = sprintf(
 302                                "UPDATE {$serendipity['dbPrefix']}karma
 303                                    SET points   = %s,
 304                                        votes    = %s,
 305                                        lastvote = %s
 306                                  WHERE entryid  = %s",
 307                                $row['points'] + $this->karmaVoting,
 308                                $row['votes']  + 1,
 309                                $now,
 310                                $this->karmaId
 311                              );
 312  
 313                              serendipity_db_query($q);
 314                          } else {
 315                              $this->karmaVote    = 'timeout';
 316                              $this->karmaTimeOut = abs(round(($now - ($row['lastvote'] + $max_votetime)) / 60, 1));
 317                              return;
 318                          }
 319                      } else {
 320                          // No Votes. Just insert it.
 321                          $q = sprintf(
 322                            "INSERT INTO {$serendipity['dbPrefix']}karma
 323                                         (entryid, points, votes, lastvote)
 324                                  VALUES (%s,      %s,     %s,    %s)",
 325                            $this->karmaId,
 326                            $this->karmaVoting,
 327                            1,
 328                            $now
 329                          );
 330  
 331                          $sql = serendipity_db_query($q);
 332                      }
 333  
 334                      if (serendipity_db_bool($this->get_config('logging', false))) {
 335                          $q = sprintf(
 336                            "INSERT INTO {$serendipity['dbPrefix']}karmalog
 337                                         (entryid, points, ip, user_agent, votetime)
 338                                  VALUES (%s, %s, '%s', '%s', %s)",
 339                            $this->karmaId,
 340                            $this->karmaVoting,
 341                            serendipity_db_escape_string($_SERVER['REMOTE_ADDR']),
 342                            substr(serendipity_db_escape_string($_SERVER['HTTP_USER_AGENT']), 0, 255),
 343                            $now
 344                          );
 345                          $sql = serendipity_db_query($q);
 346                          if (is_string($sql)) {
 347                              mail($serendipity['serendipityEmail'] , 'KARMA ERROR', $q . '<br />' . $sql . '<br />');
 348                          }
 349                      }
 350  
 351                      $karma[$this->karmaId] = $this->karmaVoting;
 352                      $this->karmaVote       = 'voted';
 353                      serendipity_setCookie('karmaVote', serialize($karma));
 354  
 355                      return true;
 356                      break;
 357  
 358                  case 'css':
 359                      if (strpos($eventData, '.serendipity_karmaVoting')) {
 360                          // class exists in CSS, so a user has customized it and we don't need default
 361                          return true;
 362                      }
 363  ?>
 364  
 365  .serendipity_karmaVoting {
 366      margin-left: auto;
 367      margin-right: 0px;
 368      text-align: right;
 369      font-size: 7pt;
 370      display: block;
 371      margin-top: 5px;
 372      margin-bottom: 0px;
 373  }
 374  
 375  .serendipity_karmaVoting a {
 376      font-size: 7pt;
 377      text-decoration: none;
 378  }
 379  
 380  .serendipity_karmaVoting a:hover {
 381      color: green;
 382  }
 383  
 384  .serendipity_karmaError {
 385      color: #FF8000;
 386  }
 387  
 388  .serendipity_karmaSuccess {
 389      color: green;
 390  }
 391  <?php
 392                      return true;
 393                      break;
 394  
 395                  case 'event_additional_statistics':
 396                      $sql = array();
 397                      $sql['visits_top']    = array('visits', 'DESC');
 398                      $sql['visits_bottom'] = array('visits', 'ASC');
 399                      $sql['votes_top']     = array('votes', 'DESC');
 400                      $sql['votes_bottom']  = array('votes', 'ASC');
 401                      $sql['points_top']    = array('points', 'DESC');
 402                      $sql['points_bottom'] = array('points', 'ASC');
 403  
 404                      foreach($sql AS $key => $rows) {
 405                          $q = "SELECT e.id,
 406                                       e.title,
 407                                       e.timestamp,
 408                                       SUM(k.{$rows[0]}) AS no
 409                                  FROM {$serendipity['dbPrefix']}karma
 410                                       AS k
 411                                  JOIN {$serendipity['dbPrefix']}entries
 412                                       AS e
 413                                    ON k.entryid = e.id
 414                              WHERE k.{$rows[0]} IS NOT NULL AND k.{$rows[0]} != 0
 415                              GROUP BY e.id, e.title, e.timestamp ORDER BY no {$rows[1]} LIMIT {$addData['maxitems']}";
 416                          $sql_rows = serendipity_db_query($q);
 417  ?>
 418      <dt><strong><?php echo constant('PLUGIN_KARMA_STATISTICS_' . strtoupper($key)); ?></strong></dt>
 419      <dl>
 420  <?php
 421                          if (is_array($sql_rows)) {
 422                              foreach($sql_rows AS $id => $row) {
 423      ?>
 424          <dt><strong><a href="<?php echo serendipity_archiveURL($row['id'], $row['title'], 'serendipityHTTPPath', true, array('timestamp' => $row['timestamp'])); ?>"><?php echo htmlspecialchars($row['title']); ?></a></strong></dt>
 425          <dd><?php echo $row['no']; ?> <?php echo constant('PLUGIN_KARMA_STATISTICS_' . strtoupper($rows[0]) . '_NO'); ?></dd>
 426      <?php
 427                              }
 428                          }
 429  ?>
 430      </dl>
 431  <?php
 432                      }
 433  
 434                      return true;
 435                      break;
 436  
 437                  case 'entry_display':
 438                      if ($this->get_config('version') != PLUGIN_KARMA_VERSION) {
 439                          $this->checkScheme();
 440                      }
 441  
 442                      // Check whether the cache plugin is used. If so, we need to append our karma-voting output
 443                      // to the cached version, since that one is used instead of the 'extended' key later on.
 444                      $extended_key = &$this->getFieldReference('add_footer', $eventData);
 445  
 446                      switch($this->karmaVote) {
 447                          case 'nocookie':
 448                              // Users with no cookies won't be able to vote.
 449                              $msg = '<div class="serendipity_karmaVoting serendipity_karmaError"><a id="karma_vote' . $this->karmaId . '"></a>' . PLUGIN_KARMA_NOCOOKIE . '</div>';
 450  
 451                          case 'timeout2':
 452                              if (!isset($msg)) {
 453                                  $msg = '<div class="serendipity_karmaVoting serendipity_karmaError"><a id="karma_vote' . $this->karmaId . '"></a>' . PLUGIN_KARMA_CLOSED . '</div>';
 454                              }
 455  
 456                          case 'timeout':
 457                              if (!isset($msg)) {
 458                                  $msg = '<div class="serendipity_karmaVoting serendipity_karmaError"><a id="karma_vote' . $this->karmaId . '"></a>' . sprintf(PLUGIN_KARMA_TIMEOUT, $this->karmaTimeOut) . '</div>';
 459                              }
 460  
 461                          case 'alreadyvoted':
 462                              if (!isset($msg)) {
 463                                  $msg = '<div class="serendipity_karmaVoting serendipity_karmaError"><a id="karma_vote' . $this->karmaId . '"></a>' . PLUGIN_KARMA_ALREADYVOTED . '</div>';
 464                              }
 465  
 466                          case 'invalid1':
 467                          case 'invalid2':
 468                          case 'invalid':
 469                              if (!isset($msg)) {
 470                                  $msg = '<div class="serendipity_karmaVoting serendipity_karmaError"><a id="karma_vote' . $this->karmaId . '"></a>' . PLUGIN_KARMA_INVALID . '</div>';
 471                              }
 472  
 473                              /* OUTPUT MESSAGE */
 474                              if ($addData['extended']) {
 475                                  $eventData[0]['exflag'] = 1;
 476                                  $eventData[0]['add_footer'] .= $msg;
 477                              } else {
 478                                  $elements = count($eventData);
 479                                  // Find the right container to store our message in.
 480                                  for ($i = 0; $i < $elements; $i++) {
 481                                      if ($eventData[$i]['id'] == $this->karmaId) {
 482                                          $eventData[$i]['add_footer'] .= $msg;
 483                                      }
 484                                  }
 485                              }
 486                              break;
 487  
 488                          case 'voted':
 489                          default:
 490                              $track_clicks  = serendipity_db_bool($this->get_config('visits_active', true));
 491                              $track_karma   = serendipity_db_bool($this->get_config('karma_active', true));
 492                              $track_exits   = serendipity_db_bool($this->get_config('exits_active', true));
 493                              $karma_active  = $track_karma;
 494  
 495                              if (!is_array($eventData)) return;
 496  
 497                              $karmatime     = $this->get_config('max_karmatime', 7);
 498                              $max_karmatime = $karmatime    * 24 * 60 * 60;
 499                              $now           = time();
 500  
 501                              $url = serendipity_currentURL() . '&amp;';
 502  
 503                              $karma = (isset($serendipity['COOKIE']['karmaVote']) ? unserialize($serendipity['COOKIE']['karmaVote']) : array());
 504  
 505                              $link_1 = '<a class="serendipity_karmaVoting_link1" rel="nofollow" href="#" onclick="javascript:location.href=\'%5$sserendipity[karmaVote]=2&amp;serendipity[karmaId]=%1$s#karma_vote%1$s\';" title="' . sprintf(PLUGIN_KARMA_RATE, PLUGIN_KARMA_VOTEPOINT_1) . '">++</a>';
 506                              $link_2 = '<a class="serendipity_karmaVoting_link2" rel="nofollow" href="#" onclick="javascript:location.href=\'%5$sserendipity[karmaVote]=1&amp;serendipity[karmaId]=%1$s#karma_vote%1$s\';" title="' . sprintf(PLUGIN_KARMA_RATE, PLUGIN_KARMA_VOTEPOINT_2) . '">+</a>';
 507                              $link_3 = '<a class="serendipity_karmaVoting_link3" rel="nofollow" href="#" onclick="javascript:location.href=\'%5$sserendipity[karmaVote]=0&amp;serendipity[karmaId]=%1$s#karma_vote%1$s\';" title="' . sprintf(PLUGIN_KARMA_RATE, PLUGIN_KARMA_VOTEPOINT_3) . '">0</a>';
 508                              $link_4 = '<a class="serendipity_karmaVoting_link4" rel="nofollow" href="#" onclick="javascript:location.href=\'%5$sserendipity[karmaVote]=-1&amp;serendipity[karmaId]=%1$s#karma_vote%1$s\';" title="' . sprintf(PLUGIN_KARMA_RATE, PLUGIN_KARMA_VOTEPOINT_4) . '">-</a>';
 509                              $link_5 = '<a class="serendipity_karmaVoting_link5" rel="nofollow" href="#" onclick="javascript:location.href=\'%5$sserendipity[karmaVote]=-2&amp;serendipity[karmaId]=%1$s#karma_vote%1$s\';" title="' . sprintf(PLUGIN_KARMA_RATE, PLUGIN_KARMA_VOTEPOINT_5) . '">--</a>';
 510  
 511                              if ($addData['extended'] && $eventData[0]['timestamp'] < ($now - $max_karmatime)) {
 512                                  $karma_active = false;
 513                              }
 514  
 515                              $karma_voting = '<div class="serendipity_karmaVoting"><br /><a id="karma_vote%1$s"></a>'
 516                                    . ($karma_active ? '<span class="serendipity_karmaVoting_text">' . PLUGIN_KARMA_VOTETEXT . '</span> <span class="serendipity_karmaVoting_links">' . $link_1 . ' | ' . $link_2 . ' | ' . $link_3 . ' | ' . $link_4 . ' | ' . $link_5 . '</span><br />'
 517                                    . '<span class="serendipity_karmaVoting_current">' . PLUGIN_KARMA_CURRENT . '</span>' : '') . ($track_clicks ? '<span class="serendipity_karmaVoting_visits">' . PLUGIN_KARMA_VISITSCOUNT . '</span>': '') . '</div>';
 518  
 519                              $karma_current = '<div class="serendipity_karmaVoting"><br /><a id="karma_vote%1$s"></a>'
 520                                  . ($karma_active ? '<div class="serendipity_karmaSuccess">' . PLUGIN_KARMA_VOTED . '</div>'
 521                                    . '<span class="serendipity_karmaVoting_current">' . PLUGIN_KARMA_CURRENT . '</span>' : '') . ($track_clicks ? '<span class="serendipity_karmaVoting_visits">' . PLUGIN_KARMA_VISITSCOUNT . '</span>': '') . '</div>';
 522  
 523                              $karma_timeout = '<div class="serendipity_karmaVoting"><br /><a id="karma_vote%1$s"></a>'
 524                                  . ($track_karma ? '<div>' . sprintf(PLUGIN_KARMA_CLOSED, $karmatime) . '</div>'
 525                                    . '<span class="serendipity_karmaVoting_current">' . PLUGIN_KARMA_CURRENT . '</span>' : '') . ($track_clicks ? '<span class="serendipity_karmaVoting_visits">' . PLUGIN_KARMA_VISITSCOUNT . '</span>': '') . '</div>';
 526  
 527                              if ($addData['extended'] || $addData['preview']) {
 528                                  $entryid = (int)serendipity_db_escape_string($eventData[0]['id']);
 529                                  $q = 'SELECT SUM(votes) AS votes, SUM(points) AS points, SUM(visits) AS visits
 530                                          FROM ' . $serendipity['dbPrefix'] . 'karma   AS k
 531                                         WHERE k.entryid = ' . $entryid . ' GROUP BY k.entryid LIMIT 1';
 532                                  $row = serendipity_db_query($q, true);
 533  
 534                                  if (empty($row['votes'])) {
 535                                      $row['votes'] = 0;
 536                                  }
 537  
 538                                  if (empty($row['points'])) {
 539                                      $row['points'] = 0;
 540                                  }
 541  
 542                                  if (empty($row['visits'])) {
 543                                      $row['visits'] = 0;
 544                                  }
 545  
 546                                  if ($track_exits) {
 547                                      $extended_key .= $this->getExits($entryid);
 548                                  }
 549  
 550                                  $eventData[0]['exflag'] = 1;
 551                                  if (isset($karma[$entryid])) {
 552                                      $extended_key .= sprintf($karma_current, $karma[$entryid], $row['points'], $row['votes'], $row['visits'], $url);
 553                                  } elseif ($eventData[0]['timestamp'] < ($now - $max_karmatime)) {
 554                                      $extended_key .= sprintf($karma_timeout, $entryid, $row['points'], $row['votes'], $row['visits'], $url);
 555                                  } else {
 556                                      $extended_key .= sprintf($karma_voting, $entryid, $row['points'], $row['votes'], $row['visits'], $url);
 557                                  }
 558                              } elseif (!serendipity_db_bool($this->get_config('extended_only', false))) {
 559                                  $elements = count($eventData);
 560  
 561                                  // Get all existing entry IDs
 562                                  $entries  = array();
 563                                  for ($i = 0; $i < $elements; $i++) {
 564                                      $entries[] = (int)$eventData[$i]['id'];
 565                                  }
 566  
 567                                  // Fetch votes for all entry IDs. Store them in an array for later usage.
 568                                  $q = 'SELECT k.entryid, SUM(votes) AS votes, SUM(points) AS points, SUM(visits) AS visits
 569                                          FROM ' . $serendipity['dbPrefix'] . 'karma   AS k
 570                                         WHERE k.entryid IN (' . implode(', ', $entries) . ') GROUP BY k.entryid';
 571  
 572                                  $sql = serendipity_db_query($q);
 573  
 574                                  $rows = array();
 575                                  if ($sql && is_array($sql)) {
 576                                      foreach($sql AS $idx => $row) {
 577                                          $rows[$row['entryid']] = array('votes' => $row['votes'], 'points' => $row['points'], 'visits' => $row['visits']);
 578                                      }
 579                                  }
 580  
 581                                  $this->prepareExits($entries);
 582  
 583                                  // Walk entry array and insert karma voting line.
 584                                  for ($i = 0; $i < $elements; $i++) {
 585                                      $entryid = $eventData[$i]['id'];
 586                                      $votes   = (!empty($rows[$entryid]['votes']) ? $rows[$entryid]['votes'] : 0);
 587                                      $points  = (!empty($rows[$entryid]['points']) ? $rows[$entryid]['points'] : 0);
 588                                      $visits  = (!empty($rows[$entryid]['visits']) ? $rows[$entryid]['visits'] : 0);
 589  
 590                                      if (!isset($eventData[$i]['add_footer'])) {
 591                                          $eventData[$i]['add_footer'] = '';
 592                                      }
 593  
 594                                      if ($track_exits) {
 595                                          $eventData[$i]['add_footer'] .= $this->getExits($entryid, true);
 596                                      }
 597  
 598                                      if (isset($karma[$entryid])) {
 599                                          $eventData[$i]['add_footer'] .= sprintf($karma_current, $karma[$entryid], $points, $votes, $visits, $url);
 600                                      } elseif ($eventData[$i]['timestamp'] < ($now - $max_karmatime)) {
 601                                          $eventData[$i]['add_footer'] .= sprintf($karma_timeout, $entryid, $points, $votes, $visits, $url);
 602                                      } else {
 603                                          $eventData[$i]['add_footer'] .= sprintf($karma_voting, $entryid, $points, $votes, $visits, $url);
 604                                      }
 605                                  }
 606                              }
 607                      }
 608                      return true;
 609                      break;
 610  
 611                  default:
 612                      return false;
 613              }
 614          } else {
 615              return false;
 616          }
 617      }
 618  }
 619  
 620  /* vim: set sts=4 ts=4 expandtab : */


Généré le : Sat Nov 24 09:00:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics