Updated the HEAD build version to 20100410
[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
6be7abc7 11 * @package moodlecore
12 */
13require_once('config.php');
14
15// Get URL parameters.
bb45fe62 16$file = optional_param('file', '', PARAM_PATH);
17$text = optional_param('text', 'No text to display', PARAM_CLEAN);
6be7abc7 18$module = optional_param('module', 'moodle', PARAM_ALPHAEXT);
19$forcelang = optional_param('forcelang', '', PARAM_SAFEDIR);
45f5cfd4 20$skiplocal = optional_param('skiplocal', 0, PARAM_INT); // shall _local help files be skipped?
f8065dd2 21$fortooltip = optional_param('fortooltip', 0, PARAM_INT);
6be7abc7 22
151165e3 23$PAGE->set_course($COURSE);
24
a6855934 25$url = new moodle_url('/help.php');
474f6bfe 26if ($file !== '') {
27 $url->param('file', $file);
28}
29if ($text !== 'No text to display') {
30 $url->param('text', $text);
31}
32if ($module !== 'moodle') {
33 $url->param('module', $module);
34}
35if ($forcelang !== '') {
36 $url->param('forcelang', $forcelang);
37}
38if ($skiplocal !== 0) {
39 $url->param('skiplocal', $skiplocal);
40}
41if ($fortooltip !== 0) {
42 $url->param('fortooltip', $fortooltip);
43}
44$PAGE->set_url($url);
45
6be7abc7 46// We look for the help to display in lots of different places, and
47// only display an error at the end if we can't find the help file
48// anywhere. This variable tracks that.
49$helpfound = false;
50
facc7b38 51// Buffer output so that we can examine it later to extract metadata (page title)
52ob_start();
53
6be7abc7 54if (!empty($file)) {
55 // The help to display is from a help file.
ef330d95 56 list($filepath, $foundlang) = get_string_manager()->find_help_file($file, $module, $forcelang, $skiplocal);
868ca14f 57
bb45fe62 58 if ($filepath) {
59 $helpfound = true;
60 @include($filepath); // The actual helpfile
20cc8f60 61
bb45fe62 62 // Now, we process some special cases.
63 if ($module == 'moodle' and ($file == 'index.html' or $file == 'mods.html')) {
64 include_help_for_each_module($file, $forcelang, $skiplocal);
65 }
66 if ($module == 'question' && $file == 'types.html') {
67 include_help_for_each_qtype();
6be7abc7 68 }
20cc8f60 69
bb45fe62 70 // The remaining horrible hardcoded special cases should be delegated to modules somehow.
bb45fe62 71 if ($module == 'moodle' && $file == 'assignment/types.html') { // ASSIGNMENTS
72 include_help_for_each_assignment_type($forcelang, $skiplocal);
6be7abc7 73 }
74 }
75} else {
76 // The help to display was given as an argument to this function.
77 echo '<p>'.s($text).'</p>'; // This param was already cleaned
78 $helpfound = true;
79}
822a1063 80
facc7b38 81// Finish buffer
bb45fe62 82$output = ob_get_contents();
f8065dd2 83
facc7b38 84ob_end_clean();
85
f8065dd2 86if ($fortooltip) {
87 echo shorten_text($output, 400, false, '<span class="readmore">' . get_string('clickhelpiconformoreinfo') . '</span>');
88 die();
89}
90
facc7b38 91// Determine title
bb45fe62 92$title = get_string('help'); // Default is just 'Help'
93$matches = array();
facc7b38 94// You can include a <title> tag to override the standard behaviour:
95// 'Help - title contents'. Otherwise it looks for the text of the first
96// heading: 'Help - heading text'. If there aren't even any headings
97// you just get 'Help'
bb45fe62 98if (preg_match('~^(.*?)<title>(.*?)</title>(.*)$~s', $output, $matches)) {
facc7b38 99 // Extract title
bb45fe62 100 $title = $title.' - '.$matches[2];
facc7b38 101 // Strip title from output
bb45fe62 102 $output = $matches[1].$matches[3];
facc7b38 103} else if(preg_match('~<h[0-9]+(\s[^>]*)?>(.*?)</h[0-9]+>~s',$output,$matches)) {
104 // Use first heading as title (obviously leave it in output too). Strip
105 // any tags from inside
bb45fe62 106 $matches[2] = preg_replace('~<[^>]*>~s','',$matches[2]);
107 $title = $title.' - '.$matches[2];
facc7b38 108}
109
01260c78 110// use ##emoticons_html## to replace the emoticons documentation
111if(preg_match('~(##emoticons_html##)~', $output, $matches)) {
740939ec 112 $output = preg_replace('~(##emoticons_html##)~', get_emoticons_list_for_help_file(), $output);
01260c78 113}
114
facc7b38 115// Do the main output.
78946b9b 116$PAGE->set_pagelayout('popup');
70f25518 117$PAGE->set_title($title);
118echo $OUTPUT->header();
aa9a6867 119echo $OUTPUT->box_start();
facc7b38 120print $output;
aa9a6867 121echo $OUTPUT->box_end();
65cf9fc3 122
6be7abc7 123// Display an error if necessary.
124if (!$helpfound) {
aa9a6867 125 echo $OUTPUT->notification('Help file "'. $file .'" could not be found!');
6be7abc7 126}
5c0ee23c 127
6be7abc7 128// End of page.
5231a15c 129echo $OUTPUT->close_window_button();
bb45fe62 130echo '<p class="helpindex"><a href="help.php?file=index.html">'. get_string('helpindex') .'</a></p>';
9dc1fb4d 131
132// Offer a link to the alternative help file language
bb45fe62 133$currentlang = current_language();
e79079de 134if ($file && $helpfound && ($foundlang != 'en_utf8' || ($forcelang == 'en_utf8' && current_language() != 'en_utf8'))) {
a6855934 135 $url = new moodle_url($PAGE->url);
bb45fe62 136 if ($foundlang != 'en_utf8') {
137 $url->param('forcelang', 'en_utf8');
138 $nextlangname = get_string('english');
9dc1fb4d 139 } else {
bb45fe62 140 $url->param('forcelang', $currentlang);
9dc1fb4d 141 $nextlangname = get_string('thislanguage');
142 }
bb45fe62 143 echo '<p><a href="' . $url->out() . '">' . get_string('showthishelpinlanguage', 'moodle', $nextlangname) . '</a></p>';
9dc1fb4d 144}
d6f73f53 145
6be7abc7 146$CFG->docroot = ''; // We don't want a doc link here
7e0d6675 147echo $OUTPUT->footer();
6be7abc7 148
6be7abc7 149function file_exists_and_readable($filepath) {
150 return file_exists($filepath) and is_file($filepath) and is_readable($filepath);
151}
152
153// Some functions for handling special cases ========================================
154
bb45fe62 155function include_help_for_each_module($file, $forcelang, $skiplocal) {
f33e1ed4 156 global $CFG, $DB;
6be7abc7 157
bb45fe62 158 if (!$modules = $DB->get_records('modules', array('visible'=> 1))) {
159 print_error('nomodules', 'debug'); // Should never happen
6be7abc7 160 }
bb45fe62 161
162 // Horrible hack to show the help about grades here too.
072901a9 163 $grade = new stdClass();
164 $grade->name = 'grade';
165 $modules[] = $grade;
6be7abc7 166
167 foreach ($modules as $mod) {
168 $strmodulename = get_string('modulename', $mod->name);
169 $modulebyname[$strmodulename] = $mod;
170 }
dba21d4a 171 ksort($modulebyname, SORT_LOCALE_STRING);
6be7abc7 172
173 foreach ($modulebyname as $mod) {
ef330d95 174 list($filepath, $foundlang) = get_string_manager()->find_help_file($file, $mod->name, $forcelang, $skiplocal);
bb45fe62 175 if ($filepath) {
176 echo '<hr />';
177 include($filepath);
65cf9fc3 178 }
aa635c47 179 }
6be7abc7 180}
aa635c47 181
8bc590f5 182function include_help_for_each_qtype() {
183 global $CFG;
184 require_once($CFG->libdir . '/questionlib.php');
185 global $QTYPES;
186 $types = question_type_menu();
187 $fakeqtypes = array();
188 foreach ($types as $qtype => $localizedname) {
189 if ($QTYPES[$qtype]->is_real_question_type()) {
190 include_help_for_qtype($qtype, $localizedname);
191 } else {
192 $fakeqtypes[$qtype] = $localizedname;
193 }
194 }
195 foreach ($fakeqtypes as $qtype => $localizedname) {
196 include_help_for_qtype($qtype, $localizedname);
197 }
198}
199function include_help_for_qtype($qtype, $localizedname) {
200 echo '<h2>' . $localizedname . "</h2>\n\n";
201 echo '<p>' . get_string($qtype . 'summary', 'qtype_' . $qtype) . "</p>\n\n";
202}
203
6be7abc7 204function include_help_for_each_assignment_type() {
205 global $CFG;
206
207 require_once($CFG->dirroot .'/mod/assignment/lib.php');
208 $typelist = assignment_types();
868ca14f 209
6be7abc7 210 foreach ($typelist as $type => $name) {
bb45fe62 211 echo '<h2>'.$name.'</h2>';
6be7abc7 212 echo get_string('help'.$type, 'assignment');
bb45fe62 213 echo '<hr />';
6be7abc7 214 }
215}
ed5ab9f7 216?>