SERIAL8 -> SERIAL
[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 =
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>";
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
539 default: // FORMAT_MOODLE or anything else
5b472756 540 $text = eregi_replace('(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)','\\3 [\\2]', $text);
7c55a29b 541 return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES)));
d342c763 542 break;
543 }
544}
0095d5cd 545
e67b9e31 546
547function filter_text($text) {
548/// Given some text in HTML format, this function will pass it
549/// through any filters that have been defined in $CFG->textfilterx
550/// The variable defines a filepath to a file containing the
551/// filter function. The file must contain a variable called
552/// $textfilter_function which contains the name of the function
553/// with $course->id and $text parameters
554
555 global $CFG, $course; // A dirty hack right now ... should not be assumed global
556
557 if (empty($course->id)) {
558 return $text;
559 }
560
561 for ($i=1; $i<=10; $i++) {
562 $variable = "textfilter$i";
563 if (empty($CFG->$variable)) { /// No more filters
564 return $text;
565 }
566 if (is_readable("$CFG->dirroot/".$CFG->$variable)) {
567 include("$CFG->dirroot/".$CFG->$variable);
568 $text = $textfilter_function($course->id, $text);
569 }
570 }
571 return $text;
572}
573
574
0095d5cd 575function clean_text($text, $format) {
c1d57101 576/// Given raw text (eg typed in by a user), this function cleans it up
577/// and removes any nasty tags that could mess up Moodle pages.
b7a3cf49 578
fc120758 579 global $ALLOWED_TAGS;
3fe3851d 580
d342c763 581 switch ($format) {
0095d5cd 582 case FORMAT_MOODLE:
0095d5cd 583 case FORMAT_HTML:
d342c763 584 case FORMAT_WIKI:
5b472756 585 /// Remove javascript: label
3fe3851d 586 $text = strip_tags($text, $ALLOWED_TAGS);
5b472756 587 /// Remove javascript/VBScript
588 $text = str_ireplace("javascript:", "xxx", $text);
589 /// Remove script events
590 $text = eregi_replace("([^a-z])language([[:space:]]*)=", "xxx", $text);
591 $text = eregi_replace("([^a-z])on([a-z]+)([[:space:]]*)=", "xxx", $text);
3fe3851d 592 return $text;
6901fa79 593
594 case FORMAT_PLAIN:
595 return $text;
0095d5cd 596 }
b7a3cf49 597}
f9903ed0 598
5f350e8f 599function replace_smilies(&$text) {
c1d57101 600/// Replaces all known smileys in the text with image equivalents
2ea9027b 601 global $CFG;
c1d57101 602
5b472756 603/// this builds the mapping array only once
617778f2 604 static $runonce = false;
69081931 605 static $e = array();
606 static $img = array();
617778f2 607 static $emoticons = array(
2ea9027b 608 ':-)' => 'smiley.gif',
609 ':)' => 'smiley.gif',
610 ':-D' => 'biggrin.gif',
611 ';-)' => 'wink.gif',
612 ':-/' => 'mixed.gif',
613 'V-.' => 'thoughtful.gif',
614 ':-P' => 'tongueout.gif',
615 'B-)' => 'cool.gif',
616 '^-)' => 'approve.gif',
617 '8-)' => 'wideeyes.gif',
618 ':o)' => 'clown.gif',
619 ':-(' => 'sad.gif',
620 ':(' => 'sad.gif',
621 '8-.' => 'shy.gif',
622 ':-I' => 'blush.gif',
623 ':-X' => 'kiss.gif',
624 '8-o' => 'surprise.gif',
625 'P-|' => 'blackeye.gif',
626 '8-[' => 'angry.gif',
627 'xx-P' => 'dead.gif',
628 '|-.' => 'sleepy.gif',
629 '}-]' => 'evil.gif',
630 );
631
5b472756 632 /// this is the meat of the code - this is run every time
633 if ($runonce == false){
617778f2 634 foreach ($emoticons as $emoticon => $image){
69081931 635 $e[] = $emoticon;
66a51452 636 $img[] = "<img alt=\"$emoticon\" width=\"15\" height=\"15\" src=\"$CFG->pixpath/s/$image\" />";
617778f2 637 }
638 $runonce = true;
c0f728ba 639 }
b7a3cf49 640
5f350e8f 641 $text = str_replace($e, $img, $text);
1a072208 642}
0095d5cd 643
909f539d 644function text_to_html($text, $smiley=true, $para=true) {
c1d57101 645/// Given plain text, makes it into HTML as nicely as possible.
646/// May contain HTML tags already
f9903ed0 647
27326a3e 648 global $CFG;
649
c1d57101 650/// Remove any whitespace that may be between HTML tags
7b3be1b1 651 $text = eregi_replace(">([[:space:]]+)<", "><", $text);
652
c1d57101 653/// Remove any returns that precede or follow HTML tags
0eae8049 654 $text = eregi_replace("([\n\r])<", " <", $text);
655 $text = eregi_replace(">([\n\r])", "> ", $text);
7b3be1b1 656
5f350e8f 657 convert_urls_into_links($text);
f9903ed0 658
c1d57101 659/// Make returns into HTML newlines.
f9903ed0 660 $text = nl2br($text);
661
c1d57101 662/// Turn smileys into images.
d69cb7f4 663 if ($smiley) {
5f350e8f 664 replace_smilies($text);
d69cb7f4 665 }
f9903ed0 666
c1d57101 667/// Wrap the whole thing in a paragraph tag if required
909f539d 668 if ($para) {
01d79966 669 return "<p>".$text."</p>";
909f539d 670 } else {
671 return $text;
672 }
f9903ed0 673}
674
3e9ca9fb 675function wiki_to_html($text) {
01d79966 676/// Given Wiki formatted text, make it into XHTML using external function
43373804 677 global $CFG;
3e9ca9fb 678
43373804 679 require_once("$CFG->libdir/wiki.php");
3e9ca9fb 680
01d79966 681 $wiki = new Wiki;
682 return $wiki->format($text);
3e9ca9fb 683}
684
5f350e8f 685function convert_urls_into_links(&$text) {
686/// Given some text, it converts any URLs it finds into HTML links.
687
688/// Make lone URLs into links. eg http://moodle.com/
3405b212 689 $text = eregi_replace("([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
88438a58 690 "\\1<a href=\"\\2://\\3\\4\" target=\"newpage\">\\2://\\3\\4</a>", $text);
5f350e8f 691
692/// eg www.moodle.com
3405b212 693 $text = eregi_replace("([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])",
88438a58 694 "\\1<a href=\"http://www.\\2\\3\" target=\"newpage\">www.\\2\\3</a>", $text);
5f350e8f 695}
696
88438a58 697function highlight($needle, $haystack, $case=0,
698 $left_string="<span class=\"highlight\">", $right_string="</span>") {
699/// This function will highlight search words in a given string
700/// It cares about HTML and will not ruin links. It's best to use
701/// this function after performing any conversions to HTML.
702/// Function found here: http://forums.devshed.com/t67822/scdaa2d1c3d4bacb4671d075ad41f0854.html
703
704 $list_of_words = eregi_replace("[^-a-zA-Z0-9&']", " ", $needle);
705 $list_array = explode(" ", $list_of_words);
706 for ($i=0; $i<sizeof($list_array); $i++) {
707 if (strlen($list_array[$i]) == 1) {
708 $list_array[$i] = "";
709 }
710 }
711 $list_of_words = implode(" ", $list_array);
712 $list_of_words_cp = $list_of_words;
713 $final = array();
714 preg_match_all('/<(.+?)>/is',$haystack,$list_of_words);
715
716 foreach (array_unique($list_of_words[0]) as $key=>$value) {
717 $final['<|'.$key.'|>'] = $value;
718 }
719
720 $haystack = str_replace($final,array_keys($final),$haystack);
721 $list_of_words_cp = eregi_replace(" +", "|", $list_of_words_cp);
722
723 if ($list_of_words_cp{0}=="|") {
724 $list_of_words_cp{0} = "";
725 }
726 if ($list_of_words_cp{strlen($list_of_words_cp)-1}=="|") {
727 $list_of_words_cp{strlen($list_of_words_cp)-1}="";
728 }
729 $list_of_words_cp = "(".trim($list_of_words_cp).")";
730
731 if (!$case){
732 $haystack = eregi_replace("$list_of_words_cp", "$left_string"."\\1"."$right_string", $haystack);
733 } else {
734 $haystack = ereg_replace("$list_of_words_cp", "$left_string"."\\1"."$right_string", $haystack);
735 }
736 $haystack = str_replace(array_keys($final),$final,$haystack);
737
738 return stripslashes($haystack);
739}
740
741function highlightfast($needle, $haystack) {
c1d57101 742/// This function will highlight instances of $needle in $haystack
88438a58 743/// It's faster that the above function and doesn't care about
744/// HTML or anything.
5af78ed2 745
746 $parts = explode(strtolower($needle), strtolower($haystack));
747
748 $pos = 0;
749
750 foreach ($parts as $key => $part) {
751 $parts[$key] = substr($haystack, $pos, strlen($part));
752 $pos += strlen($part);
753
88438a58 754 $parts[$key] .= "<span class=\"highlight\">".substr($haystack, $pos, strlen($needle))."</span>";
5af78ed2 755 $pos += strlen($needle);
756 }
757
758 return (join('', $parts));
759}
760
f9903ed0 761
9fa49e22 762/// STANDARD WEB PAGE PARTS ///////////////////////////////////////////////////
763
66a51452 764function print_header ($title="", $heading="", $navigation="", $focus="", $meta="",
765 $cache=true, $button="&nbsp;", $menu="", $usexml=false) {
9fa49e22 766// $title - appears top of window
767// $heading - appears top of page
768// $navigation - premade navigation string
769// $focus - indicates form element eg inputform.password
770// $meta - meta tags in the header
771// $cache - should this page be cacheable?
772// $button - HTML code for a button (usually for module editing)
66a51452 773// $menu - HTML code for a popup menu
774// $usexml - use XML for this page
e825f279 775 global $USER, $CFG, $THEME, $SESSION;
9fa49e22 776
777 if (file_exists("$CFG->dirroot/theme/$CFG->theme/styles.php")) {
778 $styles = $CFG->stylesheet;
779 } else {
780 $styles = "$CFG->wwwroot/theme/standard/styles.php";
781 }
782
783 if ($navigation == "home") {
784 $home = true;
785 $navigation = "";
9d378732 786 } else {
787 $home = false;
9fa49e22 788 }
789
790 if ($button == "") {
791 $button = "&nbsp;";
792 }
793
794 if (!$menu and $navigation) {
795 if (isset($USER->id)) {
66a51452 796 $menu = "<font size=\"2\"><a target=\"$CFG->framename\" href=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</a></font>";
9fa49e22 797 } else {
66a51452 798 $menu = "<font size=\"2\"><a target=\"$CFG->framename\" href=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</a></font>";
9fa49e22 799 }
800 }
801
802 // Specify character set ... default is iso-8859-1 but some languages might need something else
803 // Could be optimised by carrying the charset variable around in $USER
804 if (current_language() == "en") {
66a51452 805 $meta = "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\" />\n$meta\n";
9fa49e22 806 } else {
66a51452 807 $meta = "<meta http-equiv=\"content-type\" content=\"text/html; charset=".get_string("thischarset")."\" />\n$meta\n";
9fa49e22 808 }
809
d8152d04 810 if ( get_string("thisdirection") == "rtl" ) {
107b010b 811 $direction = " dir=\"rtl\"";
9fa49e22 812 } else {
107b010b 813 $direction = " dir=\"ltr\"";
9fa49e22 814 }
815
816 if (!$cache) { // Do everything we can to prevent clients and proxies caching
817 @header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
818 @header("Pragma: no-cache");
66a51452 819 $meta .= "\n<meta http-equiv=\"pragma\" content=\"no-cache\" />";
820 $meta .= "\n<meta http-equiv=\"expires\" content=\"0\" />";
821 }
822
823 if ($usexml) { // Added by Gustav Delius / Mad Alex for MathML output
824 $currentlanguage = current_language();
825
826 @header("Content-type: text/xml");
827 echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n";
828 if (!empty($CFG->xml_stylesheets)) {
829 $stylesheets = explode(";", $CFG->xml_stylesheets);
830 foreach ($stylesheets as $stylesheet) {
831 echo "<?xml-stylesheet type=\"text/xsl\" href=\"$CFG->wwwroot/$stylesheet\" ?>\n";
832 }
833 }
834 echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1";
e4576482 835 if (!empty($CFG->xml_doctype_extra)) {
66a51452 836 echo " plus $CFG->xml_doctype_extra";
e4576482 837 }
66a51452 838 echo "//" . strtoupper($currentlanguage) . "\" \"$CFG->xml_dtd\">\n";
839 $direction = " xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"$currentlanguage\" $direction";
9fa49e22 840 }
841
2eea2cce 842 $title = str_replace('"', '&quot;', $title);
843
9fa49e22 844 include ("$CFG->dirroot/theme/$CFG->theme/header.html");
845}
846
847function print_footer ($course=NULL) {
848// Can provide a course object to make the footer contain a link to
849// to the course home page, otherwise the link will go to the site home
850 global $USER, $CFG, $THEME;
851
852
853/// Course links
854 if ($course) {
855 if ($course == "home") { // special case for site home page - please do not remove
76c1650d 856 $homelink = "<p align=\"center\"><a title=\"moodle $CFG->release ($CFG->version)\" href=\"http://moodle.org/\" target=\"_blank\">";
66a51452 857 $homelink .= "<br /><img width=\"130\" height=\"19\" src=\"pix/madewithmoodle.gif\" border=\"0\" /></a></p>";
9fa49e22 858 $course = get_site();
859 $homepage = true;
860 } else {
76c1650d 861 $homelink = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>";
9fa49e22 862 }
863 } else {
c2cb4545 864 $homelink = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/\">".get_string("home")."</a>";
9fa49e22 865 $course = get_site();
866 }
867
868/// User links
a282d0ff 869 $loggedinas = user_login_string($course, $USER);
870
871 include ("$CFG->dirroot/theme/$CFG->theme/footer.html");
872}
873
1ddf9329 874function style_sheet_setup($lastmodified=0, $lifetime=300, $themename="") {
875/// This function is called by stylesheets to set up the header
876/// approriately as well as the current path
6535be85 877
878 global $CFG;
879
880 header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
881 header("Expires: " . gmdate("D, d M Y H:i:s", time() + $lifetime) . " GMT");
882 header("Cache-control: max_age = $lifetime");
883 header("Pragma: ");
884 header("Content-type: text/css"); // Correct MIME type
885
886 if (!empty($themename)) {
887 $CFG->theme = $themename;
888 }
889
890 return "$CFG->wwwroot/theme/$CFG->theme";
891
892}
893
a282d0ff 894
895function user_login_string($course, $user=NULL) {
896 global $USER, $CFG;
897
8d2accb6 898 if (empty($user)) {
a282d0ff 899 $user = $USER;
900 }
901
902 if (isset($user->realuser)) {
903 if ($realuser = get_record("user", "id", $user->realuser)) {
66a51452 904 $realuserinfo = " [<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&amp;return=$realuser->id\">$realuser->firstname $realuser->lastname</a>] ";
9fa49e22 905 }
9d378732 906 } else {
907 $realuserinfo = "";
9fa49e22 908 }
909
a282d0ff 910 if (isset($user->id) and $user->id) {
66a51452 911 $username = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">$user->firstname $user->lastname</a>";
9fa49e22 912 $loggedinas = $realuserinfo.get_string("loggedinas", "moodle", "$username").
ca16eaeb 913 " (<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</a>)";
9fa49e22 914 } else {
915 $loggedinas = get_string("loggedinnot", "moodle").
ca16eaeb 916 " (<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</a>)";
9fa49e22 917 }
a282d0ff 918 return $loggedinas;
9fa49e22 919}
920
921
9fa49e22 922function print_navigation ($navigation) {
923 global $CFG;
924
925 if ($navigation) {
926 if (! $site = get_site()) {
927 $site->shortname = get_string("home");;
928 }
eb347b6b 929 echo "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/\">$site->shortname</a> -> $navigation";
9fa49e22 930 }
931}
932
d4df9200 933function print_headline($text, $size=2) {
934 echo "<b><font size=\"$size\">$text</font></b><br />\n";
935}
936
76c1650d 937function print_heading($text, $align="center", $size=3) {
938 echo "<p align=\"$align\"><font size=\"$size\"><b>".stripslashes_safe($text)."</b></font></p>";
9fa49e22 939}
940
c9f6251e 941function print_heading_with_help($text, $helppage, $module="moodle", $icon="") {
9fa49e22 942// Centered heading with attached help button (same title text)
c9f6251e 943// and optional icon attached
944 echo "<p align=\"center\"><font size=\"3\">$icon<b>".stripslashes_safe($text);
9fa49e22 945 helpbutton($helppage, $text, $module);
eb347b6b 946 echo "</b></font></p>";
9fa49e22 947}
948
949function print_continue($link) {
9fa49e22 950
951 if (!$link) {
607809b3 952 $link = $_SERVER["HTTP_REFERER"];
9fa49e22 953 }
954
eb347b6b 955 print_heading("<a href=\"$link\">".get_string("continue")."</a>");
9fa49e22 956}
957
958
959function print_simple_box($message, $align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
960 print_simple_box_start($align, $width, $color, $padding, $class);
7d8f674d 961 echo stripslashes_safe($message);
9fa49e22 962 print_simple_box_end();
963}
964
965function print_simple_box_start($align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
966 global $THEME;
967
968 if ($align) {
76c1650d 969 $align = "align=\"$align\"";
9fa49e22 970 }
971 if ($width) {
76c1650d 972 $width = "width=\"$width\"";
9fa49e22 973 }
9d378732 974 echo "<table $align $width class=\"$class\" border=\"0\" cellpadding=\"$padding\" cellspacing=\"0\"><tr><td bgcolor=\"$color\" class=\"$class"."content\">";
9fa49e22 975}
976
977function print_simple_box_end() {
978 echo "</td></tr></table>";
979}
980
cc7fa0dc 981function print_single_button($link, $options, $label="OK", $method="get") {
982 echo "<form action=\"$link\" method=\"$method\">";
9fa49e22 983 if ($options) {
984 foreach ($options as $name => $value) {
66a51452 985 echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
9fa49e22 986 }
987 }
66a51452 988 echo "<input type=\"submit\" value=\"$label\" /></form>";
9fa49e22 989}
990
991function print_spacer($height=1, $width=1, $br=true) {
992 global $CFG;
66a51452 993 echo "<img height=\"$height\" width=\"$width\" src=\"$CFG->wwwroot/pix/spacer.gif\" alt=\"\" />";
9fa49e22 994 if ($br) {
76c1650d 995 echo "<br />\n";
9fa49e22 996 }
997}
998
999function print_file_picture($path, $courseid=0, $height="", $width="", $link="") {
1000// Given the path to a picture file in a course, or a URL,
1001// this function includes the picture in the page.
1002 global $CFG;
1003
1004 if ($height) {
76c1650d 1005 $height = "height=\"$height\"";
9fa49e22 1006 }
1007 if ($width) {
76c1650d 1008 $width = "width=\"$width\"";
9fa49e22 1009 }
1010 if ($link) {
76c1650d 1011 echo "<a href=\"$link\">";
9fa49e22 1012 }
1013 if (substr(strtolower($path), 0, 7) == "http://") {
66a51452 1014 echo "<img border=\"0\" $height $width src=\"$path\" />";
9fa49e22 1015
1016 } else if ($courseid) {
66a51452 1017 echo "<img border=\"0\" $height $width src=\"";
9fa49e22 1018 if ($CFG->slasharguments) { // Use this method if possible for better caching
1019 echo "$CFG->wwwroot/file.php/$courseid/$path";
1020 } else {
3f396065 1021 echo "$CFG->wwwroot/file.php?file=/$courseid/$path";
9fa49e22 1022 }
66a51452 1023 echo "\" />";
9fa49e22 1024 } else {
1025 echo "Error: must pass URL or course";
1026 }
1027 if ($link) {
76c1650d 1028 echo "</a>";
9fa49e22 1029 }
1030}
1031
1032function print_user_picture($userid, $courseid, $picture, $large=false, $returnstring=false, $link=true) {
67a63a30 1033 global $CFG, $THEME;
9fa49e22 1034
1035 if ($link) {
66a51452 1036 $output = "<a href=\"$CFG->wwwroot/user/view.php?id=$userid&amp;course=$courseid\">";
9fa49e22 1037 } else {
1038 $output = "";
1039 }
1040 if ($large) {
67a63a30 1041 $file = "f1";
9fa49e22 1042 $size = 100;
1043 } else {
67a63a30 1044 $file = "f2";
9fa49e22 1045 $size = 35;
1046 }
67a63a30 1047 if ($picture) { // Print custom user picture
9fa49e22 1048 if ($CFG->slasharguments) { // Use this method if possible for better caching
67a63a30 1049 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/user/pix.php/$userid/$file.jpg\"".
66a51452 1050 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" />";
9fa49e22 1051 } else {
67a63a30 1052 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/user/pix.php?file=/$userid/$file.jpg\"".
66a51452 1053 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" />";
9fa49e22 1054 }
67a63a30 1055 } else { // Print default user pictures (use theme version if available)
c9f6251e 1056 $output .= "<img align=\"absmiddle\" src=\"$CFG->pixpath/u/$file.png\"".
66a51452 1057 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" />";
9fa49e22 1058 }
1059 if ($link) {
76c1650d 1060 $output .= "</a>";
9fa49e22 1061 }
1062
1063 if ($returnstring) {
1064 return $output;
1065 } else {
1066 echo $output;
1067 }
1068}
1069
1070function print_table($table) {
1071// Prints a nicely formatted table.
1072// $table is an object with several properties.
1073// $table->head is an array of heading names.
1074// $table->align is an array of column alignments
1075// $table->size is an array of column sizes
5867bfb5 1076// $table->wrap is an array of "nowrap"s or nothing
9fa49e22 1077// $table->data[] is an array of arrays containing the data.
1078// $table->width is an percentage of the page
1079// $table->cellpadding padding on each cell
1080// $table->cellspacing spacing between cells
1081
1082 if (isset($table->align)) {
1083 foreach ($table->align as $key => $aa) {
1084 if ($aa) {
76c1650d 1085 $align[$key] = " align=\"$aa\"";
9fa49e22 1086 } else {
1087 $align[$key] = "";
1088 }
1089 }
1090 }
1091 if (isset($table->size)) {
1092 foreach ($table->size as $key => $ss) {
1093 if ($ss) {
76c1650d 1094 $size[$key] = " width=\"$ss\"";
9fa49e22 1095 } else {
1096 $size[$key] = "";
1097 }
1098 }
1099 }
5867bfb5 1100 if (isset($table->wrap)) {
1101 foreach ($table->wrap as $key => $ww) {
1102 if ($ww) {
66a51452 1103 $wrap[$key] = " nowrap=\"nowrap\" ";
5867bfb5 1104 } else {
1105 $wrap[$key] = "";
1106 }
1107 }
1108 }
9fa49e22 1109
9d378732 1110 if (empty($table->width)) {
9fa49e22 1111 $table->width = "80%";
1112 }
1113
9d378732 1114 if (empty($table->cellpadding)) {
9fa49e22 1115 $table->cellpadding = "5";
1116 }
1117
9d378732 1118 if (empty($table->cellspacing)) {
9fa49e22 1119 $table->cellspacing = "1";
1120 }
1121
5867bfb5 1122 print_simple_box_start("center", "$table->width", "#ffffff", 0);
66a51452 1123 echo "<table width=\"100%\" border=\"0\" valign=\"top\" align=\"center\" ";
9fa49e22 1124 echo " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"generaltable\">\n";
1125
b79f41cd 1126 if (!empty($table->head)) {
5867bfb5 1127 echo "<tr>";
9fa49e22 1128 foreach ($table->head as $key => $heading) {
9d378732 1129 if (!isset($size[$key])) {
1130 $size[$key] = "";
1131 }
1132 if (!isset($align[$key])) {
1133 $align[$key] = "";
1134 }
66a51452 1135 echo "<th valign=\"top\" ".$align[$key].$size[$key]." nowrap=\"nowrap\" class=\"generaltableheader\">$heading</th>";
9fa49e22 1136 }
66a51452 1137 echo "</tr>\n";
9fa49e22 1138 }
1139
a1f8ff87 1140 if (!empty($table->data)) {
1141 foreach ($table->data as $row) {
1142 echo "<tr valign=\"top\">";
1143 foreach ($row as $key => $item) {
1144 if (!isset($size[$key])) {
1145 $size[$key] = "";
1146 }
1147 if (!isset($align[$key])) {
1148 $align[$key] = "";
1149 }
1150 if (!isset($wrap[$key])) {
1151 $wrap[$key] = "";
1152 }
1153 echo "<td ".$align[$key].$size[$key].$wrap[$key]." class=\"generaltablecell\">$item</td>";
1154 }
1155 echo "</tr>\n";
9fa49e22 1156 }
9fa49e22 1157 }
5867bfb5 1158 echo "</table>\n";
9fa49e22 1159 print_simple_box_end();
1160
1161 return true;
1162}
1163
2f4d324b 1164function make_table($table) {
1165// Creates a nicely formatted table and returns it
1166// $table is an object with several properties.
1167// $table->head is an array of heading names.
1168// $table->align is an array of column alignments
1169// $table->size is an array of column sizes
1170// $table->wrap is an array of "nowrap"s or nothing
1171// $table->data[] is an array of arrays containing the data.
1172// $table->width is an percentage of the page
1173// $table->class is a class
1174// $table->fontsize is the size of all the text
1175// $table->tablealign align the whole table
1176// $table->cellpadding padding on each cell
1177// $table->cellspacing spacing between cells
1178
1179 if (isset($table->align)) {
1180 foreach ($table->align as $key => $aa) {
1181 if ($aa) {
1182 $align[$key] = " align=\"$aa\"";
1183 } else {
1184 $align[$key] = "";
1185 }
1186 }
1187 }
1188 if (isset($table->size)) {
1189 foreach ($table->size as $key => $ss) {
1190 if ($ss) {
1191 $size[$key] = " width=\"$ss\"";
1192 } else {
1193 $size[$key] = "";
1194 }
1195 }
1196 }
1197 if (isset($table->wrap)) {
1198 foreach ($table->wrap as $key => $ww) {
1199 if ($ww) {
66a51452 1200 $wrap[$key] = " nowrap=\"nowrap\" ";
2f4d324b 1201 } else {
1202 $wrap[$key] = "";
1203 }
1204 }
1205 }
1206
1207 if (empty($table->width)) {
1208 $table->width = "80%";
1209 }
1210
1211 if (empty($table->tablealign)) {
1212 $table->tablealign = "center";
1213 }
1214
1215 if (empty($table->cellpadding)) {
1216 $table->cellpadding = "5";
1217 }
1218
1219 if (empty($table->cellspacing)) {
1220 $table->cellspacing = "1";
1221 }
1222
1223 if (empty($table->class)) {
1224 $table->class = "generaltable";
1225 }
1226
1227 if (empty($table->fontsize)) {
1228 $fontsize = "";
1229 } else {
1230 $fontsize = "<font size=\"$table->fontsize\">";
1231 }
1232
66a51452 1233 $output = "<table width=\"$table->width\" valign=\"top\" align=\"$table->tablealign\" ";
2f4d324b 1234 $output .= " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"$table->class\">\n";
1235
1236 if (!empty($table->head)) {
1237 $output .= "<tr>";
1238 foreach ($table->head as $key => $heading) {
1239 if (!isset($size[$key])) {
1240 $size[$key] = "";
1241 }
1242 if (!isset($align[$key])) {
1243 $align[$key] = "";
1244 }
66a51452 1245 $output .= "<th valign=\"top\" ".$align[$key].$size[$key]." nowrap=\"nowrap\" class=\"{$table->class}header\">$fontsize$heading</th>";
2f4d324b 1246 }
1247 $output .= "</tr>\n";
1248 }
1249
1250 foreach ($table->data as $row) {
66a51452 1251 $output .= "<tr valign=\"top\">";
2f4d324b 1252 foreach ($row as $key => $item) {
1253 if (!isset($size[$key])) {
1254 $size[$key] = "";
1255 }
1256 if (!isset($align[$key])) {
1257 $align[$key] = "";
1258 }
1259 if (!isset($wrap[$key])) {
1260 $wrap[$key] = "";
1261 }
1262 $output .= "<td ".$align[$key].$size[$key].$wrap[$key]." class=\"{$table->class}cell\">$fontsize$item</td>";
1263 }
1264 $output .= "</tr>\n";
1265 }
1266 $output .= "</table>\n";
1267
1268 return $output;
1269}
1270
50bdc74d 1271function print_textarea($richedit, $rows, $cols, $width, $height, $name, $value="", $courseid=0) {
7d8f674d 1272/// Prints a richtext field or a normal textarea
4c46c425 1273
50bdc74d 1274 global $CFG, $THEME, $course;
1275
408e62f8 1276 if (empty($courseid)) {
50bdc74d 1277 if (!empty($course->id)) { // search for it in global context
1278 $courseid = $course->id;
1279 }
1280 }
9fa49e22 1281
1282 if ($richedit) {
4c46c425 1283 if (!empty($CFG->useneweditor)) { /// Use the new HTMLarea editor
50bdc74d 1284
4c46c425 1285 if (!empty($courseid) and isteacher($courseid)) {
1286 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/htmlarea.php?id=$courseid\"></script>\n";
1287 } else {
1288 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/htmlarea.php\"></script>\n";
1289 }
1290 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/dialog.js\"></script>\n";
1291 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/lang/en.php\"></script>\n";
1292 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/popupwin.js\"></script>\n";
1293 echo "<style type=\"text/css\">@import url($CFG->wwwroot/lib/editor/htmlarea.css);</style>\n";
1294 if ($rows < 20) {
1295 $rows = 20; /// Minimum rows
1296 }
1297 if ($cols < 65) {
1298 $cols = 65; /// Minimum columns
1299 }
1300 echo "<textarea id=\"TA\" name=\"$name\" rows=\"$rows\" cols=\"$cols\" wrap=\"virtual\">";
1301 p($value);
1302 echo "</textarea>\n";
1303
1304 } else { /// Use the old Richtext editor
1305 $richediturl = "$CFG->wwwroot/lib/rte/richedit.html";
1306 if (!empty($courseid) and isteacher($courseid)) {
1307 $richediturl = "$CFG->wwwroot/lib/rte/richedit.php?id=$courseid";
1308 }
1309
1310 echo "<object id=\"richedit\" style=\"background-color: buttonface\"";
1311 echo " data=\"$richediturl\"";
1312 echo " width=\"$width\" height=\"$height\" ";
1313 echo " type=\"text/x-scriptlet\" VIEWASTEXT=\"true\"></object>\n";
1314 echo "<textarea style=\"display:none\" name=\"$name\" rows=\"1\" cols=\"1\">";
1315 p($value);
1316 echo "</textarea>\n";
1317 }
9fa49e22 1318 } else {
66a51452 1319 echo "<textarea name=\"$name\" rows=\"$rows\" cols=\"$cols\" wrap=\"virtual\">";
9fa49e22 1320 p($value);
76c1650d 1321 echo "</textarea>\n";
9fa49e22 1322 }
1323}
1324
1325function print_richedit_javascript($form, $name, $source="no") {
4c46c425 1326 global $CFG;
1327
1328 if (!empty($CFG->useneweditor)) { /// Use the new HTMLarea editor
1329 echo "<script language=\"javascript\" type=\"text/javascript\" defer=\"1\">\n";
1330 echo "var editor = null;\n";
1331 echo "function initEditor() {\n";
1332 echo " editor = new HTMLArea(\"TA\");\n";
1333 echo " editor.generate();\n";
1334 echo " return false;\n";
1335 echo "}\n";
1336 echo "initEditor();\n";
1337 echo "</script>\n";
1338
1339 } else { /// Use the old Richtext editor
1340 echo "<script language=\"javascript\" event=\"onload\" for=\"window\">\n";
1341 echo " document.richedit.options = \"history=no;source=$source\";";
1342 echo " document.richedit.docHtml = $form.$name.innerText;";
1343 echo "</script>";
1344 }
9fa49e22 1345}
1346
1347
1348function update_course_icon($courseid) {
1349// Used to be an icon, but it's now a simple form button
1350 global $CFG, $USER;
1351
b6c12732 1352 if (isteacheredit($courseid)) {
9c9f7d77 1353 if (!empty($USER->editing)) {
9fa49e22 1354 $string = get_string("turneditingoff");
1355 $edit = "off";
1356 } else {
1357 $string = get_string("turneditingon");
1358 $edit = "on";
1359 }
66a51452 1360 return "<form target=\"_parent\" method=\"get\" action=\"$CFG->wwwroot/course/view.php\">".
1361 "<input type=\"hidden\" name=\"id\" value=\"$courseid\" />".
1362 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1363 "<input type=\"submit\" value=\"$string\" /></form>";
9fa49e22 1364 }
1365}
1366
1367function update_module_button($moduleid, $courseid, $string) {
1368// Prints the editing button on a module "view" page
1369 global $CFG;
1370
b6c12732 1371 if (isteacheredit($courseid)) {
9fa49e22 1372 $string = get_string("updatethis", "", $string);
66a51452 1373 return "<form target=\"_parent\" method=\"get\" action=\"$CFG->wwwroot/course/mod.php\">".
1374 "<input type=\"hidden\" name=\"update\" value=\"$moduleid\" />".
1375 "<input type=\"hidden\" name=\"return\" value=\"true\" />".
1376 "<input type=\"submit\" value=\"$string\" /></form>";
b6c12732 1377 } else {
1378 return "";
9fa49e22 1379 }
1380}
1381
c2cb4545 1382function update_category_button($categoryid) {
d2b6ba70 1383// Prints the editing button on a category page
1384 global $CFG, $USER;
c2cb4545 1385
d2b6ba70 1386 if (iscreator()) {
1387 if (!empty($USER->editing)) {
1388 $string = get_string("turneditingoff");
1389 $edit = "off";
1390 } else {
1391 $string = get_string("turneditingon");
1392 $edit = "on";
9b16d1ea 1393 }
66a51452 1394 return "<form method=\"get\" action=\"$CFG->wwwroot/course/category.php\">".
1395 "<input type=\"hidden\" name=\"id\" value=\"$categoryid\" />".
1396 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1397 "<input type=\"submit\" value=\"$string\" /></form>";
d2b6ba70 1398 }
1399}
1400
1401function update_categories_button() {
1402// Prints the editing button on categories listing
1403 global $CFG, $USER;
1404
1405 if (isadmin()) {
1406 if (!empty($USER->editing)) {
1407 $string = get_string("turneditingoff");
1408 $edit = "off";
1409 } else {
1410 $string = get_string("turneditingon");
1411 $edit = "on";
1412 }
66a51452 1413 return "<form target=\"_parent\" method=\"get\" action=\"$CFG->wwwroot/course/index.php\">".
1414 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1415 "<input type=\"submit\" value=\"$string\" /></form>";
c2cb4545 1416 }
1417}
9fa49e22 1418
16ef5e78 1419function navmenu($course, $cm=NULL, $targetwindow="self") {
9fa49e22 1420// Given a course and a (current) coursemodule
1421// This function returns a small popup menu with all the
1422// course activity modules in it, as a navigation menu
1423// The data is taken from the serialised array stored in
1424// the course record
1425
1426 global $CFG;
1427
1428 if ($cm) {
1429 $cm = $cm->id;
1430 }
1431
1432 if ($course->format == 'weeks') {
1433 $strsection = get_string("week");
1434 } else {
1435 $strsection = get_string("topic");
1436 }
1437
1438 if (!$modinfo = unserialize($course->modinfo)) {
1439 return "";
1440 }
1441 $section = -1;
1442 $selected = "";
1443 foreach ($modinfo as $mod) {
ab2df10c 1444 if ($mod->mod == "label") {
1445 continue;
1446 }
9fa49e22 1447 if ($mod->section > 0 and $section <> $mod->section) {
1448 $menu[] = "-------------- $strsection $mod->section --------------";
1449 }
1450 $section = $mod->section;
cf055081 1451 //Only add visible or teacher mods to jumpmenu
1452 if ($mod->visible or isteacher($course->id)) {
1453 $url = "$mod->mod/view.php?id=$mod->cm";
1454 if ($cm == $mod->cm) {
1455 $selected = $url;
1456 }
1457 $mod->name = urldecode($mod->name);
1458 if (strlen($mod->name) > 55) {
1459 $mod->name = substr($mod->name, 0, 50)."...";
1460 }
2a409368 1461 if (!$mod->visible) {
1462 $mod->name = "(".$mod->name.")";
1463 }
cf055081 1464 $menu[$url] = $mod->name;
9fa49e22 1465 }
9fa49e22 1466 }
1467
16ef5e78 1468 return popup_form("$CFG->wwwroot/mod/", $menu, "navmenu", $selected, get_string("jumpto"),
1469 "", "", true, $targetwindow);
9fa49e22 1470}
1471
1472
1473
1474function print_date_selector($day, $month, $year, $currenttime=0) {
1475// Currenttime is a default timestamp in GMT
1476// Prints form items with the names $day, $month and $year
1477
1478 if (!$currenttime) {
1479 $currenttime = time();
1480 }
1481 $currentdate = usergetdate($currenttime);
1482
1483 for ($i=1; $i<=31; $i++) {
1484 $days[$i] = "$i";
1485 }
1486 for ($i=1; $i<=12; $i++) {
39e018b3 1487 $months[$i] = userdate(gmmktime(12,0,0,$i,1,2000), "%B");
9fa49e22 1488 }
1489 for ($i=2000; $i<=2010; $i++) {
1490 $years[$i] = $i;
1491 }
47f1da80 1492 choose_from_menu($days, $day, $currentdate['mday'], "");
1493 choose_from_menu($months, $month, $currentdate['mon'], "");
1494 choose_from_menu($years, $year, $currentdate['year'], "");
9fa49e22 1495}
1496
1497function print_time_selector($hour, $minute, $currenttime=0) {
1498// Currenttime is a default timestamp in GMT
1499// Prints form items with the names $hour and $minute
1500
1501 if (!$currenttime) {
1502 $currenttime = time();
1503 }
1504 $currentdate = usergetdate($currenttime);
1505 for ($i=0; $i<=23; $i++) {
1506 $hours[$i] = sprintf("%02d",$i);
1507 }
1508 for ($i=0; $i<=59; $i++) {
1509 $minutes[$i] = sprintf("%02d",$i);
1510 }
47f1da80 1511 choose_from_menu($hours, $hour, $currentdate['hours'], "");
1512 choose_from_menu($minutes, $minute, $currentdate['minutes'], "");
9fa49e22 1513}
1514
d6bdd9d5 1515function print_grade_menu($courseid, $name, $current, $includenograde=true) {
62ca135d 1516/// Prints a grade menu (as part of an existing form) with help
1517/// Showing all possible numerical grades and scales
1518
c9f6251e 1519 global $CFG;
62ca135d 1520
1521 $strscale = get_string("scale");
1522 $strscales = get_string("scales");
1523
1f7deef6 1524 $scales = get_scales_menu($courseid);
62ca135d 1525 foreach ($scales as $i => $scalename) {
1526 $grades[-$i] = "$strscale: $scalename";
1527 }
d6bdd9d5 1528 if ($includenograde) {
1529 $grades[0] = get_string("nograde");
1530 }
62ca135d 1531 for ($i=100; $i>=1; $i--) {
1532 $grades[$i] = $i;
1533 }
1534 choose_from_menu($grades, "$name", "$current", "");
1535
c9f6251e 1536 $helpicon = "$CFG->pixpath/help.gif";
66a51452 1537 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" alt=\"$strscales\" src=\"$helpicon\" />";
1538 link_to_popup_window ("/course/scales.php?id=$courseid&amp;list=true", "ratingscales",
62ca135d 1539 $linkobject, 400, 500, $strscales);
1540}
1541
02ebf404 1542function print_scale_menu($courseid, $name, $current) {
1543/// Prints a scale menu (as part of an existing form) including help button
62ca135d 1544/// Just like print_grade_menu but without the numerical grades
02ebf404 1545
c9f6251e 1546 global $CFG;
02ebf404 1547
1548 $strscales = get_string("scales");
1549 choose_from_menu(get_scales_menu($courseid), "$name", $current, "");
c9f6251e 1550 $helpicon = "$CFG->pixpath/help.gif";
66a51452 1551 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" alt=\"$strscales\" src=\"$helpicon\" />";
1552 link_to_popup_window ("/course/scales.php?id=$courseid&amp;list=true", "ratingscales",
02ebf404 1553 $linkobject, 400, 500, $strscales);
1554}
1555
fdc47ee6 1556
02ebf404 1557function print_scale_menu_helpbutton($courseid, $scale) {
1558/// Prints a help button about a scale
1559/// scale is an object
1560
c9f6251e 1561 global $CFG;
02ebf404 1562
1563 $strscales = get_string("scales");
c9f6251e 1564 $helpicon = "$CFG->pixpath/help.gif";
66a51452 1565 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" alt=\"$scale->name\" src=\"$helpicon\" />";
1566 link_to_popup_window ("/course/scales.php?id=$courseid&amp;list=true&amp;scale=$scale->id", "ratingscale",
02ebf404 1567 $linkobject, 400, 500, $scale->name);
1568}
1569
1570
9fa49e22 1571function error ($message, $link="") {
1572 global $CFG, $SESSION;
1573
1574 print_header(get_string("error"));
66a51452 1575 echo "<br />";
9fa49e22 1576 print_simple_box($message, "center", "", "#FFBBBB");
1577
1578 if (!$link) {
1579 if ( !empty($SESSION->fromurl) ) {
1580 $link = "$SESSION->fromurl";
1581 unset($SESSION->fromurl);
9fa49e22 1582 } else {
c2cb4545 1583 $link = "$CFG->wwwroot/";
9fa49e22 1584 }
1585 }
1586 print_continue($link);
1587 print_footer();
1588 die;
1589}
1590
1591function helpbutton ($page, $title="", $module="moodle", $image=true, $linktext=false, $text="") {
1592 // $page = the keyword that defines a help page
1593 // $title = the title of links, rollover tips, alt tags etc
1594 // $module = which module is the page defined in
1595 // $image = use a help image for the link? (true/false/"both")
1596 // $text = if defined then this text is used in the page, and
1597 // the $page variable is ignored.
dc0dc7d5 1598 global $CFG, $THEME;
9fa49e22 1599
1600 if ($module == "") {
1601 $module = "moodle";
1602 }
1603
1604 if ($image) {
c9f6251e 1605 $icon = "$CFG->pixpath/help.gif";
9fa49e22 1606 if ($linktext) {
66a51452 1607 $linkobject = "$title<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" alt=\"\" src=\"$icon\" />";
9fa49e22 1608 } else {
66a51452 1609 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" alt=\"$title\" src=\"$icon\" />";
9fa49e22 1610 }
1611 } else {
1612 $linkobject = $title;
1613 }
1614 if ($text) {
66a51452 1615 $url = "/help.php?module=$module&amp;text=".htmlentities(urlencode($text));
9fa49e22 1616 } else {
66a51452 1617 $url = "/help.php?module=$module&amp;file=$page.html";
9fa49e22 1618 }
1619 link_to_popup_window ($url, "popup", $linkobject, 400, 500, $title);
1620}
1621
e825f279 1622function emoticonhelpbutton($form, $field) {
1623/// Prints a special help button that is a link to the "live" emoticon popup
1624 global $CFG, $SESSION;
1625
1626 $SESSION->inserttextform = $form;
1627 $SESSION->inserttextfield = $field;
1628 helpbutton("emoticons", get_string("helpemoticons"), "moodle", false, true);
c9f6251e 1629 echo "&nbsp;";
66a51452 1630 link_to_popup_window ("/help.php?module=moodle&amp;file=emoticons.html", "popup",
1631 "<img src=\"$CFG->pixpath/s/smiley.gif\" border=\"0\" align=\"absmiddle\" width=\"15\" height=\"15\" />",
c9f6251e 1632 400, 500, get_string("helpemoticons"));
1633 echo "<br />";
e825f279 1634}
1635
9fa49e22 1636function notice ($message, $link="") {
750ab759 1637 global $CFG, $THEME;
9fa49e22 1638
1639 if (!$link) {
750ab759 1640 if (!empty($_SERVER["HTTP_REFERER"])) {
1641 $link = $_SERVER["HTTP_REFERER"];
1642 } else {
c2cb4545 1643 $link = "$CFG->wwwroot/";
750ab759 1644 }
9fa49e22 1645 }
1646
01d79966 1647 echo "<br />";
11a876e1 1648 print_simple_box($message, "center", "50%", "$THEME->cellheading", "20", "noticebox");
eb347b6b 1649 print_heading("<a href=\"$link\">".get_string("continue")."</a>");
9fa49e22 1650 print_footer(get_site());
1651 die;
1652}
1653
1654function notice_yesno ($message, $linkyes, $linkno) {
1655 global $THEME;
1656
eb347b6b 1657 print_simple_box_start("center", "60%", "$THEME->cellheading");
66a51452 1658 echo "<p align=\"center\"><font size=\"3\">$message</font></p>";
1659 echo "<p align=\"center\"><font size=\"3\"><b>";
eb347b6b 1660 echo "<a href=\"$linkyes\">".get_string("yes")."</a>";
9fa49e22 1661 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
eb347b6b 1662 echo "<a href=\"$linkno\">".get_string("no")."</a>";
1663 echo "</b></font></p>";
9fa49e22 1664 print_simple_box_end();
1665}
1666
559573a2 1667function redirect($url, $message="", $delay="0") {
5d6c043a 1668// Redirects the user to another page, after printing a notice
9fa49e22 1669
c9082a8c 1670 if (empty($message)) {
5d6c043a 1671 sleep($delay);
8a55c984 1672 @header("Location: $url");
1673 echo "<script>location.replace('$url');</script>"; // To cope with Mozilla bug
c9082a8c 1674 } else {
05c19593 1675 if (empty($delay)) {
c9082a8c 1676 $delay = 3; // There's no point having a message with no delay
1677 }
5f546a46 1678 print_header("", "", "", "", "<meta http-equiv=\"refresh\" content=\"$delay; url=$url\" />");
76c1650d 1679 echo "<center>";
1680 echo "<p>$message</p>";
1681 echo "<p>( <a href=\"$url\">".get_string("continue")."</a> )</p>";
1682 echo "</center>";
ff019455 1683 flush();
1684 sleep($delay);
1685 echo "<script>location.replace('$url');</script>"; // To cope with Mozilla bug
9fa49e22 1686 }
1687 die;
1688}
1689
99988d1a 1690function notify ($message, $color="red", $align="center") {
1691 echo "<p align=\"$align\"><b><font color=\"$color\">$message</font></b></p>\n";
9fa49e22 1692}
1693
43373804 1694function obfuscate_email($email) {
1695/// Given an email address, this function will return an obfuscated version of it
1696 $i = 0;
1697 $length = strlen($email);
1698 $obfuscated = "";
1699 while ($i < $length) {
1700 if (rand(0,2)) {
1701 $obfuscated.='%'.dechex(ord($email{$i}));
1702 } else {
1703 $obfuscated.=$email{$i};
1704 }
1705 $i++;
1706 }
1707 return $obfuscated;
1708}
1709
1710function obfuscate_text($plaintext) {
1711/// This function takes some text and replaces about half of the characters
1712/// with HTML entity equivalents. Return string is obviously longer.
1713 $i=0;
1714 $length = strlen($plaintext);
1715 $obfuscated="";
2b09e377 1716 $prev_obfuscated = false;
43373804 1717 while ($i < $length) {
2b09e377 1718 $c = ord($plaintext{$i});
1719 $numerical = ($c >= ord('0')) && ($c <= ord('9'));
1720 if ($prev_obfuscated and $numerical ) {
1721 $obfuscated.='&#'.ord($plaintext{$i});
1722 } else if (rand(0,2)) {
43373804 1723 $obfuscated.='&#'.ord($plaintext{$i});
2b09e377 1724 $prev_obfuscated = true;
43373804 1725 } else {
1726 $obfuscated.=$plaintext{$i};
2b09e377 1727 $prev_obfuscated = false;
43373804 1728 }
2b09e377 1729 $i++;
43373804 1730 }
1731 return $obfuscated;
1732}
1733
1734function obfuscate_mailto($email, $label="") {
1735/// This function uses the above two functions to generate a fully
1736/// obfuscated email link, ready to use.
1737
1738 if (empty($label)) {
1739 $label = $email;
1740 }
1741 return sprintf('<a href="%s:%s" title="%s">%s</a>', obfuscate_text('mailto'),
1742 obfuscate_email($email),
1743 obfuscate_text($email),
1744 obfuscate_text($label));
1745}
1746
8b9c7aa0 1747function print_paging_bar($totalcount, $page, $perpage, $baseurl) {
1748/// Prints a single paging bar to provide access to other pages (usually in a search)
1749
519d369f 1750 $maxdisplay = 18;
8ef9cb56 1751
8b9c7aa0 1752 if ($totalcount > $perpage) {
f04dc61d 1753 echo "<center>";
1754 echo "<p>".get_string("page").":";
be20753e 1755 $lastpage = ceil($totalcount / $perpage);
1756 if ($page > 15) {
1757 $startpage = $page - 10;
519d369f 1758 echo "&nbsp<a href=\"{$baseurl}page=0\">1</a>&nbsp;...";
be20753e 1759 } else {
1760 $startpage = 0;
1761 }
be20753e 1762 $currpage = $startpage;
1763 $displaycount = 0;
1764 while ($displaycount < $maxdisplay and $currpage < $lastpage) {
1765 $displaypage = $currpage+1;
1766 if ($page == $currpage) {
8b9c7aa0 1767 echo "&nbsp;&nbsp;$displaypage";
1768 } else {
be20753e 1769 echo "&nbsp;&nbsp;<a href=\"{$baseurl}page=$currpage\">$displaypage</a>";
e27dbcc8 1770 }
be20753e 1771 $displaycount++;
1772 $currpage++;
8b9c7aa0 1773 }
924cef21 1774 if ($currpage < $lastpage) {
519d369f 1775 $lastpageactual = $lastpage - 1;
1776 echo "&nbsp;...<a href=\"{$baseurl}page=$lastpageactual\">$lastpage</a>&nbsp;";
924cef21 1777 }
8b9c7aa0 1778 $pagenum = $page + 1;
be20753e 1779 if ($pagenum != $displaypage) {
8b9c7aa0 1780 echo "&nbsp;&nbsp;(<a href=\"{$baseurl}page=$pagenum\">".get_string("next")."</a>)";
1781 }
1782 echo "</p>";
be20753e 1783 echo "</center>";
8b9c7aa0 1784 }
1785}
9fa49e22 1786
9d5b689c 1787// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
f9903ed0 1788?>