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