MDL-37085 Substitute function print_section_add_menus() with core_course_renderer...
[moodle.git] / index.php
CommitLineData
474f6bfe 1<?php
0cfb6a52 2
af9f5430
PS
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.
14//
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/>.
17
18/**
19 * Moodle frontpage.
20 *
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
24 */
0cfb6a52 25
8b5b1028 26 if (!file_exists('./config.php')) {
27 header('Location: install.php');
28 die;
29 }
30
d41746c9 31 require_once('config.php');
32 require_once($CFG->dirroot .'/course/lib.php');
644d506a 33 require_once($CFG->libdir .'/filelib.php');
364fffda 34
5e39d7aa 35 redirect_if_major_upgrade_required();
89d1efe4 36
98054db1 37 $urlparams = array();
ab6ec58a 38 if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY) && optional_param('redirect', 1, PARAM_BOOL) === 0) {
98054db1
PS
39 $urlparams['redirect'] = 0;
40 }
41 $PAGE->set_url('/', $urlparams);
42 $PAGE->set_course($SITE);
43
fbb4c959
SH
44 // Prevent caching of this page to stop confusion when changing page after making AJAX changes
45 $PAGE->set_cacheable(false);
46
0a45ffe3 47 if ($CFG->forcelogin) {
48 require_login();
341b5ed2 49 } else {
50 user_accesstime_log();
0a45ffe3 51 }
52
bf0f06b1 53 $hassiteconfig = has_capability('moodle/site:config', context_system::instance());
32b9e80b 54
4fe2250a 55/// If the site is currently under maintenance, then print a message
32b9e80b 56 if (!empty($CFG->maintenance_enabled) and !$hassiteconfig) {
4fe2250a 57 print_maintenance_message();
58 }
59
32b9e80b
SH
60 if ($hassiteconfig && moodle_needs_upgrading()) {
61 redirect($CFG->wwwroot .'/'. $CFG->admin .'/index.php');
62 }
63
64 if (get_home_page() != HOMEPAGE_SITE) {
4766a50c
SH
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);
ab6ec58a 68 } else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY) && optional_param('redirect', 1, PARAM_BOOL) === 1) {
4766a50c 69 redirect($CFG->wwwroot .'/my/');
ab6ec58a 70 } else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER)) {
4766a50c 71 $PAGE->settingsnav->get('usercurrentsettings')->add(get_string('makethismyhome'), new moodle_url('/', array('setdefaulthome'=>true)), navigation_node::TYPE_SETTING);
18818abf 72 }
3565715c 73 }
74
4f0c2d00 75 if (isloggedin()) {
34dd5900 76 add_to_log(SITEID, 'course', 'view', 'view.php?id='.SITEID, SITEID);
a325bd2b 77 }
a5e3644b 78
825b6fdb 79/// If the hub plugin is installed then we let it take over the homepage here
13f18ae1 80 if (file_exists($CFG->dirroot.'/local/hub/lib.php') and get_config('local_hub', 'hubenabled')) {
abf63f98 81 require_once($CFG->dirroot.'/local/hub/lib.php');
82 $hub = new local_hub();
a4e56bf3 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;
88 }
abf63f98 89 }
90
f8065dd2 91 $PAGE->set_pagetype('site-index');
cfcfb9f3 92 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
82611d8d 93 $PAGE->set_docs_path('');
191b267b 94 $PAGE->set_pagelayout('frontpage');
ad52c04f 95 $editing = $PAGE->user_is_editing();
d4a03c00 96 $PAGE->set_title($SITE->fullname);
97 $PAGE->set_heading($SITE->fullname);
244a32c6 98 echo $OUTPUT->header();
8c336db1 99
8275eab8 100/// Print Section or custom info
850acb35 101 $siteformatoptions = course_get_format($SITE)->get_format_options();
d57aa283
MG
102 $modinfo = get_fast_modinfo($SITE);
103 $modnames = get_module_types_names();
104 $modnamesplural = get_module_types_names(true);
105 $modnamesused = $modinfo->get_used_module_names();
106 $mods = $modinfo->get_cms();
107
8275eab8
PS
108 if (!empty($CFG->customfrontpageinclude)) {
109 include($CFG->customfrontpageinclude);
110
850acb35 111 } else if ($siteformatoptions['numsections'] > 0) {
d57aa283 112 if ($editing) {
4ede27b2
MG
113 // make sure section with number 1 exists
114 course_create_sections_if_missing($SITE, 1);
d57aa283
MG
115 // re-request modinfo in case section was created
116 $modinfo = get_fast_modinfo($SITE);
45a6b40d 117 }
d57aa283
MG
118 $section = $modinfo->get_section_info(1);
119 if (($section && (!empty($modinfo->sections[1]) or !empty($section->summary))) or $editing) {
20486a5a 120 echo $OUTPUT->box_start('generalbox sitetopic');
5fc2f2fd 121
a2b82263 122 /// If currently moving a file then show the current clipboard
123 if (ismoving($SITE->id)) {
294ce987 124 $stractivityclipboard = strip_tags(get_string('activityclipboard', '', $USER->activitycopyname));
a2b82263 125 echo '<p><font size="2">';
973d2660 126 echo "$stractivityclipboard&nbsp;&nbsp;(<a href=\"course/mod.php?cancelcopy=true&amp;sesskey=".sesskey()."\">". get_string('cancel') .'</a>)';
a2b82263 127 echo '</font></p>';
128 }
5fc2f2fd 129
bf0f06b1 130 $context = context_course::instance(SITEID);
64f93798 131 $summarytext = file_rewrite_pluginfile_urls($section->summary, 'pluginfile.php', $context->id, 'course', 'section', $section->id);
a226a972 132 $summaryformatoptions = new stdClass();
644d506a 133 $summaryformatoptions->noclean = true;
367a75fa 134 $summaryformatoptions->overflowdiv = true;
644d506a 135
e34a326f 136 echo format_text($summarytext, $section->summaryformat, $summaryformatoptions);
5fc2f2fd 137
a2b82263 138 if ($editing) {
139 $streditsummary = get_string('editsummary');
140 echo "<a title=\"$streditsummary\" ".
b5d0cafc 141 " href=\"course/editsection.php?id=$section->id\"><img src=\"" . $OUTPUT->pix_url('t/edit') . "\" ".
0d905d9f 142 " class=\"iconsmall\" alt=\"$streditsummary\" /></a><br /><br />";
a2b82263 143 }
144
a2b82263 145 print_section($SITE, $section, $mods, $modnamesused, true);
364fffda 146
9a6aa5c1 147 echo $courserenderer->course_section_add_cm_control($SITE, $section->section);
20486a5a 148 echo $OUTPUT->box_end();
5fc2f2fd 149 }
5fc2f2fd 150 }
01e0e704 151 // Include course AJAX
697ff999 152 include_course_ajax($SITE, $modnamesused);
5fc2f2fd 153
64f93798 154 if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin)) {
6f24e48e 155 $frontpagelayout = $CFG->frontpageloggedin;
156 } else {
157 $frontpagelayout = $CFG->frontpage;
158 }
159
64f93798 160 foreach (explode(',',$frontpagelayout) as $v) {
e1638d61 161 switch ($v) { /// Display the main part of the front page.
220a90c5 162 case FRONTPAGENEWS:
e1638d61 163 if ($SITE->newsitems) { // Print forums only when needed
164 require_once($CFG->dirroot .'/mod/forum/lib.php');
165
166 if (! $newsforum = forum_get_course_forum($SITE->id, 'news')) {
33aa5723 167 print_error('cannotfindorcreateforum', 'forum');
e1638d61 168 }
c3b825e1
EL
169
170 // fetch news forum context for proper filtering to happen
171 $newsforumcm = get_coursemodule_from_instance('forum', $newsforum->id, $SITE->id, false, MUST_EXIST);
bf0f06b1 172 $newsforumcontext = context_module::instance($newsforumcm->id, MUST_EXIST);
c3b825e1
EL
173
174 $forumname = format_string($newsforum->name, true, array('context' => $newsforumcontext));
6f3451e5 175 echo html_writer::tag('a', get_string('skipa', 'access', textlib::strtolower(strip_tags($forumname))), array('href'=>'#skipsitenews', 'class'=>'skip-block'));
e1638d61 176
4f0c2d00 177 if (isloggedin()) {
e1638d61 178 $SESSION->fromdiscussion = $CFG->wwwroot;
6e5e0c93 179 $subtext = '';
90f4745c 180 if (forum_is_subscribed($USER->id, $newsforum)) {
730535cb 181 if (!forum_is_forcesubscribed($newsforum)) {
182 $subtext = get_string('unsubscribe', 'forum');
183 }
e1638d61 184 } else {
185 $subtext = get_string('subscribe', 'forum');
186 }
c3b825e1 187 echo $OUTPUT->heading($forumname, 2, 'headingblock header');
6f7d65c7
DM
188 $suburl = new moodle_url('/mod/forum/subscribe.php', array('id' => $newsforum->id, 'sesskey' => sesskey()));
189 echo html_writer::tag('div', html_writer::link($suburl, $subtext), array('class' => 'subscribelink'));
af328e4b 190 } else {
c3b825e1 191 echo $OUTPUT->heading($forumname, 2, 'headingblock header');
af328e4b 192 }
e1638d61 193
a5a9cd99 194 forum_print_latest_discussions($SITE, $newsforum, $SITE->newsitems, 'plain', 'p.modified DESC');
fd2aa40b 195 echo html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipsitenews'));
89adb174 196 }
e1638d61 197 break;
89adb174 198
64f93798 199 case FRONTPAGECOURSELIST:
98795fd1 200 $ncourses = $DB->count_records('course');
32b9e80b 201 if (isloggedin() and !$hassiteconfig and !isguestuser() and empty($CFG->disablemycourses)) {
6f3451e5 202 echo html_writer::tag('a', get_string('skipa', 'access', textlib::strtolower(get_string('mycourses'))), array('href'=>'#skipmycourses', 'class'=>'skip-block'));
706b82da 203 echo $OUTPUT->heading(get_string('mycourses'), 2, 'headingblock header');
e1638d61 204 print_my_moodle();
fd2aa40b 205 echo html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipmycourses'));
98795fd1 206 } else if ((!$hassiteconfig and !isguestuser()) or ($ncourses <= FRONTPAGECOURSELIMIT)) {
fe2920ad 207 // admin should not see list of courses when there are too many of them
6f3451e5 208 echo html_writer::tag('a', get_string('skipa', 'access', textlib::strtolower(get_string('availablecourses'))), array('href'=>'#skipavailablecourses', 'class'=>'skip-block'));
706b82da 209 echo $OUTPUT->heading(get_string('availablecourses'), 2, 'headingblock header');
5f6b9c09 210 print_courses(0);
fd2aa40b 211 echo html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipavailablecourses'));
98795fd1
DM
212 } else {
213 echo html_writer::tag('div', get_string('therearecourses', '', $ncourses), array('class' => 'notifyproblem'));
214 print_course_search('', false, 'short');
64f93798 215 }
e1638d61 216 break;
89adb174 217
e0140f24 218 case FRONTPAGECATEGORYNAMES:
6f3451e5 219 echo html_writer::tag('a', get_string('skipa', 'access', textlib::strtolower(get_string('categories'))), array('href'=>'#skipcategories', 'class'=>'skip-block'));
706b82da 220 echo $OUTPUT->heading(get_string('categories'), 2, 'headingblock header');
20486a5a 221 echo $OUTPUT->box_start('generalbox categorybox');
d157bd5b 222 print_whole_category_list(NULL, NULL, NULL, -1, false);
20486a5a 223 echo $OUTPUT->box_end();
e0140f24 224 print_course_search('', false, 'short');
fd2aa40b 225 echo html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipcategories'));
e0140f24 226 break;
227
6f24e48e 228 case FRONTPAGECATEGORYCOMBO:
6f3451e5 229 echo html_writer::tag('a', get_string('skipa', 'access', textlib::strtolower(get_string('courses'))), array('href'=>'#skipcourses', 'class'=>'skip-block'));
fd2aa40b 230 echo $OUTPUT->heading(get_string('courses'), 2, 'headingblock header');
24e27ac0 231 $renderer = $PAGE->get_renderer('core','course');
2848b979
DC
232 // if there are too many courses, budiling course category tree could be slow,
233 // users should go to course index page to see the whole list.
234 $coursecount = $DB->count_records('course');
235 if (empty($CFG->numcoursesincombo)) {
236 // if $CFG->numcoursesincombo hasn't been set, use default value 500
237 $CFG->numcoursesincombo = 500;
238 }
239 if ($coursecount > $CFG->numcoursesincombo) {
240 $link = new moodle_url('/course/');
241 echo $OUTPUT->notification(get_string('maxnumcoursesincombo', 'moodle', array('link'=>$link->out(), 'maxnumofcourses'=>$CFG->numcoursesincombo, 'numberofcourses'=>$coursecount)));
242 } else {
243 echo $renderer->course_category_tree(get_course_category_tree());
244 }
6f24e48e 245 print_course_search('', false, 'short');
fd2aa40b 246 echo html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipcourses'));
6f24e48e 247 break;
248
e1638d61 249 case FRONTPAGETOPICONLY: // Do nothing!! :-)
250 break;
5eafd948 251
e1638d61 252 }
253 echo '<br />';
89adb174 254 }
255
d4a03c00 256 echo $OUTPUT->footer();