Updated version with bug fixes (see bug 2179)
[moodle.git] / lib / weblib.php
blob6762790e308aa0dda3abed15a6d3ddf1383a3f6d
1 <?PHP // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // weblib.php - functions for web output
5 //
6 // Library of all general-purpose Moodle PHP functions and constants
7 // that produce HTML output
8 //
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 ///////////////////////////////////////////////////////////////////////////
32 /// Constants
34 /// Define text formatting types ... eventually we can add Wiki, BBcode etc
35 define("FORMAT_MOODLE", "0"); // Does all sorts of transformations and filtering
36 define("FORMAT_HTML", "1"); // Plain HTML (with some tags stripped)
37 define("FORMAT_PLAIN", "2"); // Plain text (even tags are printed in full)
38 define("FORMAT_WIKI", "3"); // Wiki-formatted text
39 define("FORMAT_MARKDOWN", "4"); // Markdown-formatted text http://daringfireball.net/projects/markdown/
41 $ALLOWED_TAGS =
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><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><tt><code>";
44 $ALLOWED_PROTOCOLS = array('http', 'https', 'ftp', 'news', 'mailto', 'rtsp', 'teamspeak', 'gopher', 'color');
47 /// Functions
49 function s($var) {
50 /// returns $var with HTML characters (like "<", ">", etc.) properly quoted,
52 if (empty($var)) {
53 return "";
55 return htmlSpecialChars(stripslashes_safe($var));
58 function p($var) {
59 /// prints $var with HTML characters (like "<", ">", etc.) properly quoted,
61 if (empty($var)) {
62 echo "";
64 echo htmlSpecialChars(stripslashes_safe($var));
67 function nvl(&$var, $default="") {
68 /// if $var is undefined, return $default, otherwise return $var
70 return isset($var) ? $var : $default;
73 function strip_querystring($url) {
74 /// takes a URL and returns it without the querystring portion
76 if ($commapos = strpos($url, '?')) {
77 return substr($url, 0, $commapos);
78 } else {
79 return $url;
83 function get_referer() {
84 /// returns the URL of the HTTP_REFERER, less the querystring portion
86 return strip_querystring(nvl($_SERVER["HTTP_REFERER"]));
90 function me() {
91 /// returns the name of the current script, WITH the querystring portion.
92 /// this function is necessary because PHP_SELF and REQUEST_URI and SCRIPT_NAME
93 /// return different things depending on a lot of things like your OS, Web
94 /// server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.)
96 if (!empty($_SERVER["REQUEST_URI"])) {
97 return $_SERVER["REQUEST_URI"];
99 } else if (!empty($_SERVER["PHP_SELF"])) {
100 if (!empty($_SERVER["QUERY_STRING"])) {
101 return $_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"];
103 return $_SERVER["PHP_SELF"];
105 } else if (!empty($_SERVER["SCRIPT_NAME"])) {
106 if (!empty($_SERVER["QUERY_STRING"])) {
107 return $_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
109 return $_SERVER["SCRIPT_NAME"];
111 } else if (!empty($_SERVER["URL"])) { // May help IIS (not well tested)
112 if (!empty($_SERVER["QUERY_STRING"])) {
113 return $_SERVER["URL"]."?".$_SERVER["QUERY_STRING"];
115 return $_SERVER["URL"];
117 } else {
118 notify("Warning: Could not find any of these web server variables: \$REQUEST_URI, \$PHP_SELF, \$SCRIPT_NAME or \$URL");
119 return false;
124 function qualified_me() {
125 /// like me() but returns a full URL
127 if (!empty($_SERVER["HTTP_HOST"])) {
128 $hostname = $_SERVER["HTTP_HOST"];
129 } else if (!empty($_ENV["HTTP_HOST"])) {
130 $hostname = $_ENV["HTTP_HOST"];
131 } else if (!empty($_SERVER["SERVER_NAME"])) {
132 $hostname = $_SERVER["SERVER_NAME"];
133 } else if (!empty($_ENV["SERVER_NAME"])) {
134 $hostname = $_ENV["SERVER_NAME"];
135 } else {
136 notify("Warning: could not find the name of this server!");
137 return false;
140 $protocol = (isset($_SERVER["HTTPS"]) and $_SERVER["HTTPS"] == "on") ? "https://" : "http://";
141 $url_prefix = $protocol.$hostname;
142 return $url_prefix . me();
146 function match_referer($goodreferer = "") {
147 /// returns true if the referer is the same as the goodreferer. If
148 /// goodreferer is not specified, use qualified_me as the goodreferer
149 global $CFG;
151 if (empty($CFG->secureforms)) { // Don't bother checking referer
152 return true;
155 if ($goodreferer == "nomatch") { // Don't bother checking referer
156 return true;
159 if (empty($goodreferer)) {
160 $goodreferer = qualified_me();
163 $referer = get_referer();
165 return (($referer == $goodreferer) or ($referer == "$CFG->wwwroot/"));
168 function data_submitted($url="") {
169 /// Used on most forms in Moodle to check for data
170 /// Returns the data as an object, if it's found.
171 /// This object can be used in foreach loops without
172 /// casting because it's cast to (array) automatically
174 /// Checks that submitted POST data exists, and also
175 /// checks the referer against the given url (it uses
176 /// the current page if none was specified.
178 global $CFG;
180 if (empty($_POST)) {
181 return false;
183 } else {
184 if (match_referer($url)) {
185 return (object)$_POST;
186 } else {
187 if ($CFG->debug > 10) {
188 notice("The form did not come from this page! (referer = ".get_referer().")");
190 return false;
195 function stripslashes_safe($string) {
196 /// stripslashes() removes ALL backslashes even from strings
197 /// so C:\temp becomes C:temp ... this isn't good.
198 /// The following should work as a fairly safe replacement
199 /// to be called on quoted AND unquoted strings (to be sure)
201 $string = str_replace("\\'", "'", $string);
202 $string = str_replace('\\"', '"', $string);
203 //$string = str_replace('\\\\', '\\', $string); // why?
204 return $string;
208 function break_up_long_words($string, $maxsize=20, $cutchar=' ') {
209 /// Given some normal text, this function will break up any
210 /// long words to a given size, by inserting the given character
212 if (in_array(current_language(), array('ja', 'zh_cn', 'zh_tw', 'zh_tw_utf8'))) { // Multibyte languages
213 return $string;
216 $output = '';
217 $length = strlen($string);
218 $wordlength = 0;
220 for ($i=0; $i<$length; $i++) {
221 $char = $string[$i];
222 if ($char == ' ' or $char == "\t" or $char == "\n" or $char == "\r") {
223 $wordlength = 0;
224 } else {
225 $wordlength++;
226 if ($wordlength > $maxsize) {
227 $output .= $cutchar;
228 $wordlength = 0;
231 $output .= $char;
233 return $output;
237 if (!function_exists('str_ireplace')) { /// Only exists in PHP 5
238 function str_ireplace($find, $replace, $string) {
239 /// This does a search and replace, ignoring case
240 /// This function is only used for versions of PHP older than version 5
241 /// which do not have a native version of this function.
242 /// Taken from the PHP manual, by bradhuizenga @ softhome.net
244 if (!is_array($find)) {
245 $find = array($find);
248 if(!is_array($replace)) {
249 if (!is_array($find)) {
250 $replace = array($replace);
251 } else {
252 // this will duplicate the string into an array the size of $find
253 $c = count($find);
254 $rString = $replace;
255 unset($replace);
256 for ($i = 0; $i < $c; $i++) {
257 $replace[$i] = $rString;
262 foreach ($find as $fKey => $fItem) {
263 $between = explode(strtolower($fItem),strtolower($string));
264 $pos = 0;
265 foreach($between as $bKey => $bItem) {
266 $between[$bKey] = substr($string,$pos,strlen($bItem));
267 $pos += strlen($bItem) + strlen($fItem);
269 $string = implode($replace[$fKey],$between);
271 return ($string);
275 if (!function_exists('stripos')) { /// Only exists in PHP 5
276 function stripos($haystack, $needle, $offset=0) {
277 /// This function is only used for versions of PHP older than version 5
278 /// which do not have a native version of this function.
279 /// Taken from the PHP manual, by dmarsh @ spscc.ctc.edu
280 return strpos(strtoupper($haystack), strtoupper($needle), $offset);
284 function read_template($filename, &$var) {
285 /// return a (big) string containing the contents of a template file with all
286 /// the variables interpolated. all the variables must be in the $var[] array or
287 /// object (whatever you decide to use).
289 /// WARNING: do not use this on big files!!
291 $temp = str_replace("\\", "\\\\", implode(file($filename), ""));
292 $temp = str_replace('"', '\"', $temp);
293 eval("\$template = \"$temp\";");
294 return $template;
297 function checked(&$var, $set_value = 1, $unset_value = 0) {
298 /// if variable is set, set it to the set_value otherwise set it to the
299 /// unset_value. used to handle checkboxes when you are expecting them from
300 /// a form
302 if (empty($var)) {
303 $var = $unset_value;
304 } else {
305 $var = $set_value;
309 function frmchecked(&$var, $true_value = "checked", $false_value = "") {
310 /// prints the word "checked" if a variable is true, otherwise prints nothing,
311 /// used for printing the word "checked" in a checkbox form input
313 if ($var) {
314 echo $true_value;
315 } else {
316 echo $false_value;
321 function link_to_popup_window ($url, $name="popup", $linkname="click here",
322 $height=400, $width=500, $title="Popup window", $options="none", $return=false) {
323 /// This will create a HTML link that will work on both
324 /// Javascript and non-javascript browsers.
325 /// Relies on the Javascript function openpopup in javascript.php
326 /// $url must be relative to home page eg /mod/survey/stuff.php
328 global $CFG;
330 if ($options == "none") {
331 $options = "menubar=0,location=0,scrollbars,resizable,width=$width,height=$height";
333 $fullscreen = 0;
335 $link = "<a target=\"$name\" title=\"$title\" href=\"$CFG->wwwroot$url\" ".
336 "onClick=\"return openpopup('$url', '$name', '$options', $fullscreen);\">$linkname</a>\n";
337 if ($return) {
338 return $link;
339 } else {
340 echo $link;
345 function button_to_popup_window ($url, $name="popup", $linkname="click here",
346 $height=400, $width=500, $title="Popup window", $options="none") {
347 /// This will create a HTML link that will work on both
348 /// Javascript and non-javascript browsers.
349 /// Relies on the Javascript function openpopup in javascript.php
350 /// $url must be relative to home page eg /mod/survey/stuff.php
352 global $CFG;
354 if ($options == "none") {
355 $options = "menubar=0,location=0,scrollbars,resizable,width=$width,height=$height";
357 $fullscreen = 0;
359 echo "<input type=\"button\" name=\"popupwindow\" title=\"$title\" value=\"$linkname ...\" ".
360 "onClick=\"return openpopup('$url', '$name', '$options', $fullscreen);\">\n";
364 function close_window_button() {
365 /// Prints a simple button to close a window
367 echo "<center>\n";
368 echo "<script>\n";
369 echo "<!--\n";
370 echo "document.write('<form>');\n";
371 echo "document.write('<input type=\"button\" onClick=\"self.close();\" value=\"".get_string("closewindow")."\" />');\n";
372 echo "document.write('</form>');\n";
373 echo "-->\n";
374 echo "</script>\n";
375 echo "<noscript>\n";
376 echo "<a href=\"".$_SERVER['HTTP_REFERER']."\"><---</a>\n";
377 echo "</noscript>\n";
378 echo "</center>\n";
382 function choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", $nothingvalue="0", $return=false) {
383 /// Given an array of value, creates a popup menu to be part of a form
384 /// $options["value"]["label"]
386 if ($nothing == "choose") {
387 $nothing = get_string("choose")."...";
390 if ($script) {
391 $javascript = "onChange=\"$script\"";
392 } else {
393 $javascript = "";
396 $output = "<select name=\"$name\" $javascript>\n";
397 if ($nothing) {
398 $output .= " <option value=\"$nothingvalue\"\n";
399 if ($nothingvalue === $selected) {
400 $output .= " selected=\"true\"";
402 $output .= ">$nothing</option>\n";
404 if (!empty($options)) {
405 foreach ($options as $value => $label) {
406 $output .= " <option value=\"$value\"";
407 if ($value == $selected) {
408 $output .= " selected=\"true\"";
410 if ($label === "") {
411 $output .= ">$value</option>\n";
412 } else {
413 $output .= ">$label</option>\n";
417 $output .= "</select>\n";
419 if ($return) {
420 return $output;
421 } else {
422 echo $output;
426 function popup_form ($common, $options, $formname, $selected="", $nothing="choose", $help="", $helptext="", $return=false, $targetwindow="self") {
427 /// Implements a complete little popup form
428 /// $common = the URL up to the point of the variable that changes
429 /// $options = A list of value-label pairs for the popup list
430 /// $formname = name must be unique on the page
431 /// $selected = the option that is already selected
432 /// $nothing = The label for the "no choice" option
433 /// $help = The name of a help page if help is required
434 /// $helptext = The name of the label for the help button
435 /// $return = Boolean indicating whether the function should return the text
436 /// as a string or echo it directly to the page being rendered
438 // TODO:
440 // * Make sure it's W3C conformant (<form name=""> has to go for example)
441 // * Code it in a way that doesn't require JS to be on. Example code:
442 // $selector .= '<form method="get" action="" style="display: inline;"><span>';
443 // $selector .= '<input type="hidden" name="var" value="value" />';
444 // if(!empty($morevars)) {
445 // $getarray = explode('&amp;', $morevars);
446 // foreach($getarray as $thisvar) {
447 // $selector .= '<input type="hidden" name="'.strtok($thisvar, '=').'" value="'.strtok('=').'" />';
448 // }
449 // }
450 // $selector .= '<select name="" onchange="form.submit();">';
451 // foreach($options as $id => $text) {
452 // $selector .= "\n<option value='$id'";
453 // if($option->id == $selected) {
454 // $selector .= ' selected';
455 // }
456 // $selector .= '>'.$text."</option>\n";
457 // }
458 // $selector .= '</select>';
459 // $selector .= '<noscript id="unique_id" style="display: inline;"> <input type="submit" value="'.get_string('somestring').'" /></noscript>';
460 // $selector .= '<script type="text/javascript">'."\n<!--\n".'document.getElementById("unique_id").style.display = "none";'."\n<!--\n".'</script>';
461 // $selector .= '</span></form>';
464 global $CFG;
466 if (empty($options)) {
467 return '';
470 if ($nothing == "choose") {
471 $nothing = get_string("choose")."...";
474 $startoutput = "<form method=\"get\" target=\"{$CFG->framename}\" name=\"$formname\">";
475 $output = "<select name=\"popup\" onchange=\"$targetwindow.location=document.$formname.popup.options[document.$formname.popup.selectedIndex].value\">\n";
477 if ($nothing != "") {
478 $output .= " <option value=\"javascript:void(0)\">$nothing</option>\n";
481 foreach ($options as $value => $label) {
482 if (substr($label,0,2) == "--") {
483 $output .= " <optgroup label=\"$label\"></optgroup>"; // Plain labels
484 continue;
485 } else {
486 $output .= " <option value=\"$common$value\"";
487 if ($value == $selected) {
488 $output .= " selected=\"true\"";
491 if ($label) {
492 $output .= ">$label</option>\n";
493 } else {
494 $output .= ">$value</option>\n";
497 $output .= "</select>";
498 $output .= "</form>\n";
500 if ($help) {
501 $button = helpbutton($help, $helptext, 'moodle', true, false, '', true);
502 } else {
503 $button = '';
506 if ($return) {
507 return $startoutput.$button.$output;
508 } else {
509 echo $startoutput.$button.$output;
515 function formerr($error) {
516 /// Prints some red text
517 if (!empty($error)) {
518 echo "<font color=\"#ff0000\">$error</font>";
523 function validate_email ($address) {
524 /// Validates an email to make sure it makes sense.
525 return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.
526 '@'.
527 '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
528 '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
529 $address));
532 function detect_munged_arguments($string, $allowdots=1) {
533 if (substr_count($string, '..') > $allowdots) { // Sometimes we allow dots in references
534 return true;
536 if (ereg('[\|\`]', $string)) { // check for other bad characters
537 return true;
539 if (empty($string) or $string == '/') {
540 return true;
543 return false;
546 function get_slash_arguments($file="file.php") {
547 /// Searches the current environment variables for some slash arguments
549 if (!$string = me()) {
550 return false;
553 $pathinfo = explode($file, $string);
555 if (!empty($pathinfo[1])) {
556 return addslashes($pathinfo[1]);
557 } else {
558 return false;
562 function parse_slash_arguments($string, $i=0) {
563 /// Extracts arguments from "/foo/bar/something"
564 /// eg http://mysite.com/script.php/foo/bar/something
566 if (detect_munged_arguments($string)) {
567 return false;
569 $args = explode("/", $string);
571 if ($i) { // return just the required argument
572 return $args[$i];
574 } else { // return the whole array
575 array_shift($args); // get rid of the empty first one
576 return $args;
580 function format_text_menu() {
581 /// Just returns an array of formats suitable for a popup menu
582 return array (FORMAT_MOODLE => get_string("formattext"),
583 FORMAT_HTML => get_string("formathtml"),
584 FORMAT_PLAIN => get_string("formatplain"),
585 FORMAT_WIKI => get_string("formatwiki"),
586 FORMAT_MARKDOWN => get_string("formatmarkdown"));
589 function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL ) {
590 /// Given text in a variety of format codings, this function returns
591 /// the text as safe HTML.
593 /// $text is raw text (originally from a user)
594 /// $format is one of the format constants, defined above
596 global $CFG, $course;
598 if (!empty($CFG->cachetext)) {
599 $time = time() - $CFG->cachetext;
600 $md5key = md5($text);
601 if ($cacheitem = get_record_select('cache_text', "md5key = '$md5key' AND timemodified > '$time'")) {
602 return $cacheitem->formattedtext;
606 if (empty($courseid)) {
607 if (!empty($course->id)) { // An ugly hack for better compatibility
608 $courseid = $course->id;
612 $CFG->currenttextiscacheable = true; // Default status - can be changed by any filter
614 switch ($format) {
615 case FORMAT_HTML:
616 replace_smilies($text);
617 if (!isset($options->noclean)) {
618 $text = clean_text($text, $format);
620 $text = filter_text($text, $courseid);
621 break;
623 case FORMAT_PLAIN:
624 $text = htmlentities($text);
625 $text = rebuildnolinktag($text);
626 $text = str_replace(" ", "&nbsp; ", $text);
627 $text = nl2br($text);
628 break;
630 case FORMAT_WIKI:
631 $text = wiki_to_html($text,$courseid);
632 $text = rebuildnolinktag($text);
633 if (!isset($options->noclean)) {
634 $text = clean_text($text, $format);
636 $text = filter_text($text, $courseid);
637 break;
639 case FORMAT_MARKDOWN:
640 $text = markdown_to_html($text);
641 if (!isset($options->noclean)) {
642 $text = clean_text($text, $format);
644 replace_smilies($text);
645 $text = filter_text($text, $courseid);
646 break;
648 default: // FORMAT_MOODLE or anything else
649 if (!isset($options->smiley)) {
650 $options->smiley=true;
652 if (!isset($options->para)) {
653 $options->para=true;
655 if (!isset($options->newlines)) {
656 $options->newlines=true;
658 $text = text_to_html($text, $options->smiley, $options->para, $options->newlines);
659 if (!isset($options->noclean)) {
660 $text = clean_text($text, $format);
662 $text = filter_text($text, $courseid);
663 break;
666 if (!empty($CFG->cachetext) and $CFG->currenttextiscacheable) {
667 $newrecord->md5key = $md5key;
668 $newrecord->formattedtext = addslashes($text);
669 $newrecord->timemodified = time();
670 @insert_record('cache_text', $newrecord);
673 return $text;
676 function format_text_email($text, $format) {
677 /// Given text in a variety of format codings, this function returns
678 /// the text as plain text suitable for plain email.
680 /// $text is raw text (originally from a user)
681 /// $format is one of the format constants, defined above
683 switch ($format) {
685 case FORMAT_PLAIN:
686 return $text;
687 break;
689 case FORMAT_WIKI:
690 $text = wiki_to_html($text);
691 /// This expression turns links into something nice in a text format. (Russell Jungwirth)
692 /// From: http://php.net/manual/en/function.eregi-replace.php and simplified
693 $text = eregi_replace('(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)','\\3 [ \\2 ]', $text);
694 return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES)));
695 break;
697 case FORMAT_HTML:
698 return html_to_text($text);
699 break;
701 case FORMAT_MOODLE:
702 case FORMAT_MARKDOWN:
703 default:
704 $text = eregi_replace('(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)','\\3 [ \\2 ]', $text);
705 return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES)));
706 break;
711 function filter_text($text, $courseid=NULL) {
712 /// Given some text in HTML format, this function will pass it
713 /// through any filters that have been defined in $CFG->textfilterx
714 /// The variable defines a filepath to a file containing the
715 /// filter function. The file must contain a variable called
716 /// $textfilter_function which contains the name of the function
717 /// with $courseid and $text parameters
719 global $CFG;
721 if (!empty($CFG->textfilters)) {
722 $textfilters = explode(',', $CFG->textfilters);
723 foreach ($textfilters as $textfilter) {
724 if (is_readable("$CFG->dirroot/$textfilter/filter.php")) {
725 include_once("$CFG->dirroot/$textfilter/filter.php");
726 $functionname = basename($textfilter).'_filter';
727 if (function_exists($functionname)) {
728 $text = $functionname($courseid, $text);
734 return $text;
738 function clean_text($text, $format=FORMAT_MOODLE) {
739 /// Given raw text (eg typed in by a user), this function cleans it up
740 /// and removes any nasty tags that could mess up Moodle pages.
742 global $ALLOWED_TAGS;
744 switch ($format) {
745 case FORMAT_PLAIN:
746 return $text;
748 default:
750 /// Remove tags that are not allowed
751 $text = strip_tags($text, $ALLOWED_TAGS);
753 /// Remove script events
754 $text = eregi_replace("([^a-z])language([[:space:]]*)=", "\\1Xlanguage=", $text);
755 $text = eregi_replace("([^a-z])on([a-z]+)([[:space:]]*)=", "\\1Xon\\2=", $text);
757 /// Clean up embedded scripts and , using kses
758 $text = cleanAttributes($text);
760 return $text;
765 function cleanAttributes($str){
766 /// This function takes a string and examines it for html tags.
767 /// If tags are detected it passes the string to a helper function cleanAttributes2
768 /// which checks for attributes and filters them for malicious content
769 /// 17/08/2004 :: Eamon DOT Costello AT dcu DOT ie
770 $result = preg_replace(
771 '%(<[^>]*(>|$)|>)%me', #search for html tags
772 "cleanAttributes2('\\1')",
773 $str
775 return $result;
779 function cleanAttributes2($htmlTag){
780 /// This function takes a string with an html tag and strips out any unallowed
781 /// protocols e.g. javascript:
782 /// It calls ancillary functions in kses which are prefixed by kses
783 /// 17/08/2004 :: Eamon DOT Costello AT dcu DOT ie
785 global $CFG, $ALLOWED_PROTOCOLS;
787 require_once("$CFG->libdir/kses.php");
789 $htmlTag = kses_stripslashes($htmlTag);
790 if (substr($htmlTag, 0, 1) != '<') {
791 return '&gt;'; //a single character ">" detected
793 if (!preg_match('%^<\s*(/\s*)?([a-zA-Z0-9]+)([^>]*)>?$%', $htmlTag, $matches)) {
794 return ''; // It's seriously malformed
796 $slash = trim($matches[1]); //trailing xhtml slash
797 $elem = $matches[2]; //the element name
798 $attrlist = $matches[3]; // the list of attributes as a string
800 $attrArray = kses_hair($attrlist, $ALLOWED_PROTOCOLS);
802 $attStr = '';
803 foreach ($attrArray as $arreach) {
804 $attStr .= ' '.strtolower($arreach['name']).'="'.$arreach['value'].'" ';
806 $xhtml_slash = '';
807 if (preg_match('%/\s*$%', $attrlist)) {
808 $xhtml_slash = ' /';
810 return "<$slash$elem$attStr$xhtml_slash>";
814 function replace_smilies(&$text) {
815 /// Replaces all known smileys in the text with image equivalents
816 global $CFG;
818 /// this builds the mapping array only once
819 static $runonce = false;
820 static $e = array();
821 static $img = array();
822 static $emoticons = array(
823 ':-)' => 'smiley',
824 ':)' => 'smiley',
825 ':-D' => 'biggrin',
826 ';-)' => 'wink',
827 ':-/' => 'mixed',
828 'V-.' => 'thoughtful',
829 ':-P' => 'tongueout',
830 'B-)' => 'cool',
831 '^-)' => 'approve',
832 '8-)' => 'wideeyes',
833 ':o)' => 'clown',
834 ':-(' => 'sad',
835 ':(' => 'sad',
836 '8-.' => 'shy',
837 ':-I' => 'blush',
838 ':-X' => 'kiss',
839 '8-o' => 'surprise',
840 'P-|' => 'blackeye',
841 '8-[' => 'angry',
842 'xx-P' => 'dead',
843 '|-.' => 'sleepy',
844 '}-]' => 'evil',
847 if ($runonce == false) { /// After the first time this is not run again
848 foreach ($emoticons as $emoticon => $image){
849 $alttext = get_string($image, 'pix');
851 $e[] = $emoticon;
852 $img[] = "<img alt=\"$alttext\" width=\"15\" height=\"15\" src=\"$CFG->pixpath/s/$image.gif\" />";
854 $runonce = true;
857 // Exclude from transformations all the code inside <script> tags
858 // Needed to solve Bug 1185. Thanks to jouse 2001 detecting it. :-)
859 // Based on code from glossary fiter by Williams Castillo.
860 // - Eloy
862 // Detect all the <script> zones to take out
863 $excludes = array();
864 preg_match_all('/<script language(.+?)<\/script>/is',$text,$list_of_excludes);
866 // Take out all the <script> zones from text
867 foreach (array_unique($list_of_excludes[0]) as $key=>$value) {
868 $excludes['<+'.$key.'+>'] = $value;
870 if ($excludes) {
871 $text = str_replace($excludes,array_keys($excludes),$text);
874 /// this is the meat of the code - this is run every time
875 $text = str_replace($e, $img, $text);
877 // Recover all the <script> zones to text
878 if ($excludes) {
879 $text = str_replace(array_keys($excludes),$excludes,$text);
883 function text_to_html($text, $smiley=true, $para=true, $newlines=true) {
884 /// Given plain text, makes it into HTML as nicely as possible.
885 /// May contain HTML tags already
887 global $CFG;
889 /// Remove any whitespace that may be between HTML tags
890 $text = eregi_replace(">([[:space:]]+)<", "><", $text);
892 /// Remove any returns that precede or follow HTML tags
893 $text = eregi_replace("([\n\r])<", " <", $text);
894 $text = eregi_replace(">([\n\r])", "> ", $text);
896 convert_urls_into_links($text);
898 /// Make returns into HTML newlines.
899 if ($newlines) {
900 $text = nl2br($text);
903 /// Turn smileys into images.
904 if ($smiley) {
905 replace_smilies($text);
908 /// Wrap the whole thing in a paragraph tag if required
909 if ($para) {
910 return "<p>".$text."</p>";
911 } else {
912 return $text;
916 function wiki_to_html($text,$courseid) {
917 /// Given Wiki formatted text, make it into XHTML using external function
918 global $CFG;
920 require_once("$CFG->libdir/wiki.php");
922 $wiki = new Wiki;
923 return $wiki->format($text,$courseid);
926 function markdown_to_html($text) {
927 /// Given Markdown formatted text, make it into XHTML using external function
928 global $CFG;
930 require_once("$CFG->libdir/markdown.php");
932 return Markdown($text);
935 function html_to_text($html) {
936 /// Given HTML text, make it into plain text using external function
937 global $CFG;
939 require_once("$CFG->libdir/html2text.php");
941 return html2text($html);
945 function convert_urls_into_links(&$text) {
946 /// Given some text, it converts any URLs it finds into HTML links.
948 /// Make lone URLs into links. eg http://moodle.com/
949 $text = eregi_replace("([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
950 "\\1<a href=\"\\2://\\3\\4\" target=\"newpage\">\\2://\\3\\4</a>", $text);
952 /// eg www.moodle.com
953 $text = eregi_replace("([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])",
954 "\\1<a href=\"http://www.\\2\\3\" target=\"newpage\">www.\\2\\3</a>", $text);
957 function highlight($needle, $haystack, $case=0,
958 $left_string="<span class=\"highlight\">", $right_string="</span>") {
959 /// This function will highlight search words in a given string
960 /// It cares about HTML and will not ruin links. It's best to use
961 /// this function after performing any conversions to HTML.
962 /// Function found here: http://forums.devshed.com/t67822/scdaa2d1c3d4bacb4671d075ad41f0854.html
964 if (empty($needle)) {
965 return $haystack;
968 $list_of_words = eregi_replace("[^-a-zA-Z0-9&']", " ", $needle);
969 $list_array = explode(" ", $list_of_words);
970 for ($i=0; $i<sizeof($list_array); $i++) {
971 if (strlen($list_array[$i]) == 1) {
972 $list_array[$i] = "";
975 $list_of_words = implode(" ", $list_array);
976 $list_of_words_cp = $list_of_words;
977 $final = array();
978 preg_match_all('/<(.+?)>/is',$haystack,$list_of_words);
980 foreach (array_unique($list_of_words[0]) as $key=>$value) {
981 $final['<|'.$key.'|>'] = $value;
984 $haystack = str_replace($final,array_keys($final),$haystack);
985 $list_of_words_cp = eregi_replace(" +", "|", $list_of_words_cp);
987 if ($list_of_words_cp{0}=="|") {
988 $list_of_words_cp{0} = "";
990 if ($list_of_words_cp{strlen($list_of_words_cp)-1}=="|") {
991 $list_of_words_cp{strlen($list_of_words_cp)-1}="";
993 $list_of_words_cp = "(".trim($list_of_words_cp).")";
995 if (!$case){
996 $haystack = eregi_replace("$list_of_words_cp", "$left_string"."\\1"."$right_string", $haystack);
997 } else {
998 $haystack = ereg_replace("$list_of_words_cp", "$left_string"."\\1"."$right_string", $haystack);
1000 $haystack = str_replace(array_keys($final),$final,$haystack);
1002 return $haystack;
1005 function highlightfast($needle, $haystack) {
1006 /// This function will highlight instances of $needle in $haystack
1007 /// It's faster that the above function and doesn't care about
1008 /// HTML or anything.
1010 $parts = explode(strtolower($needle), strtolower($haystack));
1012 $pos = 0;
1014 foreach ($parts as $key => $part) {
1015 $parts[$key] = substr($haystack, $pos, strlen($part));
1016 $pos += strlen($part);
1018 $parts[$key] .= "<span class=\"highlight\">".substr($haystack, $pos, strlen($needle))."</span>";
1019 $pos += strlen($needle);
1022 return (join('', $parts));
1026 /// STANDARD WEB PAGE PARTS ///////////////////////////////////////////////////
1028 function print_header ($title="", $heading="", $navigation="", $focus="", $meta="",
1029 $cache=true, $button="&nbsp;", $menu="", $usexml=false, $bodytags="") {
1030 // $title - appears top of window
1031 // $heading - appears top of page
1032 // $navigation - premade navigation string
1033 // $focus - indicates form element eg inputform.password
1034 // $meta - meta tags in the header
1035 // $cache - should this page be cacheable?
1036 // $button - HTML code for a button (usually for module editing)
1037 // $menu - HTML code for a popup menu
1038 // $usexml - use XML for this page
1039 // $bodytags - this text will be included verbatim in the <body> tag (useful for onload() etc)
1041 global $USER, $CFG, $THEME, $SESSION;
1043 global $course; // This is a bit of an ugly hack to be gotten rid of later
1044 if (!empty($course->lang)) {
1045 $CFG->courselang = $course->lang;
1048 if (file_exists("$CFG->dirroot/theme/$CFG->theme/styles.php")) {
1049 $styles = $CFG->stylesheet;
1050 } else {
1051 $styles = "$CFG->wwwroot/theme/standard/styles.php";
1054 if ($navigation == "home") {
1055 $home = true;
1056 $navigation = "";
1057 } else {
1058 $home = false;
1061 if ($button == "") {
1062 $button = "&nbsp;";
1065 if (!$menu and $navigation) {
1066 if (empty($CFG->loginhttps)) {
1067 $wwwroot = $CFG->wwwroot;
1068 } else {
1069 $wwwroot = str_replace('http','https',$CFG->wwwroot);
1071 if (isset($USER->id)) {
1072 $menu = "<font size=\"2\"><a target=\"$CFG->framename\" href=\"$wwwroot/login/logout.php\">".get_string("logout")."</a></font>";
1073 } else {
1074 $menu = "<font size=\"2\"><a target=\"$CFG->framename\" href=\"$wwwroot/login/index.php\">".get_string("login")."</a></font>";
1078 if (isset($SESSION->justloggedin)) {
1079 unset($SESSION->justloggedin);
1080 if (!empty($CFG->displayloginfailures)) {
1081 if (!empty($USER->username) and !isguest()) {
1082 if ($count = count_login_failures($CFG->displayloginfailures, $USER->username, $USER->lastlogin)) {
1083 $menu .= '&nbsp;<font size="1">';
1084 if (empty($count->accounts)) {
1085 $menu .= get_string('failedloginattempts', '', $count);
1086 } else {
1087 $menu .= get_string('failedloginattemptsall', '', $count);
1089 if (isadmin()) {
1090 $menu .= ' (<a href="'.$CFG->wwwroot.'/course/log.php'.
1091 '?chooselog=1&id=1&modid=site_errors">'.get_string('logs').'</a>)';
1093 $menu .= '</font>';
1099 // Add a stylesheet for the HTML editor
1100 $meta = "<style type=\"text/css\">@import url($CFG->wwwroot/lib/editor/htmlarea.css);</style>\n$meta\n";
1102 if (!empty($CFG->unicode)) {
1103 $encoding = "utf-8";
1104 } else if (!empty($CFG->courselang)) {
1105 $encoding = get_string("thischarset");
1106 moodle_setlocale();
1107 } else {
1108 if (!empty($SESSION->encoding)) {
1109 $encoding = $SESSION->encoding;
1110 } else {
1111 $SESSION->encoding = $encoding = get_string("thischarset");
1114 $meta = "<meta http-equiv=\"content-type\" content=\"text/html; charset=$encoding\" />\n$meta\n";
1115 if (!$usexml) {
1116 @header('Content-type: text/html; charset='.$encoding);
1119 if ( get_string("thisdirection") == "rtl" ) {
1120 $direction = " dir=\"rtl\"";
1121 } else {
1122 $direction = " dir=\"ltr\"";
1125 if (!$cache) { // Do everything we can to prevent clients and proxies caching
1126 @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
1127 @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
1128 @header('Cache-Control: no-store, no-cache, must-revalidate');
1129 @header('Cache-Control: post-check=0, pre-check=0', false);
1130 @header('Pragma: no-cache');
1132 $meta .= "\n<meta http-equiv=\"pragma\" content=\"no-cache\" />";
1133 $meta .= "\n<meta http-equiv=\"expires\" content=\"0\" />";
1136 if ($usexml) { // Added by Gustav Delius / Mad Alex for MathML output
1137 $currentlanguage = current_language();
1139 @header("Content-type: text/xml");
1140 echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n";
1141 if (!empty($CFG->xml_stylesheets)) {
1142 $stylesheets = explode(";", $CFG->xml_stylesheets);
1143 foreach ($stylesheets as $stylesheet) {
1144 echo "<?xml-stylesheet type=\"text/xsl\" href=\"$CFG->wwwroot/$stylesheet\" ?>\n";
1147 echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1";
1148 if (!empty($CFG->xml_doctype_extra)) {
1149 echo " plus $CFG->xml_doctype_extra";
1151 echo "//" . strtoupper($currentlanguage) . "\" \"$CFG->xml_dtd\">\n";
1152 $direction = " xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"$currentlanguage\" $direction";
1155 $title = str_replace('"', '&quot;', $title);
1156 $title = strip_tags($title);
1158 include ("$CFG->dirroot/theme/$CFG->theme/header.html");
1162 function print_header_simple($title="", $heading="", $navigation="", $focus="", $meta="",
1163 $cache=true, $button="&nbsp;", $menu="", $usexml=false, $bodytags="") {
1164 /// This version of print_header is simpler because the course name does not have to be
1165 /// provided explicitly in the strings. It can be used on the site page as in courses
1166 /// Eventually all print_header could be replaced by print_header_simple
1168 global $course; // The same hack is used in print_header
1170 $shortname ='';
1171 if ($course->category) {
1172 $shortname = "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->";
1175 print_header("$course->shortname: $title", "$course->fullname $heading", "$shortname $navigation", $focus, $meta,
1176 $cache, $button, $menu, $usexml, $bodytags);
1181 function print_footer ($course=NULL, $usercourse=NULL) {
1182 // Can provide a course object to make the footer contain a link to
1183 // to the course home page, otherwise the link will go to the site home
1184 global $USER, $CFG, $THEME;
1186 /// Course links
1187 if ($course) {
1188 if ($course == "home") { // special case for site home page - please do not remove
1189 $homelink = "<p align=\"center\"><a title=\"moodle $CFG->release ($CFG->version)\" href=\"http://moodle.org/\" target=\"_blank\">";
1190 $homelink .= "<br /><img width=\"100\" height=\"30\" src=\"pix/moodlelogo.gif\" border=\"0\" /></a></p>";
1191 $course = get_site();
1192 $homepage = true;
1193 } else {
1194 $homelink = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>";
1196 } else {
1197 $homelink = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/\">".get_string("home")."</a>";
1198 $course = get_site();
1201 if (!$usercourse) {
1202 $usercourse = $course;
1205 /// User links
1206 $loggedinas = user_login_string($usercourse, $USER);
1208 include ("$CFG->dirroot/theme/$CFG->theme/footer.html");
1211 function style_sheet_setup($lastmodified=0, $lifetime=300, $themename="") {
1212 /// This function is called by stylesheets to set up the header
1213 /// approriately as well as the current path
1215 global $CFG;
1217 header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
1218 header("Expires: " . gmdate("D, d M Y H:i:s", time() + $lifetime) . " GMT");
1219 header("Cache-control: max_age = $lifetime");
1220 header("Pragma: ");
1221 header("Content-type: text/css"); // Correct MIME type
1223 if (!empty($themename)) {
1224 $CFG->theme = $themename;
1227 return "$CFG->wwwroot/theme/$CFG->theme";
1232 function user_login_string($course, $user=NULL) {
1233 global $USER, $CFG;
1235 if (empty($user)) {
1236 $user = $USER;
1239 if (isset($user->realuser)) {
1240 if ($realuser = get_record("user", "id", $user->realuser)) {
1241 $fullname = fullname($realuser, true);
1242 $realuserinfo = " [<a target=\"{$CFG->framename}\"
1243 href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&amp;return=$realuser->id\">$fullname</a>] ";
1245 } else {
1246 $realuserinfo = "";
1249 if (empty($CFG->loginhttps)) {
1250 $wwwroot = $CFG->wwwroot;
1251 } else {
1252 $wwwroot = str_replace('http','https',$CFG->wwwroot);
1255 if (isset($user->id) and $user->id) {
1256 $fullname = fullname($user, true);
1257 $username = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">$fullname</a>";
1258 if (isguest($user->id)) {
1259 $loggedinas = $realuserinfo.get_string("loggedinas", "moodle", "$username").
1260 " (<a target=\"{$CFG->framename}\" href=\"$wwwroot/login/index.php\">".get_string("login")."</a>)";
1261 } else {
1262 $loggedinas = $realuserinfo.get_string("loggedinas", "moodle", "$username").
1263 " (<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</a>)";
1265 } else {
1266 $loggedinas = get_string("loggedinnot", "moodle").
1267 " (<a target=\"{$CFG->framename}\" href=\"$wwwroot/login/index.php\">".get_string("login")."</a>)";
1269 return $loggedinas;
1273 function print_navigation ($navigation) {
1274 global $CFG;
1276 if ($navigation) {
1277 if (! $site = get_site()) {
1278 $site->shortname = get_string("home");;
1280 $navigation = str_replace('->', '&raquo;', $navigation);
1281 echo "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/\">$site->shortname</a> &raquo; $navigation";
1285 function print_headline($text, $size=2) {
1286 echo "<b><font size=\"$size\">$text</font></b><br />\n";
1289 function print_heading($text, $align="center", $size=3) {
1290 echo "<p align=\"$align\"><font size=\"$size\"><b>".stripslashes_safe($text)."</b></font></p>";
1293 function print_heading_with_help($text, $helppage, $module="moodle", $icon="") {
1294 // Centered heading with attached help button (same title text)
1295 // and optional icon attached
1296 echo "<p align=\"center\"><font size=\"3\">$icon<b>".stripslashes_safe($text);
1297 helpbutton($helppage, $text, $module);
1298 echo "</b></font></p>";
1301 function print_continue($link) {
1303 global $CFG;
1305 if (!$link) {
1306 $link = $_SERVER["HTTP_REFERER"];
1309 print_heading("<a target=\"{$CFG->framename}\" href=\"$link\">".get_string("continue")."</a>");
1313 function print_simple_box($message, $align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
1314 print_simple_box_start($align, $width, $color, $padding, $class);
1315 echo stripslashes_safe($message);
1316 print_simple_box_end();
1319 function print_simple_box_start($align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") {
1320 global $THEME;
1322 if ($align) {
1323 $align = "align=\"$align\"";
1325 if ($width) {
1326 $width = "width=\"$width\"";
1328 echo "<table $align $width class=\"$class\" border=\"0\" cellpadding=\"$padding\" cellspacing=\"0\"><tr><td bgcolor=\"$color\" class=\"$class"."content\">";
1331 function print_simple_box_end() {
1332 echo "</td></tr></table>";
1335 function print_single_button($link, $options, $label="OK", $method="get") {
1336 echo "<form action=\"$link\" method=\"$method\">";
1337 if ($options) {
1338 foreach ($options as $name => $value) {
1339 echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
1342 echo "<input type=\"submit\" value=\"$label\" /></form>";
1345 function print_spacer($height=1, $width=1, $br=true) {
1346 global $CFG;
1347 echo "<img height=\"$height\" width=\"$width\" src=\"$CFG->wwwroot/pix/spacer.gif\" alt=\"\" />";
1348 if ($br) {
1349 echo "<br />\n";
1353 function print_file_picture($path, $courseid=0, $height="", $width="", $link="") {
1354 // Given the path to a picture file in a course, or a URL,
1355 // this function includes the picture in the page.
1356 global $CFG;
1358 if ($height) {
1359 $height = "height=\"$height\"";
1361 if ($width) {
1362 $width = "width=\"$width\"";
1364 if ($link) {
1365 echo "<a href=\"$link\">";
1367 if (substr(strtolower($path), 0, 7) == "http://") {
1368 echo "<img border=\"0\" $height $width src=\"$path\" />";
1370 } else if ($courseid) {
1371 echo "<img border=\"0\" $height $width src=\"";
1372 if ($CFG->slasharguments) { // Use this method if possible for better caching
1373 echo "$CFG->wwwroot/file.php/$courseid/$path";
1374 } else {
1375 echo "$CFG->wwwroot/file.php?file=/$courseid/$path";
1377 echo "\" />";
1378 } else {
1379 echo "Error: must pass URL or course";
1381 if ($link) {
1382 echo "</a>";
1386 function print_user_picture($userid, $courseid, $picture, $large=false, $returnstring=false, $link=true) {
1387 global $CFG;
1389 if ($link) {
1390 $output = "<a href=\"$CFG->wwwroot/user/view.php?id=$userid&amp;course=$courseid\">";
1391 } else {
1392 $output = "";
1394 if ($large) {
1395 $file = "f1";
1396 $size = 100;
1397 } else {
1398 $file = "f2";
1399 $size = 35;
1401 if ($picture) { // Print custom user picture
1402 if ($CFG->slasharguments) { // Use this method if possible for better caching
1403 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/user/pix.php/$userid/$file.jpg\"".
1404 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" />";
1405 } else {
1406 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/user/pix.php?file=/$userid/$file.jpg\"".
1407 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" />";
1409 } else { // Print default user pictures (use theme version if available)
1410 $output .= "<img align=\"absmiddle\" src=\"$CFG->pixpath/u/$file.png\"".
1411 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" />";
1413 if ($link) {
1414 $output .= "</a>";
1417 if ($returnstring) {
1418 return $output;
1419 } else {
1420 echo $output;
1424 function print_user($user, $course) {
1425 /// Prints a summary of a user in a nice little box
1427 global $CFG,$USER;
1429 static $string;
1430 static $datestring;
1431 static $countries;
1432 static $isteacher;
1434 if (empty($string)) { // Cache all the strings for the rest of the page
1436 $string->email = get_string("email");
1437 $string->location = get_string("location");
1438 $string->lastaccess = get_string("lastaccess");
1439 $string->activity = get_string("activity");
1440 $string->unenrol = get_string("unenrol");
1441 $string->loginas = get_string("loginas");
1442 $string->fullprofile = get_string("fullprofile");
1443 $string->role = get_string("role");
1444 $string->name = get_string("name");
1445 $string->never = get_string("never");
1447 $datestring->day = get_string("day");
1448 $datestring->days = get_string("days");
1449 $datestring->hour = get_string("hour");
1450 $datestring->hours = get_string("hours");
1451 $datestring->min = get_string("min");
1452 $datestring->mins = get_string("mins");
1453 $datestring->sec = get_string("sec");
1454 $datestring->secs = get_string("secs");
1456 $countries = get_list_of_countries();
1458 $isteacher = isteacher($course->id);
1461 echo '<table width="80%" align="center" border="0" cellpadding="10" cellspacing="0" class="userinfobox">';
1462 echo '<tr>';
1463 echo '<td width="100" bgcolor="#ffffff" valign="top" class="userinfoboxside">';
1464 print_user_picture($user->id, $course->id, $user->picture, true);
1465 echo '</td>';
1466 echo '<td width="100%" bgcolor="#ffffff" valign="top" class="userinfoboxsummary">';
1467 echo '<font size="-1">';
1468 echo '<font size="3"><b>'.fullname($user, $isteacher).'</b></font>';
1469 echo '<p>';
1470 if (!empty($user->role) and ($user->role <> $course->teacher)) {
1471 echo "$string->role: $user->role<br />";
1473 if ($user->maildisplay == 1 or ($user->maildisplay == 2 and $course->category and !isguest()) or $isteacher) {
1474 echo "$string->email: <a href=\"mailto:$user->email\">$user->email</a><br />";
1476 if ($user->city or $user->country) {
1477 echo "$string->location: ";
1478 if ($user->city) {
1479 echo $user->city;
1481 if (!empty($countries[$user->country])) {
1482 if ($user->city) {
1483 echo ', ';
1485 echo $countries[$user->country];
1487 echo "<br />";
1489 if ($user->lastaccess) {
1490 echo "$string->lastaccess: ".userdate($user->lastaccess);
1491 echo "&nbsp (".format_time(time() - $user->lastaccess, $datestring).")";
1492 } else {
1493 echo "$string->lastaccess: $string->never";
1495 echo '</td><td valign="bottom" bgcolor="#ffffff" nowrap="nowrap" class="userinfoboxlinkcontent">';
1497 echo '<font size="1">';
1498 if ($isteacher) {
1499 $timemidnight = usergetmidnight(time());
1500 echo "<a href=\"$CFG->wwwroot/course/user.php?id=$course->id&user=$user->id\">$string->activity</a><br>";
1501 if (!iscreator($user->id)) { // Includes admins
1502 if ($course->category and isteacheredit($course->id) and isstudent($course->id, $user->id)) { // Includes admins
1503 echo "<a href=\"$CFG->wwwroot/course/unenrol.php?id=$course->id&user=$user->id\">$string->unenrol</a><br />";
1505 if ($USER->id != $user->id) {
1506 echo "<a href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&user=$user->id\">$string->loginas</a><br />";
1510 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$string->fullprofile...</a>";
1511 echo '</font>';
1513 echo '</td></tr></table>';
1517 function print_group_picture($group, $courseid, $large=false, $returnstring=false, $link=true) {
1518 global $CFG;
1520 static $isteacheredit;
1522 if (!isset($isteacheredit)) {
1523 $isteacheredit = isteacheredit($courseid);
1526 if ($group->hidepicture and !$isteacheredit) {
1527 return '';
1530 if ($link or $isteacheredit) {
1531 $output = "<a href=\"$CFG->wwwroot/course/group.php?id=$courseid&amp;group=$group->id\">";
1532 } else {
1533 $output = '';
1535 if ($large) {
1536 $file = "f1";
1537 $size = 100;
1538 } else {
1539 $file = "f2";
1540 $size = 35;
1542 if ($group->picture) { // Print custom group picture
1543 if ($CFG->slasharguments) { // Use this method if possible for better caching
1544 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/user/pixgroup.php/$group->id/$file.jpg\"".
1545 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" title=\"$group->name\"/>";
1546 } else {
1547 $output .= "<img align=\"absmiddle\" src=\"$CFG->wwwroot/user/pixgroup.php?file=/$group->id/$file.jpg\"".
1548 " border=\"0\" width=\"$size\" height=\"$size\" alt=\"\" title=\"$group->name\"/>";
1551 if ($link or $isteacheredit) {
1552 $output .= "</a>";
1555 if ($returnstring) {
1556 return $output;
1557 } else {
1558 echo $output;
1563 function print_png($url, $sizex, $sizey, $returnstring, $parameters='alt=""') {
1564 global $CFG;
1565 static $recentIE;
1567 if (!isset($recentIE)) {
1568 $recentIE = check_browser_version('MSIE', '5.0');
1571 if ($recentIE) { // work around the HORRIBLE bug IE has with alpha transparencies
1572 $output .= "<img src=\"$CFG->pixpath/spacer.gif\" width=\"$sizex\" height=\"$sizey\"".
1573 " border=\"0\" style=\"width: {$sizex}px; height: {$sizey}px; ".
1574 " filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='$url', sizingMethod='scale') ".
1575 " $parameters />";
1576 } else {
1577 $output .= "<img src=\"$url\" border=\"0\" width=\"$sizex\" height=\"$sizey\" ".
1578 " $parameters />";
1581 if ($returnstring) {
1582 return $output;
1583 } else {
1584 echo $output;
1589 function print_table($table) {
1590 // Prints a nicely formatted table.
1591 // $table is an object with several properties.
1592 // $table->head is an array of heading names.
1593 // $table->align is an array of column alignments
1594 // $table->size is an array of column sizes
1595 // $table->wrap is an array of "nowrap"s or nothing
1596 // $table->data[] is an array of arrays containing the data.
1597 // $table->width is an percentage of the page
1598 // $table->cellpadding padding on each cell
1599 // $table->cellspacing spacing between cells
1601 global $THEME;
1603 if (isset($table->align)) {
1604 foreach ($table->align as $key => $aa) {
1605 if ($aa) {
1606 $align[$key] = " align=\"$aa\"";
1607 } else {
1608 $align[$key] = "";
1612 if (isset($table->size)) {
1613 foreach ($table->size as $key => $ss) {
1614 if ($ss) {
1615 $size[$key] = " width=\"$ss\"";
1616 } else {
1617 $size[$key] = "";
1621 if (isset($table->wrap)) {
1622 foreach ($table->wrap as $key => $ww) {
1623 if ($ww) {
1624 $wrap[$key] = " nowrap=\"nowrap\" ";
1625 } else {
1626 $wrap[$key] = "";
1631 if (empty($table->width)) {
1632 $table->width = "80%";
1635 if (empty($table->cellpadding)) {
1636 $table->cellpadding = "5";
1639 if (empty($table->cellspacing)) {
1640 $table->cellspacing = "1";
1643 print_simple_box_start("center", "$table->width", "#ffffff", 0);
1644 echo "<table width=\"100%\" border=\"0\" valign=\"top\" align=\"center\" ";
1645 echo " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"generaltable\">\n";
1647 $countcols = 0;
1649 if (!empty($table->head)) {
1650 $countcols = count($table->head);;
1651 echo "<tr>";
1652 foreach ($table->head as $key => $heading) {
1654 if (!isset($size[$key])) {
1655 $size[$key] = "";
1657 if (!isset($align[$key])) {
1658 $align[$key] = "";
1660 echo "<th valign=\"top\" ".$align[$key].$size[$key]." nowrap=\"nowrap\" class=\"generaltableheader\">$heading</th>";
1662 echo "</tr>\n";
1665 if (!empty($table->data)) {
1666 foreach ($table->data as $row) {
1667 echo "<tr valign=\"top\">";
1668 if ($row == "hr" and $countcols) {
1669 echo "<td colspan=\"$countcols\"><div class=\"tabledivider\"></div></td>";
1670 } else { /// it's a normal row of data
1671 foreach ($row as $key => $item) {
1672 if (!isset($size[$key])) {
1673 $size[$key] = "";
1675 if (!isset($align[$key])) {
1676 $align[$key] = "";
1678 if (!isset($wrap[$key])) {
1679 $wrap[$key] = "";
1681 echo "<td ".$align[$key].$size[$key].$wrap[$key]." class=\"generaltablecell\">$item</td>";
1684 echo "</tr>\n";
1687 echo "</table>\n";
1688 print_simple_box_end();
1690 return true;
1693 function make_table($table) {
1694 // Creates a nicely formatted table and returns it
1695 // $table is an object with several properties.
1696 // $table->head is an array of heading names.
1697 // $table->align is an array of column alignments
1698 // $table->size is an array of column sizes
1699 // $table->wrap is an array of "nowrap"s or nothing
1700 // $table->data[] is an array of arrays containing the data.
1701 // $table->width is an percentage of the page
1702 // $table->class is a class
1703 // $table->fontsize is the size of all the text
1704 // $table->tablealign align the whole table
1705 // $table->cellpadding padding on each cell
1706 // $table->cellspacing spacing between cells
1708 if (isset($table->align)) {
1709 foreach ($table->align as $key => $aa) {
1710 if ($aa) {
1711 $align[$key] = " align=\"$aa\"";
1712 } else {
1713 $align[$key] = "";
1717 if (isset($table->size)) {
1718 foreach ($table->size as $key => $ss) {
1719 if ($ss) {
1720 $size[$key] = " width=\"$ss\"";
1721 } else {
1722 $size[$key] = "";
1726 if (isset($table->wrap)) {
1727 foreach ($table->wrap as $key => $ww) {
1728 if ($ww) {
1729 $wrap[$key] = " nowrap=\"nowrap\" ";
1730 } else {
1731 $wrap[$key] = "";
1736 if (empty($table->width)) {
1737 $table->width = "80%";
1740 if (empty($table->tablealign)) {
1741 $table->tablealign = "center";
1744 if (empty($table->cellpadding)) {
1745 $table->cellpadding = "5";
1748 if (empty($table->cellspacing)) {
1749 $table->cellspacing = "1";
1752 if (empty($table->class)) {
1753 $table->class = "generaltable";
1756 if (empty($table->fontsize)) {
1757 $fontsize = "";
1758 } else {
1759 $fontsize = "<font size=\"$table->fontsize\">";
1762 $output = "<table width=\"$table->width\" valign=\"top\" align=\"$table->tablealign\" ";
1763 $output .= " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"$table->class\">\n";
1765 if (!empty($table->head)) {
1766 $output .= "<tr>";
1767 foreach ($table->head as $key => $heading) {
1768 if (!isset($size[$key])) {
1769 $size[$key] = "";
1771 if (!isset($align[$key])) {
1772 $align[$key] = "";
1774 $output .= "<th valign=\"top\" ".$align[$key].$size[$key]." nowrap=\"nowrap\" class=\"{$table->class}header\">$fontsize$heading</th>";
1776 $output .= "</tr>\n";
1779 foreach ($table->data as $row) {
1780 $output .= "<tr valign=\"top\">";
1781 foreach ($row as $key => $item) {
1782 if (!isset($size[$key])) {
1783 $size[$key] = "";
1785 if (!isset($align[$key])) {
1786 $align[$key] = "";
1788 if (!isset($wrap[$key])) {
1789 $wrap[$key] = "";
1791 $output .= "<td ".$align[$key].$size[$key].$wrap[$key]." class=\"{$table->class}cell\">$fontsize$item</td>";
1793 $output .= "</tr>\n";
1795 $output .= "</table>\n";
1797 return $output;
1800 function print_textarea($usehtmleditor, $rows, $cols, $width, $height, $name, $value="", $courseid=0) {
1801 /// Prints a basic textarea field
1802 /// $width and height are legacy fields and no longer used
1804 global $CFG, $course;
1806 if (empty($courseid)) {
1807 if (!empty($course->id)) { // search for it in global context
1808 $courseid = $course->id;
1812 if ($usehtmleditor) {
1813 if (!empty($courseid) and isteacher($courseid)) {
1814 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/htmlarea.php?id=$courseid\"></script>\n";
1815 } else {
1816 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/htmlarea.php\"></script>\n";
1818 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/dialog.js\"></script>\n";
1819 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/lang/en.php\"></script>\n";
1820 echo "<script type=\"text/javascript\" src=\"$CFG->wwwroot/lib/editor/popupwin.js\"></script>\n";
1822 if ($rows < 10) {
1823 $rows = 10;
1825 if ($cols < 65) {
1826 $cols = 65;
1830 echo "<textarea id=\"$name\" name=\"$name\" rows=\"$rows\" cols=\"$cols\" wrap=\"virtual\">";
1831 p($value);
1832 echo "</textarea>\n";
1835 function print_richedit_javascript($form, $name, $source="no") {
1836 /// Legacy function, provided for backward compatability
1837 use_html_editor($name);
1840 function use_html_editor($name="") {
1841 /// Sets up the HTML editor on textareas in the current page.
1842 /// If a field name is provided, then it will only be
1843 /// applied to that field - otherwise it will be used
1844 /// on every textarea in the page.
1846 /// In most cases no arguments need to be supplied
1848 echo "<script language=\"javascript\" type=\"text/javascript\" defer=\"1\">\n";
1849 print_editor_config();
1850 if (empty($name)) {
1851 echo "\nHTMLArea.replaceAll(config);\n";
1852 } else {
1853 echo "\nHTMLArea.replace('$name', config);\n";
1855 echo "</script>\n";
1859 function update_course_icon($courseid) {
1860 // Used to be an icon, but it's now a simple form button
1861 global $CFG, $USER;
1863 if (isteacheredit($courseid)) {
1864 if (!empty($USER->editing)) {
1865 $string = get_string("turneditingoff");
1866 $edit = "off";
1867 } else {
1868 $string = get_string("turneditingon");
1869 $edit = "on";
1871 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/view.php\">".
1872 "<input type=\"hidden\" name=\"id\" value=\"$courseid\" />".
1873 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1874 "<input type=\"submit\" value=\"$string\" /></form>";
1878 function update_module_button($moduleid, $courseid, $string) {
1879 // Prints the editing button on a module "view" page
1880 global $CFG, $USER;
1882 if (isteacheredit($courseid)) {
1883 $string = get_string("updatethis", "", $string);
1884 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/mod.php\">".
1885 "<input type=\"hidden\" name=\"update\" value=\"$moduleid\" />".
1886 "<input type=\"hidden\" name=\"return\" value=\"true\" />".
1887 "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />".
1888 "<input type=\"submit\" value=\"$string\" /></form>";
1889 } else {
1890 return "";
1894 function update_category_button($categoryid) {
1895 // Prints the editing button on a category page
1896 global $CFG, $USER;
1898 if (iscreator()) {
1899 if (!empty($USER->categoryediting)) {
1900 $string = get_string("turneditingoff");
1901 $edit = "off";
1902 } else {
1903 $string = get_string("turneditingon");
1904 $edit = "on";
1906 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/category.php\">".
1907 "<input type=\"hidden\" name=\"id\" value=\"$categoryid\" />".
1908 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1909 "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />".
1910 "<input type=\"submit\" value=\"$string\" /></form>";
1914 function update_categories_button() {
1915 // Prints the editing button on categories listing
1916 global $CFG, $USER;
1918 if (isadmin()) {
1919 if (!empty($USER->categoriesediting)) {
1920 $string = get_string("turneditingoff");
1921 $edit = "off";
1922 } else {
1923 $string = get_string("turneditingon");
1924 $edit = "on";
1926 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/index.php\">".
1927 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1928 "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />".
1929 "<input type=\"submit\" value=\"$string\" /></form>";
1933 function update_group_button($courseid, $groupid) {
1934 // Prints the editing button on group page
1935 global $CFG, $USER;
1937 if (isteacheredit($courseid)) {
1938 $string = get_string('editgroupprofile');
1939 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/group.php\">".
1940 "<input type=\"hidden\" name=\"id\" value=\"$courseid\" />".
1941 "<input type=\"hidden\" name=\"group\" value=\"$groupid\" />".
1942 "<input type=\"hidden\" name=\"edit\" value=\"on\" />".
1943 "<input type=\"submit\" value=\"$string\" /></form>";
1947 function update_groups_button($courseid) {
1948 // Prints the editing button on groups page
1949 global $CFG, $USER;
1951 if (isteacheredit($courseid)) {
1952 if (!empty($USER->groupsediting)) {
1953 $string = get_string("turneditingoff");
1954 $edit = "off";
1955 } else {
1956 $string = get_string("turneditingon");
1957 $edit = "on";
1959 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/course/groups.php\">".
1960 "<input type=\"hidden\" name=\"id\" value=\"$courseid\" />".
1961 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
1962 "<input type=\"submit\" value=\"$string\" /></form>";
1966 function print_group_menu($groups, $groupmode, $currentgroup, $urlroot) {
1967 /// Prints an appropriate group selection menu
1969 /// Add an "All groups" to the start of the menu
1970 $groupsmenu[0] = get_string("allparticipants");
1971 foreach ($groups as $key => $groupname) {
1972 $groupsmenu[$key] = $groupname;
1975 echo '<table><tr><td align="right">';
1976 if ($groupmode == VISIBLEGROUPS) {
1977 print_string('groupsvisible');
1978 } else {
1979 print_string('groupsseparate');
1981 echo ':';
1982 echo '</td><td nowrap="nowrap" align="left">';
1983 popup_form($urlroot.'&group=', $groupsmenu, 'selectgroup', $currentgroup, "", "", "", false, "self");
1984 echo '</tr></table>';
1989 function navmenu($course, $cm=NULL, $targetwindow="self") {
1990 // Given a course and a (current) coursemodule
1991 // This function returns a small popup menu with all the
1992 // course activity modules in it, as a navigation menu
1993 // The data is taken from the serialised array stored in
1994 // the course record
1996 global $CFG;
1998 if ($cm) {
1999 $cm = $cm->id;
2002 if ($course->format == 'weeks') {
2003 $strsection = get_string("week");
2004 } else {
2005 $strsection = get_string("topic");
2008 if (!$modinfo = unserialize($course->modinfo)) {
2009 return "";
2011 $isteacher = isteacher($course->id);
2012 $section = -1;
2013 $selected = "";
2014 $url = "";
2015 $previousmod = NULL;
2016 $backmod = NULL;
2017 $nextmod = NULL;
2018 $selectmod = NULL;
2019 $logslink = NULL;
2020 $flag = false;
2021 $menu = array();
2022 $strjumpto = get_string('jumpto');
2024 $sections = get_records('course_sections','course',$course->id,'section',"section,visible,summary");
2026 foreach ($modinfo as $mod) {
2027 if ($mod->mod == "label") {
2028 continue;
2031 if ($mod->section > $course->numsections) { /// Don't show excess hidden sections
2032 break;
2035 if ($mod->section > 0 and $section <> $mod->section) {
2036 $thissection = $sections[$mod->section];
2038 if ($thissection->visible or !$course->hiddensections or $isteacher) {
2039 $thissection->summary = strip_tags($thissection->summary);
2040 if ($course->format == 'weeks' or empty($thissection->summary)) {
2041 $menu[] = "-------------- $strsection $mod->section --------------";
2042 } else {
2043 if (strlen($thissection->summary) < 47) {
2044 $menu[] = '-- '.$thissection->summary;
2045 } else {
2046 $menu[] = '-- '.substr($thissection->summary, 0, 50).'...';
2052 $section = $mod->section;
2054 //Only add visible or teacher mods to jumpmenu
2055 if ($mod->visible or $isteacher) {
2056 $url = "$mod->mod/view.php?id=$mod->cm";
2057 if ($flag) { // the current mod is the "next" mod
2058 $nextmod = $mod;
2059 $flag = false;
2061 if ($cm == $mod->cm) {
2062 $selected = $url;
2063 $selectmod = $mod;
2064 $backmod = $previousmod;
2065 $flag = true; // set flag so we know to use next mod for "next"
2066 $mod->name = $strjumpto;
2067 $strjumpto = '';
2068 } else {
2069 $mod->name = strip_tags(urldecode($mod->name));
2070 if (strlen($mod->name) > 55) {
2071 $mod->name = substr($mod->name, 0, 50)."...";
2073 if (!$mod->visible) {
2074 $mod->name = "(".$mod->name.")";
2077 $menu[$url] = $mod->name;
2078 $previousmod = $mod;
2081 if ($selectmod and $isteacher) {
2082 $logslink = "<td><a target=\"$CFG->framename\" href=".
2083 "\"$CFG->wwwroot/course/log.php?chooselog=1&user=0&date=0&id=$course->id&modid=$selectmod->cm\">".
2084 "<img border=\"0\" height=\"16\" width=\"16\" src=\"$CFG->pixpath/i/log.gif\"></a></td>";
2087 if ($backmod) {
2088 $backmod = "<form action=\"$CFG->wwwroot/mod/$backmod->mod/view.php\" target=\"$CFG->framename\">".
2089 "<input type=\"hidden\" name=\"id\" value=\"$backmod->cm\">".
2090 "<input type=\"submit\" value=\"&lt;\"></form>";
2092 if ($nextmod) {
2093 $nextmod = "<form action=\"$CFG->wwwroot/mod/$nextmod->mod/view.php\" target=\"$CFG->framename\">".
2094 "<input type=\"hidden\" name=\"id\" value=\"$nextmod->cm\">".
2095 "<input type=\"submit\" value=\"&gt;\"></form>";
2097 return "<table><tr>$logslink<td>$backmod</td><td>" .
2098 popup_form("$CFG->wwwroot/mod/", $menu, "navmenu", $selected, $strjumpto,
2099 "", "", true, $targetwindow).
2100 "</td><td>$nextmod</td></tr></table>";
2104 function print_date_selector($day, $month, $year, $currenttime=0) {
2105 // Currenttime is a default timestamp in GMT
2106 // Prints form items with the names $day, $month and $year
2108 if (!$currenttime) {
2109 $currenttime = time();
2111 $currentdate = usergetdate($currenttime);
2113 for ($i=1; $i<=31; $i++) {
2114 $days[$i] = "$i";
2116 for ($i=1; $i<=12; $i++) {
2117 $months[$i] = userdate(gmmktime(12,0,0,$i,1,2000), "%B");
2119 for ($i=2000; $i<=2010; $i++) {
2120 $years[$i] = $i;
2122 choose_from_menu($days, $day, $currentdate['mday'], "");
2123 choose_from_menu($months, $month, $currentdate['mon'], "");
2124 choose_from_menu($years, $year, $currentdate['year'], "");
2127 function print_time_selector($hour, $minute, $currenttime=0, $step=5) {
2128 // Currenttime is a default timestamp in GMT
2129 // Prints form items with the names $hour and $minute
2131 if (!$currenttime) {
2132 $currenttime = time();
2134 $currentdate = usergetdate($currenttime);
2135 if ($step != 1) {
2136 $currentdate['minutes'] = ceil($currentdate['minutes']/$step)*$step;
2138 for ($i=0; $i<=23; $i++) {
2139 $hours[$i] = sprintf("%02d",$i);
2141 for ($i=0; $i<=59; $i+=$step) {
2142 $minutes[$i] = sprintf("%02d",$i);
2144 choose_from_menu($hours, $hour, $currentdate['hours'], "");
2145 choose_from_menu($minutes, $minute, $currentdate['minutes'], "");
2148 function print_timer_selector($timelimit = 0, $unit = "") {
2149 /// Prints time limit value selector
2151 global $CFG;
2153 if ($unit) {
2154 $unit = ' '.$unit;
2157 // Max timelimit is sessiontimeout - 10 minutes.
2158 $maxvalue = ($CFG->sessiontimeout / 60) - 10;
2160 for ($i=1; $i<=$maxvalue; $i++) {
2161 $minutes[$i] = $i.$unit;
2163 choose_from_menu($minutes, "timelimit", $timelimit, get_string("none"));
2166 function print_grade_menu($courseid, $name, $current, $includenograde=true) {
2167 /// Prints a grade menu (as part of an existing form) with help
2168 /// Showing all possible numerical grades and scales
2170 global $CFG;
2172 $strscale = get_string("scale");
2173 $strscales = get_string("scales");
2175 $scales = get_scales_menu($courseid);
2176 foreach ($scales as $i => $scalename) {
2177 $grades[-$i] = "$strscale: $scalename";
2179 if ($includenograde) {
2180 $grades[0] = get_string("nograde");
2182 for ($i=100; $i>=1; $i--) {
2183 $grades[$i] = $i;
2185 choose_from_menu($grades, "$name", "$current", "");
2187 $helpicon = "$CFG->pixpath/help.gif";
2188 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" alt=\"$strscales\" src=\"$helpicon\" />";
2189 link_to_popup_window ("/course/scales.php?id=$courseid&amp;list=true", "ratingscales",
2190 $linkobject, 400, 500, $strscales);
2193 function print_scale_menu($courseid, $name, $current) {
2194 /// Prints a scale menu (as part of an existing form) including help button
2195 /// Just like print_grade_menu but without the numerical grades
2197 global $CFG;
2199 $strscales = get_string("scales");
2200 choose_from_menu(get_scales_menu($courseid), "$name", $current, "");
2201 $helpicon = "$CFG->pixpath/help.gif";
2202 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" alt=\"$strscales\" src=\"$helpicon\" />";
2203 link_to_popup_window ("/course/scales.php?id=$courseid&amp;list=true", "ratingscales",
2204 $linkobject, 400, 500, $strscales);
2208 function print_scale_menu_helpbutton($courseid, $scale) {
2209 /// Prints a help button about a scale
2210 /// scale is an object
2212 global $CFG;
2214 $strscales = get_string("scales");
2215 $helpicon = "$CFG->pixpath/help.gif";
2216 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" alt=\"$scale->name\" src=\"$helpicon\" />";
2217 link_to_popup_window ("/course/scales.php?id=$courseid&amp;list=true&amp;scale=$scale->id", "ratingscale",
2218 $linkobject, 400, 500, $scale->name);
2222 function error ($message, $link="") {
2223 global $CFG, $SESSION;
2225 print_header(get_string("error"));
2226 echo "<br />";
2228 $message = clean_text($message); // In case nasties are in here
2230 print_simple_box($message, "center", "", "#FFBBBB");
2232 if (!$link) {
2233 if ( !empty($SESSION->fromurl) ) {
2234 $link = "$SESSION->fromurl";
2235 unset($SESSION->fromurl);
2236 } else {
2237 $link = "$CFG->wwwroot/";
2240 print_continue($link);
2241 print_footer();
2242 die;
2245 function helpbutton ($page, $title="", $module="moodle", $image=true, $linktext=false, $text="", $return=false) {
2246 // $page = the keyword that defines a help page
2247 // $title = the title of links, rollover tips, alt tags etc
2248 // $module = which module is the page defined in
2249 // $image = use a help image for the link? (true/false/"both")
2250 // $text = if defined then this text is used in the page, and
2251 // the $page variable is ignored.
2252 global $CFG, $THEME;
2254 if ($module == "") {
2255 $module = "moodle";
2258 if ($image) {
2259 $icon = "$CFG->pixpath/help.gif";
2260 if ($linktext) {
2261 $linkobject = "<span style=\"cursor:help;\">$title<img align=\"absmiddle\" border=\"0\" ".
2262 " height=\"17\" width=\"22\" alt=\"\" src=\"$icon\" /></span>";
2263 } else {
2264 $linkobject = "<img align=\"absmiddle\" border=\"0\" height=\"17\" width=\"22\" ".
2265 " alt=\"$title\" style=\"cursor:help;\" src=\"$icon\" />";
2267 } else {
2268 $linkobject = "<span style=\"cursor:help;\">$title</span>";
2270 if ($text) {
2271 $url = "/help.php?module=$module&amp;text=".htmlentities(urlencode($text));
2272 } else {
2273 $url = "/help.php?module=$module&amp;file=$page.html";
2276 $link = link_to_popup_window ($url, "popup", $linkobject, 400, 500, $title, 'none', true);
2278 if ($return) {
2279 return $link;
2280 } else {
2281 echo $link;
2285 function emoticonhelpbutton($form, $field) {
2286 /// Prints a special help button that is a link to the "live" emoticon popup
2287 global $CFG, $SESSION;
2289 $SESSION->inserttextform = $form;
2290 $SESSION->inserttextfield = $field;
2291 helpbutton("emoticons", get_string("helpemoticons"), "moodle", false, true);
2292 echo "&nbsp;";
2293 link_to_popup_window ("/help.php?module=moodle&amp;file=emoticons.html", "popup",
2294 "<img src=\"$CFG->pixpath/s/smiley.gif\" border=\"0\" align=\"absmiddle\" width=\"15\" height=\"15\" />",
2295 400, 500, get_string("helpemoticons"));
2296 echo "<br />";
2299 function notice ($message, $link="") {
2300 global $CFG, $THEME;
2302 $message = clean_text($message);
2303 $link = clean_text($link);
2305 if (!$link) {
2306 if (!empty($_SERVER["HTTP_REFERER"])) {
2307 $link = $_SERVER["HTTP_REFERER"];
2308 } else {
2309 $link = "$CFG->wwwroot/";
2313 echo "<br />";
2314 print_simple_box($message, "center", "50%", "$THEME->cellheading", "20", "noticebox");
2315 print_heading("<a href=\"$link\">".get_string("continue")."</a>");
2316 print_footer(get_site());
2317 die;
2320 function notice_yesno ($message, $linkyes, $linkno) {
2321 global $THEME;
2323 $message = clean_text($message);
2324 $linkyes = clean_text($linkyes);
2325 $linkno = clean_text($linkno);
2327 print_simple_box_start("center", "60%", "$THEME->cellheading");
2328 echo "<p align=\"center\"><font size=\"3\">$message</font></p>";
2329 echo "<p align=\"center\"><font size=\"3\"><b>";
2330 echo "<a href=\"$linkyes\">".get_string("yes")."</a>";
2331 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
2332 echo "<a href=\"$linkno\">".get_string("no")."</a>";
2333 echo "</b></font></p>";
2334 print_simple_box_end();
2337 function redirect($url, $message="", $delay="0") {
2338 // Redirects the user to another page, after printing a notice
2340 $url = clean_text($url);
2341 $message = clean_text($message);
2343 if (empty($message)) {
2344 echo "<meta http-equiv=\"refresh\" content=\"$delay; url=$url\" />";
2345 echo "<script>location.replace('$url');</script>"; // To cope with Mozilla bug
2346 } else {
2347 if (empty($delay)) {
2348 $delay = 3; // There's no point having a message with no delay
2350 print_header("", "", "", "", "<meta http-equiv=\"refresh\" content=\"$delay; url=$url\" />");
2351 echo "<center>";
2352 echo "<p>$message</p>";
2353 echo "<p>( <a href=\"$url\">".get_string("continue")."</a> )</p>";
2354 echo "</center>";
2355 flush();
2356 sleep($delay);
2357 echo "<script>location.replace('$url');</script>"; // To cope with Mozilla bug
2359 die;
2362 function notify ($message, $color="red", $align="center") {
2364 $message = clean_text($message);
2366 echo "<p align=\"$align\"><b><font color=\"$color\">$message</font></b></p>\n";
2369 function obfuscate_email($email) {
2370 /// Given an email address, this function will return an obfuscated version of it
2371 $i = 0;
2372 $length = strlen($email);
2373 $obfuscated = "";
2374 while ($i < $length) {
2375 if (rand(0,2)) {
2376 $obfuscated.='%'.dechex(ord($email{$i}));
2377 } else {
2378 $obfuscated.=$email{$i};
2380 $i++;
2382 return $obfuscated;
2385 function obfuscate_text($plaintext) {
2386 /// This function takes some text and replaces about half of the characters
2387 /// with HTML entity equivalents. Return string is obviously longer.
2388 $i=0;
2389 $length = strlen($plaintext);
2390 $obfuscated="";
2391 $prev_obfuscated = false;
2392 while ($i < $length) {
2393 $c = ord($plaintext{$i});
2394 $numerical = ($c >= ord('0')) && ($c <= ord('9'));
2395 if ($prev_obfuscated and $numerical ) {
2396 $obfuscated.='&#'.ord($plaintext{$i});
2397 } else if (rand(0,2)) {
2398 $obfuscated.='&#'.ord($plaintext{$i});
2399 $prev_obfuscated = true;
2400 } else {
2401 $obfuscated.=$plaintext{$i};
2402 $prev_obfuscated = false;
2404 $i++;
2406 return $obfuscated;
2409 function obfuscate_mailto($email, $label="", $dimmed=false) {
2410 /// This function uses the above two functions to generate a fully
2411 /// obfuscated email link, ready to use.
2413 if (empty($label)) {
2414 $label = $email;
2416 if ($dimmed) {
2417 $title = get_string('emaildisable');
2418 $dimmed = ' class="dimmed"';
2419 } else {
2420 $title = '';
2421 $dimmed = '';
2423 return sprintf("<a href=\"%s:%s\" $dimmed title=\"$title\">%s</a>",
2424 obfuscate_text('mailto'), obfuscate_email($email),
2425 obfuscate_text($label));
2428 function print_paging_bar($totalcount, $page, $perpage, $baseurl) {
2429 /// Prints a single paging bar to provide access to other pages (usually in a search)
2431 $maxdisplay = 18;
2433 if ($totalcount > $perpage) {
2434 echo "<center>";
2435 echo "<p>".get_string("page").":";
2436 if ($page > 0) {
2437 $pagenum=$page-1;
2438 echo "&nbsp;(<a href=\"{$baseurl}page=$pagenum\">".get_string("previous")."</a>)&nbsp;";
2440 $lastpage = ceil($totalcount / $perpage);
2441 if ($page > 15) {
2442 $startpage = $page - 10;
2443 echo "&nbsp<a href=\"{$baseurl}page=0\">1</a>&nbsp;...";
2444 } else {
2445 $startpage = 0;
2447 $currpage = $startpage;
2448 $displaycount = 0;
2449 while ($displaycount < $maxdisplay and $currpage < $lastpage) {
2450 $displaypage = $currpage+1;
2451 if ($page == $currpage) {
2452 echo "&nbsp;&nbsp;$displaypage";
2453 } else {
2454 echo "&nbsp;&nbsp;<a href=\"{$baseurl}page=$currpage\">$displaypage</a>";
2456 $displaycount++;
2457 $currpage++;
2459 if ($currpage < $lastpage) {
2460 $lastpageactual = $lastpage - 1;
2461 echo "&nbsp;...<a href=\"{$baseurl}page=$lastpageactual\">$lastpage</a>&nbsp;";
2463 $pagenum = $page + 1;
2464 if ($pagenum != $displaypage) {
2465 echo "&nbsp;&nbsp;(<a href=\"{$baseurl}page=$pagenum\">".get_string("next")."</a>)";
2467 echo "</p>";
2468 echo "</center>";
2472 //This function is used to rebuild the <nolink> tag because some formats (PLAIN and WIKI)
2473 //will transform it to html entities
2474 function rebuildnolinktag($text) {
2476 $text = preg_replace('/&lt;(\/*nolink)&gt;/i','<$1>',$text);
2478 return $text;
2484 // ================================================
2485 // THREE FUNCTIONS MOVED HERE FROM course/lib.php
2486 // ================================================
2488 function print_side_block($heading='', $content='', $list=NULL, $icons=NULL, $footer='', $attributes = array()) {
2489 // Prints a nice side block with an optional header. The content can either
2490 // be a block of HTML or a list of text with optional icons.
2492 global $THEME;
2494 print_side_block_start($heading, $attributes);
2496 if ($content) {
2497 echo $content;
2498 if ($footer) {
2499 echo "<center><font size=\"-2\">$footer</font></center>";
2501 } else {
2502 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
2503 if ($list) {
2504 foreach ($list as $key => $string) {
2505 echo "<tr bgcolor=\"$THEME->cellcontent2\">";
2506 if ($icons) {
2507 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"16\">".$icons[$key]."</td>";
2509 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"*\"><font size=\"-1\">$string</font></td>";
2510 echo "</tr>";
2513 if ($footer) {
2514 echo "<tr bgcolor=\"$THEME->cellcontent2\">";
2515 echo "<td class=\"sideblocklinks\" ";
2516 if ($icons) {
2517 echo ' colspan="2" ';
2519 echo '>';
2520 echo "<center><font size=\"-2\">$footer</font></center>";
2521 echo "</td></tr>";
2523 echo "</table>";
2526 print_side_block_end();
2529 function print_side_block_start($heading='', $attributes = array()) {
2530 // Starts a nice side block with an optional header.
2531 global $THEME;
2533 // If there are no special attributes, give a default CSS class
2534 if(empty($attributes) || !is_array($attributes)) {
2535 $attributes = array('class' => 'sideblock');
2537 else if(!isset($attributes['class'])) {
2538 $attributes['class'] = 'sideblock';
2540 else if(!strpos($attributes['class'], 'sideblock')) {
2541 $attributes['class'] .= ' sideblock';
2543 // OK, the class is surely there and in addition to anything
2544 // else, it's tagged as a sideblock
2546 $attrtext = '';
2547 foreach($attributes as $attr => $val) {
2548 $attrtext .= ' '.$attr.'="'.$val.'"';
2551 // [pj] UGLY UGLY UGLY! I hate myself for doing this!
2552 // When the Lord Moodle 2.0 cometh, his mercy shalt move all this mess
2553 // to CSS and banish the evil to the abyss from whence it came.
2554 echo '<table style="width: 100%;" cellspacing="0" cellpadding="5"'.$attrtext.'>';
2555 if ($heading) {
2556 echo '<thead><tr><td class="sideblockheading">'.$heading.'</td></tr></thead>';
2558 echo '<tbody style="background-color: '.$THEME->cellcontent2.';"><tr><td class="sideblockmain">';
2563 function print_side_block_end() {
2564 echo '</td></tr></tbody></table><br />';
2565 echo "\n";
2568 function print_editor_config() {
2569 /// prints out the editor config.
2571 global $CFG;
2573 // print new config
2574 echo "var config = new HTMLArea.Config();\n";
2575 echo "config.pageStyle = \"body {";
2576 if(!(empty($CFG->editorbackgroundcolor))) {
2577 echo " background-color: $CFG->editorbackgroundcolor;";
2580 if(!(empty($CFG->editorfontfamily))) {
2581 echo " font-family: $CFG->editorfontfamily;";
2584 if(!(empty($CFG->editorfontsize))) {
2585 echo " font-size: $CFG->editorfontsize;";
2588 echo " }\";\n";
2589 echo "config.killWordOnPaste = ";
2590 echo(empty($CFG->editorkillword)) ? "false":"true";
2591 echo ";\n";
2592 echo "config.fontname = {\n";
2594 $fontlist = isset($CFG->editorfontlist) ? explode(';', $CFG->editorfontlist) : array();
2595 $i = 1; // Counter is used to get rid of the last comma.
2596 $count = count($fontlist); // Otherwise IE doesn't load the editor.
2598 foreach($fontlist as $fontline) {
2599 if(!empty($fontline)) {
2600 list($fontkey, $fontvalue) = split(":", $fontline);
2601 echo "\"". $fontkey ."\":\t'". $fontvalue ."'";
2602 if($i < $count) {
2603 echo ",\n";
2606 $i++;
2608 echo "};";
2609 if(!empty($CFG->editorspelling) && !empty($CFG->aspellpath)) {
2610 print_speller_code($usehtmleditor=true);
2614 function print_speller_code ($usehtmleditor=false) {
2615 /// Prints out code needed for spellchecking.
2616 /// Original idea by Ludo (Marc Alier).
2617 global $CFG;
2619 if(!$usehtmleditor) {
2620 echo "\n<script language=\"javascript\" type=\"text/javascript\">\n";
2621 echo "function openSpellChecker() {\n";
2622 echo "\tvar speller = new spellChecker();\n";
2623 echo "\tspeller.popUpUrl = \"" . $CFG->wwwroot ."/lib/speller/spellchecker.html\";\n";
2624 echo "\tspeller.spellCheckScript = \"". $CFG->wwwroot ."/lib/speller/server-scripts/spellchecker.php\";\n";
2625 echo "\tspeller.spellCheckAll();\n";
2626 echo "}\n";
2627 echo "</script>\n";
2628 } else {
2629 echo "\nfunction spellClickHandler(editor, buttonId) {\n";
2630 echo "\teditor._textArea.value = editor.getHTML();\n";
2631 echo "\tvar speller = new spellChecker( editor._textArea );\n";
2632 echo "\tspeller.popUpUrl = \"" . $CFG->wwwroot ."/lib/speller/spellchecker.html\";\n";
2633 echo "\tspeller.spellCheckScript = \"". $CFG->wwwroot ."/lib/speller/server-scripts/spellchecker.php\";\n";
2634 echo "\tspeller._moogle_edit=1;\n";
2635 echo "\tspeller._editor=editor;\n";
2636 echo "\tspeller.openChecker();\n";
2637 echo "}\n";
2641 function print_speller_button () {
2642 // print button for spellchecking
2643 // when editor is disabled
2644 echo "<input type=\"button\" value=\"Check spelling\" onclick=\"openSpellChecker();\" />\n";
2646 // vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140: