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