New file
[moodle.git] / index.php
blob7a1cc0b3dc790a27c13e109dedbafa72f3af8680
1 <?PHP // $Id$
2 // index.php - the front page.
4 if (!file_exists('./config.php')) {
5 header('Location: install.php');
6 die;
9 /// Bounds for block widths on this page
10 define('BLOCK_L_MIN_WIDTH', 160);
11 define('BLOCK_L_MAX_WIDTH', 210);
12 define('BLOCK_R_MIN_WIDTH', 160);
13 define('BLOCK_R_MAX_WIDTH', 210);
15 require_once('config.php');
16 require_once($CFG->dirroot.'/course/lib.php');
17 require_once($CFG->dirroot.'/lib/blocklib.php');
18 require_once($CFG->dirroot.'/mod/resource/lib.php');
19 require_once($CFG->dirroot.'/mod/forum/lib.php');
21 if (! $site = get_site()) {
22 redirect("$CFG->wwwroot/$CFG->admin/index.php");
25 if ($CFG->forcelogin) {
26 require_login();
29 if (isadmin()) {
30 if (moodle_needs_upgrading()) {
31 redirect("$CFG->wwwroot/$CFG->admin/index.php");
35 if (empty($USER->id)) {
36 if (empty($CFG->loginhttps)) {
37 $wwwroot = $CFG->wwwroot;
38 } else {
39 $wwwroot = str_replace('http','https',$CFG->wwwroot);
41 $loginstring = "<font size=2><a href=\"$wwwroot/login/index.php\">".get_string("login")."</a></font>";
42 } else {
43 $loginstring = "<font size=1>".user_login_string($site)."</font>";
46 if (empty($CFG->langmenu)) {
47 $langmenu = "";
48 } else {
49 $currlang = current_language();
50 $langs = get_list_of_languages();
51 $langmenu = popup_form ("$CFG->wwwroot/index.php?lang=", $langs, "chooselang", $currlang, "", "", "", true);
54 print_header(strip_tags($site->fullname), "$site->fullname", "home", "",
55 "<meta name=\"description\" content=\"".s(strip_tags($site->summary))."\">",
56 true, "", "$loginstring$langmenu");
58 $editing = isediting($site->id);
60 // Doing this now so we can pass the results to block_action()
61 // and dodge the overhead of doing the same work twice.
63 $blocks = $site->blockinfo;
64 $delimpos = strpos($blocks, ':');
66 if($delimpos === false) {
67 // No ':' found, we have all left blocks
68 $leftblocks = explode(',', $blocks);
69 $rightblocks = array();
71 else if($delimpos === 0) {
72 // ':' at start of string, we have all right blocks
73 $blocks = substr($blocks, 1);
74 $leftblocks = array();
75 $rightblocks = explode(',', $blocks);
77 else {
78 // Both left and right blocks
79 $leftpart = substr($blocks, 0, $delimpos);
80 $rightpart = substr($blocks, $delimpos + 1);
81 $leftblocks = explode(',', $leftpart);
82 $rightblocks = explode(',', $rightpart);
85 if($editing) {
86 if (isset($_GET['blockaction'])) {
87 if (isset($_GET['blockid'])) {
88 block_action($site, $leftblocks, $rightblocks, strtolower($_GET['blockaction']), intval($_GET['blockid']));
92 // This has to happen after block_action() has possibly updated the two arrays
93 $allblocks = array_merge($leftblocks, $rightblocks);
95 $missingblocks = array();
96 $recblocks = get_records('blocks','visible','1');
98 // Note down which blocks are going to get displayed
99 blocks_used($allblocks, $recblocks);
101 if($editing && $recblocks) {
102 foreach($recblocks as $recblock) {
103 // If it's not hidden or displayed right now...
104 if(!in_array($recblock->id, $allblocks) && !in_array(-($recblock->id), $allblocks)) {
105 // And if it's applicable for display in this format...
106 $formats = block_method_result($recblock->name, 'applicable_formats');
108 if( isset($formats['site']) ? $formats['site'] : !empty($formats['all'])) {
109 // Translation: if the 'site' format is explicitly accepted/rejected, use
110 // that setting. Otherwise, fallback to the 'all' format. The empty() test
111 // uses the trick that empty() fails if 'all' is either !isset() or false.
113 // Add it to the missing blocks
114 $missingblocks[] = $recblock->id;
120 else {
121 // Note down which blocks are going to get displayed
122 $allblocks = array_merge($leftblocks, $rightblocks);
123 $recblocks = get_records('blocks','visible','1');
124 blocks_used($allblocks, $recblocks);
127 // If the block width cache is not set, set it
128 if(!isset($SESSION) or !isset($SESSION->blockcache) or
129 !isset($SESSION->blockcache->width->{$site->id}) or $editing) {
130 // This query might be optimized away if we 're in editing mode
131 if(!isset($recblocks)) {
132 $recblocks = get_records('blocks','visible','1');
134 $preferred_width_left = blocks_preferred_width($leftblocks, $recblocks);
135 $preferred_width_right = blocks_preferred_width($rightblocks, $recblocks);
137 // This may be kind of organizational overkill, granted...
138 // But is there any real need to simplify the structure?
139 $SESSION->blockcache->width->{$site->id}->left = $preferred_width_left;
140 $SESSION->blockcache->width->{$site->id}->right = $preferred_width_right;
141 } else {
142 $preferred_width_left = $SESSION->blockcache->width->{$site->id}->left;
143 $preferred_width_right = $SESSION->blockcache->width->{$site->id}->right;
146 $preferred_width_left = min($preferred_width_left, BLOCK_L_MAX_WIDTH);
147 $preferred_width_left = max($preferred_width_left, BLOCK_L_MIN_WIDTH);
148 $preferred_width_right = min($preferred_width_right, BLOCK_R_MAX_WIDTH);
149 $preferred_width_right = max($preferred_width_right, BLOCK_R_MIN_WIDTH);
154 <table width="100%" border="0" cellspacing="5" cellpadding="5">
155 <tr>
156 <?PHP
158 if(block_have_active($leftblocks) || $editing) {
159 echo '<td style="vertical-align: top; width: '.$preferred_width_left.'px;">';
160 print_course_blocks($site, $leftblocks, BLOCK_LEFT);
161 echo '</td>';
164 echo '<td style="vertical-align: top;">';
167 /// Print Section
168 if ($site->numsections > 0) {
169 print_simple_box_start('center', '100%', $THEME->cellcontent, 5, 'sitetopic');
171 /// If currently moving a file then show the current clipboard
172 if (ismoving($site->id)) {
173 $stractivityclipboard = strip_tags(get_string("activityclipboard", "", addslashes($USER->activitycopyname)));
174 echo "<p><font size=2>";
175 echo "$stractivityclipboard&nbsp;&nbsp;(<a href=\"course/mod.php?cancelcopy=true\">".get_string("cancel")."</a>)";
176 echo "</font></p>";
180 if (!$section = get_record('course_sections', 'course', $site->id, 'section', 1)) {
181 delete_records('course_sections', 'course', $site->id, 'section', 1); // Just in case
182 $section->course = $site->id;
183 $section->section = 1;
184 $section->summary = '';
185 $section->visible = 1;
186 $section->id = insert_record('course_sections', $section);
189 echo format_text($section->summary, FORMAT_HTML);
191 if ($editing) {
192 $streditsummary = get_string('editsummary');
193 echo "<a title=\"$streditsummary\" ".
194 " href=\"course/editsection.php?id=$section->id\"><img src=\"$CFG->pixpath/t/edit.gif\" ".
195 " height=11 width=11 border=0 alt=\"$streditsummary\"></a><br />";
198 echo '<br clear="all">';
200 get_all_mods($site->id, $mods, $modnames, $modnamesplural, $modnamesused);
201 print_section($site, $section, $mods, $modnamesused, true);
203 if ($editing) {
204 print_section_add_menus($site, $section->section, $modnames);
206 print_simple_box_end();
207 print_spacer(10);
210 switch ($CFG->frontpage) { /// Display the main part of the front page.
211 case FRONTPAGENEWS:
212 if (! $newsforum = forum_get_course_forum($site->id, "news")) {
213 error("Could not find or create a main news forum for the site");
216 if (isset($USER->id)) {
217 $SESSION->fromdiscussion = "$CFG->wwwroot";
218 if (forum_is_subscribed($USER->id, $newsforum->id)) {
219 $subtext = get_string("unsubscribe", "forum");
220 } else {
221 $subtext = get_string("subscribe", "forum");
223 $headertext = "<table border=0 width=100% cellpadding=0 cellspacing=0 class=headingblockcontent><tr>
224 <td>$newsforum->name</td>
225 <td align=right><font size=1>
226 <a href=\"mod/forum/subscribe.php?id=$newsforum->id\">$subtext</a>
227 </td></tr></table>";
228 } else {
229 $headertext = $newsforum->name;
231 print_heading_block($headertext);
232 print_spacer(8,1);
233 forum_print_latest_discussions($newsforum->id, $site->newsitems);
234 break;
236 case FRONTPAGECOURSELIST:
237 case FRONTPAGECATEGORYNAMES:
238 if (isset($USER->id) and !isset($USER->admin)) {
239 print_heading_block(get_string("mycourses"));
240 print_spacer(8,1);
241 print_my_moodle();
242 } else {
243 if (count_records("course_categories") > 1) {
244 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
245 print_heading_block(get_string("availablecourses"));
246 } else {
247 print_heading_block(get_string("categories"));
249 print_spacer(8,1);
250 print_simple_box_start("center", "100%");
251 print_whole_category_list();
252 print_simple_box_end();
253 print_course_search("", false, "short");
254 } else {
255 print_heading_block(get_string("availablecourses"));
256 print_spacer(8,1);
257 print_courses(0, "100%");
260 break;
264 echo '</td>';
265 if(block_have_active($rightblocks) || $editing || isadmin()) {
266 echo '<td style="vertical-align: top; width: '.$preferred_width_right.'px;">';
267 if (isadmin()) {
268 echo '<div align="center">'.update_course_icon($site->id).'</div>';
269 echo '<br />';
271 print_course_blocks($site, $rightblocks, BLOCK_RIGHT);
272 if ($editing && !empty($missingblocks)) {
273 block_print_blocks_admin($site, $missingblocks);
275 echo '</td>';
279 </tr>
280 </table>
282 <?PHP print_footer('home'); // Please do not modify this line ?>