Removed a line from config.php
[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
0095d5cd 35define("FORMAT_MOODLE", "0");
36define("FORMAT_HTML", "1");
37
c9dda990 38$SMILEY_TEXT[] = ":-)";
39$SMILEY_IMAGE[] = "<IMG ALT=\":-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/smiley.gif\">";
40$SMILEY_TEXT[] = ":)";
41$SMILEY_IMAGE[] = "<IMG ALT=\":-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/smiley.gif\">";
42$SMILEY_TEXT[] = ":-D";
43$SMILEY_IMAGE[] = "<IMG ALT=\":-D\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/biggrin.gif\">";
44$SMILEY_TEXT[] = ";-)";
45$SMILEY_IMAGE[] = "<IMG ALT=\";-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/wink.gif\">";
46$SMILEY_TEXT[] = ":-/";
47$SMILEY_IMAGE[] = "<IMG ALT=\":-/\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/mixed.gif\">";
48$SMILEY_TEXT[] = "V-.";
49$SMILEY_IMAGE[] = "<IMG ALT=\"V-.\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/thoughtful.gif\">";
50$SMILEY_TEXT[] = ":-P";
51$SMILEY_IMAGE[] = "<IMG ALT=\":-P\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/tongueout.gif\">";
52$SMILEY_TEXT[] = "B-)";
53$SMILEY_IMAGE[] = "<IMG ALT=\"B-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/cool.gif\">";
54$SMILEY_TEXT[] = "^-)";
55$SMILEY_IMAGE[] = "<IMG ALT=\"^-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/approve.gif\">";
56$SMILEY_TEXT[] = "8-)";
57$SMILEY_IMAGE[] = "<IMG ALT=\"8-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/wideeyes.gif\">";
58$SMILEY_TEXT[] = ":o)";
59$SMILEY_IMAGE[] = "<IMG ALT=\":o)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/clown.gif\">";
60$SMILEY_TEXT[] = ":-(";
61$SMILEY_IMAGE[] = "<IMG ALT=\":-(\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/sad.gif\">";
62$SMILEY_TEXT[] = ":(";
63$SMILEY_IMAGE[] = "<IMG ALT=\":-(\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/sad.gif\">";
64$SMILEY_TEXT[] = "8-.";
65$SMILEY_IMAGE[] = "<IMG ALT=\"8-.\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/shy.gif\">";
66$SMILEY_TEXT[] = ":-I";
67$SMILEY_IMAGE[] = "<IMG ALT=\":-I\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/blush.gif\">";
68$SMILEY_TEXT[] = ":-X";
69$SMILEY_IMAGE[] = "<IMG ALT=\":-X\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/kiss.gif\">";
70$SMILEY_TEXT[] = "8-o";
71$SMILEY_IMAGE[] = "<IMG ALT=\"8-o\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/surprise.gif\">";
72$SMILEY_TEXT[] = "P-|";
73$SMILEY_IMAGE[] = "<IMG ALT=\"P-|\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/blackeye.gif\">";
74$SMILEY_TEXT[] = "8-[";
75$SMILEY_IMAGE[] = "<IMG ALT=\"8-[\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/angry.gif\">";
76$SMILEY_TEXT[] = "xx-P";
77$SMILEY_IMAGE[] = "<IMG ALT=\"xx-P\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/dead.gif\">";
78$SMILEY_TEXT[] = "|-.";
79$SMILEY_IMAGE[] = "<IMG ALT=\"|-.\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/sleepy.gif\">";
80$SMILEY_TEXT[] = "}-]";
81$SMILEY_IMAGE[] = "<IMG ALT=\"}-]\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/evil.gif\">";
0095d5cd 82
3fe3851d 83$JAVASCRIPT_TAGS = array("javascript:", "onclick=", "ondblclick=", "onkeydown=", "onkeypress=", "onkeyup=",
84 "onmouseover=", "onmouseout=", "onmousedown=", "onmouseup=",
85 "onblur=", "onfocus=", "onload=", "onselect=");
86
6dd38d46 87$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><sup><sub><address><cite><blockquote><pre><strike>";
3fe3851d 88
89
0095d5cd 90/// Functions
91
8553b700 92function s($var) {
c1d57101 93/// returns $var with HTML characters (like "<", ">", etc.) properly quoted,
f9903ed0 94
b9b8ab69 95 return htmlSpecialChars(stripslashes($var));
f9903ed0 96}
97
98function p($var) {
c1d57101 99/// prints $var with HTML characters (like "<", ">", etc.) properly quoted,
f9903ed0 100
b9b8ab69 101 echo htmlSpecialChars(stripslashes($var));
f9903ed0 102}
103
8553b700 104function nvl(&$var, $default="") {
c1d57101 105/// if $var is undefined, return $default, otherwise return $var
8553b700 106
107 return isset($var) ? $var : $default;
108}
f9903ed0 109
110function strip_querystring($url) {
c1d57101 111/// takes a URL and returns it without the querystring portion
f9903ed0 112
b9b8ab69 113 if ($commapos = strpos($url, '?')) {
114 return substr($url, 0, $commapos);
115 } else {
116 return $url;
117 }
f9903ed0 118}
119
120function get_referer() {
c1d57101 121/// returns the URL of the HTTP_REFERER, less the querystring portion
f9903ed0 122
607809b3 123 return strip_querystring(nvl($_SERVER["HTTP_REFERER"]));
f9903ed0 124}
125
c1d57101 126
f9903ed0 127function me() {
c1d57101 128/// returns the name of the current script, WITH the querystring portion.
129/// this function is necessary because PHP_SELF and REQUEST_URI and PATH_INFO
130/// return different things depending on a lot of things like your OS, Web
131/// server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.)
f9903ed0 132
607809b3 133 if (!empty($_SERVER["REQUEST_URI"])) {
134 return $_SERVER["REQUEST_URI"];
c1d57101 135
607809b3 136 } else if (!empty($_SERVER["PATH_INFO"])) {
137 return $_SERVER["PATH_INFO"];
c1d57101 138
607809b3 139 } else if (!empty($_SERVER["PHP_SELF"])) {
140 return $_SERVER["PHP_SELF"];
c1d57101 141
b9b8ab69 142 } else {
39e018b3 143 notify("Warning: Could not find any of these web server variables: \$REQUEST_URI, \$PATH_INFO or \$PHP_SELF");
7fbd6b1c 144 }
f9903ed0 145}
146
147
f9903ed0 148function qualified_me() {
c1d57101 149/// like me() but returns a full URL
f9903ed0 150
39e018b3 151 if (!empty($_SERVER["HTTP_HOST"])) {
152 $hostname = $_SERVER["HTTP_HOST"];
153 } else if (!empty($_ENV["HTTP_HOST"])) {
154 $hostname = $_ENV["HTTP_HOST"];
155 } else if (!empty($_ENV["SERVER_NAME"])) {
156 $hostname = $_ENV["SERVER_NAME"];
157 } else {
158 notify("Warning: could not find the name of this server!");
c1d57101 159 }
f9903ed0 160
607809b3 161 $protocol = (isset($_SERVER["HTTPS"]) and $_SERVER["HTTPS"] == "on") ? "https://" : "http://";
39e018b3 162 $url_prefix = $protocol.$hostname;
b9b8ab69 163 return $url_prefix . me();
f9903ed0 164}
165
166
167function match_referer($good_referer = "") {
c1d57101 168/// returns true if the referer is the same as the good_referer. If
87a2fa03 169/// good_referer is not specified, use qualified_me as the good_referer
60f18531 170 global $CFG;
171
ce78926d 172 if (!empty($CFG->buggy_referer)) {
60f18531 173 return true;
174 }
f9903ed0 175
ce78926d 176 if (empty($good_referer)) {
c1d57101 177 $good_referer = qualified_me();
178 }
b9b8ab69 179 return $good_referer == get_referer();
f9903ed0 180}
181
36b4f985 182function data_submitted($url="") {
183/// Used on most forms in Moodle to check for data
184/// Returns the data as an object, if it's found.
607809b3 185/// This object can be used in foreach loops without
186/// casting because it's cast to (array) automatically
36b4f985 187///
188/// Checks that submitted POST data exists, and also
189/// checks the referer against the given url (it uses
190/// the current page if none was specified.
191
607809b3 192 if (empty($_POST)) {
36b4f985 193 return false;
607809b3 194
36b4f985 195 } else {
196 if (match_referer($url)) {
607809b3 197 return (object)$_POST;
36b4f985 198 } else {
199 if ($CFG->debug > 10) {
200 notice("The form did not come from this page! (referer = ".get_referer().")");
201 }
202 return false;
203 }
204 }
205}
206
f9903ed0 207
3fe3851d 208function stri_replace($find, $replace, $string ) {
c1d57101 209/// This does a search and replace, ignoring case
210/// This function is only here because one doesn't exist yet in PHP
211/// Unlike str_replace(), this only works on single values (not arrays)
3fe3851d 212
213 $parts = explode(strtolower($find), strtolower($string));
214
215 $pos = 0;
216
217 foreach ($parts as $key => $part) {
218 $parts[$key] = substr($string, $pos, strlen($part));
219 $pos += strlen($part) + strlen($find);
220 }
221
222 return (join($replace, $parts));
223}
224
f9903ed0 225function read_template($filename, &$var) {
c1d57101 226/// return a (big) string containing the contents of a template file with all
227/// the variables interpolated. all the variables must be in the $var[] array or
228/// object (whatever you decide to use).
229///
230/// WARNING: do not use this on big files!!
f9903ed0 231
b9b8ab69 232 $temp = str_replace("\\", "\\\\", implode(file($filename), ""));
233 $temp = str_replace('"', '\"', $temp);
234 eval("\$template = \"$temp\";");
235 return $template;
f9903ed0 236}
237
238function checked(&$var, $set_value = 1, $unset_value = 0) {
c1d57101 239/// if variable is set, set it to the set_value otherwise set it to the
240/// unset_value. used to handle checkboxes when you are expecting them from
241/// a form
f9903ed0 242
b9b8ab69 243 if (empty($var)) {
244 $var = $unset_value;
245 } else {
246 $var = $set_value;
247 }
f9903ed0 248}
249
250function frmchecked(&$var, $true_value = "checked", $false_value = "") {
c1d57101 251/// prints the word "checked" if a variable is true, otherwise prints nothing,
252/// used for printing the word "checked" in a checkbox form input
f9903ed0 253
b9b8ab69 254 if ($var) {
255 echo $true_value;
256 } else {
257 echo $false_value;
258 }
f9903ed0 259}
260
261
65cf9fc3 262function link_to_popup_window ($url, $name="popup", $linkname="click here", $height=400, $width=500, $title="Popup window") {
c1d57101 263/// This will create a HTML link that will work on both
264/// Javascript and non-javascript browsers.
265/// Relies on the Javascript function openpopup in javascript.php
266/// $url must be relative to home page eg /mod/survey/stuff.php
f9903ed0 267
ff80e012 268 global $CFG;
269
b9b8ab69 270 echo "\n<SCRIPT language=\"Javascript\">";
f9903ed0 271 echo "\n<!--";
b9b8ab69 272 echo "\ndocument.write('<A TITLE=\"$title\" HREF=javascript:openpopup(\"$url\",\"$name\",\"$height\",\"$width\") >$linkname</A>');";
f9903ed0 273 echo "\n//-->";
b9b8ab69 274 echo "\n</SCRIPT>";
275 echo "\n<NOSCRIPT>\n<A TARGET=\"$name\" TITLE=\"$title\" HREF=\"$CFG->wwwroot/$url\">$linkname</A>\n</NOSCRIPT>\n";
f9903ed0 276
277}
278
279function close_window_button() {
c1d57101 280/// Prints a simple button to close a window
281
f9903ed0 282 echo "<FORM><CENTER>";
e5dfd0f3 283 echo "<INPUT TYPE=button onClick=\"self.close();\" VALUE=\"".get_string("closewindow")."\">";
f9903ed0 284 echo "</CENTER></FORM>";
285}
286
287
08056730 288function choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", $nothingvalue="0", $return=false) {
c1d57101 289/// Given an array of value, creates a popup menu to be part of a form
290/// $options["value"]["label"]
f9903ed0 291
618b22c5 292 if ($nothing == "choose") {
293 $nothing = get_string("choose")."...";
294 }
295
f9903ed0 296 if ($script) {
297 $javascript = "onChange=\"$script\"";
9c9f7d77 298 } else {
299 $javascript = "";
f9903ed0 300 }
9c9f7d77 301
08056730 302 $output = "<SELECT NAME=$name $javascript>\n";
bda8d43a 303 if ($nothing) {
08056730 304 $output .= " <OPTION VALUE=\"$nothingvalue\"\n";
bda8d43a 305 if ($nothingvalue == $selected) {
08056730 306 $output .= " SELECTED";
bda8d43a 307 }
08056730 308 $output .= ">$nothing</OPTION>\n";
873960de 309 }
607809b3 310 if (!empty($options)) {
311 foreach ($options as $value => $label) {
312 $output .= " <OPTION VALUE=\"$value\"";
313 if ($value == $selected) {
314 $output .= " SELECTED";
315 }
316 if ($label) {
317 $output .= ">$label</OPTION>\n";
318 } else {
319 $output .= ">$value</OPTION>\n";
320 }
f9903ed0 321 }
322 }
08056730 323 $output .= "</SELECT>\n";
324
325 if ($return) {
326 return $output;
327 } else {
328 echo $output;
329 }
f9903ed0 330}
331
d897cae4 332function popup_form ($common, $options, $formname, $selected="", $nothing="choose", $help="", $helptext="", $return=false) {
c1d57101 333/// Implements a complete little popup form
334/// $common = the URL up to the point of the variable that changes
335/// $options = A list of value-label pairs for the popup list
336/// $formname = name must be unique on the page
337/// $selected = the option that is already selected
338/// $nothing = The label for the "no choice" option
e5dfd0f3 339/// $help = The name of a help page if help is required
340/// $helptext = The name of the label for the help button
f9903ed0 341
618b22c5 342 if ($nothing == "choose") {
343 $nothing = get_string("choose")."...";
344 }
345
3e50a139 346 $output = "<FORM TARGET=_top NAME=$formname>";
347 $output .= "<SELECT NAME=popup onChange=\"top.location=document.$formname.popup.options[document.$formname.popup.selectedIndex].value\">\n";
f9903ed0 348
349 if ($nothing != "") {
d897cae4 350 $output .= " <OPTION VALUE=\"javascript:void(0)\">$nothing</OPTION>\n";
f9903ed0 351 }
352
353 foreach ($options as $value => $label) {
d897cae4 354 if (substr($label,0,1) == "-") {
355 $output .= " <OPTION VALUE=\"\"";
356 } else {
357 $output .= " <OPTION VALUE=\"$common$value\"";
358 if ($value == $selected) {
359 $output .= " SELECTED";
360 }
f9903ed0 361 }
362 if ($label) {
d897cae4 363 $output .= ">$label</OPTION>\n";
f9903ed0 364 } else {
d897cae4 365 $output .= ">$value</OPTION>\n";
f9903ed0 366 }
367 }
d897cae4 368 $output .= "</SELECT>";
d897cae4 369 $output .= "</FORM>\n";
370
371 if ($return) {
372 return $output;
373 } else {
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
400
401function get_slash_arguments($i=0) {
c1d57101 402/// Extracts arguments from "/foo/bar/something"
403/// eg http://mysite.com/script.php/foo/bar/something
404/// Might only work on Apache
f9903ed0 405
406 global $PATH_INFO;
407
408 if (!isset($PATH_INFO)) {
409 return false;
410 }
411
780db230 412 if (strpos($PATH_INFO, "..")) { // check for parent URLs
413 return false;
414 }
415 if (strpos($PATH_INFO, "|")) { // check for pipes
416 return false;
417 }
418 if (strpos($PATH_INFO, "`")) { // check for backquotes
e2d89725 419 return false;
420 }
421
f9903ed0 422 $args = explode("/", $PATH_INFO);
423
424 if ($i) { // return just the required argument
425 return $args[$i];
426
427 } else { // return the whole array
428 array_shift($args); // get rid of the empty first one
429 return $args;
430 }
431}
432
0095d5cd 433function format_text_menu() {
c1d57101 434/// Just returns an array of formats suitable for a popup menu
0095d5cd 435 return array (FORMAT_MOODLE => get_string("formattext"),
436 FORMAT_HTML => get_string("formathtml") );
437}
438
60f18531 439function format_text($text, $format=FORMAT_MOODLE, $options=NULL) {
c1d57101 440/// Given text in a variety of format codings, this function returns
441/// the text as safe HTML.
442///
443/// $text is raw text (originally from a user)
444/// $format is one of the format constants, defined above
0095d5cd 445
446 switch ($format) {
447 case FORMAT_MOODLE:
c9dda990 448 if (!isset($options->smiley)) {
449 $options->smiley=true;
450 }
451 if (!isset($options->para)) {
1a072208 452 $options->para=true;
c9dda990 453 }
0095d5cd 454 return text_to_html($text, $options->smiley, $options->para);
455 break;
f9903ed0 456
0095d5cd 457 case FORMAT_HTML:
1a072208 458 $text = replace_smilies($text);
3fe3851d 459 return $text;
0095d5cd 460 break;
461 }
462}
463
464
465function clean_text($text, $format) {
c1d57101 466/// Given raw text (eg typed in by a user), this function cleans it up
467/// and removes any nasty tags that could mess up Moodle pages.
b7a3cf49 468
3fe3851d 469 global $JAVASCRIPT_TAGS, $ALLOWED_TAGS;
470
471 switch ($format) { // Does the same thing, currently, but it's nice to have the option
0095d5cd 472 case FORMAT_MOODLE:
3fe3851d 473 $text = strip_tags($text, $ALLOWED_TAGS);
474 foreach ($JAVASCRIPT_TAGS as $tag) {
475 $text = stri_replace($tag, "", $text);
476 }
477 return $text;
0095d5cd 478
479 case FORMAT_HTML:
3fe3851d 480 $text = strip_tags($text, $ALLOWED_TAGS);
481 foreach ($JAVASCRIPT_TAGS as $tag) {
482 $text = stri_replace($tag, "", $text);
483 }
484 return $text;
0095d5cd 485 }
b7a3cf49 486}
f9903ed0 487
1a072208 488function replace_smilies($text) {
c1d57101 489/// Replaces all known smileys in the text with image equivalents
490
1a072208 491 global $CFG, $SMILEY_TEXT, $SMILEY_IMAGE;
b7a3cf49 492
1a072208 493 return str_replace($SMILEY_TEXT, $SMILEY_IMAGE, $text);
494}
0095d5cd 495
909f539d 496function text_to_html($text, $smiley=true, $para=true) {
c1d57101 497/// Given plain text, makes it into HTML as nicely as possible.
498/// May contain HTML tags already
f9903ed0 499
c1d57101 500/// Remove any whitespace that may be between HTML tags
7b3be1b1 501 $text = eregi_replace(">([[:space:]]+)<", "><", $text);
502
c1d57101 503/// Remove any returns that precede or follow HTML tags
0eae8049 504 $text = eregi_replace("([\n\r])<", " <", $text);
505 $text = eregi_replace(">([\n\r])", "> ", $text);
7b3be1b1 506
c1d57101 507/// Make lone URLs into links. eg http://moodle.com/
0be05df0 508 $text = eregi_replace("([\n\r ([])([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
3fe3851d 509 "\\1<A HREF=\"\\2://\\3\\4\" TARGET=\"newpage\">\\2://\\3\\4</A>", $text);
f9903ed0 510
c1d57101 511/// eg www.moodle.com
e1bf736f 512 $text = eregi_replace("([[:space:]])www\.([^[:space:]]*)([[:alnum:]#?/&=])",
f9903ed0 513 "\\1<A HREF=\"http://www.\\2\\3\" TARGET=\"newpage\">www.\\2\\3</A>", $text);
514
c1d57101 515/// Make returns into HTML newlines.
f9903ed0 516 $text = nl2br($text);
517
c1d57101 518/// Turn smileys into images.
d69cb7f4 519 if ($smiley) {
1a072208 520 $text = replace_smilies($text);
d69cb7f4 521 }
f9903ed0 522
c1d57101 523/// Wrap the whole thing in a paragraph tag if required
909f539d 524 if ($para) {
525 return "<P>".$text."</P>";
526 } else {
527 return $text;
528 }
f9903ed0 529}
530
5af78ed2 531function highlight($needle, $haystack) {
c1d57101 532/// This function will highlight instances of $needle in $haystack
5af78ed2 533
534 $parts = explode(strtolower($needle), strtolower($haystack));
535
536 $pos = 0;
537
538 foreach ($parts as $key => $part) {
539 $parts[$key] = substr($haystack, $pos, strlen($part));
540 $pos += strlen($part);
541
542 $parts[$key] .= "<SPAN CLASS=highlight>".substr($haystack, $pos, strlen($needle))."</SPAN>";
543 $pos += strlen($needle);
544 }
545
546 return (join('', $parts));
547}
548
f9903ed0 549
9fa49e22 550
551/// STANDARD WEB PAGE PARTS ///////////////////////////////////////////////////
552
553function print_header ($title="", $heading="", $navigation="", $focus="", $meta="", $cache=true, $button="&nbsp;", $menu="") {
554// $title - appears top of window
555// $heading - appears top of page
556// $navigation - premade navigation string
557// $focus - indicates form element eg inputform.password
558// $meta - meta tags in the header
559// $cache - should this page be cacheable?
560// $button - HTML code for a button (usually for module editing)
561// $menu - HTML code for a popup menu
562 global $USER, $CFG, $THEME;
563
564 if (file_exists("$CFG->dirroot/theme/$CFG->theme/styles.php")) {
565 $styles = $CFG->stylesheet;
566 } else {
567 $styles = "$CFG->wwwroot/theme/standard/styles.php";
568 }
569
570 if ($navigation == "home") {
571 $home = true;
572 $navigation = "";
9d378732 573 } else {
574 $home = false;
9fa49e22 575 }
576
577 if ($button == "") {
578 $button = "&nbsp;";
579 }
580
581 if (!$menu and $navigation) {
582 if (isset($USER->id)) {
583 $menu = "<FONT SIZE=2><A TARGET=_parent HREF=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</A></FONT>";
584 } else {
585 $menu = "<FONT SIZE=2><A TARGET=_parent HREF=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</A></FONT>";
586 }
587 }
588
589 // Specify character set ... default is iso-8859-1 but some languages might need something else
590 // Could be optimised by carrying the charset variable around in $USER
591 if (current_language() == "en") {
592 $meta = "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=iso-8859-1\">\n$meta\n";
593 } else {
594 $meta = "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=".get_string("thischarset")."\">\n$meta\n";
595 }
596
597 if ($CFG->langdir == "RTL") {
598 $direction = " DIR=\"RTL\"";
599 } else {
600 $direction = " DIR=\"LTR\"";
601 }
602
603 if (!$cache) { // Do everything we can to prevent clients and proxies caching
604 @header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
605 @header("Pragma: no-cache");
606 $meta .= "\n<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">";
607 $meta .= "\n<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">";
608 }
609
610 include ("$CFG->dirroot/theme/$CFG->theme/header.html");
611}
612
613function print_footer ($course=NULL) {
614// Can provide a course object to make the footer contain a link to
615// to the course home page, otherwise the link will go to the site home
616 global $USER, $CFG, $THEME;
617
618
619/// Course links
620 if ($course) {
621 if ($course == "home") { // special case for site home page - please do not remove
622 $homelink = "<P ALIGN=center><A TITLE=\"Moodle $CFG->release ($CFG->version)\" HREF=\"http://moodle.com/\">";
623 $homelink .= "<BR><IMG WIDTH=130 HEIGHT=19 SRC=\"pix/madewithmoodle2.gif\" BORDER=0></A></P>";
624 $course = get_site();
625 $homepage = true;
626 } else {
627 $homelink = "<A TARGET=_top HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>";
628 }
629 } else {
630 $homelink = "<A TARGET=_top HREF=\"$CFG->wwwroot\">".get_string("home")."</A>";
631 $course = get_site();
632 }
633
634/// User links
9d378732 635 if (isset($USER->realuser)) {
9fa49e22 636 if ($realuser = get_record("user", "id", $USER->realuser)) {
637 $realuserinfo = " [<A HREF=\"$CFG->wwwroot/course/loginas.php?id=$course->id&return=$realuser->id\">$realuser->firstname $realuser->lastname</A>] ";
638 }
9d378732 639 } else {
640 $realuserinfo = "";
9fa49e22 641 }
642
9c9f7d77 643 if (isset($USER->id) and $USER->id) {
9fa49e22 644 $username = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id\">$USER->firstname $USER->lastname</A>";
645 $loggedinas = $realuserinfo.get_string("loggedinas", "moodle", "$username").
646 " (<A HREF=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</A>)";
647 } else {
648 $loggedinas = get_string("loggedinnot", "moodle").
649 " (<A HREF=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</A>)";
650 }
651
652 include ("$CFG->dirroot/theme/$CFG->theme/footer.html");
653}
654
655
656
657function print_navigation ($navigation) {
658 global $CFG;
659
660 if ($navigation) {
661 if (! $site = get_site()) {
662 $site->shortname = get_string("home");;
663 }
664 echo "<A TARGET=_top HREF=\"$CFG->wwwroot/\">$site->shortname</A> -> $navigation";
665 }
666}
667
668function print_heading($text, $align="CENTER", $size=3) {
669 echo "<P ALIGN=\"$align\"><FONT SIZE=\"$size\"><B>".stripslashes($text)."</B></FONT></P>";
670}
671
672function print_heading_with_help($text, $helppage, $module="moodle") {
673// Centered heading with attached help button (same title text)
674 echo "<P ALIGN=\"CENTER\"><FONT SIZE=\"3\"><B>".stripslashes($text);
675 helpbutton($helppage, $text, $module);
676 echo "</B></FONT></P>";
677}
678
679function print_continue($link) {
9fa49e22 680
681 if (!$link) {
607809b3 682 $link = $_SERVER["HTTP_REFERER"];
9fa49e22 683 }
684
685 print_heading("<A HREF=\"$link\">".get_string("continue")."</A>");
686}
687
688
689function print_simple_box($message, $align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
690 print_simple_box_start($align, $width, $color, $padding, $class);
691 echo stripslashes($message);
692 print_simple_box_end();
693}
694
695function print_simple_box_start($align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
696 global $THEME;
697
698 if ($align) {
9d378732 699 $align = "ALIGN=\"$align\"";
9fa49e22 700 }
701 if ($width) {
9d378732 702 $width = "WIDTH=\"$width\"";
9fa49e22 703 }
9d378732 704 echo "<table $align $width class=\"$class\" border=\"0\" cellpadding=\"$padding\" cellspacing=\"0\"><tr><td bgcolor=\"$color\" class=\"$class"."content\">";
9fa49e22 705}
706
707function print_simple_box_end() {
708 echo "</td></tr></table>";
709}
710
711function print_single_button($link, $options, $label="OK") {
712 echo "<FORM ACTION=\"$link\" METHOD=GET>";
713 if ($options) {
714 foreach ($options as $name => $value) {
715 echo "<INPUT TYPE=hidden NAME=\"$name\" VALUE=\"$value\">";
716 }
717 }
718 echo "<INPUT TYPE=submit VALUE=\"$label\"></FORM>";
719}
720
721function print_spacer($height=1, $width=1, $br=true) {
722 global $CFG;
723 echo "<IMG HEIGHT=\"$height\" WIDTH=\"$width\" SRC=\"$CFG->wwwroot/pix/spacer.gif\" ALT=\"\">";
724 if ($br) {
725 echo "<BR \>\n";
726 }
727}
728
729function print_file_picture($path, $courseid=0, $height="", $width="", $link="") {
730// Given the path to a picture file in a course, or a URL,
731// this function includes the picture in the page.
732 global $CFG;
733
734 if ($height) {
735 $height = "HEIGHT=\"$height\"";
736 }
737 if ($width) {
738 $width = "WIDTH=\"$width\"";
739 }
740 if ($link) {
741 echo "<A HREF=\"$link\">";
742 }
743 if (substr(strtolower($path), 0, 7) == "http://") {
744 echo "<IMG BORDER=0 $height $width SRC=\"$path\">";
745
746 } else if ($courseid) {
747 echo "<IMG BORDER=0 $height $width SRC=\"";
748 if ($CFG->slasharguments) { // Use this method if possible for better caching
749 echo "$CFG->wwwroot/file.php/$courseid/$path";
750 } else {
751 echo "$CFG->wwwroot/file.php?file=$courseid/$path";
752 }
753 echo "\">";
754 } else {
755 echo "Error: must pass URL or course";
756 }
757 if ($link) {
758 echo "</A>";
759 }
760}
761
762function print_user_picture($userid, $courseid, $picture, $large=false, $returnstring=false, $link=true) {
763 global $CFG;
764
765 if ($link) {
766 $output = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$userid&course=$courseid\">";
767 } else {
768 $output = "";
769 }
770 if ($large) {
771 $file = "f1.jpg";
772 $size = 100;
773 } else {
774 $file = "f2.jpg";
775 $size = 35;
776 }
777 if ($picture) {
778 if ($CFG->slasharguments) { // Use this method if possible for better caching
779 $output .= "<IMG SRC=\"$CFG->wwwroot/user/pix.php/$userid/$file\" BORDER=0 WIDTH=$size HEIGHT=$size ALT=\"\">";
780 } else {
781 $output .= "<IMG SRC=\"$CFG->wwwroot/user/pix.php?file=/$userid/$file\" BORDER=0 WIDTH=$size HEIGHT=$size ALT=\"\">";
782 }
783 } else {
784 $output .= "<IMG SRC=\"$CFG->wwwroot/user/default/$file\" BORDER=0 WIDTH=$size HEIGHT=$size ALT=\"\">";
785 }
786 if ($link) {
787 $output .= "</A>";
788 }
789
790 if ($returnstring) {
791 return $output;
792 } else {
793 echo $output;
794 }
795}
796
797function print_table($table) {
798// Prints a nicely formatted table.
799// $table is an object with several properties.
800// $table->head is an array of heading names.
801// $table->align is an array of column alignments
802// $table->size is an array of column sizes
803// $table->data[] is an array of arrays containing the data.
804// $table->width is an percentage of the page
805// $table->cellpadding padding on each cell
806// $table->cellspacing spacing between cells
807
808 if (isset($table->align)) {
809 foreach ($table->align as $key => $aa) {
810 if ($aa) {
811 $align[$key] = " ALIGN=\"$aa\"";
812 } else {
813 $align[$key] = "";
814 }
815 }
816 }
817 if (isset($table->size)) {
818 foreach ($table->size as $key => $ss) {
819 if ($ss) {
820 $size[$key] = " WIDTH=\"$ss\"";
821 } else {
822 $size[$key] = "";
823 }
824 }
825 }
826
9d378732 827 if (empty($table->width)) {
9fa49e22 828 $table->width = "80%";
829 }
830
9d378732 831 if (empty($table->cellpadding)) {
9fa49e22 832 $table->cellpadding = "5";
833 }
834
9d378732 835 if (empty($table->cellspacing)) {
9fa49e22 836 $table->cellspacing = "1";
837 }
838
839 print_simple_box_start("CENTER", "$table->width", "#FFFFFF", 0);
840 echo "<TABLE WIDTH=100% BORDER=0 valign=top align=center ";
841 echo " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"generaltable\">\n";
842
b79f41cd 843 if (!empty($table->head)) {
9fa49e22 844 echo "<TR>";
845 foreach ($table->head as $key => $heading) {
9d378732 846 if (!isset($size[$key])) {
847 $size[$key] = "";
848 }
849 if (!isset($align[$key])) {
850 $align[$key] = "";
851 }
9fa49e22 852 echo "<TH VALIGN=top ".$align[$key].$size[$key]." NOWRAP class=\"generaltableheader\">$heading</TH>";
853 }
854 echo "</TR>\n";
855 }
856
857 foreach ($table->data as $row) {
858 echo "<TR VALIGN=TOP>";
859 foreach ($row as $key => $item) {
9d378732 860 if (!isset($size[$key])) {
861 $size[$key] = "";
862 }
863 if (!isset($align[$key])) {
864 $align[$key] = "";
865 }
9fa49e22 866 echo "<TD ".$align[$key].$size[$key]." class=\"generaltablecell\">$item</TD>";
867 }
868 echo "</TR>\n";
869 }
870 echo "</TABLE>\n";
871 print_simple_box_end();
872
873 return true;
874}
875
876function print_editing_switch($courseid) {
877 global $CFG, $USER;
878
879 if (isteacher($courseid)) {
880 if ($USER->editing) {
881 echo "<A HREF=\"$CFG->wwwroot/course/view.php?id=$courseid&edit=off\">Turn editing off</A>";
882 } else {
883 echo "<A HREF=\"$CFG->wwwroot/course/view.php?id=$courseid&edit=on\">Turn editing on</A>";
884 }
885 }
886}
887
888function print_textarea($richedit, $rows, $cols, $width, $height, $name, $value="") {
889 global $CFG, $THEME;
890
891 if ($richedit) {
892 echo "<object id=richedit style=\"BACKGROUND-COLOR: buttonface\"";
893 echo " data=\"$CFG->wwwroot/lib/rte/richedit.html\"";
894 echo " width=\"$width\" height=\"$height\" ";
895 echo " type=\"text/x-scriptlet\" VIEWASTEXT></object>\n";
896 echo "<TEXTAREA style=\"display:none\" NAME=\"$name\" ROWS=1 COLS=1>";
897 p($value);
898 echo "</TEXTAREA>\n";
899 } else {
900 echo "<TEXTAREA name=\"$name\" rows=\"$rows\" cols=\"$cols\" wrap=virtual>";
901 p($value);
902 echo "</TEXTAREA>\n";
903 }
904}
905
906function print_richedit_javascript($form, $name, $source="no") {
907 echo "<SCRIPT language=\"JavaScript\" event=\"onload\" for=\"window\">\n";
908 echo " document.richedit.options = \"history=no;source=$source\";";
909 echo " document.richedit.docHtml = $form.$name.innerText;";
910 echo "</SCRIPT>";
911}
912
913
914function update_course_icon($courseid) {
915// Used to be an icon, but it's now a simple form button
916 global $CFG, $USER;
917
918 if (isteacher($courseid)) {
9c9f7d77 919 if (!empty($USER->editing)) {
9fa49e22 920 $string = get_string("turneditingoff");
921 $edit = "off";
922 } else {
923 $string = get_string("turneditingon");
924 $edit = "on";
925 }
926 return "<FORM TARGET=_parent METHOD=GET ACTION=\"$CFG->wwwroot/course/view.php\">".
927 "<INPUT TYPE=hidden NAME=id VALUE=\"$courseid\">".
928 "<INPUT TYPE=hidden NAME=edit VALUE=\"$edit\">".
929 "<INPUT TYPE=submit VALUE=\"$string\"></FORM>";
930 }
931}
932
933function update_module_button($moduleid, $courseid, $string) {
934// Prints the editing button on a module "view" page
935 global $CFG;
936
937 if (isteacher($courseid)) {
938 $string = get_string("updatethis", "", $string);
939 return "<FORM TARGET=_parent METHOD=GET ACTION=\"$CFG->wwwroot/course/mod.php\">".
940 "<INPUT TYPE=hidden NAME=update VALUE=\"$moduleid\">".
941 "<INPUT TYPE=hidden NAME=return VALUE=\"true\">".
942 "<INPUT TYPE=submit VALUE=\"$string\"></FORM>";
943 }
944}
945
946
947function navmenu($course, $cm=NULL) {
948// Given a course and a (current) coursemodule
949// This function returns a small popup menu with all the
950// course activity modules in it, as a navigation menu
951// The data is taken from the serialised array stored in
952// the course record
953
954 global $CFG;
955
956 if ($cm) {
957 $cm = $cm->id;
958 }
959
960 if ($course->format == 'weeks') {
961 $strsection = get_string("week");
962 } else {
963 $strsection = get_string("topic");
964 }
965
966 if (!$modinfo = unserialize($course->modinfo)) {
967 return "";
968 }
969 $section = -1;
970 $selected = "";
971 foreach ($modinfo as $mod) {
972 if ($mod->section > 0 and $section <> $mod->section) {
973 $menu[] = "-------------- $strsection $mod->section --------------";
974 }
975 $section = $mod->section;
976 $url = "$mod->mod/view.php?id=$mod->cm";
977 if ($cm == $mod->cm) {
978 $selected = $url;
979 }
980 $mod->name = urldecode($mod->name);
981 if (strlen($mod->name) > 55) {
982 $mod->name = substr($mod->name, 0, 50)."...";
983 }
984 $menu[$url] = $mod->name;
985 }
986
987 return popup_form("$CFG->wwwroot/mod/", $menu, "navmenu", $selected, get_string("jumpto"), "", "", true);
988}
989
990
991
992function print_date_selector($day, $month, $year, $currenttime=0) {
993// Currenttime is a default timestamp in GMT
994// Prints form items with the names $day, $month and $year
995
996 if (!$currenttime) {
997 $currenttime = time();
998 }
999 $currentdate = usergetdate($currenttime);
1000
1001 for ($i=1; $i<=31; $i++) {
1002 $days[$i] = "$i";
1003 }
1004 for ($i=1; $i<=12; $i++) {
39e018b3 1005 $months[$i] = userdate(gmmktime(12,0,0,$i,1,2000), "%B");
9fa49e22 1006 }
1007 for ($i=2000; $i<=2010; $i++) {
1008 $years[$i] = $i;
1009 }
47f1da80 1010 choose_from_menu($days, $day, $currentdate['mday'], "");
1011 choose_from_menu($months, $month, $currentdate['mon'], "");
1012 choose_from_menu($years, $year, $currentdate['year'], "");
9fa49e22 1013}
1014
1015function print_time_selector($hour, $minute, $currenttime=0) {
1016// Currenttime is a default timestamp in GMT
1017// Prints form items with the names $hour and $minute
1018
1019 if (!$currenttime) {
1020 $currenttime = time();
1021 }
1022 $currentdate = usergetdate($currenttime);
1023 for ($i=0; $i<=23; $i++) {
1024 $hours[$i] = sprintf("%02d",$i);
1025 }
1026 for ($i=0; $i<=59; $i++) {
1027 $minutes[$i] = sprintf("%02d",$i);
1028 }
47f1da80 1029 choose_from_menu($hours, $hour, $currentdate['hours'], "");
1030 choose_from_menu($minutes, $minute, $currentdate['minutes'], "");
9fa49e22 1031}
1032
1033function error ($message, $link="") {
1034 global $CFG, $SESSION;
1035
1036 print_header(get_string("error"));
1037 echo "<BR>";
1038 print_simple_box($message, "center", "", "#FFBBBB");
1039
1040 if (!$link) {
1041 if ( !empty($SESSION->fromurl) ) {
1042 $link = "$SESSION->fromurl";
1043 unset($SESSION->fromurl);
1044 save_session("SESSION");
1045 } else {
1046 $link = "$CFG->wwwroot";
1047 }
1048 }
1049 print_continue($link);
1050 print_footer();
1051 die;
1052}
1053
1054function helpbutton ($page, $title="", $module="moodle", $image=true, $linktext=false, $text="") {
1055 // $page = the keyword that defines a help page
1056 // $title = the title of links, rollover tips, alt tags etc
1057 // $module = which module is the page defined in
1058 // $image = use a help image for the link? (true/false/"both")
1059 // $text = if defined then this text is used in the page, and
1060 // the $page variable is ignored.
1061 global $CFG;
1062
1063 if ($module == "") {
1064 $module = "moodle";
1065 }
1066
1067 if ($image) {
1068 if ($linktext) {
1069 $linkobject = "$title<IMG align=\"absmiddle\" BORDER=0 HEIGHT=17 WIDTH=22 ALT=\"\" SRC=\"$CFG->wwwroot/pix/help.gif\">";
1070 } else {
1071 $linkobject = "<IMG align=\"absmiddle\" BORDER=0 HEIGHT=17 WIDTH=22 ALT=\"$title\" SRC=\"$CFG->wwwroot/pix/help.gif\">";
1072 }
1073 } else {
1074 $linkobject = $title;
1075 }
1076 if ($text) {
1077 $url = "/help.php?module=$module&text=".htmlentities(urlencode($text));
1078 } else {
1079 $url = "/help.php?module=$module&file=$page.html";
1080 }
1081 link_to_popup_window ($url, "popup", $linkobject, 400, 500, $title);
1082}
1083
1084function notice ($message, $link="") {
607809b3 1085 global $THEME;
9fa49e22 1086
1087 if (!$link) {
607809b3 1088 $link = $_SERVER["HTTP_REFERER"];
9fa49e22 1089 }
1090
1091 echo "<BR>";
1092 print_simple_box($message, "center", "", "$THEME->cellheading");
1093 print_heading("<A HREF=\"$link\">".get_string("continue")."</A>");
1094 print_footer(get_site());
1095 die;
1096}
1097
1098function notice_yesno ($message, $linkyes, $linkno) {
1099 global $THEME;
1100
1101 print_simple_box_start("center", "", "$THEME->cellheading");
1102 echo "<P ALIGN=CENTER><FONT SIZE=3>$message</FONT></P>";
1103 echo "<P ALIGN=CENTER><FONT SIZE=3><B>";
1104 echo "<A HREF=\"$linkyes\">".get_string("yes")."</A>";
1105 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
1106 echo "<A HREF=\"$linkno\">".get_string("no")."</A>";
1107 echo "</B></FONT></P>";
1108 print_simple_box_end();
1109}
1110
1111function redirect($url, $message="", $delay=0) {
1112// Uses META tags to redirect the user, after printing a notice
1113
1114 echo "<META HTTP-EQUIV='Refresh' CONTENT='$delay; URL=$url'>";
1115
1116 if (!empty($message)) {
1117 print_header();
1118 echo "<CENTER>";
1119 echo "<P>$message</P>";
1120 echo "<P>( <A HREF=\"$url\">".get_string("continue")."</A> )</P>";
1121 echo "</CENTER>";
1122 }
1123 die;
1124}
1125
99988d1a 1126function notify ($message, $color="red", $align="center") {
1127 echo "<p align=\"$align\"><b><font color=\"$color\">$message</font></b></p>\n";
9fa49e22 1128}
1129
1130
f9903ed0 1131?>