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