missing strings (the last ones for 1.2??)
[moodle.git] / lib / weblib.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
9fa49e22 3///////////////////////////////////////////////////////////////////////////
4// weblib.php - functions for web output
f9903ed0 5//
9fa49e22 6// Library of all general-purpose Moodle PHP functions and constants
7// that produce HTML output
f9903ed0 8//
9fa49e22 9///////////////////////////////////////////////////////////////////////////
10// //
11// NOTICE OF COPYRIGHT //
12// //
13// Moodle - Modular Object-Oriented Dynamic Learning Environment //
14// http://moodle.com //
15// //
16// Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com //
17// //
18// This program is free software; you can redistribute it and/or modify //
19// it under the terms of the GNU General Public License as published by //
20// the Free Software Foundation; either version 2 of the License, or //
21// (at your option) any later version. //
22// //
23// This program is distributed in the hope that it will be useful, //
24// but WITHOUT ANY WARRANTY; without even the implied warranty of //
25// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
26// GNU General Public License for more details: //
27// //
28// http://www.gnu.org/copyleft/gpl.html //
29// //
30///////////////////////////////////////////////////////////////////////////
f9903ed0 31
0095d5cd 32/// Constants
33
c1d57101 34/// Define text formatting types ... eventually we can add Wiki, BBcode etc
6901fa79 35define("FORMAT_MOODLE", "0"); // Does all sorts of transformations and filtering
d342c763 36define("FORMAT_HTML", "1"); // Plain HTML (with some tags stripped)
37define("FORMAT_PLAIN", "2"); // Plain text (even tags are printed in full)
38define("FORMAT_WIKI", "3"); // Wiki-formatted text
0095d5cd 39
39dda0fc 40$ALLOWED_TAGS =
36268d02 41"<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><em><sup><sub><address><cite><blockquote><pre><strike><embed><object><param><acronym><nolink><style><lang><tex><algebra><math><mi><mn><mo><mtext><mspace><ms><mrow><mfrac><msqrt><mroot><mstyle><merror><mpadded><mphantom><mfenced><msub><msup><msubsup><munder><mover><munderover><mmultiscripts><mtable><mtr><mtd><maligngroup><malignmark><maction><cn><ci><apply><reln><fn><interval><inverse><sep><condition><declare><lambda><compose><ident><quotient><exp><factorial><divide><max><min><minus><plus><power><rem><times><root><gcd><and><or><xor><not><implies><forall><exists><abs><conjugate><eq><neq><gt><lt><geq><leq><ln><log><int><diff><partialdiff><lowlimit><uplimit><bvar><degree><set><list><union><intersect><in><notin><subset><prsubset><notsubset><notprsubset><setdiff><sum><product><limit><tendsto><mean><sdev><variance><median><mode><moment><vector><matrix><matrixrow><determinant><transpose><selector><annotation><semantics><annotation-xml>";
3fe3851d 42
43
0095d5cd 44/// Functions
45
3662bce5 46function s($var) {
c1d57101 47/// returns $var with HTML characters (like "<", ">", etc.) properly quoted,
f9903ed0 48
3662bce5 49 if (empty($var)) {
50 return "";
51 }
7d8f674d 52 return htmlSpecialChars(stripslashes_safe($var));
f9903ed0 53}
54
3662bce5 55function p($var) {
c1d57101 56/// prints $var with HTML characters (like "<", ">", etc.) properly quoted,
f9903ed0 57
3662bce5 58 if (empty($var)) {
59 echo "";
60 }
7d8f674d 61 echo htmlSpecialChars(stripslashes_safe($var));
f9903ed0 62}
63
8553b700 64function nvl(&$var, $default="") {
c1d57101 65/// if $var is undefined, return $default, otherwise return $var
8553b700 66
67 return isset($var) ? $var : $default;
68}
f9903ed0 69
70function strip_querystring($url) {
c1d57101 71/// takes a URL and returns it without the querystring portion
f9903ed0 72
b9b8ab69 73 if ($commapos = strpos($url, '?')) {
74 return substr($url, 0, $commapos);
75 } else {
76 return $url;
77 }
f9903ed0 78}
79
80function get_referer() {
c1d57101 81/// returns the URL of the HTTP_REFERER, less the querystring portion
f9903ed0 82
607809b3 83 return strip_querystring(nvl($_SERVER["HTTP_REFERER"]));
f9903ed0 84}
85
c1d57101 86
f9903ed0 87function me() {
c1d57101 88/// returns the name of the current script, WITH the querystring portion.
eaa50dbc 89/// this function is necessary because PHP_SELF and REQUEST_URI and SCRIPT_NAME
c1d57101 90/// return different things depending on a lot of things like your OS, Web
91/// server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.)
f9903ed0 92
607809b3 93 if (!empty($_SERVER["REQUEST_URI"])) {
94 return $_SERVER["REQUEST_URI"];
c1d57101 95
607809b3 96 } else if (!empty($_SERVER["PHP_SELF"])) {
fced815c 97 if (!empty($_SERVER["QUERY_STRING"])) {
98 return $_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"];
99 }
607809b3 100 return $_SERVER["PHP_SELF"];
c1d57101 101
fced815c 102 } else if (!empty($_SERVER["SCRIPT_NAME"])) {
103 if (!empty($_SERVER["QUERY_STRING"])) {
104 return $_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
105 }
106 return $_SERVER["SCRIPT_NAME"];
107
b9b8ab69 108 } else {
fced815c 109 notify("Warning: Could not find any of these web server variables: \$REQUEST_URI, \$PHP_SELF or \$SCRIPT_NAME");
bcdfe14e 110 return false;
7fbd6b1c 111 }
f9903ed0 112}
113
114
f9903ed0 115function qualified_me() {
c1d57101 116/// like me() but returns a full URL
f9903ed0 117
39e018b3 118 if (!empty($_SERVER["HTTP_HOST"])) {
119 $hostname = $_SERVER["HTTP_HOST"];
120 } else if (!empty($_ENV["HTTP_HOST"])) {
121 $hostname = $_ENV["HTTP_HOST"];
df3fd249 122 } else if (!empty($_SERVER["SERVER_NAME"])) {
123 $hostname = $_SERVER["SERVER_NAME"];
39e018b3 124 } else if (!empty($_ENV["SERVER_NAME"])) {
125 $hostname = $_ENV["SERVER_NAME"];
126 } else {
127 notify("Warning: could not find the name of this server!");
bcdfe14e 128 return false;
c1d57101 129 }
f9903ed0 130
607809b3 131 $protocol = (isset($_SERVER["HTTPS"]) and $_SERVER["HTTPS"] == "on") ? "https://" : "http://";
39e018b3 132 $url_prefix = $protocol.$hostname;
b9b8ab69 133 return $url_prefix . me();
f9903ed0 134}
135
136
a0deb5db 137function match_referer($goodreferer = "") {
138/// returns true if the referer is the same as the goodreferer. If
139/// goodreferer is not specified, use qualified_me as the goodreferer
60f18531 140 global $CFG;
141
ae384ef1 142 if (empty($CFG->secureforms)) { // Don't bother checking referer
60f18531 143 return true;
144 }
f9903ed0 145
ae384ef1 146 if ($goodreferer == "nomatch") { // Don't bother checking referer
a0deb5db 147 return true;
148 }
149
150 if (empty($goodreferer)) {
151 $goodreferer = qualified_me();
c1d57101 152 }
a0deb5db 153 return $goodreferer == get_referer();
f9903ed0 154}
155
36b4f985 156function data_submitted($url="") {
157/// Used on most forms in Moodle to check for data
158/// Returns the data as an object, if it's found.
607809b3 159/// This object can be used in foreach loops without
160/// casting because it's cast to (array) automatically
36b4f985 161///
162/// Checks that submitted POST data exists, and also
163/// checks the referer against the given url (it uses
164/// the current page if none was specified.
165
37208cd2 166 global $CFG;
167
607809b3 168 if (empty($_POST)) {
36b4f985 169 return false;
607809b3 170
36b4f985 171 } else {
172 if (match_referer($url)) {
607809b3 173 return (object)$_POST;
36b4f985 174 } else {
175 if ($CFG->debug > 10) {
176 notice("The form did not come from this page! (referer = ".get_referer().")");
177 }
178 return false;
179 }
180 }
181}
182
7d8f674d 183function stripslashes_safe($string) {
184/// stripslashes() removes ALL backslashes even from strings
185/// so C:\temp becomes C:temp ... this isn't good.
186/// The following should work as a fairly safe replacement
187/// to be called on quoted AND unquoted strings (to be sure)
188
189 $string = str_replace("\\'", "'", $string);
190 $string = str_replace('\\"', '"', $string);
191 $string = str_replace('\\\\', '\\', $string);
192 return $string;
193}
f9903ed0 194
ce57cc79 195if (!function_exists('str_ireplace')) { /// Only exists in PHP 5
7ec2fc00 196 function str_ireplace($find, $replace, $string) {
72e4eac6 197 /// This does a search and replace, ignoring case
ce57cc79 198 /// This function is only used for versions of PHP older than version 5
199 /// which do not have a native version of this function.
200 /// Taken from the PHP manual, by bradhuizenga @ softhome.net
7ec2fc00 201
202 if (!is_array($find)) {
203 $find = array($find);
204 }
205
206 if(!is_array($replace)) {
207 if (!is_array($find)) {
208 $replace = array($replace);
209 } else {
210 // this will duplicate the string into an array the size of $find
211 $c = count($find);
212 $rString = $replace;
213 unset($replace);
214 for ($i = 0; $i < $c; $i++) {
215 $replace[$i] = $rString;
216 }
217 }
218 }
219
220 foreach ($find as $fKey => $fItem) {
221 $between = explode(strtolower($fItem),strtolower($string));
222 $pos = 0;
223 foreach($between as $bKey => $bItem) {
224 $between[$bKey] = substr($string,$pos,strlen($bItem));
225 $pos += strlen($bItem) + strlen($fItem);
226 }
227 $string = implode($replace[$fKey],$between);
72e4eac6 228 }
7ec2fc00 229 return ($string);
3fe3851d 230 }
3fe3851d 231}
232
ce57cc79 233if (!function_exists('stripos')) { /// Only exists in PHP 5
234 function stripos($haystack, $needle, $offset=0) {
235 /// This function is only used for versions of PHP older than version 5
236 /// which do not have a native version of this function.
237 /// Taken from the PHP manual, by dmarsh @ spscc.ctc.edu
238 return strpos(strtoupper($haystack), strtoupper($needle), $offset);
239 }
240}
241
f9903ed0 242function read_template($filename, &$var) {
c1d57101 243/// return a (big) string containing the contents of a template file with all
244/// the variables interpolated. all the variables must be in the $var[] array or
245/// object (whatever you decide to use).
246///
247/// WARNING: do not use this on big files!!
f9903ed0 248
b9b8ab69 249 $temp = str_replace("\\", "\\\\", implode(file($filename), ""));
250 $temp = str_replace('"', '\"', $temp);
251 eval("\$template = \"$temp\";");
252 return $template;
f9903ed0 253}
254
255function checked(&$var, $set_value = 1, $unset_value = 0) {
c1d57101 256/// if variable is set, set it to the set_value otherwise set it to the
257/// unset_value. used to handle checkboxes when you are expecting them from
258/// a form
f9903ed0 259
b9b8ab69 260 if (empty($var)) {
261 $var = $unset_value;
262 } else {
263 $var = $set_value;
264 }
f9903ed0 265}
266
267function frmchecked(&$var, $true_value = "checked", $false_value = "") {
c1d57101 268/// prints the word "checked" if a variable is true, otherwise prints nothing,
269/// used for printing the word "checked" in a checkbox form input
f9903ed0 270
b9b8ab69 271 if ($var) {
272 echo $true_value;
273 } else {
274 echo $false_value;
275 }
f9903ed0 276}
277
278
86aa7ccf 279function link_to_popup_window ($url, $name="popup", $linkname="click here",
b48f834c 280 $height=400, $width=500, $title="Popup window", $options="none") {
c1d57101 281/// This will create a HTML link that will work on both
282/// Javascript and non-javascript browsers.
283/// Relies on the Javascript function openpopup in javascript.php
284/// $url must be relative to home page eg /mod/survey/stuff.php
f9903ed0 285
ff80e012 286 global $CFG;
287
b48f834c 288 if ($options == "none") {
289 $options = "menubar=0,location=0,scrollbars,resizable,width=$width,height=$height";
290 }
86aa7ccf 291 $fullscreen = 0;
f9903ed0 292
55e4b5f9 293 echo "<a target=\"$name\" title=\"$title\" href=\"$CFG->wwwroot$url\" ".
86aa7ccf 294 "onClick=\"return openpopup('$url', '$name', '$options', $fullscreen);\">$linkname</a>\n";
f9903ed0 295}
296
86aa7ccf 297
52f1b496 298function button_to_popup_window ($url, $name="popup", $linkname="click here",
299 $height=400, $width=500, $title="Popup window", $options="none") {
300/// This will create a HTML link that will work on both
301/// Javascript and non-javascript browsers.
302/// Relies on the Javascript function openpopup in javascript.php
303/// $url must be relative to home page eg /mod/survey/stuff.php
304
305 global $CFG;
306
307 if ($options == "none") {
308 $options = "menubar=0,location=0,scrollbars,resizable,width=$width,height=$height";
309 }
310 $fullscreen = 0;
311
312 echo "<input type=\"button\" name=\"popupwindow\" title=\"$title\" value=\"$linkname ...\" ".
313 "onClick=\"return openpopup('$url', '$name', '$options', $fullscreen);\">\n";
314}
315
316
f9903ed0 317function close_window_button() {
c1d57101 318/// Prints a simple button to close a window
319
86aa7ccf 320 echo "<center>\n";
321 echo "<script>\n";
322 echo "<!--\n";
323 echo "document.write('<form>');\n";
66a51452 324 echo "document.write('<input type=\"button\" onClick=\"self.close();\" value=\"".get_string("closewindow")."\" />');\n";
86aa7ccf 325 echo "document.write('</form>');\n";
326 echo "-->\n";
327 echo "</script>\n";
328 echo "<noscript>\n";
329 echo "<a href=\"".$_SERVER['HTTP_REFERER']."\"><---</a>\n";
330 echo "</noscript>\n";
331 echo "</center>\n";
f9903ed0 332}
333
334
08056730 335function choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", $nothingvalue="0", $return=false) {
c1d57101 336/// Given an array of value, creates a popup menu to be part of a form
337/// $options["value"]["label"]
f9903ed0 338
618b22c5 339 if ($nothing == "choose") {
340 $nothing = get_string("choose")."...";
341 }
342
f9903ed0 343 if ($script) {
344 $javascript = "onChange=\"$script\"";
9c9f7d77 345 } else {
346 $javascript = "";
f9903ed0 347 }
9c9f7d77 348
66a51452 349 $output = "<select name=\"$name\" $javascript>\n";
bda8d43a 350 if ($nothing) {
76c1650d 351 $output .= " <option value=\"$nothingvalue\"\n";
bda8d43a 352 if ($nothingvalue == $selected) {
66a51452 353 $output .= " selected=\"true\"";
bda8d43a 354 }
76c1650d 355 $output .= ">$nothing</option>\n";
873960de 356 }
607809b3 357 if (!empty($options)) {
358 foreach ($options as $value => $label) {
76c1650d 359 $output .= " <option value=\"$value\"";
607809b3 360 if ($value == $selected) {
66a51452 361 $output .= " selected=\"true\"";
607809b3 362 }
a20c1090 363 if ($label === "") {
76c1650d 364 $output .= ">$value</option>\n";
a20c1090 365 } else {
366 $output .= ">$label</option>\n";
607809b3 367 }
f9903ed0 368 }
369 }
76c1650d 370 $output .= "</select>\n";
08056730 371
372 if ($return) {
373 return $output;
374 } else {
375 echo $output;
376 }
f9903ed0 377}
378
16ef5e78 379function popup_form ($common, $options, $formname, $selected="", $nothing="choose", $help="", $helptext="", $return=false, $targetwindow="self") {
c1d57101 380/// Implements a complete little popup form
381/// $common = the URL up to the point of the variable that changes
382/// $options = A list of value-label pairs for the popup list
383/// $formname = name must be unique on the page
384/// $selected = the option that is already selected
385/// $nothing = The label for the "no choice" option
e5dfd0f3 386/// $help = The name of a help page if help is required
387/// $helptext = The name of the label for the help button
5b472756 388/// $return = Boolean indicating whether the function should return the text
389/// as a string or echo it directly to the page being rendered
f9903ed0 390
0d0baabf 391 global $CFG;
392
618b22c5 393 if ($nothing == "choose") {
394 $nothing = get_string("choose")."...";
395 }
396
66a51452 397 $startoutput = "<form target=\"{$CFG->framename}\" name=\"$formname\">";
398 $output = "<select name=\"popup\" onchange=\"$targetwindow.location=document.$formname.popup.options[document.$formname.popup.selectedIndex].value\">\n";
f9903ed0 399
400 if ($nothing != "") {
dfec7b01 401 $output .= " <option value=\"javascript:void(0)\">$nothing</option>\n";
f9903ed0 402 }
403
404 foreach ($options as $value => $label) {
d897cae4 405 if (substr($label,0,1) == "-") {
dfec7b01 406 $output .= " <option value=\"\"";
d897cae4 407 } else {
dfec7b01 408 $output .= " <option value=\"$common$value\"";
d897cae4 409 if ($value == $selected) {
66a51452 410 $output .= " selected=\"true\"";
d897cae4 411 }
f9903ed0 412 }
413 if ($label) {
dfec7b01 414 $output .= ">$label</option>\n";
f9903ed0 415 } else {
dfec7b01 416 $output .= ">$value</option>\n";
f9903ed0 417 }
418 }
dfec7b01 419 $output .= "</select>";
420 $output .= "</form>\n";
d897cae4 421
422 if ($return) {
dfec7b01 423 return $startoutput.$output;
d897cae4 424 } else {
dfec7b01 425 echo $startoutput;
9c9f7d77 426 if ($help) {
427 helpbutton($help, $helptext);
428 }
d897cae4 429 echo $output;
430 }
f9903ed0 431}
432
433
434
435function formerr($error) {
c1d57101 436/// Prints some red text
f9903ed0 437 if (!empty($error)) {
66a51452 438 echo "<font color=\"#ff0000\">$error</font>";
f9903ed0 439 }
440}
441
442
443function validate_email ($address) {
66a51452 444/// Validates an email to make sure it makes sense.
f9903ed0 445 return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.
446 '@'.
447 '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
448 '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
449 $address));
450}
451
6c8e8b5e 452function detect_munged_arguments($string) {
393c9b4f 453 if (ereg('\.\.', $string)) { // check for parent URLs
6c8e8b5e 454 return true;
455 }
393c9b4f 456 if (ereg('[\|\`]', $string)) { // check for other bad characters
6c8e8b5e 457 return true;
458 }
459 return false;
460}
461
6ed3da1d 462function get_slash_arguments($file="file.php") {
463/// Searches the current environment variables for some slash arguments
f9903ed0 464
eaa50dbc 465 if (!$string = me()) {
f9903ed0 466 return false;
467 }
eaa50dbc 468
6ed3da1d 469 $pathinfo = explode($file, $string);
470
bcdfe14e 471 if (!empty($pathinfo[1])) {
472 return $pathinfo[1];
6ed3da1d 473 } else {
474 return false;
475 }
476}
477
478function parse_slash_arguments($string, $i=0) {
479/// Extracts arguments from "/foo/bar/something"
480/// eg http://mysite.com/script.php/foo/bar/something
f9903ed0 481
6c8e8b5e 482 if (detect_munged_arguments($string)) {
780db230 483 return false;
484 }
6ed3da1d 485 $args = explode("/", $string);
f9903ed0 486
487 if ($i) { // return just the required argument
488 return $args[$i];
489
490 } else { // return the whole array
491 array_shift($args); // get rid of the empty first one
492 return $args;
493 }
494}
495
0095d5cd 496function format_text_menu() {
c1d57101 497/// Just returns an array of formats suitable for a popup menu
0095d5cd 498 return array (FORMAT_MOODLE => get_string("formattext"),
6901fa79 499 FORMAT_HTML => get_string("formathtml"),
d342c763 500 FORMAT_PLAIN => get_string("formatplain"),
501 FORMAT_WIKI => get_string("formatwiki"));
0095d5cd 502}
503
c4ae4fa1 504function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL ) {
c1d57101 505/// Given text in a variety of format codings, this function returns
506/// the text as safe HTML.
507///
508/// $text is raw text (originally from a user)
509/// $format is one of the format constants, defined above
0095d5cd 510
c4ae4fa1 511 global $CFG, $course;
512
f0aa2fed 513 if (!empty($CFG->cachetext)) {
514 $time = time() - $CFG->cachetext;
515 $md5key = md5($text);
45121ffb 516 if ($cacheitem = get_record_select('cache_text', "md5key = '$md5key' AND timemodified > '$time'")) {
f0aa2fed 517 return $cacheitem->formattedtext;
518 }
519 }
520
c4ae4fa1 521 if (empty($courseid)) {
8eaa4c61 522 if (!empty($course->id)) { // An ugly hack for better compatibility
c4ae4fa1 523 $courseid = $course->id;
524 }
525 }
a751a4e5 526
8eaa4c61 527 $CFG->currenttextiscacheable = true; // Default status - can be changed by any filter
528
0095d5cd 529 switch ($format) {
73f8658c 530 case FORMAT_HTML:
5f350e8f 531 replace_smilies($text);
f0aa2fed 532 $text = filter_text($text, $courseid);
73f8658c 533 break;
534
6901fa79 535 case FORMAT_PLAIN:
536 $text = htmlentities($text);
3405b212 537 $text = str_replace(" ", "&nbsp; ", $text);
5f350e8f 538 replace_smilies($text);
6901fa79 539 $text = nl2br($text);
6901fa79 540 break;
541
d342c763 542 case FORMAT_WIKI:
e67b9e31 543 $text = wiki_to_html($text);
f0aa2fed 544 $text = filter_text($text, $courseid);
d342c763 545 break;
546
73f8658c 547 default: // FORMAT_MOODLE or anything else
c9dda990 548 if (!isset($options->smiley)) {
549 $options->smiley=true;
550 }
551 if (!isset($options->para)) {
1a072208 552 $options->para=true;
c9dda990 553 }
b7a3d3b2 554 if (!isset($options->newlines)) {
555 $options->newlines=true;
556 }
557 $text = text_to_html($text, $options->smiley, $options->para, $options->newlines);
f0aa2fed 558 $text = filter_text($text, $courseid);
0095d5cd 559 break;
0095d5cd 560 }
f0aa2fed 561
8eaa4c61 562 if (!empty($CFG->cachetext) and $CFG->currenttextiscacheable) {
f0aa2fed 563 $newrecord->md5key = $md5key;
564 $newrecord->formattedtext = addslashes($text);
565 $newrecord->timemodified = time();
45121ffb 566 insert_record('cache_text', $newrecord);
f0aa2fed 567 }
568
569 return $text;
0095d5cd 570}
571
d342c763 572function format_text_email($text, $format) {
573/// Given text in a variety of format codings, this function returns
574/// the text as plain text suitable for plain email.
575///
576/// $text is raw text (originally from a user)
577/// $format is one of the format constants, defined above
578
579 switch ($format) {
580
581 case FORMAT_PLAIN:
582 return $text;
583 break;
584
585 case FORMAT_WIKI:
586 $text = wiki_to_html($text);
5b472756 587 /// This expression turns links into something nice in a text format. (Russell Jungwirth)
588 /// From: http://php.net/manual/en/function.eregi-replace.php and simplified
589 $text = eregi_replace('(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)','\\3 [\\2]', $text);
7c55a29b 590 return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES)));
d342c763 591 break;
592
6ff45b59 593 case FORMAT_HTML:
594 return html_to_text($text);
595 break;
596
d342c763 597 default: // FORMAT_MOODLE or anything else
5b472756 598 $text = eregi_replace('(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)','\\3 [\\2]', $text);
7c55a29b 599 return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES)));
d342c763 600 break;
601 }
602}
0095d5cd 603
e67b9e31 604
c4ae4fa1 605function filter_text($text, $courseid=NULL) {
e67b9e31 606/// Given some text in HTML format, this function will pass it
607/// through any filters that have been defined in $CFG->textfilterx
608/// The variable defines a filepath to a file containing the
609/// filter function. The file must contain a variable called
610/// $textfilter_function which contains the name of the function
c4ae4fa1 611/// with $courseid and $text parameters
e67b9e31 612
c4ae4fa1 613 global $CFG;
e67b9e31 614
d523d2ea 615 if (!empty($CFG->textfilters)) {
616 $textfilters = explode(',', $CFG->textfilters);
617 foreach ($textfilters as $textfilter) {
828aeff2 618 if (is_readable("$CFG->dirroot/$textfilter/filter.php")) {
619 include("$CFG->dirroot/$textfilter/filter.php");
d523d2ea 620 $text = $textfilter_function($courseid, $text);
621 }
e67b9e31 622 }
623 }
d523d2ea 624
e67b9e31 625 return $text;
626}
627
628
3da47524 629function clean_text($text, $format=FORMAT_MOODLE) {
c1d57101 630/// Given raw text (eg typed in by a user), this function cleans it up
631/// and removes any nasty tags that could mess up Moodle pages.
b7a3cf49 632
fc120758 633 global $ALLOWED_TAGS;
3fe3851d 634
d342c763 635 switch ($format) {
0095d5cd 636 case FORMAT_MOODLE:
0095d5cd 637 case FORMAT_HTML:
d342c763 638 case FORMAT_WIKI:
5b472756 639 /// Remove javascript: label
3fe3851d 640 $text = strip_tags($text, $ALLOWED_TAGS);
5b472756 641 /// Remove javascript/VBScript
642 $text = str_ireplace("javascript:", "xxx", $text);
643 /// Remove script events
644 $text = eregi_replace("([^a-z])language([[:space:]]*)=", "xxx", $text);
645 $text = eregi_replace("([^a-z])on([a-z]+)([[:space:]]*)=", "xxx", $text);
3fe3851d 646 return $text;
6901fa79 647
648 case FORMAT_PLAIN:
649 return $text;
0095d5cd 650 }
b7a3cf49 651}
f9903ed0 652
5f350e8f 653function replace_smilies(&$text) {
c1d57101 654/// Replaces all known smileys in the text with image equivalents
2ea9027b 655 global $CFG;
c1d57101 656
5b472756 657/// this builds the mapping array only once
617778f2 658 static $runonce = false;
69081931 659 static $e = array();
660 static $img = array();
617778f2 661 static $emoticons = array(
fbfc2675 662 ':-)' => 'smiley',
663 ':)' => 'smiley',
664 ':-D' => 'biggrin',
665 ';-)' => 'wink',
666 ':-/' => 'mixed',
667 'V-.' => 'thoughtful',
668 ':-P' => 'tongueout',
669 'B-)' => 'cool',
670 '^-)' => 'approve',
671 '8-)' => 'wideeyes',
672 ':o)' => 'clown',
673 ':-(' => 'sad',
674 ':(' => 'sad',
675 '8-.' => 'shy',
676 ':-I' => 'blush',
677 ':-X' => 'kiss',
678 '8-o' => 'surprise',
679 'P-|' => 'blackeye',
680 '8-[' => 'angry',
681 'xx-P' => 'dead',
682 '|-.' => 'sleepy',
683 '}-]' => 'evil',
2ea9027b 684 );
685
fbfc2675 686 if ($runonce == false) { /// After the first time this is not run again
617778f2 687 foreach ($emoticons as $emoticon => $image){
fbfc2675 688 $alttext = get_string($image, 'pix');
689
69081931 690 $e[] = $emoticon;
fbfc2675 691 $img[] = "<img alt=\"$alttext\" width=\"15\" height=\"15\" src=\"$CFG->pixpath/s/$image.gif\" />";
617778f2 692 }
693 $runonce = true;
c0f728ba 694 }
b7a3cf49 695
fbfc2675 696/// this is the meat of the code - this is run every time
5f350e8f 697 $text = str_replace($e, $img, $text);
1a072208 698}
0095d5cd 699
b7a3d3b2 700function text_to_html($text, $smiley=true, $para=true, $newlines=true) {
c1d57101 701/// Given plain text, makes it into HTML as nicely as possible.
702/// May contain HTML tags already
f9903ed0 703
27326a3e 704 global $CFG;
705
c1d57101 706/// Remove any whitespace that may be between HTML tags
7b3be1b1 707 $text = eregi_replace(">([[:space:]]+)<", "><", $text);
708
c1d57101 709/// Remove any returns that precede or follow HTML tags
0eae8049 710 $text = eregi_replace("([\n\r])<", " <", $text);
711 $text = eregi_replace(">([\n\r])", "> ", $text);
7b3be1b1 712
5f350e8f 713 convert_urls_into_links($text);
f9903ed0 714
c1d57101 715/// Make returns into HTML newlines.
b7a3d3b2 716 if ($newlines) {
717 $text = nl2br($text);
718 }
f9903ed0 719
c1d57101 720/// Turn smileys into images.
d69cb7f4 721 if ($smiley) {
5f350e8f 722 replace_smilies($text);
d69cb7f4 723 }
f9903ed0 724
c1d57101 725/// Wrap the whole thing in a paragraph tag if required
909f539d 726 if ($para) {
01d79966 727 return "<p>".$text."</p>";
909f539d 728 } else {
729 return $text;
730 }
f9903ed0 731}
732
3e9ca9fb 733function wiki_to_html($text) {
01d79966 734/// Given Wiki formatted text, make it into XHTML using external function
43373804 735 global $CFG;
3e9ca9fb 736
43373804 737 require_once("$CFG->libdir/wiki.php");
3e9ca9fb 738
01d79966 739 $wiki = new Wiki;
740 return $wiki->format($text);
3e9ca9fb 741}
742
6ff45b59 743function html_to_text($html) {
744/// Given HTML text, make it into plain text using external function
428aaa29 745 global $CFG;
6ff45b59 746
747 require_once("$CFG->libdir/html2text.php");
748
749 return html2text($html);
750}
751
752
5f350e8f 753function convert_urls_into_links(&$text) {
754/// Given some text, it converts any URLs it finds into HTML links.
755
756/// Make lone URLs into links. eg http://moodle.com/
3405b212 757 $text = eregi_replace("([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
88438a58 758 "\\1<a href=\"\\2://\\3\\4\" target=\"newpage\">\\2://\\3\\4</a>", $text);
5f350e8f 759
760/// eg www.moodle.com
3405b212 761 $text = eregi_replace("([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])",
88438a58 762 "\\1<a href=\"http://www.\\2\\3\" target=\"newpage\">www.\\2\\3</a>", $text);
5f350e8f 763}
764
88438a58 765function highlight($needle, $haystack, $case=0,
766 $left_string="<span class=\"highlight\">", $right_string="</span>") {
767/// This function will highlight search words in a given string
768/// It cares about HTML and will not ruin links. It's best to use
769/// this function after performing any conversions to HTML.
770/// Function found here: http://forums.devshed.com/t67822/scdaa2d1c3d4bacb4671d075ad41f0854.html
771
69d51d3a 772 if (empty($needle)) {
773 return $haystack;
774 }
775
88438a58 776 $list_of_words = eregi_replace("[^-a-zA-Z0-9&']", " ", $needle);
777 $list_array = explode(" ", $list_of_words);
778 for ($i=0; $i<sizeof($list_array); $i++) {
779 if (strlen($list_array[$i]) == 1) {
780 $list_array[$i] = "";
781 }
782 }
783 $list_of_words = implode(" ", $list_array);
784 $list_of_words_cp = $list_of_words;
785 $final = array();
786 preg_match_all('/<(.+?)>/is',$haystack,$list_of_words);
787
788 foreach (array_unique($list_of_words[0]) as $key=>$value) {
789 $final['<|'.$key.'|>'] = $value;
790 }
791
792 $haystack = str_replace($final,array_keys($final),$haystack);
793 $list_of_words_cp = eregi_replace(" +", "|", $list_of_words_cp);
794
795 if ($list_of_words_cp{0}=="|") {
796 $list_of_words_cp{0} = "";
797 }
798 if ($list_of_words_cp{strlen($list_of_words_cp)-1}=="|") {
799 $list_of_words_cp{strlen($list_of_words_cp)-1}="";
800 }
801 $list_of_words_cp = "(".trim($list_of_words_cp).")";
802
803 if (!$case){
804 $haystack = eregi_replace("$list_of_words_cp", "$left_string"."\\1"."$right_string", $haystack);
805 } else {
806 $haystack = ereg_replace("$list_of_words_cp", "$left_string"."\\1"."$right_string", $haystack);
807 }
808 $haystack = str_replace(array_keys($final),$final,$haystack);
809
810 return stripslashes($haystack);
811}
812
813function highlightfast($needle, $haystack) {
c1d57101 814/// This function will highlight instances of $needle in $haystack
88438a58 815/// It's faster that the above function and doesn't care about
816/// HTML or anything.
5af78ed2 817
818 $parts = explode(strtolower($needle), strtolower($haystack));
819
820 $pos = 0;
821
822 foreach ($parts as $key => $part) {
823 $parts[$key] = substr($haystack, $pos, strlen($part));
824 $pos += strlen($part);
825
88438a58 826 $parts[$key] .= "<span class=\"highlight\">".substr($haystack, $pos, strlen($needle))."</span>";
5af78ed2 827 $pos += strlen($needle);
828 }
829
830 return (join('', $parts));
831}
832
f9903ed0 833
9fa49e22 834/// STANDARD WEB PAGE PARTS ///////////////////////////////////////////////////
835
66a51452 836function print_header ($title="", $heading="", $navigation="", $focus="", $meta="",
837 $cache=true, $button="&nbsp;", $menu="", $usexml=false) {
9fa49e22 838// $title - appears top of window
839// $heading - appears top of page
840// $navigation - premade navigation string
841// $focus - indicates form element eg inputform.password
842// $meta - meta tags in the header
843// $cache - should this page be cacheable?
844// $button - HTML code for a button (usually for module editing)
66a51452 845// $menu - HTML code for a popup menu
846// $usexml - use XML for this page
e825f279 847 global $USER, $CFG, $THEME, $SESSION;
9fa49e22 848
b3153e4b 849 global $course; // This is a bit of an ugly hack to be gotten rid of later
850 if (!empty($course->lang)) {
851 $CFG->courselang = $course->lang;
852 }
853
9fa49e22 854 if (file_exists("$CFG->dirroot/theme/$CFG->theme/styles.php")) {
855 $styles = $CFG->stylesheet;
856 } else {
857 $styles = "$CFG->wwwroot/theme/standard/styles.php";
858 }
859
860 if ($navigation == "home") {
861 $home = true;
862 $navigation = "";
9d378732 863 } else {
864 $home = false;
9fa49e22 865 }
866
867 if ($button == "") {
868 $button = "&nbsp;";
869 }
870
871 if (!$menu and $navigation) {
872 if (isset($USER->id)) {
66a51452 873 $menu = "<font size=\"2\"><a target=\"$CFG->framename\" href=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</a></font>";
9fa49e22 874 } else {
66a51452 875 $menu = "<font size=\"2\"><a target=\"$CFG->framename\" href=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</a></font>";
9fa49e22 876 }
877 }
878
47037513 879 // Add a stylesheet for the HTML editor
880 $meta = "<style type=\"text/css\">@import url($CFG->wwwroot/lib/editor/htmlarea.css);</style>\n$meta\n";
881
9fa49e22 882 // Specify character set ... default is iso-8859-1 but some languages might need something else
883 // Could be optimised by carrying the charset variable around in $USER
884 if (current_language() == "en") {
66a51452 885 $meta = "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\" />\n$meta\n";
9fa49e22 886 } else {
66a51452 887 $meta = "<meta http-equiv=\"content-type\" content=\"text/html; charset=".get_string("thischarset")."\" />\n$meta\n";
9fa49e22 888 }
889
d8152d04 890 if ( get_string("thisdirection") == "rtl" ) {
107b010b 891 $direction = " dir=\"rtl\"";
9fa49e22 892 } else {
107b010b 893 $direction = " dir=\"ltr\"";
9fa49e22 894 }
895
896 if (!$cache) { // Do everything we can to prevent clients and proxies caching
897 @header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
898 @header("Pragma: no-cache");
66a51452 899 $meta .= "\n<meta http-equiv=\"pragma\" content=\"no-cache\" />";
900 $meta .= "\n<meta http-equiv=\"expires\" content=\"0\" />";
901 }
902
903 if ($usexml) { // Added by Gustav Delius / Mad Alex for MathML output
904 $currentlanguage = current_language();
905
906 @header("Content-type: text/xml");
907 echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n";
908 if (!empty($CFG->xml_stylesheets)) {
909 $stylesheets = explode(";", $CFG->xml_stylesheets);
910 foreach ($stylesheets as $stylesheet) {
911 echo "<?xml-stylesheet type=\"text/xsl\" href=\"$CFG->wwwroot/$stylesheet\" ?>\n";
912 }
913 }
914 echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1";
e4576482 915 if (!empty($CFG->xml_doctype_extra)) {
66a51452 916 echo " plus $CFG->xml_doctype_extra";
e4576482 917 }
66a51452 918 echo "//" . strtoupper($currentlanguage) . "\" \"$CFG->xml_dtd\">\n";
919 $direction = " xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"$currentlanguage\" $direction";
9fa49e22 920 }
921
2eea2cce 922 $title = str_replace('"', '&quot;', $title);
1d9fc417 923 $title = strip_tags($title);
2eea2cce 924
9fa49e22 925 include ("$CFG->dirroot/theme/$CFG->theme/header.html");
926}
927
928function print_footer ($course=NULL) {
929// Can provide a course object to make the footer contain a link to
930// to the course home page, otherwise the link will go to the site home
931 global $USER, $CFG, $THEME;
932
933
934/// Course links
935 if ($course) {
936 if ($course == "home") { // special case for site home page - please do not remove
76c1650d 937 $homelink = "<p align=\"center\"><a title=\"moodle $CFG->release ($CFG->version)\" href=\"http://moodle.org/\" target=\"_blank\">";
2d8b2369 938 $homelink .= "<br /><img width=\"100\" height=\"30\" src=\"pix/moodlelogo.gif\" border=\"0\" /></a></p>";
9fa49e22 939 $course = get_site();
940 $homepage = true;
941 } else {
76c1650d 942 $homelink = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>";
9fa49e22 943 }
944 } else {
c2cb4545 945 $homelink = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/\">".get_string("home")."</a>";
9fa49e22 946 $course = get_site();
947 }
948
949/// User links
a282d0ff 950 $loggedinas = user_login_string($course, $USER);
951
952 include ("$CFG->dirroot/theme/$CFG->theme/footer.html");
953}
954
1ddf9329 955function style_sheet_setup($lastmodified=0, $lifetime=300, $themename="") {
956/// This function is called by stylesheets to set up the header
957/// approriately as well as the current path
6535be85 958
959 global $CFG;
e3b343e0 960
6535be85 961 header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
962 header("Expires: " . gmdate("D, d M Y H:i:s", time() + $lifetime) . " GMT");
963 header("Cache-control: max_age = $lifetime");
964 header("Pragma: ");
965 header("Content-type: text/css"); // Correct MIME type
966
967 if (!empty($themename)) {
968 $CFG->theme = $themename;
969 }
970
971 return "$CFG->wwwroot/theme/$CFG->theme";
972
973}
974
a282d0ff 975
976function user_login_string($course, $user=NULL) {
977 global $USER, $CFG;
978
8d2accb6 979 if (empty($user)) {
a282d0ff 980 $user = $USER;
981 }
982
983 if (isset($user->realuser)) {
984 if ($realuser = get_record("user", "id", $user->realuser)) {
2d71e8ee 985 $fullname = fullname($realuser, true);
986 $realuserinfo = " [<a target=\"{$CFG->framename}\"
987 href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&amp;return=$realuser->id\">$fullname</a>] ";
9fa49e22 988 }
9d378732 989 } else {
990 $realuserinfo = "";
9fa49e22 991 }
992
a282d0ff 993 if (isset($user->id) and $user->id) {
2d71e8ee 994 $fullname = fullname($user, true);
995 $username = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">$fullname</a>";
9fa49e22 996 $loggedinas = $realuserinfo.get_string("loggedinas", "moodle", "$username").
ca16eaeb 997 " (<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</a>)";
9fa49e22 998 } else {
999 $loggedinas = get_string("loggedinnot", "moodle").
ca16eaeb 1000 " (<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</a>)";
9fa49e22 1001 }
a282d0ff 1002 return $loggedinas;
9fa49e22 1003}
1004
1005
9fa49e22 1006function print_navigation ($navigation) {
1007 global $CFG;
1008
1009 if ($navigation) {
1010 if (! $site = get_site()) {
1011 $site->shortname = get_string("home");;
1012 }
eb347b6b 1013 echo "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/\">$site->shortname</a> -> $navigation";
9fa49e22 1014 }
1015}
1016
d4df9200 1017function print_headline($text, $size=2) {
1018 echo "<b><font size=\"$size\">$text</font></b><br />\n";
1019}
1020
76c1650d 1021function print_heading($text, $align="center", $size=3) {
1022 echo "<p align=\"$align\"><font size=\"$size\"><b>".stripslashes_safe($text)."</b></font></p>";
9fa49e22 1023}
1024
c9f6251e 1025function print_heading_with_help($text, $helppage, $module="moodle", $icon="") {
9fa49e22 1026// Centered heading with attached help button (same title text)
c9f6251e 1027// and optional icon attached
1028 echo "<p align=\"center\"><font size=\"3\">$icon<b>".stripslashes_safe($text);
9fa49e22 1029 helpbutton($helppage, $text, $module);
eb347b6b 1030 echo "</b></font></p>";
9fa49e22 1031}
1032
1033function print_continue($link) {
9fa49e22 1034
1035 if (!$link) {
607809b3 1036 $link = $_SERVER["HTTP_REFERER"];
9fa49e22 1037 }
1038
eb347b6b 1039 print_heading("<a href=\"$link\">".get_string("continue")."</a>");
9fa49e22 1040}
1041
1042
1043function print_simple_box($message, $align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
1044 print_simple_box_start($align, $width, $color, $padding, $class);
7d8f674d 1045 echo stripslashes_safe($message);
9fa49e22 1046 print_simple_box_end();
1047}
1048
1049function print_simple_box_start($align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
1050 global $THEME;
1051
1052 if ($align) {
76c1650d 1053 $align = "align=\"$align\"";
9fa49e22 1054 }
1055 if ($width) {
76c1650d 1056 $width = "width=\"$width\"";
9fa49e22 1057 }
9d378732 1058 echo "<table $align $width class=\"$class\" border=\"0\" cellpadding=\"$padding\" cellspacing=\"0\"><tr><td bgcolor=\"$color\" class=\"$class"."content\">";
9fa49e22 1059}
1060
1061function print_simple_box_end() {
1062 echo "</td></tr></table>";
1063}
1064
cc7fa0dc 1065function print_single_button($link, $options, $label="OK", $method="get") {
1066 echo "<form action=\"$link\" method=\"$method\">";
9fa49e22 1067 if ($options) {
1068 foreach ($options as $name => $value) {
66a51452 1069 echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
9fa49e22 1070 }
1071 }
66a51452 1072 echo "<input type=\"submit\" value=\"$label\" /></form>";
9fa49e22 1073}
1074
1075function print_spacer($height=1, $width=1, $br=true) {
1076 global $CFG;
66a51452 1077 echo "<img height=\"$height\" width=\"$width\" src=\"$CFG->wwwroot/pix/spacer.gif\" alt=\"\" />";
9fa49e22 1078 if ($br) {
76c1650d 1079 echo "<br />\n";
9fa49e22 1080 }
1081}
1082
1083function print_file_picture($path, $courseid=0, $height="", $width="", $link="") {
1084// Given the path to a picture file in a course, or a URL,
1085// this function includes the picture in the page.
1086 global $CFG;
1087
1088 if ($height) {
76c1650d 1089 $height = "height=\"$height\"";
9fa49e22 1090 }
1091 if ($width) {
76c1650d 1092 $width = "width=\"$width\"";
9fa49e22 1093 }
1094 if ($link) {
76c1650d 1095 echo "<a href=\"$link\">";
9fa49e22 1096 }
1097 if (substr(strtolower($path), 0, 7) == "http://") {
66a51452 1098 echo "<img border=\"0\" $height $width src=\"$path\" />";
9fa49e22 1099
1100 } else if ($courseid) {
66a51452 1101 echo "<img border=\"0\" $height $width src=\"";
9fa49e22 1102 if ($CFG->slasharguments) { // Use this method if possible for better caching
1103 echo "$CFG->wwwroot/file.php/$courseid/$path";
1104 } else {
3f396065 1105 echo "$CFG->wwwroot/file.php?file=/$courseid/$path";
9fa49e22 1106 }
66a51452 1107 echo "\" />";
9fa49e22 1108 } else {
1109 echo "Error: must pass URL or course";
1110 }
1111 if ($link) {
76c1650d 1112 echo "</a>";
9fa49e22 1113 }
1114}
1115
1116function print_user_picture($userid, $courseid, $picture, $large=false, $returnstring=false, $link=true) {
f374fb10 1117 global $CFG;
9fa49e22 1118
1119 if ($link) {
66a51452 1120 $output = "<a href=\"$CFG->wwwroot/user/view.php?id=$userid&amp;course=$courseid\">";
9fa49e22 1121 } else {
1122 $output = "";
1123 }
1124 if ($large) {
67a63a30 1125 $file = "f1";
9fa49e22 1126 $size = 100;
1127 } else {
67a63a30 1128 $file = "f2";
9fa49e22 1129 $size = 35;
1130 }
67a63a30 1131 if ($picture) { // Print custom user picture
9fa49e22 1132 if ($CFG->slasharguments) { // Use this method if possible for better caching
67a63a30 1133 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/user/pix.php/$userid/$file.jpg\"".
66a51452 1134 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" />";
9fa49e22 1135 } else {
67a63a30 1136 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/user/pix.php?file=/$userid/$file.jpg\"".
66a51452 1137 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" />";
9fa49e22 1138 }
67a63a30 1139 } else { // Print default user pictures (use theme version if available)
c9f6251e 1140 $output .= "<img align=\"absmiddle\" src=\"$CFG->pixpath/u/$file.png\"".
66a51452 1141 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" />";
9fa49e22 1142 }
1143 if ($link) {
76c1650d 1144 $output .= "</a>";
9fa49e22 1145 }
1146
1147 if ($returnstring) {
1148 return $output;
1149 } else {
1150 echo $output;
1151 }
1152}
1153
951b22a8 1154function print_user($user, $course) {
1155/// Prints a summary of a user in a nice little box
1156
499795e8 1157 global $CFG;
1158
951b22a8 1159 static $string;
1160 static $datestring;
1161 static $countries;
1162 static $isteacher;
1163
1164 if (empty($string)) { // Cache all the strings for the rest of the page
1165
1166 $string->email = get_string("email");
1167 $string->location = get_string("location");
1168 $string->lastaccess = get_string("lastaccess");
1169 $string->activity = get_string("activity");
1170 $string->unenrol = get_string("unenrol");
1171 $string->loginas = get_string("loginas");
1172 $string->fullprofile = get_string("fullprofile");
1173 $string->role = get_string("role");
1174 $string->name = get_string("name");
1175 $string->never = get_string("never");
1176
1177 $datestring->day = get_string("day");
1178 $datestring->days = get_string("days");
1179 $datestring->hour = get_string("hour");
1180 $datestring->hours = get_string("hours");
1181 $datestring->min = get_string("min");
1182 $datestring->mins = get_string("mins");
1183 $datestring->sec = get_string("sec");
1184 $datestring->secs = get_string("secs");
1185
1186 $countries = get_list_of_countries();
1187
1188 $isteacher = isteacher($course->id);
1189 }
1190
1191 echo '<table width="80%" align="center" border="0" cellpadding="10" cellspacing="0" class="userinfobox">';
1192 echo '<tr>';
1193 echo '<td width="100" bgcolor="#ffffff" valign="top" class="userinfoboxside">';
1194 print_user_picture($user->id, $course->id, $user->picture, true);
1195 echo '</td>';
1196 echo '<td width="100%" bgcolor="#ffffff" valign="top" class="userinfoboxsummary">';
1197 echo '<font size="-1">';
1198 echo '<font size="3"><b>'.fullname($user, $isteacher).'</b></font>';
1199 echo '<p>';
1200 if (!empty($user->role) and ($user->role <> $course->teacher)) {
1201 echo "$string->role: $user->role<br />";
1202 }
1203 if ($user->maildisplay == 1 or ($user->maildisplay == 2 and $course->category) or $isteacher) {
1204 echo "$string->email: <a href=\"mailto:$user->email\">$user->email</a><br />";
1205 }
1206 if ($user->city or $user->country) {
1207 echo "$string->location: $user->city, ".$countries["$user->country"]."<br />";
1208 }
1209 if ($user->lastaccess) {
1210 echo "$string->lastaccess: ".userdate($user->lastaccess);
1211 echo "&nbsp (".format_time(time() - $user->lastaccess, $datestring).")";
1212 } else {
1213 echo "$string->lastaccess: $string->never";
1214 }
1215 echo '</td><td valign="bottom" bgcolor="#ffffff" nowrap="nowrap" class="userinfoboxlinkcontent">';
1216
1217 echo '<font size="1">';
1218 if ($isteacher) {
1219 $timemidnight = usergetmidnight(time());
499795e8 1220 echo "<a href=\"$CFG->wwwroot/course/user.php?id=$course->id&user=$user->id\">$string->activity</a><br>";
951b22a8 1221 if (isstudent($course->id, $user->id) and !iscreator($user->id)) { // Includes admins
499795e8 1222 echo "<a href=\"$CFG->wwwroot/course/unenrol.php?id=$course->id&user=$user->id\">$string->unenrol</a><br />";
1223 echo "<a href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&user=$user->id\">$string->loginas</a><br />";
951b22a8 1224 }
1225 }
499795e8 1226 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$string->fullprofile...</a>";
951b22a8 1227 echo '</font>';
1228
1229 echo '</td></tr></table>';
1230}
1231
1232
f2c80965 1233function print_group_picture($group, $courseid, $large=false, $returnstring=false, $link=true) {
f374fb10 1234 global $CFG;
1235
97ea4833 1236 static $isteacheredit;
1237
1238 if (!isset($isteacheredit)) {
1239 $isteacheredit = isteacheredit($courseid);
1240 }
1241
1242 if ($group->hidepicture and !$isteacheredit) {
3c0561cf 1243 return '';
1244 }
c3cbfe7f 1245
97ea4833 1246 if ($link or $isteacheredit) {
1247 $output = "<a href=\"$CFG->wwwroot/course/group.php?id=$courseid&amp;group=$group->id\">";
3c0561cf 1248 } else {
1249 $output = '';
1250 }
1251 if ($large) {
1252 $file = "f1";
1253 $size = 100;
1254 } else {
1255 $file = "f2";
1256 $size = 35;
1257 }
1258 if ($group->picture) { // Print custom group picture
1259 if ($CFG->slasharguments) { // Use this method if possible for better caching
1260 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/user/pixgroup.php/$group->id/$file.jpg\"".
97ea4833 1261 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" title=\"$group->name\"/>";
f2c80965 1262 } else {
3c0561cf 1263 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/user/pixgroup.php?file=/$group->id/$file.jpg\"".
97ea4833 1264 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" title=\"$group->name\"/>";
f2c80965 1265 }
f374fb10 1266 }
97ea4833 1267 if ($link or $isteacheredit) {
3c0561cf 1268 $output .= "</a>";
1269 }
f374fb10 1270
1271 if ($returnstring) {
1272 return $output;
1273 } else {
1274 echo $output;
1275 }
1276}
1277
35067c43 1278
1279function print_png($url, $sizex, $sizey, $returnstring, $parameters='alt=""') {
1280 global $CFG;
1281 static $recentIE;
1282
1283 if (!isset($recentIE)) {
1284 $recentIE = check_browser_version('MSIE', '5.0');
1285 }
1286
1287 if ($recentIE) { // work around the HORRIBLE bug IE has with alpha transparencies
1288 $output .= "<img src=\"$CFG->pixpath/spacer.gif\" width=\"$sizex\" height=\"$sizey\"".
1289 " border=\"0\" style=\"width: {$sizex}px; height: {$sizey}px; ".
1290 " filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='$url', sizingMethod='scale') ".
1291 " $parameters />";
1292 } else {
1293 $output .= "<img src=\"$url\" border=\"0\" width=\"$sizex\" height=\"$sizey\" ".
1294 " $parameters />";
1295 }
1296
1297 if ($returnstring) {
1298 return $output;
1299 } else {
1300 echo $output;
1301 }
1302}
1303
1304
9fa49e22 1305function print_table($table) {
1306// Prints a nicely formatted table.
1307// $table is an object with several properties.
1308// $table->head is an array of heading names.
1309// $table->align is an array of column alignments
1310// $table->size is an array of column sizes
5867bfb5 1311// $table->wrap is an array of "nowrap"s or nothing
9fa49e22 1312// $table->data[] is an array of arrays containing the data.
1313// $table->width is an percentage of the page
1314// $table->cellpadding padding on each cell
1315// $table->cellspacing spacing between cells
1316
e21e20cf 1317 global $THEME;
1318
9fa49e22 1319 if (isset($table->align)) {
1320 foreach ($table->align as $key => $aa) {
1321 if ($aa) {
76c1650d 1322 $align[$key] = " align=\"$aa\"";
9fa49e22 1323 } else {
1324 $align[$key] = "";
1325 }
1326 }
1327 }
1328 if (isset($table->size)) {
1329 foreach ($table->size as $key => $ss) {
1330 if ($ss) {
76c1650d 1331 $size[$key] = " width=\"$ss\"";
9fa49e22 1332 } else {
1333 $size[$key] = "";
1334 }
1335 }
1336 }
5867bfb5 1337 if (isset($table->wrap)) {
1338 foreach ($table->wrap as $key => $ww) {
1339 if ($ww) {
66a51452 1340 $wrap[$key] = " nowrap=\"nowrap\" ";
5867bfb5 1341 } else {
1342 $wrap[$key] = "";
1343 }
1344 }
1345 }
9fa49e22 1346
9d378732 1347 if (empty($table->width)) {
9fa49e22 1348 $table->width = "80%";
1349 }
1350
9d378732 1351 if (empty($table->cellpadding)) {
9fa49e22 1352 $table->cellpadding = "5";
1353 }
1354
9d378732 1355 if (empty($table->cellspacing)) {
9fa49e22 1356 $table->cellspacing = "1";
1357 }
1358
5867bfb5 1359 print_simple_box_start("center", "$table->width", "#ffffff", 0);
66a51452 1360 echo "<table width=\"100%\" border=\"0\" valign=\"top\" align=\"center\" ";
9fa49e22 1361 echo " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"generaltable\">\n";
1362
e21e20cf 1363 $countcols = 0;
1364
b79f41cd 1365 if (!empty($table->head)) {
e21e20cf 1366 $countcols = count($table->head);;
5867bfb5 1367 echo "<tr>";
9fa49e22 1368 foreach ($table->head as $key => $heading) {
e21e20cf 1369
9d378732 1370 if (!isset($size[$key])) {
1371 $size[$key] = "";
1372 }
1373 if (!isset($align[$key])) {
1374 $align[$key] = "";
1375 }
66a51452 1376 echo "<th valign=\"top\" ".$align[$key].$size[$key]." nowrap=\"nowrap\" class=\"generaltableheader\">$heading</th>";
9fa49e22 1377 }
66a51452 1378 echo "</tr>\n";
9fa49e22 1379 }
1380
a1f8ff87 1381 if (!empty($table->data)) {
1382 foreach ($table->data as $row) {
1383 echo "<tr valign=\"top\">";
e21e20cf 1384 if ($row == "hr" and $countcols) {
6a903e0e 1385 echo "<td colspan=\"$countcols\"><div class=\"tabledivider\"></div></td>";
e21e20cf 1386 } else { /// it's a normal row of data
1387 foreach ($row as $key => $item) {
1388 if (!isset($size[$key])) {
1389 $size[$key] = "";
1390 }
1391 if (!isset($align[$key])) {
1392 $align[$key] = "";
1393 }
1394 if (!isset($wrap[$key])) {
1395 $wrap[$key] = "";
1396 }
1397 echo "<td ".$align[$key].$size[$key].$wrap[$key]." class=\"generaltablecell\">$item</td>";
1398 }
a1f8ff87 1399 }
1400 echo "</tr>\n";
9fa49e22 1401 }
9fa49e22 1402 }
5867bfb5 1403 echo "</table>\n";
9fa49e22 1404 print_simple_box_end();
1405
1406 return true;
1407}
1408
2f4d324b 1409function make_table($table) {
1410// Creates a nicely formatted table and returns it
1411// $table is an object with several properties.
1412// $table->head is an array of heading names.
1413// $table->align is an array of column alignments
1414// $table->size is an array of column sizes
1415// $table->wrap is an array of "nowrap"s or nothing
1416// $table->data[] is an array of arrays containing the data.
1417// $table->width is an percentage of the page
1418// $table->class is a class
1419// $table->fontsize is the size of all the text
1420// $table->tablealign align the whole table
1421// $table->cellpadding padding on each cell
1422// $table->cellspacing spacing between cells
1423
1424 if (isset($table->align)) {
1425 foreach ($table->align as $key => $aa) {
1426 if ($aa) {
1427 $align[$key] = " align=\"$aa\"";
1428 } else {
1429 $align[$key] = "";
1430 }
1431 }
1432 }
1433 if (isset($table->size)) {
1434 foreach ($table->size as $key => $ss) {
1435 if ($ss) {
1436 $size[$key] = " width=\"$ss\"";
1437 } else {
1438 $size[$key] = "";
1439 }
1440 }
1441 }
1442 if (isset($table->wrap)) {
1443 foreach ($table->wrap as $key => $ww) {
1444 if ($ww) {
66a51452 1445 $wrap[$key] = " nowrap=\"nowrap\" ";
2f4d324b 1446 } else {
1447 $wrap[$key] = "";
1448 }
1449 }
1450 }
1451
1452 if (empty($table->width)) {
1453 $table->width = "80%";
1454 }
1455
1456 if (empty($table->tablealign)) {
1457 $table->tablealign = "center";
1458 }
1459
1460 if (empty($table->cellpadding)) {
1461 $table->cellpadding = "5";
1462 }
1463
1464 if (empty($table->cellspacing)) {
1465 $table->cellspacing = "1";
1466 }
1467
1468 if (empty($table->class)) {
1469 $table->class = "generaltable";
1470 }
1471
1472 if (empty($table->fontsize)) {
1473 $fontsize = "";
1474 } else {
1475 $fontsize = "<font size=\"$table->fontsize\">";
1476 }
1477
66a51452 1478 $output = "<table width=\"$table->width\" valign=\"top\" align=\"$table->tablealign\" ";
2f4d324b 1479 $output .= " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"$table->class\">\n";
1480
1481 if (!empty($table->head)) {
1482 $output .= "<tr>";
1483 foreach ($table->head as $key => $heading) {
1484 if (!isset($size[$key])) {
1485 $size[$key] = "";
1486 }
1487 if (!isset($align[$key])) {
1488 $align[$key] = "";
1489 }
66a51452 1490 $output .= "<th valign=\"top\" ".$align[$key].$size[$key]." nowrap=\"nowrap\" class=\"{$table->class}header\">$fontsize$heading</th>";
2f4d324b 1491 }
1492 $output .= "</tr>\n";
1493 }
1494
1495 foreach ($table->data as $row) {
66a51452 1496 $output .= "<tr valign=\"top\">";
2f4d324b 1497 foreach ($row as $key => $item) {
1498 if (!isset($size[$key])) {
1499 $size[$key] = "";
1500 }
1501 if (!isset($align[$key])) {
1502 $align[$key] = "";
1503 }
1504 if (!isset($wrap[$key])) {
1505 $wrap[$key] = "";
1506 }
1507 $output .= "<td ".$align[$key].$size[$key].$wrap[$key]." class=\"{$table->class}cell\">$fontsize$item</td>";
1508 }
1509 $output .= "</tr>\n";
1510 }
1511 $output .= "</table>\n";
1512
1513 return $output;
1514}
1515
47037513 1516function print_textarea($usehtmleditor, $rows, $cols, $width, $height, $name, $value="", $courseid=0) {
1517/// Prints a basic textarea field
1518/// $width and height are legacy fields and no longer used
4c46c425 1519
47037513 1520 global $CFG, $course;
50bdc74d 1521
408e62f8 1522 if (empty($courseid)) {
50bdc74d 1523 if (!empty($course->id)) { // search for it in global context
1524 $courseid = $course->id;
1525 }
1526 }
9fa49e22 1527
47037513 1528 if ($usehtmleditor) {
1529 if (!empty($courseid) and isteacher($courseid)) {
1530 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/htmlarea.php?id=$courseid\"></script>\n";
1531 } else {
1532 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/htmlarea.php\"></script>\n";
1533 }
1534 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/dialog.js\"></script>\n";
1535 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/lang/en.php\"></script>\n";
1536 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/popupwin.js\"></script>\n";
50bdc74d 1537
47037513 1538 if ($rows < 20) {
1539 $rows = 20;
1540 }
1541 if ($cols < 65) {
1542 $cols = 65;
4c46c425 1543 }
9fa49e22 1544 }
47037513 1545
1546 echo "<textarea id=\"$name\" name=\"$name\" rows=\"$rows\" cols=\"$cols\" wrap=\"virtual\">";
1547 p($value);
1548 echo "</textarea>\n";
9fa49e22 1549}
1550
1551function print_richedit_javascript($form, $name, $source="no") {
47037513 1552/// Legacy function, provided for backward compatability
1553 use_html_editor($name);
1554}
1555
1556function use_html_editor($name="") {
1557/// Sets up the HTML editor on textareas in the current page.
1558/// If a field name is provided, then it will only be
1559/// applied to that field - otherwise it will be used
1560/// on every textarea in the page.
1561///
1562/// In most cases no arguments need to be supplied
4c46c425 1563
47037513 1564 echo "<script language=\"javascript\" type=\"text/javascript\" defer=\"1\">\n";
1565 if (empty($name)) {
1566 echo "HTMLArea.replaceAll();";
1567 } else {
1568 echo "HTMLArea.replace('$name')";
4c46c425 1569 }
47037513 1570 echo "</script>";
9fa49e22 1571}
1572
1573
1574function update_course_icon($courseid) {
1575// Used to be an icon, but it's now a simple form button
1576 global $CFG, $USER;
1577
b6c12732 1578 if (isteacheredit($courseid)) {
9c9f7d77 1579 if (!empty($USER->editing)) {
9fa49e22 1580 $string = get_string("turneditingoff");
1581 $edit = "off";
1582 } else {
1583 $string = get_string("turneditingon");
1584 $edit = "on";
1585 }
60b9a565 1586 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/view.php\">".
66a51452 1587 "<input type=\"hidden\" name=\"id\" value=\"$courseid\" />".
1588 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1589 "<input type=\"submit\" value=\"$string\" /></form>";
9fa49e22 1590 }
1591}
1592
1593function update_module_button($moduleid, $courseid, $string) {
1594// Prints the editing button on a module "view" page
1595 global $CFG;
1596
b6c12732 1597 if (isteacheredit($courseid)) {
9fa49e22 1598 $string = get_string("updatethis", "", $string);
60b9a565 1599 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/mod.php\">".
66a51452 1600 "<input type=\"hidden\" name=\"update\" value=\"$moduleid\" />".
1601 "<input type=\"hidden\" name=\"return\" value=\"true\" />".
1602 "<input type=\"submit\" value=\"$string\" /></form>";
b6c12732 1603 } else {
1604 return "";
9fa49e22 1605 }
1606}
1607
c2cb4545 1608function update_category_button($categoryid) {
d2b6ba70 1609// Prints the editing button on a category page
1610 global $CFG, $USER;
c2cb4545 1611
d2b6ba70 1612 if (iscreator()) {
f374fb10 1613 if (!empty($USER->categoryediting)) {
d2b6ba70 1614 $string = get_string("turneditingoff");
1615 $edit = "off";
1616 } else {
1617 $string = get_string("turneditingon");
1618 $edit = "on";
9b16d1ea 1619 }
60b9a565 1620 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/category.php\">".
66a51452 1621 "<input type=\"hidden\" name=\"id\" value=\"$categoryid\" />".
1622 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1623 "<input type=\"submit\" value=\"$string\" /></form>";
d2b6ba70 1624 }
1625}
1626
1627function update_categories_button() {
1628// Prints the editing button on categories listing
1629 global $CFG, $USER;
1630
1631 if (isadmin()) {
f374fb10 1632 if (!empty($USER->categoriesediting)) {
d2b6ba70 1633 $string = get_string("turneditingoff");
1634 $edit = "off";
1635 } else {
1636 $string = get_string("turneditingon");
1637 $edit = "on";
1638 }
60b9a565 1639 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/index.php\">".
66a51452 1640 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1641 "<input type=\"submit\" value=\"$string\" /></form>";
c2cb4545 1642 }
1643}
9fa49e22 1644
b3153e4b 1645function update_group_button($courseid, $groupid) {
f374fb10 1646// Prints the editing button on group page
1647 global $CFG, $USER;
1648
1649 if (isteacheredit($courseid)) {
b3153e4b 1650 $string = get_string('editgroupprofile');
f374fb10 1651 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/group.php\">".
1652 "<input type=\"hidden\" name=\"id\" value=\"$courseid\" />".
b3153e4b 1653 "<input type=\"hidden\" name=\"group\" value=\"$groupid\" />".
1654 "<input type=\"hidden\" name=\"edit\" value=\"on\" />".
f374fb10 1655 "<input type=\"submit\" value=\"$string\" /></form>";
1656 }
1657}
1658
1659function update_groups_button($courseid) {
1660// Prints the editing button on groups page
1661 global $CFG, $USER;
1662
1663 if (isteacheredit($courseid)) {
1664 if (!empty($USER->groupsediting)) {
1665 $string = get_string("turneditingoff");
1666 $edit = "off";
1667 } else {
1668 $string = get_string("turneditingon");
1669 $edit = "on";
1670 }
1671 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/groups.php\">".
1672 "<input type=\"hidden\" name=\"id\" value=\"$courseid\" />".
1673 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1674 "<input type=\"submit\" value=\"$string\" /></form>";
1675 }
1676}
1677
c3cbfe7f 1678function print_group_menu($groups, $groupmode, $currentgroup, $urlroot) {
1679/// Prints an appropriate group selection menu
1680
1d9fc417 1681/// Add an "All groups" to the start of the menu
743a92ff 1682 $groupsmenu[0] = get_string("allparticipants");
1d9fc417 1683 foreach ($groups as $key => $groupname) {
1684 $groupsmenu[$key] = $groupname;
1685 }
1686
e9a551b3 1687 echo '<table><tr><td align="right">';
c3cbfe7f 1688 if ($groupmode == VISIBLEGROUPS) {
1689 print_string('groupsvisible');
1690 } else {
1691 print_string('groupsseparate');
1692 }
1693 echo ':';
e9a551b3 1694 echo '</td><td nowrap="nowrap" align="left">';
1d9fc417 1695 popup_form($urlroot.'&group=', $groupsmenu, 'selectgroup', $currentgroup, "", "", "", false, "self");
c3cbfe7f 1696 echo '</tr></table>';
1697
1698}
1699
f2d91421 1700
16ef5e78 1701function navmenu($course, $cm=NULL, $targetwindow="self") {
9fa49e22 1702// Given a course and a (current) coursemodule
f2d91421 1703// This function returns a small popup menu with all the
9fa49e22 1704// course activity modules in it, as a navigation menu
f2d91421 1705// The data is taken from the serialised array stored in
9fa49e22 1706// the course record
1707
1708 global $CFG;
1709
1710 if ($cm) {
f2d91421 1711 $cm = $cm->id;
9fa49e22 1712 }
1713
1714 if ($course->format == 'weeks') {
1715 $strsection = get_string("week");
1716 } else {
1717 $strsection = get_string("topic");
1718 }
1719
1720 if (!$modinfo = unserialize($course->modinfo)) {
1721 return "";
1722 }
f2d91421 1723 $isteacher = isteacher($course->id);
9fa49e22 1724 $section = -1;
1725 $selected = "";
f2d91421 1726 $url = "";
1727 $previousmod = NULL;
1728 $backmod = NULL;
1729 $nextmod = NULL;
4866a367 1730 $selectmod = NULL;
3da47524 1731 $logslink = NULL;
f2d91421 1732 $flag = false;
1733
9fa49e22 1734 foreach ($modinfo as $mod) {
ab2df10c 1735 if ($mod->mod == "label") {
1736 continue;
1737 }
9fa49e22 1738 if ($mod->section > 0 and $section <> $mod->section) {
1739 $menu[] = "-------------- $strsection $mod->section --------------";
1740 }
1741 $section = $mod->section;
cf055081 1742 //Only add visible or teacher mods to jumpmenu
f2d91421 1743 if ($mod->visible or $isteacher) {
cf055081 1744 $url = "$mod->mod/view.php?id=$mod->cm";
f2d91421 1745 if ($flag) { // the current mod is the "next" mod
1746 $nextmod = $mod;
1747 $flag = false;
1748 }
cf055081 1749 if ($cm == $mod->cm) {
1750 $selected = $url;
3da47524 1751 $selectmod = $mod;
f2d91421 1752 $backmod = $previousmod;
1753 $flag = true; // set flag so we know to use next mod for "next"
cf055081 1754 }
1755 $mod->name = urldecode($mod->name);
1756 if (strlen($mod->name) > 55) {
1757 $mod->name = substr($mod->name, 0, 50)."...";
1758 }
2a409368 1759 if (!$mod->visible) {
1760 $mod->name = "(".$mod->name.")";
1761 }
f2d91421 1762 $menu[$url] = $mod->name;
db0d0337 1763 $previousmod = $mod;
9fa49e22 1764 }
f2d91421 1765 }
69d79bc3 1766 if ($selectmod and $isteacher) {
791b42ee 1767 $logslink = "<td><a target=\"$CFG->framename\" href=".
69d79bc3 1768 "\"$CFG->wwwroot/course/log.php?chooselog=1&user=0&date=0&id=$course->id&modid=$selectmod->cm\">".
1769 "<img border=\"0\" height=\"16\" width=\"16\" src=\"$CFG->pixpath/i/log.gif\"></a></td>";
3da47524 1770
1771 }
f2d91421 1772 if ($backmod) {
1773 $backmod = "<form action=\"$CFG->wwwroot/mod/$backmod->mod/view.php\" target=\"$CFG->framename\">".
1774 "<input type=\"hidden\" name=\"id\" value=\"$backmod->cm\">".
1775 "<input type=\"submit\" value=\"&lt;\"></form>";
1776 }
1777 if ($nextmod) {
1778 $nextmod = "<form action=\"$CFG->wwwroot/mod/$nextmod->mod/view.php\" target=\"$CFG->framename\">".
1779 "<input type=\"hidden\" name=\"id\" value=\"$nextmod->cm\">".
1780 "<input type=\"submit\" value=\"&gt;\"></form>";
1781 }
3da47524 1782 return "<table><tr>$logslink<td>$backmod</td><td>" .
f2d91421 1783 popup_form("$CFG->wwwroot/mod/", $menu, "navmenu", $selected, get_string("jumpto"),
1784 "", "", true, $targetwindow).
1785 "</td><td>$nextmod</td></tr></table>";
1786}
9fa49e22 1787
1788
1789function print_date_selector($day, $month, $year, $currenttime=0) {
1790// Currenttime is a default timestamp in GMT
1791// Prints form items with the names $day, $month and $year
1792
1793 if (!$currenttime) {
1794 $currenttime = time();
1795 }
1796 $currentdate = usergetdate($currenttime);
1797
1798 for ($i=1; $i<=31; $i++) {
1799 $days[$i] = "$i";
1800 }
1801 for ($i=1; $i<=12; $i++) {
39e018b3 1802 $months[$i] = userdate(gmmktime(12,0,0,$i,1,2000), "%B");
9fa49e22 1803 }
1804 for ($i=2000; $i<=2010; $i++) {
1805 $years[$i] = $i;
1806 }
47f1da80 1807 choose_from_menu($days, $day, $currentdate['mday'], "");
1808 choose_from_menu($months, $month, $currentdate['mon'], "");
1809 choose_from_menu($years, $year, $currentdate['year'], "");
9fa49e22 1810}
1811
6942583e 1812function print_time_selector($hour, $minute, $currenttime=0, $step=5) {
9fa49e22 1813// Currenttime is a default timestamp in GMT
1814// Prints form items with the names $hour and $minute
1815
1816 if (!$currenttime) {
1817 $currenttime = time();
1818 }
1819 $currentdate = usergetdate($currenttime);
6942583e 1820 if ($step != 1) {
1821 $currentdate['minutes'] = ceil($currentdate['minutes']/$step)*$step;
1822 }
9fa49e22 1823 for ($i=0; $i<=23; $i++) {
1824 $hours[$i] = sprintf("%02d",$i);
1825 }
6942583e 1826 for ($i=0; $i<=59; $i+=$step) {
9fa49e22 1827 $minutes[$i] = sprintf("%02d",$i);
1828 }
47f1da80 1829 choose_from_menu($hours, $hour, $currentdate['hours'], "");
1830 choose_from_menu($minutes, $minute, $currentdate['minutes'], "");
9fa49e22 1831}
1832
d6bdd9d5 1833function print_grade_menu($courseid, $name, $current, $includenograde=true) {
62ca135d 1834/// Prints a grade menu (as part of an existing form) with help
1835/// Showing all possible numerical grades and scales
1836
c9f6251e 1837 global $CFG;
62ca135d 1838
1839 $strscale = get_string("scale");
1840 $strscales = get_string("scales");
1841
1f7deef6 1842 $scales = get_scales_menu($courseid);
62ca135d 1843 foreach ($scales as $i => $scalename) {
1844 $grades[-$i] = "$strscale: $scalename";
1845 }
d6bdd9d5 1846 if ($includenograde) {
1847 $grades[0] = get_string("nograde");
1848 }
62ca135d 1849 for ($i=100; $i>=1; $i--) {
1850 $grades[$i] = $i;
1851 }
1852 choose_from_menu($grades, "$name", "$current", "");
1853
c9f6251e 1854 $helpicon = "$CFG->pixpath/help.gif";
66a51452 1855 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" alt=\"$strscales\" src=\"$helpicon\" />";
1856 link_to_popup_window ("/course/scales.php?id=$courseid&amp;list=true", "ratingscales",
62ca135d 1857 $linkobject, 400, 500, $strscales);
1858}
1859
02ebf404 1860function print_scale_menu($courseid, $name, $current) {
1861/// Prints a scale menu (as part of an existing form) including help button
62ca135d 1862/// Just like print_grade_menu but without the numerical grades
02ebf404 1863
c9f6251e 1864 global $CFG;
02ebf404 1865
1866 $strscales = get_string("scales");
1867 choose_from_menu(get_scales_menu($courseid), "$name", $current, "");
c9f6251e 1868 $helpicon = "$CFG->pixpath/help.gif";
66a51452 1869 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" alt=\"$strscales\" src=\"$helpicon\" />";
1870 link_to_popup_window ("/course/scales.php?id=$courseid&amp;list=true", "ratingscales",
02ebf404 1871 $linkobject, 400, 500, $strscales);
1872}
1873
fdc47ee6 1874
02ebf404 1875function print_scale_menu_helpbutton($courseid, $scale) {
1876/// Prints a help button about a scale
1877/// scale is an object
1878
c9f6251e 1879 global $CFG;
02ebf404 1880
1881 $strscales = get_string("scales");
c9f6251e 1882 $helpicon = "$CFG->pixpath/help.gif";
66a51452 1883 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" alt=\"$scale->name\" src=\"$helpicon\" />";
1884 link_to_popup_window ("/course/scales.php?id=$courseid&amp;list=true&amp;scale=$scale->id", "ratingscale",
02ebf404 1885 $linkobject, 400, 500, $scale->name);
1886}
1887
1888
9fa49e22 1889function error ($message, $link="") {
1890 global $CFG, $SESSION;
1891
1892 print_header(get_string("error"));
66a51452 1893 echo "<br />";
9fa49e22 1894 print_simple_box($message, "center", "", "#FFBBBB");
1895
1896 if (!$link) {
1897 if ( !empty($SESSION->fromurl) ) {
1898 $link = "$SESSION->fromurl";
1899 unset($SESSION->fromurl);
9fa49e22 1900 } else {
c2cb4545 1901 $link = "$CFG->wwwroot/";
9fa49e22 1902 }
1903 }
1904 print_continue($link);
1905 print_footer();
1906 die;
1907}
1908
1909function helpbutton ($page, $title="", $module="moodle", $image=true, $linktext=false, $text="") {
1910 // $page = the keyword that defines a help page
1911 // $title = the title of links, rollover tips, alt tags etc
1912 // $module = which module is the page defined in
1913 // $image = use a help image for the link? (true/false/"both")
1914 // $text = if defined then this text is used in the page, and
1915 // the $page variable is ignored.
dc0dc7d5 1916 global $CFG, $THEME;
9fa49e22 1917
1918 if ($module == "") {
1919 $module = "moodle";
1920 }
1921
1922 if ($image) {
c9f6251e 1923 $icon = "$CFG->pixpath/help.gif";
9fa49e22 1924 if ($linktext) {
7fc0f9e5 1925 $linkobject = "<span style=\"cursor:help;\">$title<img align=\"absmiddle\" border=\"0\" ".
1926 " height=\"17\" width=\"22\" alt=\"\" src=\"$icon\" /></span>";
9fa49e22 1927 } else {
7fc0f9e5 1928 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" ".
1929 " alt=\"$title\" style=\"cursor:help;\" src=\"$icon\" />";
9fa49e22 1930 }
1931 } else {
7fc0f9e5 1932 $linkobject = "<span style=\"cursor:help;\">$title</span>";
9fa49e22 1933 }
1934 if ($text) {
66a51452 1935 $url = "/help.php?module=$module&amp;text=".htmlentities(urlencode($text));
9fa49e22 1936 } else {
66a51452 1937 $url = "/help.php?module=$module&amp;file=$page.html";
9fa49e22 1938 }
1939 link_to_popup_window ($url, "popup", $linkobject, 400, 500, $title);
1940}
1941
e825f279 1942function emoticonhelpbutton($form, $field) {
1943/// Prints a special help button that is a link to the "live" emoticon popup
1944 global $CFG, $SESSION;
1945
1946 $SESSION->inserttextform = $form;
1947 $SESSION->inserttextfield = $field;
1948 helpbutton("emoticons", get_string("helpemoticons"), "moodle", false, true);
c9f6251e 1949 echo "&nbsp;";
66a51452 1950 link_to_popup_window ("/help.php?module=moodle&amp;file=emoticons.html", "popup",
1951 "<img src=\"$CFG->pixpath/s/smiley.gif\" border=\"0\" align=\"absmiddle\" width=\"15\" height=\"15\" />",
c9f6251e 1952 400, 500, get_string("helpemoticons"));
1953 echo "<br />";
e825f279 1954}
1955
9fa49e22 1956function notice ($message, $link="") {
750ab759 1957 global $CFG, $THEME;
9fa49e22 1958
1959 if (!$link) {
750ab759 1960 if (!empty($_SERVER["HTTP_REFERER"])) {
1961 $link = $_SERVER["HTTP_REFERER"];
1962 } else {
c2cb4545 1963 $link = "$CFG->wwwroot/";
750ab759 1964 }
9fa49e22 1965 }
1966
01d79966 1967 echo "<br />";
11a876e1 1968 print_simple_box($message, "center", "50%", "$THEME->cellheading", "20", "noticebox");
eb347b6b 1969 print_heading("<a href=\"$link\">".get_string("continue")."</a>");
9fa49e22 1970 print_footer(get_site());
1971 die;
1972}
1973
1974function notice_yesno ($message, $linkyes, $linkno) {
1975 global $THEME;
1976
eb347b6b 1977 print_simple_box_start("center", "60%", "$THEME->cellheading");
66a51452 1978 echo "<p align=\"center\"><font size=\"3\">$message</font></p>";
1979 echo "<p align=\"center\"><font size=\"3\"><b>";
eb347b6b 1980 echo "<a href=\"$linkyes\">".get_string("yes")."</a>";
9fa49e22 1981 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
eb347b6b 1982 echo "<a href=\"$linkno\">".get_string("no")."</a>";
1983 echo "</b></font></p>";
9fa49e22 1984 print_simple_box_end();
1985}
1986
559573a2 1987function redirect($url, $message="", $delay="0") {
5d6c043a 1988// Redirects the user to another page, after printing a notice
9fa49e22 1989
c9082a8c 1990 if (empty($message)) {
2b23eea3 1991 echo "<meta http-equiv=\"refresh\" content=\"$delay; url=$url\" />";
8a55c984 1992 echo "<script>location.replace('$url');</script>"; // To cope with Mozilla bug
c9082a8c 1993 } else {
05c19593 1994 if (empty($delay)) {
c9082a8c 1995 $delay = 3; // There's no point having a message with no delay
1996 }
5f546a46 1997 print_header("", "", "", "", "<meta http-equiv=\"refresh\" content=\"$delay; url=$url\" />");
76c1650d 1998 echo "<center>";
1999 echo "<p>$message</p>";
2000 echo "<p>( <a href=\"$url\">".get_string("continue")."</a> )</p>";
2001 echo "</center>";
ff019455 2002 flush();
2003 sleep($delay);
2004 echo "<script>location.replace('$url');</script>"; // To cope with Mozilla bug
9fa49e22 2005 }
2006 die;
2007}
2008
99988d1a 2009function notify ($message, $color="red", $align="center") {
2010 echo "<p align=\"$align\"><b><font color=\"$color\">$message</font></b></p>\n";
9fa49e22 2011}
2012
43373804 2013function obfuscate_email($email) {
2014/// Given an email address, this function will return an obfuscated version of it
2015 $i = 0;
2016 $length = strlen($email);
2017 $obfuscated = "";
2018 while ($i < $length) {
2019 if (rand(0,2)) {
2020 $obfuscated.='%'.dechex(ord($email{$i}));
2021 } else {
2022 $obfuscated.=$email{$i};
2023 }
2024 $i++;
2025 }
2026 return $obfuscated;
2027}
2028
2029function obfuscate_text($plaintext) {
2030/// This function takes some text and replaces about half of the characters
2031/// with HTML entity equivalents. Return string is obviously longer.
2032 $i=0;
2033 $length = strlen($plaintext);
2034 $obfuscated="";
2b09e377 2035 $prev_obfuscated = false;
43373804 2036 while ($i < $length) {
2b09e377 2037 $c = ord($plaintext{$i});
2038 $numerical = ($c >= ord('0')) && ($c <= ord('9'));
2039 if ($prev_obfuscated and $numerical ) {
2040 $obfuscated.='&#'.ord($plaintext{$i});
2041 } else if (rand(0,2)) {
43373804 2042 $obfuscated.='&#'.ord($plaintext{$i});
2b09e377 2043 $prev_obfuscated = true;
43373804 2044 } else {
2045 $obfuscated.=$plaintext{$i};
2b09e377 2046 $prev_obfuscated = false;
43373804 2047 }
2b09e377 2048 $i++;
43373804 2049 }
2050 return $obfuscated;
2051}
2052
cadb96f2 2053function obfuscate_mailto($email, $label="", $dimmed=false) {
43373804 2054/// This function uses the above two functions to generate a fully
2055/// obfuscated email link, ready to use.
2056
2057 if (empty($label)) {
2058 $label = $email;
2059 }
cadb96f2 2060 if ($dimmed) {
2061 $title = get_string('emaildisable');
2062 $dimmed = ' class="dimmed"';
2063 } else {
2064 $title = '';
2065 $dimmed = '';
2066 }
2067 return sprintf("<a href=\"%s:%s\" $dimmed title=\"$title\">%s</a>",
2068 obfuscate_text('mailto'), obfuscate_email($email),
2069 obfuscate_text($label));
43373804 2070}
2071
8b9c7aa0 2072function print_paging_bar($totalcount, $page, $perpage, $baseurl) {
2073/// Prints a single paging bar to provide access to other pages (usually in a search)
2074
519d369f 2075 $maxdisplay = 18;
8ef9cb56 2076
8b9c7aa0 2077 if ($totalcount > $perpage) {
f04dc61d 2078 echo "<center>";
2079 echo "<p>".get_string("page").":";
f374fb10 2080 if ($page > 0) {
2081 $pagenum=$page-1;
2082 echo "&nbsp;(<a href=\"{$baseurl}page=$pagenum\">".get_string("previous")."</a>)&nbsp;";
2083 }
be20753e 2084 $lastpage = ceil($totalcount / $perpage);
2085 if ($page > 15) {
2086 $startpage = $page - 10;
519d369f 2087 echo "&nbsp<a href=\"{$baseurl}page=0\">1</a>&nbsp;...";
be20753e 2088 } else {
2089 $startpage = 0;
2090 }
be20753e 2091 $currpage = $startpage;
2092 $displaycount = 0;
2093 while ($displaycount < $maxdisplay and $currpage < $lastpage) {
2094 $displaypage = $currpage+1;
2095 if ($page == $currpage) {
8b9c7aa0 2096 echo "&nbsp;&nbsp;$displaypage";
2097 } else {
be20753e 2098 echo "&nbsp;&nbsp;<a href=\"{$baseurl}page=$currpage\">$displaypage</a>";
e27dbcc8 2099 }
be20753e 2100 $displaycount++;
2101 $currpage++;
8b9c7aa0 2102 }
924cef21 2103 if ($currpage < $lastpage) {
519d369f 2104 $lastpageactual = $lastpage - 1;
2105 echo "&nbsp;...<a href=\"{$baseurl}page=$lastpageactual\">$lastpage</a>&nbsp;";
924cef21 2106 }
8b9c7aa0 2107 $pagenum = $page + 1;
be20753e 2108 if ($pagenum != $displaypage) {
8b9c7aa0 2109 echo "&nbsp;&nbsp;(<a href=\"{$baseurl}page=$pagenum\">".get_string("next")."</a>)";
2110 }
2111 echo "</p>";
be20753e 2112 echo "</center>";
8b9c7aa0 2113 }
2114}
9fa49e22 2115
9d5b689c 2116// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
f9903ed0 2117?>