Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Post data mapper.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_forum\local\data_mappers\legacy;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use mod_forum\local\entities\post as post_entity;
  30  use stdClass;
  31  
  32  /**
  33   * Convert a post entity into an stdClass.
  34   *
  35   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class post {
  39      /**
  40       * Convert a list of post entities into stdClasses.
  41       *
  42       * @param post_entity[] $posts The posts to convert.
  43       * @return stdClass[]
  44       */
  45      public function to_legacy_objects(array $posts) : array {
  46          return array_map(function(post_entity $post) {
  47              return (object) [
  48                  'id' => $post->get_id(),
  49                  'discussion' => $post->get_discussion_id(),
  50                  'parent' => $post->get_parent_id(),
  51                  'userid' => $post->get_author_id(),
  52                  'created' => $post->get_time_created(),
  53                  'modified' => $post->get_time_modified(),
  54                  'mailed' => $post->has_been_mailed(),
  55                  'subject' => $post->get_subject(),
  56                  'message' => $post->get_message(),
  57                  'messageformat' => $post->get_message_format(),
  58                  'messagetrust' => $post->is_message_trusted(),
  59                  'attachment' => $post->has_attachments(),
  60                  'totalscore' => $post->get_total_score(),
  61                  'mailnow' => $post->should_mail_now(),
  62                  'deleted' => $post->is_deleted(),
  63                  'privatereplyto' => $post->get_private_reply_recipient_id(),
  64                  'wordcount' => $post->get_wordcount(),
  65                  'charcount' => $post->get_charcount(),
  66              ];
  67          }, $posts);
  68      }
  69  
  70      /**
  71       * Convert a post entity into an stdClass.
  72       *
  73       * @param post_entity $post The post to convert.
  74       * @return stdClass
  75       */
  76      public function to_legacy_object(post_entity $post) : stdClass {
  77          return $this->to_legacy_objects([$post])[0];
  78      }
  79  }