Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet;
   4  
   5  use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
   6  use PhpOffice\PhpSpreadsheet\Helper\Size;
   7  use PhpOffice\PhpSpreadsheet\RichText\RichText;
   8  use PhpOffice\PhpSpreadsheet\Shared\Drawing as SharedDrawing;
   9  use PhpOffice\PhpSpreadsheet\Style\Alignment;
  10  use PhpOffice\PhpSpreadsheet\Style\Color;
  11  use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
  12  
  13  class Comment implements IComparable
  14  {
  15      /**
  16       * Author.
  17       *
  18       * @var string
  19       */
  20      private $author;
  21  
  22      /**
  23       * Rich text comment.
  24       *
  25       * @var RichText
  26       */
  27      private $text;
  28  
  29      /**
  30       * Comment width (CSS style, i.e. XXpx or YYpt).
  31       *
  32       * @var string
  33       */
  34      private $width = '96pt';
  35  
  36      /**
  37       * Left margin (CSS style, i.e. XXpx or YYpt).
  38       *
  39       * @var string
  40       */
  41      private $marginLeft = '59.25pt';
  42  
  43      /**
  44       * Top margin (CSS style, i.e. XXpx or YYpt).
  45       *
  46       * @var string
  47       */
  48      private $marginTop = '1.5pt';
  49  
  50      /**
  51       * Visible.
  52       *
  53       * @var bool
  54       */
  55      private $visible = false;
  56  
  57      /**
  58       * Comment height (CSS style, i.e. XXpx or YYpt).
  59       *
  60       * @var string
  61       */
  62      private $height = '55.5pt';
  63  
  64      /**
  65       * Comment fill color.
  66       *
  67       * @var Color
  68       */
  69      private $fillColor;
  70  
  71      /**
  72       * Alignment.
  73       *
  74       * @var string
  75       */
  76      private $alignment;
  77  
  78      /**
  79       * Background image in comment.
  80       *
  81       * @var Drawing
  82       */
  83      private $backgroundImage;
  84  
  85      /**
  86       * Create a new Comment.
  87       */
  88      public function __construct()
  89      {
  90          // Initialise variables
  91          $this->author = 'Author';
  92          $this->text = new RichText();
  93          $this->fillColor = new Color('FFFFFFE1');
  94          $this->alignment = Alignment::HORIZONTAL_GENERAL;
  95          $this->backgroundImage = new Drawing();
  96      }
  97  
  98      /**
  99       * Get Author.
 100       */
 101      public function getAuthor(): string
 102      {
 103          return $this->author;
 104      }
 105  
 106      /**
 107       * Set Author.
 108       */
 109      public function setAuthor(string $author): self
 110      {
 111          $this->author = $author;
 112  
 113          return $this;
 114      }
 115  
 116      /**
 117       * Get Rich text comment.
 118       */
 119      public function getText(): RichText
 120      {
 121          return $this->text;
 122      }
 123  
 124      /**
 125       * Set Rich text comment.
 126       */
 127      public function setText(RichText $text): self
 128      {
 129          $this->text = $text;
 130  
 131          return $this;
 132      }
 133  
 134      /**
 135       * Get comment width (CSS style, i.e. XXpx or YYpt).
 136       */
 137      public function getWidth(): string
 138      {
 139          return $this->width;
 140      }
 141  
 142      /**
 143       * Set comment width (CSS style, i.e. XXpx or YYpt). Default unit is pt.
 144       */
 145      public function setWidth(string $width): self
 146      {
 147          $width = new Size($width);
 148          if ($width->valid()) {
 149              $this->width = (string) $width;
 150          }
 151  
 152          return $this;
 153      }
 154  
 155      /**
 156       * Get comment height (CSS style, i.e. XXpx or YYpt).
 157       */
 158      public function getHeight(): string
 159      {
 160          return $this->height;
 161      }
 162  
 163      /**
 164       * Set comment height (CSS style, i.e. XXpx or YYpt). Default unit is pt.
 165       */
 166      public function setHeight(string $height): self
 167      {
 168          $height = new Size($height);
 169          if ($height->valid()) {
 170              $this->height = (string) $height;
 171          }
 172  
 173          return $this;
 174      }
 175  
 176      /**
 177       * Get left margin (CSS style, i.e. XXpx or YYpt).
 178       */
 179      public function getMarginLeft(): string
 180      {
 181          return $this->marginLeft;
 182      }
 183  
 184      /**
 185       * Set left margin (CSS style, i.e. XXpx or YYpt). Default unit is pt.
 186       */
 187      public function setMarginLeft(string $margin): self
 188      {
 189          $margin = new Size($margin);
 190          if ($margin->valid()) {
 191              $this->marginLeft = (string) $margin;
 192          }
 193  
 194          return $this;
 195      }
 196  
 197      /**
 198       * Get top margin (CSS style, i.e. XXpx or YYpt).
 199       */
 200      public function getMarginTop(): string
 201      {
 202          return $this->marginTop;
 203      }
 204  
 205      /**
 206       * Set top margin (CSS style, i.e. XXpx or YYpt). Default unit is pt.
 207       */
 208      public function setMarginTop(string $margin): self
 209      {
 210          $margin = new Size($margin);
 211          if ($margin->valid()) {
 212              $this->marginTop = (string) $margin;
 213          }
 214  
 215          return $this;
 216      }
 217  
 218      /**
 219       * Is the comment visible by default?
 220       */
 221      public function getVisible(): bool
 222      {
 223          return $this->visible;
 224      }
 225  
 226      /**
 227       * Set comment default visibility.
 228       */
 229      public function setVisible(bool $visibility): self
 230      {
 231          $this->visible = $visibility;
 232  
 233          return $this;
 234      }
 235  
 236      /**
 237       * Set fill color.
 238       */
 239      public function setFillColor(Color $color): self
 240      {
 241          $this->fillColor = $color;
 242  
 243          return $this;
 244      }
 245  
 246      /**
 247       * Get fill color.
 248       */
 249      public function getFillColor(): Color
 250      {
 251          return $this->fillColor;
 252      }
 253  
 254      /**
 255       * Set Alignment.
 256       */
 257      public function setAlignment(string $alignment): self
 258      {
 259          $this->alignment = $alignment;
 260  
 261          return $this;
 262      }
 263  
 264      /**
 265       * Get Alignment.
 266       */
 267      public function getAlignment(): string
 268      {
 269          return $this->alignment;
 270      }
 271  
 272      /**
 273       * Get hash code.
 274       */
 275      public function getHashCode(): string
 276      {
 277          return md5(
 278              $this->author .
 279              $this->text->getHashCode() .
 280              $this->width .
 281              $this->height .
 282              $this->marginLeft .
 283              $this->marginTop .
 284              ($this->visible ? 1 : 0) .
 285              $this->fillColor->getHashCode() .
 286              $this->alignment .
 287              ($this->hasBackgroundImage() ? $this->backgroundImage->getHashCode() : '') .
 288              __CLASS__
 289          );
 290      }
 291  
 292      /**
 293       * Implement PHP __clone to create a deep clone, not just a shallow copy.
 294       */
 295      public function __clone()
 296      {
 297          $vars = get_object_vars($this);
 298          foreach ($vars as $key => $value) {
 299              if (is_object($value)) {
 300                  $this->$key = clone $value;
 301              } else {
 302                  $this->$key = $value;
 303              }
 304          }
 305      }
 306  
 307      /**
 308       * Convert to string.
 309       */
 310      public function __toString(): string
 311      {
 312          return $this->text->getPlainText();
 313      }
 314  
 315      /**
 316       * Check is background image exists.
 317       */
 318      public function hasBackgroundImage(): bool
 319      {
 320          $path = $this->backgroundImage->getPath();
 321  
 322          if (empty($path)) {
 323              return false;
 324          }
 325  
 326          return getimagesize($path) !== false;
 327      }
 328  
 329      /**
 330       * Returns background image.
 331       */
 332      public function getBackgroundImage(): Drawing
 333      {
 334          return $this->backgroundImage;
 335      }
 336  
 337      /**
 338       * Sets background image.
 339       */
 340      public function setBackgroundImage(Drawing $objDrawing): self
 341      {
 342          if (!array_key_exists($objDrawing->getType(), Drawing::IMAGE_TYPES_CONVERTION_MAP)) {
 343              throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
 344          }
 345          $this->backgroundImage = $objDrawing;
 346  
 347          return $this;
 348      }
 349  
 350      /**
 351       * Sets size of comment as size of background image.
 352       */
 353      public function setSizeAsBackgroundImage(): self
 354      {
 355          if ($this->hasBackgroundImage()) {
 356              $this->setWidth(SharedDrawing::pixelsToPoints($this->backgroundImage->getWidth()) . 'pt');
 357              $this->setHeight(SharedDrawing::pixelsToPoints($this->backgroundImage->getHeight()) . 'pt');
 358          }
 359  
 360          return $this;
 361      }
 362  }