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