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