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