[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/inc/ -> functions_indicators.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.6
   4   * Copyright 2010 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://mybb.com
   7   * License: http://mybb.com/about/license
   8   *
   9   * $Id$
  10   */
  11  
  12  /**
  13   * Mark a particular thread as read for the current user.
  14   *
  15   * @param int The thread ID
  16   * @param int The forum ID of the thread
  17   */
  18  function mark_thread_read($tid, $fid)
  19  {
  20      global $mybb, $db;
  21  
  22      // Can only do "true" tracking for registered users
  23      if($mybb->settings['threadreadcut'] > 0 && $mybb->user['uid'])
  24      {
  25          // For registered users, store the information in the database.
  26          switch($db->type)
  27          {
  28              case "pgsql":
  29              case "sqlite":
  30                  $db->replace_query("threadsread", array('tid' => $tid, 'uid' => $mybb->user['uid'], 'dateline' => TIME_NOW), array("tid", "uid"));
  31                  break;
  32              default:
  33                  $db->write_query("
  34                      REPLACE INTO ".TABLE_PREFIX."threadsread (tid, uid, dateline)
  35                      VALUES('$tid', '{$mybb->user['uid']}', '".TIME_NOW."')
  36                  ");
  37          }
  38      }
  39      // Default back to cookie marking
  40      else
  41      {
  42          my_set_array_cookie("threadread", $tid, TIME_NOW, -1);
  43      }
  44  
  45      $unread_count = fetch_unread_count($fid);
  46      if($unread_count == 0)
  47      {
  48          mark_forum_read($fid);
  49      }
  50  }
  51  
  52  /**
  53   * Fetches the number of unread threads for the current user in a particular forum.
  54   *
  55   * @param string The forums (CSV list)
  56   * @return int The number of unread threads
  57   */
  58  function fetch_unread_count($fid)
  59  {
  60      global $cache, $db, $mybb;
  61  
  62      $onlyview = $onlyview2 = '';
  63      $permissions = forum_permissions($fid);
  64      $cutoff = TIME_NOW-$mybb->settings['threadreadcut']*60*60*24;
  65  
  66      if(!empty($permissions['canonlyviewownthreads']))
  67      {
  68          $onlyview = " AND uid = '{$mybb->user['uid']}'";
  69          $onlyview2 = " AND t.uid = '{$mybb->user['uid']}'";
  70      }
  71  
  72      if($mybb->user['uid'] == 0)
  73      {
  74          $comma = '';
  75          $tids = '';
  76          $threadsread = my_unserialize($mybb->cookies['mybb']['threadread']);
  77          $forumsread = my_unserialize($mybb->cookies['mybb']['forumread']);
  78  
  79          if(!empty($threadsread))
  80          {
  81              foreach($threadsread as $key => $value)
  82              {
  83                  $tids .= $comma.intval($key);
  84                  $comma = ',';
  85              }
  86          }
  87  
  88          if(!empty($tids))
  89          {
  90              $count = 0;
  91  
  92              // We've read at least some threads, are they here?
  93              $query = $db->simple_select("threads", "lastpost, tid, fid", "visible=1 AND closed NOT LIKE 'moved|%' AND fid IN ($fid) AND lastpost > '{$cutoff}'{$onlyview}", array("limit" => 100));
  94  
  95              while($thread = $db->fetch_array($query))
  96              {
  97                  if($thread['lastpost'] > intval($threadsread[$thread['tid']]) && $thread['lastpost'] > intval($forumsread[$thread['fid']]))
  98                  {
  99                      ++$count;
 100                  }
 101              }
 102  
 103              return $count;
 104          }
 105  
 106          // Not read any threads?
 107          return false;
 108      }
 109      else
 110      {
 111          switch($db->type)
 112          {
 113              case "pgsql":
 114                  $query = $db->query("
 115                      SELECT COUNT(t.tid) AS unread_count
 116                      FROM ".TABLE_PREFIX."threads t
 117                      LEFT JOIN ".TABLE_PREFIX."threadsread tr ON (tr.tid=t.tid AND tr.uid='{$mybb->user['uid']}')
 118                      LEFT JOIN ".TABLE_PREFIX."forumsread fr ON (fr.fid=t.fid AND fr.uid='{$mybb->user['uid']}')
 119                      WHERE t.visible=1 AND t.closed NOT LIKE 'moved|%' AND t.fid IN ($fid) AND t.lastpost > COALESCE(tr.dateline,$cutoff) AND t.lastpost > COALESCE(fr.dateline,$cutoff) AND t.lastpost>$cutoff{$onlyview2}
 120                  ");
 121                  break;
 122              default:
 123                  $query = $db->query("
 124                      SELECT COUNT(t.tid) AS unread_count
 125                      FROM ".TABLE_PREFIX."threads t
 126                      LEFT JOIN ".TABLE_PREFIX."threadsread tr ON (tr.tid=t.tid AND tr.uid='{$mybb->user['uid']}')
 127                      LEFT JOIN ".TABLE_PREFIX."forumsread fr ON (fr.fid=t.fid AND fr.uid='{$mybb->user['uid']}')
 128                      WHERE t.visible=1 AND t.closed NOT LIKE 'moved|%' AND t.fid IN ($fid) AND t.lastpost > IFNULL(tr.dateline,$cutoff) AND t.lastpost > IFNULL(fr.dateline,$cutoff) AND t.lastpost>$cutoff{$onlyview2}
 129                  ");
 130          }
 131          return $db->fetch_field($query, "unread_count");
 132      }
 133  }
 134  
 135  /**
 136   * Mark a particular forum as read.
 137   *
 138   * @param int The forum ID
 139   */
 140  function mark_forum_read($fid)
 141  {
 142      global $mybb, $db;
 143  
 144      // Can only do "true" tracking for registered users
 145      if($mybb->settings['threadreadcut'] > 0 && $mybb->user['uid'])
 146      {
 147          // Experimental setting to mark parent forums as read
 148          $forums_to_read = array();
 149  
 150          if($mybb->settings['readparentforums'])
 151          {
 152              $ignored_forums = array();
 153              $forums = array_reverse(explode(",", get_parent_list($fid)));
 154  
 155              unset($forums[0]);
 156              if(!empty($forums))
 157              {
 158                  $ignored_forums[] = $fid;
 159  
 160                  foreach($forums as $forum)
 161                  {
 162                      $fids = array($forum);
 163                      $ignored_forums[] = $forum;
 164  
 165                      $children = explode(",", get_parent_list($forum));
 166                      foreach($children as $child)
 167                      {
 168                          if(in_array($child, $ignored_forums))
 169                          {
 170                              continue;
 171                          }
 172  
 173                          $fids[] = $child;
 174                          $ignored_forums[] = $child;
 175                      }
 176  
 177                      if(fetch_unread_count(implode(",", $fids)) == 0)
 178                      {
 179                          $forums_to_read[] = $forum;
 180                      }
 181                  }
 182              }
 183          }
 184  
 185          switch($db->type)
 186          {
 187              case "pgsql":
 188              case "sqlite":
 189                  add_shutdown(array($db, "replace_query"), array("forumsread", array('fid' => $fid, 'uid' => $mybb->user['uid'], 'dateline' => TIME_NOW), array("fid", "uid")));
 190                  
 191                  if(!empty($forums_to_read))
 192                  {
 193                      foreach($forums_to_read as $forum)
 194                      {
 195                          add_shutdown(array($db, "replace_query"), array("forumsread", array('fid' => $forum, 'uid' => $mybb->user['uid'], 'dateline' => TIME_NOW), array('fid', 'uid')));
 196                      }
 197                  }
 198                  break;
 199              default:
 200                  $child_sql = '';
 201                  if(!empty($forums_to_read))
 202                  {
 203                      foreach($forums_to_read as $forum)
 204                      {
 205                          $child_sql .= ", ('{$forum}', '{$mybb->user['uid']}', '".TIME_NOW."')";
 206                      }
 207                  }
 208  
 209                  $db->shutdown_query("
 210                      REPLACE INTO ".TABLE_PREFIX."forumsread (fid, uid, dateline)
 211                      VALUES('{$fid}', '{$mybb->user['uid']}', '".TIME_NOW."'){$child_sql}
 212                  ");
 213          }
 214      }
 215      // Mark in a cookie
 216      else
 217      {
 218          my_set_array_cookie("forumread", $fid, TIME_NOW, -1);
 219      }
 220  }
 221  
 222  /**
 223   * Marks all forums as read.
 224   *
 225   */
 226  function mark_all_forums_read()
 227  {
 228      global $mybb, $db, $cache;
 229  
 230      // Can only do "true" tracking for registered users
 231      if($mybb->user['uid'] > 0)
 232      {
 233          $db->update_query("users", array('lastvisit' => TIME_NOW), "uid='".$mybb->user['uid']."'");
 234          require_once  MYBB_ROOT."inc/functions_user.php";
 235          update_pm_count('', 2);
 236  
 237          if($mybb->settings['threadreadcut'] > 0)
 238          {
 239              // Need to loop through all forums and mark them as read
 240              $forums = $cache->read('forums');
 241              
 242              $update_count = ceil(count($forums)/20);
 243              
 244              if($update_count < 15)
 245              {
 246                  $update_count = 15;
 247              }
 248              
 249              $mark_query = '';
 250              $done = 0;
 251              foreach(array_keys($forums) as $fid)
 252              {                
 253                  switch($db->type)
 254                  {
 255                      case "pgsql":
 256                      case "sqlite":
 257                          $mark_query[] = array('fid' => $fid, 'uid' => $mybb->user['uid'], 'dateline' => TIME_NOW);
 258                          break;
 259                      default:
 260                          if($mark_query != '')
 261                          {
 262                              $mark_query .= ',';
 263                          }
 264                          $mark_query .= "('{$fid}', '{$mybb->user['uid']}', '".TIME_NOW."')";
 265                  }
 266                  ++$done;
 267                  
 268                  // Only do this in loops of $update_count, save query time
 269                  if($done % $update_count)
 270                  {
 271                      switch($db->type)
 272                      {
 273                          case "pgsql":
 274                          case "sqlite":
 275                              foreach($mark_query as $replace_query)
 276                              {
 277                                  add_shutdown(array($db, "replace_query"), array("forumsread", $replace_query, array("fid", "uid")));
 278                              }
 279                              $mark_query = array();
 280                              break;
 281                          default:
 282                              $db->shutdown_query("
 283                                  REPLACE INTO ".TABLE_PREFIX."forumsread (fid, uid, dateline)
 284                                  VALUES {$mark_query}
 285                              ");
 286                              $mark_query = '';
 287                      }
 288                  }
 289              }
 290              
 291              if($mark_query != '')
 292              {
 293                  switch($db->type)
 294                  {
 295                      case "pgsql":
 296                      case "sqlite":
 297                          foreach($mark_query as $replace_query)
 298                          {
 299                              add_shutdown(array($db, "replace_query"), array("forumsread", $replace_query, array("fid", "uid")));
 300                          }
 301                          break;
 302                      default:
 303                          $db->shutdown_query("
 304                              REPLACE INTO ".TABLE_PREFIX."forumsread (fid, uid, dateline)
 305                              VALUES {$mark_query}
 306                          ");
 307                  }
 308              }
 309          }
 310      }
 311      else
 312      {
 313          my_setcookie("mybb[readallforums]", 1);
 314          my_setcookie("mybb[lastvisit]", TIME_NOW);
 315  
 316          my_unsetcookie("mybb[threadread]");
 317          my_unsetcookie("mybb[forumread]");
 318      }
 319  }
 320  ?>


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1