Merge branch 'MDL-49159' of https://github.com/spvickers/moodle
[moodle.git] / index.php
blob5d5e0f740783187ced5700b47994d1a00eba81dd
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.
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/>.
17 /**
18 * Moodle frontpage.
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
25 if (!file_exists('./config.php')) {
26 header('Location: install.php');
27 die;
30 require_once('config.php');
31 require_once($CFG->dirroot .'/course/lib.php');
32 require_once($CFG->libdir .'/filelib.php');
34 redirect_if_major_upgrade_required();
36 $urlparams = array();
37 if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY) && optional_param('redirect', 1, PARAM_BOOL) === 0) {
38 $urlparams['redirect'] = 0;
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');
46 // Prevent caching of this page to stop confusion when changing page after making AJAX changes.
47 $PAGE->set_cacheable(false);
49 if ($CFG->forcelogin) {
50 require_login();
51 } else {
52 user_accesstime_log();
55 $hassiteconfig = has_capability('moodle/site:config', context_system::instance());
57 // If the site is currently under maintenance, then print a message.
58 if (!empty($CFG->maintenance_enabled) and !$hassiteconfig) {
59 print_maintenance_message();
62 if ($hassiteconfig && moodle_needs_upgrading()) {
63 redirect($CFG->wwwroot .'/'. $CFG->admin .'/index.php');
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 $PAGE->settingsnav->get('usercurrentsettings')->add(
75 get_string('makethismyhome'),
76 new moodle_url('/', array('setdefaulthome' => true)),
77 navigation_node::TYPE_SETTING);
81 $eventparams = array('context' => context_course::instance(SITEID));
82 $event = \core\event\course_viewed::create($eventparams);
83 $event->trigger();
85 // If the hub plugin is installed then we let it take over the homepage here.
86 if (file_exists($CFG->dirroot.'/local/hub/lib.php') and get_config('local_hub', 'hubenabled')) {
87 require_once($CFG->dirroot.'/local/hub/lib.php');
88 $hub = new local_hub();
89 $continue = $hub->display_homepage();
90 // Function display_homepage() returns true if the hub home page is not displayed
91 // ...mostly when search form is not displayed for not logged users.
92 if (empty($continue)) {
93 exit;
97 $PAGE->set_pagetype('site-index');
98 $PAGE->set_docs_path('');
99 $PAGE->set_pagelayout('frontpage');
100 $editing = $PAGE->user_is_editing();
101 $PAGE->set_title($SITE->fullname);
102 $PAGE->set_heading($SITE->fullname);
103 $courserenderer = $PAGE->get_renderer('core', 'course');
104 echo $OUTPUT->header();
106 // Print Section or custom info.
107 $siteformatoptions = course_get_format($SITE)->get_format_options();
108 $modinfo = get_fast_modinfo($SITE);
109 $modnames = get_module_types_names();
110 $modnamesplural = get_module_types_names(true);
111 $modnamesused = $modinfo->get_used_module_names();
112 $mods = $modinfo->get_cms();
114 if (!empty($CFG->customfrontpageinclude)) {
115 include($CFG->customfrontpageinclude);
117 } else if ($siteformatoptions['numsections'] > 0) {
118 if ($editing) {
119 // Make sure section with number 1 exists.
120 course_create_sections_if_missing($SITE, 1);
121 // Re-request modinfo in case section was created.
122 $modinfo = get_fast_modinfo($SITE);
124 $section = $modinfo->get_section_info(1);
125 if (($section && (!empty($modinfo->sections[1]) or !empty($section->summary))) or $editing) {
126 echo $OUTPUT->box_start('generalbox sitetopic');
128 // If currently moving a file then show the current clipboard.
129 if (ismoving($SITE->id)) {
130 $stractivityclipboard = strip_tags(get_string('activityclipboard', '', $USER->activitycopyname));
131 echo '<p><font size="2">';
132 echo "$stractivityclipboard&nbsp;&nbsp;(<a href=\"course/mod.php?cancelcopy=true&amp;sesskey=".sesskey()."\">";
133 echo get_string('cancel') . '</a>)';
134 echo '</font></p>';
137 $context = context_course::instance(SITEID);
139 // If the section name is set we show it.
140 if (!is_null($section->name)) {
141 echo $OUTPUT->heading(
142 format_string($section->name, true, array('context' => $context)),
144 'sectionname'
148 $summarytext = file_rewrite_pluginfile_urls($section->summary,
149 'pluginfile.php',
150 $context->id,
151 'course',
152 'section',
153 $section->id);
154 $summaryformatoptions = new stdClass();
155 $summaryformatoptions->noclean = true;
156 $summaryformatoptions->overflowdiv = true;
158 echo format_text($summarytext, $section->summaryformat, $summaryformatoptions);
160 if ($editing && has_capability('moodle/course:update', $context)) {
161 $streditsummary = get_string('editsummary');
162 echo "<a title=\"$streditsummary\" ".
163 " href=\"course/editsection.php?id=$section->id\"><img src=\"" . $OUTPUT->pix_url('t/edit') . "\" ".
164 " class=\"iconsmall\" alt=\"$streditsummary\" /></a><br /><br />";
167 $courserenderer = $PAGE->get_renderer('core', 'course');
168 echo $courserenderer->course_section_cm_list($SITE, $section);
170 echo $courserenderer->course_section_add_cm_control($SITE, $section->section);
171 echo $OUTPUT->box_end();
174 // Include course AJAX.
175 include_course_ajax($SITE, $modnamesused);
177 if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin)) {
178 $frontpagelayout = $CFG->frontpageloggedin;
179 } else {
180 $frontpagelayout = $CFG->frontpage;
183 foreach (explode(',', $frontpagelayout) as $v) {
184 switch ($v) {
185 // Display the main part of the front page.
186 case FRONTPAGENEWS:
187 if ($SITE->newsitems) {
188 // Print forums only when needed.
189 require_once($CFG->dirroot .'/mod/forum/lib.php');
191 if (! $newsforum = forum_get_course_forum($SITE->id, 'news')) {
192 print_error('cannotfindorcreateforum', 'forum');
195 // Fetch news forum context for proper filtering to happen.
196 $newsforumcm = get_coursemodule_from_instance('forum', $newsforum->id, $SITE->id, false, MUST_EXIST);
197 $newsforumcontext = context_module::instance($newsforumcm->id, MUST_EXIST);
199 $forumname = format_string($newsforum->name, true, array('context' => $newsforumcontext));
200 echo html_writer::tag('a',
201 get_string('skipa', 'access', core_text::strtolower(strip_tags($forumname))),
202 array('href' => '#skipsitenews', 'class' => 'skip-block'));
204 // Wraps site news forum in div container.
205 echo html_writer::start_tag('div', array('id' => 'site-news-forum'));
207 if (isloggedin()) {
208 $SESSION->fromdiscussion = $CFG->wwwroot;
209 $subtext = '';
210 if (\mod_forum\subscriptions::is_subscribed($USER->id, $newsforum)) {
211 if (!\mod_forum\subscriptions::is_forcesubscribed($newsforum)) {
212 $subtext = get_string('unsubscribe', 'forum');
214 } else {
215 $subtext = get_string('subscribe', 'forum');
217 echo $OUTPUT->heading($forumname);
218 $suburl = new moodle_url('/mod/forum/subscribe.php', array('id' => $newsforum->id, 'sesskey' => sesskey()));
219 echo html_writer::tag('div', html_writer::link($suburl, $subtext), array('class' => 'subscribelink'));
220 } else {
221 echo $OUTPUT->heading($forumname);
224 forum_print_latest_discussions($SITE, $newsforum, $SITE->newsitems, 'plain', 'p.modified DESC');
226 // End site news forum div container.
227 echo html_writer::end_tag('div');
229 echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipsitenews'));
231 break;
233 case FRONTPAGEENROLLEDCOURSELIST:
234 $mycourseshtml = $courserenderer->frontpage_my_courses();
235 if (!empty($mycourseshtml)) {
236 echo html_writer::tag('a',
237 get_string('skipa', 'access', core_text::strtolower(get_string('mycourses'))),
238 array('href' => '#skipmycourses', 'class' => 'skip-block'));
240 // Wrap frontpage course list in div container.
241 echo html_writer::start_tag('div', array('id' => 'frontpage-course-list'));
243 echo $OUTPUT->heading(get_string('mycourses'));
244 echo $mycourseshtml;
246 // End frontpage course list div container.
247 echo html_writer::end_tag('div');
249 echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipmycourses'));
250 break;
252 // No "break" here. If there are no enrolled courses - continue to 'Available courses'.
254 case FRONTPAGEALLCOURSELIST:
255 $availablecourseshtml = $courserenderer->frontpage_available_courses();
256 if (!empty($availablecourseshtml)) {
257 echo html_writer::tag('a',
258 get_string('skipa', 'access', core_text::strtolower(get_string('availablecourses'))),
259 array('href' => '#skipavailablecourses', 'class' => 'skip-block'));
261 // Wrap frontpage course list in div container.
262 echo html_writer::start_tag('div', array('id' => 'frontpage-course-list'));
264 echo $OUTPUT->heading(get_string('availablecourses'));
265 echo $availablecourseshtml;
267 // End frontpage course list div container.
268 echo html_writer::end_tag('div');
270 echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipavailablecourses'));
272 break;
274 case FRONTPAGECATEGORYNAMES:
275 echo html_writer::tag('a',
276 get_string('skipa', 'access', core_text::strtolower(get_string('categories'))),
277 array('href' => '#skipcategories', 'class' => 'skip-block'));
279 // Wrap frontpage category names in div container.
280 echo html_writer::start_tag('div', array('id' => 'frontpage-category-names'));
282 echo $OUTPUT->heading(get_string('categories'));
283 echo $courserenderer->frontpage_categories_list();
285 // End frontpage category names div container.
286 echo html_writer::end_tag('div');
288 echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipcategories'));
289 break;
291 case FRONTPAGECATEGORYCOMBO:
292 echo html_writer::tag('a',
293 get_string('skipa', 'access', core_text::strtolower(get_string('courses'))),
294 array('href' => '#skipcourses', 'class' => 'skip-block'));
296 // Wrap frontpage category combo in div container.
297 echo html_writer::start_tag('div', array('id' => 'frontpage-category-combo'));
299 echo $OUTPUT->heading(get_string('courses'));
300 echo $courserenderer->frontpage_combo_list();
302 // End frontpage category combo div container.
303 echo html_writer::end_tag('div');
305 echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipcourses'));
306 break;
308 case FRONTPAGECOURSESEARCH:
309 echo $OUTPUT->box($courserenderer->course_search_form('', 'short'), 'mdl-align');
310 break;
313 echo '<br />';
315 if ($editing && has_capability('moodle/course:create', context_system::instance())) {
316 echo $courserenderer->add_new_course_button();
318 echo $OUTPUT->footer();