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 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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   * Recent Blog Entries Block page.
  19   *
  20   * @package   block_blog_recent
  21   * @copyright 2009 Nicolas Connault
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * This block simply outputs a list of links to recent blog entries, depending on
  29   * the context of the current page.
  30   */
  31  class block_blog_recent extends block_base {
  32  
  33      function init() {
  34          $this->title = get_string('pluginname', 'block_blog_recent');
  35          $this->content_type = BLOCK_TYPE_TEXT;
  36      }
  37  
  38      function applicable_formats() {
  39          return array('all' => true, 'my' => false, 'tag' => false);
  40      }
  41  
  42      function instance_allow_config() {
  43          return true;
  44      }
  45  
  46      function get_content() {
  47          global $CFG;
  48  
  49          if ($this->content !== NULL) {
  50              return $this->content;
  51          }
  52  
  53          // verify blog is enabled
  54          if (empty($CFG->enableblogs)) {
  55              $this->content = new stdClass();
  56              $this->content->text = '';
  57              if ($this->page->user_is_editing()) {
  58                  $this->content->text = get_string('blogdisable', 'blog');
  59              }
  60              return $this->content;
  61  
  62          } else if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL and (!isloggedin() or isguestuser())) {
  63              $this->content = new stdClass();
  64              $this->content->text = '';
  65              return $this->content;
  66          }
  67  
  68          require_once($CFG->dirroot .'/blog/lib.php');
  69          require_once($CFG->dirroot .'/blog/locallib.php');
  70  
  71          if (empty($this->config)) {
  72              $this->config = new stdClass();
  73          }
  74  
  75          if (empty($this->config->recentbloginterval)) {
  76              $this->config->recentbloginterval = 8400;
  77          }
  78  
  79          if (empty($this->config->numberofrecentblogentries)) {
  80              $this->config->numberofrecentblogentries = 4;
  81          }
  82  
  83          $this->content = new stdClass();
  84          $this->content->footer = '';
  85          $this->content->text = '';
  86  
  87          $context = $this->page->context;
  88  
  89          $url = new moodle_url('/blog/index.php');
  90          $filter = array();
  91          if ($context->contextlevel == CONTEXT_MODULE) {
  92              $filter['module'] = $context->instanceid;
  93              $a = new stdClass;
  94              $a->type = get_string('modulename', $this->page->cm->modname);
  95              $strview = get_string('viewallmodentries', 'blog', $a);
  96              $url->param('modid', $context->instanceid);
  97          } else if ($context->contextlevel == CONTEXT_COURSE) {
  98              $filter['course'] = $context->instanceid;
  99              $a = new stdClass;
 100              $a->type = get_string('course');
 101              $strview = get_string('viewblogentries', 'blog', $a);
 102              $url->param('courseid', $context->instanceid);
 103          } else {
 104              $strview = get_string('viewsiteentries', 'blog');
 105          }
 106          $filter['since'] = $this->config->recentbloginterval;
 107  
 108          $bloglisting = new blog_listing($filter);
 109          $entries = $bloglisting->get_entries(0, $this->config->numberofrecentblogentries, 4);
 110  
 111          if (!empty($entries)) {
 112              $entrieslist = array();
 113              $viewblogurl = new moodle_url('/blog/index.php');
 114  
 115              foreach ($entries as $entryid => $entry) {
 116                  $viewblogurl->param('entryid', $entryid);
 117                  $entrylink = html_writer::link($viewblogurl, shorten_text(format_string($entry->subject, true,
 118                      ['context' => $context])));
 119                  $entrieslist[] = $entrylink;
 120              }
 121  
 122              $this->content->text .= html_writer::alist($entrieslist, array('class'=>'list'));
 123              $viewallentrieslink = html_writer::link($url, $strview);
 124              $this->content->text .= $viewallentrieslink;
 125          } else {
 126              $this->content->text .= get_string('norecentblogentries', 'block_blog_recent');
 127          }
 128      }
 129  
 130      /**
 131       * Return the plugin config settings for external functions.
 132       *
 133       * @return stdClass the configs for both the block instance and plugin
 134       * @since Moodle 3.8
 135       */
 136      public function get_config_for_external() {
 137          // Return all settings for all users since it is safe (no private keys, etc..).
 138          $configs = !empty($this->config) ? $this->config : new stdClass();
 139  
 140          return (object) [
 141              'instance' => $configs,
 142              'plugin' => new stdClass(),
 143          ];
 144      }
 145  
 146      /**
 147       * This block shouldn't be added to a page if the blogs advanced feature is disabled.
 148       *
 149       * @param moodle_page $page
 150       * @return bool
 151       */
 152      public function can_block_be_added(moodle_page $page): bool {
 153          global $CFG;
 154  
 155          return $CFG->enableblogs;
 156      }
 157  }