Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is 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   * Course list block.
  19   *
  20   * @package    block_course_list
  21   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  include_once($CFG->dirroot . '/course/lib.php');
  26  
  27  class block_course_list extends block_list {
  28      function init() {
  29          $this->title = get_string('pluginname', 'block_course_list');
  30      }
  31  
  32      function has_config() {
  33          return true;
  34      }
  35  
  36      function get_content() {
  37          global $CFG, $USER, $DB, $OUTPUT;
  38  
  39          if($this->content !== NULL) {
  40              return $this->content;
  41          }
  42  
  43          $this->content = new stdClass;
  44          $this->content->items = array();
  45          $this->content->icons = array();
  46          $this->content->footer = '';
  47  
  48          $icon = $OUTPUT->pix_icon('i/course', get_string('course'));
  49  
  50          $adminseesall = true;
  51          if (isset($CFG->block_course_list_adminview)) {
  52             if ( $CFG->block_course_list_adminview == 'own'){
  53                 $adminseesall = false;
  54             }
  55          }
  56  
  57          $allcourselink =
  58              (has_capability('moodle/course:update', context_system::instance())
  59              || empty($CFG->block_course_list_hideallcourseslink)) &&
  60              core_course_category::user_top();
  61  
  62          if (empty($CFG->disablemycourses) and isloggedin() and !isguestuser() and
  63            !(has_capability('moodle/course:update', context_system::instance()) and $adminseesall)) {    // Just print My Courses
  64              if ($courses = enrol_get_my_courses()) {
  65                  foreach ($courses as $course) {
  66                      $coursecontext = context_course::instance($course->id);
  67                      $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
  68                      $this->content->items[]="<a $linkcss title=\"" . format_string($course->shortname, true, array('context' => $coursecontext)) . "\" ".
  69                                 "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">".$icon.format_string(get_course_display_name_for_list($course)). "</a>";
  70                  }
  71                  $this->title = get_string('mycourses');
  72              /// If we can update any course of the view all isn't hidden, show the view all courses link
  73                  if ($allcourselink) {
  74                      $this->content->footer = "<a href=\"$CFG->wwwroot/course/index.php\">".get_string("fulllistofcourses")."</a> ...";
  75                  }
  76              }
  77              $this->get_remote_courses();
  78              if ($this->content->items) { // make sure we don't return an empty list
  79                  return $this->content;
  80              }
  81          }
  82  
  83          // User is not enrolled in any courses, show list of available categories or courses (if there is only one category).
  84          $topcategory = core_course_category::top();
  85          if ($topcategory->is_uservisible() && ($categories = $topcategory->get_children())) { // Check we have categories.
  86              if (count($categories) > 1 || (count($categories) == 1 && $DB->count_records('course') > 200)) {     // Just print top level category links
  87                  foreach ($categories as $category) {
  88                      $categoryname = $category->get_formatted_name();
  89                      $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
  90                      $this->content->items[]="<a $linkcss href=\"$CFG->wwwroot/course/index.php?categoryid=$category->id\">".$icon . $categoryname . "</a>";
  91                  }
  92              /// If we can update any course of the view all isn't hidden, show the view all courses link
  93                  if ($allcourselink) {
  94                      $this->content->footer .= "<a href=\"$CFG->wwwroot/course/index.php\">".get_string('fulllistofcourses').'</a> ...';
  95                  }
  96                  $this->title = get_string('categories');
  97              } else {                          // Just print course names of single category
  98                  $category = array_shift($categories);
  99                  $courses = $category->get_courses();
 100  
 101                  if ($courses) {
 102                      foreach ($courses as $course) {
 103                          $coursecontext = context_course::instance($course->id);
 104                          $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
 105  
 106                          $this->content->items[]="<a $linkcss title=\""
 107                                     . s($course->get_formatted_shortname())."\" ".
 108                                     "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">"
 109                                     .$icon. $course->get_formatted_name() . "</a>";
 110                      }
 111                  /// If we can update any course of the view all isn't hidden, show the view all courses link
 112                      if ($allcourselink) {
 113                          $this->content->footer .= "<a href=\"$CFG->wwwroot/course/index.php\">".get_string('fulllistofcourses').'</a> ...';
 114                      }
 115                      $this->get_remote_courses();
 116                  } else {
 117  
 118                      $this->content->icons[] = '';
 119                      $this->content->items[] = get_string('nocoursesyet');
 120                      if (has_capability('moodle/course:create', context_coursecat::instance($category->id))) {
 121                          $this->content->footer = '<a href="'.$CFG->wwwroot.'/course/edit.php?category='.$category->id.'">'.get_string("addnewcourse").'</a> ...';
 122                      }
 123                      $this->get_remote_courses();
 124                  }
 125                  $this->title = get_string('courses');
 126              }
 127          }
 128  
 129          return $this->content;
 130      }
 131  
 132      function get_remote_courses() {
 133          global $CFG, $USER, $OUTPUT;
 134  
 135          if (!is_enabled_auth('mnet')) {
 136              // no need to query anything remote related
 137              return;
 138          }
 139  
 140          $icon = $OUTPUT->pix_icon('i/mnethost', get_string('host', 'mnet'));
 141  
 142          // shortcut - the rest is only for logged in users!
 143          if (!isloggedin() || isguestuser()) {
 144              return false;
 145          }
 146  
 147          if ($courses = get_my_remotecourses()) {
 148              $this->content->items[] = get_string('remotecourses','mnet');
 149              $this->content->icons[] = '';
 150              foreach ($courses as $course) {
 151                  $this->content->items[]="<a title=\"" . format_string($course->shortname, true) . "\" ".
 152                      "href=\"{$CFG->wwwroot}/auth/mnet/jump.php?hostid={$course->hostid}&amp;wantsurl=/course/view.php?id={$course->remoteid}\">"
 153                      .$icon. format_string(get_course_display_name_for_list($course)) . "</a>";
 154              }
 155              // if we listed courses, we are done
 156              return true;
 157          }
 158  
 159          if ($hosts = get_my_remotehosts()) {
 160              $this->content->items[] = get_string('remotehosts', 'mnet');
 161              $this->content->icons[] = '';
 162              foreach($USER->mnet_foreign_host_array as $somehost) {
 163                  $this->content->items[] = $somehost['count'].get_string('courseson','mnet').'<a title="'.$somehost['name'].'" href="'.$somehost['url'].'">'.$icon.$somehost['name'].'</a>';
 164              }
 165              // if we listed hosts, done
 166              return true;
 167          }
 168  
 169          return false;
 170      }
 171  
 172      /**
 173       * Returns the role that best describes the course list block.
 174       *
 175       * @return string
 176       */
 177      public function get_aria_role() {
 178          return 'navigation';
 179      }
 180  
 181      /**
 182       * Return the plugin config settings for external functions.
 183       *
 184       * @return stdClass the configs for both the block instance and plugin
 185       * @since Moodle 3.8
 186       */
 187      public function get_config_for_external() {
 188          global $CFG;
 189  
 190          // Return all settings for all users since it is safe (no private keys, etc..).
 191          $configs = (object) [
 192              'adminview' => $CFG->block_course_list_adminview,
 193              'hideallcourseslink' => $CFG->block_course_list_hideallcourseslink
 194          ];
 195  
 196          return (object) [
 197              'instance' => new stdClass(),
 198              'plugin' => $configs,
 199          ];
 200      }
 201  }
 202  
 203