MDL-37977 frontpage: renderer method for frontpage section1
[moodle.git] / index.php
CommitLineData
474f6bfe 1<?php
af9f5430
PS
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 */
0cfb6a52 24
c9edde8b
DB
25if (!file_exists('./config.php')) {
26 header('Location: install.php');
27 die;
28}
29
30require_once('config.php');
31require_once($CFG->dirroot .'/course/lib.php');
32require_once($CFG->libdir .'/filelib.php');
33
34redirect_if_major_upgrade_required();
35
36$urlparams = array();
37if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY) && optional_param('redirect', 1, PARAM_BOOL) === 0) {
38 $urlparams['redirect'] = 0;
39}
40$PAGE->set_url('/', $urlparams);
e8a7f5ff 41$PAGE->set_pagelayout('frontpage');
c9edde8b
DB
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
ce3f8046 49require_course_login($SITE);
c9edde8b 50
59c66f92 51$hasmaintenanceaccess = has_capability('moodle/site:maintenanceaccess', context_system::instance());
c9edde8b
DB
52
53// If the site is currently under maintenance, then print a message.
59c66f92 54if (!empty($CFG->maintenance_enabled) and !$hasmaintenanceaccess) {
c9edde8b
DB
55 print_maintenance_message();
56}
57
59c66f92
MD
58$hassiteconfig = has_capability('moodle/site:config', context_system::instance());
59
c9edde8b
DB
60if ($hassiteconfig && moodle_needs_upgrading()) {
61 redirect($CFG->wwwroot .'/'. $CFG->admin .'/index.php');
62}
63
1095afa4
MG
64// If site registration needs updating, redirect.
65\core\hub\registration::registration_reminder('/index.php');
66
c9edde8b
DB
67if (get_home_page() != HOMEPAGE_SITE) {
68 // Redirect logged-in users to My Moodle overview if required.
69 $redirect = optional_param('redirect', 1, PARAM_BOOL);
70 if (optional_param('setdefaulthome', false, PARAM_BOOL)) {
71 set_user_preference('user_home_page_preference', HOMEPAGE_SITE);
72 } else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY) && $redirect === 1) {
73 redirect($CFG->wwwroot .'/my/');
74 } else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER)) {
d9926e86
AG
75 $frontpagenode = $PAGE->settingsnav->find('frontpage', null);
76 if ($frontpagenode) {
77 $frontpagenode->add(
78 get_string('makethismyhome'),
79 new moodle_url('/', array('setdefaulthome' => true)),
80 navigation_node::TYPE_SETTING);
81 } else {
82 $frontpagenode = $PAGE->settingsnav->add(get_string('frontpagesettings'), null, navigation_node::TYPE_SETTING, null);
83 $frontpagenode->force_open();
84 $frontpagenode->add(get_string('makethismyhome'),
85 new moodle_url('/', array('setdefaulthome' => true)),
86 navigation_node::TYPE_SETTING);
87 }
98054db1 88 }
c9edde8b
DB
89}
90
22588835
JL
91// Trigger event.
92course_view(context_course::instance(SITEID));
c9edde8b
DB
93
94// If the hub plugin is installed then we let it take over the homepage here.
95if (file_exists($CFG->dirroot.'/local/hub/lib.php') and get_config('local_hub', 'hubenabled')) {
96 require_once($CFG->dirroot.'/local/hub/lib.php');
97 $hub = new local_hub();
98 $continue = $hub->display_homepage();
99 // Function display_homepage() returns true if the hub home page is not displayed
100 // ...mostly when search form is not displayed for not logged users.
101 if (empty($continue)) {
102 exit;
0a45ffe3 103 }
c9edde8b
DB
104}
105
106$PAGE->set_pagetype('site-index');
107$PAGE->set_docs_path('');
c9edde8b
DB
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');
112echo $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
122if (!empty($CFG->customfrontpageinclude)) {
123 include($CFG->customfrontpageinclude);
124
125} else if ($siteformatoptions['numsections'] > 0) {
a400dd2d 126 echo $courserenderer->frontpage_section1();
c9edde8b
DB
127}
128// Include course AJAX.
129include_course_ajax($SITE, $modnamesused);
130
131if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin)) {
132 $frontpagelayout = $CFG->frontpageloggedin;
133} else {
134 $frontpagelayout = $CFG->frontpage;
135}
136
137foreach (explode(',', $frontpagelayout) as $v) {
138 switch ($v) {
139 // Display the main part of the front page.
140 case FRONTPAGENEWS:
141 if ($SITE->newsitems) {
142 // Print forums only when needed.
143 require_once($CFG->dirroot .'/mod/forum/lib.php');
144
145 if (! $newsforum = forum_get_course_forum($SITE->id, 'news')) {
146 print_error('cannotfindorcreateforum', 'forum');
147 }
5fc2f2fd 148
c9edde8b
DB
149 // Fetch news forum context for proper filtering to happen.
150 $newsforumcm = get_coursemodule_from_instance('forum', $newsforum->id, $SITE->id, false, MUST_EXIST);
151 $newsforumcontext = context_module::instance($newsforumcm->id, MUST_EXIST);
6f24e48e 152
c9edde8b 153 $forumname = format_string($newsforum->name, true, array('context' => $newsforumcontext));
9e8d0842 154 echo html_writer::link('#skipsitenews',
c9edde8b 155 get_string('skipa', 'access', core_text::strtolower(strip_tags($forumname))),
9e8d0842 156 array('class' => 'skip-block skip'));
e1638d61 157
c9edde8b
DB
158 // Wraps site news forum in div container.
159 echo html_writer::start_tag('div', array('id' => 'site-news-forum'));
c3b825e1 160
c9edde8b
DB
161 if (isloggedin()) {
162 $SESSION->fromdiscussion = $CFG->wwwroot;
163 $subtext = '';
164 if (\mod_forum\subscriptions::is_subscribed($USER->id, $newsforum)) {
165 if (!\mod_forum\subscriptions::is_forcesubscribed($newsforum)) {
166 $subtext = get_string('unsubscribe', 'forum');
e1638d61 167 }
af328e4b 168 } else {
c9edde8b 169 $subtext = get_string('subscribe', 'forum');
af328e4b 170 }
c9edde8b
DB
171 echo $OUTPUT->heading($forumname);
172 $suburl = new moodle_url('/mod/forum/subscribe.php', array('id' => $newsforum->id, 'sesskey' => sesskey()));
173 echo html_writer::tag('div', html_writer::link($suburl, $subtext), array('class' => 'subscribelink'));
174 } else {
175 echo $OUTPUT->heading($forumname);
176 }
e1638d61 177
c9edde8b 178 forum_print_latest_discussions($SITE, $newsforum, $SITE->newsitems, 'plain', 'p.modified DESC');
4c71a80a 179
c9edde8b
DB
180 // End site news forum div container.
181 echo html_writer::end_tag('div');
4c71a80a 182
9e8d0842 183 echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipsitenews'));
c9edde8b
DB
184 }
185 break;
89adb174 186
c9edde8b
DB
187 case FRONTPAGEENROLLEDCOURSELIST:
188 $mycourseshtml = $courserenderer->frontpage_my_courses();
189 if (!empty($mycourseshtml)) {
9e8d0842 190 echo html_writer::link('#skipmycourses',
c9edde8b 191 get_string('skipa', 'access', core_text::strtolower(get_string('mycourses'))),
9e8d0842 192 array('class' => 'skip skip-block'));
4c71a80a 193
c9edde8b
DB
194 // Wrap frontpage course list in div container.
195 echo html_writer::start_tag('div', array('id' => 'frontpage-course-list'));
4c71a80a 196
c9edde8b
DB
197 echo $OUTPUT->heading(get_string('mycourses'));
198 echo $mycourseshtml;
4c71a80a 199
c9edde8b
DB
200 // End frontpage course list div container.
201 echo html_writer::end_tag('div');
4c71a80a 202
9e8d0842 203 echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipmycourses'));
c9edde8b
DB
204 break;
205 }
206 // No "break" here. If there are no enrolled courses - continue to 'Available courses'.
0fd26350 207
c9edde8b
DB
208 case FRONTPAGEALLCOURSELIST:
209 $availablecourseshtml = $courserenderer->frontpage_available_courses();
210 if (!empty($availablecourseshtml)) {
9e8d0842 211 echo html_writer::link('#skipavailablecourses',
c9edde8b 212 get_string('skipa', 'access', core_text::strtolower(get_string('availablecourses'))),
9e8d0842 213 array('class' => 'skip skip-block'));
4c71a80a 214
c9edde8b
DB
215 // Wrap frontpage course list in div container.
216 echo html_writer::start_tag('div', array('id' => 'frontpage-course-list'));
4c71a80a 217
c9edde8b
DB
218 echo $OUTPUT->heading(get_string('availablecourses'));
219 echo $availablecourseshtml;
4c71a80a 220
c9edde8b
DB
221 // End frontpage course list div container.
222 echo html_writer::end_tag('div');
4c71a80a 223
9e8d0842 224 echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipavailablecourses'));
c9edde8b
DB
225 }
226 break;
89adb174 227
c9edde8b 228 case FRONTPAGECATEGORYNAMES:
9e8d0842 229 echo html_writer::link('#skipcategories',
c9edde8b 230 get_string('skipa', 'access', core_text::strtolower(get_string('categories'))),
9e8d0842 231 array('class' => 'skip skip-block'));
4c71a80a 232
c9edde8b
DB
233 // Wrap frontpage category names in div container.
234 echo html_writer::start_tag('div', array('id' => 'frontpage-category-names'));
4c71a80a 235
c9edde8b
DB
236 echo $OUTPUT->heading(get_string('categories'));
237 echo $courserenderer->frontpage_categories_list();
4c71a80a 238
c9edde8b
DB
239 // End frontpage category names div container.
240 echo html_writer::end_tag('div');
4c71a80a 241
9e8d0842 242 echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipcategories'));
c9edde8b 243 break;
e0140f24 244
c9edde8b 245 case FRONTPAGECATEGORYCOMBO:
9e8d0842 246 echo html_writer::link('#skipcourses',
c9edde8b 247 get_string('skipa', 'access', core_text::strtolower(get_string('courses'))),
9e8d0842 248 array('class' => 'skip skip-block'));
4c71a80a 249
c9edde8b
DB
250 // Wrap frontpage category combo in div container.
251 echo html_writer::start_tag('div', array('id' => 'frontpage-category-combo'));
4c71a80a 252
c9edde8b
DB
253 echo $OUTPUT->heading(get_string('courses'));
254 echo $courserenderer->frontpage_combo_list();
4c71a80a 255
c9edde8b
DB
256 // End frontpage category combo div container.
257 echo html_writer::end_tag('div');
4c71a80a 258
9e8d0842 259 echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipcourses'));
c9edde8b 260 break;
6f24e48e 261
c9edde8b
DB
262 case FRONTPAGECOURSESEARCH:
263 echo $OUTPUT->box($courserenderer->course_search_form('', 'short'), 'mdl-align');
264 break;
5eafd948 265
ee11f4a7 266 }
c9edde8b
DB
267 echo '<br />';
268}
269if ($editing && has_capability('moodle/course:create', context_system::instance())) {
270 echo $courserenderer->add_new_course_button();
271}
272echo $OUTPUT->footer();