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