Search moodle.org's
Developer Documentation


/ -> index.php (source)
   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   * Moodle frontpage.
  19   *
  20   * @package    core
  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  if (!file_exists('./config.php')) {
  26      header('Location: install.php');
  27      die;
  28  }
  29  
  30  require_once('config.php');
  31  require_once($CFG->dirroot .'/course/lib.php');
  32  require_once($CFG->libdir .'/filelib.php');
  33  
  34  redirect_if_major_upgrade_required();
  35  
  36  $urlparams = array();
  37  if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY) && optional_param('redirect', 1, PARAM_BOOL) === 0) {
  38      $urlparams['redirect'] = 0;
  39  }
  40  $PAGE->set_url('/', $urlparams);
  41  $PAGE->set_course($SITE);
  42  $PAGE->set_other_editing_capability('moodle/course:update');
  43  $PAGE->set_other_editing_capability('moodle/course:manageactivities');
  44  $PAGE->set_other_editing_capability('moodle/course:activityvisibility');
  45  
  46  // Prevent caching of this page to stop confusion when changing page after making AJAX changes.
  47  $PAGE->set_cacheable(false);
  48  
  49  if ($CFG->forcelogin) {
  50      require_login();
  51  } else {
  52      user_accesstime_log();
  53  }
  54  
  55  $hassiteconfig = has_capability('moodle/site:config', context_system::instance());
  56  
  57  // If the site is currently under maintenance, then print a message.
  58  if (!empty($CFG->maintenance_enabled) and !$hassiteconfig) {
  59      print_maintenance_message();
  60  }
  61  
  62  if ($hassiteconfig && moodle_needs_upgrading()) {
  63      redirect($CFG->wwwroot .'/'. $CFG->admin .'/index.php');
  64  }
  65  
  66  if (get_home_page() != HOMEPAGE_SITE) {
  67      // Redirect logged-in users to My Moodle overview if required.
  68      $redirect = optional_param('redirect', 1, PARAM_BOOL);
  69      if (optional_param('setdefaulthome', false, PARAM_BOOL)) {
  70          set_user_preference('user_home_page_preference', HOMEPAGE_SITE);
  71      } else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY) && $redirect === 1) {
  72          redirect($CFG->wwwroot .'/my/');
  73      } else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER)) {
  74          $frontpagenode = $PAGE->settingsnav->find('frontpage', null);
  75          if ($frontpagenode) {
  76              $frontpagenode->add(
  77                  get_string('makethismyhome'),
  78                  new moodle_url('/', array('setdefaulthome' => true)),
  79                  navigation_node::TYPE_SETTING);
  80          } else {
  81              $frontpagenode = $PAGE->settingsnav->add(get_string('frontpagesettings'), null, navigation_node::TYPE_SETTING, null);
  82              $frontpagenode->force_open();
  83              $frontpagenode->add(get_string('makethismyhome'),
  84                  new moodle_url('/', array('setdefaulthome' => true)),
  85                  navigation_node::TYPE_SETTING);
  86          }
  87      }
  88  }
  89  
  90  // Trigger event.
  91  course_view(context_course::instance(SITEID));
  92  
  93  // If the hub plugin is installed then we let it take over the homepage here.
  94  if (file_exists($CFG->dirroot.'/local/hub/lib.php') and get_config('local_hub', 'hubenabled')) {
  95      require_once($CFG->dirroot.'/local/hub/lib.php');
  96      $hub = new local_hub();
  97      $continue = $hub->display_homepage();
  98      // Function display_homepage() returns true if the hub home page is not displayed
  99      // ...mostly when search form is not displayed for not logged users.
 100      if (empty($continue)) {
 101          exit;
 102      }
 103  }
 104  
 105  $PAGE->set_pagetype('site-index');
 106  $PAGE->set_docs_path('');
 107  $PAGE->set_pagelayout('frontpage');
 108  $editing = $PAGE->user_is_editing();
 109  $PAGE->set_title($SITE->fullname);
 110  $PAGE->set_heading($SITE->fullname);
 111  $courserenderer = $PAGE->get_renderer('core', 'course');
 112  echo $OUTPUT->header();
 113  
 114  // Print Section or custom info.
 115  $siteformatoptions = course_get_format($SITE)->get_format_options();
 116  $modinfo = get_fast_modinfo($SITE);
 117  $modnames = get_module_types_names();
 118  $modnamesplural = get_module_types_names(true);
 119  $modnamesused = $modinfo->get_used_module_names();
 120  $mods = $modinfo->get_cms();
 121  
 122  if (!empty($CFG->customfrontpageinclude)) {
 123      include($CFG->customfrontpageinclude);
 124  
 125  } else if ($siteformatoptions['numsections'] > 0) {
 126      if ($editing) {
 127          // Make sure section with number 1 exists.
 128          course_create_sections_if_missing($SITE, 1);
 129          // Re-request modinfo in case section was created.
 130          $modinfo = get_fast_modinfo($SITE);
 131      }
 132      $section = $modinfo->get_section_info(1);
 133      if (($section && (!empty($modinfo->sections[1]) or !empty($section->summary))) or $editing) {
 134          echo $OUTPUT->box_start('generalbox sitetopic');
 135  
 136          // If currently moving a file then show the current clipboard.
 137          if (ismoving($SITE->id)) {
 138              $stractivityclipboard = strip_tags(get_string('activityclipboard', '', $USER->activitycopyname));
 139              echo '<p><font size="2">';
 140              echo "$stractivityclipboard&nbsp;&nbsp;(<a href=\"course/mod.php?cancelcopy=true&amp;sesskey=".sesskey()."\">";
 141              echo get_string('cancel') . '</a>)';
 142              echo '</font></p>';
 143          }
 144  
 145          $context = context_course::instance(SITEID);
 146  
 147          // If the section name is set we show it.
 148          if (!is_null($section->name)) {
 149              echo $OUTPUT->heading(
 150                  format_string($section->name, true, array('context' => $context)),
 151                  2,
 152                  'sectionname'
 153              );
 154          }
 155  
 156          $summarytext = file_rewrite_pluginfile_urls($section->summary,
 157              'pluginfile.php',
 158              $context->id,
 159              'course',
 160              'section',
 161              $section->id);
 162          $summaryformatoptions = new stdClass();
 163          $summaryformatoptions->noclean = true;
 164          $summaryformatoptions->overflowdiv = true;
 165  
 166          echo format_text($summarytext, $section->summaryformat, $summaryformatoptions);
 167  
 168          if ($editing && has_capability('moodle/course:update', $context)) {
 169              $streditsummary = get_string('editsummary');
 170              echo "<a title=\"$streditsummary\" ".
 171                   " href=\"course/editsection.php?id=$section->id\"><img src=\"" . $OUTPUT->pix_url('t/edit') . "\" ".
 172                   " class=\"iconsmall\" alt=\"$streditsummary\" /></a><br /><br />";
 173          }
 174  
 175          $courserenderer = $PAGE->get_renderer('core', 'course');
 176          echo $courserenderer->course_section_cm_list($SITE, $section);
 177  
 178          echo $courserenderer->course_section_add_cm_control($SITE, $section->section);
 179          echo $OUTPUT->box_end();
 180      }
 181  }
 182  // Include course AJAX.
 183  include_course_ajax($SITE, $modnamesused);
 184  
 185  if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin)) {
 186      $frontpagelayout = $CFG->frontpageloggedin;
 187  } else {
 188      $frontpagelayout = $CFG->frontpage;
 189  }
 190  
 191  foreach (explode(',', $frontpagelayout) as $v) {
 192      switch ($v) {
 193          // Display the main part of the front page.
 194          case FRONTPAGENEWS:
 195              if ($SITE->newsitems) {
 196                  // Print forums only when needed.
 197                  require_once($CFG->dirroot .'/mod/forum/lib.php');
 198  
 199                  if (! $newsforum = forum_get_course_forum($SITE->id, 'news')) {
 200                      print_error('cannotfindorcreateforum', 'forum');
 201                  }
 202  
 203                  // Fetch news forum context for proper filtering to happen.
 204                  $newsforumcm = get_coursemodule_from_instance('forum', $newsforum->id, $SITE->id, false, MUST_EXIST);
 205                  $newsforumcontext = context_module::instance($newsforumcm->id, MUST_EXIST);
 206  
 207                  $forumname = format_string($newsforum->name, true, array('context' => $newsforumcontext));
 208                  echo html_writer::tag('a',
 209                      get_string('skipa', 'access', core_text::strtolower(strip_tags($forumname))),
 210                      array('href' => '#skipsitenews', 'class' => 'skip-block'));
 211  
 212                  // Wraps site news forum in div container.
 213                  echo html_writer::start_tag('div', array('id' => 'site-news-forum'));
 214  
 215                  if (isloggedin()) {
 216                      $SESSION->fromdiscussion = $CFG->wwwroot;
 217                      $subtext = '';
 218                      if (\mod_forum\subscriptions::is_subscribed($USER->id, $newsforum)) {
 219                          if (!\mod_forum\subscriptions::is_forcesubscribed($newsforum)) {
 220                              $subtext = get_string('unsubscribe', 'forum');
 221                          }
 222                      } else {
 223                          $subtext = get_string('subscribe', 'forum');
 224                      }
 225                      echo $OUTPUT->heading($forumname);
 226                      $suburl = new moodle_url('/mod/forum/subscribe.php', array('id' => $newsforum->id, 'sesskey' => sesskey()));
 227                      echo html_writer::tag('div', html_writer::link($suburl, $subtext), array('class' => 'subscribelink'));
 228                  } else {
 229                      echo $OUTPUT->heading($forumname);
 230                  }
 231  
 232                  forum_print_latest_discussions($SITE, $newsforum, $SITE->newsitems, 'plain', 'p.modified DESC');
 233  
 234                  // End site news forum div container.
 235                  echo html_writer::end_tag('div');
 236  
 237                  echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipsitenews'));
 238              }
 239          break;
 240  
 241          case FRONTPAGEENROLLEDCOURSELIST:
 242              $mycourseshtml = $courserenderer->frontpage_my_courses();
 243              if (!empty($mycourseshtml)) {
 244                  echo html_writer::tag('a',
 245                      get_string('skipa', 'access', core_text::strtolower(get_string('mycourses'))),
 246                      array('href' => '#skipmycourses', 'class' => 'skip-block'));
 247  
 248                  // Wrap frontpage course list in div container.
 249                  echo html_writer::start_tag('div', array('id' => 'frontpage-course-list'));
 250  
 251                  echo $OUTPUT->heading(get_string('mycourses'));
 252                  echo $mycourseshtml;
 253  
 254                  // End frontpage course list div container.
 255                  echo html_writer::end_tag('div');
 256  
 257                  echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipmycourses'));
 258                  break;
 259              }
 260              // No "break" here. If there are no enrolled courses - continue to 'Available courses'.
 261  
 262          case FRONTPAGEALLCOURSELIST:
 263              $availablecourseshtml = $courserenderer->frontpage_available_courses();
 264              if (!empty($availablecourseshtml)) {
 265                  echo html_writer::tag('a',
 266                      get_string('skipa', 'access', core_text::strtolower(get_string('availablecourses'))),
 267                      array('href' => '#skipavailablecourses', 'class' => 'skip-block'));
 268  
 269                  // Wrap frontpage course list in div container.
 270                  echo html_writer::start_tag('div', array('id' => 'frontpage-course-list'));
 271  
 272                  echo $OUTPUT->heading(get_string('availablecourses'));
 273                  echo $availablecourseshtml;
 274  
 275                  // End frontpage course list div container.
 276                  echo html_writer::end_tag('div');
 277  
 278                  echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipavailablecourses'));
 279              }
 280          break;
 281  
 282          case FRONTPAGECATEGORYNAMES:
 283              echo html_writer::tag('a',
 284                  get_string('skipa', 'access', core_text::strtolower(get_string('categories'))),
 285                  array('href' => '#skipcategories', 'class' => 'skip-block'));
 286  
 287              // Wrap frontpage category names in div container.
 288              echo html_writer::start_tag('div', array('id' => 'frontpage-category-names'));
 289  
 290              echo $OUTPUT->heading(get_string('categories'));
 291              echo $courserenderer->frontpage_categories_list();
 292  
 293              // End frontpage category names div container.
 294              echo html_writer::end_tag('div');
 295  
 296              echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipcategories'));
 297          break;
 298  
 299          case FRONTPAGECATEGORYCOMBO:
 300              echo html_writer::tag('a',
 301                  get_string('skipa', 'access', core_text::strtolower(get_string('courses'))),
 302                  array('href' => '#skipcourses', 'class' => 'skip-block'));
 303  
 304              // Wrap frontpage category combo in div container.
 305              echo html_writer::start_tag('div', array('id' => 'frontpage-category-combo'));
 306  
 307              echo $OUTPUT->heading(get_string('courses'));
 308              echo $courserenderer->frontpage_combo_list();
 309  
 310              // End frontpage category combo div container.
 311              echo html_writer::end_tag('div');
 312  
 313              echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipcourses'));
 314          break;
 315  
 316          case FRONTPAGECOURSESEARCH:
 317              echo $OUTPUT->box($courserenderer->course_search_form('', 'short'), 'mdl-align');
 318          break;
 319  
 320      }
 321      echo '<br />';
 322  }
 323  if ($editing && has_capability('moodle/course:create', context_system::instance())) {
 324      echo $courserenderer->add_new_course_button();
 325  }
 326  echo $OUTPUT->footer();

Search This Site: