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