Small typo when creating structures in a fresh installation
[moodle.git] / lib / weblib.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
9fa49e22 3///////////////////////////////////////////////////////////////////////////
4// weblib.php - functions for web output
f9903ed0 5//
9fa49e22 6// Library of all general-purpose Moodle PHP functions and constants
7// that produce HTML output
f9903ed0 8//
9fa49e22 9///////////////////////////////////////////////////////////////////////////
10// //
11// NOTICE OF COPYRIGHT //
12// //
13// Moodle - Modular Object-Oriented Dynamic Learning Environment //
14// http://moodle.com //
15// //
16// Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com //
17// //
18// This program is free software; you can redistribute it and/or modify //
19// it under the terms of the GNU General Public License as published by //
20// the Free Software Foundation; either version 2 of the License, or //
21// (at your option) any later version. //
22// //
23// This program is distributed in the hope that it will be useful, //
24// but WITHOUT ANY WARRANTY; without even the implied warranty of //
25// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
26// GNU General Public License for more details: //
27// //
28// http://www.gnu.org/copyleft/gpl.html //
29// //
30///////////////////////////////////////////////////////////////////////////
f9903ed0 31
0095d5cd 32/// Constants
33
c1d57101 34/// Define text formatting types ... eventually we can add Wiki, BBcode etc
6901fa79 35define("FORMAT_MOODLE", "0"); // Does all sorts of transformations and filtering
d342c763 36define("FORMAT_HTML", "1"); // Plain HTML (with some tags stripped)
37define("FORMAT_PLAIN", "2"); // Plain text (even tags are printed in full)
38define("FORMAT_WIKI", "3"); // Wiki-formatted text
0095d5cd 39
ef5db724 40$ALLOWED_TAGS = "<p><br><b><i><u><font><table><tbody><span><div><tr><td><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>";
3fe3851d 41
42
0095d5cd 43/// Functions
44
3662bce5 45function s($var) {
c1d57101 46/// returns $var with HTML characters (like "<", ">", etc.) properly quoted,
f9903ed0 47
3662bce5 48 if (empty($var)) {
49 return "";
50 }
7d8f674d 51 return htmlSpecialChars(stripslashes_safe($var));
f9903ed0 52}
53
3662bce5 54function p($var) {
c1d57101 55/// prints $var with HTML characters (like "<", ">", etc.) properly quoted,
f9903ed0 56
3662bce5 57 if (empty($var)) {
58 echo "";
59 }
7d8f674d 60 echo htmlSpecialChars(stripslashes_safe($var));
f9903ed0 61}
62
8553b700 63function nvl(&$var, $default="") {
c1d57101 64/// if $var is undefined, return $default, otherwise return $var
8553b700 65
66 return isset($var) ? $var : $default;
67}
f9903ed0 68
69function strip_querystring($url) {
c1d57101 70/// takes a URL and returns it without the querystring portion
f9903ed0 71
b9b8ab69 72 if ($commapos = strpos($url, '?')) {
73 return substr($url, 0, $commapos);
74 } else {
75 return $url;
76 }
f9903ed0 77}
78
79function get_referer() {
c1d57101 80/// returns the URL of the HTTP_REFERER, less the querystring portion
f9903ed0 81
607809b3 82 return strip_querystring(nvl($_SERVER["HTTP_REFERER"]));
f9903ed0 83}
84
c1d57101 85
f9903ed0 86function me() {
c1d57101 87/// returns the name of the current script, WITH the querystring portion.
eaa50dbc 88/// this function is necessary because PHP_SELF and REQUEST_URI and SCRIPT_NAME
c1d57101 89/// return different things depending on a lot of things like your OS, Web
90/// server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.)
f9903ed0 91
607809b3 92 if (!empty($_SERVER["REQUEST_URI"])) {
93 return $_SERVER["REQUEST_URI"];
c1d57101 94
607809b3 95 } else if (!empty($_SERVER["PHP_SELF"])) {
fced815c 96 if (!empty($_SERVER["QUERY_STRING"])) {
97 return $_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"];
98 }
607809b3 99 return $_SERVER["PHP_SELF"];
c1d57101 100
fced815c 101 } else if (!empty($_SERVER["SCRIPT_NAME"])) {
102 if (!empty($_SERVER["QUERY_STRING"])) {
103 return $_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
104 }
105 return $_SERVER["SCRIPT_NAME"];
106
b9b8ab69 107 } else {
fced815c 108 notify("Warning: Could not find any of these web server variables: \$REQUEST_URI, \$PHP_SELF or \$SCRIPT_NAME");
bcdfe14e 109 return false;
7fbd6b1c 110 }
f9903ed0 111}
112
113
f9903ed0 114function qualified_me() {
c1d57101 115/// like me() but returns a full URL
f9903ed0 116
39e018b3 117 if (!empty($_SERVER["HTTP_HOST"])) {
118 $hostname = $_SERVER["HTTP_HOST"];
119 } else if (!empty($_ENV["HTTP_HOST"])) {
120 $hostname = $_ENV["HTTP_HOST"];
df3fd249 121 } else if (!empty($_SERVER["SERVER_NAME"])) {
122 $hostname = $_SERVER["SERVER_NAME"];
39e018b3 123 } else if (!empty($_ENV["SERVER_NAME"])) {
124 $hostname = $_ENV["SERVER_NAME"];
125 } else {
126 notify("Warning: could not find the name of this server!");
bcdfe14e 127 return false;
c1d57101 128 }
f9903ed0 129
607809b3 130 $protocol = (isset($_SERVER["HTTPS"]) and $_SERVER["HTTPS"] == "on") ? "https://" : "http://";
39e018b3 131 $url_prefix = $protocol.$hostname;
b9b8ab69 132 return $url_prefix . me();
f9903ed0 133}
134
135
a0deb5db 136function match_referer($goodreferer = "") {
137/// returns true if the referer is the same as the goodreferer. If
138/// goodreferer is not specified, use qualified_me as the goodreferer
60f18531 139 global $CFG;
140
ae384ef1 141 if (empty($CFG->secureforms)) { // Don't bother checking referer
60f18531 142 return true;
143 }
f9903ed0 144
ae384ef1 145 if ($goodreferer == "nomatch") { // Don't bother checking referer
a0deb5db 146 return true;
147 }
148
149 if (empty($goodreferer)) {
150 $goodreferer = qualified_me();
c1d57101 151 }
a0deb5db 152 return $goodreferer == get_referer();
f9903ed0 153}
154
36b4f985 155function data_submitted($url="") {
156/// Used on most forms in Moodle to check for data
157/// Returns the data as an object, if it's found.
607809b3 158/// This object can be used in foreach loops without
159/// casting because it's cast to (array) automatically
36b4f985 160///
161/// Checks that submitted POST data exists, and also
162/// checks the referer against the given url (it uses
163/// the current page if none was specified.
164
37208cd2 165 global $CFG;
166
607809b3 167 if (empty($_POST)) {
36b4f985 168 return false;
607809b3 169
36b4f985 170 } else {
171 if (match_referer($url)) {
607809b3 172 return (object)$_POST;
36b4f985 173 } else {
174 if ($CFG->debug > 10) {
175 notice("The form did not come from this page! (referer = ".get_referer().")");
176 }
177 return false;
178 }
179 }
180}
181
7d8f674d 182function stripslashes_safe($string) {
183/// stripslashes() removes ALL backslashes even from strings
184/// so C:\temp becomes C:temp ... this isn't good.
185/// The following should work as a fairly safe replacement
186/// to be called on quoted AND unquoted strings (to be sure)
187
188 $string = str_replace("\\'", "'", $string);
189 $string = str_replace('\\"', '"', $string);
190 $string = str_replace('\\\\', '\\', $string);
191 return $string;
192}
f9903ed0 193
72e4eac6 194if (!function_exists('str_ireplace')) {
195 function str_ireplace($find, $replace, $string ) {
196 /// This does a search and replace, ignoring case
197 /// This function is only here because one doesn't exist yet in PHP
198 /// Unlike str_replace(), this only works on single values (not arrays)
199
200 $parts = explode(strtolower($find), strtolower($string));
201
202 $pos = 0;
203
204 foreach ($parts as $key => $part) {
205 $parts[$key] = substr($string, $pos, strlen($part));
206 $pos += strlen($part) + strlen($find);
207 }
208
209 return (join($replace, $parts));
3fe3851d 210 }
3fe3851d 211}
212
f9903ed0 213function read_template($filename, &$var) {
c1d57101 214/// return a (big) string containing the contents of a template file with all
215/// the variables interpolated. all the variables must be in the $var[] array or
216/// object (whatever you decide to use).
217///
218/// WARNING: do not use this on big files!!
f9903ed0 219
b9b8ab69 220 $temp = str_replace("\\", "\\\\", implode(file($filename), ""));
221 $temp = str_replace('"', '\"', $temp);
222 eval("\$template = \"$temp\";");
223 return $template;
f9903ed0 224}
225
226function checked(&$var, $set_value = 1, $unset_value = 0) {
c1d57101 227/// if variable is set, set it to the set_value otherwise set it to the
228/// unset_value. used to handle checkboxes when you are expecting them from
229/// a form
f9903ed0 230
b9b8ab69 231 if (empty($var)) {
232 $var = $unset_value;
233 } else {
234 $var = $set_value;
235 }
f9903ed0 236}
237
238function frmchecked(&$var, $true_value = "checked", $false_value = "") {
c1d57101 239/// prints the word "checked" if a variable is true, otherwise prints nothing,
240/// used for printing the word "checked" in a checkbox form input
f9903ed0 241
b9b8ab69 242 if ($var) {
243 echo $true_value;
244 } else {
245 echo $false_value;
246 }
f9903ed0 247}
248
249
65cf9fc3 250function link_to_popup_window ($url, $name="popup", $linkname="click here", $height=400, $width=500, $title="Popup window") {
c1d57101 251/// This will create a HTML link that will work on both
252/// Javascript and non-javascript browsers.
253/// Relies on the Javascript function openpopup in javascript.php
254/// $url must be relative to home page eg /mod/survey/stuff.php
f9903ed0 255
ff80e012 256 global $CFG;
257
76c1650d 258 echo "\n<script language=\"javascript\">";
f9903ed0 259 echo "\n<!--";
aaa2ebbb 260 echo "\ndocument.write('<a title=\"".addslashes($title)."\" href=javascript:openpopup(\"$url\",\"$name\",\"$height\",\"$width\") >".addslashes($linkname)."</a>');";
f9903ed0 261 echo "\n//-->";
76c1650d 262 echo "\n</script>";
263 echo "\n<noscript>\n<a target=\"$name\" title=\"$title\" href=\"$CFG->wwwroot/$url\">$linkname</a>\n</noscript>\n";
f9903ed0 264
265}
266
267function close_window_button() {
c1d57101 268/// Prints a simple button to close a window
269
76c1650d 270 echo "<form><center>";
271 echo "<input type=button onClick=\"self.close();\" value=\"".get_string("closewindow")."\">";
272 echo "</center></form>";
f9903ed0 273}
274
275
08056730 276function choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", $nothingvalue="0", $return=false) {
c1d57101 277/// Given an array of value, creates a popup menu to be part of a form
278/// $options["value"]["label"]
f9903ed0 279
618b22c5 280 if ($nothing == "choose") {
281 $nothing = get_string("choose")."...";
282 }
283
f9903ed0 284 if ($script) {
285 $javascript = "onChange=\"$script\"";
9c9f7d77 286 } else {
287 $javascript = "";
f9903ed0 288 }
9c9f7d77 289
76c1650d 290 $output = "<select name=$name $javascript>\n";
bda8d43a 291 if ($nothing) {
76c1650d 292 $output .= " <option value=\"$nothingvalue\"\n";
bda8d43a 293 if ($nothingvalue == $selected) {
76c1650d 294 $output .= " selected";
bda8d43a 295 }
76c1650d 296 $output .= ">$nothing</option>\n";
873960de 297 }
607809b3 298 if (!empty($options)) {
299 foreach ($options as $value => $label) {
76c1650d 300 $output .= " <option value=\"$value\"";
607809b3 301 if ($value == $selected) {
76c1650d 302 $output .= " selected";
607809b3 303 }
304 if ($label) {
76c1650d 305 $output .= ">$label</option>\n";
607809b3 306 } else {
76c1650d 307 $output .= ">$value</option>\n";
607809b3 308 }
f9903ed0 309 }
310 }
76c1650d 311 $output .= "</select>\n";
08056730 312
313 if ($return) {
314 return $output;
315 } else {
316 echo $output;
317 }
f9903ed0 318}
319
d897cae4 320function popup_form ($common, $options, $formname, $selected="", $nothing="choose", $help="", $helptext="", $return=false) {
c1d57101 321/// Implements a complete little popup form
322/// $common = the URL up to the point of the variable that changes
323/// $options = A list of value-label pairs for the popup list
324/// $formname = name must be unique on the page
325/// $selected = the option that is already selected
326/// $nothing = The label for the "no choice" option
e5dfd0f3 327/// $help = The name of a help page if help is required
328/// $helptext = The name of the label for the help button
f9903ed0 329
0d0baabf 330 global $CFG;
331
618b22c5 332 if ($nothing == "choose") {
333 $nothing = get_string("choose")."...";
334 }
335
dfec7b01 336 $startoutput = "<form target=\"{$CFG->framename}\" name=$formname>";
2bc269fd 337 $output = "<select name=popup onchange=\"top.location=document.$formname.popup.options[document.$formname.popup.selectedIndex].value\">\n";
f9903ed0 338
339 if ($nothing != "") {
dfec7b01 340 $output .= " <option value=\"javascript:void(0)\">$nothing</option>\n";
f9903ed0 341 }
342
343 foreach ($options as $value => $label) {
d897cae4 344 if (substr($label,0,1) == "-") {
dfec7b01 345 $output .= " <option value=\"\"";
d897cae4 346 } else {
dfec7b01 347 $output .= " <option value=\"$common$value\"";
d897cae4 348 if ($value == $selected) {
dfec7b01 349 $output .= " selected";
d897cae4 350 }
f9903ed0 351 }
352 if ($label) {
dfec7b01 353 $output .= ">$label</option>\n";
f9903ed0 354 } else {
dfec7b01 355 $output .= ">$value</option>\n";
f9903ed0 356 }
357 }
dfec7b01 358 $output .= "</select>";
359 $output .= "</form>\n";
d897cae4 360
361 if ($return) {
dfec7b01 362 return $startoutput.$output;
d897cae4 363 } else {
dfec7b01 364 echo $startoutput;
9c9f7d77 365 if ($help) {
366 helpbutton($help, $helptext);
367 }
d897cae4 368 echo $output;
369 }
f9903ed0 370}
371
372
373
374function formerr($error) {
c1d57101 375/// Prints some red text
f9903ed0 376 if (!empty($error)) {
377 echo "<font color=#ff0000>$error</font>";
378 }
379}
380
381
382function validate_email ($address) {
c1d57101 383/// Validates an email to make it makes sense.
f9903ed0 384 return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.
385 '@'.
386 '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
387 '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
388 $address));
389}
390
6c8e8b5e 391function detect_munged_arguments($string) {
393c9b4f 392 if (ereg('\.\.', $string)) { // check for parent URLs
6c8e8b5e 393 return true;
394 }
393c9b4f 395 if (ereg('[\|\`]', $string)) { // check for other bad characters
6c8e8b5e 396 return true;
397 }
398 return false;
399}
400
6ed3da1d 401function get_slash_arguments($file="file.php") {
402/// Searches the current environment variables for some slash arguments
f9903ed0 403
eaa50dbc 404 if (!$string = me()) {
f9903ed0 405 return false;
406 }
eaa50dbc 407
6ed3da1d 408 $pathinfo = explode($file, $string);
409
bcdfe14e 410 if (!empty($pathinfo[1])) {
411 return $pathinfo[1];
6ed3da1d 412 } else {
413 return false;
414 }
415}
416
417function parse_slash_arguments($string, $i=0) {
418/// Extracts arguments from "/foo/bar/something"
419/// eg http://mysite.com/script.php/foo/bar/something
f9903ed0 420
6c8e8b5e 421 if (detect_munged_arguments($string)) {
780db230 422 return false;
423 }
6ed3da1d 424 $args = explode("/", $string);
f9903ed0 425
426 if ($i) { // return just the required argument
427 return $args[$i];
428
429 } else { // return the whole array
430 array_shift($args); // get rid of the empty first one
431 return $args;
432 }
433}
434
0095d5cd 435function format_text_menu() {
c1d57101 436/// Just returns an array of formats suitable for a popup menu
0095d5cd 437 return array (FORMAT_MOODLE => get_string("formattext"),
6901fa79 438 FORMAT_HTML => get_string("formathtml"),
d342c763 439 FORMAT_PLAIN => get_string("formatplain"),
440 FORMAT_WIKI => get_string("formatwiki"));
0095d5cd 441}
442
60f18531 443function format_text($text, $format=FORMAT_MOODLE, $options=NULL) {
c1d57101 444/// Given text in a variety of format codings, this function returns
445/// the text as safe HTML.
446///
447/// $text is raw text (originally from a user)
448/// $format is one of the format constants, defined above
0095d5cd 449
450 switch ($format) {
73f8658c 451 case FORMAT_HTML:
5f350e8f 452 replace_smilies($text);
73f8658c 453 return $text;
454 break;
455
6901fa79 456 case FORMAT_PLAIN:
457 $text = htmlentities($text);
3405b212 458 $text = str_replace(" ", "&nbsp; ", $text);
5f350e8f 459 replace_smilies($text);
460 convert_urls_into_links($text);
6901fa79 461 $text = nl2br($text);
462 return $text;
463 break;
464
d342c763 465 case FORMAT_WIKI:
7f9bd7b9 466 return wiki_to_html($text);
d342c763 467 break;
468
73f8658c 469 default: // FORMAT_MOODLE or anything else
c9dda990 470 if (!isset($options->smiley)) {
471 $options->smiley=true;
472 }
473 if (!isset($options->para)) {
1a072208 474 $options->para=true;
c9dda990 475 }
0095d5cd 476 return text_to_html($text, $options->smiley, $options->para);
477 break;
0095d5cd 478 }
479}
480
d342c763 481function format_text_email($text, $format) {
482/// Given text in a variety of format codings, this function returns
483/// the text as plain text suitable for plain email.
484///
485/// $text is raw text (originally from a user)
486/// $format is one of the format constants, defined above
487
488 switch ($format) {
489
490 case FORMAT_PLAIN:
491 return $text;
492 break;
493
494 case FORMAT_WIKI:
495 $text = wiki_to_html($text);
496 return strip_tags($text);
497 break;
498
499 default: // FORMAT_MOODLE or anything else
500 // Need to add something in here to create a text-friendly way of presenting URLs
501 return strip_tags($text);
502 break;
503 }
504}
0095d5cd 505
506function clean_text($text, $format) {
c1d57101 507/// Given raw text (eg typed in by a user), this function cleans it up
508/// and removes any nasty tags that could mess up Moodle pages.
b7a3cf49 509
fc120758 510 global $ALLOWED_TAGS;
3fe3851d 511
d342c763 512 switch ($format) {
0095d5cd 513 case FORMAT_MOODLE:
0095d5cd 514 case FORMAT_HTML:
d342c763 515 case FORMAT_WIKI:
3fe3851d 516 $text = strip_tags($text, $ALLOWED_TAGS);
71e8bd81 517 $text = str_ireplace("javascript:", "xxx", $text); // Remove javascript: label
518 $text = eregi_replace("([^a-z])language([[:space:]]*)=", "xxx", $text); // Remove javascript/VBScript
519 $text = eregi_replace("([^a-z])on([a-z]+)([[:space:]]*)=", "xxx", $text); // Remove script events
3fe3851d 520 return $text;
6901fa79 521
522 case FORMAT_PLAIN:
523 return $text;
0095d5cd 524 }
b7a3cf49 525}
f9903ed0 526
5f350e8f 527function replace_smilies(&$text) {
c1d57101 528/// Replaces all known smileys in the text with image equivalents
2ea9027b 529 global $CFG;
c1d57101 530
617778f2 531 static $runonce = false;
69081931 532 static $e = array();
533 static $img = array();
617778f2 534 static $emoticons = array(
2ea9027b 535 ':-)' => 'smiley.gif',
536 ':)' => 'smiley.gif',
537 ':-D' => 'biggrin.gif',
538 ';-)' => 'wink.gif',
539 ':-/' => 'mixed.gif',
540 'V-.' => 'thoughtful.gif',
541 ':-P' => 'tongueout.gif',
542 'B-)' => 'cool.gif',
543 '^-)' => 'approve.gif',
544 '8-)' => 'wideeyes.gif',
545 ':o)' => 'clown.gif',
546 ':-(' => 'sad.gif',
547 ':(' => 'sad.gif',
548 '8-.' => 'shy.gif',
549 ':-I' => 'blush.gif',
550 ':-X' => 'kiss.gif',
551 '8-o' => 'surprise.gif',
552 'P-|' => 'blackeye.gif',
553 '8-[' => 'angry.gif',
554 'xx-P' => 'dead.gif',
555 '|-.' => 'sleepy.gif',
556 '}-]' => 'evil.gif',
557 );
558
01d79966 559 if ($runonce == false){
617778f2 560 foreach ($emoticons as $emoticon => $image){
69081931 561 $e[] = $emoticon;
01d79966 562 $img[] = "<img alt=\"$emoticon\" width=15 height=15 src=\"$CFG->wwwroot/pix/s/$image\">";
617778f2 563 }
564 $runonce = true;
c0f728ba 565 }
b7a3cf49 566
5f350e8f 567 $text = str_replace($e, $img, $text);
1a072208 568}
0095d5cd 569
909f539d 570function text_to_html($text, $smiley=true, $para=true) {
c1d57101 571/// Given plain text, makes it into HTML as nicely as possible.
572/// May contain HTML tags already
f9903ed0 573
c1d57101 574/// Remove any whitespace that may be between HTML tags
7b3be1b1 575 $text = eregi_replace(">([[:space:]]+)<", "><", $text);
576
c1d57101 577/// Remove any returns that precede or follow HTML tags
0eae8049 578 $text = eregi_replace("([\n\r])<", " <", $text);
579 $text = eregi_replace(">([\n\r])", "> ", $text);
7b3be1b1 580
5f350e8f 581 convert_urls_into_links($text);
f9903ed0 582
c1d57101 583/// Make returns into HTML newlines.
f9903ed0 584 $text = nl2br($text);
585
c1d57101 586/// Turn smileys into images.
d69cb7f4 587 if ($smiley) {
5f350e8f 588 replace_smilies($text);
d69cb7f4 589 }
f9903ed0 590
c1d57101 591/// Wrap the whole thing in a paragraph tag if required
909f539d 592 if ($para) {
01d79966 593 return "<p>".$text."</p>";
909f539d 594 } else {
595 return $text;
596 }
f9903ed0 597}
598
3e9ca9fb 599function wiki_to_html($text) {
01d79966 600/// Given Wiki formatted text, make it into XHTML using external function
3e9ca9fb 601
01d79966 602 require_once('wiki.php');
3e9ca9fb 603
01d79966 604 $wiki = new Wiki;
605 return $wiki->format($text);
3e9ca9fb 606}
607
5f350e8f 608function convert_urls_into_links(&$text) {
609/// Given some text, it converts any URLs it finds into HTML links.
610
611/// Make lone URLs into links. eg http://moodle.com/
3405b212 612 $text = eregi_replace("([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
5f350e8f 613 "\\1<a href=\"\\2://\\3\\4\" TARGET=\"newpage\">\\2://\\3\\4</a>", $text);
614
615/// eg www.moodle.com
3405b212 616 $text = eregi_replace("([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])",
5f350e8f 617 "\\1<a href=\"http://www.\\2\\3\" TARGET=\"newpage\">www.\\2\\3</a>", $text);
618}
619
5af78ed2 620function highlight($needle, $haystack) {
c1d57101 621/// This function will highlight instances of $needle in $haystack
5af78ed2 622
623 $parts = explode(strtolower($needle), strtolower($haystack));
624
625 $pos = 0;
626
627 foreach ($parts as $key => $part) {
628 $parts[$key] = substr($haystack, $pos, strlen($part));
629 $pos += strlen($part);
630
631 $parts[$key] .= "<SPAN CLASS=highlight>".substr($haystack, $pos, strlen($needle))."</SPAN>";
632 $pos += strlen($needle);
633 }
634
635 return (join('', $parts));
636}
637
f9903ed0 638
9fa49e22 639
640/// STANDARD WEB PAGE PARTS ///////////////////////////////////////////////////
641
642function print_header ($title="", $heading="", $navigation="", $focus="", $meta="", $cache=true, $button="&nbsp;", $menu="") {
643// $title - appears top of window
644// $heading - appears top of page
645// $navigation - premade navigation string
646// $focus - indicates form element eg inputform.password
647// $meta - meta tags in the header
648// $cache - should this page be cacheable?
649// $button - HTML code for a button (usually for module editing)
650// $menu - HTML code for a popup menu
e825f279 651 global $USER, $CFG, $THEME, $SESSION;
9fa49e22 652
653 if (file_exists("$CFG->dirroot/theme/$CFG->theme/styles.php")) {
654 $styles = $CFG->stylesheet;
655 } else {
656 $styles = "$CFG->wwwroot/theme/standard/styles.php";
657 }
658
659 if ($navigation == "home") {
660 $home = true;
661 $navigation = "";
9d378732 662 } else {
663 $home = false;
9fa49e22 664 }
665
666 if ($button == "") {
667 $button = "&nbsp;";
668 }
669
670 if (!$menu and $navigation) {
671 if (isset($USER->id)) {
c6a0f9d5 672 $menu = "<font size=2><a target=\"$CFG->framename\" href=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</a></font>";
9fa49e22 673 } else {
c6a0f9d5 674 $menu = "<font size=2><a target=\"$CFG->framename\" href=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</a></font>";
9fa49e22 675 }
676 }
677
678 // Specify character set ... default is iso-8859-1 but some languages might need something else
679 // Could be optimised by carrying the charset variable around in $USER
680 if (current_language() == "en") {
107b010b 681 $meta = "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n$meta\n";
9fa49e22 682 } else {
107b010b 683 $meta = "<meta http-equiv=\"content-type\" content=\"text/html; charset=".get_string("thischarset")."\">\n$meta\n";
9fa49e22 684 }
685
3662bce5 686 if (!empty($CFG->langdir) and $CFG->langdir == "RTL") {
107b010b 687 $direction = " dir=\"rtl\"";
9fa49e22 688 } else {
107b010b 689 $direction = " dir=\"ltr\"";
9fa49e22 690 }
691
692 if (!$cache) { // Do everything we can to prevent clients and proxies caching
693 @header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
694 @header("Pragma: no-cache");
76c1650d 695 $meta .= "\n<meta http-equiv=\"pragma\" content=\"no-cache\">";
696 $meta .= "\n<meta http-equiv=\"expires\" content=\"0\">";
9fa49e22 697 }
698
699 include ("$CFG->dirroot/theme/$CFG->theme/header.html");
700}
701
702function print_footer ($course=NULL) {
703// Can provide a course object to make the footer contain a link to
704// to the course home page, otherwise the link will go to the site home
705 global $USER, $CFG, $THEME;
706
707
708/// Course links
709 if ($course) {
710 if ($course == "home") { // special case for site home page - please do not remove
76c1650d 711 $homelink = "<p align=\"center\"><a title=\"moodle $CFG->release ($CFG->version)\" href=\"http://moodle.org/\" target=\"_blank\">";
712 $homelink .= "<br><img width=\"130\" height=\"19\" src=\"pix/madewithmoodle2.gif\" border=0></a></p>";
9fa49e22 713 $course = get_site();
714 $homepage = true;
715 } else {
76c1650d 716 $homelink = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>";
9fa49e22 717 }
718 } else {
76c1650d 719 $homelink = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot\">".get_string("home")."</a>";
9fa49e22 720 $course = get_site();
721 }
722
723/// User links
a282d0ff 724 $loggedinas = user_login_string($course, $USER);
725
726 include ("$CFG->dirroot/theme/$CFG->theme/footer.html");
727}
728
729
730function user_login_string($course, $user=NULL) {
731 global $USER, $CFG;
732
8d2accb6 733 if (empty($user)) {
a282d0ff 734 $user = $USER;
735 }
736
737 if (isset($user->realuser)) {
738 if ($realuser = get_record("user", "id", $user->realuser)) {
ca16eaeb 739 $realuserinfo = " [<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&return=$realuser->id\">$realuser->firstname $realuser->lastname</A>] ";
9fa49e22 740 }
9d378732 741 } else {
742 $realuserinfo = "";
9fa49e22 743 }
744
a282d0ff 745 if (isset($user->id) and $user->id) {
ca16eaeb 746 $username = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</a>";
9fa49e22 747 $loggedinas = $realuserinfo.get_string("loggedinas", "moodle", "$username").
ca16eaeb 748 " (<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</a>)";
9fa49e22 749 } else {
750 $loggedinas = get_string("loggedinnot", "moodle").
ca16eaeb 751 " (<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</a>)";
9fa49e22 752 }
a282d0ff 753 return $loggedinas;
9fa49e22 754}
755
756
9fa49e22 757function print_navigation ($navigation) {
758 global $CFG;
759
760 if ($navigation) {
761 if (! $site = get_site()) {
762 $site->shortname = get_string("home");;
763 }
eb347b6b 764 echo "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/\">$site->shortname</a> -> $navigation";
9fa49e22 765 }
766}
767
76c1650d 768function print_heading($text, $align="center", $size=3) {
769 echo "<p align=\"$align\"><font size=\"$size\"><b>".stripslashes_safe($text)."</b></font></p>";
9fa49e22 770}
771
772function print_heading_with_help($text, $helppage, $module="moodle") {
773// Centered heading with attached help button (same title text)
eb347b6b 774 echo "<p align=\"center\"><font size=\"3\"><b>".stripslashes_safe($text);
9fa49e22 775 helpbutton($helppage, $text, $module);
eb347b6b 776 echo "</b></font></p>";
9fa49e22 777}
778
779function print_continue($link) {
9fa49e22 780
781 if (!$link) {
607809b3 782 $link = $_SERVER["HTTP_REFERER"];
9fa49e22 783 }
784
eb347b6b 785 print_heading("<a href=\"$link\">".get_string("continue")."</a>");
9fa49e22 786}
787
788
789function print_simple_box($message, $align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
790 print_simple_box_start($align, $width, $color, $padding, $class);
7d8f674d 791 echo stripslashes_safe($message);
9fa49e22 792 print_simple_box_end();
793}
794
795function print_simple_box_start($align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
796 global $THEME;
797
798 if ($align) {
76c1650d 799 $align = "align=\"$align\"";
9fa49e22 800 }
801 if ($width) {
76c1650d 802 $width = "width=\"$width\"";
9fa49e22 803 }
9d378732 804 echo "<table $align $width class=\"$class\" border=\"0\" cellpadding=\"$padding\" cellspacing=\"0\"><tr><td bgcolor=\"$color\" class=\"$class"."content\">";
9fa49e22 805}
806
807function print_simple_box_end() {
808 echo "</td></tr></table>";
809}
810
cc7fa0dc 811function print_single_button($link, $options, $label="OK", $method="get") {
812 echo "<form action=\"$link\" method=\"$method\">";
9fa49e22 813 if ($options) {
814 foreach ($options as $name => $value) {
76c1650d 815 echo "<input type=hidden name=\"$name\" value=\"$value\">";
9fa49e22 816 }
817 }
76c1650d 818 echo "<input type=submit value=\"$label\"></form>";
9fa49e22 819}
820
821function print_spacer($height=1, $width=1, $br=true) {
822 global $CFG;
76c1650d 823 echo "<img height=\"$height\" width=\"$width\" src=\"$CFG->wwwroot/pix/spacer.gif\" alt=\"\">";
9fa49e22 824 if ($br) {
76c1650d 825 echo "<br />\n";
9fa49e22 826 }
827}
828
829function print_file_picture($path, $courseid=0, $height="", $width="", $link="") {
830// Given the path to a picture file in a course, or a URL,
831// this function includes the picture in the page.
832 global $CFG;
833
834 if ($height) {
76c1650d 835 $height = "height=\"$height\"";
9fa49e22 836 }
837 if ($width) {
76c1650d 838 $width = "width=\"$width\"";
9fa49e22 839 }
840 if ($link) {
76c1650d 841 echo "<a href=\"$link\">";
9fa49e22 842 }
843 if (substr(strtolower($path), 0, 7) == "http://") {
76c1650d 844 echo "<img border=0 $height $width src=\"$path\">";
9fa49e22 845
846 } else if ($courseid) {
76c1650d 847 echo "<img border=0 $height $width src=\"";
9fa49e22 848 if ($CFG->slasharguments) { // Use this method if possible for better caching
849 echo "$CFG->wwwroot/file.php/$courseid/$path";
850 } else {
3f396065 851 echo "$CFG->wwwroot/file.php?file=/$courseid/$path";
9fa49e22 852 }
853 echo "\">";
854 } else {
855 echo "Error: must pass URL or course";
856 }
857 if ($link) {
76c1650d 858 echo "</a>";
9fa49e22 859 }
860}
861
862function print_user_picture($userid, $courseid, $picture, $large=false, $returnstring=false, $link=true) {
863 global $CFG;
864
865 if ($link) {
76c1650d 866 $output = "<a href=\"$CFG->wwwroot/user/view.php?id=$userid&course=$courseid\">";
9fa49e22 867 } else {
868 $output = "";
869 }
870 if ($large) {
871 $file = "f1.jpg";
872 $size = 100;
873 } else {
874 $file = "f2.jpg";
875 $size = 35;
876 }
877 if ($picture) {
878 if ($CFG->slasharguments) { // Use this method if possible for better caching
76c1650d 879 $output .= "<img src=\"$CFG->wwwroot/user/pix.php/$userid/$file\" border=0 width=$size height=$size alt=\"\">";
9fa49e22 880 } else {
76c1650d 881 $output .= "<img src=\"$CFG->wwwroot/user/pix.php?file=/$userid/$file\" border=0 width=$size height=$size alt=\"\">";
9fa49e22 882 }
883 } else {
76c1650d 884 $output .= "<img src=\"$CFG->wwwroot/user/default/$file\" border=0 width=$size height=$size alt=\"\">";
9fa49e22 885 }
886 if ($link) {
76c1650d 887 $output .= "</a>";
9fa49e22 888 }
889
890 if ($returnstring) {
891 return $output;
892 } else {
893 echo $output;
894 }
895}
896
897function print_table($table) {
898// Prints a nicely formatted table.
899// $table is an object with several properties.
900// $table->head is an array of heading names.
901// $table->align is an array of column alignments
902// $table->size is an array of column sizes
5867bfb5 903// $table->wrap is an array of "nowrap"s or nothing
9fa49e22 904// $table->data[] is an array of arrays containing the data.
905// $table->width is an percentage of the page
906// $table->cellpadding padding on each cell
907// $table->cellspacing spacing between cells
908
909 if (isset($table->align)) {
910 foreach ($table->align as $key => $aa) {
911 if ($aa) {
76c1650d 912 $align[$key] = " align=\"$aa\"";
9fa49e22 913 } else {
914 $align[$key] = "";
915 }
916 }
917 }
918 if (isset($table->size)) {
919 foreach ($table->size as $key => $ss) {
920 if ($ss) {
76c1650d 921 $size[$key] = " width=\"$ss\"";
9fa49e22 922 } else {
923 $size[$key] = "";
924 }
925 }
926 }
5867bfb5 927 if (isset($table->wrap)) {
928 foreach ($table->wrap as $key => $ww) {
929 if ($ww) {
76c1650d 930 $wrap[$key] = " nowrap ";
5867bfb5 931 } else {
932 $wrap[$key] = "";
933 }
934 }
935 }
9fa49e22 936
9d378732 937 if (empty($table->width)) {
9fa49e22 938 $table->width = "80%";
939 }
940
9d378732 941 if (empty($table->cellpadding)) {
9fa49e22 942 $table->cellpadding = "5";
943 }
944
9d378732 945 if (empty($table->cellspacing)) {
9fa49e22 946 $table->cellspacing = "1";
947 }
948
5867bfb5 949 print_simple_box_start("center", "$table->width", "#ffffff", 0);
950 echo "<table width=100% border=0 valign=top align=center ";
9fa49e22 951 echo " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"generaltable\">\n";
952
b79f41cd 953 if (!empty($table->head)) {
5867bfb5 954 echo "<tr>";
9fa49e22 955 foreach ($table->head as $key => $heading) {
9d378732 956 if (!isset($size[$key])) {
957 $size[$key] = "";
958 }
959 if (!isset($align[$key])) {
960 $align[$key] = "";
961 }
5867bfb5 962 echo "<th valign=top ".$align[$key].$size[$key]." nowrap class=\"generaltableheader\">$heading</th>";
9fa49e22 963 }
964 echo "</TR>\n";
965 }
966
967 foreach ($table->data as $row) {
5867bfb5 968 echo "<tr valign=top>";
9fa49e22 969 foreach ($row as $key => $item) {
9d378732 970 if (!isset($size[$key])) {
971 $size[$key] = "";
972 }
973 if (!isset($align[$key])) {
974 $align[$key] = "";
975 }
5867bfb5 976 if (!isset($wrap[$key])) {
977 $wrap[$key] = "";
978 }
979 echo "<td ".$align[$key].$size[$key].$wrap[$key]." class=\"generaltablecell\">$item</td>";
9fa49e22 980 }
5867bfb5 981 echo "</tr>\n";
9fa49e22 982 }
5867bfb5 983 echo "</table>\n";
9fa49e22 984 print_simple_box_end();
985
986 return true;
987}
988
989function print_editing_switch($courseid) {
990 global $CFG, $USER;
991
992 if (isteacher($courseid)) {
993 if ($USER->editing) {
76c1650d 994 echo "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid&edit=off\">turn editing off</a>";
9fa49e22 995 } else {
76c1650d 996 echo "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid&edit=on\">turn editing on</a>";
9fa49e22 997 }
998 }
999}
1000
1001function print_textarea($richedit, $rows, $cols, $width, $height, $name, $value="") {
7d8f674d 1002/// Prints a richtext field or a normal textarea
9fa49e22 1003 global $CFG, $THEME;
1004
1005 if ($richedit) {
76c1650d 1006 echo "<object id=richedit style=\"background-color: buttonface\"";
9fa49e22 1007 echo " data=\"$CFG->wwwroot/lib/rte/richedit.html\"";
1008 echo " width=\"$width\" height=\"$height\" ";
1009 echo " type=\"text/x-scriptlet\" VIEWASTEXT></object>\n";
76c1650d 1010 echo "<textarea style=\"display:none\" name=\"$name\" rows=1 cols=1>";
9fa49e22 1011 p($value);
76c1650d 1012 echo "</textarea>\n";
9fa49e22 1013 } else {
76c1650d 1014 echo "<textarea name=\"$name\" rows=\"$rows\" cols=\"$cols\" wrap=virtual>";
9fa49e22 1015 p($value);
76c1650d 1016 echo "</textarea>\n";
9fa49e22 1017 }
1018}
1019
1020function print_richedit_javascript($form, $name, $source="no") {
76c1650d 1021 echo "<script language=\"javascript\" event=\"onload\" for=\"window\">\n";
9fa49e22 1022 echo " document.richedit.options = \"history=no;source=$source\";";
1023 echo " document.richedit.docHtml = $form.$name.innerText;";
76c1650d 1024 echo "</script>";
9fa49e22 1025}
1026
1027
1028function update_course_icon($courseid) {
1029// Used to be an icon, but it's now a simple form button
1030 global $CFG, $USER;
1031
1032 if (isteacher($courseid)) {
9c9f7d77 1033 if (!empty($USER->editing)) {
9fa49e22 1034 $string = get_string("turneditingoff");
1035 $edit = "off";
1036 } else {
1037 $string = get_string("turneditingon");
1038 $edit = "on";
1039 }
76c1650d 1040 return "<form target=_parent method=get action=\"$CFG->wwwroot/course/view.php\">".
1041 "<input type=hidden name=id value=\"$courseid\">".
1042 "<input type=hidden name=edit value=\"$edit\">".
1043 "<input type=submit value=\"$string\"></form>";
9fa49e22 1044 }
1045}
1046
1047function update_module_button($moduleid, $courseid, $string) {
1048// Prints the editing button on a module "view" page
1049 global $CFG;
1050
1051 if (isteacher($courseid)) {
1052 $string = get_string("updatethis", "", $string);
76c1650d 1053 return "<form target=_parent method=get action=\"$CFG->wwwroot/course/mod.php\">".
1054 "<input type=hidden name=update value=\"$moduleid\">".
1055 "<input type=hidden name=return value=\"true\">".
1056 "<input type=submit value=\"$string\"></form>";
9fa49e22 1057 }
1058}
1059
1060
1061function navmenu($course, $cm=NULL) {
1062// Given a course and a (current) coursemodule
1063// This function returns a small popup menu with all the
1064// course activity modules in it, as a navigation menu
1065// The data is taken from the serialised array stored in
1066// the course record
1067
1068 global $CFG;
1069
1070 if ($cm) {
1071 $cm = $cm->id;
1072 }
1073
1074 if ($course->format == 'weeks') {
1075 $strsection = get_string("week");
1076 } else {
1077 $strsection = get_string("topic");
1078 }
1079
1080 if (!$modinfo = unserialize($course->modinfo)) {
1081 return "";
1082 }
1083 $section = -1;
1084 $selected = "";
1085 foreach ($modinfo as $mod) {
1086 if ($mod->section > 0 and $section <> $mod->section) {
1087 $menu[] = "-------------- $strsection $mod->section --------------";
1088 }
1089 $section = $mod->section;
cf055081 1090 //Only add visible or teacher mods to jumpmenu
1091 if ($mod->visible or isteacher($course->id)) {
1092 $url = "$mod->mod/view.php?id=$mod->cm";
1093 if ($cm == $mod->cm) {
1094 $selected = $url;
1095 }
1096 $mod->name = urldecode($mod->name);
1097 if (strlen($mod->name) > 55) {
1098 $mod->name = substr($mod->name, 0, 50)."...";
1099 }
2a409368 1100 if (!$mod->visible) {
1101 $mod->name = "(".$mod->name.")";
1102 }
cf055081 1103 $menu[$url] = $mod->name;
9fa49e22 1104 }
9fa49e22 1105 }
1106
1107 return popup_form("$CFG->wwwroot/mod/", $menu, "navmenu", $selected, get_string("jumpto"), "", "", true);
1108}
1109
1110
1111
1112function print_date_selector($day, $month, $year, $currenttime=0) {
1113// Currenttime is a default timestamp in GMT
1114// Prints form items with the names $day, $month and $year
1115
1116 if (!$currenttime) {
1117 $currenttime = time();
1118 }
1119 $currentdate = usergetdate($currenttime);
1120
1121 for ($i=1; $i<=31; $i++) {
1122 $days[$i] = "$i";
1123 }
1124 for ($i=1; $i<=12; $i++) {
39e018b3 1125 $months[$i] = userdate(gmmktime(12,0,0,$i,1,2000), "%B");
9fa49e22 1126 }
1127 for ($i=2000; $i<=2010; $i++) {
1128 $years[$i] = $i;
1129 }
47f1da80 1130 choose_from_menu($days, $day, $currentdate['mday'], "");
1131 choose_from_menu($months, $month, $currentdate['mon'], "");
1132 choose_from_menu($years, $year, $currentdate['year'], "");
9fa49e22 1133}
1134
1135function print_time_selector($hour, $minute, $currenttime=0) {
1136// Currenttime is a default timestamp in GMT
1137// Prints form items with the names $hour and $minute
1138
1139 if (!$currenttime) {
1140 $currenttime = time();
1141 }
1142 $currentdate = usergetdate($currenttime);
1143 for ($i=0; $i<=23; $i++) {
1144 $hours[$i] = sprintf("%02d",$i);
1145 }
1146 for ($i=0; $i<=59; $i++) {
1147 $minutes[$i] = sprintf("%02d",$i);
1148 }
47f1da80 1149 choose_from_menu($hours, $hour, $currentdate['hours'], "");
1150 choose_from_menu($minutes, $minute, $currentdate['minutes'], "");
9fa49e22 1151}
1152
1153function error ($message, $link="") {
1154 global $CFG, $SESSION;
1155
1156 print_header(get_string("error"));
1157 echo "<BR>";
1158 print_simple_box($message, "center", "", "#FFBBBB");
1159
1160 if (!$link) {
1161 if ( !empty($SESSION->fromurl) ) {
1162 $link = "$SESSION->fromurl";
1163 unset($SESSION->fromurl);
9fa49e22 1164 } else {
1165 $link = "$CFG->wwwroot";
1166 }
1167 }
1168 print_continue($link);
1169 print_footer();
1170 die;
1171}
1172
1173function helpbutton ($page, $title="", $module="moodle", $image=true, $linktext=false, $text="") {
1174 // $page = the keyword that defines a help page
1175 // $title = the title of links, rollover tips, alt tags etc
1176 // $module = which module is the page defined in
1177 // $image = use a help image for the link? (true/false/"both")
1178 // $text = if defined then this text is used in the page, and
1179 // the $page variable is ignored.
dc0dc7d5 1180 global $CFG, $THEME;
9fa49e22 1181
1182 if ($module == "") {
1183 $module = "moodle";
1184 }
1185
dc0dc7d5 1186 if (empty($THEME->custompix)) {
1187 $icon = "$CFG->wwwroot/pix/help.gif";
1188 } else {
1189 $icon = "$CFG->wwwroot/theme/$CFG->theme/pix/help.gif";
1190 }
1191
9fa49e22 1192 if ($image) {
1193 if ($linktext) {
dc0dc7d5 1194 $linkobject = "$title<img align=\"absmiddle\" border=0 height=17 width=22 alt=\"\" src=\"$icon\">";
9fa49e22 1195 } else {
dc0dc7d5 1196 $linkobject = "<img align=\"absmiddle\" border=0 height=17 width=22 alt=\"$title\" src=\"$icon\">";
9fa49e22 1197 }
1198 } else {
1199 $linkobject = $title;
1200 }
1201 if ($text) {
1202 $url = "/help.php?module=$module&text=".htmlentities(urlencode($text));
1203 } else {
1204 $url = "/help.php?module=$module&file=$page.html";
1205 }
1206 link_to_popup_window ($url, "popup", $linkobject, 400, 500, $title);
1207}
1208
e825f279 1209function emoticonhelpbutton($form, $field) {
1210/// Prints a special help button that is a link to the "live" emoticon popup
1211 global $CFG, $SESSION;
1212
1213 $SESSION->inserttextform = $form;
1214 $SESSION->inserttextfield = $field;
1215 helpbutton("emoticons", get_string("helpemoticons"), "moodle", false, true);
1216 echo "&nbsp;<img src=\"$CFG->wwwroot/pix/s/smiley.gif\" align=\"absmiddle\" width=15 height=15></a>";
1217}
1218
9fa49e22 1219function notice ($message, $link="") {
750ab759 1220 global $CFG, $THEME;
9fa49e22 1221
1222 if (!$link) {
750ab759 1223 if (!empty($_SERVER["HTTP_REFERER"])) {
1224 $link = $_SERVER["HTTP_REFERER"];
1225 } else {
1226 $link = $CFG->wwwroot;
1227 }
9fa49e22 1228 }
1229
01d79966 1230 echo "<br />";
11a876e1 1231 print_simple_box($message, "center", "50%", "$THEME->cellheading", "20", "noticebox");
eb347b6b 1232 print_heading("<a href=\"$link\">".get_string("continue")."</a>");
9fa49e22 1233 print_footer(get_site());
1234 die;
1235}
1236
1237function notice_yesno ($message, $linkyes, $linkno) {
1238 global $THEME;
1239
eb347b6b 1240 print_simple_box_start("center", "60%", "$THEME->cellheading");
1241 echo "<p align=center><font size=3>$message</font></p>";
1242 echo "<p align=center><font size=3><b>";
1243 echo "<a href=\"$linkyes\">".get_string("yes")."</a>";
9fa49e22 1244 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
eb347b6b 1245 echo "<a href=\"$linkno\">".get_string("no")."</a>";
1246 echo "</b></font></p>";
9fa49e22 1247 print_simple_box_end();
1248}
1249
1250function redirect($url, $message="", $delay=0) {
1251// Uses META tags to redirect the user, after printing a notice
1252
c9082a8c 1253 if (empty($message)) {
76c1650d 1254 echo "<meta http-equiv='refresh' content='$delay; url=$url'>";
c9082a8c 1255 } else {
1256 if (! $delay) {
1257 $delay = 3; // There's no point having a message with no delay
1258 }
76c1650d 1259 echo "<meta http-equiv='refresh' content='$delay; url=$url'>";
9fa49e22 1260 print_header();
76c1650d 1261 echo "<center>";
1262 echo "<p>$message</p>";
1263 echo "<p>( <a href=\"$url\">".get_string("continue")."</a> )</p>";
1264 echo "</center>";
9fa49e22 1265 }
1266 die;
1267}
1268
99988d1a 1269function notify ($message, $color="red", $align="center") {
1270 echo "<p align=\"$align\"><b><font color=\"$color\">$message</font></b></p>\n";
9fa49e22 1271}
1272
1273
9d5b689c 1274// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
f9903ed0 1275?>