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