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