Hide password field when password change is not supported by auth-module.
[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
9d378732 656 if (isset($USER->realuser)) {
9fa49e22 657 if ($realuser = get_record("user", "id", $USER->realuser)) {
658 $realuserinfo = " [<A HREF=\"$CFG->wwwroot/course/loginas.php?id=$course->id&return=$realuser->id\">$realuser->firstname $realuser->lastname</A>] ";
659 }
9d378732 660 } else {
661 $realuserinfo = "";
9fa49e22 662 }
663
9c9f7d77 664 if (isset($USER->id) and $USER->id) {
9fa49e22 665 $username = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id\">$USER->firstname $USER->lastname</A>";
666 $loggedinas = $realuserinfo.get_string("loggedinas", "moodle", "$username").
667 " (<A HREF=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</A>)";
668 } else {
669 $loggedinas = get_string("loggedinnot", "moodle").
670 " (<A HREF=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</A>)";
671 }
672
673 include ("$CFG->dirroot/theme/$CFG->theme/footer.html");
674}
675
676
677
678function print_navigation ($navigation) {
679 global $CFG;
680
681 if ($navigation) {
682 if (! $site = get_site()) {
683 $site->shortname = get_string("home");;
684 }
f82c2d42 685 echo "<A TARGET=\"{$CFG->framename}\" HREF=\"$CFG->wwwroot/\">$site->shortname</A> -> $navigation";
9fa49e22 686 }
687}
688
689function print_heading($text, $align="CENTER", $size=3) {
7d8f674d 690 echo "<P ALIGN=\"$align\"><FONT SIZE=\"$size\"><B>".stripslashes_safe($text)."</B></FONT></P>";
9fa49e22 691}
692
693function print_heading_with_help($text, $helppage, $module="moodle") {
694// Centered heading with attached help button (same title text)
7d8f674d 695 echo "<P ALIGN=\"CENTER\"><FONT SIZE=\"3\"><B>".stripslashes_safe($text);
9fa49e22 696 helpbutton($helppage, $text, $module);
697 echo "</B></FONT></P>";
698}
699
700function print_continue($link) {
9fa49e22 701
702 if (!$link) {
607809b3 703 $link = $_SERVER["HTTP_REFERER"];
9fa49e22 704 }
705
706 print_heading("<A HREF=\"$link\">".get_string("continue")."</A>");
707}
708
709
710function print_simple_box($message, $align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
711 print_simple_box_start($align, $width, $color, $padding, $class);
7d8f674d 712 echo stripslashes_safe($message);
9fa49e22 713 print_simple_box_end();
714}
715
716function print_simple_box_start($align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
717 global $THEME;
718
719 if ($align) {
9d378732 720 $align = "ALIGN=\"$align\"";
9fa49e22 721 }
722 if ($width) {
9d378732 723 $width = "WIDTH=\"$width\"";
9fa49e22 724 }
9d378732 725 echo "<table $align $width class=\"$class\" border=\"0\" cellpadding=\"$padding\" cellspacing=\"0\"><tr><td bgcolor=\"$color\" class=\"$class"."content\">";
9fa49e22 726}
727
728function print_simple_box_end() {
729 echo "</td></tr></table>";
730}
731
732function print_single_button($link, $options, $label="OK") {
733 echo "<FORM ACTION=\"$link\" METHOD=GET>";
734 if ($options) {
735 foreach ($options as $name => $value) {
736 echo "<INPUT TYPE=hidden NAME=\"$name\" VALUE=\"$value\">";
737 }
738 }
739 echo "<INPUT TYPE=submit VALUE=\"$label\"></FORM>";
740}
741
742function print_spacer($height=1, $width=1, $br=true) {
743 global $CFG;
744 echo "<IMG HEIGHT=\"$height\" WIDTH=\"$width\" SRC=\"$CFG->wwwroot/pix/spacer.gif\" ALT=\"\">";
745 if ($br) {
746 echo "<BR \>\n";
747 }
748}
749
750function print_file_picture($path, $courseid=0, $height="", $width="", $link="") {
751// Given the path to a picture file in a course, or a URL,
752// this function includes the picture in the page.
753 global $CFG;
754
755 if ($height) {
756 $height = "HEIGHT=\"$height\"";
757 }
758 if ($width) {
759 $width = "WIDTH=\"$width\"";
760 }
761 if ($link) {
762 echo "<A HREF=\"$link\">";
763 }
764 if (substr(strtolower($path), 0, 7) == "http://") {
765 echo "<IMG BORDER=0 $height $width SRC=\"$path\">";
766
767 } else if ($courseid) {
768 echo "<IMG BORDER=0 $height $width SRC=\"";
769 if ($CFG->slasharguments) { // Use this method if possible for better caching
770 echo "$CFG->wwwroot/file.php/$courseid/$path";
771 } else {
3f396065 772 echo "$CFG->wwwroot/file.php?file=/$courseid/$path";
9fa49e22 773 }
774 echo "\">";
775 } else {
776 echo "Error: must pass URL or course";
777 }
778 if ($link) {
779 echo "</A>";
780 }
781}
782
783function print_user_picture($userid, $courseid, $picture, $large=false, $returnstring=false, $link=true) {
784 global $CFG;
785
786 if ($link) {
787 $output = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$userid&course=$courseid\">";
788 } else {
789 $output = "";
790 }
791 if ($large) {
792 $file = "f1.jpg";
793 $size = 100;
794 } else {
795 $file = "f2.jpg";
796 $size = 35;
797 }
798 if ($picture) {
799 if ($CFG->slasharguments) { // Use this method if possible for better caching
800 $output .= "<IMG SRC=\"$CFG->wwwroot/user/pix.php/$userid/$file\" BORDER=0 WIDTH=$size HEIGHT=$size ALT=\"\">";
801 } else {
802 $output .= "<IMG SRC=\"$CFG->wwwroot/user/pix.php?file=/$userid/$file\" BORDER=0 WIDTH=$size HEIGHT=$size ALT=\"\">";
803 }
804 } else {
805 $output .= "<IMG SRC=\"$CFG->wwwroot/user/default/$file\" BORDER=0 WIDTH=$size HEIGHT=$size ALT=\"\">";
806 }
807 if ($link) {
808 $output .= "</A>";
809 }
810
811 if ($returnstring) {
812 return $output;
813 } else {
814 echo $output;
815 }
816}
817
818function print_table($table) {
819// Prints a nicely formatted table.
820// $table is an object with several properties.
821// $table->head is an array of heading names.
822// $table->align is an array of column alignments
823// $table->size is an array of column sizes
824// $table->data[] is an array of arrays containing the data.
825// $table->width is an percentage of the page
826// $table->cellpadding padding on each cell
827// $table->cellspacing spacing between cells
828
829 if (isset($table->align)) {
830 foreach ($table->align as $key => $aa) {
831 if ($aa) {
832 $align[$key] = " ALIGN=\"$aa\"";
833 } else {
834 $align[$key] = "";
835 }
836 }
837 }
838 if (isset($table->size)) {
839 foreach ($table->size as $key => $ss) {
840 if ($ss) {
841 $size[$key] = " WIDTH=\"$ss\"";
842 } else {
843 $size[$key] = "";
844 }
845 }
846 }
847
9d378732 848 if (empty($table->width)) {
9fa49e22 849 $table->width = "80%";
850 }
851
9d378732 852 if (empty($table->cellpadding)) {
9fa49e22 853 $table->cellpadding = "5";
854 }
855
9d378732 856 if (empty($table->cellspacing)) {
9fa49e22 857 $table->cellspacing = "1";
858 }
859
860 print_simple_box_start("CENTER", "$table->width", "#FFFFFF", 0);
861 echo "<TABLE WIDTH=100% BORDER=0 valign=top align=center ";
862 echo " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"generaltable\">\n";
863
b79f41cd 864 if (!empty($table->head)) {
9fa49e22 865 echo "<TR>";
866 foreach ($table->head as $key => $heading) {
9d378732 867 if (!isset($size[$key])) {
868 $size[$key] = "";
869 }
870 if (!isset($align[$key])) {
871 $align[$key] = "";
872 }
9fa49e22 873 echo "<TH VALIGN=top ".$align[$key].$size[$key]." NOWRAP class=\"generaltableheader\">$heading</TH>";
874 }
875 echo "</TR>\n";
876 }
877
878 foreach ($table->data as $row) {
879 echo "<TR VALIGN=TOP>";
880 foreach ($row as $key => $item) {
9d378732 881 if (!isset($size[$key])) {
882 $size[$key] = "";
883 }
884 if (!isset($align[$key])) {
885 $align[$key] = "";
886 }
9fa49e22 887 echo "<TD ".$align[$key].$size[$key]." class=\"generaltablecell\">$item</TD>";
888 }
889 echo "</TR>\n";
890 }
891 echo "</TABLE>\n";
892 print_simple_box_end();
893
894 return true;
895}
896
897function print_editing_switch($courseid) {
898 global $CFG, $USER;
899
900 if (isteacher($courseid)) {
901 if ($USER->editing) {
902 echo "<A HREF=\"$CFG->wwwroot/course/view.php?id=$courseid&edit=off\">Turn editing off</A>";
903 } else {
904 echo "<A HREF=\"$CFG->wwwroot/course/view.php?id=$courseid&edit=on\">Turn editing on</A>";
905 }
906 }
907}
908
909function print_textarea($richedit, $rows, $cols, $width, $height, $name, $value="") {
7d8f674d 910/// Prints a richtext field or a normal textarea
9fa49e22 911 global $CFG, $THEME;
912
913 if ($richedit) {
914 echo "<object id=richedit style=\"BACKGROUND-COLOR: buttonface\"";
915 echo " data=\"$CFG->wwwroot/lib/rte/richedit.html\"";
916 echo " width=\"$width\" height=\"$height\" ";
917 echo " type=\"text/x-scriptlet\" VIEWASTEXT></object>\n";
918 echo "<TEXTAREA style=\"display:none\" NAME=\"$name\" ROWS=1 COLS=1>";
919 p($value);
920 echo "</TEXTAREA>\n";
921 } else {
922 echo "<TEXTAREA name=\"$name\" rows=\"$rows\" cols=\"$cols\" wrap=virtual>";
923 p($value);
924 echo "</TEXTAREA>\n";
925 }
926}
927
928function print_richedit_javascript($form, $name, $source="no") {
929 echo "<SCRIPT language=\"JavaScript\" event=\"onload\" for=\"window\">\n";
930 echo " document.richedit.options = \"history=no;source=$source\";";
931 echo " document.richedit.docHtml = $form.$name.innerText;";
932 echo "</SCRIPT>";
933}
934
935
936function update_course_icon($courseid) {
937// Used to be an icon, but it's now a simple form button
938 global $CFG, $USER;
939
940 if (isteacher($courseid)) {
9c9f7d77 941 if (!empty($USER->editing)) {
9fa49e22 942 $string = get_string("turneditingoff");
943 $edit = "off";
944 } else {
945 $string = get_string("turneditingon");
946 $edit = "on";
947 }
948 return "<FORM TARGET=_parent METHOD=GET ACTION=\"$CFG->wwwroot/course/view.php\">".
949 "<INPUT TYPE=hidden NAME=id VALUE=\"$courseid\">".
950 "<INPUT TYPE=hidden NAME=edit VALUE=\"$edit\">".
951 "<INPUT TYPE=submit VALUE=\"$string\"></FORM>";
952 }
953}
954
955function update_module_button($moduleid, $courseid, $string) {
956// Prints the editing button on a module "view" page
957 global $CFG;
958
959 if (isteacher($courseid)) {
960 $string = get_string("updatethis", "", $string);
961 return "<FORM TARGET=_parent METHOD=GET ACTION=\"$CFG->wwwroot/course/mod.php\">".
962 "<INPUT TYPE=hidden NAME=update VALUE=\"$moduleid\">".
963 "<INPUT TYPE=hidden NAME=return VALUE=\"true\">".
964 "<INPUT TYPE=submit VALUE=\"$string\"></FORM>";
965 }
966}
967
968
969function navmenu($course, $cm=NULL) {
970// Given a course and a (current) coursemodule
971// This function returns a small popup menu with all the
972// course activity modules in it, as a navigation menu
973// The data is taken from the serialised array stored in
974// the course record
975
976 global $CFG;
977
978 if ($cm) {
979 $cm = $cm->id;
980 }
981
982 if ($course->format == 'weeks') {
983 $strsection = get_string("week");
984 } else {
985 $strsection = get_string("topic");
986 }
987
988 if (!$modinfo = unserialize($course->modinfo)) {
989 return "";
990 }
991 $section = -1;
992 $selected = "";
993 foreach ($modinfo as $mod) {
994 if ($mod->section > 0 and $section <> $mod->section) {
995 $menu[] = "-------------- $strsection $mod->section --------------";
996 }
997 $section = $mod->section;
cf055081 998 //Only add visible or teacher mods to jumpmenu
999 if ($mod->visible or isteacher($course->id)) {
1000 $url = "$mod->mod/view.php?id=$mod->cm";
1001 if ($cm == $mod->cm) {
1002 $selected = $url;
1003 }
1004 $mod->name = urldecode($mod->name);
1005 if (strlen($mod->name) > 55) {
1006 $mod->name = substr($mod->name, 0, 50)."...";
1007 }
2a409368 1008 if (!$mod->visible) {
1009 $mod->name = "(".$mod->name.")";
1010 }
cf055081 1011 $menu[$url] = $mod->name;
9fa49e22 1012 }
9fa49e22 1013 }
1014
1015 return popup_form("$CFG->wwwroot/mod/", $menu, "navmenu", $selected, get_string("jumpto"), "", "", true);
1016}
1017
1018
1019
1020function print_date_selector($day, $month, $year, $currenttime=0) {
1021// Currenttime is a default timestamp in GMT
1022// Prints form items with the names $day, $month and $year
1023
1024 if (!$currenttime) {
1025 $currenttime = time();
1026 }
1027 $currentdate = usergetdate($currenttime);
1028
1029 for ($i=1; $i<=31; $i++) {
1030 $days[$i] = "$i";
1031 }
1032 for ($i=1; $i<=12; $i++) {
39e018b3 1033 $months[$i] = userdate(gmmktime(12,0,0,$i,1,2000), "%B");
9fa49e22 1034 }
1035 for ($i=2000; $i<=2010; $i++) {
1036 $years[$i] = $i;
1037 }
47f1da80 1038 choose_from_menu($days, $day, $currentdate['mday'], "");
1039 choose_from_menu($months, $month, $currentdate['mon'], "");
1040 choose_from_menu($years, $year, $currentdate['year'], "");
9fa49e22 1041}
1042
1043function print_time_selector($hour, $minute, $currenttime=0) {
1044// Currenttime is a default timestamp in GMT
1045// Prints form items with the names $hour and $minute
1046
1047 if (!$currenttime) {
1048 $currenttime = time();
1049 }
1050 $currentdate = usergetdate($currenttime);
1051 for ($i=0; $i<=23; $i++) {
1052 $hours[$i] = sprintf("%02d",$i);
1053 }
1054 for ($i=0; $i<=59; $i++) {
1055 $minutes[$i] = sprintf("%02d",$i);
1056 }
47f1da80 1057 choose_from_menu($hours, $hour, $currentdate['hours'], "");
1058 choose_from_menu($minutes, $minute, $currentdate['minutes'], "");
9fa49e22 1059}
1060
1061function error ($message, $link="") {
1062 global $CFG, $SESSION;
1063
1064 print_header(get_string("error"));
1065 echo "<BR>";
1066 print_simple_box($message, "center", "", "#FFBBBB");
1067
1068 if (!$link) {
1069 if ( !empty($SESSION->fromurl) ) {
1070 $link = "$SESSION->fromurl";
1071 unset($SESSION->fromurl);
9fa49e22 1072 } else {
1073 $link = "$CFG->wwwroot";
1074 }
1075 }
1076 print_continue($link);
1077 print_footer();
1078 die;
1079}
1080
1081function helpbutton ($page, $title="", $module="moodle", $image=true, $linktext=false, $text="") {
1082 // $page = the keyword that defines a help page
1083 // $title = the title of links, rollover tips, alt tags etc
1084 // $module = which module is the page defined in
1085 // $image = use a help image for the link? (true/false/"both")
1086 // $text = if defined then this text is used in the page, and
1087 // the $page variable is ignored.
1088 global $CFG;
1089
1090 if ($module == "") {
1091 $module = "moodle";
1092 }
1093
1094 if ($image) {
1095 if ($linktext) {
1096 $linkobject = "$title<IMG align=\"absmiddle\" BORDER=0 HEIGHT=17 WIDTH=22 ALT=\"\" SRC=\"$CFG->wwwroot/pix/help.gif\">";
1097 } else {
1098 $linkobject = "<IMG align=\"absmiddle\" BORDER=0 HEIGHT=17 WIDTH=22 ALT=\"$title\" SRC=\"$CFG->wwwroot/pix/help.gif\">";
1099 }
1100 } else {
1101 $linkobject = $title;
1102 }
1103 if ($text) {
1104 $url = "/help.php?module=$module&text=".htmlentities(urlencode($text));
1105 } else {
1106 $url = "/help.php?module=$module&file=$page.html";
1107 }
1108 link_to_popup_window ($url, "popup", $linkobject, 400, 500, $title);
1109}
1110
1111function notice ($message, $link="") {
750ab759 1112 global $CFG, $THEME;
9fa49e22 1113
1114 if (!$link) {
750ab759 1115 if (!empty($_SERVER["HTTP_REFERER"])) {
1116 $link = $_SERVER["HTTP_REFERER"];
1117 } else {
1118 $link = $CFG->wwwroot;
1119 }
9fa49e22 1120 }
1121
1122 echo "<BR>";
1123 print_simple_box($message, "center", "", "$THEME->cellheading");
1124 print_heading("<A HREF=\"$link\">".get_string("continue")."</A>");
1125 print_footer(get_site());
1126 die;
1127}
1128
1129function notice_yesno ($message, $linkyes, $linkno) {
1130 global $THEME;
1131
1132 print_simple_box_start("center", "", "$THEME->cellheading");
1133 echo "<P ALIGN=CENTER><FONT SIZE=3>$message</FONT></P>";
1134 echo "<P ALIGN=CENTER><FONT SIZE=3><B>";
1135 echo "<A HREF=\"$linkyes\">".get_string("yes")."</A>";
1136 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
1137 echo "<A HREF=\"$linkno\">".get_string("no")."</A>";
1138 echo "</B></FONT></P>";
1139 print_simple_box_end();
1140}
1141
1142function redirect($url, $message="", $delay=0) {
1143// Uses META tags to redirect the user, after printing a notice
1144
c9082a8c 1145 if (empty($message)) {
1146 echo "<META HTTP-EQUIV='Refresh' CONTENT='$delay; URL=$url'>";
1147 } else {
1148 if (! $delay) {
1149 $delay = 3; // There's no point having a message with no delay
1150 }
1151 echo "<META HTTP-EQUIV='Refresh' CONTENT='$delay; URL=$url'>";
9fa49e22 1152 print_header();
1153 echo "<CENTER>";
1154 echo "<P>$message</P>";
1155 echo "<P>( <A HREF=\"$url\">".get_string("continue")."</A> )</P>";
1156 echo "</CENTER>";
1157 }
1158 die;
1159}
1160
99988d1a 1161function notify ($message, $color="red", $align="center") {
1162 echo "<p align=\"$align\"><b><font color=\"$color\">$message</font></b></p>\n";
9fa49e22 1163}
1164
1165
9d5b689c 1166// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
f9903ed0 1167?>