Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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   * Class for exporting a blog post (entry).
  19   *
  20   * @package    core_blog
  21   * @copyright  2018 Juan Leyva <juan@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_blog\external;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use core\external\exporter;
  28  use external_util;
  29  use external_files;
  30  use renderer_base;
  31  use context_system;
  32  use core_tag\external\tag_item_exporter;
  33  
  34  /**
  35   * Class for exporting a blog post (entry).
  36   *
  37   * @copyright  2018 Juan Leyva <juan@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class post_exporter extends exporter {
  41  
  42      /**
  43       * Return the list of properties.
  44       *
  45       * @return array list of properties
  46       */
  47      protected static function define_properties() {
  48          return array(
  49              'id' => array(
  50                  'type' => PARAM_INT,
  51                  'null' => NULL_ALLOWED,
  52                  'description' => 'Post/entry id.',
  53              ),
  54              'module' => array(
  55                  'type' => PARAM_ALPHANUMEXT,
  56                  'null' => NULL_NOT_ALLOWED,
  57                  'description' => 'Where it was published the post (blog, blog_external...).',
  58              ),
  59              'userid' => array(
  60                  'type' => PARAM_INT,
  61                  'null' => NULL_NOT_ALLOWED,
  62                  'default' => 0,
  63                  'description' => 'Post author.',
  64              ),
  65              'courseid' => array(
  66                  'type' => PARAM_INT,
  67                  'null' => NULL_NOT_ALLOWED,
  68                  'default' => 0,
  69                  'description' => 'Course where the post was created.',
  70              ),
  71              'groupid' => array(
  72                  'type' => PARAM_INT,
  73                  'null' => NULL_NOT_ALLOWED,
  74                  'default' => 0,
  75                  'description' => 'Group post was created for.',
  76              ),
  77              'moduleid' => array(
  78                  'type' => PARAM_INT,
  79                  'null' => NULL_NOT_ALLOWED,
  80                  'default' => 0,
  81                  'description' => 'Module id where the post was created (not used anymore).',
  82              ),
  83              'coursemoduleid' => array(
  84                  'type' => PARAM_INT,
  85                  'null' => NULL_NOT_ALLOWED,
  86                  'default' => 0,
  87                  'description' => 'Course module id where the post was created.',
  88              ),
  89              'subject' => array(
  90                  'type' => PARAM_TEXT,
  91                  'null' => NULL_NOT_ALLOWED,
  92                  'description' => 'Post subject.',
  93              ),
  94              'summary' => array(
  95                  'type' => PARAM_RAW,
  96                  'null' => NULL_ALLOWED,
  97                  'description' => 'Post summary.',
  98              ),
  99              'content' => array(
 100                  'type' => PARAM_RAW,
 101                  'null' => NULL_ALLOWED,
 102                  'description' => 'Post content.',
 103              ),
 104              'uniquehash' => array(
 105                  'type' => PARAM_RAW,
 106                  'null' => NULL_NOT_ALLOWED,
 107                  'description' => 'Post unique hash.',
 108              ),
 109              'rating' => array(
 110                  'type' => PARAM_INT,
 111                  'null' => NULL_NOT_ALLOWED,
 112                  'default' => 0,
 113                  'description' => 'Post rating.',
 114              ),
 115              'format' => array(
 116                  'type' => PARAM_INT,
 117                  'null' => NULL_NOT_ALLOWED,
 118                  'default' => 0,
 119                  'description' => 'Post content format.',
 120              ),
 121              'summaryformat' => array(
 122                  'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
 123                  'type' => PARAM_INT,
 124                  'default' => FORMAT_MOODLE,
 125                  'description' => 'Format for the summary field.',
 126              ),
 127              'attachment' => array(
 128                  'type' => PARAM_RAW,
 129                  'null' => NULL_ALLOWED,
 130                  'description' => 'Post atachment.',
 131              ),
 132              'publishstate' => array(
 133                  'type' => PARAM_ALPHA,
 134                  'null' => NULL_NOT_ALLOWED,
 135                  'default' => 'draft',
 136                  'description' => 'Post publish state.',
 137              ),
 138              'lastmodified' => array(
 139                  'type' => PARAM_INT,
 140                  'null' => NULL_NOT_ALLOWED,
 141                  'default' => 0,
 142                  'description' => 'When it was last modified.',
 143              ),
 144              'created' => array(
 145                  'type' => PARAM_INT,
 146                  'null' => NULL_NOT_ALLOWED,
 147                  'default' => 0,
 148                  'description' => 'When it was created.',
 149              ),
 150              'usermodified' => array(
 151                  'type' => PARAM_INT,
 152                  'null' => NULL_ALLOWED,
 153                  'description' => 'User that updated the post.',
 154              ),
 155          );
 156      }
 157  
 158      protected static function define_related() {
 159          return array(
 160              'context' => 'context'
 161          );
 162      }
 163  
 164      protected static function define_other_properties() {
 165          return array(
 166              'summaryfiles' => array(
 167                  'type' => external_files::get_properties_for_exporter(),
 168                  'multiple' => true
 169              ),
 170              'attachmentfiles' => array(
 171                  'type' => external_files::get_properties_for_exporter(),
 172                  'multiple' => true,
 173                  'optional' => true
 174              ),
 175              'tags' => array(
 176                  'type' => tag_item_exporter::read_properties_definition(),
 177                  'description' => 'Tags.',
 178                  'multiple' => true,
 179                  'optional' => true,
 180              ),
 181          );
 182      }
 183  
 184      protected function get_other_values(renderer_base $output) {
 185          $context = context_system::instance(); // Files always on site context.
 186  
 187          $values['summaryfiles'] = external_util::get_area_files($context->id, 'blog', 'post', $this->data->id);
 188          $values['attachmentfiles'] = external_util::get_area_files($context->id, 'blog', 'attachment', $this->data->id);
 189          if ($this->data->module == 'blog_external') {
 190              // For external blogs, the content field has the external blog id.
 191              $values['tags'] = \core_tag\external\util::get_item_tags('core', 'blog_external', $this->data->content);
 192          } else {
 193              $values['tags'] = \core_tag\external\util::get_item_tags('core', 'post', $this->data->id);
 194          }
 195  
 196          return $values;
 197      }
 198  }