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