Merge branch 'master_MDL-37844' of git://github.com/danmarsden/moodle
[moodle.git] / index.php
blob8ad377bf613c74d2d8ccddec3978464429ca5d36
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Moodle frontpage.
21 * @package core
22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 if (!file_exists('./config.php')) {
27 header('Location: install.php');
28 die;
31 require_once('config.php');
32 require_once($CFG->dirroot .'/course/lib.php');
33 require_once($CFG->libdir .'/filelib.php');
35 redirect_if_major_upgrade_required();
37 $urlparams = array();
38 if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY) && optional_param('redirect', 1, PARAM_BOOL) === 0) {
39 $urlparams['redirect'] = 0;
41 $PAGE->set_url('/', $urlparams);
42 $PAGE->set_course($SITE);
44 // Prevent caching of this page to stop confusion when changing page after making AJAX changes
45 $PAGE->set_cacheable(false);
47 if ($CFG->forcelogin) {
48 require_login();
49 } else {
50 user_accesstime_log();
53 $hassiteconfig = has_capability('moodle/site:config', context_system::instance());
55 /// If the site is currently under maintenance, then print a message
56 if (!empty($CFG->maintenance_enabled) and !$hassiteconfig) {
57 print_maintenance_message();
60 if ($hassiteconfig && moodle_needs_upgrading()) {
61 redirect($CFG->wwwroot .'/'. $CFG->admin .'/index.php');
64 if (get_home_page() != HOMEPAGE_SITE) {
65 // Redirect logged-in users to My Moodle overview if required
66 if (optional_param('setdefaulthome', false, PARAM_BOOL)) {
67 set_user_preference('user_home_page_preference', HOMEPAGE_SITE);
68 } else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY) && optional_param('redirect', 1, PARAM_BOOL) === 1) {
69 redirect($CFG->wwwroot .'/my/');
70 } else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER)) {
71 $PAGE->settingsnav->get('usercurrentsettings')->add(get_string('makethismyhome'), new moodle_url('/', array('setdefaulthome'=>true)), navigation_node::TYPE_SETTING);
75 if (isloggedin()) {
76 add_to_log(SITEID, 'course', 'view', 'view.php?id='.SITEID, SITEID);
79 /// If the hub plugin is installed then we let it take over the homepage here
80 if (file_exists($CFG->dirroot.'/local/hub/lib.php') and get_config('local_hub', 'hubenabled')) {
81 require_once($CFG->dirroot.'/local/hub/lib.php');
82 $hub = new local_hub();
83 $continue = $hub->display_homepage();
84 //display_homepage() return true if the hub home page is not displayed
85 //mostly when search form is not displayed for not logged users
86 if (empty($continue)) {
87 exit;
91 $PAGE->set_pagetype('site-index');
92 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
93 $PAGE->set_docs_path('');
94 $PAGE->set_pagelayout('frontpage');
95 $editing = $PAGE->user_is_editing();
96 $PAGE->set_title($SITE->fullname);
97 $PAGE->set_heading($SITE->fullname);
98 $courserenderer = $PAGE->get_renderer('core', 'course');
99 echo $OUTPUT->header();
101 /// Print Section or custom info
102 $siteformatoptions = course_get_format($SITE)->get_format_options();
103 $modinfo = get_fast_modinfo($SITE);
104 $modnames = get_module_types_names();
105 $modnamesplural = get_module_types_names(true);
106 $modnamesused = $modinfo->get_used_module_names();
107 $mods = $modinfo->get_cms();
109 if (!empty($CFG->customfrontpageinclude)) {
110 include($CFG->customfrontpageinclude);
112 } else if ($siteformatoptions['numsections'] > 0) {
113 if ($editing) {
114 // make sure section with number 1 exists
115 course_create_sections_if_missing($SITE, 1);
116 // re-request modinfo in case section was created
117 $modinfo = get_fast_modinfo($SITE);
119 $section = $modinfo->get_section_info(1);
120 if (($section && (!empty($modinfo->sections[1]) or !empty($section->summary))) or $editing) {
121 echo $OUTPUT->box_start('generalbox sitetopic');
123 /// If currently moving a file then show the current clipboard
124 if (ismoving($SITE->id)) {
125 $stractivityclipboard = strip_tags(get_string('activityclipboard', '', $USER->activitycopyname));
126 echo '<p><font size="2">';
127 echo "$stractivityclipboard&nbsp;&nbsp;(<a href=\"course/mod.php?cancelcopy=true&amp;sesskey=".sesskey()."\">". get_string('cancel') .'</a>)';
128 echo '</font></p>';
131 $context = context_course::instance(SITEID);
132 $summarytext = file_rewrite_pluginfile_urls($section->summary, 'pluginfile.php', $context->id, 'course', 'section', $section->id);
133 $summaryformatoptions = new stdClass();
134 $summaryformatoptions->noclean = true;
135 $summaryformatoptions->overflowdiv = true;
137 echo format_text($summarytext, $section->summaryformat, $summaryformatoptions);
139 if ($editing) {
140 $streditsummary = get_string('editsummary');
141 echo "<a title=\"$streditsummary\" ".
142 " href=\"course/editsection.php?id=$section->id\"><img src=\"" . $OUTPUT->pix_url('t/edit') . "\" ".
143 " class=\"iconsmall\" alt=\"$streditsummary\" /></a><br /><br />";
146 $courserenderer = $PAGE->get_renderer('core', 'course');
147 echo $courserenderer->course_section_cm_list($SITE, $section);
149 echo $courserenderer->course_section_add_cm_control($SITE, $section->section);
150 echo $OUTPUT->box_end();
153 // Include course AJAX
154 include_course_ajax($SITE, $modnamesused);
156 if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin)) {
157 $frontpagelayout = $CFG->frontpageloggedin;
158 } else {
159 $frontpagelayout = $CFG->frontpage;
162 foreach (explode(',',$frontpagelayout) as $v) {
163 switch ($v) { /// Display the main part of the front page.
164 case FRONTPAGENEWS:
165 if ($SITE->newsitems) { // Print forums only when needed
166 require_once($CFG->dirroot .'/mod/forum/lib.php');
168 if (! $newsforum = forum_get_course_forum($SITE->id, 'news')) {
169 print_error('cannotfindorcreateforum', 'forum');
172 // fetch news forum context for proper filtering to happen
173 $newsforumcm = get_coursemodule_from_instance('forum', $newsforum->id, $SITE->id, false, MUST_EXIST);
174 $newsforumcontext = context_module::instance($newsforumcm->id, MUST_EXIST);
176 $forumname = format_string($newsforum->name, true, array('context' => $newsforumcontext));
177 echo html_writer::tag('a', get_string('skipa', 'access', textlib::strtolower(strip_tags($forumname))), array('href'=>'#skipsitenews', 'class'=>'skip-block'));
179 // wraps site news forum in div container.
180 echo html_writer::start_tag('div', array('id'=>'site-news-forum'));
182 if (isloggedin()) {
183 $SESSION->fromdiscussion = $CFG->wwwroot;
184 $subtext = '';
185 if (forum_is_subscribed($USER->id, $newsforum)) {
186 if (!forum_is_forcesubscribed($newsforum)) {
187 $subtext = get_string('unsubscribe', 'forum');
189 } else {
190 $subtext = get_string('subscribe', 'forum');
192 echo $OUTPUT->heading($forumname, 2, 'headingblock header');
193 $suburl = new moodle_url('/mod/forum/subscribe.php', array('id' => $newsforum->id, 'sesskey' => sesskey()));
194 echo html_writer::tag('div', html_writer::link($suburl, $subtext), array('class' => 'subscribelink'));
195 } else {
196 echo $OUTPUT->heading($forumname, 2, 'headingblock header');
199 forum_print_latest_discussions($SITE, $newsforum, $SITE->newsitems, 'plain', 'p.modified DESC');
201 //end site news forum div container
202 echo html_writer::end_tag('div');
204 echo html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipsitenews'));
206 break;
208 case FRONTPAGECOURSELIST:
209 $ncourses = $DB->count_records('course');
210 if (isloggedin() and !$hassiteconfig and !isguestuser() and empty($CFG->disablemycourses)) {
211 $mycourseshtml = $courserenderer->frontpage_my_courses();
212 if (empty($mycourseshtml)) {
213 if ($DB->count_records("course_categories") > 1) {
214 $mycourseshtml = $courserenderer->frontpage_categories_list();
215 } else {
216 $mycourseshtml = $courserenderer->frontpage_available_courses();
219 echo html_writer::tag('a', get_string('skipa', 'access', textlib::strtolower(get_string('mycourses'))), array('href'=>'#skipmycourses', 'class'=>'skip-block'));
221 //wrap frontpage course list in div container
222 echo html_writer::start_tag('div', array('id'=>'frontpage-course-list'));
224 echo $OUTPUT->heading(get_string('mycourses'), 2, 'headingblock header');
225 echo $mycourseshtml;
226 echo $OUTPUT->box($courserenderer->course_search_form('', 'short'), 'mdl-align');
228 //end frontpage course list div container
229 echo html_writer::end_tag('div');
231 echo html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipmycourses'));
232 } else if ((!$hassiteconfig and !isguestuser()) or ($ncourses <= FRONTPAGECOURSELIMIT)) {
233 // admin should not see list of courses when there are too many of them
234 echo html_writer::tag('a', get_string('skipa', 'access', textlib::strtolower(get_string('availablecourses'))), array('href'=>'#skipavailablecourses', 'class'=>'skip-block'));
236 //wrap frontpage course list in div container
237 echo html_writer::start_tag('div', array('id'=>'frontpage-course-list'));
239 echo $OUTPUT->heading(get_string('availablecourses'), 2, 'headingblock header');
240 echo $courserenderer->frontpage_available_courses();
242 //end frontpage course list div container
243 echo html_writer::end_tag('div');
245 echo html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipavailablecourses'));
246 } else {
247 echo html_writer::tag('div', get_string('therearecourses', '', $ncourses), array('class' => 'notifyproblem'));
248 echo $OUTPUT->box($courserenderer->course_search_form('', 'short'), 'mdl-align');
250 break;
252 case FRONTPAGECATEGORYNAMES:
253 echo html_writer::tag('a', get_string('skipa', 'access', textlib::strtolower(get_string('categories'))), array('href'=>'#skipcategories', 'class'=>'skip-block'));
255 //wrap frontpage category names in div container
256 echo html_writer::start_tag('div', array('id'=>'frontpage-category-names'));
258 echo $OUTPUT->heading(get_string('categories'), 2, 'headingblock header');
259 echo $courserenderer->frontpage_categories_list();
260 echo $OUTPUT->box($courserenderer->course_search_form('', 'short'), 'mdl-align');
262 //end frontpage category names div container
263 echo html_writer::end_tag('div');
265 echo html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipcategories'));
266 break;
268 case FRONTPAGECATEGORYCOMBO:
269 echo html_writer::tag('a', get_string('skipa', 'access', textlib::strtolower(get_string('courses'))), array('href'=>'#skipcourses', 'class'=>'skip-block'));
271 //wrap frontpage category combo in div container
272 echo html_writer::start_tag('div', array('id'=>'frontpage-category-combo'));
274 echo $OUTPUT->heading(get_string('courses'), 2, 'headingblock header');
275 // if there are too many courses, building course category tree could be slow,
276 // users should go to course index page to see the whole list.
277 $coursecount = $DB->count_records('course');
278 if (empty($CFG->numcoursesincombo)) {
279 // if $CFG->numcoursesincombo hasn't been set, use default value 500
280 $CFG->numcoursesincombo = 500;
282 if ($coursecount > $CFG->numcoursesincombo) {
283 $link = new moodle_url('/course/');
284 echo $OUTPUT->notification(get_string('maxnumcoursesincombo', 'moodle', array('link'=>$link->out(), 'maxnumofcourses'=>$CFG->numcoursesincombo, 'numberofcourses'=>$coursecount)));
285 } else {
286 echo $courserenderer->frontpage_combo_list();
288 echo $OUTPUT->box($courserenderer->course_search_form('', 'short'), 'mdl-align');
290 //end frontpage category combo div container
291 echo html_writer::end_tag('div');
293 echo html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipcourses'));
294 break;
296 case FRONTPAGETOPICONLY: // Do nothing!! :-)
297 break;
300 echo '<br />';
303 echo $OUTPUT->footer();