MDL-20808 Fixes for amf web services and test client - a web service browser.
[moodle.git] / help.php
blobff3b7c973b197d194af441eb304186c54da4d3ac
1 <?php
2 /**
3 * help.php - Displays help page.
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}
10 * @author Martin Dougiamas
11 * @package moodlecore
13 require_once('config.php');
15 // Get URL parameters.
16 $file = optional_param('file', '', PARAM_PATH);
17 $text = optional_param('text', 'No text to display', PARAM_CLEAN);
18 $module = optional_param('module', 'moodle', PARAM_ALPHAEXT);
19 $forcelang = optional_param('forcelang', '', PARAM_SAFEDIR);
20 $skiplocal = optional_param('skiplocal', 0, PARAM_INT); // shall _local help files be skipped?
21 $fortooltip = optional_param('fortooltip', 0, PARAM_INT);
23 $PAGE->set_course($COURSE);
25 $url = new moodle_url('/help.php');
26 if ($file !== '') {
27 $url->param('file', $file);
29 if ($text !== 'No text to display') {
30 $url->param('text', $text);
32 if ($module !== 'moodle') {
33 $url->param('module', $module);
35 if ($forcelang !== '') {
36 $url->param('forcelang', $forcelang);
38 if ($skiplocal !== 0) {
39 $url->param('skiplocal', $skiplocal);
41 if ($fortooltip !== 0) {
42 $url->param('fortooltip', $fortooltip);
44 $PAGE->set_url($url);
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;
51 // Buffer output so that we can examine it later to extract metadata (page title)
52 ob_start();
54 if (!empty($file)) {
55 // The help to display is from a help file.
56 list($filepath, $foundlang) = string_manager::instance()->find_help_file($file, $module, $forcelang, $skiplocal);
58 if ($filepath) {
59 $helpfound = true;
60 @include($filepath); // The actual helpfile
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);
66 if ($module == 'question' && $file == 'types.html') {
67 include_help_for_each_qtype();
70 // The remaining horrible hardcoded special cases should be delegated to modules somehow.
71 if ($module == 'moodle' && $file == 'assignment/types.html') { // ASSIGNMENTS
72 include_help_for_each_assignment_type($forcelang, $skiplocal);
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;
81 // Finish buffer
82 $output = ob_get_contents();
84 ob_end_clean();
86 if ($fortooltip) {
87 echo shorten_text($output, 400, false, '<span class="readmore">' . get_string('clickhelpiconformoreinfo') . '</span>');
88 die();
91 // Determine title
92 $title = get_string('help'); // Default is just 'Help'
93 $matches = array();
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'
98 if (preg_match('~^(.*?)<title>(.*?)</title>(.*)$~s', $output, $matches)) {
99 // Extract title
100 $title = $title.' - '.$matches[2];
101 // Strip title from output
102 $output = $matches[1].$matches[3];
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
106 $matches[2] = preg_replace('~<[^>]*>~s','',$matches[2]);
107 $title = $title.' - '.$matches[2];
110 // use ##emoticons_html## to replace the emoticons documentation
111 if(preg_match('~(##emoticons_html##)~', $output, $matches)) {
112 $output = preg_replace('~(##emoticons_html##)~', get_emoticons_list_for_help_file(), $output);
115 // Do the main output.
116 $PAGE->set_pagelayout('popup');
117 $PAGE->set_title($title);
118 echo $OUTPUT->header();
119 echo $OUTPUT->box_start();
120 print $output;
121 echo $OUTPUT->box_end();
123 // Display an error if necessary.
124 if (!$helpfound) {
125 echo $OUTPUT->notification('Help file "'. $file .'" could not be found!');
128 // End of page.
129 echo $OUTPUT->close_window_button();
130 echo '<p class="helpindex"><a href="help.php?file=index.html">'. get_string('helpindex') .'</a></p>';
132 // Offer a link to the alternative help file language
133 $currentlang = current_language();
134 if ($file && $helpfound && ($foundlang != 'en_utf8' || ($forcelang == 'en_utf8' && current_language() != 'en_utf8'))) {
135 $url = new moodle_url($PAGE->url);
136 if ($foundlang != 'en_utf8') {
137 $url->param('forcelang', 'en_utf8');
138 $nextlangname = get_string('english');
139 } else {
140 $url->param('forcelang', $currentlang);
141 $nextlangname = get_string('thislanguage');
143 echo '<p><a href="' . $url->out() . '">' . get_string('showthishelpinlanguage', 'moodle', $nextlangname) . '</a></p>';
146 $CFG->docroot = ''; // We don't want a doc link here
147 echo $OUTPUT->footer();
149 function file_exists_and_readable($filepath) {
150 return file_exists($filepath) and is_file($filepath) and is_readable($filepath);
153 // Some functions for handling special cases ========================================
155 function include_help_for_each_module($file, $forcelang, $skiplocal) {
156 global $CFG, $DB;
158 if (!$modules = $DB->get_records('modules', array('visible'=> 1))) {
159 print_error('nomodules', 'debug'); // Should never happen
162 // Horrible hack to show the help about grades here too.
163 $grade = new stdClass();
164 $grade->name = 'grade';
165 $modules[] = $grade;
167 foreach ($modules as $mod) {
168 $strmodulename = get_string('modulename', $mod->name);
169 $modulebyname[$strmodulename] = $mod;
171 ksort($modulebyname, SORT_LOCALE_STRING);
173 foreach ($modulebyname as $mod) {
174 list($filepath, $foundlang) = string_manager::instance()->find_help_file($file, $mod->name, $forcelang, $skiplocal);
175 if ($filepath) {
176 echo '<hr />';
177 include($filepath);
182 function 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;
195 foreach ($fakeqtypes as $qtype => $localizedname) {
196 include_help_for_qtype($qtype, $localizedname);
199 function 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";
204 function include_help_for_each_assignment_type() {
205 global $CFG;
207 require_once($CFG->dirroot .'/mod/assignment/lib.php');
208 $typelist = assignment_types();
210 foreach ($typelist as $type => $name) {
211 echo '<h2>'.$name.'</h2>';
212 echo get_string('help'.$type, 'assignment');
213 echo '<hr />';