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