New library for collecting routines related to GD
[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
39dda0fc 40$ALLOWED_TAGS =
4110d37f 41"<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><acronym><nolink><style>";
3fe3851d 42
43
0095d5cd 44/// Functions
45
3662bce5 46function s($var) {
c1d57101 47/// returns $var with HTML characters (like "<", ">", etc.) properly quoted,
f9903ed0 48
3662bce5 49 if (empty($var)) {
50 return "";
51 }
7d8f674d 52 return htmlSpecialChars(stripslashes_safe($var));
f9903ed0 53}
54
3662bce5 55function p($var) {
c1d57101 56/// prints $var with HTML characters (like "<", ">", etc.) properly quoted,
f9903ed0 57
3662bce5 58 if (empty($var)) {
59 echo "";
60 }
7d8f674d 61 echo htmlSpecialChars(stripslashes_safe($var));
f9903ed0 62}
63
8553b700 64function nvl(&$var, $default="") {
c1d57101 65/// if $var is undefined, return $default, otherwise return $var
8553b700 66
67 return isset($var) ? $var : $default;
68}
f9903ed0 69
70function strip_querystring($url) {
c1d57101 71/// takes a URL and returns it without the querystring portion
f9903ed0 72
b9b8ab69 73 if ($commapos = strpos($url, '?')) {
74 return substr($url, 0, $commapos);
75 } else {
76 return $url;
77 }
f9903ed0 78}
79
80function get_referer() {
c1d57101 81/// returns the URL of the HTTP_REFERER, less the querystring portion
f9903ed0 82
607809b3 83 return strip_querystring(nvl($_SERVER["HTTP_REFERER"]));
f9903ed0 84}
85
c1d57101 86
f9903ed0 87function me() {
c1d57101 88/// returns the name of the current script, WITH the querystring portion.
eaa50dbc 89/// this function is necessary because PHP_SELF and REQUEST_URI and SCRIPT_NAME
c1d57101 90/// return different things depending on a lot of things like your OS, Web
91/// server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.)
f9903ed0 92
607809b3 93 if (!empty($_SERVER["REQUEST_URI"])) {
94 return $_SERVER["REQUEST_URI"];
c1d57101 95
607809b3 96 } else if (!empty($_SERVER["PHP_SELF"])) {
fced815c 97 if (!empty($_SERVER["QUERY_STRING"])) {
98 return $_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"];
99 }
607809b3 100 return $_SERVER["PHP_SELF"];
c1d57101 101
fced815c 102 } else if (!empty($_SERVER["SCRIPT_NAME"])) {
103 if (!empty($_SERVER["QUERY_STRING"])) {
104 return $_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
105 }
106 return $_SERVER["SCRIPT_NAME"];
107
b9b8ab69 108 } else {
fced815c 109 notify("Warning: Could not find any of these web server variables: \$REQUEST_URI, \$PHP_SELF or \$SCRIPT_NAME");
bcdfe14e 110 return false;
7fbd6b1c 111 }
f9903ed0 112}
113
114
f9903ed0 115function qualified_me() {
c1d57101 116/// like me() but returns a full URL
f9903ed0 117
39e018b3 118 if (!empty($_SERVER["HTTP_HOST"])) {
119 $hostname = $_SERVER["HTTP_HOST"];
120 } else if (!empty($_ENV["HTTP_HOST"])) {
121 $hostname = $_ENV["HTTP_HOST"];
df3fd249 122 } else if (!empty($_SERVER["SERVER_NAME"])) {
123 $hostname = $_SERVER["SERVER_NAME"];
39e018b3 124 } else if (!empty($_ENV["SERVER_NAME"])) {
125 $hostname = $_ENV["SERVER_NAME"];
126 } else {
127 notify("Warning: could not find the name of this server!");
bcdfe14e 128 return false;
c1d57101 129 }
f9903ed0 130
607809b3 131 $protocol = (isset($_SERVER["HTTPS"]) and $_SERVER["HTTPS"] == "on") ? "https://" : "http://";
39e018b3 132 $url_prefix = $protocol.$hostname;
b9b8ab69 133 return $url_prefix . me();
f9903ed0 134}
135
136
a0deb5db 137function match_referer($goodreferer = "") {
138/// returns true if the referer is the same as the goodreferer. If
139/// goodreferer is not specified, use qualified_me as the goodreferer
60f18531 140 global $CFG;
141
ae384ef1 142 if (empty($CFG->secureforms)) { // Don't bother checking referer
60f18531 143 return true;
144 }
f9903ed0 145
ae384ef1 146 if ($goodreferer == "nomatch") { // Don't bother checking referer
a0deb5db 147 return true;
148 }
149
150 if (empty($goodreferer)) {
151 $goodreferer = qualified_me();
c1d57101 152 }
a0deb5db 153 return $goodreferer == get_referer();
f9903ed0 154}
155
36b4f985 156function data_submitted($url="") {
157/// Used on most forms in Moodle to check for data
158/// Returns the data as an object, if it's found.
607809b3 159/// This object can be used in foreach loops without
160/// casting because it's cast to (array) automatically
36b4f985 161///
162/// Checks that submitted POST data exists, and also
163/// checks the referer against the given url (it uses
164/// the current page if none was specified.
165
37208cd2 166 global $CFG;
167
607809b3 168 if (empty($_POST)) {
36b4f985 169 return false;
607809b3 170
36b4f985 171 } else {
172 if (match_referer($url)) {
607809b3 173 return (object)$_POST;
36b4f985 174 } else {
175 if ($CFG->debug > 10) {
176 notice("The form did not come from this page! (referer = ".get_referer().")");
177 }
178 return false;
179 }
180 }
181}
182
7d8f674d 183function stripslashes_safe($string) {
184/// stripslashes() removes ALL backslashes even from strings
185/// so C:\temp becomes C:temp ... this isn't good.
186/// The following should work as a fairly safe replacement
187/// to be called on quoted AND unquoted strings (to be sure)
188
189 $string = str_replace("\\'", "'", $string);
190 $string = str_replace('\\"', '"', $string);
191 $string = str_replace('\\\\', '\\', $string);
192 return $string;
193}
f9903ed0 194
72e4eac6 195if (!function_exists('str_ireplace')) {
7ec2fc00 196 function str_ireplace($find, $replace, $string) {
72e4eac6 197 /// This does a search and replace, ignoring case
3528bae2 198 /// This function is only here versions of PHP older than version 5
199 /// may not have a native version of this function.
200 /// Taken from the PHP manual, by bradhuizenga@softhome.net
7ec2fc00 201
202 if (!is_array($find)) {
203 $find = array($find);
204 }
205
206 if(!is_array($replace)) {
207 if (!is_array($find)) {
208 $replace = array($replace);
209 } else {
210 // this will duplicate the string into an array the size of $find
211 $c = count($find);
212 $rString = $replace;
213 unset($replace);
214 for ($i = 0; $i < $c; $i++) {
215 $replace[$i] = $rString;
216 }
217 }
218 }
219
220 foreach ($find as $fKey => $fItem) {
221 $between = explode(strtolower($fItem),strtolower($string));
222 $pos = 0;
223 foreach($between as $bKey => $bItem) {
224 $between[$bKey] = substr($string,$pos,strlen($bItem));
225 $pos += strlen($bItem) + strlen($fItem);
226 }
227 $string = implode($replace[$fKey],$between);
72e4eac6 228 }
7ec2fc00 229 return ($string);
3fe3851d 230 }
3fe3851d 231}
232
f9903ed0 233function read_template($filename, &$var) {
c1d57101 234/// return a (big) string containing the contents of a template file with all
235/// the variables interpolated. all the variables must be in the $var[] array or
236/// object (whatever you decide to use).
237///
238/// WARNING: do not use this on big files!!
f9903ed0 239
b9b8ab69 240 $temp = str_replace("\\", "\\\\", implode(file($filename), ""));
241 $temp = str_replace('"', '\"', $temp);
242 eval("\$template = \"$temp\";");
243 return $template;
f9903ed0 244}
245
246function checked(&$var, $set_value = 1, $unset_value = 0) {
c1d57101 247/// if variable is set, set it to the set_value otherwise set it to the
248/// unset_value. used to handle checkboxes when you are expecting them from
249/// a form
f9903ed0 250
b9b8ab69 251 if (empty($var)) {
252 $var = $unset_value;
253 } else {
254 $var = $set_value;
255 }
f9903ed0 256}
257
258function frmchecked(&$var, $true_value = "checked", $false_value = "") {
c1d57101 259/// prints the word "checked" if a variable is true, otherwise prints nothing,
260/// used for printing the word "checked" in a checkbox form input
f9903ed0 261
b9b8ab69 262 if ($var) {
263 echo $true_value;
264 } else {
265 echo $false_value;
266 }
f9903ed0 267}
268
269
86aa7ccf 270function link_to_popup_window ($url, $name="popup", $linkname="click here",
b48f834c 271 $height=400, $width=500, $title="Popup window", $options="none") {
c1d57101 272/// This will create a HTML link that will work on both
273/// Javascript and non-javascript browsers.
274/// Relies on the Javascript function openpopup in javascript.php
275/// $url must be relative to home page eg /mod/survey/stuff.php
f9903ed0 276
ff80e012 277 global $CFG;
278
b48f834c 279 if ($options == "none") {
280 $options = "menubar=0,location=0,scrollbars,resizable,width=$width,height=$height";
281 }
86aa7ccf 282 $fullscreen = 0;
f9903ed0 283
55e4b5f9 284 echo "<a target=\"$name\" title=\"$title\" href=\"$CFG->wwwroot$url\" ".
86aa7ccf 285 "onClick=\"return openpopup('$url', '$name', '$options', $fullscreen);\">$linkname</a>\n";
f9903ed0 286}
287
86aa7ccf 288
f9903ed0 289function close_window_button() {
c1d57101 290/// Prints a simple button to close a window
291
86aa7ccf 292 echo "<center>\n";
293 echo "<script>\n";
294 echo "<!--\n";
295 echo "document.write('<form>');\n";
66a51452 296 echo "document.write('<input type=\"button\" onClick=\"self.close();\" value=\"".get_string("closewindow")."\" />');\n";
86aa7ccf 297 echo "document.write('</form>');\n";
298 echo "-->\n";
299 echo "</script>\n";
300 echo "<noscript>\n";
301 echo "<a href=\"".$_SERVER['HTTP_REFERER']."\"><---</a>\n";
302 echo "</noscript>\n";
303 echo "</center>\n";
f9903ed0 304}
305
306
08056730 307function choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", $nothingvalue="0", $return=false) {
c1d57101 308/// Given an array of value, creates a popup menu to be part of a form
309/// $options["value"]["label"]
f9903ed0 310
618b22c5 311 if ($nothing == "choose") {
312 $nothing = get_string("choose")."...";
313 }
314
f9903ed0 315 if ($script) {
316 $javascript = "onChange=\"$script\"";
9c9f7d77 317 } else {
318 $javascript = "";
f9903ed0 319 }
9c9f7d77 320
66a51452 321 $output = "<select name=\"$name\" $javascript>\n";
bda8d43a 322 if ($nothing) {
76c1650d 323 $output .= " <option value=\"$nothingvalue\"\n";
bda8d43a 324 if ($nothingvalue == $selected) {
66a51452 325 $output .= " selected=\"true\"";
bda8d43a 326 }
76c1650d 327 $output .= ">$nothing</option>\n";
873960de 328 }
607809b3 329 if (!empty($options)) {
330 foreach ($options as $value => $label) {
76c1650d 331 $output .= " <option value=\"$value\"";
607809b3 332 if ($value == $selected) {
66a51452 333 $output .= " selected=\"true\"";
607809b3 334 }
a20c1090 335 if ($label === "") {
76c1650d 336 $output .= ">$value</option>\n";
a20c1090 337 } else {
338 $output .= ">$label</option>\n";
607809b3 339 }
f9903ed0 340 }
341 }
76c1650d 342 $output .= "</select>\n";
08056730 343
344 if ($return) {
345 return $output;
346 } else {
347 echo $output;
348 }
f9903ed0 349}
350
16ef5e78 351function popup_form ($common, $options, $formname, $selected="", $nothing="choose", $help="", $helptext="", $return=false, $targetwindow="self") {
c1d57101 352/// Implements a complete little popup form
353/// $common = the URL up to the point of the variable that changes
354/// $options = A list of value-label pairs for the popup list
355/// $formname = name must be unique on the page
356/// $selected = the option that is already selected
357/// $nothing = The label for the "no choice" option
e5dfd0f3 358/// $help = The name of a help page if help is required
359/// $helptext = The name of the label for the help button
5b472756 360/// $return = Boolean indicating whether the function should return the text
361/// as a string or echo it directly to the page being rendered
f9903ed0 362
0d0baabf 363 global $CFG;
364
618b22c5 365 if ($nothing == "choose") {
366 $nothing = get_string("choose")."...";
367 }
368
66a51452 369 $startoutput = "<form target=\"{$CFG->framename}\" name=\"$formname\">";
370 $output = "<select name=\"popup\" onchange=\"$targetwindow.location=document.$formname.popup.options[document.$formname.popup.selectedIndex].value\">\n";
f9903ed0 371
372 if ($nothing != "") {
dfec7b01 373 $output .= " <option value=\"javascript:void(0)\">$nothing</option>\n";
f9903ed0 374 }
375
376 foreach ($options as $value => $label) {
d897cae4 377 if (substr($label,0,1) == "-") {
dfec7b01 378 $output .= " <option value=\"\"";
d897cae4 379 } else {
dfec7b01 380 $output .= " <option value=\"$common$value\"";
d897cae4 381 if ($value == $selected) {
66a51452 382 $output .= " selected=\"true\"";
d897cae4 383 }
f9903ed0 384 }
385 if ($label) {
dfec7b01 386 $output .= ">$label</option>\n";
f9903ed0 387 } else {
dfec7b01 388 $output .= ">$value</option>\n";
f9903ed0 389 }
390 }
dfec7b01 391 $output .= "</select>";
392 $output .= "</form>\n";
d897cae4 393
394 if ($return) {
dfec7b01 395 return $startoutput.$output;
d897cae4 396 } else {
dfec7b01 397 echo $startoutput;
9c9f7d77 398 if ($help) {
399 helpbutton($help, $helptext);
400 }
d897cae4 401 echo $output;
402 }
f9903ed0 403}
404
405
406
407function formerr($error) {
c1d57101 408/// Prints some red text
f9903ed0 409 if (!empty($error)) {
66a51452 410 echo "<font color=\"#ff0000\">$error</font>";
f9903ed0 411 }
412}
413
414
415function validate_email ($address) {
66a51452 416/// Validates an email to make sure it makes sense.
f9903ed0 417 return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.
418 '@'.
419 '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
420 '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
421 $address));
422}
423
6c8e8b5e 424function detect_munged_arguments($string) {
393c9b4f 425 if (ereg('\.\.', $string)) { // check for parent URLs
6c8e8b5e 426 return true;
427 }
393c9b4f 428 if (ereg('[\|\`]', $string)) { // check for other bad characters
6c8e8b5e 429 return true;
430 }
431 return false;
432}
433
6ed3da1d 434function get_slash_arguments($file="file.php") {
435/// Searches the current environment variables for some slash arguments
f9903ed0 436
eaa50dbc 437 if (!$string = me()) {
f9903ed0 438 return false;
439 }
eaa50dbc 440
6ed3da1d 441 $pathinfo = explode($file, $string);
442
bcdfe14e 443 if (!empty($pathinfo[1])) {
444 return $pathinfo[1];
6ed3da1d 445 } else {
446 return false;
447 }
448}
449
450function parse_slash_arguments($string, $i=0) {
451/// Extracts arguments from "/foo/bar/something"
452/// eg http://mysite.com/script.php/foo/bar/something
f9903ed0 453
6c8e8b5e 454 if (detect_munged_arguments($string)) {
780db230 455 return false;
456 }
6ed3da1d 457 $args = explode("/", $string);
f9903ed0 458
459 if ($i) { // return just the required argument
460 return $args[$i];
461
462 } else { // return the whole array
463 array_shift($args); // get rid of the empty first one
464 return $args;
465 }
466}
467
0095d5cd 468function format_text_menu() {
c1d57101 469/// Just returns an array of formats suitable for a popup menu
0095d5cd 470 return array (FORMAT_MOODLE => get_string("formattext"),
6901fa79 471 FORMAT_HTML => get_string("formathtml"),
d342c763 472 FORMAT_PLAIN => get_string("formatplain"),
473 FORMAT_WIKI => get_string("formatwiki"));
0095d5cd 474}
475
60f18531 476function format_text($text, $format=FORMAT_MOODLE, $options=NULL) {
c1d57101 477/// Given text in a variety of format codings, this function returns
478/// the text as safe HTML.
479///
480/// $text is raw text (originally from a user)
481/// $format is one of the format constants, defined above
0095d5cd 482
e67b9e31 483 global $CFG, $course;
a751a4e5 484
0095d5cd 485 switch ($format) {
73f8658c 486 case FORMAT_HTML:
5f350e8f 487 replace_smilies($text);
e67b9e31 488 return filter_text($text);
73f8658c 489 break;
490
6901fa79 491 case FORMAT_PLAIN:
492 $text = htmlentities($text);
3405b212 493 $text = str_replace(" ", "&nbsp; ", $text);
5f350e8f 494 replace_smilies($text);
6901fa79 495 $text = nl2br($text);
496 return $text;
497 break;
498
d342c763 499 case FORMAT_WIKI:
e67b9e31 500 $text = wiki_to_html($text);
501 return filter_text($text);
d342c763 502 break;
503
73f8658c 504 default: // FORMAT_MOODLE or anything else
c9dda990 505 if (!isset($options->smiley)) {
506 $options->smiley=true;
507 }
508 if (!isset($options->para)) {
1a072208 509 $options->para=true;
c9dda990 510 }
e67b9e31 511 $text = text_to_html($text, $options->smiley, $options->para);
512 return filter_text($text);
513
0095d5cd 514 break;
0095d5cd 515 }
516}
517
d342c763 518function format_text_email($text, $format) {
519/// Given text in a variety of format codings, this function returns
520/// the text as plain text suitable for plain email.
521///
522/// $text is raw text (originally from a user)
523/// $format is one of the format constants, defined above
524
525 switch ($format) {
526
527 case FORMAT_PLAIN:
528 return $text;
529 break;
530
531 case FORMAT_WIKI:
532 $text = wiki_to_html($text);
5b472756 533 /// This expression turns links into something nice in a text format. (Russell Jungwirth)
534 /// From: http://php.net/manual/en/function.eregi-replace.php and simplified
535 $text = eregi_replace('(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)','\\3 [\\2]', $text);
7c55a29b 536 return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES)));
d342c763 537 break;
538
6ff45b59 539 case FORMAT_HTML:
540 return html_to_text($text);
541 break;
542
d342c763 543 default: // FORMAT_MOODLE or anything else
5b472756 544 $text = eregi_replace('(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)','\\3 [\\2]', $text);
7c55a29b 545 return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES)));
d342c763 546 break;
547 }
548}
0095d5cd 549
e67b9e31 550
551function filter_text($text) {
552/// Given some text in HTML format, this function will pass it
553/// through any filters that have been defined in $CFG->textfilterx
554/// The variable defines a filepath to a file containing the
555/// filter function. The file must contain a variable called
556/// $textfilter_function which contains the name of the function
557/// with $course->id and $text parameters
558
559 global $CFG, $course; // A dirty hack right now ... should not be assumed global
560
561 if (empty($course->id)) {
562 return $text;
563 }
564
565 for ($i=1; $i<=10; $i++) {
566 $variable = "textfilter$i";
567 if (empty($CFG->$variable)) { /// No more filters
568 return $text;
569 }
570 if (is_readable("$CFG->dirroot/".$CFG->$variable)) {
571 include("$CFG->dirroot/".$CFG->$variable);
572 $text = $textfilter_function($course->id, $text);
573 }
574 }
575 return $text;
576}
577
578
0095d5cd 579function clean_text($text, $format) {
c1d57101 580/// Given raw text (eg typed in by a user), this function cleans it up
581/// and removes any nasty tags that could mess up Moodle pages.
b7a3cf49 582
fc120758 583 global $ALLOWED_TAGS;
3fe3851d 584
d342c763 585 switch ($format) {
0095d5cd 586 case FORMAT_MOODLE:
0095d5cd 587 case FORMAT_HTML:
d342c763 588 case FORMAT_WIKI:
5b472756 589 /// Remove javascript: label
3fe3851d 590 $text = strip_tags($text, $ALLOWED_TAGS);
5b472756 591 /// Remove javascript/VBScript
592 $text = str_ireplace("javascript:", "xxx", $text);
593 /// Remove script events
594 $text = eregi_replace("([^a-z])language([[:space:]]*)=", "xxx", $text);
595 $text = eregi_replace("([^a-z])on([a-z]+)([[:space:]]*)=", "xxx", $text);
3fe3851d 596 return $text;
6901fa79 597
598 case FORMAT_PLAIN:
599 return $text;
0095d5cd 600 }
b7a3cf49 601}
f9903ed0 602
5f350e8f 603function replace_smilies(&$text) {
c1d57101 604/// Replaces all known smileys in the text with image equivalents
2ea9027b 605 global $CFG;
c1d57101 606
5b472756 607/// this builds the mapping array only once
617778f2 608 static $runonce = false;
69081931 609 static $e = array();
610 static $img = array();
617778f2 611 static $emoticons = array(
fbfc2675 612 ':-)' => 'smiley',
613 ':)' => 'smiley',
614 ':-D' => 'biggrin',
615 ';-)' => 'wink',
616 ':-/' => 'mixed',
617 'V-.' => 'thoughtful',
618 ':-P' => 'tongueout',
619 'B-)' => 'cool',
620 '^-)' => 'approve',
621 '8-)' => 'wideeyes',
622 ':o)' => 'clown',
623 ':-(' => 'sad',
624 ':(' => 'sad',
625 '8-.' => 'shy',
626 ':-I' => 'blush',
627 ':-X' => 'kiss',
628 '8-o' => 'surprise',
629 'P-|' => 'blackeye',
630 '8-[' => 'angry',
631 'xx-P' => 'dead',
632 '|-.' => 'sleepy',
633 '}-]' => 'evil',
2ea9027b 634 );
635
fbfc2675 636 if ($runonce == false) { /// After the first time this is not run again
617778f2 637 foreach ($emoticons as $emoticon => $image){
fbfc2675 638 $alttext = get_string($image, 'pix');
639
69081931 640 $e[] = $emoticon;
fbfc2675 641 $img[] = "<img alt=\"$alttext\" width=\"15\" height=\"15\" src=\"$CFG->pixpath/s/$image.gif\" />";
617778f2 642 }
643 $runonce = true;
c0f728ba 644 }
b7a3cf49 645
fbfc2675 646/// this is the meat of the code - this is run every time
5f350e8f 647 $text = str_replace($e, $img, $text);
1a072208 648}
0095d5cd 649
909f539d 650function text_to_html($text, $smiley=true, $para=true) {
c1d57101 651/// Given plain text, makes it into HTML as nicely as possible.
652/// May contain HTML tags already
f9903ed0 653
27326a3e 654 global $CFG;
655
c1d57101 656/// Remove any whitespace that may be between HTML tags
7b3be1b1 657 $text = eregi_replace(">([[:space:]]+)<", "><", $text);
658
c1d57101 659/// Remove any returns that precede or follow HTML tags
0eae8049 660 $text = eregi_replace("([\n\r])<", " <", $text);
661 $text = eregi_replace(">([\n\r])", "> ", $text);
7b3be1b1 662
5f350e8f 663 convert_urls_into_links($text);
f9903ed0 664
c1d57101 665/// Make returns into HTML newlines.
f9903ed0 666 $text = nl2br($text);
667
c1d57101 668/// Turn smileys into images.
d69cb7f4 669 if ($smiley) {
5f350e8f 670 replace_smilies($text);
d69cb7f4 671 }
f9903ed0 672
c1d57101 673/// Wrap the whole thing in a paragraph tag if required
909f539d 674 if ($para) {
01d79966 675 return "<p>".$text."</p>";
909f539d 676 } else {
677 return $text;
678 }
f9903ed0 679}
680
3e9ca9fb 681function wiki_to_html($text) {
01d79966 682/// Given Wiki formatted text, make it into XHTML using external function
43373804 683 global $CFG;
3e9ca9fb 684
43373804 685 require_once("$CFG->libdir/wiki.php");
3e9ca9fb 686
01d79966 687 $wiki = new Wiki;
688 return $wiki->format($text);
3e9ca9fb 689}
690
6ff45b59 691function html_to_text($html) {
692/// Given HTML text, make it into plain text using external function
428aaa29 693 global $CFG;
6ff45b59 694
695 require_once("$CFG->libdir/html2text.php");
696
697 return html2text($html);
698}
699
700
5f350e8f 701function convert_urls_into_links(&$text) {
702/// Given some text, it converts any URLs it finds into HTML links.
703
704/// Make lone URLs into links. eg http://moodle.com/
3405b212 705 $text = eregi_replace("([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
88438a58 706 "\\1<a href=\"\\2://\\3\\4\" target=\"newpage\">\\2://\\3\\4</a>", $text);
5f350e8f 707
708/// eg www.moodle.com
3405b212 709 $text = eregi_replace("([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])",
88438a58 710 "\\1<a href=\"http://www.\\2\\3\" target=\"newpage\">www.\\2\\3</a>", $text);
5f350e8f 711}
712
88438a58 713function highlight($needle, $haystack, $case=0,
714 $left_string="<span class=\"highlight\">", $right_string="</span>") {
715/// This function will highlight search words in a given string
716/// It cares about HTML and will not ruin links. It's best to use
717/// this function after performing any conversions to HTML.
718/// Function found here: http://forums.devshed.com/t67822/scdaa2d1c3d4bacb4671d075ad41f0854.html
719
69d51d3a 720 if (empty($needle)) {
721 return $haystack;
722 }
723
88438a58 724 $list_of_words = eregi_replace("[^-a-zA-Z0-9&']", " ", $needle);
725 $list_array = explode(" ", $list_of_words);
726 for ($i=0; $i<sizeof($list_array); $i++) {
727 if (strlen($list_array[$i]) == 1) {
728 $list_array[$i] = "";
729 }
730 }
731 $list_of_words = implode(" ", $list_array);
732 $list_of_words_cp = $list_of_words;
733 $final = array();
734 preg_match_all('/<(.+?)>/is',$haystack,$list_of_words);
735
736 foreach (array_unique($list_of_words[0]) as $key=>$value) {
737 $final['<|'.$key.'|>'] = $value;
738 }
739
740 $haystack = str_replace($final,array_keys($final),$haystack);
741 $list_of_words_cp = eregi_replace(" +", "|", $list_of_words_cp);
742
743 if ($list_of_words_cp{0}=="|") {
744 $list_of_words_cp{0} = "";
745 }
746 if ($list_of_words_cp{strlen($list_of_words_cp)-1}=="|") {
747 $list_of_words_cp{strlen($list_of_words_cp)-1}="";
748 }
749 $list_of_words_cp = "(".trim($list_of_words_cp).")";
750
751 if (!$case){
752 $haystack = eregi_replace("$list_of_words_cp", "$left_string"."\\1"."$right_string", $haystack);
753 } else {
754 $haystack = ereg_replace("$list_of_words_cp", "$left_string"."\\1"."$right_string", $haystack);
755 }
756 $haystack = str_replace(array_keys($final),$final,$haystack);
757
758 return stripslashes($haystack);
759}
760
761function highlightfast($needle, $haystack) {
c1d57101 762/// This function will highlight instances of $needle in $haystack
88438a58 763/// It's faster that the above function and doesn't care about
764/// HTML or anything.
5af78ed2 765
766 $parts = explode(strtolower($needle), strtolower($haystack));
767
768 $pos = 0;
769
770 foreach ($parts as $key => $part) {
771 $parts[$key] = substr($haystack, $pos, strlen($part));
772 $pos += strlen($part);
773
88438a58 774 $parts[$key] .= "<span class=\"highlight\">".substr($haystack, $pos, strlen($needle))."</span>";
5af78ed2 775 $pos += strlen($needle);
776 }
777
778 return (join('', $parts));
779}
780
f9903ed0 781
9fa49e22 782/// STANDARD WEB PAGE PARTS ///////////////////////////////////////////////////
783
66a51452 784function print_header ($title="", $heading="", $navigation="", $focus="", $meta="",
785 $cache=true, $button="&nbsp;", $menu="", $usexml=false) {
9fa49e22 786// $title - appears top of window
787// $heading - appears top of page
788// $navigation - premade navigation string
789// $focus - indicates form element eg inputform.password
790// $meta - meta tags in the header
791// $cache - should this page be cacheable?
792// $button - HTML code for a button (usually for module editing)
66a51452 793// $menu - HTML code for a popup menu
794// $usexml - use XML for this page
e825f279 795 global $USER, $CFG, $THEME, $SESSION;
9fa49e22 796
797 if (file_exists("$CFG->dirroot/theme/$CFG->theme/styles.php")) {
798 $styles = $CFG->stylesheet;
799 } else {
800 $styles = "$CFG->wwwroot/theme/standard/styles.php";
801 }
802
803 if ($navigation == "home") {
804 $home = true;
805 $navigation = "";
9d378732 806 } else {
807 $home = false;
9fa49e22 808 }
809
810 if ($button == "") {
811 $button = "&nbsp;";
812 }
813
814 if (!$menu and $navigation) {
815 if (isset($USER->id)) {
66a51452 816 $menu = "<font size=\"2\"><a target=\"$CFG->framename\" href=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</a></font>";
9fa49e22 817 } else {
66a51452 818 $menu = "<font size=\"2\"><a target=\"$CFG->framename\" href=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</a></font>";
9fa49e22 819 }
820 }
821
47037513 822 // Add a stylesheet for the HTML editor
823 $meta = "<style type=\"text/css\">@import url($CFG->wwwroot/lib/editor/htmlarea.css);</style>\n$meta\n";
824
9fa49e22 825 // Specify character set ... default is iso-8859-1 but some languages might need something else
826 // Could be optimised by carrying the charset variable around in $USER
827 if (current_language() == "en") {
66a51452 828 $meta = "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\" />\n$meta\n";
9fa49e22 829 } else {
66a51452 830 $meta = "<meta http-equiv=\"content-type\" content=\"text/html; charset=".get_string("thischarset")."\" />\n$meta\n";
9fa49e22 831 }
832
d8152d04 833 if ( get_string("thisdirection") == "rtl" ) {
107b010b 834 $direction = " dir=\"rtl\"";
9fa49e22 835 } else {
107b010b 836 $direction = " dir=\"ltr\"";
9fa49e22 837 }
838
839 if (!$cache) { // Do everything we can to prevent clients and proxies caching
840 @header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
841 @header("Pragma: no-cache");
66a51452 842 $meta .= "\n<meta http-equiv=\"pragma\" content=\"no-cache\" />";
843 $meta .= "\n<meta http-equiv=\"expires\" content=\"0\" />";
844 }
845
846 if ($usexml) { // Added by Gustav Delius / Mad Alex for MathML output
847 $currentlanguage = current_language();
848
849 @header("Content-type: text/xml");
850 echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n";
851 if (!empty($CFG->xml_stylesheets)) {
852 $stylesheets = explode(";", $CFG->xml_stylesheets);
853 foreach ($stylesheets as $stylesheet) {
854 echo "<?xml-stylesheet type=\"text/xsl\" href=\"$CFG->wwwroot/$stylesheet\" ?>\n";
855 }
856 }
857 echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1";
e4576482 858 if (!empty($CFG->xml_doctype_extra)) {
66a51452 859 echo " plus $CFG->xml_doctype_extra";
e4576482 860 }
66a51452 861 echo "//" . strtoupper($currentlanguage) . "\" \"$CFG->xml_dtd\">\n";
862 $direction = " xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"$currentlanguage\" $direction";
9fa49e22 863 }
864
2eea2cce 865 $title = str_replace('"', '&quot;', $title);
866
9fa49e22 867 include ("$CFG->dirroot/theme/$CFG->theme/header.html");
868}
869
870function print_footer ($course=NULL) {
871// Can provide a course object to make the footer contain a link to
872// to the course home page, otherwise the link will go to the site home
873 global $USER, $CFG, $THEME;
874
875
876/// Course links
877 if ($course) {
878 if ($course == "home") { // special case for site home page - please do not remove
76c1650d 879 $homelink = "<p align=\"center\"><a title=\"moodle $CFG->release ($CFG->version)\" href=\"http://moodle.org/\" target=\"_blank\">";
66a51452 880 $homelink .= "<br /><img width=\"130\" height=\"19\" src=\"pix/madewithmoodle.gif\" border=\"0\" /></a></p>";
9fa49e22 881 $course = get_site();
882 $homepage = true;
883 } else {
76c1650d 884 $homelink = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>";
9fa49e22 885 }
886 } else {
c2cb4545 887 $homelink = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/\">".get_string("home")."</a>";
9fa49e22 888 $course = get_site();
889 }
890
891/// User links
a282d0ff 892 $loggedinas = user_login_string($course, $USER);
893
894 include ("$CFG->dirroot/theme/$CFG->theme/footer.html");
895}
896
1ddf9329 897function style_sheet_setup($lastmodified=0, $lifetime=300, $themename="") {
898/// This function is called by stylesheets to set up the header
899/// approriately as well as the current path
6535be85 900
901 global $CFG;
e3b343e0 902
903 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { // May not need to send stylesheet
904 // Following lines from Wolfram Kriesing and John Dell
905 $months = array_flip(array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'));
906 preg_match('~[^,]*,\s(\d+)\s(\w+)\s(\d+)\s(\d+):(\d+):(\d+).*~',
907 $_SERVER['HTTP_IF_MODIFIED_SINCE'], $splitDate);
908 $timestamp = gmmktime($splitDate[4], $splitDate[5], $splitDate[6], $months[$splitDate[2]]+1,
909 $splitDate[1], $splitDate[3]);
910 if ($lastmodified <= $timestamp) {
911 header('HTTP/1.x 304 Not Modified');
912 exit;
913 }
914 }
6535be85 915
916 header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
917 header("Expires: " . gmdate("D, d M Y H:i:s", time() + $lifetime) . " GMT");
918 header("Cache-control: max_age = $lifetime");
919 header("Pragma: ");
920 header("Content-type: text/css"); // Correct MIME type
921
922 if (!empty($themename)) {
923 $CFG->theme = $themename;
924 }
925
926 return "$CFG->wwwroot/theme/$CFG->theme";
927
928}
929
a282d0ff 930
931function user_login_string($course, $user=NULL) {
932 global $USER, $CFG;
933
8d2accb6 934 if (empty($user)) {
a282d0ff 935 $user = $USER;
936 }
937
938 if (isset($user->realuser)) {
939 if ($realuser = get_record("user", "id", $user->realuser)) {
2d71e8ee 940 $fullname = fullname($realuser, true);
941 $realuserinfo = " [<a target=\"{$CFG->framename}\"
942 href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&amp;return=$realuser->id\">$fullname</a>] ";
9fa49e22 943 }
9d378732 944 } else {
945 $realuserinfo = "";
9fa49e22 946 }
947
a282d0ff 948 if (isset($user->id) and $user->id) {
2d71e8ee 949 $fullname = fullname($user, true);
950 $username = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">$fullname</a>";
9fa49e22 951 $loggedinas = $realuserinfo.get_string("loggedinas", "moodle", "$username").
ca16eaeb 952 " (<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</a>)";
9fa49e22 953 } else {
954 $loggedinas = get_string("loggedinnot", "moodle").
ca16eaeb 955 " (<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</a>)";
9fa49e22 956 }
a282d0ff 957 return $loggedinas;
9fa49e22 958}
959
960
9fa49e22 961function print_navigation ($navigation) {
962 global $CFG;
963
964 if ($navigation) {
965 if (! $site = get_site()) {
966 $site->shortname = get_string("home");;
967 }
eb347b6b 968 echo "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/\">$site->shortname</a> -> $navigation";
9fa49e22 969 }
970}
971
d4df9200 972function print_headline($text, $size=2) {
973 echo "<b><font size=\"$size\">$text</font></b><br />\n";
974}
975
76c1650d 976function print_heading($text, $align="center", $size=3) {
977 echo "<p align=\"$align\"><font size=\"$size\"><b>".stripslashes_safe($text)."</b></font></p>";
9fa49e22 978}
979
c9f6251e 980function print_heading_with_help($text, $helppage, $module="moodle", $icon="") {
9fa49e22 981// Centered heading with attached help button (same title text)
c9f6251e 982// and optional icon attached
983 echo "<p align=\"center\"><font size=\"3\">$icon<b>".stripslashes_safe($text);
9fa49e22 984 helpbutton($helppage, $text, $module);
eb347b6b 985 echo "</b></font></p>";
9fa49e22 986}
987
988function print_continue($link) {
9fa49e22 989
990 if (!$link) {
607809b3 991 $link = $_SERVER["HTTP_REFERER"];
9fa49e22 992 }
993
eb347b6b 994 print_heading("<a href=\"$link\">".get_string("continue")."</a>");
9fa49e22 995}
996
997
998function print_simple_box($message, $align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
999 print_simple_box_start($align, $width, $color, $padding, $class);
7d8f674d 1000 echo stripslashes_safe($message);
9fa49e22 1001 print_simple_box_end();
1002}
1003
1004function print_simple_box_start($align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
1005 global $THEME;
1006
1007 if ($align) {
76c1650d 1008 $align = "align=\"$align\"";
9fa49e22 1009 }
1010 if ($width) {
76c1650d 1011 $width = "width=\"$width\"";
9fa49e22 1012 }
9d378732 1013 echo "<table $align $width class=\"$class\" border=\"0\" cellpadding=\"$padding\" cellspacing=\"0\"><tr><td bgcolor=\"$color\" class=\"$class"."content\">";
9fa49e22 1014}
1015
1016function print_simple_box_end() {
1017 echo "</td></tr></table>";
1018}
1019
cc7fa0dc 1020function print_single_button($link, $options, $label="OK", $method="get") {
1021 echo "<form action=\"$link\" method=\"$method\">";
9fa49e22 1022 if ($options) {
1023 foreach ($options as $name => $value) {
66a51452 1024 echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
9fa49e22 1025 }
1026 }
66a51452 1027 echo "<input type=\"submit\" value=\"$label\" /></form>";
9fa49e22 1028}
1029
1030function print_spacer($height=1, $width=1, $br=true) {
1031 global $CFG;
66a51452 1032 echo "<img height=\"$height\" width=\"$width\" src=\"$CFG->wwwroot/pix/spacer.gif\" alt=\"\" />";
9fa49e22 1033 if ($br) {
76c1650d 1034 echo "<br />\n";
9fa49e22 1035 }
1036}
1037
1038function print_file_picture($path, $courseid=0, $height="", $width="", $link="") {
1039// Given the path to a picture file in a course, or a URL,
1040// this function includes the picture in the page.
1041 global $CFG;
1042
1043 if ($height) {
76c1650d 1044 $height = "height=\"$height\"";
9fa49e22 1045 }
1046 if ($width) {
76c1650d 1047 $width = "width=\"$width\"";
9fa49e22 1048 }
1049 if ($link) {
76c1650d 1050 echo "<a href=\"$link\">";
9fa49e22 1051 }
1052 if (substr(strtolower($path), 0, 7) == "http://") {
66a51452 1053 echo "<img border=\"0\" $height $width src=\"$path\" />";
9fa49e22 1054
1055 } else if ($courseid) {
66a51452 1056 echo "<img border=\"0\" $height $width src=\"";
9fa49e22 1057 if ($CFG->slasharguments) { // Use this method if possible for better caching
1058 echo "$CFG->wwwroot/file.php/$courseid/$path";
1059 } else {
3f396065 1060 echo "$CFG->wwwroot/file.php?file=/$courseid/$path";
9fa49e22 1061 }
66a51452 1062 echo "\" />";
9fa49e22 1063 } else {
1064 echo "Error: must pass URL or course";
1065 }
1066 if ($link) {
76c1650d 1067 echo "</a>";
9fa49e22 1068 }
1069}
1070
1071function print_user_picture($userid, $courseid, $picture, $large=false, $returnstring=false, $link=true) {
f374fb10 1072 global $CFG;
9fa49e22 1073
1074 if ($link) {
66a51452 1075 $output = "<a href=\"$CFG->wwwroot/user/view.php?id=$userid&amp;course=$courseid\">";
9fa49e22 1076 } else {
1077 $output = "";
1078 }
1079 if ($large) {
67a63a30 1080 $file = "f1";
9fa49e22 1081 $size = 100;
1082 } else {
67a63a30 1083 $file = "f2";
9fa49e22 1084 $size = 35;
1085 }
67a63a30 1086 if ($picture) { // Print custom user picture
9fa49e22 1087 if ($CFG->slasharguments) { // Use this method if possible for better caching
67a63a30 1088 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/user/pix.php/$userid/$file.jpg\"".
66a51452 1089 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" />";
9fa49e22 1090 } else {
67a63a30 1091 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/user/pix.php?file=/$userid/$file.jpg\"".
66a51452 1092 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" />";
9fa49e22 1093 }
67a63a30 1094 } else { // Print default user pictures (use theme version if available)
c9f6251e 1095 $output .= "<img align=\"absmiddle\" src=\"$CFG->pixpath/u/$file.png\"".
66a51452 1096 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" />";
9fa49e22 1097 }
1098 if ($link) {
76c1650d 1099 $output .= "</a>";
9fa49e22 1100 }
1101
1102 if ($returnstring) {
1103 return $output;
1104 } else {
1105 echo $output;
1106 }
1107}
1108
f2c80965 1109function recent_internet_explorer() {
1110/// Returns true if the current browser is a recent version
1111/// of Internet Explorer
1112
1113 $msie = '/msie\s([5-9])\.?[0-9]*.*(win)/i';
1114 $opera='/opera\s+[0-9]+/i';
1115
1116 return isset($_SERVER['HTTP_USER_AGENT']) and
1117 preg_match($msie,$_SERVER['HTTP_USER_AGENT']) and
1118 !preg_match($opera,$_SERVER['HTTP_USER_AGENT']);
1119}
1120
1121function print_group_picture($group, $courseid, $large=false, $returnstring=false, $link=true) {
f374fb10 1122 global $CFG;
f2c80965 1123 static $recentIE;
1124
1125 if (!isset($recentIE)) {
1126 $recentIE = recent_internet_explorer();
1127 }
f374fb10 1128
1129 if ($link) {
f2c80965 1130 $output = "<a href=\"$CFG->wwwroot/course/group.php?id=$courseid&group=$group->id\">";
f374fb10 1131 } else {
1132 $output = "";
1133 }
1134 if ($large) {
1135 $file = "f1";
1136 $size = 100;
1137 } else {
1138 $file = "f2";
1139 $size = 35;
1140 }
f2c80965 1141 if ($group->picture) { // Print custom group picture
f374fb10 1142 if ($CFG->slasharguments) { // Use this method if possible for better caching
f2c80965 1143 $pngsrc = "$CFG->wwwroot/user/pixgroup.php/$group->id/$file.png";
f374fb10 1144 } else {
f2c80965 1145 $pngsrc = "$CFG->wwwroot/user/pixgroup.php?file=/$group->id/$file.png";
f374fb10 1146 }
02759cb5 1147 $title = s($group->description);
f2c80965 1148 if ($recentIE) { // work around the HORRIBLE bug IE has with alpha transparencies
1149 $output .= "<img align=\"absmiddle\" src=\"$CFG->pixpath/spacer.gif\" width=\"$size\" height=\"$size\"".
1150 " border=\"0\" style=\"width: {$size}px; height: {$size}px; ".
1151 " filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='$pngsrc', sizingMethod='scale') ".
02759cb5 1152 " alt=\"\" title=\"$title\" />";
f2c80965 1153 } else {
1154 $output .= "<img align=\"absmiddle\" src=\"$pngsrc\" border=\"0\" width=\"$size\" height=\"$size\" ".
02759cb5 1155 " alt=\"\" title=\"$title\" />";
f2c80965 1156 }
1157 } else { // Print nothing
1158 $output .= "";
f374fb10 1159 }
1160 if ($link) {
1161 $output .= "</a>";
1162 }
1163
1164 if ($returnstring) {
1165 return $output;
1166 } else {
1167 echo $output;
1168 }
1169}
1170
9fa49e22 1171function print_table($table) {
1172// Prints a nicely formatted table.
1173// $table is an object with several properties.
1174// $table->head is an array of heading names.
1175// $table->align is an array of column alignments
1176// $table->size is an array of column sizes
5867bfb5 1177// $table->wrap is an array of "nowrap"s or nothing
9fa49e22 1178// $table->data[] is an array of arrays containing the data.
1179// $table->width is an percentage of the page
1180// $table->cellpadding padding on each cell
1181// $table->cellspacing spacing between cells
1182
1183 if (isset($table->align)) {
1184 foreach ($table->align as $key => $aa) {
1185 if ($aa) {
76c1650d 1186 $align[$key] = " align=\"$aa\"";
9fa49e22 1187 } else {
1188 $align[$key] = "";
1189 }
1190 }
1191 }
1192 if (isset($table->size)) {
1193 foreach ($table->size as $key => $ss) {
1194 if ($ss) {
76c1650d 1195 $size[$key] = " width=\"$ss\"";
9fa49e22 1196 } else {
1197 $size[$key] = "";
1198 }
1199 }
1200 }
5867bfb5 1201 if (isset($table->wrap)) {
1202 foreach ($table->wrap as $key => $ww) {
1203 if ($ww) {
66a51452 1204 $wrap[$key] = " nowrap=\"nowrap\" ";
5867bfb5 1205 } else {
1206 $wrap[$key] = "";
1207 }
1208 }
1209 }
9fa49e22 1210
9d378732 1211 if (empty($table->width)) {
9fa49e22 1212 $table->width = "80%";
1213 }
1214
9d378732 1215 if (empty($table->cellpadding)) {
9fa49e22 1216 $table->cellpadding = "5";
1217 }
1218
9d378732 1219 if (empty($table->cellspacing)) {
9fa49e22 1220 $table->cellspacing = "1";
1221 }
1222
5867bfb5 1223 print_simple_box_start("center", "$table->width", "#ffffff", 0);
66a51452 1224 echo "<table width=\"100%\" border=\"0\" valign=\"top\" align=\"center\" ";
9fa49e22 1225 echo " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"generaltable\">\n";
1226
b79f41cd 1227 if (!empty($table->head)) {
5867bfb5 1228 echo "<tr>";
9fa49e22 1229 foreach ($table->head as $key => $heading) {
9d378732 1230 if (!isset($size[$key])) {
1231 $size[$key] = "";
1232 }
1233 if (!isset($align[$key])) {
1234 $align[$key] = "";
1235 }
66a51452 1236 echo "<th valign=\"top\" ".$align[$key].$size[$key]." nowrap=\"nowrap\" class=\"generaltableheader\">$heading</th>";
9fa49e22 1237 }
66a51452 1238 echo "</tr>\n";
9fa49e22 1239 }
1240
a1f8ff87 1241 if (!empty($table->data)) {
1242 foreach ($table->data as $row) {
1243 echo "<tr valign=\"top\">";
1244 foreach ($row as $key => $item) {
1245 if (!isset($size[$key])) {
1246 $size[$key] = "";
1247 }
1248 if (!isset($align[$key])) {
1249 $align[$key] = "";
1250 }
1251 if (!isset($wrap[$key])) {
1252 $wrap[$key] = "";
1253 }
1254 echo "<td ".$align[$key].$size[$key].$wrap[$key]." class=\"generaltablecell\">$item</td>";
1255 }
1256 echo "</tr>\n";
9fa49e22 1257 }
9fa49e22 1258 }
5867bfb5 1259 echo "</table>\n";
9fa49e22 1260 print_simple_box_end();
1261
1262 return true;
1263}
1264
2f4d324b 1265function make_table($table) {
1266// Creates a nicely formatted table and returns it
1267// $table is an object with several properties.
1268// $table->head is an array of heading names.
1269// $table->align is an array of column alignments
1270// $table->size is an array of column sizes
1271// $table->wrap is an array of "nowrap"s or nothing
1272// $table->data[] is an array of arrays containing the data.
1273// $table->width is an percentage of the page
1274// $table->class is a class
1275// $table->fontsize is the size of all the text
1276// $table->tablealign align the whole table
1277// $table->cellpadding padding on each cell
1278// $table->cellspacing spacing between cells
1279
1280 if (isset($table->align)) {
1281 foreach ($table->align as $key => $aa) {
1282 if ($aa) {
1283 $align[$key] = " align=\"$aa\"";
1284 } else {
1285 $align[$key] = "";
1286 }
1287 }
1288 }
1289 if (isset($table->size)) {
1290 foreach ($table->size as $key => $ss) {
1291 if ($ss) {
1292 $size[$key] = " width=\"$ss\"";
1293 } else {
1294 $size[$key] = "";
1295 }
1296 }
1297 }
1298 if (isset($table->wrap)) {
1299 foreach ($table->wrap as $key => $ww) {
1300 if ($ww) {
66a51452 1301 $wrap[$key] = " nowrap=\"nowrap\" ";
2f4d324b 1302 } else {
1303 $wrap[$key] = "";
1304 }
1305 }
1306 }
1307
1308 if (empty($table->width)) {
1309 $table->width = "80%";
1310 }
1311
1312 if (empty($table->tablealign)) {
1313 $table->tablealign = "center";
1314 }
1315
1316 if (empty($table->cellpadding)) {
1317 $table->cellpadding = "5";
1318 }
1319
1320 if (empty($table->cellspacing)) {
1321 $table->cellspacing = "1";
1322 }
1323
1324 if (empty($table->class)) {
1325 $table->class = "generaltable";
1326 }
1327
1328 if (empty($table->fontsize)) {
1329 $fontsize = "";
1330 } else {
1331 $fontsize = "<font size=\"$table->fontsize\">";
1332 }
1333
66a51452 1334 $output = "<table width=\"$table->width\" valign=\"top\" align=\"$table->tablealign\" ";
2f4d324b 1335 $output .= " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"$table->class\">\n";
1336
1337 if (!empty($table->head)) {
1338 $output .= "<tr>";
1339 foreach ($table->head as $key => $heading) {
1340 if (!isset($size[$key])) {
1341 $size[$key] = "";
1342 }
1343 if (!isset($align[$key])) {
1344 $align[$key] = "";
1345 }
66a51452 1346 $output .= "<th valign=\"top\" ".$align[$key].$size[$key]." nowrap=\"nowrap\" class=\"{$table->class}header\">$fontsize$heading</th>";
2f4d324b 1347 }
1348 $output .= "</tr>\n";
1349 }
1350
1351 foreach ($table->data as $row) {
66a51452 1352 $output .= "<tr valign=\"top\">";
2f4d324b 1353 foreach ($row as $key => $item) {
1354 if (!isset($size[$key])) {
1355 $size[$key] = "";
1356 }
1357 if (!isset($align[$key])) {
1358 $align[$key] = "";
1359 }
1360 if (!isset($wrap[$key])) {
1361 $wrap[$key] = "";
1362 }
1363 $output .= "<td ".$align[$key].$size[$key].$wrap[$key]." class=\"{$table->class}cell\">$fontsize$item</td>";
1364 }
1365 $output .= "</tr>\n";
1366 }
1367 $output .= "</table>\n";
1368
1369 return $output;
1370}
1371
47037513 1372function print_textarea($usehtmleditor, $rows, $cols, $width, $height, $name, $value="", $courseid=0) {
1373/// Prints a basic textarea field
1374/// $width and height are legacy fields and no longer used
4c46c425 1375
47037513 1376 global $CFG, $course;
50bdc74d 1377
408e62f8 1378 if (empty($courseid)) {
50bdc74d 1379 if (!empty($course->id)) { // search for it in global context
1380 $courseid = $course->id;
1381 }
1382 }
9fa49e22 1383
47037513 1384 if ($usehtmleditor) {
1385 if (!empty($courseid) and isteacher($courseid)) {
1386 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/htmlarea.php?id=$courseid\"></script>\n";
1387 } else {
1388 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/htmlarea.php\"></script>\n";
1389 }
1390 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/dialog.js\"></script>\n";
1391 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/lang/en.php\"></script>\n";
1392 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/popupwin.js\"></script>\n";
50bdc74d 1393
47037513 1394 if ($rows < 20) {
1395 $rows = 20;
1396 }
1397 if ($cols < 65) {
1398 $cols = 65;
4c46c425 1399 }
9fa49e22 1400 }
47037513 1401
1402 echo "<textarea id=\"$name\" name=\"$name\" rows=\"$rows\" cols=\"$cols\" wrap=\"virtual\">";
1403 p($value);
1404 echo "</textarea>\n";
9fa49e22 1405}
1406
1407function print_richedit_javascript($form, $name, $source="no") {
47037513 1408/// Legacy function, provided for backward compatability
1409 use_html_editor($name);
1410}
1411
1412function use_html_editor($name="") {
1413/// Sets up the HTML editor on textareas in the current page.
1414/// If a field name is provided, then it will only be
1415/// applied to that field - otherwise it will be used
1416/// on every textarea in the page.
1417///
1418/// In most cases no arguments need to be supplied
4c46c425 1419
47037513 1420 echo "<script language=\"javascript\" type=\"text/javascript\" defer=\"1\">\n";
1421 if (empty($name)) {
1422 echo "HTMLArea.replaceAll();";
1423 } else {
1424 echo "HTMLArea.replace('$name')";
4c46c425 1425 }
47037513 1426 echo "</script>";
9fa49e22 1427}
1428
1429
1430function update_course_icon($courseid) {
1431// Used to be an icon, but it's now a simple form button
1432 global $CFG, $USER;
1433
b6c12732 1434 if (isteacheredit($courseid)) {
9c9f7d77 1435 if (!empty($USER->editing)) {
9fa49e22 1436 $string = get_string("turneditingoff");
1437 $edit = "off";
1438 } else {
1439 $string = get_string("turneditingon");
1440 $edit = "on";
1441 }
60b9a565 1442 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/view.php\">".
66a51452 1443 "<input type=\"hidden\" name=\"id\" value=\"$courseid\" />".
1444 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1445 "<input type=\"submit\" value=\"$string\" /></form>";
9fa49e22 1446 }
1447}
1448
1449function update_module_button($moduleid, $courseid, $string) {
1450// Prints the editing button on a module "view" page
1451 global $CFG;
1452
b6c12732 1453 if (isteacheredit($courseid)) {
9fa49e22 1454 $string = get_string("updatethis", "", $string);
60b9a565 1455 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/mod.php\">".
66a51452 1456 "<input type=\"hidden\" name=\"update\" value=\"$moduleid\" />".
1457 "<input type=\"hidden\" name=\"return\" value=\"true\" />".
1458 "<input type=\"submit\" value=\"$string\" /></form>";
b6c12732 1459 } else {
1460 return "";
9fa49e22 1461 }
1462}
1463
c2cb4545 1464function update_category_button($categoryid) {
d2b6ba70 1465// Prints the editing button on a category page
1466 global $CFG, $USER;
c2cb4545 1467
d2b6ba70 1468 if (iscreator()) {
f374fb10 1469 if (!empty($USER->categoryediting)) {
d2b6ba70 1470 $string = get_string("turneditingoff");
1471 $edit = "off";
1472 } else {
1473 $string = get_string("turneditingon");
1474 $edit = "on";
9b16d1ea 1475 }
60b9a565 1476 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/category.php\">".
66a51452 1477 "<input type=\"hidden\" name=\"id\" value=\"$categoryid\" />".
1478 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1479 "<input type=\"submit\" value=\"$string\" /></form>";
d2b6ba70 1480 }
1481}
1482
1483function update_categories_button() {
1484// Prints the editing button on categories listing
1485 global $CFG, $USER;
1486
1487 if (isadmin()) {
f374fb10 1488 if (!empty($USER->categoriesediting)) {
d2b6ba70 1489 $string = get_string("turneditingoff");
1490 $edit = "off";
1491 } else {
1492 $string = get_string("turneditingon");
1493 $edit = "on";
1494 }
60b9a565 1495 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/index.php\">".
66a51452 1496 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1497 "<input type=\"submit\" value=\"$string\" /></form>";
c2cb4545 1498 }
1499}
9fa49e22 1500
f374fb10 1501function update_group_button($courseid) {
1502// Prints the editing button on group page
1503 global $CFG, $USER;
1504
1505 if (isteacheredit($courseid)) {
1506 if (!empty($USER->groupediting)) {
1507 $string = get_string("turneditingoff");
1508 $edit = "off";
1509 } else {
1510 $string = get_string("turneditingon");
1511 $edit = "on";
1512 }
1513 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/group.php\">".
1514 "<input type=\"hidden\" name=\"id\" value=\"$courseid\" />".
1515 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1516 "<input type=\"submit\" value=\"$string\" /></form>";
1517 }
1518}
1519
1520function update_groups_button($courseid) {
1521// Prints the editing button on groups page
1522 global $CFG, $USER;
1523
1524 if (isteacheredit($courseid)) {
1525 if (!empty($USER->groupsediting)) {
1526 $string = get_string("turneditingoff");
1527 $edit = "off";
1528 } else {
1529 $string = get_string("turneditingon");
1530 $edit = "on";
1531 }
1532 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/groups.php\">".
1533 "<input type=\"hidden\" name=\"id\" value=\"$courseid\" />".
1534 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1535 "<input type=\"submit\" value=\"$string\" /></form>";
1536 }
1537}
1538
f2d91421 1539
16ef5e78 1540function navmenu($course, $cm=NULL, $targetwindow="self") {
9fa49e22 1541// Given a course and a (current) coursemodule
f2d91421 1542// This function returns a small popup menu with all the
9fa49e22 1543// course activity modules in it, as a navigation menu
f2d91421 1544// The data is taken from the serialised array stored in
9fa49e22 1545// the course record
1546
1547 global $CFG;
1548
1549 if ($cm) {
f2d91421 1550 $cm = $cm->id;
9fa49e22 1551 }
1552
1553 if ($course->format == 'weeks') {
1554 $strsection = get_string("week");
1555 } else {
1556 $strsection = get_string("topic");
1557 }
1558
1559 if (!$modinfo = unserialize($course->modinfo)) {
1560 return "";
1561 }
f2d91421 1562 $isteacher = isteacher($course->id);
9fa49e22 1563 $section = -1;
1564 $selected = "";
f2d91421 1565 $url = "";
1566 $previousmod = NULL;
1567 $backmod = NULL;
1568 $nextmod = NULL;
1569 $flag = false;
1570
9fa49e22 1571 foreach ($modinfo as $mod) {
ab2df10c 1572 if ($mod->mod == "label") {
1573 continue;
1574 }
9fa49e22 1575 if ($mod->section > 0 and $section <> $mod->section) {
1576 $menu[] = "-------------- $strsection $mod->section --------------";
1577 }
1578 $section = $mod->section;
cf055081 1579 //Only add visible or teacher mods to jumpmenu
f2d91421 1580 if ($mod->visible or $isteacher) {
cf055081 1581 $url = "$mod->mod/view.php?id=$mod->cm";
f2d91421 1582 if ($flag) { // the current mod is the "next" mod
1583 $nextmod = $mod;
1584 $flag = false;
1585 }
cf055081 1586 if ($cm == $mod->cm) {
1587 $selected = $url;
f2d91421 1588 $backmod = $previousmod;
1589 $flag = true; // set flag so we know to use next mod for "next"
cf055081 1590 }
1591 $mod->name = urldecode($mod->name);
1592 if (strlen($mod->name) > 55) {
1593 $mod->name = substr($mod->name, 0, 50)."...";
1594 }
2a409368 1595 if (!$mod->visible) {
1596 $mod->name = "(".$mod->name.")";
1597 }
f2d91421 1598 $menu[$url] = $mod->name;
db0d0337 1599 $previousmod = $mod;
9fa49e22 1600 }
f2d91421 1601 }
1602 if ($backmod) {
1603 $backmod = "<form action=\"$CFG->wwwroot/mod/$backmod->mod/view.php\" target=\"$CFG->framename\">".
1604 "<input type=\"hidden\" name=\"id\" value=\"$backmod->cm\">".
1605 "<input type=\"submit\" value=\"&lt;\"></form>";
1606 }
1607 if ($nextmod) {
1608 $nextmod = "<form action=\"$CFG->wwwroot/mod/$nextmod->mod/view.php\" target=\"$CFG->framename\">".
1609 "<input type=\"hidden\" name=\"id\" value=\"$nextmod->cm\">".
1610 "<input type=\"submit\" value=\"&gt;\"></form>";
1611 }
1612 return "<table><tr><td>$backmod</td><td>" .
1613 popup_form("$CFG->wwwroot/mod/", $menu, "navmenu", $selected, get_string("jumpto"),
1614 "", "", true, $targetwindow).
1615 "</td><td>$nextmod</td></tr></table>";
1616}
9fa49e22 1617
1618
1619function print_date_selector($day, $month, $year, $currenttime=0) {
1620// Currenttime is a default timestamp in GMT
1621// Prints form items with the names $day, $month and $year
1622
1623 if (!$currenttime) {
1624 $currenttime = time();
1625 }
1626 $currentdate = usergetdate($currenttime);
1627
1628 for ($i=1; $i<=31; $i++) {
1629 $days[$i] = "$i";
1630 }
1631 for ($i=1; $i<=12; $i++) {
39e018b3 1632 $months[$i] = userdate(gmmktime(12,0,0,$i,1,2000), "%B");
9fa49e22 1633 }
1634 for ($i=2000; $i<=2010; $i++) {
1635 $years[$i] = $i;
1636 }
47f1da80 1637 choose_from_menu($days, $day, $currentdate['mday'], "");
1638 choose_from_menu($months, $month, $currentdate['mon'], "");
1639 choose_from_menu($years, $year, $currentdate['year'], "");
9fa49e22 1640}
1641
6942583e 1642function print_time_selector($hour, $minute, $currenttime=0, $step=5) {
9fa49e22 1643// Currenttime is a default timestamp in GMT
1644// Prints form items with the names $hour and $minute
1645
1646 if (!$currenttime) {
1647 $currenttime = time();
1648 }
1649 $currentdate = usergetdate($currenttime);
6942583e 1650 if ($step != 1) {
1651 $currentdate['minutes'] = ceil($currentdate['minutes']/$step)*$step;
1652 }
9fa49e22 1653 for ($i=0; $i<=23; $i++) {
1654 $hours[$i] = sprintf("%02d",$i);
1655 }
6942583e 1656 for ($i=0; $i<=59; $i+=$step) {
9fa49e22 1657 $minutes[$i] = sprintf("%02d",$i);
1658 }
47f1da80 1659 choose_from_menu($hours, $hour, $currentdate['hours'], "");
1660 choose_from_menu($minutes, $minute, $currentdate['minutes'], "");
9fa49e22 1661}
1662
d6bdd9d5 1663function print_grade_menu($courseid, $name, $current, $includenograde=true) {
62ca135d 1664/// Prints a grade menu (as part of an existing form) with help
1665/// Showing all possible numerical grades and scales
1666
c9f6251e 1667 global $CFG;
62ca135d 1668
1669 $strscale = get_string("scale");
1670 $strscales = get_string("scales");
1671
1f7deef6 1672 $scales = get_scales_menu($courseid);
62ca135d 1673 foreach ($scales as $i => $scalename) {
1674 $grades[-$i] = "$strscale: $scalename";
1675 }
d6bdd9d5 1676 if ($includenograde) {
1677 $grades[0] = get_string("nograde");
1678 }
62ca135d 1679 for ($i=100; $i>=1; $i--) {
1680 $grades[$i] = $i;
1681 }
1682 choose_from_menu($grades, "$name", "$current", "");
1683
c9f6251e 1684 $helpicon = "$CFG->pixpath/help.gif";
66a51452 1685 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" alt=\"$strscales\" src=\"$helpicon\" />";
1686 link_to_popup_window ("/course/scales.php?id=$courseid&amp;list=true", "ratingscales",
62ca135d 1687 $linkobject, 400, 500, $strscales);
1688}
1689
02ebf404 1690function print_scale_menu($courseid, $name, $current) {
1691/// Prints a scale menu (as part of an existing form) including help button
62ca135d 1692/// Just like print_grade_menu but without the numerical grades
02ebf404 1693
c9f6251e 1694 global $CFG;
02ebf404 1695
1696 $strscales = get_string("scales");
1697 choose_from_menu(get_scales_menu($courseid), "$name", $current, "");
c9f6251e 1698 $helpicon = "$CFG->pixpath/help.gif";
66a51452 1699 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" alt=\"$strscales\" src=\"$helpicon\" />";
1700 link_to_popup_window ("/course/scales.php?id=$courseid&amp;list=true", "ratingscales",
02ebf404 1701 $linkobject, 400, 500, $strscales);
1702}
1703
fdc47ee6 1704
02ebf404 1705function print_scale_menu_helpbutton($courseid, $scale) {
1706/// Prints a help button about a scale
1707/// scale is an object
1708
c9f6251e 1709 global $CFG;
02ebf404 1710
1711 $strscales = get_string("scales");
c9f6251e 1712 $helpicon = "$CFG->pixpath/help.gif";
66a51452 1713 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" alt=\"$scale->name\" src=\"$helpicon\" />";
1714 link_to_popup_window ("/course/scales.php?id=$courseid&amp;list=true&amp;scale=$scale->id", "ratingscale",
02ebf404 1715 $linkobject, 400, 500, $scale->name);
1716}
1717
1718
9fa49e22 1719function error ($message, $link="") {
1720 global $CFG, $SESSION;
1721
1722 print_header(get_string("error"));
66a51452 1723 echo "<br />";
9fa49e22 1724 print_simple_box($message, "center", "", "#FFBBBB");
1725
1726 if (!$link) {
1727 if ( !empty($SESSION->fromurl) ) {
1728 $link = "$SESSION->fromurl";
1729 unset($SESSION->fromurl);
9fa49e22 1730 } else {
c2cb4545 1731 $link = "$CFG->wwwroot/";
9fa49e22 1732 }
1733 }
1734 print_continue($link);
1735 print_footer();
1736 die;
1737}
1738
1739function helpbutton ($page, $title="", $module="moodle", $image=true, $linktext=false, $text="") {
1740 // $page = the keyword that defines a help page
1741 // $title = the title of links, rollover tips, alt tags etc
1742 // $module = which module is the page defined in
1743 // $image = use a help image for the link? (true/false/"both")
1744 // $text = if defined then this text is used in the page, and
1745 // the $page variable is ignored.
dc0dc7d5 1746 global $CFG, $THEME;
9fa49e22 1747
1748 if ($module == "") {
1749 $module = "moodle";
1750 }
1751
1752 if ($image) {
c9f6251e 1753 $icon = "$CFG->pixpath/help.gif";
9fa49e22 1754 if ($linktext) {
7fc0f9e5 1755 $linkobject = "<span style=\"cursor:help;\">$title<img align=\"absmiddle\" border=\"0\" ".
1756 " height=\"17\" width=\"22\" alt=\"\" src=\"$icon\" /></span>";
9fa49e22 1757 } else {
7fc0f9e5 1758 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" ".
1759 " alt=\"$title\" style=\"cursor:help;\" src=\"$icon\" />";
9fa49e22 1760 }
1761 } else {
7fc0f9e5 1762 $linkobject = "<span style=\"cursor:help;\">$title</span>";
9fa49e22 1763 }
1764 if ($text) {
66a51452 1765 $url = "/help.php?module=$module&amp;text=".htmlentities(urlencode($text));
9fa49e22 1766 } else {
66a51452 1767 $url = "/help.php?module=$module&amp;file=$page.html";
9fa49e22 1768 }
1769 link_to_popup_window ($url, "popup", $linkobject, 400, 500, $title);
1770}
1771
e825f279 1772function emoticonhelpbutton($form, $field) {
1773/// Prints a special help button that is a link to the "live" emoticon popup
1774 global $CFG, $SESSION;
1775
1776 $SESSION->inserttextform = $form;
1777 $SESSION->inserttextfield = $field;
1778 helpbutton("emoticons", get_string("helpemoticons"), "moodle", false, true);
c9f6251e 1779 echo "&nbsp;";
66a51452 1780 link_to_popup_window ("/help.php?module=moodle&amp;file=emoticons.html", "popup",
1781 "<img src=\"$CFG->pixpath/s/smiley.gif\" border=\"0\" align=\"absmiddle\" width=\"15\" height=\"15\" />",
c9f6251e 1782 400, 500, get_string("helpemoticons"));
1783 echo "<br />";
e825f279 1784}
1785
9fa49e22 1786function notice ($message, $link="") {
750ab759 1787 global $CFG, $THEME;
9fa49e22 1788
1789 if (!$link) {
750ab759 1790 if (!empty($_SERVER["HTTP_REFERER"])) {
1791 $link = $_SERVER["HTTP_REFERER"];
1792 } else {
c2cb4545 1793 $link = "$CFG->wwwroot/";
750ab759 1794 }
9fa49e22 1795 }
1796
01d79966 1797 echo "<br />";
11a876e1 1798 print_simple_box($message, "center", "50%", "$THEME->cellheading", "20", "noticebox");
eb347b6b 1799 print_heading("<a href=\"$link\">".get_string("continue")."</a>");
9fa49e22 1800 print_footer(get_site());
1801 die;
1802}
1803
1804function notice_yesno ($message, $linkyes, $linkno) {
1805 global $THEME;
1806
eb347b6b 1807 print_simple_box_start("center", "60%", "$THEME->cellheading");
66a51452 1808 echo "<p align=\"center\"><font size=\"3\">$message</font></p>";
1809 echo "<p align=\"center\"><font size=\"3\"><b>";
eb347b6b 1810 echo "<a href=\"$linkyes\">".get_string("yes")."</a>";
9fa49e22 1811 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
eb347b6b 1812 echo "<a href=\"$linkno\">".get_string("no")."</a>";
1813 echo "</b></font></p>";
9fa49e22 1814 print_simple_box_end();
1815}
1816
559573a2 1817function redirect($url, $message="", $delay="0") {
5d6c043a 1818// Redirects the user to another page, after printing a notice
9fa49e22 1819
c9082a8c 1820 if (empty($message)) {
2b23eea3 1821 echo "<meta http-equiv=\"refresh\" content=\"$delay; url=$url\" />";
8a55c984 1822 echo "<script>location.replace('$url');</script>"; // To cope with Mozilla bug
c9082a8c 1823 } else {
05c19593 1824 if (empty($delay)) {
c9082a8c 1825 $delay = 3; // There's no point having a message with no delay
1826 }
5f546a46 1827 print_header("", "", "", "", "<meta http-equiv=\"refresh\" content=\"$delay; url=$url\" />");
76c1650d 1828 echo "<center>";
1829 echo "<p>$message</p>";
1830 echo "<p>( <a href=\"$url\">".get_string("continue")."</a> )</p>";
1831 echo "</center>";
ff019455 1832 flush();
1833 sleep($delay);
1834 echo "<script>location.replace('$url');</script>"; // To cope with Mozilla bug
9fa49e22 1835 }
1836 die;
1837}
1838
99988d1a 1839function notify ($message, $color="red", $align="center") {
1840 echo "<p align=\"$align\"><b><font color=\"$color\">$message</font></b></p>\n";
9fa49e22 1841}
1842
43373804 1843function obfuscate_email($email) {
1844/// Given an email address, this function will return an obfuscated version of it
1845 $i = 0;
1846 $length = strlen($email);
1847 $obfuscated = "";
1848 while ($i < $length) {
1849 if (rand(0,2)) {
1850 $obfuscated.='%'.dechex(ord($email{$i}));
1851 } else {
1852 $obfuscated.=$email{$i};
1853 }
1854 $i++;
1855 }
1856 return $obfuscated;
1857}
1858
1859function obfuscate_text($plaintext) {
1860/// This function takes some text and replaces about half of the characters
1861/// with HTML entity equivalents. Return string is obviously longer.
1862 $i=0;
1863 $length = strlen($plaintext);
1864 $obfuscated="";
2b09e377 1865 $prev_obfuscated = false;
43373804 1866 while ($i < $length) {
2b09e377 1867 $c = ord($plaintext{$i});
1868 $numerical = ($c >= ord('0')) && ($c <= ord('9'));
1869 if ($prev_obfuscated and $numerical ) {
1870 $obfuscated.='&#'.ord($plaintext{$i});
1871 } else if (rand(0,2)) {
43373804 1872 $obfuscated.='&#'.ord($plaintext{$i});
2b09e377 1873 $prev_obfuscated = true;
43373804 1874 } else {
1875 $obfuscated.=$plaintext{$i};
2b09e377 1876 $prev_obfuscated = false;
43373804 1877 }
2b09e377 1878 $i++;
43373804 1879 }
1880 return $obfuscated;
1881}
1882
1883function obfuscate_mailto($email, $label="") {
1884/// This function uses the above two functions to generate a fully
1885/// obfuscated email link, ready to use.
1886
1887 if (empty($label)) {
1888 $label = $email;
1889 }
1890 return sprintf('<a href="%s:%s" title="%s">%s</a>', obfuscate_text('mailto'),
1891 obfuscate_email($email),
1892 obfuscate_text($email),
1893 obfuscate_text($label));
1894}
1895
8b9c7aa0 1896function print_paging_bar($totalcount, $page, $perpage, $baseurl) {
1897/// Prints a single paging bar to provide access to other pages (usually in a search)
1898
519d369f 1899 $maxdisplay = 18;
8ef9cb56 1900
8b9c7aa0 1901 if ($totalcount > $perpage) {
f04dc61d 1902 echo "<center>";
1903 echo "<p>".get_string("page").":";
f374fb10 1904 if ($page > 0) {
1905 $pagenum=$page-1;
1906 echo "&nbsp;(<a href=\"{$baseurl}page=$pagenum\">".get_string("previous")."</a>)&nbsp;";
1907 }
be20753e 1908 $lastpage = ceil($totalcount / $perpage);
1909 if ($page > 15) {
1910 $startpage = $page - 10;
519d369f 1911 echo "&nbsp<a href=\"{$baseurl}page=0\">1</a>&nbsp;...";
be20753e 1912 } else {
1913 $startpage = 0;
1914 }
be20753e 1915 $currpage = $startpage;
1916 $displaycount = 0;
1917 while ($displaycount < $maxdisplay and $currpage < $lastpage) {
1918 $displaypage = $currpage+1;
1919 if ($page == $currpage) {
8b9c7aa0 1920 echo "&nbsp;&nbsp;$displaypage";
1921 } else {
be20753e 1922 echo "&nbsp;&nbsp;<a href=\"{$baseurl}page=$currpage\">$displaypage</a>";
e27dbcc8 1923 }
be20753e 1924 $displaycount++;
1925 $currpage++;
8b9c7aa0 1926 }
924cef21 1927 if ($currpage < $lastpage) {
519d369f 1928 $lastpageactual = $lastpage - 1;
1929 echo "&nbsp;...<a href=\"{$baseurl}page=$lastpageactual\">$lastpage</a>&nbsp;";
924cef21 1930 }
8b9c7aa0 1931 $pagenum = $page + 1;
be20753e 1932 if ($pagenum != $displaypage) {
8b9c7aa0 1933 echo "&nbsp;&nbsp;(<a href=\"{$baseurl}page=$pagenum\">".get_string("next")."</a>)";
1934 }
1935 echo "</p>";
be20753e 1936 echo "</center>";
8b9c7aa0 1937 }
1938}
9fa49e22 1939
9d5b689c 1940// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
f9903ed0 1941?>