Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   1  <?php
   2  /**
   3   * Copyright 2008-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file LICENSE for license information (LGPL). If you
   6   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
   7   *
   8   * @category  Horde
   9   * @copyright 2008-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Imap_Client
  12   */
  13  
  14  /**
  15   * Object representing the threaded sort results from
  16   * Horde_Imap_Client_Base#thread().
  17   *
  18   * @author    Michael Slusarz <slusarz@horde.org>
  19   * @category  Horde
  20   * @copyright 2008-2017 Horde LLC
  21   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  22   * @package   Imap_Client
  23   */
  24  class Horde_Imap_Client_Data_Thread implements Countable, Serializable
  25  {
  26      /**
  27       * Internal thread data structure. Keys are base values, values are arrays
  28       * with keys as the ID and values as the level.
  29       *
  30       * @var array
  31       */
  32      protected $_thread = array();
  33  
  34      /**
  35       * The index type.
  36       *
  37       * @var string
  38       */
  39      protected $_type;
  40  
  41      /**
  42       * Constructor.
  43       *
  44       * @param array $data   See $_thread.
  45       * @param string $type  Either 'sequence' or 'uid'.
  46       */
  47      public function __construct($data, $type)
  48      {
  49          $this->_thread = $data;
  50          $this->_type = $type;
  51      }
  52  
  53      /**
  54       * Return the ID type.
  55       *
  56       * @return string  Either 'sequence' or 'uid'.
  57       */
  58      public function getType()
  59      {
  60          return $this->_type;
  61      }
  62  
  63      /**
  64       * Return the sorted list of messages indices.
  65       *
  66       * @return Horde_Imap_Client_Ids  The sorted list of messages.
  67       */
  68      public function messageList()
  69      {
  70          return new Horde_Imap_Client_Ids($this->_getAllIndices(), $this->getType() == 'sequence');
  71      }
  72  
  73      /**
  74       * Returns the list of messages in a thread.
  75       *
  76       * @param integer $index  An index contained in the thread.
  77       *
  78       * @return array  Keys are indices, values are objects with the following
  79       *                properties:
  80       *   - base: (integer) Base ID of the thread. If null, thread is a single
  81       *           message.
  82       *   - last: (boolean) If true, this is the last index in the sublevel.
  83       *   - level: (integer) The sublevel of the index.
  84       */
  85      public function getThread($index)
  86      {
  87          foreach ($this->_thread as $v) {
  88              if (isset($v[$index])) {
  89                  reset($v);
  90  
  91                  $ob = new stdClass;
  92                  $ob->base = (count($v) > 1) ? key($v) : null;
  93                  $ob->last = false;
  94  
  95                  $levels = $out = array();
  96                  $last = 0;
  97  
  98                  while (($v2 = current($v)) !== false) {
  99                      $k2 = key($v);
 100                      $ob2 = clone $ob;
 101                      $ob2->level = $v2;
 102                      $out[$k2] = $ob2;
 103  
 104                      if (($last < $v2) && isset($levels[$v2])) {
 105                          $out[$levels[$v2]]->last = true;
 106                      }
 107                      $levels[$v2] = $k2;
 108                      $last = $v2;
 109                      next($v);
 110                  }
 111  
 112                  foreach ($levels as $v) {
 113                      $out[$v]->last = true;
 114                  }
 115  
 116                  return $out;
 117              }
 118          }
 119  
 120          return array();
 121      }
 122  
 123      /**
 124       * Returns array of all threads.
 125       *
 126       * @return array  Keys of thread arrays are indices, values are objects with the following
 127       *                properties:
 128       *   - base: (integer) Base ID of the thread. If null, thread is a single
 129       *           message.
 130       *   - last: (boolean) If true, this is the last index in the sublevel.
 131       *   - level: (integer) The sublevel of the index.
 132       */
 133      public function getThreads()
 134      {
 135          $data = array();
 136          foreach ($this->_thread as $v) {
 137              reset($v);
 138  
 139              $ob = new stdClass;
 140              $ob->base = (count($v) > 1) ? key($v) : null;
 141              $ob->last = false;
 142  
 143              $levels = $out = array();
 144              $last = 0;
 145  
 146              while (($v2 = current($v)) !== false) {
 147                  $k2 = key($v);
 148                  $ob2 = clone $ob;
 149                  $ob2->level = $v2;
 150                  $out[$k2] = $ob2;
 151  
 152                  if (($last < $v2) && isset($levels[$v2])) {
 153                      $out[$levels[$v2]]->last = true;
 154                  }
 155                  $levels[$v2] = $k2;
 156                  $last = $v2;
 157                  next($v);
 158              }
 159  
 160              foreach ($levels as $v) {
 161                  $out[$v]->last = true;
 162              }
 163  
 164              $data[] = $out;
 165          }
 166  
 167          return $data;
 168      }
 169  
 170      /* Countable methods. */
 171  
 172      /**
 173       */
 174      public function count()
 175      {
 176          return count($this->_getAllIndices());
 177      }
 178  
 179      /* Serializable methods. */
 180  
 181      /**
 182       */
 183      public function serialize()
 184      {
 185          return json_encode(array(
 186              $this->_thread,
 187              $this->_type
 188          ));
 189      }
 190  
 191      /**
 192       */
 193      public function unserialize($data)
 194      {
 195          list($this->_thread, $this->_type) = json_decode($data, true);
 196      }
 197  
 198      /* Protected methods. */
 199  
 200      /**
 201       * Return all indices.
 202       *
 203       * @return array  An array of indices.
 204       */
 205      protected function _getAllIndices()
 206      {
 207          $out = array();
 208  
 209          foreach ($this->_thread as $val) {
 210              $out += $val;
 211          }
 212  
 213          return array_keys($out);
 214      }
 215  
 216  }