Improved interface for course creators
[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
86aa7ccf 250function link_to_popup_window ($url, $name="popup", $linkname="click here",
251 $height=400, $width=500, $title="Popup window") {
c1d57101 252/// This will create a HTML link that will work on both
253/// Javascript and non-javascript browsers.
254/// Relies on the Javascript function openpopup in javascript.php
255/// $url must be relative to home page eg /mod/survey/stuff.php
f9903ed0 256
ff80e012 257 global $CFG;
258
86aa7ccf 259 $options = "menubar=0,location=0,scrollbars,resizable,width=$width,height=$height";
260 $fullscreen = 0;
f9903ed0 261
86aa7ccf 262 echo "<a target=\"$name\" title=\"$title\" href=\"$CFG->wwwroot/$url\" ".
263 "onClick=\"return openpopup('$url', '$name', '$options', $fullscreen);\">$linkname</a>\n";
f9903ed0 264}
265
86aa7ccf 266
f9903ed0 267function close_window_button() {
c1d57101 268/// Prints a simple button to close a window
269
86aa7ccf 270 echo "<center>\n";
271 echo "<script>\n";
272 echo "<!--\n";
273 echo "document.write('<form>');\n";
274 echo "document.write('<input type=button onClick=\"self.close();\" value=\"".get_string("closewindow")."\">');\n";
275 echo "document.write('</form>');\n";
276 echo "-->\n";
277 echo "</script>\n";
278 echo "<noscript>\n";
279 echo "<a href=\"".$_SERVER['HTTP_REFERER']."\"><---</a>\n";
280 echo "</noscript>\n";
281 echo "</center>\n";
f9903ed0 282}
283
284
08056730 285function choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", $nothingvalue="0", $return=false) {
c1d57101 286/// Given an array of value, creates a popup menu to be part of a form
287/// $options["value"]["label"]
f9903ed0 288
618b22c5 289 if ($nothing == "choose") {
290 $nothing = get_string("choose")."...";
291 }
292
f9903ed0 293 if ($script) {
294 $javascript = "onChange=\"$script\"";
9c9f7d77 295 } else {
296 $javascript = "";
f9903ed0 297 }
9c9f7d77 298
76c1650d 299 $output = "<select name=$name $javascript>\n";
bda8d43a 300 if ($nothing) {
76c1650d 301 $output .= " <option value=\"$nothingvalue\"\n";
bda8d43a 302 if ($nothingvalue == $selected) {
76c1650d 303 $output .= " selected";
bda8d43a 304 }
76c1650d 305 $output .= ">$nothing</option>\n";
873960de 306 }
607809b3 307 if (!empty($options)) {
308 foreach ($options as $value => $label) {
76c1650d 309 $output .= " <option value=\"$value\"";
607809b3 310 if ($value == $selected) {
76c1650d 311 $output .= " selected";
607809b3 312 }
313 if ($label) {
76c1650d 314 $output .= ">$label</option>\n";
607809b3 315 } else {
76c1650d 316 $output .= ">$value</option>\n";
607809b3 317 }
f9903ed0 318 }
319 }
76c1650d 320 $output .= "</select>\n";
08056730 321
322 if ($return) {
323 return $output;
324 } else {
325 echo $output;
326 }
f9903ed0 327}
328
d897cae4 329function popup_form ($common, $options, $formname, $selected="", $nothing="choose", $help="", $helptext="", $return=false) {
c1d57101 330/// Implements a complete little popup form
331/// $common = the URL up to the point of the variable that changes
332/// $options = A list of value-label pairs for the popup list
333/// $formname = name must be unique on the page
334/// $selected = the option that is already selected
335/// $nothing = The label for the "no choice" option
e5dfd0f3 336/// $help = The name of a help page if help is required
337/// $helptext = The name of the label for the help button
f9903ed0 338
0d0baabf 339 global $CFG;
340
618b22c5 341 if ($nothing == "choose") {
342 $nothing = get_string("choose")."...";
343 }
344
dfec7b01 345 $startoutput = "<form target=\"{$CFG->framename}\" name=$formname>";
2bc269fd 346 $output = "<select name=popup onchange=\"top.location=document.$formname.popup.options[document.$formname.popup.selectedIndex].value\">\n";
f9903ed0 347
348 if ($nothing != "") {
dfec7b01 349 $output .= " <option value=\"javascript:void(0)\">$nothing</option>\n";
f9903ed0 350 }
351
352 foreach ($options as $value => $label) {
d897cae4 353 if (substr($label,0,1) == "-") {
dfec7b01 354 $output .= " <option value=\"\"";
d897cae4 355 } else {
dfec7b01 356 $output .= " <option value=\"$common$value\"";
d897cae4 357 if ($value == $selected) {
dfec7b01 358 $output .= " selected";
d897cae4 359 }
f9903ed0 360 }
361 if ($label) {
dfec7b01 362 $output .= ">$label</option>\n";
f9903ed0 363 } else {
dfec7b01 364 $output .= ">$value</option>\n";
f9903ed0 365 }
366 }
dfec7b01 367 $output .= "</select>";
368 $output .= "</form>\n";
d897cae4 369
370 if ($return) {
dfec7b01 371 return $startoutput.$output;
d897cae4 372 } else {
dfec7b01 373 echo $startoutput;
9c9f7d77 374 if ($help) {
375 helpbutton($help, $helptext);
376 }
d897cae4 377 echo $output;
378 }
f9903ed0 379}
380
381
382
383function formerr($error) {
c1d57101 384/// Prints some red text
f9903ed0 385 if (!empty($error)) {
386 echo "<font color=#ff0000>$error</font>";
387 }
388}
389
390
391function validate_email ($address) {
c1d57101 392/// Validates an email to make it makes sense.
f9903ed0 393 return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.
394 '@'.
395 '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
396 '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
397 $address));
398}
399
6c8e8b5e 400function detect_munged_arguments($string) {
393c9b4f 401 if (ereg('\.\.', $string)) { // check for parent URLs
6c8e8b5e 402 return true;
403 }
393c9b4f 404 if (ereg('[\|\`]', $string)) { // check for other bad characters
6c8e8b5e 405 return true;
406 }
407 return false;
408}
409
6ed3da1d 410function get_slash_arguments($file="file.php") {
411/// Searches the current environment variables for some slash arguments
f9903ed0 412
eaa50dbc 413 if (!$string = me()) {
f9903ed0 414 return false;
415 }
eaa50dbc 416
6ed3da1d 417 $pathinfo = explode($file, $string);
418
bcdfe14e 419 if (!empty($pathinfo[1])) {
420 return $pathinfo[1];
6ed3da1d 421 } else {
422 return false;
423 }
424}
425
426function parse_slash_arguments($string, $i=0) {
427/// Extracts arguments from "/foo/bar/something"
428/// eg http://mysite.com/script.php/foo/bar/something
f9903ed0 429
6c8e8b5e 430 if (detect_munged_arguments($string)) {
780db230 431 return false;
432 }
6ed3da1d 433 $args = explode("/", $string);
f9903ed0 434
435 if ($i) { // return just the required argument
436 return $args[$i];
437
438 } else { // return the whole array
439 array_shift($args); // get rid of the empty first one
440 return $args;
441 }
442}
443
0095d5cd 444function format_text_menu() {
c1d57101 445/// Just returns an array of formats suitable for a popup menu
0095d5cd 446 return array (FORMAT_MOODLE => get_string("formattext"),
6901fa79 447 FORMAT_HTML => get_string("formathtml"),
d342c763 448 FORMAT_PLAIN => get_string("formatplain"),
449 FORMAT_WIKI => get_string("formatwiki"));
0095d5cd 450}
451
60f18531 452function format_text($text, $format=FORMAT_MOODLE, $options=NULL) {
c1d57101 453/// Given text in a variety of format codings, this function returns
454/// the text as safe HTML.
455///
456/// $text is raw text (originally from a user)
457/// $format is one of the format constants, defined above
0095d5cd 458
459 switch ($format) {
73f8658c 460 case FORMAT_HTML:
5f350e8f 461 replace_smilies($text);
73f8658c 462 return $text;
463 break;
464
6901fa79 465 case FORMAT_PLAIN:
466 $text = htmlentities($text);
3405b212 467 $text = str_replace(" ", "&nbsp; ", $text);
5f350e8f 468 replace_smilies($text);
6901fa79 469 $text = nl2br($text);
470 return $text;
471 break;
472
d342c763 473 case FORMAT_WIKI:
7f9bd7b9 474 return wiki_to_html($text);
d342c763 475 break;
476
73f8658c 477 default: // FORMAT_MOODLE or anything else
c9dda990 478 if (!isset($options->smiley)) {
479 $options->smiley=true;
480 }
481 if (!isset($options->para)) {
1a072208 482 $options->para=true;
c9dda990 483 }
0095d5cd 484 return text_to_html($text, $options->smiley, $options->para);
485 break;
0095d5cd 486 }
487}
488
d342c763 489function format_text_email($text, $format) {
490/// Given text in a variety of format codings, this function returns
491/// the text as plain text suitable for plain email.
492///
493/// $text is raw text (originally from a user)
494/// $format is one of the format constants, defined above
495
496 switch ($format) {
497
498 case FORMAT_PLAIN:
499 return $text;
500 break;
501
502 case FORMAT_WIKI:
503 $text = wiki_to_html($text);
7c55a29b 504 return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES)));
d342c763 505 break;
506
507 default: // FORMAT_MOODLE or anything else
508 // Need to add something in here to create a text-friendly way of presenting URLs
7c55a29b 509 return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES)));
d342c763 510 break;
511 }
512}
0095d5cd 513
514function clean_text($text, $format) {
c1d57101 515/// Given raw text (eg typed in by a user), this function cleans it up
516/// and removes any nasty tags that could mess up Moodle pages.
b7a3cf49 517
fc120758 518 global $ALLOWED_TAGS;
3fe3851d 519
d342c763 520 switch ($format) {
0095d5cd 521 case FORMAT_MOODLE:
0095d5cd 522 case FORMAT_HTML:
d342c763 523 case FORMAT_WIKI:
3fe3851d 524 $text = strip_tags($text, $ALLOWED_TAGS);
71e8bd81 525 $text = str_ireplace("javascript:", "xxx", $text); // Remove javascript: label
526 $text = eregi_replace("([^a-z])language([[:space:]]*)=", "xxx", $text); // Remove javascript/VBScript
527 $text = eregi_replace("([^a-z])on([a-z]+)([[:space:]]*)=", "xxx", $text); // Remove script events
3fe3851d 528 return $text;
6901fa79 529
530 case FORMAT_PLAIN:
531 return $text;
0095d5cd 532 }
b7a3cf49 533}
f9903ed0 534
5f350e8f 535function replace_smilies(&$text) {
c1d57101 536/// Replaces all known smileys in the text with image equivalents
2ea9027b 537 global $CFG;
c1d57101 538
617778f2 539 static $runonce = false;
69081931 540 static $e = array();
541 static $img = array();
617778f2 542 static $emoticons = array(
2ea9027b 543 ':-)' => 'smiley.gif',
544 ':)' => 'smiley.gif',
545 ':-D' => 'biggrin.gif',
546 ';-)' => 'wink.gif',
547 ':-/' => 'mixed.gif',
548 'V-.' => 'thoughtful.gif',
549 ':-P' => 'tongueout.gif',
550 'B-)' => 'cool.gif',
551 '^-)' => 'approve.gif',
552 '8-)' => 'wideeyes.gif',
553 ':o)' => 'clown.gif',
554 ':-(' => 'sad.gif',
555 ':(' => 'sad.gif',
556 '8-.' => 'shy.gif',
557 ':-I' => 'blush.gif',
558 ':-X' => 'kiss.gif',
559 '8-o' => 'surprise.gif',
560 'P-|' => 'blackeye.gif',
561 '8-[' => 'angry.gif',
562 'xx-P' => 'dead.gif',
563 '|-.' => 'sleepy.gif',
564 '}-]' => 'evil.gif',
565 );
566
01d79966 567 if ($runonce == false){
617778f2 568 foreach ($emoticons as $emoticon => $image){
69081931 569 $e[] = $emoticon;
01d79966 570 $img[] = "<img alt=\"$emoticon\" width=15 height=15 src=\"$CFG->wwwroot/pix/s/$image\">";
617778f2 571 }
572 $runonce = true;
c0f728ba 573 }
b7a3cf49 574
5f350e8f 575 $text = str_replace($e, $img, $text);
1a072208 576}
0095d5cd 577
909f539d 578function text_to_html($text, $smiley=true, $para=true) {
c1d57101 579/// Given plain text, makes it into HTML as nicely as possible.
580/// May contain HTML tags already
f9903ed0 581
c1d57101 582/// Remove any whitespace that may be between HTML tags
7b3be1b1 583 $text = eregi_replace(">([[:space:]]+)<", "><", $text);
584
c1d57101 585/// Remove any returns that precede or follow HTML tags
0eae8049 586 $text = eregi_replace("([\n\r])<", " <", $text);
587 $text = eregi_replace(">([\n\r])", "> ", $text);
7b3be1b1 588
5f350e8f 589 convert_urls_into_links($text);
f9903ed0 590
c1d57101 591/// Make returns into HTML newlines.
f9903ed0 592 $text = nl2br($text);
593
c1d57101 594/// Turn smileys into images.
d69cb7f4 595 if ($smiley) {
5f350e8f 596 replace_smilies($text);
d69cb7f4 597 }
f9903ed0 598
c1d57101 599/// Wrap the whole thing in a paragraph tag if required
909f539d 600 if ($para) {
01d79966 601 return "<p>".$text."</p>";
909f539d 602 } else {
603 return $text;
604 }
f9903ed0 605}
606
3e9ca9fb 607function wiki_to_html($text) {
01d79966 608/// Given Wiki formatted text, make it into XHTML using external function
43373804 609 global $CFG;
3e9ca9fb 610
43373804 611 require_once("$CFG->libdir/wiki.php");
3e9ca9fb 612
01d79966 613 $wiki = new Wiki;
614 return $wiki->format($text);
3e9ca9fb 615}
616
5f350e8f 617function convert_urls_into_links(&$text) {
618/// Given some text, it converts any URLs it finds into HTML links.
619
620/// Make lone URLs into links. eg http://moodle.com/
3405b212 621 $text = eregi_replace("([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
5f350e8f 622 "\\1<a href=\"\\2://\\3\\4\" TARGET=\"newpage\">\\2://\\3\\4</a>", $text);
623
624/// eg www.moodle.com
3405b212 625 $text = eregi_replace("([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])",
5f350e8f 626 "\\1<a href=\"http://www.\\2\\3\" TARGET=\"newpage\">www.\\2\\3</a>", $text);
627}
628
5af78ed2 629function highlight($needle, $haystack) {
c1d57101 630/// This function will highlight instances of $needle in $haystack
5af78ed2 631
632 $parts = explode(strtolower($needle), strtolower($haystack));
633
634 $pos = 0;
635
636 foreach ($parts as $key => $part) {
637 $parts[$key] = substr($haystack, $pos, strlen($part));
638 $pos += strlen($part);
639
640 $parts[$key] .= "<SPAN CLASS=highlight>".substr($haystack, $pos, strlen($needle))."</SPAN>";
641 $pos += strlen($needle);
642 }
643
644 return (join('', $parts));
645}
646
f9903ed0 647
9fa49e22 648
649/// STANDARD WEB PAGE PARTS ///////////////////////////////////////////////////
650
651function print_header ($title="", $heading="", $navigation="", $focus="", $meta="", $cache=true, $button="&nbsp;", $menu="") {
652// $title - appears top of window
653// $heading - appears top of page
654// $navigation - premade navigation string
655// $focus - indicates form element eg inputform.password
656// $meta - meta tags in the header
657// $cache - should this page be cacheable?
658// $button - HTML code for a button (usually for module editing)
659// $menu - HTML code for a popup menu
e825f279 660 global $USER, $CFG, $THEME, $SESSION;
9fa49e22 661
662 if (file_exists("$CFG->dirroot/theme/$CFG->theme/styles.php")) {
663 $styles = $CFG->stylesheet;
664 } else {
665 $styles = "$CFG->wwwroot/theme/standard/styles.php";
666 }
667
668 if ($navigation == "home") {
669 $home = true;
670 $navigation = "";
9d378732 671 } else {
672 $home = false;
9fa49e22 673 }
674
675 if ($button == "") {
676 $button = "&nbsp;";
677 }
678
679 if (!$menu and $navigation) {
680 if (isset($USER->id)) {
c6a0f9d5 681 $menu = "<font size=2><a target=\"$CFG->framename\" href=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</a></font>";
9fa49e22 682 } else {
c6a0f9d5 683 $menu = "<font size=2><a target=\"$CFG->framename\" href=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</a></font>";
9fa49e22 684 }
685 }
686
687 // Specify character set ... default is iso-8859-1 but some languages might need something else
688 // Could be optimised by carrying the charset variable around in $USER
689 if (current_language() == "en") {
107b010b 690 $meta = "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n$meta\n";
9fa49e22 691 } else {
107b010b 692 $meta = "<meta http-equiv=\"content-type\" content=\"text/html; charset=".get_string("thischarset")."\">\n$meta\n";
9fa49e22 693 }
694
3662bce5 695 if (!empty($CFG->langdir) and $CFG->langdir == "RTL") {
107b010b 696 $direction = " dir=\"rtl\"";
9fa49e22 697 } else {
107b010b 698 $direction = " dir=\"ltr\"";
9fa49e22 699 }
700
701 if (!$cache) { // Do everything we can to prevent clients and proxies caching
702 @header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
703 @header("Pragma: no-cache");
76c1650d 704 $meta .= "\n<meta http-equiv=\"pragma\" content=\"no-cache\">";
705 $meta .= "\n<meta http-equiv=\"expires\" content=\"0\">";
9fa49e22 706 }
707
708 include ("$CFG->dirroot/theme/$CFG->theme/header.html");
709}
710
711function print_footer ($course=NULL) {
712// Can provide a course object to make the footer contain a link to
713// to the course home page, otherwise the link will go to the site home
714 global $USER, $CFG, $THEME;
715
716
717/// Course links
718 if ($course) {
719 if ($course == "home") { // special case for site home page - please do not remove
76c1650d 720 $homelink = "<p align=\"center\"><a title=\"moodle $CFG->release ($CFG->version)\" href=\"http://moodle.org/\" target=\"_blank\">";
67a63a30 721 $homelink .= "<br><img width=\"130\" height=\"19\" src=\"pix/madewithmoodle.gif\" border=0></a></p>";
9fa49e22 722 $course = get_site();
723 $homepage = true;
724 } else {
76c1650d 725 $homelink = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>";
9fa49e22 726 }
727 } else {
c2cb4545 728 $homelink = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/\">".get_string("home")."</a>";
9fa49e22 729 $course = get_site();
730 }
731
732/// User links
a282d0ff 733 $loggedinas = user_login_string($course, $USER);
734
735 include ("$CFG->dirroot/theme/$CFG->theme/footer.html");
736}
737
738
739function user_login_string($course, $user=NULL) {
740 global $USER, $CFG;
741
8d2accb6 742 if (empty($user)) {
a282d0ff 743 $user = $USER;
744 }
745
746 if (isset($user->realuser)) {
747 if ($realuser = get_record("user", "id", $user->realuser)) {
ca16eaeb 748 $realuserinfo = " [<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&return=$realuser->id\">$realuser->firstname $realuser->lastname</A>] ";
9fa49e22 749 }
9d378732 750 } else {
751 $realuserinfo = "";
9fa49e22 752 }
753
a282d0ff 754 if (isset($user->id) and $user->id) {
ca16eaeb 755 $username = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</a>";
9fa49e22 756 $loggedinas = $realuserinfo.get_string("loggedinas", "moodle", "$username").
ca16eaeb 757 " (<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</a>)";
9fa49e22 758 } else {
759 $loggedinas = get_string("loggedinnot", "moodle").
ca16eaeb 760 " (<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</a>)";
9fa49e22 761 }
a282d0ff 762 return $loggedinas;
9fa49e22 763}
764
765
9fa49e22 766function print_navigation ($navigation) {
767 global $CFG;
768
769 if ($navigation) {
770 if (! $site = get_site()) {
771 $site->shortname = get_string("home");;
772 }
eb347b6b 773 echo "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/\">$site->shortname</a> -> $navigation";
9fa49e22 774 }
775}
776
d4df9200 777function print_headline($text, $size=2) {
778 echo "<b><font size=\"$size\">$text</font></b><br />\n";
779}
780
76c1650d 781function print_heading($text, $align="center", $size=3) {
782 echo "<p align=\"$align\"><font size=\"$size\"><b>".stripslashes_safe($text)."</b></font></p>";
9fa49e22 783}
784
785function print_heading_with_help($text, $helppage, $module="moodle") {
786// Centered heading with attached help button (same title text)
eb347b6b 787 echo "<p align=\"center\"><font size=\"3\"><b>".stripslashes_safe($text);
9fa49e22 788 helpbutton($helppage, $text, $module);
eb347b6b 789 echo "</b></font></p>";
9fa49e22 790}
791
792function print_continue($link) {
9fa49e22 793
794 if (!$link) {
607809b3 795 $link = $_SERVER["HTTP_REFERER"];
9fa49e22 796 }
797
eb347b6b 798 print_heading("<a href=\"$link\">".get_string("continue")."</a>");
9fa49e22 799}
800
801
802function print_simple_box($message, $align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
803 print_simple_box_start($align, $width, $color, $padding, $class);
7d8f674d 804 echo stripslashes_safe($message);
9fa49e22 805 print_simple_box_end();
806}
807
808function print_simple_box_start($align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
809 global $THEME;
810
811 if ($align) {
76c1650d 812 $align = "align=\"$align\"";
9fa49e22 813 }
814 if ($width) {
76c1650d 815 $width = "width=\"$width\"";
9fa49e22 816 }
9d378732 817 echo "<table $align $width class=\"$class\" border=\"0\" cellpadding=\"$padding\" cellspacing=\"0\"><tr><td bgcolor=\"$color\" class=\"$class"."content\">";
9fa49e22 818}
819
820function print_simple_box_end() {
821 echo "</td></tr></table>";
822}
823
cc7fa0dc 824function print_single_button($link, $options, $label="OK", $method="get") {
825 echo "<form action=\"$link\" method=\"$method\">";
9fa49e22 826 if ($options) {
827 foreach ($options as $name => $value) {
76c1650d 828 echo "<input type=hidden name=\"$name\" value=\"$value\">";
9fa49e22 829 }
830 }
76c1650d 831 echo "<input type=submit value=\"$label\"></form>";
9fa49e22 832}
833
834function print_spacer($height=1, $width=1, $br=true) {
835 global $CFG;
76c1650d 836 echo "<img height=\"$height\" width=\"$width\" src=\"$CFG->wwwroot/pix/spacer.gif\" alt=\"\">";
9fa49e22 837 if ($br) {
76c1650d 838 echo "<br />\n";
9fa49e22 839 }
840}
841
842function print_file_picture($path, $courseid=0, $height="", $width="", $link="") {
843// Given the path to a picture file in a course, or a URL,
844// this function includes the picture in the page.
845 global $CFG;
846
847 if ($height) {
76c1650d 848 $height = "height=\"$height\"";
9fa49e22 849 }
850 if ($width) {
76c1650d 851 $width = "width=\"$width\"";
9fa49e22 852 }
853 if ($link) {
76c1650d 854 echo "<a href=\"$link\">";
9fa49e22 855 }
856 if (substr(strtolower($path), 0, 7) == "http://") {
76c1650d 857 echo "<img border=0 $height $width src=\"$path\">";
9fa49e22 858
859 } else if ($courseid) {
76c1650d 860 echo "<img border=0 $height $width src=\"";
9fa49e22 861 if ($CFG->slasharguments) { // Use this method if possible for better caching
862 echo "$CFG->wwwroot/file.php/$courseid/$path";
863 } else {
3f396065 864 echo "$CFG->wwwroot/file.php?file=/$courseid/$path";
9fa49e22 865 }
866 echo "\">";
867 } else {
868 echo "Error: must pass URL or course";
869 }
870 if ($link) {
76c1650d 871 echo "</a>";
9fa49e22 872 }
873}
874
875function print_user_picture($userid, $courseid, $picture, $large=false, $returnstring=false, $link=true) {
67a63a30 876 global $CFG, $THEME;
9fa49e22 877
878 if ($link) {
76c1650d 879 $output = "<a href=\"$CFG->wwwroot/user/view.php?id=$userid&course=$courseid\">";
9fa49e22 880 } else {
881 $output = "";
882 }
883 if ($large) {
67a63a30 884 $file = "f1";
9fa49e22 885 $size = 100;
886 } else {
67a63a30 887 $file = "f2";
9fa49e22 888 $size = 35;
889 }
67a63a30 890 if ($picture) { // Print custom user picture
9fa49e22 891 if ($CFG->slasharguments) { // Use this method if possible for better caching
67a63a30 892 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/user/pix.php/$userid/$file.jpg\"".
893 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\">";
9fa49e22 894 } else {
67a63a30 895 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/user/pix.php?file=/$userid/$file.jpg\"".
896 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\">";
9fa49e22 897 }
67a63a30 898 } else { // Print default user pictures (use theme version if available)
899 if (empty($THEME->custompix)) {
900 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/pix/u/$file.png\"".
901 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\">";
902 } else {
903 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/theme/$CFG->theme/pix/u/$file.png\"".
904 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\">";
905 }
906
9fa49e22 907 }
908 if ($link) {
76c1650d 909 $output .= "</a>";
9fa49e22 910 }
911
912 if ($returnstring) {
913 return $output;
914 } else {
915 echo $output;
916 }
917}
918
919function print_table($table) {
920// Prints a nicely formatted table.
921// $table is an object with several properties.
922// $table->head is an array of heading names.
923// $table->align is an array of column alignments
924// $table->size is an array of column sizes
5867bfb5 925// $table->wrap is an array of "nowrap"s or nothing
9fa49e22 926// $table->data[] is an array of arrays containing the data.
927// $table->width is an percentage of the page
928// $table->cellpadding padding on each cell
929// $table->cellspacing spacing between cells
930
931 if (isset($table->align)) {
932 foreach ($table->align as $key => $aa) {
933 if ($aa) {
76c1650d 934 $align[$key] = " align=\"$aa\"";
9fa49e22 935 } else {
936 $align[$key] = "";
937 }
938 }
939 }
940 if (isset($table->size)) {
941 foreach ($table->size as $key => $ss) {
942 if ($ss) {
76c1650d 943 $size[$key] = " width=\"$ss\"";
9fa49e22 944 } else {
945 $size[$key] = "";
946 }
947 }
948 }
5867bfb5 949 if (isset($table->wrap)) {
950 foreach ($table->wrap as $key => $ww) {
951 if ($ww) {
76c1650d 952 $wrap[$key] = " nowrap ";
5867bfb5 953 } else {
954 $wrap[$key] = "";
955 }
956 }
957 }
9fa49e22 958
9d378732 959 if (empty($table->width)) {
9fa49e22 960 $table->width = "80%";
961 }
962
9d378732 963 if (empty($table->cellpadding)) {
9fa49e22 964 $table->cellpadding = "5";
965 }
966
9d378732 967 if (empty($table->cellspacing)) {
9fa49e22 968 $table->cellspacing = "1";
969 }
970
5867bfb5 971 print_simple_box_start("center", "$table->width", "#ffffff", 0);
972 echo "<table width=100% border=0 valign=top align=center ";
9fa49e22 973 echo " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"generaltable\">\n";
974
b79f41cd 975 if (!empty($table->head)) {
5867bfb5 976 echo "<tr>";
9fa49e22 977 foreach ($table->head as $key => $heading) {
9d378732 978 if (!isset($size[$key])) {
979 $size[$key] = "";
980 }
981 if (!isset($align[$key])) {
982 $align[$key] = "";
983 }
5867bfb5 984 echo "<th valign=top ".$align[$key].$size[$key]." nowrap class=\"generaltableheader\">$heading</th>";
9fa49e22 985 }
986 echo "</TR>\n";
987 }
988
989 foreach ($table->data as $row) {
5867bfb5 990 echo "<tr valign=top>";
9fa49e22 991 foreach ($row as $key => $item) {
9d378732 992 if (!isset($size[$key])) {
993 $size[$key] = "";
994 }
995 if (!isset($align[$key])) {
996 $align[$key] = "";
997 }
5867bfb5 998 if (!isset($wrap[$key])) {
999 $wrap[$key] = "";
1000 }
1001 echo "<td ".$align[$key].$size[$key].$wrap[$key]." class=\"generaltablecell\">$item</td>";
9fa49e22 1002 }
5867bfb5 1003 echo "</tr>\n";
9fa49e22 1004 }
5867bfb5 1005 echo "</table>\n";
9fa49e22 1006 print_simple_box_end();
1007
1008 return true;
1009}
1010
1011function print_editing_switch($courseid) {
1012 global $CFG, $USER;
1013
1014 if (isteacher($courseid)) {
1015 if ($USER->editing) {
76c1650d 1016 echo "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid&edit=off\">turn editing off</a>";
9fa49e22 1017 } else {
76c1650d 1018 echo "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid&edit=on\">turn editing on</a>";
9fa49e22 1019 }
1020 }
1021}
1022
1023function print_textarea($richedit, $rows, $cols, $width, $height, $name, $value="") {
7d8f674d 1024/// Prints a richtext field or a normal textarea
9fa49e22 1025 global $CFG, $THEME;
1026
1027 if ($richedit) {
76c1650d 1028 echo "<object id=richedit style=\"background-color: buttonface\"";
9fa49e22 1029 echo " data=\"$CFG->wwwroot/lib/rte/richedit.html\"";
1030 echo " width=\"$width\" height=\"$height\" ";
1031 echo " type=\"text/x-scriptlet\" VIEWASTEXT></object>\n";
76c1650d 1032 echo "<textarea style=\"display:none\" name=\"$name\" rows=1 cols=1>";
9fa49e22 1033 p($value);
76c1650d 1034 echo "</textarea>\n";
9fa49e22 1035 } else {
76c1650d 1036 echo "<textarea name=\"$name\" rows=\"$rows\" cols=\"$cols\" wrap=virtual>";
9fa49e22 1037 p($value);
76c1650d 1038 echo "</textarea>\n";
9fa49e22 1039 }
1040}
1041
1042function print_richedit_javascript($form, $name, $source="no") {
76c1650d 1043 echo "<script language=\"javascript\" event=\"onload\" for=\"window\">\n";
9fa49e22 1044 echo " document.richedit.options = \"history=no;source=$source\";";
1045 echo " document.richedit.docHtml = $form.$name.innerText;";
76c1650d 1046 echo "</script>";
9fa49e22 1047}
1048
1049
1050function update_course_icon($courseid) {
1051// Used to be an icon, but it's now a simple form button
1052 global $CFG, $USER;
1053
1054 if (isteacher($courseid)) {
9c9f7d77 1055 if (!empty($USER->editing)) {
9fa49e22 1056 $string = get_string("turneditingoff");
1057 $edit = "off";
1058 } else {
1059 $string = get_string("turneditingon");
1060 $edit = "on";
1061 }
76c1650d 1062 return "<form target=_parent method=get action=\"$CFG->wwwroot/course/view.php\">".
1063 "<input type=hidden name=id value=\"$courseid\">".
1064 "<input type=hidden name=edit value=\"$edit\">".
1065 "<input type=submit value=\"$string\"></form>";
9fa49e22 1066 }
1067}
1068
1069function update_module_button($moduleid, $courseid, $string) {
1070// Prints the editing button on a module "view" page
1071 global $CFG;
1072
1073 if (isteacher($courseid)) {
1074 $string = get_string("updatethis", "", $string);
76c1650d 1075 return "<form target=_parent method=get action=\"$CFG->wwwroot/course/mod.php\">".
1076 "<input type=hidden name=update value=\"$moduleid\">".
1077 "<input type=hidden name=return value=\"true\">".
1078 "<input type=submit value=\"$string\"></form>";
9fa49e22 1079 }
1080}
1081
c2cb4545 1082function update_category_button($categoryid) {
d2b6ba70 1083// Prints the editing button on a category page
1084 global $CFG, $USER;
c2cb4545 1085
d2b6ba70 1086 if (iscreator()) {
1087 if (!empty($USER->editing)) {
1088 $string = get_string("turneditingoff");
1089 $edit = "off";
1090 } else {
1091 $string = get_string("turneditingon");
1092 $edit = "on";
1093 }
c2cb4545 1094 return "<form target=_parent method=get action=\"$CFG->wwwroot/course/category.php\">".
1095 "<input type=hidden name=id value=\"$categoryid\">".
d2b6ba70 1096 "<input type=hidden name=edit value=\"$edit\">".
1097 "<input type=submit value=\"$string\"></form>";
1098 }
1099}
1100
1101function update_categories_button() {
1102// Prints the editing button on categories listing
1103 global $CFG, $USER;
1104
1105 if (isadmin()) {
1106 if (!empty($USER->editing)) {
1107 $string = get_string("turneditingoff");
1108 $edit = "off";
1109 } else {
1110 $string = get_string("turneditingon");
1111 $edit = "on";
1112 }
1113 return "<form target=_parent method=get action=\"$CFG->wwwroot/course/index.php\">".
1114 "<input type=hidden name=edit value=\"$edit\">".
c2cb4545 1115 "<input type=submit value=\"$string\"></form>";
1116 }
1117}
9fa49e22 1118
1119function navmenu($course, $cm=NULL) {
1120// Given a course and a (current) coursemodule
1121// This function returns a small popup menu with all the
1122// course activity modules in it, as a navigation menu
1123// The data is taken from the serialised array stored in
1124// the course record
1125
1126 global $CFG;
1127
1128 if ($cm) {
1129 $cm = $cm->id;
1130 }
1131
1132 if ($course->format == 'weeks') {
1133 $strsection = get_string("week");
1134 } else {
1135 $strsection = get_string("topic");
1136 }
1137
1138 if (!$modinfo = unserialize($course->modinfo)) {
1139 return "";
1140 }
1141 $section = -1;
1142 $selected = "";
1143 foreach ($modinfo as $mod) {
1144 if ($mod->section > 0 and $section <> $mod->section) {
1145 $menu[] = "-------------- $strsection $mod->section --------------";
1146 }
1147 $section = $mod->section;
cf055081 1148 //Only add visible or teacher mods to jumpmenu
1149 if ($mod->visible or isteacher($course->id)) {
1150 $url = "$mod->mod/view.php?id=$mod->cm";
1151 if ($cm == $mod->cm) {
1152 $selected = $url;
1153 }
1154 $mod->name = urldecode($mod->name);
1155 if (strlen($mod->name) > 55) {
1156 $mod->name = substr($mod->name, 0, 50)."...";
1157 }
2a409368 1158 if (!$mod->visible) {
1159 $mod->name = "(".$mod->name.")";
1160 }
cf055081 1161 $menu[$url] = $mod->name;
9fa49e22 1162 }
9fa49e22 1163 }
1164
1165 return popup_form("$CFG->wwwroot/mod/", $menu, "navmenu", $selected, get_string("jumpto"), "", "", true);
1166}
1167
1168
1169
1170function print_date_selector($day, $month, $year, $currenttime=0) {
1171// Currenttime is a default timestamp in GMT
1172// Prints form items with the names $day, $month and $year
1173
1174 if (!$currenttime) {
1175 $currenttime = time();
1176 }
1177 $currentdate = usergetdate($currenttime);
1178
1179 for ($i=1; $i<=31; $i++) {
1180 $days[$i] = "$i";
1181 }
1182 for ($i=1; $i<=12; $i++) {
39e018b3 1183 $months[$i] = userdate(gmmktime(12,0,0,$i,1,2000), "%B");
9fa49e22 1184 }
1185 for ($i=2000; $i<=2010; $i++) {
1186 $years[$i] = $i;
1187 }
47f1da80 1188 choose_from_menu($days, $day, $currentdate['mday'], "");
1189 choose_from_menu($months, $month, $currentdate['mon'], "");
1190 choose_from_menu($years, $year, $currentdate['year'], "");
9fa49e22 1191}
1192
1193function print_time_selector($hour, $minute, $currenttime=0) {
1194// Currenttime is a default timestamp in GMT
1195// Prints form items with the names $hour and $minute
1196
1197 if (!$currenttime) {
1198 $currenttime = time();
1199 }
1200 $currentdate = usergetdate($currenttime);
1201 for ($i=0; $i<=23; $i++) {
1202 $hours[$i] = sprintf("%02d",$i);
1203 }
1204 for ($i=0; $i<=59; $i++) {
1205 $minutes[$i] = sprintf("%02d",$i);
1206 }
47f1da80 1207 choose_from_menu($hours, $hour, $currentdate['hours'], "");
1208 choose_from_menu($minutes, $minute, $currentdate['minutes'], "");
9fa49e22 1209}
1210
1211function error ($message, $link="") {
1212 global $CFG, $SESSION;
1213
1214 print_header(get_string("error"));
1215 echo "<BR>";
1216 print_simple_box($message, "center", "", "#FFBBBB");
1217
1218 if (!$link) {
1219 if ( !empty($SESSION->fromurl) ) {
1220 $link = "$SESSION->fromurl";
1221 unset($SESSION->fromurl);
9fa49e22 1222 } else {
c2cb4545 1223 $link = "$CFG->wwwroot/";
9fa49e22 1224 }
1225 }
1226 print_continue($link);
1227 print_footer();
1228 die;
1229}
1230
1231function helpbutton ($page, $title="", $module="moodle", $image=true, $linktext=false, $text="") {
1232 // $page = the keyword that defines a help page
1233 // $title = the title of links, rollover tips, alt tags etc
1234 // $module = which module is the page defined in
1235 // $image = use a help image for the link? (true/false/"both")
1236 // $text = if defined then this text is used in the page, and
1237 // the $page variable is ignored.
dc0dc7d5 1238 global $CFG, $THEME;
9fa49e22 1239
1240 if ($module == "") {
1241 $module = "moodle";
1242 }
1243
dc0dc7d5 1244 if (empty($THEME->custompix)) {
1245 $icon = "$CFG->wwwroot/pix/help.gif";
1246 } else {
1247 $icon = "$CFG->wwwroot/theme/$CFG->theme/pix/help.gif";
1248 }
1249
9fa49e22 1250 if ($image) {
1251 if ($linktext) {
dc0dc7d5 1252 $linkobject = "$title<img align=\"absmiddle\" border=0 height=17 width=22 alt=\"\" src=\"$icon\">";
9fa49e22 1253 } else {
dc0dc7d5 1254 $linkobject = "<img align=\"absmiddle\" border=0 height=17 width=22 alt=\"$title\" src=\"$icon\">";
9fa49e22 1255 }
1256 } else {
1257 $linkobject = $title;
1258 }
1259 if ($text) {
1260 $url = "/help.php?module=$module&text=".htmlentities(urlencode($text));
1261 } else {
1262 $url = "/help.php?module=$module&file=$page.html";
1263 }
1264 link_to_popup_window ($url, "popup", $linkobject, 400, 500, $title);
1265}
1266
e825f279 1267function emoticonhelpbutton($form, $field) {
1268/// Prints a special help button that is a link to the "live" emoticon popup
1269 global $CFG, $SESSION;
1270
1271 $SESSION->inserttextform = $form;
1272 $SESSION->inserttextfield = $field;
1273 helpbutton("emoticons", get_string("helpemoticons"), "moodle", false, true);
1274 echo "&nbsp;<img src=\"$CFG->wwwroot/pix/s/smiley.gif\" align=\"absmiddle\" width=15 height=15></a>";
1275}
1276
9fa49e22 1277function notice ($message, $link="") {
750ab759 1278 global $CFG, $THEME;
9fa49e22 1279
1280 if (!$link) {
750ab759 1281 if (!empty($_SERVER["HTTP_REFERER"])) {
1282 $link = $_SERVER["HTTP_REFERER"];
1283 } else {
c2cb4545 1284 $link = "$CFG->wwwroot/";
750ab759 1285 }
9fa49e22 1286 }
1287
01d79966 1288 echo "<br />";
11a876e1 1289 print_simple_box($message, "center", "50%", "$THEME->cellheading", "20", "noticebox");
eb347b6b 1290 print_heading("<a href=\"$link\">".get_string("continue")."</a>");
9fa49e22 1291 print_footer(get_site());
1292 die;
1293}
1294
1295function notice_yesno ($message, $linkyes, $linkno) {
1296 global $THEME;
1297
eb347b6b 1298 print_simple_box_start("center", "60%", "$THEME->cellheading");
1299 echo "<p align=center><font size=3>$message</font></p>";
1300 echo "<p align=center><font size=3><b>";
1301 echo "<a href=\"$linkyes\">".get_string("yes")."</a>";
9fa49e22 1302 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
eb347b6b 1303 echo "<a href=\"$linkno\">".get_string("no")."</a>";
1304 echo "</b></font></p>";
9fa49e22 1305 print_simple_box_end();
1306}
1307
1308function redirect($url, $message="", $delay=0) {
1309// Uses META tags to redirect the user, after printing a notice
1310
c9082a8c 1311 if (empty($message)) {
76c1650d 1312 echo "<meta http-equiv='refresh' content='$delay; url=$url'>";
c9082a8c 1313 } else {
1314 if (! $delay) {
1315 $delay = 3; // There's no point having a message with no delay
1316 }
76c1650d 1317 echo "<meta http-equiv='refresh' content='$delay; url=$url'>";
9fa49e22 1318 print_header();
76c1650d 1319 echo "<center>";
1320 echo "<p>$message</p>";
1321 echo "<p>( <a href=\"$url\">".get_string("continue")."</a> )</p>";
1322 echo "</center>";
9fa49e22 1323 }
1324 die;
1325}
1326
99988d1a 1327function notify ($message, $color="red", $align="center") {
1328 echo "<p align=\"$align\"><b><font color=\"$color\">$message</font></b></p>\n";
9fa49e22 1329}
1330
43373804 1331function obfuscate_email($email) {
1332/// Given an email address, this function will return an obfuscated version of it
1333 $i = 0;
1334 $length = strlen($email);
1335 $obfuscated = "";
1336 while ($i < $length) {
1337 if (rand(0,2)) {
1338 $obfuscated.='%'.dechex(ord($email{$i}));
1339 } else {
1340 $obfuscated.=$email{$i};
1341 }
1342 $i++;
1343 }
1344 return $obfuscated;
1345}
1346
1347function obfuscate_text($plaintext) {
1348/// This function takes some text and replaces about half of the characters
1349/// with HTML entity equivalents. Return string is obviously longer.
1350 $i=0;
1351 $length = strlen($plaintext);
1352 $obfuscated="";
1353 while ($i < $length) {
1354 if (rand(0,2)) {
1355 $obfuscated.='&#'.ord($plaintext{$i});
1356 } else {
1357 $obfuscated.=$plaintext{$i};
1358 }
1359 $i++;
1360 }
1361 return $obfuscated;
1362}
1363
1364function obfuscate_mailto($email, $label="") {
1365/// This function uses the above two functions to generate a fully
1366/// obfuscated email link, ready to use.
1367
1368 if (empty($label)) {
1369 $label = $email;
1370 }
1371 return sprintf('<a href="%s:%s" title="%s">%s</a>', obfuscate_text('mailto'),
1372 obfuscate_email($email),
1373 obfuscate_text($email),
1374 obfuscate_text($label));
1375}
1376
9fa49e22 1377
9d5b689c 1378// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
f9903ed0 1379?>