abf45bed |
1 | <?php // $Id$ |
f9903ed0 |
2 | |
9fa49e22 |
3 | /////////////////////////////////////////////////////////////////////////// |
4 | // // |
5 | // NOTICE OF COPYRIGHT // |
6 | // // |
7 | // Moodle - Modular Object-Oriented Dynamic Learning Environment // |
8 | // http://moodle.com // |
9 | // // |
10 | // Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com // |
11 | // // |
12 | // This program is free software; you can redistribute it and/or modify // |
13 | // it under the terms of the GNU General Public License as published by // |
14 | // the Free Software Foundation; either version 2 of the License, or // |
15 | // (at your option) any later version. // |
16 | // // |
17 | // This program is distributed in the hope that it will be useful, // |
18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
20 | // GNU General Public License for more details: // |
21 | // // |
22 | // http://www.gnu.org/copyleft/gpl.html // |
23 | // // |
24 | /////////////////////////////////////////////////////////////////////////// |
f9903ed0 |
25 | |
7cf1c7bd |
26 | /** |
27 | * Library of functions for web output |
28 | * |
29 | * Library of all general-purpose Moodle PHP functions and constants |
30 | * that produce HTML output |
31 | * |
32 | * Other main libraries: |
33 | * - datalib.php - functions that access the database. |
34 | * - moodlelib.php - general-purpose Moodle functions. |
35 | * @author Martin Dougiamas |
36 | * @version $Id$ |
89dcb99d |
37 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
7cf1c7bd |
38 | * @package moodlecore |
39 | */ |
772e78be |
40 | |
6aaa17c7 |
41 | /// We are going to uses filterlib functions here |
42 | require_once("$CFG->libdir/filterlib.php"); |
43 | |
0095d5cd |
44 | /// Constants |
45 | |
c1d57101 |
46 | /// Define text formatting types ... eventually we can add Wiki, BBcode etc |
7cf1c7bd |
47 | |
48 | /** |
49 | * Does all sorts of transformations and filtering |
50 | */ |
b0ccd3fb |
51 | define('FORMAT_MOODLE', '0'); // Does all sorts of transformations and filtering |
7cf1c7bd |
52 | |
53 | /** |
54 | * Plain HTML (with some tags stripped) |
55 | */ |
b0ccd3fb |
56 | define('FORMAT_HTML', '1'); // Plain HTML (with some tags stripped) |
7cf1c7bd |
57 | |
58 | /** |
59 | * Plain text (even tags are printed in full) |
60 | */ |
b0ccd3fb |
61 | define('FORMAT_PLAIN', '2'); // Plain text (even tags are printed in full) |
7cf1c7bd |
62 | |
63 | /** |
64 | * Wiki-formatted text |
6a6495ff |
65 | * Deprecated: left here just to note that '3' is not used (at the moment) |
66 | * and to catch any latent wiki-like text (which generates an error) |
7cf1c7bd |
67 | */ |
b0ccd3fb |
68 | define('FORMAT_WIKI', '3'); // Wiki-formatted text |
7cf1c7bd |
69 | |
70 | /** |
71 | * Markdown-formatted text http://daringfireball.net/projects/markdown/ |
72 | */ |
b0ccd3fb |
73 | define('FORMAT_MARKDOWN', '4'); // Markdown-formatted text http://daringfireball.net/projects/markdown/ |
0095d5cd |
74 | |
7cf1c7bd |
75 | |
76 | /** |
77 | * Allowed tags - string of html tags that can be tested against for safe html tags |
78 | * @global string $ALLOWED_TAGS |
79 | */ |
39dda0fc |
80 | $ALLOWED_TAGS = |
d046ae55 |
81 | '<p><br><b><i><u><font><table><tbody><span><div><tr><td><th><ol><ul><dl><li><dt><dd><h1><h2><h3><h4><h5><h6><hr><img><a><strong><emphasis><em><sup><sub><address><cite><blockquote><pre><strike><param><acronym><nolink><lang><tex><algebra><math><mi><mn><mo><mtext><mspace><ms><mrow><mfrac><msqrt><mroot><mstyle><merror><mpadded><mphantom><mfenced><msub><msup><msubsup><munder><mover><munderover><mmultiscripts><mtable><mtr><mtd><maligngroup><malignmark><maction><cn><ci><apply><reln><fn><interval><inverse><sep><condition><declare><lambda><compose><ident><quotient><exp><factorial><divide><max><min><minus><plus><power><rem><times><root><gcd><and><or><xor><not><implies><forall><exists><abs><conjugate><eq><neq><gt><lt><geq><leq><ln><log><int><diff><partialdiff><lowlimit><uplimit><bvar><degree><set><list><union><intersect><in><notin><subset><prsubset><notsubset><notprsubset><setdiff><sum><product><limit><tendsto><mean><sdev><variance><median><mode><moment><vector><matrix><matrixrow><determinant><transpose><selector><annotation><semantics><annotation-xml><tt><code>'; |
82 | |
037dcbb6 |
83 | /** |
84 | * Allowed protocols - array of protocols that are safe to use in links and so on |
85 | * @global string $ALLOWED_PROTOCOLS |
86 | */ |
f941df22 |
87 | $ALLOWED_PROTOCOLS = array('http', 'https', 'ftp', 'news', 'mailto', 'rtsp', 'teamspeak', 'gopher', 'mms', |
cd76de7e |
88 | 'color', 'callto', 'cursor', 'text-align', 'font-size', 'font-weight', 'font-style', |
89 | 'border', 'margin', 'padding'); // CSS as well to get through kses |
037dcbb6 |
90 | |
91 | |
0095d5cd |
92 | /// Functions |
93 | |
7cf1c7bd |
94 | /** |
95 | * Add quotes to HTML characters |
96 | * |
97 | * Returns $var with HTML characters (like "<", ">", etc.) properly quoted. |
98 | * This function is very similar to {@link p()} |
99 | * |
100 | * @param string $var the string potentially containing HTML characters |
101 | * @return string |
102 | */ |
3662bce5 |
103 | function s($var) { |
63e554d0 |
104 | if ($var == '0') { // for integer 0, boolean false, string '0' |
105 | return '0'; |
3662bce5 |
106 | } |
67afdb11 |
107 | return preg_replace("/&(#\d+);/i", "&$1;", htmlspecialchars(stripslashes_safe($var))); |
f9903ed0 |
108 | } |
109 | |
7cf1c7bd |
110 | /** |
111 | * Add quotes to HTML characters |
112 | * |
d48b00b4 |
113 | * Prints $var with HTML characters (like "<", ">", etc.) properly quoted. |
7cf1c7bd |
114 | * This function is very similar to {@link s()} |
115 | * |
116 | * @param string $var the string potentially containing HTML characters |
117 | * @return string |
118 | */ |
3662bce5 |
119 | function p($var) { |
df8d3e66 |
120 | echo s($var); |
f9903ed0 |
121 | } |
122 | |
7cf1c7bd |
123 | |
124 | /** |
125 | * Ensure that a variable is set |
126 | * |
772e78be |
127 | * Return $var if it is defined, otherwise return $default, |
7cf1c7bd |
128 | * This function is very similar to {@link optional_variable()} |
129 | * |
130 | * @param mixed $var the variable which may be unset |
131 | * @param mixed $default the value to return if $var is unset |
132 | * @return mixed |
133 | */ |
b0ccd3fb |
134 | function nvl(&$var, $default='') { |
8553b700 |
135 | |
136 | return isset($var) ? $var : $default; |
137 | } |
f9903ed0 |
138 | |
7cf1c7bd |
139 | /** |
140 | * Remove query string from url |
141 | * |
142 | * Takes in a URL and returns it without the querystring portion |
143 | * |
144 | * @param string $url the url which may have a query string attached |
145 | * @return string |
146 | */ |
147 | function strip_querystring($url) { |
f9903ed0 |
148 | |
b9b8ab69 |
149 | if ($commapos = strpos($url, '?')) { |
150 | return substr($url, 0, $commapos); |
151 | } else { |
152 | return $url; |
153 | } |
f9903ed0 |
154 | } |
155 | |
7cf1c7bd |
156 | /** |
157 | * Returns the URL of the HTTP_REFERER, less the querystring portion |
158 | * @return string |
159 | */ |
f9903ed0 |
160 | function get_referer() { |
f9903ed0 |
161 | |
b0ccd3fb |
162 | return strip_querystring(nvl($_SERVER['HTTP_REFERER'])); |
f9903ed0 |
163 | } |
164 | |
c1d57101 |
165 | |
7cf1c7bd |
166 | /** |
167 | * Returns the name of the current script, WITH the querystring portion. |
168 | * this function is necessary because PHP_SELF and REQUEST_URI and SCRIPT_NAME |
169 | * return different things depending on a lot of things like your OS, Web |
170 | * server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.) |
d48b00b4 |
171 | * <b>NOTE:</b> This function returns false if the global variables needed are not set. |
172 | * |
7cf1c7bd |
173 | * @return string |
174 | */ |
175 | function me() { |
f9903ed0 |
176 | |
b0ccd3fb |
177 | if (!empty($_SERVER['REQUEST_URI'])) { |
178 | return $_SERVER['REQUEST_URI']; |
c1d57101 |
179 | |
b0ccd3fb |
180 | } else if (!empty($_SERVER['PHP_SELF'])) { |
181 | if (!empty($_SERVER['QUERY_STRING'])) { |
182 | return $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING']; |
fced815c |
183 | } |
b0ccd3fb |
184 | return $_SERVER['PHP_SELF']; |
c1d57101 |
185 | |
b0ccd3fb |
186 | } else if (!empty($_SERVER['SCRIPT_NAME'])) { |
187 | if (!empty($_SERVER['QUERY_STRING'])) { |
188 | return $_SERVER['SCRIPT_NAME'] .'?'. $_SERVER['QUERY_STRING']; |
fced815c |
189 | } |
b0ccd3fb |
190 | return $_SERVER['SCRIPT_NAME']; |
fced815c |
191 | |
b0ccd3fb |
192 | } else if (!empty($_SERVER['URL'])) { // May help IIS (not well tested) |
193 | if (!empty($_SERVER['QUERY_STRING'])) { |
194 | return $_SERVER['URL'] .'?'. $_SERVER['QUERY_STRING']; |
16c4d82a |
195 | } |
b0ccd3fb |
196 | return $_SERVER['URL']; |
16c4d82a |
197 | |
b9b8ab69 |
198 | } else { |
b0ccd3fb |
199 | notify('Warning: Could not find any of these web server variables: $REQUEST_URI, $PHP_SELF, $SCRIPT_NAME or $URL'); |
bcdfe14e |
200 | return false; |
7fbd6b1c |
201 | } |
f9903ed0 |
202 | } |
203 | |
7cf1c7bd |
204 | /** |
d48b00b4 |
205 | * Like {@link me()} but returns a full URL |
7cf1c7bd |
206 | * @see me() |
7cf1c7bd |
207 | * @return string |
208 | */ |
f9903ed0 |
209 | function qualified_me() { |
f9903ed0 |
210 | |
b6e22603 |
211 | global $CFG; |
212 | |
68796e6b |
213 | if (!empty($CFG->wwwroot)) { |
214 | $url = parse_url($CFG->wwwroot); |
215 | } |
b6e22603 |
216 | |
217 | if (!empty($url['host'])) { |
218 | $hostname = $url['host']; |
219 | } else if (!empty($_SERVER['SERVER_NAME'])) { |
b0ccd3fb |
220 | $hostname = $_SERVER['SERVER_NAME']; |
221 | } else if (!empty($_ENV['SERVER_NAME'])) { |
222 | $hostname = $_ENV['SERVER_NAME']; |
223 | } else if (!empty($_SERVER['HTTP_HOST'])) { |
224 | $hostname = $_SERVER['HTTP_HOST']; |
225 | } else if (!empty($_ENV['HTTP_HOST'])) { |
226 | $hostname = $_ENV['HTTP_HOST']; |
39e018b3 |
227 | } else { |
b0ccd3fb |
228 | notify('Warning: could not find the name of this server!'); |
bcdfe14e |
229 | return false; |
c1d57101 |
230 | } |
dc28eede |
231 | |
232 | if (!empty($url['port'])) { |
233 | $hostname .= ':'.$url['port']; |
234 | } else if (!empty($_SERVER['SERVER_PORT'])) { |
235 | if ($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) { |
236 | $hostname .= ':'.$_SERVER['SERVER_PORT']; |
237 | } |
238 | } |
239 | |
f77c261e |
240 | if (isset($_SERVER['HTTPS'])) { |
241 | $protocol = ($_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://'; |
242 | } else if (isset($_SERVER['SERVER_PORT'])) { # Apache2 does not export $_SERVER['HTTPS'] |
243 | $protocol = ($_SERVER['SERVER_PORT'] == '443') ? 'https://' : 'http://'; |
244 | } else { |
245 | $protocol = 'http://'; |
246 | } |
f9903ed0 |
247 | |
39e018b3 |
248 | $url_prefix = $protocol.$hostname; |
b9b8ab69 |
249 | return $url_prefix . me(); |
f9903ed0 |
250 | } |
251 | |
7cf1c7bd |
252 | /** |
253 | * Determine if a web referer is valid |
254 | * |
255 | * Returns true if the referer is the same as the goodreferer. If |
256 | * the referer to test is not specified, use {@link qualified_me()}. |
257 | * If the admin has not set secure forms ($CFG->secureforms) then |
258 | * this function returns true regardless of a match. |
d48b00b4 |
259 | * |
7cf1c7bd |
260 | * @uses $CFG |
261 | * @param string $goodreferer the url to compare to referer |
262 | * @return boolean |
263 | */ |
b0ccd3fb |
264 | function match_referer($goodreferer = '') { |
60f18531 |
265 | global $CFG; |
266 | |
ae384ef1 |
267 | if (empty($CFG->secureforms)) { // Don't bother checking referer |
60f18531 |
268 | return true; |
269 | } |
f9903ed0 |
270 | |
b0ccd3fb |
271 | if ($goodreferer == 'nomatch') { // Don't bother checking referer |
a0deb5db |
272 | return true; |
273 | } |
274 | |
ab9f24ad |
275 | if (empty($goodreferer)) { |
276 | $goodreferer = qualified_me(); |
c1d57101 |
277 | } |
67b1b6c2 |
278 | |
9d54c2fb |
279 | $referer = get_referer(); |
280 | |
ad489e94 |
281 | return (($referer == $goodreferer) or ($referer == $CFG->wwwroot .'/') or ($referer == $CFG->wwwroot .'/index.php')); |
f9903ed0 |
282 | } |
283 | |
7cf1c7bd |
284 | /** |
285 | * Determine if there is data waiting to be processed from a form |
286 | * |
287 | * Used on most forms in Moodle to check for data |
288 | * Returns the data as an object, if it's found. |
289 | * This object can be used in foreach loops without |
290 | * casting because it's cast to (array) automatically |
772e78be |
291 | * |
7cf1c7bd |
292 | * Checks that submitted POST data exists, and also |
293 | * checks the referer against the given url (it uses |
d48b00b4 |
294 | * the current page if none was specified. |
295 | * |
7cf1c7bd |
296 | * @uses $CFG |
297 | * @param string $url the url to compare to referer for secure forms |
298 | * @return boolean |
299 | */ |
b0ccd3fb |
300 | function data_submitted($url='') { |
d48b00b4 |
301 | |
36b4f985 |
302 | |
37208cd2 |
303 | global $CFG; |
304 | |
607809b3 |
305 | if (empty($_POST)) { |
36b4f985 |
306 | return false; |
607809b3 |
307 | |
36b4f985 |
308 | } else { |
309 | if (match_referer($url)) { |
607809b3 |
310 | return (object)$_POST; |
36b4f985 |
311 | } else { |
312 | if ($CFG->debug > 10) { |
b0ccd3fb |
313 | notice('The form did not come from this page! (referer = '. get_referer() .')'); |
36b4f985 |
314 | } |
315 | return false; |
316 | } |
317 | } |
318 | } |
319 | |
7cf1c7bd |
320 | /** |
321 | * Moodle replacement for php stripslashes() function |
322 | * |
772e78be |
323 | * The standard php stripslashes() removes ALL backslashes |
7cf1c7bd |
324 | * even from strings - so C:\temp becomes C:temp - this isn't good. |
325 | * This function should work as a fairly safe replacement |
326 | * to be called on quoted AND unquoted strings (to be sure) |
d48b00b4 |
327 | * |
7cf1c7bd |
328 | * @param string the string to remove unsafe slashes from |
329 | * @return string |
330 | */ |
7d8f674d |
331 | function stripslashes_safe($string) { |
7d8f674d |
332 | |
333 | $string = str_replace("\\'", "'", $string); |
334 | $string = str_replace('\\"', '"', $string); |
b403c32a |
335 | $string = str_replace('\\\\', '\\', $string); |
7d8f674d |
336 | return $string; |
337 | } |
f9903ed0 |
338 | |
9b4b78fd |
339 | /** |
340 | * Recursive implementation of stripslashes() |
341 | * |
342 | * This function will allow you to strip the slashes from a variable. |
343 | * If the variable is an array or object, slashes will be stripped |
344 | * from the items (or properties) it contains, even if they are arrays |
345 | * or objects themselves. |
346 | * |
347 | * @param mixed the variable to remove slashes from |
348 | * @return mixed |
349 | */ |
350 | function stripslashes_recursive($var) { |
351 | if(is_object($var)) { |
352 | $properties = get_object_vars($var); |
353 | foreach($properties as $property => $value) { |
354 | $var->$property = stripslashes_recursive($value); |
355 | } |
356 | } |
357 | else if(is_array($var)) { |
358 | foreach($var as $property => $value) { |
359 | $var[$property] = stripslashes_recursive($value); |
360 | } |
361 | } |
362 | else if(is_string($var)) { |
363 | $var = stripslashes($var); |
364 | } |
365 | return $var; |
366 | } |
367 | |
7cf1c7bd |
368 | /** |
d48b00b4 |
369 | * Given some normal text this function will break up any |
370 | * long words to a given size by inserting the given character |
371 | * |
6aaa17c7 |
372 | * It's multibyte savvy and doesn't change anything inside html tags. |
373 | * |
7cf1c7bd |
374 | * @param string $string the string to be modified |
89dcb99d |
375 | * @param int $maxsize maximum length of the string to be returned |
7cf1c7bd |
376 | * @param string $cutchar the string used to represent word breaks |
377 | * @return string |
378 | */ |
4a5644e5 |
379 | function break_up_long_words($string, $maxsize=20, $cutchar=' ') { |
a2b3f884 |
380 | |
6aaa17c7 |
381 | /// Loading the textlib singleton instance. We are going to need it. |
382 | $textlib = textlib_get_instance(); |
8f7dc7f1 |
383 | |
6aaa17c7 |
384 | /// First of all, save all the tags inside the text to skip them |
385 | $tags = array(); |
386 | filter_save_tags($string,$tags); |
5b07d990 |
387 | |
6aaa17c7 |
388 | /// Process the string adding the cut when necessary |
4a5644e5 |
389 | $output = ''; |
6aaa17c7 |
390 | $length = $textlib->strlen($string, current_charset()); |
4a5644e5 |
391 | $wordlength = 0; |
392 | |
393 | for ($i=0; $i<$length; $i++) { |
6aaa17c7 |
394 | $char = $textlib->substr($string, $i, 1, current_charset()); |
395 | if ($char == ' ' or $char == "\t" or $char == "\n" or $char == "\r" or $char == "<" or $char == ">") { |
4a5644e5 |
396 | $wordlength = 0; |
397 | } else { |
398 | $wordlength++; |
399 | if ($wordlength > $maxsize) { |
400 | $output .= $cutchar; |
401 | $wordlength = 0; |
402 | } |
403 | } |
404 | $output .= $char; |
405 | } |
6aaa17c7 |
406 | |
407 | /// Finally load the tags back again |
408 | if (!empty($tags)) { |
409 | $output = str_replace(array_keys($tags), $tags, $output); |
410 | } |
411 | |
4a5644e5 |
412 | return $output; |
413 | } |
414 | |
7cf1c7bd |
415 | /** |
416 | * This does a search and replace, ignoring case |
417 | * This function is only used for versions of PHP older than version 5 |
418 | * which do not have a native version of this function. |
419 | * Taken from the PHP manual, by bradhuizenga @ softhome.net |
d48b00b4 |
420 | * |
7cf1c7bd |
421 | * @param string $find the string to search for |
422 | * @param string $replace the string to replace $find with |
423 | * @param string $string the string to search through |
424 | * return string |
425 | */ |
ce57cc79 |
426 | if (!function_exists('str_ireplace')) { /// Only exists in PHP 5 |
7ec2fc00 |
427 | function str_ireplace($find, $replace, $string) { |
7ec2fc00 |
428 | |
429 | if (!is_array($find)) { |
430 | $find = array($find); |
431 | } |
432 | |
433 | if(!is_array($replace)) { |
434 | if (!is_array($find)) { |
435 | $replace = array($replace); |
436 | } else { |
437 | // this will duplicate the string into an array the size of $find |
438 | $c = count($find); |
439 | $rString = $replace; |
440 | unset($replace); |
441 | for ($i = 0; $i < $c; $i++) { |
442 | $replace[$i] = $rString; |
443 | } |
444 | } |
445 | } |
446 | |
447 | foreach ($find as $fKey => $fItem) { |
448 | $between = explode(strtolower($fItem),strtolower($string)); |
449 | $pos = 0; |
450 | foreach($between as $bKey => $bItem) { |
451 | $between[$bKey] = substr($string,$pos,strlen($bItem)); |
452 | $pos += strlen($bItem) + strlen($fItem); |
453 | } |
454 | $string = implode($replace[$fKey],$between); |
72e4eac6 |
455 | } |
7ec2fc00 |
456 | return ($string); |
3fe3851d |
457 | } |
3fe3851d |
458 | } |
459 | |
7cf1c7bd |
460 | /** |
461 | * Locate the position of a string in another string |
462 | * |
463 | * This function is only used for versions of PHP older than version 5 |
464 | * which do not have a native version of this function. |
465 | * Taken from the PHP manual, by dmarsh @ spscc.ctc.edu |
d48b00b4 |
466 | * |
7cf1c7bd |
467 | * @param string $haystack The string to be searched |
468 | * @param string $needle The string to search for |
89dcb99d |
469 | * @param int $offset The position in $haystack where the search should begin. |
7cf1c7bd |
470 | */ |
ce57cc79 |
471 | if (!function_exists('stripos')) { /// Only exists in PHP 5 |
472 | function stripos($haystack, $needle, $offset=0) { |
d48b00b4 |
473 | |
ce57cc79 |
474 | return strpos(strtoupper($haystack), strtoupper($needle), $offset); |
475 | } |
476 | } |
477 | |
7cf1c7bd |
478 | /** |
479 | * Load a template from file |
480 | * |
481 | * Returns a (big) string containing the contents of a template file with all |
482 | * the variables interpolated. all the variables must be in the $var[] array or |
483 | * object (whatever you decide to use). |
484 | * |
485 | * <b>WARNING: do not use this on big files!!</b> |
d48b00b4 |
486 | * |
7cf1c7bd |
487 | * @param string $filename Location on the server's filesystem where template can be found. |
488 | * @param mixed $var Passed in by reference. An array or object which will be loaded with data from the template file. |
489 | */ |
f9903ed0 |
490 | function read_template($filename, &$var) { |
f9903ed0 |
491 | |
b0ccd3fb |
492 | $temp = str_replace("\\", "\\\\", implode(file($filename), '')); |
b9b8ab69 |
493 | $temp = str_replace('"', '\"', $temp); |
494 | eval("\$template = \"$temp\";"); |
495 | return $template; |
f9903ed0 |
496 | } |
497 | |
7cf1c7bd |
498 | /** |
499 | * Set a variable's value depending on whether or not it already has a value. |
500 | * |
772e78be |
501 | * If variable is set, set it to the set_value otherwise set it to the |
7cf1c7bd |
502 | * unset_value. used to handle checkboxes when you are expecting them from |
503 | * a form |
d48b00b4 |
504 | * |
7cf1c7bd |
505 | * @param mixed $var Passed in by reference. The variable to check. |
506 | * @param mixed $set_value The value to set $var to if $var already has a value. |
507 | * @param mixed $unset_value The value to set $var to if $var does not already have a value. |
508 | */ |
f9903ed0 |
509 | function checked(&$var, $set_value = 1, $unset_value = 0) { |
f9903ed0 |
510 | |
b9b8ab69 |
511 | if (empty($var)) { |
512 | $var = $unset_value; |
513 | } else { |
514 | $var = $set_value; |
515 | } |
f9903ed0 |
516 | } |
517 | |
7cf1c7bd |
518 | /** |
519 | * Prints the word "checked" if a variable is true, otherwise prints nothing, |
d48b00b4 |
520 | * used for printing the word "checked" in a checkbox form element. |
521 | * |
7cf1c7bd |
522 | * @param boolean $var Variable to be checked for true value |
523 | * @param string $true_value Value to be printed if $var is true |
524 | * @param string $false_value Value to be printed if $var is false |
525 | */ |
b0ccd3fb |
526 | function frmchecked(&$var, $true_value = 'checked', $false_value = '') { |
f9903ed0 |
527 | |
b9b8ab69 |
528 | if ($var) { |
529 | echo $true_value; |
530 | } else { |
531 | echo $false_value; |
532 | } |
f9903ed0 |
533 | } |
534 | |
7cf1c7bd |
535 | /** |
536 | * This function will create a HTML link that will work on both |
537 | * Javascript and non-javascript browsers. |
538 | * Relies on the Javascript function openpopup in javascript.php |
d48b00b4 |
539 | * |
7cf1c7bd |
540 | * $url must be relative to home page eg /mod/survey/stuff.php |
541 | * @param string $url Web link relative to home page |
542 | * @param string $name Name to be assigned to the popup window |
543 | * @param string $linkname Text to be displayed as web link |
89dcb99d |
544 | * @param int $height Height to assign to popup window |
545 | * @param int $width Height to assign to popup window |
7cf1c7bd |
546 | * @param string $title Text to be displayed as popup page title |
547 | * @param string $options List of additional options for popup window |
548 | * @todo Add code examples and list of some options that might be used. |
549 | * @param boolean $return Should the link to the popup window be returned as a string (true) or printed immediately (false)? |
550 | * @return string |
551 | * @uses $CFG |
552 | */ |
b0ccd3fb |
553 | function link_to_popup_window ($url, $name='popup', $linkname='click here', |
554 | $height=400, $width=500, $title='Popup window', $options='none', $return=false) { |
f9903ed0 |
555 | |
ff80e012 |
556 | global $CFG; |
557 | |
b0ccd3fb |
558 | if ($options == 'none') { |
559 | $options = 'menubar=0,location=0,scrollbars,resizable,width='. $width .',height='. $height; |
b48f834c |
560 | } |
86aa7ccf |
561 | $fullscreen = 0; |
f9903ed0 |
562 | |
c80b7585 |
563 | if (!(strpos($url,$CFG->wwwroot) === false)) { // some log url entries contain _SERVER[HTTP_REFERRER] in which case wwwroot is already there. |
7cf1c7bd |
564 | $url = substr($url, strlen($CFG->wwwroot)+1); |
c80b7585 |
565 | } |
566 | |
b0ccd3fb |
567 | $link = '<a target="'. $name .'" title="'. $title .'" href="'. $CFG->wwwroot . $url .'" '. |
9de39eae |
568 | "onclick=\"return openpopup('$url', '$name', '$options', $fullscreen);\">$linkname</a>"; |
1f2eec7b |
569 | if ($return) { |
570 | return $link; |
571 | } else { |
572 | echo $link; |
573 | } |
f9903ed0 |
574 | } |
575 | |
7cf1c7bd |
576 | /** |
577 | * This function will print a button submit form element |
578 | * that will work on both Javascript and non-javascript browsers. |
579 | * Relies on the Javascript function openpopup in javascript.php |
d48b00b4 |
580 | * |
7cf1c7bd |
581 | * $url must be relative to home page eg /mod/survey/stuff.php |
582 | * @param string $url Web link relative to home page |
583 | * @param string $name Name to be assigned to the popup window |
584 | * @param string $linkname Text to be displayed as web link |
89dcb99d |
585 | * @param int $height Height to assign to popup window |
586 | * @param int $width Height to assign to popup window |
7cf1c7bd |
587 | * @param string $title Text to be displayed as popup page title |
588 | * @param string $options List of additional options for popup window |
dc28eede |
589 | * @param string $return If true, return as a string, otherwise print |
7cf1c7bd |
590 | * @return string |
591 | * @uses $CFG |
592 | */ |
b0ccd3fb |
593 | function button_to_popup_window ($url, $name='popup', $linkname='click here', |
572fe9ab |
594 | $height=400, $width=500, $title='Popup window', $options='none', $return=false, |
0f7d4e5e |
595 | $id='', $class='') { |
52f1b496 |
596 | |
597 | global $CFG; |
598 | |
b0ccd3fb |
599 | if ($options == 'none') { |
600 | $options = 'menubar=0,location=0,scrollbars,resizable,width='. $width .',height='. $height; |
52f1b496 |
601 | } |
0f7d4e5e |
602 | |
603 | if ($id) { |
604 | $id = ' id="'.$id.'" '; |
605 | } |
606 | if ($class) { |
607 | $class = ' class="'.$class.'" '; |
608 | } |
52f1b496 |
609 | $fullscreen = 0; |
610 | |
0f7d4e5e |
611 | $button = '<input type="button" name="'.$name.'" title="'. $title .'" value="'. $linkname .' ..." '.$id.$class. |
dc28eede |
612 | "onclick=\"return openpopup('$url', '$name', '$options', $fullscreen);\" />\n"; |
613 | if ($return) { |
614 | return $button; |
615 | } else { |
616 | echo $button; |
617 | } |
52f1b496 |
618 | } |
619 | |
620 | |
7cf1c7bd |
621 | /** |
622 | * Prints a simple button to close a window |
623 | */ |
08396bb2 |
624 | function close_window_button($name='closewindow') { |
c1d57101 |
625 | |
b0ccd3fb |
626 | echo '<center>' . "\n"; |
627 | echo '<script type="text/javascript">' . "\n"; |
628 | echo '<!--' . "\n"; |
86aa7ccf |
629 | echo "document.write('<form>');\n"; |
d7d21c38 |
630 | echo "document.write('<input type=\"button\" onclick=\"self.close();\" value=\"".get_string("closewindow")."\" />');\n"; |
c01f71d4 |
631 | echo "document.write('<\/form>');\n"; |
b0ccd3fb |
632 | echo '-->' . "\n"; |
633 | echo '</script>' . "\n"; |
634 | echo '<noscript>' . "\n"; |
08396bb2 |
635 | print_string($name); |
b0ccd3fb |
636 | echo '</noscript>' . "\n"; |
637 | echo '</center>' . "\n"; |
f9903ed0 |
638 | } |
639 | |
08396bb2 |
640 | /* |
641 | * Try and close the current window immediately using Javascript |
642 | */ |
643 | function close_window($delay=0) { |
644 | echo '<script language="JavaScript" type="text/javascript">'."\n"; |
645 | echo '<!--'."\n"; |
646 | if ($delay) { |
647 | sleep($delay); |
648 | } |
649 | echo 'self.close();'."\n"; |
650 | echo '-->'."\n"; |
651 | echo '</script>'."\n"; |
652 | exit; |
653 | } |
654 | |
655 | |
d48b00b4 |
656 | /** |
657 | * Given an array of value, creates a popup menu to be part of a form |
658 | * $options["value"]["label"] |
659 | * |
660 | * @param type description |
661 | * @todo Finish documenting this function |
662 | */ |
25e5dbf9 |
663 | function choose_from_menu ($options, $name, $selected='', $nothing='choose', $script='', |
664 | $nothingvalue='0', $return=false, $disabled=false, $tabindex=0) { |
ab9f24ad |
665 | |
b0ccd3fb |
666 | if ($nothing == 'choose') { |
667 | $nothing = get_string('choose') .'...'; |
618b22c5 |
668 | } |
669 | |
48e535bc |
670 | $attributes = ($script) ? 'onchange="'. $script .'"' : ''; |
671 | if ($disabled) { |
672 | $attributes .= ' disabled="disabled"'; |
f9903ed0 |
673 | } |
9c9f7d77 |
674 | |
25e5dbf9 |
675 | if ($tabindex) { |
676 | $attributes .= ' tabindex="'.$tabindex.'"'; |
677 | } |
678 | |
dc28eede |
679 | $output = '<select id="menu'.$name.'" name="'. $name .'" '. $attributes .'>' . "\n"; |
bda8d43a |
680 | if ($nothing) { |
b0ccd3fb |
681 | $output .= ' <option value="'. $nothingvalue .'"'. "\n"; |
cec0a0fc |
682 | if ($nothingvalue === $selected) { |
b0ccd3fb |
683 | $output .= ' selected="selected"'; |
bda8d43a |
684 | } |
b0ccd3fb |
685 | $output .= '>'. $nothing .'</option>' . "\n"; |
873960de |
686 | } |
607809b3 |
687 | if (!empty($options)) { |
688 | foreach ($options as $value => $label) { |
b0ccd3fb |
689 | $output .= ' <option value="'. $value .'"'; |
bd905b45 |
690 | if ((string)$value == (string)$selected) { |
b0ccd3fb |
691 | $output .= ' selected="selected"'; |
607809b3 |
692 | } |
b0ccd3fb |
693 | if ($label === '') { |
694 | $output .= '>'. $value .'</option>' . "\n"; |
a20c1090 |
695 | } else { |
b0ccd3fb |
696 | $output .= '>'. $label .'</option>' . "\n"; |
607809b3 |
697 | } |
f9903ed0 |
698 | } |
699 | } |
b0ccd3fb |
700 | $output .= '</select>' . "\n"; |
08056730 |
701 | |
702 | if ($return) { |
703 | return $output; |
704 | } else { |
705 | echo $output; |
706 | } |
ab9f24ad |
707 | } |
f9903ed0 |
708 | |
14040797 |
709 | /** |
710 | * Just like choose_from_menu, but takes a nested array (2 levels) and makes a dropdown menu |
711 | * including option headings with the first level. |
712 | */ |
713 | function choose_from_menu_nested($options,$name,$selected='',$nothing='choose',$script = '', |
714 | $nothingvalue=0,$return=false,$disabled=false,$tabindex=0) { |
715 | |
716 | if ($nothing == 'choose') { |
717 | $nothing = get_string('choose') .'...'; |
718 | } |
719 | |
720 | $attributes = ($script) ? 'onchange="'. $script .'"' : ''; |
721 | if ($disabled) { |
722 | $attributes .= ' disabled="disabled"'; |
723 | } |
724 | |
725 | if ($tabindex) { |
726 | $attributes .= ' tabindex="'.$tabindex.'"'; |
727 | } |
728 | |
729 | $output = '<select id="menu'.$name.'" name="'. $name .'" '. $attributes .'>' . "\n"; |
730 | if ($nothing) { |
731 | $output .= ' <option value="'. $nothingvalue .'"'. "\n"; |
732 | if ($nothingvalue === $selected) { |
733 | $output .= ' selected="selected"'; |
734 | } |
735 | $output .= '>'. $nothing .'</option>' . "\n"; |
736 | } |
737 | if (!empty($options)) { |
738 | foreach ($options as $section => $values) { |
739 | $output .= ' <optgroup label="'.$section.'">'."\n"; |
740 | foreach ($values as $value => $label) { |
741 | $output .= ' <option value="'. $value .'"'; |
f332bd02 |
742 | if ((string)$value == (string)$selected) { |
14040797 |
743 | $output .= ' selected="selected"'; |
744 | } |
745 | if ($label === '') { |
746 | $output .= '>'. $value .'</option>' . "\n"; |
747 | } else { |
748 | $output .= '>'. $label .'</option>' . "\n"; |
749 | } |
750 | } |
751 | $output .= ' </optgroup>'."\n"; |
752 | } |
753 | } |
754 | $output .= '</select>' . "\n"; |
755 | |
756 | if ($return) { |
757 | return $output; |
758 | } else { |
759 | echo $output; |
760 | } |
761 | } |
762 | |
763 | |
531a3cb2 |
764 | /** |
765 | * Given an array of values, creates a group of radio buttons to be part of a form |
766 | * |
767 | * @param array $options An array of value-label pairs for the radio group (values as keys) |
768 | * @param string $name Name of the radiogroup (unique in the form) |
769 | * @param string $checked The value that is already checked |
770 | */ |
771 | function choose_from_radio ($options, $name, $checked='') { |
772 | |
20cbef63 |
773 | static $idcounter = 0; |
774 | |
531a3cb2 |
775 | if (!$name) { |
776 | $name = 'unnamed'; |
777 | } |
778 | |
779 | $output = '<span class="radiogroup '.$name."\">\n"; |
780 | |
781 | if (!empty($options)) { |
782 | $currentradio = 0; |
783 | foreach ($options as $value => $label) { |
20cbef63 |
784 | $htmlid = 'auto-rb'.sprintf('%04d', ++$idcounter); |
785 | $output .= ' <span class="radioelement '.$name.' rb'.$currentradio."\">"; |
786 | $output .= '<input name="'.$name.'" id="'.$htmlid.'" type="radio" value="'.$value.'"'; |
531a3cb2 |
787 | if ($value == $checked) { |
788 | $output .= ' checked="checked"'; |
789 | } |
790 | if ($label === '') { |
20cbef63 |
791 | $output .= ' /> <label for="'.$htmlid.'">'. $value .'</label></span>' . "\n"; |
531a3cb2 |
792 | } else { |
20cbef63 |
793 | $output .= ' /> <label for="'.$htmlid.'">'. $label .'</label></span>' . "\n"; |
531a3cb2 |
794 | } |
795 | $currentradio = ($currentradio + 1) % 2; |
796 | } |
797 | } |
798 | |
799 | $output .= '</span>' . "\n"; |
800 | |
801 | echo $output; |
802 | } |
803 | |
2481037f |
804 | /** Display an standard html checkbox with an optional label |
805 | * |
806 | * @param string $name The name of the checkbox |
807 | * @param string $value The valus that the checkbox will pass when checked |
808 | * @param boolean $checked The flag to tell the checkbox initial state |
809 | * @param string $label The label to be showed near the checkbox |
810 | * @param string $alt The info to be inserted in the alt tag |
811 | */ |
d0d272e7 |
812 | function print_checkbox ($name, $value, $checked = true, $label = '', $alt = '', $script='',$return=false) { |
2481037f |
813 | |
20cbef63 |
814 | static $idcounter = 0; |
815 | |
2481037f |
816 | if (!$name) { |
817 | $name = 'unnamed'; |
818 | } |
819 | |
820 | if (!$alt) { |
821 | $alt = 'checkbox'; |
822 | } |
823 | |
824 | if ($checked) { |
825 | $strchecked = ' checked="checked"'; |
826 | } |
827 | |
20cbef63 |
828 | $htmlid = 'auto-cb'.sprintf('%04d', ++$idcounter); |
2481037f |
829 | $output = '<span class="checkbox '.$name."\">"; |
d0d272e7 |
830 | $output .= '<input name="'.$name.'" id="'.$htmlid.'" type="checkbox" value="'.$value.'" alt="'.$alt.'"'.$strchecked.' '.((!empty($script)) ? ' onClick="'.$script.'" ' : '').' />'; |
20cbef63 |
831 | if(!empty($label)) { |
832 | $output .= ' <label for="'.$htmlid.'">'.$label.'</label>'; |
833 | } |
2481037f |
834 | $output .= '</span>'."\n"; |
835 | |
d0d272e7 |
836 | if (empty($return)) { |
837 | echo $output; |
838 | } else { |
839 | return $output; |
840 | } |
2481037f |
841 | |
842 | } |
843 | |
d0d272e7 |
844 | /** Display an standard html text field with an optional label |
845 | * |
846 | * @param string $name The name of the text field |
847 | * @param string $value The value of the text field |
848 | * @param string $label The label to be showed near the text field |
849 | * @param string $alt The info to be inserted in the alt tag |
850 | */ |
851 | function print_textfield ($name, $value, $alt = '',$size=50,$maxlength= 0,$return=false) { |
852 | |
853 | static $idcounter = 0; |
854 | |
855 | if (empty($name)) { |
856 | $name = 'unnamed'; |
857 | } |
858 | |
859 | if (empty($alt)) { |
860 | $alt = 'textfield'; |
861 | } |
862 | |
863 | if (!empty($maxlength)) { |
864 | $maxlength = ' maxlength="'.$maxlength.'" '; |
865 | } |
866 | |
ce432524 |
867 | $htmlid = 'auto-tf'.sprintf('%04d', ++$idcounter); |
d0d272e7 |
868 | $output = '<span class="textfield '.$name."\">"; |
869 | $output .= '<input name="'.$name.'" id="'.$htmlid.'" type="text" value="'.$value.'" size="'.$size.'" '.$maxlength.' alt="'.$alt.'" />'; |
870 | |
871 | $output .= '</span>'."\n"; |
872 | |
873 | if (empty($return)) { |
874 | echo $output; |
875 | } else { |
876 | return $output; |
877 | } |
878 | |
879 | } |
880 | |
881 | |
d48b00b4 |
882 | /** |
883 | * Implements a complete little popup form |
884 | * |
89dcb99d |
885 | * @uses $CFG |
886 | * @param string $common The URL up to the point of the variable that changes |
887 | * @param array $options Alist of value-label pairs for the popup list |
888 | * @param string $formname Name must be unique on the page |
889 | * @param string $selected The option that is already selected |
890 | * @param string $nothing The label for the "no choice" option |
891 | * @param string $help The name of a help page if help is required |
892 | * @param string $helptext The name of the label for the help button |
893 | * @param boolean $return Indicates whether the function should return the text |
894 | * as a string or echo it directly to the page being rendered |
772e78be |
895 | * @param string $targetwindow The name of the target page to open the linked page in. |
89dcb99d |
896 | * @return string If $return is true then the entire form is returned as a string. |
897 | * @todo Finish documenting this function<br> |
89dcb99d |
898 | */ |
899 | function popup_form($common, $options, $formname, $selected='', $nothing='choose', $help='', $helptext='', $return=false, $targetwindow='self') { |
900 | |
772e78be |
901 | global $CFG; |
b0542a1e |
902 | static $go, $choose; /// Locally cached, in case there's lots on a page |
87180677 |
903 | |
6f9f3b69 |
904 | if (empty($options)) { |
905 | return ''; |
906 | } |
0d0baabf |
907 | |
b0542a1e |
908 | if (!isset($go)) { |
909 | $go = get_string('go'); |
037dcbb6 |
910 | } |
911 | |
b0ccd3fb |
912 | if ($nothing == 'choose') { |
037dcbb6 |
913 | if (!isset($choose)) { |
914 | $choose = get_string('choose'); |
915 | } |
916 | $nothing = $choose.'...'; |
618b22c5 |
917 | } |
918 | |
037dcbb6 |
919 | $startoutput = '<form action="'.$CFG->wwwroot.'/course/jumpto.php"'. |
920 | ' method="get"'. |
921 | ' target="'.$CFG->framename.'"'. |
922 | ' name="'.$formname.'"'. |
c4d951e1 |
923 | ' class="popupform">'; |
037dcbb6 |
924 | |
925 | $output = '<select name="jump" onchange="'.$targetwindow.'.location=document.'.$formname. |
16570a27 |
926 | '.jump.options[document.'.$formname.'.jump.selectedIndex].value;">'."\n"; |
f9903ed0 |
927 | |
b0ccd3fb |
928 | if ($nothing != '') { |
dfec7b01 |
929 | $output .= " <option value=\"javascript:void(0)\">$nothing</option>\n"; |
f9903ed0 |
930 | } |
931 | |
72b4e283 |
932 | $inoptgroup = false; |
f9903ed0 |
933 | foreach ($options as $value => $label) { |
772e78be |
934 | |
2a003a90 |
935 | if (substr($label,0,2) == '--') { /// we are starting a new optgroup |
772e78be |
936 | |
2a003a90 |
937 | /// Check to see if we already have a valid open optgroup |
938 | /// XHTML demands that there be at least 1 option within an optgroup |
939 | if ($inoptgroup and (count($optgr) > 1) ) { |
940 | $output .= implode('', $optgr); |
72b4e283 |
941 | $output .= ' </optgroup>'; |
72b4e283 |
942 | } |
2a003a90 |
943 | |
944 | unset($optgr); |
945 | $optgr = array(); |
946 | |
947 | $optgr[] = ' <optgroup label="'. substr($label,2) .'">'; // Plain labels |
772e78be |
948 | |
2a003a90 |
949 | $inoptgroup = true; /// everything following will be in an optgroup |
3326450b |
950 | continue; |
772e78be |
951 | |
d897cae4 |
952 | } else { |
fd78420b |
953 | if (!empty($CFG->usesid) && !isset($_COOKIE[session_name()])) |
954 | { |
955 | $url=sid_process_url( $common . $value ); |
956 | } else |
957 | { |
958 | $url=$common . $value; |
959 | } |
960 | $optstr = ' <option value="' . $url . '"'; |
772e78be |
961 | |
d897cae4 |
962 | if ($value == $selected) { |
2a003a90 |
963 | $optstr .= ' selected="selected"'; |
964 | } |
772e78be |
965 | |
2a003a90 |
966 | if ($label) { |
967 | $optstr .= '>'. $label .'</option>' . "\n"; |
968 | } else { |
969 | $optstr .= '>'. $value .'</option>' . "\n"; |
970 | } |
772e78be |
971 | |
2a003a90 |
972 | if ($inoptgroup) { |
973 | $optgr[] = $optstr; |
974 | } else { |
975 | $output .= $optstr; |
d897cae4 |
976 | } |
f9903ed0 |
977 | } |
772e78be |
978 | |
f9903ed0 |
979 | } |
2a003a90 |
980 | |
981 | /// catch the final group if not closed |
982 | if ($inoptgroup and count($optgr) > 1) { |
983 | $output .= implode('', $optgr); |
72b4e283 |
984 | $output .= ' </optgroup>'; |
985 | } |
2a003a90 |
986 | |
b0ccd3fb |
987 | $output .= '</select>'; |
037dcbb6 |
988 | $output .= '<noscript id="noscript'.$formname.'" style="display: inline;">'; |
b0542a1e |
989 | $output .= '<input type="submit" value="'.$go.'" /></noscript>'; |
037dcbb6 |
990 | $output .= '<script type="text/javascript">'. |
991 | "\n<!--\n". |
992 | 'document.getElementById("noscript'.$formname.'").style.display = "none";'. |
993 | "\n-->\n".'</script>'; |
b0ccd3fb |
994 | $output .= '</form>' . "\n"; |
d897cae4 |
995 | |
1f2eec7b |
996 | if ($help) { |
997 | $button = helpbutton($help, $helptext, 'moodle', true, false, '', true); |
998 | } else { |
999 | $button = ''; |
1000 | } |
1001 | |
d897cae4 |
1002 | if ($return) { |
1f2eec7b |
1003 | return $startoutput.$button.$output; |
d897cae4 |
1004 | } else { |
1f2eec7b |
1005 | echo $startoutput.$button.$output; |
d897cae4 |
1006 | } |
f9903ed0 |
1007 | } |
1008 | |
1009 | |
d48b00b4 |
1010 | /** |
1011 | * Prints some red text |
1012 | * |
1013 | * @param string $error The text to be displayed in red |
1014 | */ |
f9903ed0 |
1015 | function formerr($error) { |
d48b00b4 |
1016 | |
f9903ed0 |
1017 | if (!empty($error)) { |
b0ccd3fb |
1018 | echo '<font color="#ff0000">'. $error .'</font>'; |
f9903ed0 |
1019 | } |
1020 | } |
1021 | |
d48b00b4 |
1022 | /** |
1023 | * Validates an email to make sure it makes sense. |
1024 | * |
1025 | * @param string $address The email address to validate. |
1026 | * @return boolean |
1027 | */ |
89dcb99d |
1028 | function validate_email($address) { |
d48b00b4 |
1029 | |
f9903ed0 |
1030 | return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'. |
1031 | '@'. |
1032 | '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'. |
1033 | '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', |
1034 | $address)); |
1035 | } |
1036 | |
690f358b |
1037 | /** |
1038 | * Extracts file argument either from file parameter or PATH_INFO |
1039 | * |
1040 | * @param string $scriptname name of the calling script |
1041 | * @return string file path (only safe characters) |
1042 | */ |
1043 | function get_file_argument($scriptname) { |
1044 | global $_SERVER; |
1045 | |
1046 | $relativepath = FALSE; |
1047 | |
1048 | // first try normal parameter (compatible method == no relative links!) |
1049 | $relativepath = optional_param('file', FALSE, PARAM_PATH); |
48283ff6 |
1050 | if ($relativepath === '/testslasharguments') { |
1051 | echo 'test -1: Incorrect use - try "file.php/testslasharguments" instead'; //indicate fopen/fread works for health center |
1052 | die; |
1053 | } |
690f358b |
1054 | |
1055 | // then try extract file from PATH_INFO (slasharguments method) |
1056 | if (!$relativepath and !empty($_SERVER['PATH_INFO'])) { |
1057 | $path_info = $_SERVER['PATH_INFO']; |
1058 | // check that PATH_INFO works == must not contain the script name |
1059 | if (!strpos($path_info, $scriptname)) { |
1060 | $relativepath = clean_param(rawurldecode($path_info), PARAM_PATH); |
48283ff6 |
1061 | if ($relativepath === '/testslasharguments') { |
1062 | echo 'test 1: Slasharguments test passed.'; //indicate ok for health center |
690f358b |
1063 | die; |
1064 | } |
1065 | } |
1066 | } |
1067 | |
1068 | // now if both fail try the old way |
1069 | // (for compatibility with misconfigured or older buggy php implementations) |
1070 | if (!$relativepath) { |
1071 | $arr = explode($scriptname, me()); |
1072 | if (!empty($arr[1])) { |
1073 | $path_info = strip_querystring($arr[1]); |
1074 | $relativepath = clean_param(rawurldecode($path_info), PARAM_PATH); |
48283ff6 |
1075 | if ($relativepath === '/testslasharguments') { |
1076 | echo 'test 2:Slasharguments test passed (compatibility hack).'; //indicate ok for health center |
690f358b |
1077 | die; |
1078 | } |
1079 | } |
1080 | } |
1081 | |
1082 | return $relativepath; |
1083 | } |
1084 | |
d48b00b4 |
1085 | /** |
1086 | * Check for bad characters ? |
1087 | * |
1088 | * @param string $string ? |
89dcb99d |
1089 | * @param int $allowdots ? |
1090 | * @todo Finish documenting this function - more detail needed in description as well as details on arguments |
d48b00b4 |
1091 | */ |
80035a89 |
1092 | function detect_munged_arguments($string, $allowdots=1) { |
1093 | if (substr_count($string, '..') > $allowdots) { // Sometimes we allow dots in references |
6c8e8b5e |
1094 | return true; |
1095 | } |
393c9b4f |
1096 | if (ereg('[\|\`]', $string)) { // check for other bad characters |
6c8e8b5e |
1097 | return true; |
1098 | } |
f038d9a3 |
1099 | if (empty($string) or $string == '/') { |
1100 | return true; |
1101 | } |
1102 | |
6c8e8b5e |
1103 | return false; |
1104 | } |
1105 | |
d48b00b4 |
1106 | /** |
1107 | * Searches the current environment variables for some slash arguments |
1108 | * |
1109 | * @param string $file ? |
1110 | * @todo Finish documenting this function |
1111 | */ |
b0ccd3fb |
1112 | function get_slash_arguments($file='file.php') { |
f9903ed0 |
1113 | |
eaa50dbc |
1114 | if (!$string = me()) { |
f9903ed0 |
1115 | return false; |
1116 | } |
eaa50dbc |
1117 | |
6ed3da1d |
1118 | $pathinfo = explode($file, $string); |
ab9f24ad |
1119 | |
bcdfe14e |
1120 | if (!empty($pathinfo[1])) { |
9b74055f |
1121 | return addslashes($pathinfo[1]); |
6ed3da1d |
1122 | } else { |
1123 | return false; |
1124 | } |
1125 | } |
1126 | |
d48b00b4 |
1127 | /** |
1128 | * Extracts arguments from "/foo/bar/something" |
1129 | * eg http://mysite.com/script.php/foo/bar/something |
1130 | * |
89dcb99d |
1131 | * @param string $string ? |
1132 | * @param int $i ? |
1133 | * @return array|string |
d48b00b4 |
1134 | * @todo Finish documenting this function |
1135 | */ |
6ed3da1d |
1136 | function parse_slash_arguments($string, $i=0) { |
f9903ed0 |
1137 | |
6c8e8b5e |
1138 | if (detect_munged_arguments($string)) { |
780db230 |
1139 | return false; |
1140 | } |
b0ccd3fb |
1141 | $args = explode('/', $string); |
f9903ed0 |
1142 | |
1143 | if ($i) { // return just the required argument |
1144 | return $args[$i]; |
1145 | |
1146 | } else { // return the whole array |
1147 | array_shift($args); // get rid of the empty first one |
1148 | return $args; |
1149 | } |
1150 | } |
1151 | |
d48b00b4 |
1152 | /** |
89dcb99d |
1153 | * Just returns an array of text formats suitable for a popup menu |
d48b00b4 |
1154 | * |
89dcb99d |
1155 | * @uses FORMAT_MOODLE |
1156 | * @uses FORMAT_HTML |
1157 | * @uses FORMAT_PLAIN |
89dcb99d |
1158 | * @uses FORMAT_MARKDOWN |
1159 | * @return array |
d48b00b4 |
1160 | */ |
0095d5cd |
1161 | function format_text_menu() { |
d48b00b4 |
1162 | |
b0ccd3fb |
1163 | return array (FORMAT_MOODLE => get_string('formattext'), |
1164 | FORMAT_HTML => get_string('formathtml'), |
1165 | FORMAT_PLAIN => get_string('formatplain'), |
b0ccd3fb |
1166 | FORMAT_MARKDOWN => get_string('formatmarkdown')); |
0095d5cd |
1167 | } |
1168 | |
d48b00b4 |
1169 | /** |
1170 | * Given text in a variety of format codings, this function returns |
772e78be |
1171 | * the text as safe HTML. |
d48b00b4 |
1172 | * |
1173 | * @uses $CFG |
89dcb99d |
1174 | * @uses FORMAT_MOODLE |
1175 | * @uses FORMAT_HTML |
1176 | * @uses FORMAT_PLAIN |
1177 | * @uses FORMAT_WIKI |
1178 | * @uses FORMAT_MARKDOWN |
1179 | * @param string $text The text to be formatted. This is raw text originally from user input. |
772e78be |
1180 | * @param int $format Identifier of the text format to be used |
89dcb99d |
1181 | * (FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN, FORMAT_WIKI, FORMAT_MARKDOWN) |
1182 | * @param array $options ? |
1183 | * @param int $courseid ? |
1184 | * @return string |
d48b00b4 |
1185 | * @todo Finish documenting this function |
1186 | */ |
c4ae4fa1 |
1187 | function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL ) { |
0095d5cd |
1188 | |
ab9f24ad |
1189 | global $CFG, $course; |
c4ae4fa1 |
1190 | |
e7a47153 |
1191 | if (!isset($options->noclean)) { |
1192 | $options->noclean=false; |
1193 | } |
1194 | if (!isset($options->smiley)) { |
1195 | $options->smiley=true; |
1196 | } |
1197 | if (!isset($options->filter)) { |
1198 | $options->filter=true; |
1199 | } |
1200 | if (!isset($options->para)) { |
1201 | $options->para=true; |
1202 | } |
1203 | if (!isset($options->newlines)) { |
1204 | $options->newlines=true; |
f0aa2fed |
1205 | } |
1206 | |
c4ae4fa1 |
1207 | if (empty($courseid)) { |
8eaa4c61 |
1208 | if (!empty($course->id)) { // An ugly hack for better compatibility |
c4ae4fa1 |
1209 | $courseid = $course->id; |
1210 | } |
1211 | } |
a751a4e5 |
1212 | |
e7a47153 |
1213 | if (!empty($CFG->cachetext)) { |
1214 | $time = time() - $CFG->cachetext; |
1215 | $md5key = md5($text.'-'.$courseid.$options->noclean.$options->smiley.$options->filter.$options->para.$options->newlines); |
1216 | if ($cacheitem = get_record_select('cache_text', "md5key = '$md5key' AND timemodified > '$time'")) { |
1217 | return $cacheitem->formattedtext; |
1218 | } |
1219 | } |
1220 | |
8eaa4c61 |
1221 | $CFG->currenttextiscacheable = true; // Default status - can be changed by any filter |
e746e4dd |
1222 | |
0095d5cd |
1223 | switch ($format) { |
73f8658c |
1224 | case FORMAT_HTML: |
e7a47153 |
1225 | if (!empty($options->smiley)) { |
1226 | replace_smilies($text); |
1227 | } |
1228 | if (empty($options->noclean)) { |
9d40806d |
1229 | $text = clean_text($text, $format); |
1230 | } |
e7a47153 |
1231 | if (!empty($options->filter)) { |
1232 | $text = filter_text($text, $courseid); |
1233 | } |
73f8658c |
1234 | break; |
1235 | |
6901fa79 |
1236 | case FORMAT_PLAIN: |
e7a47153 |
1237 | $text = s($text); |
ab892a4f |
1238 | $text = rebuildnolinktag($text); |
b0ccd3fb |
1239 | $text = str_replace(' ', ' ', $text); |
6901fa79 |
1240 | $text = nl2br($text); |
6901fa79 |
1241 | break; |
1242 | |
d342c763 |
1243 | case FORMAT_WIKI: |
6a6495ff |
1244 | // this format is deprecated |
572fe9ab |
1245 | $text = '<p>NOTICE: Wiki-like formatting has been removed from Moodle. You should not be seeing |
1246 | this message as all texts should have been converted to Markdown format instead. |
ce50cc70 |
1247 | Please post a bug report to http://moodle.org/bugs with information about where you |
e7a47153 |
1248 | saw this message.</p>'.s($text); |
d342c763 |
1249 | break; |
1250 | |
e7cdcd18 |
1251 | case FORMAT_MARKDOWN: |
1252 | $text = markdown_to_html($text); |
e7a47153 |
1253 | if (!empty($options->smiley)) { |
1254 | replace_smilies($text); |
1255 | } |
1256 | if (empty($options->noclean)) { |
9d40806d |
1257 | $text = clean_text($text, $format); |
1258 | } |
e746e4dd |
1259 | |
e7a47153 |
1260 | if (!empty($options->filter)) { |
1261 | $text = filter_text($text, $courseid); |
1262 | } |
e7cdcd18 |
1263 | break; |
1264 | |
73f8658c |
1265 | default: // FORMAT_MOODLE or anything else |
b7a3d3b2 |
1266 | $text = text_to_html($text, $options->smiley, $options->para, $options->newlines); |
e7a47153 |
1267 | if (empty($options->noclean)) { |
9d40806d |
1268 | $text = clean_text($text, $format); |
1269 | } |
e746e4dd |
1270 | |
e7a47153 |
1271 | if (!empty($options->filter)) { |
1272 | $text = filter_text($text, $courseid); |
1273 | } |
0095d5cd |
1274 | break; |
0095d5cd |
1275 | } |
f0aa2fed |
1276 | |
8eaa4c61 |
1277 | if (!empty($CFG->cachetext) and $CFG->currenttextiscacheable) { |
f0aa2fed |
1278 | $newrecord->md5key = $md5key; |
1279 | $newrecord->formattedtext = addslashes($text); |
1280 | $newrecord->timemodified = time(); |
9d40806d |
1281 | @insert_record('cache_text', $newrecord); |
f0aa2fed |
1282 | } |
1283 | |
1284 | return $text; |
0095d5cd |
1285 | } |
1286 | |
4a28b5ca |
1287 | /** Converts the text format from the value to the 'internal' |
1288 | * name or vice versa. $key can either be the value or the name |
1289 | * and you get the other back. |
1290 | * |
1291 | * @param mixed int 0-4 or string one of 'moodle','html','plain','markdown' |
1292 | * @return mixed as above but the other way around! |
1293 | */ |
1294 | function text_format_name( $key ) { |
1295 | $lookup = array(); |
1296 | $lookup[FORMAT_MOODLE] = 'moodle'; |
1297 | $lookup[FORMAT_HTML] = 'html'; |
1298 | $lookup[FORMAT_PLAIN] = 'plain'; |
1299 | $lookup[FORMAT_MARKDOWN] = 'markdown'; |
1300 | $value = "error"; |
1301 | if (!is_numeric($key)) { |
1302 | $key = strtolower( $key ); |
1303 | $value = array_search( $key, $lookup ); |
1304 | } |
1305 | else { |
1306 | if (isset( $lookup[$key] )) { |
1307 | $value = $lookup[ $key ]; |
1308 | } |
1309 | } |
1310 | return $value; |
1311 | } |
1312 | |
7b2c5e72 |
1313 | /** Given a simple string, this function returns the string |
1314 | * processed by enabled filters if $CFG->filterall is enabled |
1315 | * |
3e6691ee |
1316 | * @param string $string The string to be filtered. |
1317 | * @param boolean $striplinks To strip any link in the result text. |
1318 | * @param int $courseid Current course as filters can, potentially, use it |
7b2c5e72 |
1319 | * @return string |
1320 | */ |
3e6691ee |
1321 | function format_string ($string, $striplinks = false, $courseid=NULL ) { |
7b2c5e72 |
1322 | |
1323 | global $CFG, $course; |
1324 | |
2a3affe9 |
1325 | //We'll use a in-memory cache here to speed up repeated strings |
1326 | static $strcache; |
1327 | |
1328 | //Calculate md5 |
1c58f440 |
1329 | $md5 = md5($string.'<+>'.$striplinks); |
2a3affe9 |
1330 | |
1331 | //Fetch from cache if possible |
1332 | if(isset($strcache[$md5])) { |
1333 | return $strcache[$md5]; |
1334 | } |
1335 | |
7b2c5e72 |
1336 | if (empty($courseid)) { |
1337 | if (!empty($course->id)) { // An ugly hack for better compatibility |
1338 | $courseid = $course->id; // (copied from format_text) |
1339 | } |
1340 | } |
1341 | |
45daee10 |
1342 | if (!empty($CFG->filterall)) { |
7b2c5e72 |
1343 | $string = filter_text($string, $courseid); |
1344 | } |
1345 | |
3e6691ee |
1346 | if ($striplinks) { //strip links in string |
1347 | $string = preg_replace('/(<a[^>]+?>)(.+?)(<\/a>)/is','$2',$string); |
1348 | } |
1349 | |
2a3affe9 |
1350 | //Store to cache |
1351 | $strcache[$md5] = $string; |
1352 | |
7b2c5e72 |
1353 | return $string; |
1354 | } |
1355 | |
d48b00b4 |
1356 | /** |
1357 | * Given text in a variety of format codings, this function returns |
1358 | * the text as plain text suitable for plain email. |
d48b00b4 |
1359 | * |
89dcb99d |
1360 | * @uses FORMAT_MOODLE |
1361 | * @uses FORMAT_HTML |
1362 | * @uses FORMAT_PLAIN |
1363 | * @uses FORMAT_WIKI |
1364 | * @uses FORMAT_MARKDOWN |
1365 | * @param string $text The text to be formatted. This is raw text originally from user input. |
772e78be |
1366 | * @param int $format Identifier of the text format to be used |
89dcb99d |
1367 | * (FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN, FORMAT_WIKI, FORMAT_MARKDOWN) |
1368 | * @return string |
d48b00b4 |
1369 | */ |
d342c763 |
1370 | function format_text_email($text, $format) { |
d342c763 |
1371 | |
1372 | switch ($format) { |
1373 | |
1374 | case FORMAT_PLAIN: |
1375 | return $text; |
1376 | break; |
1377 | |
1378 | case FORMAT_WIKI: |
1379 | $text = wiki_to_html($text); |
5b472756 |
1380 | /// This expression turns links into something nice in a text format. (Russell Jungwirth) |
1381 | /// From: http://php.net/manual/en/function.eregi-replace.php and simplified |
76add072 |
1382 | $text = eregi_replace('(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)','\\3 [ \\2 ]', $text); |
7c55a29b |
1383 | return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES))); |
d342c763 |
1384 | break; |
1385 | |
6ff45b59 |
1386 | case FORMAT_HTML: |
1387 | return html_to_text($text); |
1388 | break; |
1389 | |
e7cdcd18 |
1390 | case FORMAT_MOODLE: |
1391 | case FORMAT_MARKDOWN: |
67ccec43 |
1392 | default: |
76add072 |
1393 | $text = eregi_replace('(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)','\\3 [ \\2 ]', $text); |
7c55a29b |
1394 | return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES))); |
d342c763 |
1395 | break; |
1396 | } |
1397 | } |
0095d5cd |
1398 | |
d48b00b4 |
1399 | /** |
1400 | * Given some text in HTML format, this function will pass it |
1401 | * through any filters that have been defined in $CFG->textfilterx |
1402 | * The variable defines a filepath to a file containing the |
1403 | * filter function. The file must contain a variable called |
1404 | * $textfilter_function which contains the name of the function |
1405 | * with $courseid and $text parameters |
1406 | * |
89dcb99d |
1407 | * @param string $text The text to be passed through format filters |
1408 | * @param int $courseid ? |
d48b00b4 |
1409 | * @return string |
1410 | * @todo Finish documenting this function |
1411 | */ |
c4ae4fa1 |
1412 | function filter_text($text, $courseid=NULL) { |
e67b9e31 |
1413 | |
c4ae4fa1 |
1414 | global $CFG; |
e67b9e31 |
1415 | |
01af49bb |
1416 | require_once($CFG->libdir.'/filterlib.php'); |
d523d2ea |
1417 | if (!empty($CFG->textfilters)) { |
1418 | $textfilters = explode(',', $CFG->textfilters); |
1419 | foreach ($textfilters as $textfilter) { |
b0ccd3fb |
1420 | if (is_readable($CFG->dirroot .'/'. $textfilter .'/filter.php')) { |
1421 | include_once($CFG->dirroot .'/'. $textfilter .'/filter.php'); |
df1c4611 |
1422 | $functionname = basename($textfilter).'_filter'; |
1423 | if (function_exists($functionname)) { |
1424 | $text = $functionname($courseid, $text); |
1425 | } |
d523d2ea |
1426 | } |
e67b9e31 |
1427 | } |
1428 | } |
d523d2ea |
1429 | |
c7444a36 |
1430 | /// <nolink> tags removed for XHTML compatibility |
1431 | $text = str_replace('<nolink>', '', $text); |
1432 | $text = str_replace('</nolink>', '', $text); |
1433 | |
e67b9e31 |
1434 | return $text; |
1435 | } |
1436 | |
d48b00b4 |
1437 | /** |
1438 | * Given raw text (eg typed in by a user), this function cleans it up |
1439 | * and removes any nasty tags that could mess up Moodle pages. |
1440 | * |
89dcb99d |
1441 | * @uses FORMAT_MOODLE |
1442 | * @uses FORMAT_PLAIN |
1443 | * @uses ALLOWED_TAGS |
1444 | * @param string $text The text to be cleaned |
772e78be |
1445 | * @param int $format Identifier of the text format to be used |
89dcb99d |
1446 | * (FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN, FORMAT_WIKI, FORMAT_MARKDOWN) |
1447 | * @return string The cleaned up text |
d48b00b4 |
1448 | */ |
3da47524 |
1449 | function clean_text($text, $format=FORMAT_MOODLE) { |
b7a3cf49 |
1450 | |
fc120758 |
1451 | global $ALLOWED_TAGS; |
3fe3851d |
1452 | |
ab9f24ad |
1453 | switch ($format) { |
e7cdcd18 |
1454 | case FORMAT_PLAIN: |
1455 | return $text; |
1456 | |
1457 | default: |
1458 | |
09cbeb40 |
1459 | /// Remove tags that are not allowed |
3fe3851d |
1460 | $text = strip_tags($text, $ALLOWED_TAGS); |
e7cdcd18 |
1461 | |
7789ffbf |
1462 | /// Clean up embedded scripts and , using kses |
1463 | $text = cleanAttributes($text); |
1464 | |
5b472756 |
1465 | /// Remove script events |
ab9f24ad |
1466 | $text = eregi_replace("([^a-z])language([[:space:]]*)=", "\\1Xlanguage=", $text); |
1467 | $text = eregi_replace("([^a-z])on([a-z]+)([[:space:]]*)=", "\\1Xon\\2=", $text); |
6901fa79 |
1468 | |
6901fa79 |
1469 | return $text; |
0095d5cd |
1470 | } |
b7a3cf49 |
1471 | } |
f9903ed0 |
1472 | |
d48b00b4 |
1473 | /** |
89dcb99d |
1474 | * This function takes a string and examines it for HTML tags. |
d48b00b4 |
1475 | * If tags are detected it passes the string to a helper function {@link cleanAttributes2()} |
1476 | * which checks for attributes and filters them for malicious content |
1477 | * 17/08/2004 :: Eamon DOT Costello AT dcu DOT ie |
1478 | * |
1479 | * @param string $str The string to be examined for html tags |
1480 | * @return string |
1481 | */ |
3bd7ffec |
1482 | function cleanAttributes($str){ |
4e8f2e6b |
1483 | $result = preg_replace_callback( |
1484 | '%(<[^>]*(>|$)|>)%m', #search for html tags |
1485 | "cleanAttributes2", |
3bd7ffec |
1486 | $str |
67ccec43 |
1487 | ); |
3bd7ffec |
1488 | return $result; |
67ccec43 |
1489 | } |
1490 | |
d48b00b4 |
1491 | /** |
1492 | * This function takes a string with an html tag and strips out any unallowed |
1493 | * protocols e.g. javascript: |
1494 | * It calls ancillary functions in kses which are prefixed by kses |
1495 | * 17/08/2004 :: Eamon DOT Costello AT dcu DOT ie |
1496 | * |
4e8f2e6b |
1497 | * @param array $htmlArray An array from {@link cleanAttributes()}, containing in its 1st |
1498 | * element the html to be cleared |
d48b00b4 |
1499 | * @return string |
1500 | */ |
4e8f2e6b |
1501 | function cleanAttributes2($htmlArray){ |
3bd7ffec |
1502 | |
037dcbb6 |
1503 | global $CFG, $ALLOWED_PROTOCOLS; |
b0ccd3fb |
1504 | require_once($CFG->libdir .'/kses.php'); |
3bd7ffec |
1505 | |
4e8f2e6b |
1506 | $htmlTag = $htmlArray[1]; |
037dcbb6 |
1507 | if (substr($htmlTag, 0, 1) != '<') { |
3bd7ffec |
1508 | return '>'; //a single character ">" detected |
1509 | } |
037dcbb6 |
1510 | if (!preg_match('%^<\s*(/\s*)?([a-zA-Z0-9]+)([^>]*)>?$%', $htmlTag, $matches)) { |
67ccec43 |
1511 | return ''; // It's seriously malformed |
1512 | } |
3bd7ffec |
1513 | $slash = trim($matches[1]); //trailing xhtml slash |
67ccec43 |
1514 | $elem = $matches[2]; //the element name |
3bd7ffec |
1515 | $attrlist = $matches[3]; // the list of attributes as a string |
1516 | |
037dcbb6 |
1517 | $attrArray = kses_hair($attrlist, $ALLOWED_PROTOCOLS); |
3bd7ffec |
1518 | |
67ccec43 |
1519 | $attStr = ''; |
037dcbb6 |
1520 | foreach ($attrArray as $arreach) { |
ec51c7fb |
1521 | $attStr .= ' '.strtolower($arreach['name']).'="'.$arreach['value'].'" '; |
3bd7ffec |
1522 | } |
713126cd |
1523 | |
1524 | // Remove last space from attribute list |
1525 | $attStr = rtrim($attStr); |
1526 | |
3bd7ffec |
1527 | $xhtml_slash = ''; |
037dcbb6 |
1528 | if (preg_match('%/\s*$%', $attrlist)) { |
67ccec43 |
1529 | $xhtml_slash = ' /'; |
3bd7ffec |
1530 | } |
b0ccd3fb |
1531 | return '<'. $slash . $elem . $attStr . $xhtml_slash .'>'; |
3bd7ffec |
1532 | } |
1533 | |
d48b00b4 |
1534 | /** |
1535 | * Replaces all known smileys in the text with image equivalents |
1536 | * |
1537 | * @uses $CFG |
1538 | * @param string $text Passed by reference. The string to search for smily strings. |
1539 | * @return string |
1540 | */ |
5f350e8f |
1541 | function replace_smilies(&$text) { |
772e78be |
1542 | /// |
2ea9027b |
1543 | global $CFG; |
c1d57101 |
1544 | |
5b472756 |
1545 | /// this builds the mapping array only once |
617778f2 |
1546 | static $runonce = false; |
69081931 |
1547 | static $e = array(); |
1548 | static $img = array(); |
617778f2 |
1549 | static $emoticons = array( |
fbfc2675 |
1550 | ':-)' => 'smiley', |
1551 | ':)' => 'smiley', |
1552 | ':-D' => 'biggrin', |
1553 | ';-)' => 'wink', |
1554 | ':-/' => 'mixed', |
1555 | 'V-.' => 'thoughtful', |
1556 | ':-P' => 'tongueout', |
1557 | 'B-)' => 'cool', |
1558 | '^-)' => 'approve', |
1559 | '8-)' => 'wideeyes', |
1560 | ':o)' => 'clown', |
1561 | ':-(' => 'sad', |
1562 | ':(' => 'sad', |
1563 | '8-.' => 'shy', |
1564 | ':-I' => 'blush', |
1565 | ':-X' => 'kiss', |
1566 | '8-o' => 'surprise', |
1567 | 'P-|' => 'blackeye', |
1568 | '8-[' => 'angry', |
1569 | 'xx-P' => 'dead', |
1570 | '|-.' => 'sleepy', |
1571 | '}-]' => 'evil', |
2ea9027b |
1572 | ); |
1573 | |
fbfc2675 |
1574 | if ($runonce == false) { /// After the first time this is not run again |
617778f2 |
1575 | foreach ($emoticons as $emoticon => $image){ |
fbfc2675 |
1576 | $alttext = get_string($image, 'pix'); |
1577 | |
69081931 |
1578 | $e[] = $emoticon; |
d48b00b4 |
1579 | $img[] = '<img alt="'. $alttext .'" width="15" height="15" src="'. $CFG->pixpath .'/s/'. $image .'.gif" />'; |
617778f2 |
1580 | } |
1581 | $runonce = true; |
c0f728ba |
1582 | } |
b7a3cf49 |
1583 | |
8dcd43f3 |
1584 | // Exclude from transformations all the code inside <script> tags |
1585 | // Needed to solve Bug 1185. Thanks to jouse 2001 detecting it. :-) |
1586 | // Based on code from glossary fiter by Williams Castillo. |
1587 | // - Eloy |
1588 | |
1589 | // Detect all the <script> zones to take out |
1590 | $excludes = array(); |
1591 | preg_match_all('/<script language(.+?)<\/script>/is',$text,$list_of_excludes); |
1592 | |
1593 | // Take out all the <script> zones from text |
1594 | foreach (array_unique($list_of_excludes[0]) as $key=>$value) { |
1595 | $excludes['<+'.$key.'+>'] = $value; |
1596 | } |
1597 | if ($excludes) { |
1598 | $text = str_replace($excludes,array_keys($excludes),$text); |
1599 | } |
1600 | |
fbfc2675 |
1601 | /// this is the meat of the code - this is run every time |
5f350e8f |
1602 | $text = str_replace($e, $img, $text); |
8dcd43f3 |
1603 | |
1604 | // Recover all the <script> zones to text |
1605 | if ($excludes) { |
1606 | $text = str_replace(array_keys($excludes),$excludes,$text); |
1607 | } |
1a072208 |
1608 | } |
0095d5cd |
1609 | |
89dcb99d |
1610 | /** |
1611 | * Given plain text, makes it into HTML as nicely as possible. |
1612 | * May contain HTML tags already |
1613 | * |
1614 | * @uses $CFG |
1615 | * @param string $text The string to convert. |
1616 | * @param boolean $smiley Convert any smiley characters to smiley images? |
1617 | * @param boolean $para If true then the returned string will be wrapped in paragraph tags |
1618 | * @param boolean $newlines If true then lines newline breaks will be converted to HTML newline breaks. |
1619 | * @return string |
1620 | */ |
1621 | |
b7a3d3b2 |
1622 | function text_to_html($text, $smiley=true, $para=true, $newlines=true) { |
772e78be |
1623 | /// |
f9903ed0 |
1624 | |
27326a3e |
1625 | global $CFG; |
1626 | |
c1d57101 |
1627 | /// Remove any whitespace that may be between HTML tags |
7b3be1b1 |
1628 | $text = eregi_replace(">([[:space:]]+)<", "><", $text); |
1629 | |
c1d57101 |
1630 | /// Remove any returns that precede or follow HTML tags |
0eae8049 |
1631 | $text = eregi_replace("([\n\r])<", " <", $text); |
1632 | $text = eregi_replace(">([\n\r])", "> ", $text); |
7b3be1b1 |
1633 | |
5f350e8f |
1634 | convert_urls_into_links($text); |
f9903ed0 |
1635 | |
c1d57101 |
1636 | /// Make returns into HTML newlines. |
b7a3d3b2 |
1637 | if ($newlines) { |
1638 | $text = nl2br($text); |
1639 | } |
f9903ed0 |
1640 | |
c1d57101 |
1641 | /// Turn smileys into images. |
d69cb7f4 |
1642 | if ($smiley) { |
5f350e8f |
1643 | replace_smilies($text); |
d69cb7f4 |
1644 | } |
f9903ed0 |
1645 | |
c1d57101 |
1646 | /// Wrap the whole thing in a paragraph tag if required |
909f539d |
1647 | if ($para) { |
b0ccd3fb |
1648 | return '<p>'.$text.'</p>'; |
909f539d |
1649 | } else { |
1650 | return $text; |
1651 | } |
f9903ed0 |
1652 | } |
1653 | |
d48b00b4 |
1654 | /** |
1655 | * Given Markdown formatted text, make it into XHTML using external function |
1656 | * |
89dcb99d |
1657 | * @uses $CFG |
1658 | * @param string $text The markdown formatted text to be converted. |
1659 | * @return string Converted text |
d48b00b4 |
1660 | */ |
e7cdcd18 |
1661 | function markdown_to_html($text) { |
e7cdcd18 |
1662 | global $CFG; |
1663 | |
b0ccd3fb |
1664 | require_once($CFG->libdir .'/markdown.php'); |
e7cdcd18 |
1665 | |
1666 | return Markdown($text); |
1667 | } |
1668 | |
d48b00b4 |
1669 | /** |
89dcb99d |
1670 | * Given HTML text, make it into plain text using external function |
d48b00b4 |
1671 | * |
1672 | * @uses $CFG |
1673 | * @param string $html The text to be converted. |
1674 | * @return string |
1675 | */ |
6ff45b59 |
1676 | function html_to_text($html) { |
89dcb99d |
1677 | |
428aaa29 |
1678 | global $CFG; |
6ff45b59 |
1679 | |
b0ccd3fb |
1680 | require_once($CFG->libdir .'/html2text.php'); |
6ff45b59 |
1681 | |
1682 | return html2text($html); |
1683 | } |
1684 | |
d48b00b4 |
1685 | /** |
1686 | * Given some text this function converts any URLs it finds into HTML links |
1687 | * |
1688 | * @param string $text Passed in by reference. The string to be searched for urls. |
1689 | */ |
5f350e8f |
1690 | function convert_urls_into_links(&$text) { |
5f350e8f |
1691 | /// Make lone URLs into links. eg http://moodle.com/ |
3405b212 |
1692 | $text = eregi_replace("([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", |
bc01a2b8 |
1693 | "\\1<a href=\"\\2://\\3\\4\" target=\"_blank\">\\2://\\3\\4</a>", $text); |
5f350e8f |
1694 | |
1695 | /// eg www.moodle.com |
ab9f24ad |
1696 | $text = eregi_replace("([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])", |
bc01a2b8 |
1697 | "\\1<a href=\"http://www.\\2\\3\" target=\"_blank\">www.\\2\\3</a>", $text); |
5f350e8f |
1698 | } |
1699 | |
d48b00b4 |
1700 | /** |
1701 | * This function will highlight search words in a given string |
1702 | * It cares about HTML and will not ruin links. It's best to use |
1703 | * this function after performing any conversions to HTML. |
1704 | * Function found here: http://forums.devshed.com/t67822/scdaa2d1c3d4bacb4671d075ad41f0854.html |
1705 | * |
89dcb99d |
1706 | * @param string $needle The string to search for |
1707 | * @param string $haystack The string to search for $needle in |
1708 | * @param int $case ? |
1709 | * @return string |
d48b00b4 |
1710 | * @todo Finish documenting this function |
1711 | */ |
ab9f24ad |
1712 | function highlight($needle, $haystack, $case=0, |
b0ccd3fb |
1713 | $left_string='<span class="highlight">', $right_string='</span>') { |
69d51d3a |
1714 | if (empty($needle)) { |
1715 | return $haystack; |
1716 | } |
1717 | |
5eecb8cb |
1718 | //$list_of_words = eregi_replace("[^-a-zA-Z0-9&.']", " ", $needle); // bug 3101 |
1719 | $list_of_words = $needle; |
b0ccd3fb |
1720 | $list_array = explode(' ', $list_of_words); |
88438a58 |
1721 | for ($i=0; $i<sizeof($list_array); $i++) { |
1722 | if (strlen($list_array[$i]) == 1) { |
b0ccd3fb |
1723 | $list_array[$i] = ''; |
88438a58 |
1724 | } |
1725 | } |
b0ccd3fb |
1726 | $list_of_words = implode(' ', $list_array); |
88438a58 |
1727 | $list_of_words_cp = $list_of_words; |
1728 | $final = array(); |
1729 | preg_match_all('/<(.+?)>/is',$haystack,$list_of_words); |
1730 | |
1731 | foreach (array_unique($list_of_words[0]) as $key=>$value) { |
1732 | $final['<|'.$key.'|>'] = $value; |
1733 | } |
1734 | |
1735 | $haystack = str_replace($final,array_keys($final),$haystack); |
b0ccd3fb |
1736 | $list_of_words_cp = eregi_replace(' +', '|', $list_of_words_cp); |
88438a58 |
1737 | |
b0ccd3fb |
1738 | if ($list_of_words_cp{0}=='|') { |
1739 | $list_of_words_cp{0} = ''; |
88438a58 |
1740 | } |
b0ccd3fb |
1741 | if ($list_of_words_cp{strlen($list_of_words_cp)-1}=='|') { |
1742 | $list_of_words_cp{strlen($list_of_words_cp)-1}=''; |
88438a58 |
1743 | } |
88438a58 |
1744 | |
9ccdcd97 |
1745 | $list_of_words_cp = trim($list_of_words_cp); |
1746 | |
1747 | if ($list_of_words_cp) { |
1748 | |
1749 | $list_of_words_cp = "(". $list_of_words_cp .")"; |
1750 | |
1751 | if (!$case){ |
1752 | $haystack = eregi_replace("$list_of_words_cp", "$left_string"."\\1"."$right_string", $haystack); |
1753 | } else { |
1754 | $haystack = ereg_replace("$list_of_words_cp", "$left_string"."\\1"."$right_string", $haystack); |
1755 | } |
88438a58 |
1756 | } |
1757 | $haystack = str_replace(array_keys($final),$final,$haystack); |
1758 | |
f60e7cfe |
1759 | return $haystack; |
88438a58 |
1760 | } |
1761 | |
d48b00b4 |
1762 | /** |
1763 | * This function will highlight instances of $needle in $haystack |
1764 | * It's faster that the above function and doesn't care about |
1765 | * HTML or anything. |
1766 | * |
1767 | * @param string $needle The string to search for |
1768 | * @param string $haystack The string to search for $needle in |
1769 | * @return string |
1770 | */ |
88438a58 |
1771 | function highlightfast($needle, $haystack) { |
5af78ed2 |
1772 | |
1773 | $parts = explode(strtolower($needle), strtolower($haystack)); |
1774 | |
1775 | $pos = 0; |
1776 | |
1777 | foreach ($parts as $key => $part) { |
1778 | $parts[$key] = substr($haystack, $pos, strlen($part)); |
1779 | $pos += strlen($part); |
1780 | |
b0ccd3fb |
1781 | $parts[$key] .= '<span class="highlight">'.substr($haystack, $pos, strlen($needle)).'</span>'; |
5af78ed2 |
1782 | $pos += strlen($needle); |
ab9f24ad |
1783 | } |
5af78ed2 |
1784 | |
1785 | return (join('', $parts)); |
1786 | } |
1787 | |
f9903ed0 |
1788 | |
9fa49e22 |
1789 | /// STANDARD WEB PAGE PARTS /////////////////////////////////////////////////// |
1790 | |
d48b00b4 |
1791 | /** |
1792 | * Print a standard header |
1793 | * |
89dcb99d |
1794 | * @uses $USER |
1795 | * @uses $CFG |
89dcb99d |
1796 | * @uses $SESSION |
1797 | * @param string $title Appears at the top of the window |
1798 | * @param string $heading Appears at the top of the page |
1799 | * @param string $navigation Premade navigation string (for use as breadcrumbs links) |
1800 | * @param string $focus Indicates form element to get cursor focus on load eg inputform.password |
1801 | * @param string $meta Meta tags to be added to the header |
1802 | * @param boolean $cache Should this page be cacheable? |
1803 | * @param string $button HTML code for a button (usually for module editing) |
1804 | * @param string $menu HTML code for a popup menu |
1805 | * @param boolean $usexml use XML for this page |
1806 | * @param string $bodytags This text will be included verbatim in the <body> tag (useful for onload() etc) |
d48b00b4 |
1807 | */ |
b0ccd3fb |
1808 | function print_header ($title='', $heading='', $navigation='', $focus='', $meta='', |
1809 | $cache=true, $button=' ', $menu='', $usexml=false, $bodytags='') { |
63f3cbbd |
1810 | |
f78b1dad |
1811 | global $USER, $CFG, $THEME, $SESSION, $ME, $SITE, $HTTPSPAGEREQUIRED; |
2deebecd |
1812 | |
e89fb61e |
1813 | /// This is an ugly hack to be replaced later by a proper global $COURSE |
1814 | global $course; |
b3153e4b |
1815 | if (!empty($course->lang)) { |
1816 | $CFG->courselang = $course->lang; |
1817 | } |
32e2b302 |
1818 | if (!empty($course->theme)) { |
1819 | if (!empty($CFG->allowcoursethemes)) { |
1820 | $CFG->coursetheme = $course->theme; |
1821 | theme_setup(); |
1822 | } |
1823 | } |
b3153e4b |
1824 | |
f78b1dad |
1825 | /// We have to change some URLs in styles if we are in a $HTTPSPAGEREQUIRED page |
1826 | if (!empty($HTTPSPAGEREQUIRED)) { |
1827 | $CFG->themewww = str_replace('http', 'https', $CFG->themewww); |
1828 | $CFG->pixpath = str_replace('http', 'https', $CFG->pixpath); |
1829 | $CFG->modpixpath = str_replace('http', 'https', $CFG->modpixpath); |
1830 | foreach ($CFG->stylesheets as $key => $stylesheet) { |
1831 | $CFG->stylesheets[$key] = str_replace('http', 'https', $stylesheet); |
1832 | } |
1833 | } |
0d741155 |
1834 | |
c3f55692 |
1835 | /// Add the required stylesheets |
d74d4f20 |
1836 | $stylesheetshtml = ''; |
1837 | foreach ($CFG->stylesheets as $stylesheet) { |
1838 | $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n"; |
c3f55692 |
1839 | } |
d74d4f20 |
1840 | $meta = $stylesheetshtml.$meta; |
e89fb61e |
1841 | |
9fa49e22 |
1842 | |
b0ccd3fb |
1843 | if ($navigation == 'home') { |
9fa49e22 |
1844 | $home = true; |
b0ccd3fb |
1845 | $navigation = ''; |
9d378732 |
1846 | } else { |
1847 | $home = false; |
9fa49e22 |
1848 | } |
1849 | |
0d741155 |
1850 | /// This is another ugly hack to make navigation elements available to print_footer later |
1851 | $THEME->title = $title; |
1852 | $THEME->heading = $heading; |
1853 | $THEME->navigation = $navigation; |
1854 | $THEME->button = $button; |
1855 | $THEME->menu = $menu; |
2507b2f5 |
1856 | $navmenulist = isset($THEME->navmenulist) ? $THEME->navmenulist : ''; |
0d741155 |
1857 | |
b0ccd3fb |
1858 | if ($button == '') { |
1859 | $button = ' '; |
9fa49e22 |
1860 | } |
1861 | |
1862 | if (!$menu and $navigation) { |
8a33e371 |
1863 | if (empty($CFG->loginhttps)) { |
1864 | $wwwroot = $CFG->wwwroot; |
1865 | } else { |
1866 | $wwwroot = str_replace('http','https',$CFG->wwwroot); |
1867 | } |
0e18f827 |
1868 | if (isset($course->id)) { |
009f39be |
1869 | $menu = user_login_string($course); |
9fa49e22 |
1870 | } else { |
c44d5d42 |
1871 | $menu = user_login_string($SITE); |
9fa49e22 |
1872 | } |
1873 | } |
67ccec43 |
1874 | |
b4bac9b6 |
1875 | if (isset($SESSION->justloggedin)) { |
1876 | unset($SESSION->justloggedin); |
1877 | if (!empty($CFG->displayloginfailures)) { |
1878 | if (!empty($USER->username) and !isguest()) { |
1879 | if ($count = count_login_failures($CFG->displayloginfailures, $USER->username, $USER->lastlogin)) { |
1880 | $menu .= ' <font size="1">'; |
1881 | if (empty($count->accounts)) { |
1882 | $menu .= get_string('failedloginattempts', '', $count); |
1883 | } else { |
1884 | $menu .= get_string('failedloginattemptsall', '', $count); |
1885 | } |
1886 | if (isadmin()) { |
1887 | $menu .= ' (<a href="'.$CFG->wwwroot.'/course/log.php'. |
839f2456 |
1888 | '?chooselog=1&id=1&modid=site_errors">'.get_string('logs').'</a>)'; |
b4bac9b6 |
1889 | } |
1890 | $menu .= '</font>'; |
1891 | } |
1892 | } |
1893 | } |
1894 | } |
9fa49e22 |
1895 | |
47037513 |
1896 | |
6aaa17c7 |
1897 | $encoding = current_charset(); |
1898 | if (!empty($CFG->courselang)) { |
a2fa19d8 |
1899 | moodle_setlocale(); |
9fa49e22 |
1900 | } |
6aaa17c7 |
1901 | $SESSION->encoding = $encoding; |
1902 | |
b0ccd3fb |
1903 | $meta = '<meta http-equiv="content-type" content="text/html; charset='. $encoding .'" />'. "\n". $meta ."\n"; |
03fe48e7 |
1904 | if (!$usexml) { |
1905 | @header('Content-type: text/html; charset='.$encoding); |
1906 | } |
9fa49e22 |
1907 | |
b0ccd3fb |
1908 | if ( get_string('thisdirection') == 'rtl' ) { |
1909 | $direction = ' dir="rtl"'; |
9fa49e22 |
1910 | } else { |
b0ccd3fb |
1911 | $direction = ' dir="ltr"'; |
9fa49e22 |
1912 | } |
ab9f24ad |
1913 | |
5debee2d |
1914 | if ($cache) { // Allow caching on "back" (but not on normal clicks) |
1915 | @header('Cache-Control: private, pre-check=0, post-check=0, max-age=0'); |
1916 | @header('Pragma: no-cache'); |
772e78be |
1917 | @header('Expires: '); |
5debee2d |
1918 | } else { // Do everything we can to always prevent clients and proxies caching |
03fe48e7 |
1919 | @header('Cache-Control: no-store, no-cache, must-revalidate'); |
1920 | @header('Cache-Control: post-check=0, pre-check=0', false); |
1921 | @header('Pragma: no-cache'); |
5debee2d |
1922 | @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT'); |
1923 | @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); |
03fe48e7 |
1924 | |
5debee2d |
1925 | $meta .= "\n<meta http-equiv=\"pragma\" content=\"no-cache\" />"; |
1926 | $meta .= "\n<meta http-equiv=\"expires\" content=\"0\" />"; |
66a51452 |
1927 | } |
5debee2d |
1928 | @header('Accept-Ranges: none'); |
66a51452 |
1929 | |
1930 | if ($usexml) { // Added by Gustav Delius / Mad Alex for MathML output |
8f0cd6ef |
1931 | // Modified by Julian Sedding |
66a51452 |
1932 | $currentlanguage = current_language(); |
8f0cd6ef |
1933 | $mathplayer = preg_match("/MathPlayer/i", $_SERVER['HTTP_USER_AGENT']); |
1934 | if(!$mathplayer) { |
1935 | header('Content-Type: application/xhtml+xml'); |
1936 | } |
b0ccd3fb |
1937 | echo '<?xml version="1.0" ?>'."\n"; |
66a51452 |
1938 | if (!empty($CFG->xml_stylesheets)) { |
b0ccd3fb |
1939 | $stylesheets = explode(';', $CFG->xml_stylesheets); |
66a51452 |
1940 | foreach ($stylesheets as $stylesheet) { |
b0ccd3fb |
1941 | echo '<?xml-stylesheet type="text/xsl" href="'. $CFG->wwwroot .'/'. $stylesheet .'" ?>' . "\n"; |
66a51452 |
1942 | } |
1943 | } |
b0ccd3fb |
1944 | echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1'; |
e4576482 |
1945 | if (!empty($CFG->xml_doctype_extra)) { |
b0ccd3fb |
1946 | echo ' plus '. $CFG->xml_doctype_extra; |
e4576482 |
1947 | } |
b0ccd3fb |
1948 | echo '//' . strtoupper($currentlanguage) . '" "'. $CFG->xml_dtd .'">'."\n"; |
8f0cd6ef |
1949 | $direction = " xmlns=\"http://www.w3.org/1999/xhtml\" |
1950 | xmlns:math=\"http://www.w3.org/1998/Math/MathML\" |
1951 | xml:lang=\"en\" |
1952 | xmlns:xlink=\"http://www.w3.org/1999/xlink\" |
1953 | $direction"; |
1954 | if($mathplayer) { |
1955 | $meta .= '<object id="mathplayer" classid="clsid:32F66A20-7614-11D4-BD11-00104BD3F987">' . "\n"; |
b0ccd3fb |
1956 | $meta .= '<!--comment required to prevent this becoming an empty tag-->'."\n"; |
1957 | $meta .= '</object>'."\n"; |
8f0cd6ef |
1958 | $meta .= '<?import namespace="math" implementation="#mathplayer" ?>' . "\n"; |
1959 | } |
9fa49e22 |
1960 | } |
1961 | |
7bb6d80f |
1962 | // Clean up the title |
1963 | |
2eea2cce |
1964 | $title = str_replace('"', '"', $title); |
1d9fc417 |
1965 | $title = strip_tags($title); |
2eea2cce |
1966 | |
7bb6d80f |
1967 | // Create class and id for this page |
1968 | |
68d5f00a |
1969 | page_id_and_class($pageid, $pageclass); |
7bb6d80f |
1970 | |
d6c66e12 |
1971 | if (isset($course->id)) { |
1972 | $pageclass .= ' course-'.$course->id; |
c09e00ba |
1973 | } else { |
1974 | $pageclass .= ' course-'.SITEID; |
d6c66e12 |
1975 | } |
1976 | |
e299f862 |
1977 | if (!empty($USER->editing)) { |
1978 | $pageclass .= ' editing'; |
1979 | } |
1980 | |
7bb6d80f |
1981 | $bodytags .= ' class="'.$pageclass.'" id="'.$pageid.'"'; |
772e78be |
1982 | |
560811bb |
1983 | include ($CFG->themedir.current_theme().'/header.html'); |
e608dddd |
1984 | |
cdf39255 |
1985 | if (!empty($CFG->messaging)) { |
1986 | echo message_popup_window(); |
1987 | } |
9fa49e22 |
1988 | } |
1989 | |
d48b00b4 |
1990 | /** |
1991 | * This version of print_header is simpler because the course name does not have to be |
1992 | * provided explicitly in the strings. It can be used on the site page as in courses |
1993 | * Eventually all print_header could be replaced by print_header_simple |
1994 | * |
89dcb99d |
1995 | * @param string $title Appears at the top of the window |
1996 | * @param string $heading Appears at the top of the page |
1997 | * @param string $navigation Premade navigation string (for use as breadcrumbs links) |
1998 | * @param string $focus Indicates form element to get cursor focus on load eg inputform.password |
1999 | * @param string $meta Meta tags to be added to the header |
2000 | * @param boolean $cache Should this page be cacheable? |
2001 | * @param string $button HTML code for a button (usually for module editing) |
2002 | * @param string $menu HTML code for a popup menu |
2003 | * @param boolean $usexml use XML for this page |
2004 | * @param string $bodytags This text will be included verbatim in the <body> tag (useful for onload() etc) |
d48b00b4 |
2005 | */ |
b0ccd3fb |
2006 | function print_header_simple($title='', $heading='', $navigation='', $focus='', $meta='', |
2007 | $cache=true, $button=' ', $menu='', $usexml=false, $bodytags='') { |
90fcc576 |
2008 | |
52d55cfc |
2009 | global $course,$CFG; // The same hack is used in print_header |
90fcc576 |
2010 | |
2011 | $shortname =''; |
2012 | if ($course->category) { |
52d55cfc |
2013 | $shortname = '<a href="'.$CFG->wwwroot.'/course/view.php?id='. $course->id .'">'. $course->shortname .'</a> ->'; |
90fcc576 |
2014 | } |
2015 | |
b0ccd3fb |
2016 | print_header($course->shortname .': '. $title, $course->fullname .' '. $heading, $shortname .' '. $navigation, $focus, $meta, |
90fcc576 |
2017 | $cache, $button, $menu, $usexml, $bodytags); |
2018 | } |
629b5885 |
2019 | |
2020 | |
d48b00b4 |
2021 | /** |
2022 | * Can provide a course object to make the footer contain a link to |
2023 | * to the course home page, otherwise the link will go to the site home |
2024 | * |
2025 | * @uses $CFG |
2026 | * @uses $USER |
89dcb99d |
2027 | * @param course $course {@link $COURSE} object containing course information |
2028 | * @param ? $usercourse ? |
d48b00b4 |
2029 | * @todo Finish documenting this function |
2030 | */ |
b3a2b9dd |
2031 | function print_footer($course=NULL, $usercourse=NULL) { |
0d741155 |
2032 | global $USER, $CFG, $THEME; |
9fa49e22 |
2033 | |
9fa49e22 |
2034 | /// Course links |
2035 | if ($course) { |
a789577b |
2036 | if (is_string($course) && $course == 'none') { // Don't print any links etc |
b3a2b9dd |
2037 | $homelink = ''; |
2038 | $loggedinas = ''; |
0d741155 |
2039 | $home = false; |
a789577b |
2040 | } else if (is_string($course) && $course == 'home') { // special case for site home page - please do not remove |
b3a2b9dd |
2041 | $course = get_site(); |
a77ac3dc |
2042 | $homelink = '<div class="sitelink">'. |
2043 | '<a title="moodle '. $CFG->release .' ('. $CFG->version .')" href="http://moodle.org/" target="_blank">'. |
2044 | '<br /><img width="100" height="30" src="pix/moodlelogo.gif" border="0" alt="moodlelogo" /></a></div>'; |
0d741155 |
2045 | $home = true; |
9fa49e22 |
2046 | } else { |
a77ac3dc |
2047 | $homelink = '<div class="homelink"><a target="'.$CFG->framename.'" href="'.$CFG->wwwroot. |
2048 | '/course/view.php?id='.$course->id.'">'.$course->shortname.'</a></div>'; |
0d741155 |
2049 | $home = false; |
9fa49e22 |
2050 | } |
2051 | } else { |
b3a2b9dd |
2052 | $course = get_site(); // Set course as site course by default |
a77ac3dc |
2053 | $homelink = '<div class="homelink"><a target="'.$CFG->framename.'" href="'.$CFG->wwwroot.'/">'.get_string('home').'</a></div>'; |
0d741155 |
2054 | $home = false; |
9fa49e22 |
2055 | } |
2056 | |
0d741155 |
2057 | /// Set up some other navigation links (passed from print_header by ugly hack) |
2507b2f5 |
2058 | $menu = isset($THEME->menu) ? str_replace('navmenu', 'navmenufooter', $THEME->menu) : ''; |
2059 | $title = isset($THEME->title) ? $THEME->title : ''; |
2060 | $button = isset($THEME->button) ? $THEME->button : ''; |
2061 | $heading = isset($THEME->heading) ? $THEME->heading : ''; |
2062 | $navigation = isset($THEME->navigation) ? $THEME->navigation : ''; |
2063 | $navmenulist = isset($THEME->navmenulist) ? $THEME->navmenulist : ''; |
0d741155 |
2064 | |
f940ee41 |
2065 | |
b3a2b9dd |
2066 | /// Set the user link if necessary |
2067 | if (!$usercourse and is_object($course)) { |
1f2eec7b |
2068 | $usercourse = $course; |
2069 | } |
2070 | |
b3a2b9dd |
2071 | if (!isset($loggedinas)) { |
2072 | $loggedinas = user_login_string($usercourse, $USER); |
2073 | } |
2074 | |
0d741155 |
2075 | if ($loggedinas == $menu) { |
9e0d1983 |
2076 | $menu = ''; |
0d741155 |
2077 | } |
2078 | |
b8cea9b2 |
2079 | /// Provide some performance info if required |
c2fd9e95 |
2080 | $performanceinfo = ''; |
4907efae |
2081 | if (defined('MDL_PERF') || $CFG->debug > 7 || $CFG->perfdebug > 7) { |
c2fd9e95 |
2082 | $perf = get_performance_info(); |
853df85e |
2083 | if (defined('MDL_PERFTOLOG')) { |
2084 | error_log("PERF: " . $perf['txt']); |
2085 | } |
4907efae |
2086 | if (defined('MDL_PERFTOFOOT') || $CFG->debug > 7 || $CFG->perfdebug > 7) { |
c2fd9e95 |
2087 | $performanceinfo = $perf['html']; |
2088 | } |
572fe9ab |
2089 | } |
b8cea9b2 |
2090 | |
0d741155 |
2091 | |
b3a2b9dd |
2092 | /// Include the actual footer file |
a282d0ff |
2093 | |
560811bb |
2094 | include ($CFG->themedir.current_theme().'/footer.html'); |
a9a9bdba |
2095 | |
a282d0ff |
2096 | } |
2097 | |
c3f55692 |
2098 | /** |
2099 | * Returns the name of the current theme |
2100 | * |
2101 | * @uses $CFG |
2102 | * @param $USER |
2103 | * @param $SESSION |
2104 | * @return string |
2105 | */ |
2106 | function current_theme() { |
32e2b302 |
2107 | global $CFG, $USER, $SESSION, $course; |
c3f55692 |
2108 | |
417375b7 |
2109 | if (!empty($CFG->pagetheme)) { // Page theme is for special page-only themes set by code |
2110 | return $CFG->pagetheme; |
c3f55692 |
2111 | |
59552aab |
2112 | } else if (!empty($CFG->coursetheme) and !empty($CFG->allowcoursethemes)) { // Course themes override others |
c3f55692 |
2113 | return $CFG->coursetheme; |
2114 | |
2115 | } else if (!empty($SESSION->theme)) { // Session theme can override other settings |
2116 | return $SESSION->theme; |
2117 | |
32e2b302 |
2118 | } else if (!empty($USER->theme) and !empty($CFG->allowuserthemes)) { // User theme can override site theme |
c3f55692 |
2119 | return $USER->theme; |
2120 | |
2121 | } else { |
2122 | return $CFG->theme; |
2123 | } |
2124 | } |
2125 | |
2126 | |
d48b00b4 |
2127 | /** |
2128 | * This function is called by stylesheets to set up the header |
2129 | * approriately as well as the current path |
2130 | * |
2131 | * @uses $CFG |
89dcb99d |
2132 | * @param int $lastmodified ? |
2133 | * @param int $lifetime ? |
2134 | * @param string $thename ? |
d48b00b4 |
2135 | */ |
55b734fb |
2136 | function style_sheet_setup($lastmodified=0, $lifetime=300, $themename='', $forceconfig='', $lang='') { |
6535be85 |
2137 | |
9c4e6e21 |
2138 | global $CFG, $THEME; |
ab9f24ad |
2139 | |
b0ccd3fb |
2140 | header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $lastmodified) . ' GMT'); |
2141 | header('Expires: ' . gmdate("D, d M Y H:i:s", time() + $lifetime) . ' GMT'); |
2142 | header('Cache-control: max_age = '. $lifetime); |
2143 | header('Pragma: '); |
2144 | header('Content-type: text/css'); // Correct MIME type |
6535be85 |
2145 | |
d45fd4dd |
2146 | $DEFAULT_SHEET_LIST = array('styles_layout', 'styles_fonts', 'styles_color'); |
9c4e6e21 |
2147 | |
2148 | if (empty($themename)) { |
2149 | $themename = current_theme(); // So we have something. Normally not needed. |
ab25ce31 |
2150 | } else { |
2151 | $themename = clean_param($themename, PARAM_SAFEDIR); |
9c4e6e21 |
2152 | } |
2153 | |
2154 | if (!empty($forceconfig)) { // Page wants to use the config from this theme instead |
2155 | unset($THEME); |
2d40fc05 |
2156 | include($CFG->themedir.$forceconfig.'/'.'config.php'); |
9c4e6e21 |
2157 | } |
2158 | |
2159 | /// If this is the standard theme calling us, then find out what sheets we need |
2160 | |
2161 | if ($themename == 'standard') { |
2162 | if (!isset($THEME->standardsheets) or $THEME->standardsheets === true) { // Use all the sheets we have |
2163 | $THEME->sheets = $DEFAULT_SHEET_LIST; |
2164 | } else if (empty($THEME->standardsheets)) { // We can stop right now! |
2165 | echo "/***** Nothing required from this stylesheet by main theme *****/\n\n"; |
2166 | exit; |
2167 | } else { // Use the provided subset only |
2168 | $THEME->sheets = $THEME->standardsheets; |
2169 | } |
2170 | |
2171 | /// If we are a parent theme, then check for parent definitions |
2172 | |
2173 | } else if (!empty($THEME->parent) && $themename == $THEME->parent) { |
2174 | if (!isset($THEME->parentsheets) or $THEME->parentsheets === true) { // Use all the sheets we have |
2175 | $THEME->sheets = $DEFAULT_SHEET_LIST; |
2176 | } else if (empty($THEME->parentsheets)) { // We can stop right now! |
2177 | echo "/***** Nothing required from this stylesheet by main theme *****/\n\n"; |
2178 | exit; |
2179 | } else { // Use the provided subset only |
2180 | $THEME->sheets = $THEME->parentsheets; |
2181 | } |
2182 | } |
2183 | |
2184 | /// Work out the last modified date for this theme |
2185 | |
2186 | foreach ($THEME->sheets as $sheet) { |
2d40fc05 |
2187 | if (file_exists($CFG->themedir.$themename.'/'.$sheet.'.css')) { |
2188 | $sheetmodified = filemtime($CFG->themedir.$themename.'/'.$sheet.'.css'); |
9c4e6e21 |
2189 | if ($sheetmodified > $lastmodified) { |
2190 | $lastmodified = $sheetmodified; |
2191 | } |
2192 | } |
6535be85 |
2193 | } |
2194 | |
6535be85 |
2195 | |
6ba172fb |
2196 | /// Get a list of all the files we want to include |
2197 | $files = array(); |
2198 | |
2199 | foreach ($THEME->sheets as $sheet) { |
2200 | $files[] = array($CFG->themedir, $themename.'/'.$sheet.'.css'); |
2201 | } |
2202 | |
2203 | if ($themename == 'standard') { // Add any standard styles included in any modules |
2204 | if (!empty($THEME->modsheets)) { // Search for styles.php within activity modules |
2205 | if ($mods = get_list_of_plugins('mod')) { |
2206 | foreach ($mods as $mod) { |
2207 | if (file_exists($CFG->dirroot.'/mod/'.$mod.'/styles.php')) { |
2208 | $files[] = array($CFG->dirroot, '/mod/'.$mod.'/styles.php'); |
2209 | } |
08396bb2 |
2210 | } |
2211 | } |
2212 | } |
a2b3f884 |
2213 | |
6ba172fb |
2214 | if (!empty($THEME->blocksheets)) { // Search for styles.php within block modules |
2215 | if ($mods = get_list_of_plugins('blocks')) { |
2216 | foreach ($mods as $mod) { |
2217 | if (file_exists($CFG->dirroot.'/blocks/'.$mod.'/styles.php')) { |
2218 | $files[] = array($CFG->dirroot, '/blocks/'.$mod.'/styles.php'); |
2219 | } |
08396bb2 |
2220 | } |
2221 | } |
2222 | } |
a2b3f884 |
2223 | |
6ba172fb |
2224 | if (!empty($THEME->langsheets)) { // Search for styles.php within the current language |
6ba172fb |
2225 | if (file_exists($CFG->dirroot.'/lang/'.$lang.'/styles.php')) { |
2226 | $files[] = array($CFG->dirroot, '/lang/'.$lang.'/styles.php'); |
2227 | } |
2228 | } |
08396bb2 |
2229 | } |
2230 | |
6ba172fb |
2231 | |
2232 | if ($files) { |
2233 | /// Produce a list of all the files first |
2234 | echo '/**************************************'."\n"; |
2235 | echo ' * THEME NAME: '.$themename."\n *\n"; |
2236 | echo ' * Files included in this sheet:'."\n *\n"; |
2237 | foreach ($files as $file) { |
2238 | echo ' * '.$file[1]."\n"; |
08396bb2 |
2239 | } |
6ba172fb |
2240 | echo ' **************************************/'."\n\n"; |
08396bb2 |
2241 | |
6ba172fb |
2242 | |
2243 | /// Actually output all the files in order. |
2244 | foreach ($files as $file) { |
2245 | echo '/***** '.$file[1].' start *****/'."\n\n"; |
2246 | @include_once($file[0].$file[1]); |
2247 | echo '/***** '.$file[1].' end *****/'."\n\n"; |
2248 | } |
9c4e6e21 |
2249 | } |
2250 | |
2d40fc05 |
2251 | return $CFG->themewww.$themename; // Only to help old themes (1.4 and earlier) |
6535be85 |
2252 | } |
2253 | |
9c4e6e21 |
2254 | |
2255 | function theme_setup($theme = '', $params=NULL) { |
d74d4f20 |
2256 | /// Sets up global variables related to themes |
2257 | |
56766f85 |
2258 | global $CFG, $THEME, $SESSION, $USER; |
d74d4f20 |
2259 | |
2260 | if (empty($theme)) { |
2261 | $theme = current_theme(); |
2262 | } |
9c4e6e21 |
2263 | |
55b734fb |
2264 | /// Load up the theme config |
2265 | $THEME = NULL; // Just to be sure |
2266 | include($CFG->themedir. $theme .'/config.php'); // Main config for current theme |
2267 | |
9c4e6e21 |
2268 | /// Put together the parameters |
2269 | if (!$params) { |
2270 | $params = array(); |
2271 | } |
56766f85 |
2272 | if ($theme != $CFG->theme) { |
2273 | $params[] = 'forceconfig='.$theme; |
9c4e6e21 |
2274 | } |
55b734fb |
2275 | |
2276 | /// Force language too if required |
2277 | if (!empty($THEME->langsheets)) { |
2278 | $params[] = 'lang='.current_language(); |
2279 | } |
2280 | |
2281 | /// Convert params to string |
9c4e6e21 |
2282 | if ($params) { |
55b734fb |
2283 | $paramstring = '?'.implode('&', $params); |
d74d4f20 |
2284 | } else { |
9c4e6e21 |
2285 | $paramstring = ''; |
d74d4f20 |
2286 | } |
2287 | |
9c4e6e21 |
2288 | /// Set up image paths |
25580407 |
2289 | if (empty($THEME->custompix)) { // Could be set in the above file |
d74d4f20 |
2290 | $CFG->pixpath = $CFG->wwwroot .'/pix'; |
2291 | $CFG->modpixpath = $CFG->wwwroot .'/mod'; |
2292 | } else { |
2d40fc05 |
2293 | $CFG->pixpath = $CFG->themewww . $theme .'/pix'; |
2294 | $CFG->modpixpath = $CFG->themewww . $theme .'/pix/mod'; |
d74d4f20 |
2295 | } |
2296 | |
9c4e6e21 |
2297 | /// Header and footer paths |
2d40fc05 |
2298 | $CFG->header = $CFG->themedir . $theme .'/header.html'; |
2299 | $CFG->footer = $CFG->themedir . $theme .'/footer.html'; |
d74d4f20 |
2300 | |
9c4e6e21 |
2301 | /// Define stylesheet loading order |
d74d4f20 |
2302 | $CFG->stylesheets = array(); |
2303 | if ($theme != 'standard') { /// The standard sheet is always loaded first |
2d40fc05 |
2304 | $CFG->stylesheets[] = $CFG->themewww.'standard/styles.php'.$paramstring; |
d74d4f20 |
2305 | } |
2306 | if (!empty($THEME->parent)) { /// Parent stylesheets are loaded next |
2d40fc05 |
2307 | $CFG->stylesheets[] = $CFG->themewww.$THEME->parent.'/styles.php'.$paramstring; |
d74d4f20 |
2308 | } |
2d40fc05 |
2309 | $CFG->stylesheets[] = $CFG->themewww.$theme.'/styles.php'.$paramstring; |
d74d4f20 |
2310 | |
2311 | } |
2312 | |
c3f55692 |
2313 | |
d48b00b4 |
2314 | /** |
2315 | * Returns text to be displayed to the user which reflects their login status |
2316 | * |
2317 | * @uses $CFG |
2318 | * @uses $USER |
89dcb99d |
2319 | * @param course $course {@link $COURSE} object containing course information |
2320 | * @param user $user {@link $USER} object containing user information |
2321 | * @return string |
d48b00b4 |
2322 | */ |
c44d5d42 |
2323 | function user_login_string($course=NULL, $user=NULL) { |
2324 | global $USER, $CFG, $SITE; |
a282d0ff |
2325 | |
0e18f827 |
2326 | if (empty($user) and isset($USER->id)) { |
a282d0ff |
2327 | $user = $USER; |
2328 | } |
2329 | |
c44d5d42 |
2330 | if (empty($course)) { |
2331 | $course = $SITE; |
2332 | } |
2333 | |
a282d0ff |
2334 | if (isset($user->realuser)) { |
b0ccd3fb |
2335 | if ($realuser = get_record('user', 'id', $user->realuser)) { |
2d71e8ee |
2336 | $fullname = fullname($realuser, true); |
2337 | $realuserinfo = " [<a target=\"{$CFG->framename}\" |
2338 | href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&return=$realuser->id\">$fullname</a>] "; |
9fa49e22 |
2339 | } |
9d378732 |
2340 | } else { |
b0ccd3fb |
2341 | $realuserinfo = ''; |
9fa49e22 |
2342 | } |
2343 | |
87180677 |
2344 | if (empty($CFG->loginhttps)) { |
2345 | $wwwroot = $CFG->wwwroot; |
2346 | } else { |
2347 | $wwwroot = str_replace('http','https',$CFG->wwwroot); |
2348 | } |
2349 | |
a282d0ff |
2350 | if (isset($user->id) and $user->id) { |
2d71e8ee |
2351 | $fullname = fullname($user, true); |
2352 | $username = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$fullname</a>"; |
5c3706b2 |
2353 | $instudentview = (!empty($USER->studentview)) ? get_string('instudentview') : ''; |
0ae7e6f4 |
2354 | if (isguest($user->id)) { |
158de846 |
2355 | $loggedinas = $realuserinfo.get_string('loggedinasguest'). |
b0ccd3fb |
2356 | " (<a target=\"{$CFG->framename}\" href=\"$wwwroot/login/index.php\">".get_string('login').'</a>)'; |
0ae7e6f4 |
2357 | } else { |
5c3706b2 |
2358 | $loggedinas = $realuserinfo.get_string('loggedinas', 'moodle', $username).' '.$instudentview. |
b0ccd3fb |
2359 | " (<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/login/logout.php\">".get_string('logout').'</a>)'; |
0ae7e6f4 |
2360 | } |
9fa49e22 |
2361 | } else { |
b0ccd3fb |
2362 | $loggedinas = get_string('loggedinnot', 'moodle'). |
2363 | " (<a target=\"{$CFG->framename}\" href=\"$wwwroot/login/index.php\">".get_string('login').'</a>)'; |
9fa49e22 |
2364 | } |
d3593a67 |
2365 | return '<div class="logininfo">'.$loggedinas.'</div>'; |
9fa49e22 |
2366 | } |
2367 | |
d48b00b4 |
2368 | /** |
2369 | * Prints breadcrumbs links |
2370 | * |
2371 | * @uses $CFG |
89dcb99d |
2372 | * @param string $navigation The breadcrumbs string to be printed |
d48b00b4 |
2373 | */ |
9fa49e22 |
2374 | function print_navigation ($navigation) { |
0d6b9d4f |
2375 | global $CFG, $USER; |
9fa49e22 |
2376 | |
2377 | if ($navigation) { |
2378 | if (! $site = get_site()) { |
f150292f |
2379 | $site->shortname = get_string('home'); |
9fa49e22 |
2380 | } |
50e4a15e |
2381 | $navigation = str_replace('->', '»', $navigation); |
52788321 |
2382 | echo '<a target="'. $CFG->framename .'" href="'. $CFG->wwwroot.((!isadmin() && !empty($USER->id) && !empty($CFG->mymoodleredirect) && !isguest()) |
2383 | ? '/my' : '') .'/">'. $site->shortname .'</a> » '. $navigation; |
0d6b9d4f |
2384 | |
9fa49e22 |
2385 | } |
2386 | } |
2387 | |
d48b00b4 |
2388 | /** |
bfca0094 |
2389 | * Prints a string in a specified size (retained for backward compatibility) |
d48b00b4 |
2390 | * |
89dcb99d |
2391 | * @param string $text The text to be displayed |
2392 | * @param int $size The size to set the font for text display. |
d48b00b4 |
2393 | */ |
d4df9200 |
2394 | function print_headline($text, $size=2) { |
bfca0094 |
2395 | print_heading($text, 'left', $size); |
d4df9200 |
2396 | } |
2397 | |
d48b00b4 |
2398 | /** |
2399 | * Prints text in a format for use in headings. |
2400 | * |
89dcb99d |
2401 | * @param string $text The text to be displayed |
2402 | * @param string $align The alignment of the printed paragraph of text |
2403 | * @param int $size The size to set the font for text display. |
d48b00b4 |
2404 | */ |
4419aa80 |
2405 | function print_heading($text, $align='', $size=2, $class='main') { |
bfca0094 |
2406 | if ($align) { |
2407 | $align = ' align="'.$align.'"'; |
2408 | } |
4419aa80 |
2409 | if ($class) { |
2410 | $class = ' class="'.$class.'"'; |
2411 | } |
2412 | echo "<h$size $align $class>".stripslashes_safe($text)."</h$size>"; |
9fa49e22 |
2413 | } |
2414 | |
d48b00b4 |
2415 | /** |
2416 | * Centered heading with attached help button (same title text) |
2417 | * and optional icon attached |
2418 | * |
89dcb99d |
2419 | * @param string $text The text to be displayed |
2420 | * @param string $helppage The help page to link to |
2421 | * @param string $module The module whose help should be linked to |
2422 | * @param string $icon Image to display if needed |
d48b00b4 |
2423 | */ |
b0ccd3fb |
2424 | function print_heading_with_help($text, $helppage, $module='moodle', $icon='') { |
4419aa80 |
2425 | echo '<h2 class="main help">'.$icon.stripslashes_safe($text); |
9fa49e22 |
2426 | helpbutton($helppage, $text, $module); |
26ecdc5b |
2427 | echo '</h2>'; |
9fa49e22 |
2428 | } |
ab9f24ad |
2429 | |
6ee8277f |
2430 | |
2431 | function print_heading_block($heading, $class='') { |
b6f30b18 |
2432 | //Accessibility: 'headingblock' is now H1, see theme/standard/styles_*.css: ?? |
2433 | echo '<h2 class="headingblock header '.$class.'">'.stripslashes($heading).'</h2>'; |
6ee8277f |
2434 | } |
2435 | |
2436 | |
d48b00b4 |
2437 | /** |
2438 | * Print a link to continue on to another page. |
2439 | * |
2440 | * @uses $CFG |
2441 | * @param string $link The url to create a link to. |
2442 | */ |
9fa49e22 |
2443 | function print_continue($link) { |
9fa49e22 |
2444 | |
51a96819 |
2445 | global $CFG; |
2446 | |
9fa49e22 |
2447 | if (!$link) { |
b0ccd3fb |
2448 | $link = $_SERVER['HTTP_REFERER']; |
9fa49e22 |
2449 | } |
2450 | |
a8f68426 |
2451 | echo '<div class="continuebutton">'; |
2452 | print_single_button($link, NULL, get_string('continue'), 'post', $CFG->framename); |
2453 | echo '</div>'."\n"; |
9fa49e22 |
2454 | } |
2455 | |
d48b00b4 |
2456 | /** |
2457 | * Print a message in a standard themed box. |
2458 | * |
89dcb99d |
2459 | * @param string $message ? |
2460 | * @param string $align ? |
2461 | * @param string $width ? |
2462 | * @param string $color ? |
2463 | * @param int $padding ? |
2464 | * @param string $class ? |
d48b00b4 |
2465 | * @todo Finish documenting this function |
2466 | */ |
da17a899 |
2467 | function print_simple_box($message, $align='', $width='', $color='', $padding=5, $class='generalbox', $id='') { |
2468 | print_simple_box_start($align, $width, $color, $padding, $class, $id); |
7d8f674d |
2469 | echo stripslashes_safe($message); |
9fa49e22 |
2470 | print_simple_box_end(); |
2471 | } |
2472 | |
d48b00b4 |
2473 | /** |
2474 | * Print the top portion of a standard themed box. |
2475 | * |
89dcb99d |
2476 | * @param string $align ? |
2477 | * @param string $width ? |
2478 | * @param string $color ? |
2479 | * @param int $padding ? |
2480 | * @param string $class ? |
d48b00b4 |
2481 | * @todo Finish documenting this function |
2482 | */ |
da17a899 |
2483 | function print_simple_box_start($align='', $width='', $color='', $padding=5, $class='generalbox', $id='') { |
9fa49e22 |
2484 | |
c147b8ab |
2485 | if ($color) { |
2486 | $color = 'bgcolor="'. $color .'"'; |
2487 | } |
9fa49e22 |
2488 | if ($align) { |
b0ccd3fb |
2489 | $align = 'align="'. $align .'"'; |
9fa49e22 |
2490 | } |
2491 | if ($width) { |
b0ccd3fb |
2492 | $width = 'width="'. $width .'"'; |
9fa49e22 |
2493 | } |
da17a899 |
2494 | if ($id) { |
2495 | $id = 'id="'. $id .'"'; |
2496 | } |
2497 | echo "<table $align $width $id class=\"$class\" border=\"0\" cellpadding=\"$padding\" cellspacing=\"0\">". |
c147b8ab |
2498 | "<tr><td $color class=\"$class"."content\">"; |
9fa49e22 |
2499 | } |
2500 | |
d48b00b4 |
2501 | /** |
2502 | * Print the end portion of a standard themed box. |
d48b00b4 |
2503 | */ |
9fa49e22 |
2504 | function print_simple_box_end() { |
b0ccd3fb |
2505 | echo '</td></tr></table>'; |
9fa49e22 |
2506 | } |
2507 | |
d48b00b4 |
2508 | /** |
2509 | * Print a self contained form with a single submit button. |
2510 | * |
89dcb99d |
2511 | * @param string $link ? |
2512 | * @param array $options ? |
2513 | * @param string $label ? |
2514 | * @param string $method ? |
d48b00b4 |
2515 | * @todo Finish documenting this function |
2516 | */ |
4c6c0e01 |
2517 | function print_single_button($link, $options, $label='OK', $method='get', $target='_self') { |
6ba172fb |
2518 | echo '<div class="singlebutton">'; |
4c6c0e01 |
2519 | echo '<form action="'. $link .'" method="'. $method .'" target="'.$target.'">'; |
9fa49e22 |
2520 | if ($options) { |
2521 | foreach ($options as $name => $value) { |
b0ccd3fb |
2522 | echo '<input type="hidden" name="'. $name .'" value="'. $value .'" />'; |
9fa49e22 |
2523 | } |
2524 | } |
6ba172fb |
2525 | echo '<input type="submit" value="'. $label .'" /></form></div>'; |
9fa49e22 |
2526 | } |
2527 | |
d48b00b4 |
2528 | /** |
2529 | * Print a spacer image with the option of including a line break. |
2530 | * |
89dcb99d |
2531 | * @param int $height ? |
2532 | * @param int $width ? |
2533 | * @param boolean $br ? |
d48b00b4 |
2534 | * @todo Finish documenting this function |
2535 | */ |
9fa49e22 |
2536 | function print_spacer($height=1, $width=1, $br=true) { |
2537 | global $CFG; |
ed3136ff |
2538 | echo '<img class="spacer" height="'. $height .'" width="'. $width .'" src="'. $CFG->wwwroot .'/pix/spacer.gif" alt="" />'; |
9fa49e22 |
2539 | if ($br) { |
b0ccd3fb |
2540 | echo '<br />'."\n"; |
9fa49e22 |
2541 | } |
2542 | } |
2543 | |
d48b00b4 |
2544 | /** |
2545 | * Given the path to a picture file in a course, or a URL, |
2546 | * this function includes the picture in the page. |
2547 | * |
89dcb99d |
2548 | * @param string $path ? |
2549 | * @param int $courseid ? |
2550 | * @param int $height ? |
2551 | * @param int $width ? |
2552 | * @param string $link ? |
d48b00b4 |
2553 | * @todo Finish documenting this function |
2554 | */ |
b0ccd3fb |
2555 | function print_file_picture($path, $courseid=0, $height='', $width='', $link='') { |
9fa49e22 |
2556 | global $CFG; |
2557 | |
2558 | if ($height) { |
b0ccd3fb |
2559 | $height = 'height="'. $height .'"'; |
9fa49e22 |
2560 | } |
2561 | if ($width) { |
b0ccd3fb |
2562 | $width = 'width="'. $width .'"'; |
9fa49e22 |
2563 | } |
2564 | if ($link) { |
b0ccd3fb |
2565 | echo '<a href="'. $link .'">'; |
9fa49e22 |
2566 | } |
b0ccd3fb |
2567 | if (substr(strtolower($path), 0, 7) == 'http://') { |
2568 | echo '<img border="0" '.$height . $width .' src="'. $path .'" />'; |
9fa49e22 |
2569 | |
2570 | } else if ($courseid) { |
b0ccd3fb |
2571 | echo '<img border="0" '. $height . $width .' src="'; |
9fa49e22 |
2572 | if ($CFG->slasharguments) { // Use this method if possible for better caching |
b0ccd3fb |
2573 | echo $CFG->wwwroot .'/file.php/'. $courseid .'/'. $path; |
9fa49e22 |
2574 | } else { |
b0ccd3fb |
2575 | echo $CFG->wwwroot .'/file.php?file=/'. $courseid .'/'. $path; |
9fa49e22 |
2576 | } |
b0ccd3fb |
2577 | echo '" />'; |
9fa49e22 |
2578 | } else { |
b0ccd3fb |
2579 | echo 'Error: must pass URL or course'; |
9fa49e22 |
2580 | } |
2581 | if ($link) { |
b0ccd3fb |
2582 | echo '</a>'; |
9fa49e22 |
2583 | } |
2584 | } |
2585 | |
d48b00b4 |
2586 | /** |
2587 | * Print the specified user's avatar. |
2588 | * |
89dcb99d |
2589 | * @param int $userid ? |
2590 | * @param int $courseid ? |
2591 | * @param boolean $picture Print the user picture? |
4c6c0e01 |
2592 | * @param int $size Size in pixels. Special values are (true/1 = 100px) and (false/0 = 35px) for backward compatability |
89dcb99d |
2593 | * @param boolean $returnstring If false print picture to current page, otherwise return the output as string |
2594 | * @param boolean $link Enclose printed image in a link to view specified course? |
2595 | * return string |
d48b00b4 |
2596 | * @todo Finish documenting this function |
2597 | */ |
f66e1977 |
2598 | function print_user_picture($userid, $courseid, $picture, $size=0, $returnstring=false, $link=true, $target='') { |
f374fb10 |
2599 | global $CFG; |
9fa49e22 |
2600 | |
2601 | if ($link) { |
f66e1977 |
2602 | if ($target) { |
2603 | $target=' target="_blank"'; |
2604 | } |
2605 | $output = '<a '.$target.' href="'. $CFG->wwwroot .'/user/view.php?id='. $userid .'&course='. $courseid .'">'; |
9fa49e22 |
2606 | } else { |
b0ccd3fb |
2607 | $output = ''; |
9fa49e22 |
2608 | } |
4c6c0e01 |
2609 | if (empty($size)) { |
2610 | $file = 'f2'; |
da7a785b |
2611 | $size = 35; |
4c6c0e01 |
2612 | } else if ($size === true or $size == 1) { |
b0ccd3fb |
2613 | $file = 'f1'; |
9fa49e22 |
2614 | $size = 100; |
4c6c0e01 |
2615 | } else if ($size >= 50) { |
2616 | $file = 'f1'; |
9fa49e22 |
2617 | } else { |
b0ccd3fb |
2618 | $file = 'f2'; |
9fa49e22 |
2619 | } |
113a79e2 |
2620 | $class = "userpicture"; |
67a63a30 |
2621 | if ($picture) { // Print custom user picture |
9fa49e22 |
2622 | if ($CFG->slasharguments) { // Use this method if possible for better caching |
113a79e2 |
2623 | $src = $CFG->wwwroot .'/user/pix.php/'. $userid .'/'. $file .'.jpg"'; |
9fa49e22 |
2624 | } else { |
113a79e2 |
2625 | $src = $CFG->wwwroot .'/user/pix.php?file=/'. $userid .'/'. $file .'.jpg"'; |
9fa49e22 |
2626 | } |
67a63a30 |
2627 | } else { // Print default user pictures (use theme version if available) |
113a79e2 |
2628 | $class .= " defaultuserpic"; |
2629 | $src = "$CFG->pixpath/u/$file.png\""; |
9fa49e22 |
2630 | } |
113a79e2 |
2631 | $output .= "<img class=\"$class\" align=\"middle\" src=\"$src". |
2632 | " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" />"; |
9fa49e22 |
2633 | if ($link) { |
b0ccd3fb |
2634 | $output .= '</a>'; |
9fa49e22 |
2635 | } |
2636 | |
2637 | if ($returnstring) { |
2638 | return $output; |
2639 | } else { |
2640 | echo $output; |
2641 | } |
2642 | } |
2643 | |
d48b00b4 |
2644 | /** |
2645 | * Prints a summary of a user in a nice little box. |
2646 | * |
89dcb99d |
2647 | * @uses $CFG |
2648 | * @uses $USER |
2649 | * @param user $user A {@link $USER} object representing a user |
2650 | * @param course $course A {@link $COURSE} object representing a course |
d48b00b4 |
2651 | */ |
f19570d0 |
2652 | function print_user($user, $course, $messageselect=false) { |
951b22a8 |
2653 | |
89dcb99d |
2654 | global $CFG, $USER; |
499795e8 |
2655 | |
951b22a8 |
2656 | static $string; |
2657 | static $datestring; |
2658 | static $countries; |
2659 | static $isteacher; |
37d83d99 |
2660 | static $isadmin; |
951b22a8 |
2661 | |
2662 | if (empty($string)) { // Cache all the strings for the rest of the page |
2663 | |
b0ccd3fb |
2664 | $string->email = get_string('email'); |
2665 | $string->location = get_string('location'); |
2666 | $string->lastaccess = get_string('lastaccess'); |
2667 | $string->activity = get_string('activity'); |
2668 | $string->unenrol = get_string('unenrol'); |
2669 | $string->loginas = get_string('loginas'); |
2670 | $string->fullprofile = get_string('fullprofile'); |
2671 | $string->role = get_string('role'); |
2672 | $string->name = get_string('name'); |
2673 | $string->never = get_string('never'); |
2674 | |
2675 | $datestring->day = get_string('day'); |
2676 | $datestring->days = get_string('days'); |
2677 | $datestring->hour = get_string('hour'); |
2678 | $datestring->hours = get_string('hours'); |
2679 | $datestring->min = get_string('min'); |
2680 | $datestring->mins = get_string('mins'); |
2681 | $datestring->sec = get_string('sec'); |
2682 | $datestring->secs = get_string('secs'); |
951b22a8 |
2683 | |
2684 | $countries = get_list_of_countries(); |
2685 | |
2686 | $isteacher = isteacher($course->id); |
37d83d99 |
2687 | $isadmin = isadmin(); |
951b22a8 |
2688 | } |
2689 | |
3468d58a |
2690 | /// Get the hidden field list |
2691 | if ($isteacher || $isadmin) { |
2692 | $hiddenfields = array(); |
2693 | } else { |
2694 | $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields)); |
2695 | } |
2696 | |
ed3136ff |
2697 | echo '<table class="userinfobox">'; |
951b22a8 |
2698 | echo '<tr>'; |
ed3136ff |
2699 | echo '<td class="left side">'; |
951b22a8 |
2700 | print_user_picture($user->id, $course->id, $user->picture, true); |
2701 | echo '</td>'; |
ed3136ff |
2702 | echo '<td class="content">'; |
2703 | echo '<div class="username">'.fullname($user, $isteacher).'</div>'; |
2704 | echo '<div class="info">'; |
951b22a8 |
2705 | if (!empty($user->role) and ($user->role <> $course->teacher)) { |
b0ccd3fb |
2706 | echo $string->role .': '. $user->role .'<br />'; |
951b22a8 |
2707 | } |
d0ec93fb |
2708 | if ($user->maildisplay == 1 or ($user->maildisplay == 2 and $course->category and !isguest()) or $isteacher) { |
b0ccd3fb |
2709 | echo $string->email .': <a href="mailto:'. $user->email .'">'. $user->email .'</a><br />'; |
951b22a8 |
2710 | } |
3468d58a |
2711 | if (($user->city or $user->country) and (!isset($hiddenfields['city']) or !isset($hiddenfields['country']))) { |
b0ccd3fb |
2712 | echo $string->location .': '; |
3468d58a |
2713 | if ($user->city && !isset($hiddenfields['city'])) { |
b40bc478 |
2714 | echo $user->city; |
2715 | } |
3468d58a |
2716 | if (!empty($countries[$user->country]) && !isset($hiddenfields['country'])) { |
2717 | if ($user->city && !isset($hiddenfields['city'])) { |
b40bc478 |
2718 | echo ', '; |
2719 | } |
2720 | echo $countries[$user->country]; |
2721 | } |
b0ccd3fb |
2722 | echo '<br />'; |
951b22a8 |
2723 | } |
3468d58a |
2724 | if (!isset($hiddenfields['lastaccess'])) { |
2725 | if ($user->lastaccess) { |
2726 | echo $string->lastaccess .': '. userdate($user->lastaccess); |
2727 | echo '  ('. format_time(time() - $user->lastaccess, $datestring) .')'; |
2728 | } else { |
2729 | echo $string->lastaccess .': '. $string->never; |
2730 | } |
951b22a8 |
2731 | } |
ed3136ff |
2732 | echo '</div></td><td class="links">'; |
951b22a8 |
2733 | |
951b22a8 |
2734 | if ($isteacher) { |
2735 | $timemidnight = usergetmidnight(time()); |
b0ccd3fb |
2736 | echo '<a href="'. $CFG->wwwroot .'/course/user.php?id='. $course->id .'&user='. $user->id .'">'. $string->activity .'</a><br />'; |
37d83d99 |
2737 | if (!iscreator($user->id) or ($isadmin and !isadmin($user->id))) { // Includes admins |
f29667f6 |
2738 | if ($course->category and isteacheredit($course->id) and isstudent($course->id, $user->id)) { // Includes admins |
b0ccd3fb |
2739 | echo '<a href="'. $CFG->wwwroot .'/course/unenrol.php?id='. $course->id .'&user='. $user->id .'">'. $string->unenrol .'</a><br />'; |
4e3a6092 |
2740 | } |
2741 | if ($USER->id != $user->id) { |
b0ccd3fb |
2742 | echo '<a href="'. $CFG->wwwroot .'/course/loginas.php?id='. $course->id .'&user='. $user->id .'">'. $string->loginas .'</a><br />'; |
4e3a6092 |
2743 | } |
951b22a8 |
2744 | } |
ab9f24ad |
2745 | } |
b0ccd3fb |
2746 | echo '<a href="'. $CFG->wwwroot .'/user/view.php?id='. $user->id .'&course='. $course->id .'">'. $string->fullprofile .'...</a>'; |
951b22a8 |
2747 | |
f19570d0 |
2748 | if (!empty($messageselect) && $isteacher) { |
9a0f8502 |
2749 | echo '<br /><input type="checkbox" name="'; |
2750 | if (isteacher($course->id, $user->id)) { |
2751 | echo 'teacher'; |
2752 | } else { |
2753 | echo 'user'; |
2754 | } |
2755 | echo $user->id.'" /> '; |
f19570d0 |
2756 | } |
2757 | |
951b22a8 |
2758 | echo '</td></tr></table>'; |
2759 | } |
2760 | |
d48b00b4 |
2761 | /** |
2762 | * Print a specified group's avatar. |
2763 | * |
89dcb99d |
2764 | * @param group $group A {@link group} object representing a group |
2765 | * @param int $courseid ? |
2766 | * @param boolean $large ? |
2767 | * @param boolean $returnstring ? |
2768 | * @param boolean $link ? |
2769 | * @return string |
d48b00b4 |
2770 | * @todo Finish documenting this function |
2771 | */ |
f2c80965 |
2772 | function print_group_picture($group, $courseid, $large=false, $returnstring=false, $link=true) { |
f374fb10 |
2773 | global $CFG; |
2774 | |
97ea4833 |
2775 | static $isteacheredit; |
2776 | |
2777 | if (!isset($isteacheredit)) { |
2778 | $isteacheredit = isteacheredit($courseid); |
2779 | } |
2780 | |
2781 | if ($group->hidepicture and !$isteacheredit) { |
3c0561cf |
2782 | return ''; |
2783 | } |
c3cbfe7f |
2784 | |
97ea4833 |
2785 | if ($link or $isteacheredit) { |
a756cf1d |
2786 | $output = '<a href="'. $CFG->wwwroot .'/user/index.php?id='. $courseid .'&group='. $group->id .'">'; |
3c0561cf |
2787 | } else { |
2788 | $output = ''; |
2789 | } |
2790 | if ($large) { |
b0ccd3fb |
2791 | $file = 'f1'; |
3c0561cf |
2792 | $size = 100; |
2793 | } else { |
b0ccd3fb |
2794 | $file = 'f2'; |
3c0561cf |
2795 | $size = 35; |
2796 | } |
2797 | if ($group->picture) { // Print custom group picture |
2798 | if ($CFG->slasharguments) { // Use this method if possible for better caching |
ed3136ff |
2799 | $output .= '<img class="grouppicture" align="middle" src="'.$CFG->wwwroot.'/user/pixgroup.php/'.$group->id.'/'.$file.'.jpg"'. |
2800 | ' border="0" width="'.$size.'" height="'.$size.'" alt="" title="'.s($group->name).'"/>'; |
f2c80965 |
2801 | } else { |
ed3136ff |
2802 | $output .= '<img class="grouppicture" align="middle" src="'.$CFG->wwwroot.'/user/pixgroup.php?file=/'.$group->id.'/'.$file.'.jpg"'. |
2803 | ' border="0" width="'.$size.'" height="'.$size.'" alt="" title="'.s($group->name).'"/>'; |
f2c80965 |
2804 | } |
f374fb10 |
2805 | } |
97ea4833 |
2806 | if ($link or $isteacheredit) { |
b0ccd3fb |
2807 | $output .= '</a>'; |
3c0561cf |
2808 | } |
f374fb10 |
2809 | |
2810 | if ($returnstring) { |
2811 | return $output; |
2812 | } else { |
2813 | echo $output; |
2814 | } |
2815 | } |
2816 | |
d48b00b4 |
2817 | /** |
2818 | * Print a png image. |
2819 | * |
89dcb99d |
2820 | * @param string $url ? |
2821 | * @param int $sizex ? |
2822 | * @param int $sizey ? |
2823 | * @param boolean $returnstring ? |
2824 | * @param string $parameters ? |
d48b00b4 |
2825 | * @todo Finish documenting this function |
2826 | */ |
35067c43 |
2827 | function print_png($url, $sizex, $sizey, $returnstring, $parameters='alt=""') { |
2828 | global $CFG; |
2829 | static $recentIE; |
2830 | |
2831 | if (!isset($recentIE)) { |
2832 | $recentIE = check_browser_version('MSIE', '5.0'); |
2833 | } |
2834 | |
2835 | if ($recentIE) { // work around the HORRIBLE bug IE has with alpha transparencies |
89dcb99d |
2836 | $output .= '<img src="'. $CFG->pixpath .'/spacer.gif" width="'. $sizex .'" height="'. $sizey .'"'. |
430c6392 |
2837 | ' border="0" class="png" style="width: '. $sizex .'px; height: '. $sizey .'px; '. |
89dcb99d |
2838 | ' filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='. |
2839 | "'$url', sizingMethod='scale') ". |
2840 | ' '. $parameters .' />'; |
35067c43 |
2841 | } else { |
89dcb99d |
2842 | $output .= '<img src="'. $url .'" border="0" width="'. $sizex .'" height="'. $sizey .'" '. |
2843 | ' '. $parameters .' />'; |
35067c43 |
2844 | } |
2845 | |
2846 | if ($returnstring) { |
2847 | return $output; |
2848 | } else { |
2849 | echo $output; |
2850 | } |
2851 | } |
2852 | |
d48b00b4 |
2853 | /** |
2854 | * Print a nicely formatted table. |
2855 | * |
89dcb99d |
2856 | * @param array $table is an object with several properties. |
2857 | * <ul<li>$table->head - An array of heading names. |
2858 | * <li>$table->align - An array of column alignments |
2859 | * <li>$table->size - An array of column sizes |
2860 | * <li>$table->wrap - An array of "nowrap"s or nothing |
2861 | * <li>$table->data[] - An array of arrays containing the data. |
2862 | * <li>$table->width - A percentage of the page |
2863 | * <li>$table->cellpadding - Padding on each cell |
2864 | * <li>$table->cellspacing - Spacing between cells |
2865 | * </ul> |
2866 | * @return boolean |
d48b00b4 |
2867 | * @todo Finish documenting this function |
2868 | */ |
9fa49e22 |
2869 | function print_table($table) { |
9fa49e22 |
2870 | |
2871 | if (isset($table->align)) { |
2872 | foreach ($table->align as $key => $aa) { |
2873 | if ($aa) { |
b0ccd3fb |
2874 | $align[$key] = ' align="'. $aa .'"'; |
9fa49e22 |
2875 | } else { |
b0ccd3fb |
2876 | $align[$key] = ''; |
9fa49e22 |
2877 | } |
2878 | } |
2879 | } |
2880 | if (isset($table->size)) { |
2881 | foreach ($table->size as $key => $ss) { |
2882 | if ($ss) { |
b0ccd3fb |
2883 | $size[$key] = ' width="'. $ss .'"'; |
9fa49e22 |
2884 | } else { |
b0ccd3fb |
2885 | $size[$key] = ''; |
9fa49e22 |
2886 | } |
2887 | } |
2888 | } |
5867bfb5 |
2889 | if (isset($table->wrap)) { |
2890 | foreach ($table->wrap as $key => $ww) { |
2891 | if ($ww) { |
b0ccd3fb |
2892 | $wrap[$key] = ' nowrap="nowrap" '; |
5867bfb5 |
2893 | } else { |
b0ccd3fb |
2894 | $wrap[$key] = ''; |
5867bfb5 |
2895 | } |
2896 | } |
2897 | } |
9fa49e22 |
2898 | |
9d378732 |
2899 | if (empty($table->width)) { |
b0ccd3fb |
2900 | $table->width = '80%'; |
9fa49e22 |
2901 | } |
2902 | |
9d378732 |
2903 | if (empty($table->cellpadding)) { |
b0ccd3fb |
2904 | $table->cellpadding = '5'; |
9fa49e22 |
2905 | } |
2906 | |
9d378732 |
2907 | if (empty($table->cellspacing)) { |
b0ccd3fb |
2908 | $table->cellspacing = '1'; |
9fa49e22 |
2909 | } |
2910 | |
da17a899 |
2911 | if (empty($table->class)) { |
2912 | $table->class = 'generaltable'; |
2913 | } |
2914 | |
2915 | $tableid = empty($table->id) ? '' : 'id="'.$table->id.'"'; |
2916 | |
b0ccd3fb |
2917 | print_simple_box_start('center', $table->width, '#ffffff', 0); |
2918 | echo '<table width="100%" border="0" align="center" '; |
da17a899 |
2919 | echo " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"$table->class\" $tableid>\n"; |
9fa49e22 |
2920 | |
e21e20cf |
2921 | $countcols = 0; |
2922 | |
b79f41cd |
2923 | if (!empty($table->head)) { |
f150292f |
2924 | $countcols = count($table->head); |
b0ccd3fb |
2925 | echo '<tr>'; |
9fa49e22 |
2926 | foreach ($table->head as $key => $heading) { |
ab9f24ad |
2927 | |
9d378732 |
2928 | if (!isset($size[$key])) { |
b0ccd3fb |
2929 | $size[$key] = ''; |
ab9f24ad |
2930 | } |
9d378732 |
2931 | if (!isset($align[$key])) { |
b0ccd3fb |
2932 | $align[$key] = ''; |
ab9f24ad |
2933 | } |
591052a4 |
2934 | echo '<th valign="top" '. $align[$key].$size[$key] .' nowrap="nowrap" class="header c'.$key.'">'. $heading .'</th>'; |
9fa49e22 |
2935 | } |
b0ccd3fb |
2936 | echo '</tr>'."\n"; |
9fa49e22 |
2937 | } |
2938 | |
a1f8ff87 |
2939 | if (!empty($table->data)) { |
19505667 |
2940 | $oddeven = 1; |
2941 | foreach ($table->data as $key => $row) { |
2942 | $oddeven = $oddeven ? 0 : 1; |
2943 | echo '<tr class="r'.$oddeven.'">'."\n"; |
b0ccd3fb |
2944 | if ($row == 'hr' and $countcols) { |
2945 | echo '<td colspan="'. $countcols .'"><div class="tabledivider"></div></td>'; |
e21e20cf |
2946 | } else { /// it's a normal row of data |
2947 | foreach ($row as $key => $item) { |
2948 | if (!isset($size[$key])) { |
b0ccd3fb |
2949 | $size[$key] = ''; |
ab9f24ad |
2950 | } |
e21e20cf |
2951 | if (!isset($align[$key])) { |
b0ccd3fb |
2952 | $align[$key] = ''; |
ab9f24ad |
2953 | } |
e21e20cf |
2954 | if (!isset($wrap[$key])) { |
b0ccd3fb |
2955 | $wrap[$key] = ''; |
ab9f24ad |
2956 | } |
591052a4 |
2957 | echo '<td '. $align[$key].$size[$key].$wrap[$key] .' class="cell c'.$key.'">'. $item .'</td>'; |
e21e20cf |
2958 | } |
a1f8ff87 |
2959 | } |
b0ccd3fb |
2960 | echo '</tr>'."\n"; |
9fa49e22 |
2961 | } |
9fa49e22 |
2962 | } |
b0ccd3fb |
2963 | echo '</table>'."\n"; |
9fa49e22 |
2964 | print_simple_box_end(); |
2965 | |
2966 | return true; |
2967 | } |
2968 | |
d48b00b4 |
2969 | /** |
2970 | * Creates a nicely formatted table and returns it. |
2971 | * |
89dcb99d |
2972 | * @param array $table is an object with several properties. |
2973 | * <ul<li>$table->head - An array of heading names. |
2974 | * <li>$table->align - An array of column alignments |
2975 | * <li>$table->size - An array of column sizes |
2976 | * <li>$table->wrap - An array of "nowrap"s or nothing |
2977 | * <li>$table->data[] - An array of arrays containing the data. |
2978 | * <li>$table->class - A css class name |
2979 | * <li>$table->fontsize - Is the size of all the text |
2980 | * <li>$table->tablealign - Align the whole table |
2981 | * <li>$table->width - A percentage of the page |
2982 | * <li>$table->cellpadding - Padding on each cell |
2983 | * <li>$table->cellspacing - Spacing between cells |
2984 | * </ul> |
2985 | * @return string |
d48b00b4 |
2986 | * @todo Finish documenting this function |
2987 | */ |
2f4d324b |
2988 | function make_table($table) { |
2f4d324b |
2989 | |
2990 | if (isset($table->align)) { |
2991 | foreach ($table->align as $key => $aa) { |
2992 | if ($aa) { |
b0ccd3fb |
2993 | $align[$key] = ' align="'. $aa .'"'; |
2f4d324b |
2994 | } else { |
b0ccd3fb |
2995 | $align[$key] = ''; |
2f4d324b |
2996 | } |
2997 | } |
2998 | } |
2999 | if (isset($table->size)) { |
3000 | foreach ($table->size as $key => $ss) { |
3001 | if ($ss) { |
b0ccd3fb |
3002 | $size[$key] = ' width="'. $ss .'"'; |
2f4d324b |
3003 | } else { |
b0ccd3fb |
3004 | $size[$key] = ''; |
2f4d324b |
3005 | } |
3006 | } |
3007 | } |
3008 | if (isset($table->wrap)) { |
3009 | foreach ($table->wrap as $key => $ww) { |
3010 | if ($ww) { |
b0ccd3fb |
3011 | $wrap[$key] = ' nowrap="nowrap" '; |
2f4d324b |
3012 | } else { |
b0ccd3fb |
3013 | $wrap[$key] = ''; |
2f4d324b |
3014 | } |
3015 | } |
3016 | } |
3017 | |
3018 | if (empty($table->width)) { |
b0ccd3fb |
3019 | $table->width = '80%'; |
2f4d324b |
3020 | } |
3021 | |
3022 | if (empty($table->tablealign)) { |
b0ccd3fb |
3023 | $table->tablealign = 'center'; |
2f4d324b |
3024 | } |
3025 | |
3026 | if (empty($table->cellpadding)) { |
b0ccd3fb |
3027 | $table->cellpadding = '5'; |
2f4d324b |
3028 | } |
3029 | |
3030 | if (empty($table->cellspacing)) { |
b0ccd3fb |
3031 | $table->cellspacing = '1'; |
2f4d324b |
3032 | } |
3033 | |
3034 | if (empty($table->class)) { |
b0ccd3fb |
3035 | $table->class = 'generaltable'; |
2f4d324b |
3036 | } |
3037 | |
3038 | if (empty($table->fontsize)) { |
b0ccd3fb |
3039 | $fontsize = ''; |
2f4d324b |
3040 | } else { |
b0ccd3fb |
3041 | $fontsize = '<font size="'. $table->fontsize .'">'; |
2f4d324b |
3042 | } |
3043 | |
5aeaeb57 |
3044 | $output = '<table width="'. $table->width .'" align="'. $table->tablealign .'" '; |
b0ccd3fb |
3045 | $output .= ' cellpadding="'. $table->cellpadding .'" cellspacing="'. $table->cellspacing .'" class="'. $table->class .'">'."\n"; |
2f4d324b |
3046 | |
3047 | if (!empty($table->head)) { |
5aeaeb57 |
3048 | $output .= '<tr valign="top">'; |
2f4d324b |
3049 | foreach ($table->head as $key => $heading) { |
3050 | if (!isset($size[$key])) { |
b0ccd3fb |
3051 | $size[$key] = ''; |
ab9f24ad |
3052 | } |
2f4d324b |
3053 | if (!isset($align[$key])) { |
b0ccd3fb |
3054 | $align[$key] = ''; |
ab9f24ad |
3055 | } |
b0ccd3fb |
3056 | $output .= '<th valign="top" '. $align[$key].$size[$key] .' nowrap="nowrap" class="'. $table->class .'header">'.$fontsize.$heading.'</th>'; |
2f4d324b |
3057 | } |
b0ccd3fb |
3058 | $output .= '</tr>'."\n"; |
2f4d324b |
3059 | } |
3060 | |
3061 | foreach ($table->data as $row) { |
b0ccd3fb |
3062 | $output .= '<tr valign="top">'; |
2f4d324b |
3063 | foreach ($row as $key => $item) { |
3064 | if (!isset($size[$key])) { |
b0ccd3fb |
3065 | $size[$key] = ''; |
ab9f24ad |
3066 | } |
2f4d324b |
3067 | if (!isset($align[$key])) { |
b0ccd3fb |
3068 | $align[$key] = ''; |
ab9f24ad |
3069 | } |
2f4d324b |
3070 | if (!isset($wrap[$key])) { |
b0ccd3fb |
3071 | $wrap[$key] = ''; |
ab9f24ad |
3072 | } |
b0ccd3fb |
3073 | $output .= '<td '. $align[$key].$size[$key].$wrap[$key] .' class="'. $table->class .'cell">'. $fontsize . $item .'</td>'; |
2f4d324b |
3074 | } |
b0ccd3fb |
3075 | $output .= '</tr>'."\n"; |
2f4d324b |
3076 | } |
b0ccd3fb |
3077 | $output .= '</table>'."\n"; |
2f4d324b |
3078 | |
3079 | return $output; |
3080 | } |
3081 | |
8f7dc7f1 |
3082 | function print_recent_activity_note($time, $user, $isteacher, $text, $link) { |
3083 | static $strftimerecent; |
3084 | |
3085 | if (empty($strftimerecent)) { |
3086 | $strftimerecent = get_string('strftimerecent'); |
3087 | } |
3088 | |
3089 | $date = userdate($time, $strftimerecent); |
3090 | $name = fullname($user, $isteacher); |
3091 | |
3092 | echo '<div class="head">'; |
a169b5ee |
3093 | echo '<div class="date">'.$date.'</div> '. |
3094 | '<div class="name">'.fullname($user, $isteacher).'</div>'; |
8f7dc7f1 |
3095 | echo '</div>'; |
188e4dd9 |
3096 | echo '<div class="info"><a href="'.$link.'">'.format_string($text,true).'</a></div>'; |
8f7dc7f1 |
3097 | } |
3098 | |
3099 | |
d48b00b4 |
3100 | /** |
3101 | * Prints a basic textarea field. |
3102 | * |
89dcb99d |
3103 | * @uses $CFG |
3104 | * @param boolean $usehtmleditor ? |
3105 | * @param int $rows ? |
3106 | * @param int $cols ? |
ed3136ff |
3107 | * @param null $width <b>Legacy field no longer used!</b> Set to zero to get control over mincols |
3108 | * @param null $height <b>Legacy field no longer used!</b> Set to zero to get control over minrows |
89dcb99d |
3109 | * @param string $name ? |
3110 | * @param string $value ? |
3111 | * @param int $courseid ? |
d48b00b4 |
3112 | * @todo Finish documenting this function |
3113 | */ |
b0ccd3fb |
3114 | function print_textarea($usehtmleditor, $rows, $cols, $width, $height, $name, $value='', $courseid=0) { |
bc01a2b8 |
3115 | /// $width and height are legacy fields and no longer used as pixels like they used to be. |
3116 | /// However, you can set them to zero to override the mincols and minrows values below. |
4c46c425 |
3117 | |
47037513 |
3118 | global $CFG, $course; |
572fe9ab |
3119 | static $scriptcount; // For loading the htmlarea script only once. |
50bdc74d |
3120 | |
bc01a2b8 |
3121 | $mincols = 65; |
3122 | $minrows = 10; |
3123 | |
408e62f8 |
3124 | if (empty($courseid)) { |
50bdc74d |
3125 | if (!empty($course->id)) { // search for it in global context |
3126 | $courseid = $course->id; |
3127 | } |
3128 | } |
9fa49e22 |
3129 | |
572fe9ab |
3130 | if (empty($scriptcount)) { |
3131 | $scriptcount = 0; |
3132 | } |
3133 | |
47037513 |
3134 | if ($usehtmleditor) { |
d046ae55 |
3135 | |
47037513 |
3136 | if (!empty($courseid) and isteacher($courseid)) { |
572fe9ab |
3137 | echo ($scriptcount < 1) ? '<script type="text/javascript" src="'. $CFG->wwwroot .'/lib/editor/htmlarea.php?id='. $courseid .'"></script>'."\n" : ''; |
47037513 |
3138 | } else { |
572fe9ab |
3139 | echo ($scriptcount < 1) ? '<script type="text/javascript" src="'. $CFG->wwwroot .'/lib/editor/htmlarea.php"></script>'."\n" : ''; |
47037513 |
3140 | } |
572fe9ab |
3141 | echo ($scriptcount < 1) ? '<script type="text/javascript" src="'. $CFG->wwwroot .'/lib/editor/lang/en.php"></script>'."\n" : ''; |
3142 | $scriptcount++; |
50bdc74d |
3143 | |
ed3136ff |
3144 | if ($height) { // Usually with legacy calls |
bc01a2b8 |
3145 | if ($rows < $minrows) { |
3146 | $rows = $minrows; |
ed3136ff |
3147 | } |
47037513 |
3148 | } |
ed3136ff |
3149 | if ($width) { // Usually with legacy calls |
bc01a2b8 |
3150 | if ($cols < $mincols) { |
3151 | $cols = $mincols; |
ed3136ff |
3152 | } |
4c46c425 |
3153 | } |
9fa49e22 |
3154 | } |
47037513 |
3155 | |
fd9d1ff3 |
3156 | echo '<textarea id="edit-'. $name .'" name="'. $name .'" rows="'. $rows .'" cols="'. $cols .'">'; |
67afdb11 |
3157 | if ($usehtmleditor) { |
3158 | echo htmlspecialchars(stripslashes_safe($value)); // needed for editing of cleaned text! |
3159 | } else { |
3160 | p ($value); |
3161 | } |
b0ccd3fb |
3162 | echo '</textarea>'."\n"; |
9fa49e22 |
3163 | } |
3164 | |
d48b00b4 |
3165 | /** |
772e78be |
3166 | * Legacy function, provided for backward compatability. |
d48b00b4 |
3167 | * This method now simply calls {@link use_html_editor()} |
3168 | * |
89dcb99d |
3169 | * @deprecated Use {@link use_html_editor()} instead. |
3170 | * @param string $name Form element to replace with HTMl editor by name |
d48b00b4 |
3171 | * @todo Finish documenting this function |
3172 | */ |
b0ccd3fb |
3173 | function print_richedit_javascript($form, $name, $source='no') { |
47037513 |
3174 | use_html_editor($name); |
3175 | } |
3176 | |
d48b00b4 |
3177 | /** |
3178 | * Sets up the HTML editor on textareas in the current page. |
3179 | * If a field name is provided, then it will only be |
3180 | * applied to that field - otherwise it will be used |
3181 | * on every textarea in the page. |
3182 | * |
3183 | * In most cases no arguments need to be supplied |
3184 | * |
89dcb99d |
3185 | * @param string $name Form element to replace with HTMl editor by name |
d48b00b4 |
3186 | */ |
ed3136ff |
3187 | function use_html_editor($name='', $editorhidebuttons='') { |
b0ccd3fb |
3188 | echo '<script language="javascript" type="text/javascript" defer="defer">'."\n"; |
ed3136ff |
3189 | print_editor_config($editorhidebuttons); |
47037513 |
3190 | if (empty($name)) { |
b0ccd3fb |
3191 | echo "\n".'HTMLArea.replaceAll(config);'."\n"; |
47037513 |
3192 | } else { |
a2b3f884 |
3193 | echo "\nHTMLArea.replace('edit-$name', config);\n"; |
4c46c425 |
3194 | } |
b0ccd3fb |
3195 | echo '</script>'."\n"; |
9fa49e22 |
3196 | } |
3197 | |
d48b00b4 |
3198 | /** |
3199 | * Returns a turn edit on/off button for course in a self contained form. |
3200 | * Used to be an icon, but it's now a simple form button |
3201 | * |
89dcb99d |
3202 | * @uses $CFG |
3203 | * @uses $USER |
3204 | * @param int $courseid The course to update by id as found in 'course' table |
3205 | * @return string |
d48b00b4 |
3206 | */ |
9fa49e22 |
3207 | function update_course_icon($courseid) { |
772e78be |
3208 | |
9fa49e22 |
3209 | global $CFG, $USER; |
3210 | |
b6c12732 |
3211 | if (isteacheredit($courseid)) { |
9c9f7d77 |
3212 | if (!empty($USER->editing)) { |
b0ccd3fb |
3213 | $string = get_string('turneditingoff'); |
3214 | $edit = 'off'; |
9fa49e22 |
3215 | } else { |
b0ccd3fb |
3216 | $string = get_string('turneditingon'); |
3217 | $edit = 'on'; |
9fa49e22 |
3218 | } |
60b9a565 |
3219 | return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/view.php\">". |
367b8413 |
3220 | "<input type=\"hidden\" name=\"id\" value=\"$courseid\" />". |
3221 | "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />". |
3222 | "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />". |
3223 | "<input type=\"submit\" value=\"$string\" /></form>"; |
3224 | } |
3225 | } |
3226 | |
3227 | /** |
3228 | * Returns a turn student view on/off button for course in a self contained form. |
3229 | * |
3230 | * @uses $CFG |
3231 | * @uses $USER |
|