adding capabilities
[moodle.git] / help.php
CommitLineData
822a1063 1<?php
6be7abc7 2/**
3 * help.php - Displays help page.
4 *
5 * Prints a very simple page and includes
6 * page content or a string from elsewhere.
7 * Usually this will appear in a popup
8 * See {@link helpbutton()} in {@link lib/moodlelib.php}
9 *
10 * @author Martin Dougiamas
11 * @version $Id$
12 * @package moodlecore
13 */
14require_once('config.php');
15
16// Get URL parameters.
17$file = optional_param('file', '', PARAM_PATH);
18$text = optional_param('text', 'No text to display', PARAM_CLEAN);
19$module = optional_param('module', 'moodle', PARAM_ALPHAEXT);
20$forcelang = optional_param('forcelang', '', PARAM_SAFEDIR);
21
22// Start the output.
23print_header();
24print_simple_box_start('center', '96%');
25
26// We look for the help to display in lots of different places, and
27// only display an error at the end if we can't find the help file
28// anywhere. This variable tracks that.
29$helpfound = false;
30
31if (!empty($file)) {
32 // The help to display is from a help file.
868ca14f 33
6be7abc7 34 // Get the list of parent languages.
35 if (empty($forcelang)) {
36 $langs = array(current_language(), get_string('parentlanguage'), 'en_utf8'); // Fallback
37 } else {
38 $langs = array($forcelang);
868ca14f 39 }
6be7abc7 40
41 // Work through the possible languages, starting with the most specific.
42 foreach ($langs as $lang) {
43 if (empty($lang)) {
44 continue;
45 }
868ca14f 46
6be7abc7 47 // Work out which directory the help files live in.
48 if ($lang == 'en_utf8') {
49 $helpdir = $CFG->dirroot;
50 } else {
51 $helpdir = $CFG->dataroot;
52 }
53 $helpdir .= "/lang/$lang/help";
868ca14f 54
6be7abc7 55 // Then which file in there we should be serving.
56 if ($module == 'moodle') {
57 $filepath = "$helpdir/$file";
58 } else {
59 $filepath = "$helpdir/$module/$file";
868ca14f 60
6be7abc7 61 // If that does not exist, try a fallback into the module code folder.
62 if (!file_exists($filepath)) {
63 $filepath = "$CFG->dirroot/mod/$module/lang/$lang/help/$module/$file";
64 }
65 }
66
67 // Now, try to include the help text from this file, if we can.
68 if (file_exists_and_readable($filepath)) {
69 $helpfound = true;
70 @include($filepath); // The actual helpfile
822a1063 71
868ca14f 72 // Now, we process some special cases.
6be7abc7 73 if ($module == 'moodle' and ($file == 'index.html' or $file == 'mods.html')) {
74 include_help_for_each_module($file, $langs, $helpdir);
75 }
68396ed9 76
6be7abc7 77 // The remaining horrible hardcoded special cases should be delegated to modules somehow.
78 if ($module == 'moodle' and ($file == 'resource/types.html')) { // RESOURCES
79 include_help_for_each_resource($file, $langs, $helpdir);
80 }
81 if ($module == 'moodle' and ($file == 'assignment/types.html')) { // ASSIGNMENTS
82 include_help_for_each_assignment_type();
83 }
822a1063 84
6be7abc7 85 // Having found some help, we break out of the loop over languages.
86 break;
87 }
88 }
89} else {
90 // The help to display was given as an argument to this function.
91 echo '<p>'.s($text).'</p>'; // This param was already cleaned
92 $helpfound = true;
93}
822a1063 94
6be7abc7 95print_simple_box_end();
65cf9fc3 96
6be7abc7 97// Display an error if necessary.
98if (!$helpfound) {
99 notify('Help file "'. $file .'" could not be found!');
100}
5c0ee23c 101
6be7abc7 102// End of page.
103close_window_button();
104echo '<p align="center"><a href="help.php?file=index.html">'. get_string('helpindex') .'</a></p>';
d6f73f53 105
6be7abc7 106$CFG->docroot = ''; // We don't want a doc link here
107print_footer('none');
108
109// Utility function =================================================================
110
111function file_exists_and_readable($filepath) {
112 return file_exists($filepath) and is_file($filepath) and is_readable($filepath);
113}
114
115// Some functions for handling special cases ========================================
116
117function include_help_for_each_module($file, $langs, $helpdir) {
118 global $CFG;
119
120 if (!$modules = get_records('modules', 'visible', 1)) {
121 error('No modules found!!'); // Should never happen
122 }
123
124 foreach ($modules as $mod) {
125 $strmodulename = get_string('modulename', $mod->name);
126 $modulebyname[$strmodulename] = $mod;
127 }
128 ksort($modulebyname);
129
130 foreach ($modulebyname as $mod) {
aa635c47 131 foreach ($langs as $lang) {
132 if (empty($lang)) {
133 continue;
134 }
6be7abc7 135
136 $filepath = "$helpdir/$mod->name/$file";
137
138 // If that does not exist, try a fallback into the module code folder.
139 if (!file_exists($filepath)) {
140 $filepath = "$CFG->dirroot/mod/$mod->name/lang/$lang/help/$mod->name/$file";
70442fe3 141 }
68396ed9 142
6be7abc7 143 if (file_exists_and_readable($filepath)) {
144 echo '<hr size="1" />';
145 @include($filepath); // The actual helpfile
146 break; // Out of loop over languages.
70442fe3 147 }
65cf9fc3 148 }
aa635c47 149 }
6be7abc7 150}
aa635c47 151
6be7abc7 152function include_help_for_each_resource($file, $langs, $helpdir) {
153 global $CFG;
d6f73f53 154
6be7abc7 155 require_once($CFG->dirroot .'/mod/resource/lib.php');
156 $typelist = resource_get_resource_types();
157 $typelist['label'] = get_string('resourcetypelabel', 'resource');
95b50076 158
6be7abc7 159 foreach ($typelist as $type => $name) {
160 foreach ($langs as $lang) {
161 if (empty($lang)) {
162 continue;
163 }
8634bc27 164
6be7abc7 165 $filepath = "$helpdir/resource/type/$type.html";
8634bc27 166
6be7abc7 167 if (file_exists_and_readable($filepath)) {
168 echo '<hr size="1" />';
169 @include($filepath); // The actual helpfile
170 break; // Out of loop over languages.
171 }
172 }
173 }
174}
175
176function include_help_for_each_assignment_type() {
177 global $CFG;
178
179 require_once($CFG->dirroot .'/mod/assignment/lib.php');
180 $typelist = assignment_types();
868ca14f 181
6be7abc7 182 foreach ($typelist as $type => $name) {
183 echo '<p><b>'.$name.'</b></p>';
184 echo get_string('help'.$type, 'assignment');
185 echo '<hr size="1" />';
186 }
187}
ed5ab9f7 188?>