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 |
0095d5cd |
35 | define("FORMAT_MOODLE", "0"); |
36 | define("FORMAT_HTML", "1"); |
37 | |
c9dda990 |
38 | $SMILEY_TEXT[] = ":-)"; |
39 | $SMILEY_IMAGE[] = "<IMG ALT=\":-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/smiley.gif\">"; |
40 | $SMILEY_TEXT[] = ":)"; |
41 | $SMILEY_IMAGE[] = "<IMG ALT=\":-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/smiley.gif\">"; |
42 | $SMILEY_TEXT[] = ":-D"; |
43 | $SMILEY_IMAGE[] = "<IMG ALT=\":-D\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/biggrin.gif\">"; |
44 | $SMILEY_TEXT[] = ";-)"; |
45 | $SMILEY_IMAGE[] = "<IMG ALT=\";-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/wink.gif\">"; |
46 | $SMILEY_TEXT[] = ":-/"; |
47 | $SMILEY_IMAGE[] = "<IMG ALT=\":-/\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/mixed.gif\">"; |
48 | $SMILEY_TEXT[] = "V-."; |
49 | $SMILEY_IMAGE[] = "<IMG ALT=\"V-.\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/thoughtful.gif\">"; |
50 | $SMILEY_TEXT[] = ":-P"; |
51 | $SMILEY_IMAGE[] = "<IMG ALT=\":-P\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/tongueout.gif\">"; |
52 | $SMILEY_TEXT[] = "B-)"; |
53 | $SMILEY_IMAGE[] = "<IMG ALT=\"B-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/cool.gif\">"; |
54 | $SMILEY_TEXT[] = "^-)"; |
55 | $SMILEY_IMAGE[] = "<IMG ALT=\"^-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/approve.gif\">"; |
56 | $SMILEY_TEXT[] = "8-)"; |
57 | $SMILEY_IMAGE[] = "<IMG ALT=\"8-)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/wideeyes.gif\">"; |
58 | $SMILEY_TEXT[] = ":o)"; |
59 | $SMILEY_IMAGE[] = "<IMG ALT=\":o)\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/clown.gif\">"; |
60 | $SMILEY_TEXT[] = ":-("; |
61 | $SMILEY_IMAGE[] = "<IMG ALT=\":-(\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/sad.gif\">"; |
62 | $SMILEY_TEXT[] = ":("; |
63 | $SMILEY_IMAGE[] = "<IMG ALT=\":-(\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/sad.gif\">"; |
64 | $SMILEY_TEXT[] = "8-."; |
65 | $SMILEY_IMAGE[] = "<IMG ALT=\"8-.\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/shy.gif\">"; |
66 | $SMILEY_TEXT[] = ":-I"; |
67 | $SMILEY_IMAGE[] = "<IMG ALT=\":-I\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/blush.gif\">"; |
68 | $SMILEY_TEXT[] = ":-X"; |
69 | $SMILEY_IMAGE[] = "<IMG ALT=\":-X\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/kiss.gif\">"; |
70 | $SMILEY_TEXT[] = "8-o"; |
71 | $SMILEY_IMAGE[] = "<IMG ALT=\"8-o\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/surprise.gif\">"; |
72 | $SMILEY_TEXT[] = "P-|"; |
73 | $SMILEY_IMAGE[] = "<IMG ALT=\"P-|\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/blackeye.gif\">"; |
74 | $SMILEY_TEXT[] = "8-["; |
75 | $SMILEY_IMAGE[] = "<IMG ALT=\"8-[\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/angry.gif\">"; |
76 | $SMILEY_TEXT[] = "xx-P"; |
77 | $SMILEY_IMAGE[] = "<IMG ALT=\"xx-P\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/dead.gif\">"; |
78 | $SMILEY_TEXT[] = "|-."; |
79 | $SMILEY_IMAGE[] = "<IMG ALT=\"|-.\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/sleepy.gif\">"; |
80 | $SMILEY_TEXT[] = "}-]"; |
81 | $SMILEY_IMAGE[] = "<IMG ALT=\"}-]\" WIDTH=15 HEIGHT=15 SRC=\"$CFG->wwwroot/pix/s/evil.gif\">"; |
0095d5cd |
82 | |
3fe3851d |
83 | $JAVASCRIPT_TAGS = array("javascript:", "onclick=", "ondblclick=", "onkeydown=", "onkeypress=", "onkeyup=", |
84 | "onmouseover=", "onmouseout=", "onmousedown=", "onmouseup=", |
85 | "onblur=", "onfocus=", "onload=", "onselect="); |
86 | |
db11e52d |
87 | $ALLOWED_TAGS = "<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><sup><sub><address><cite><blockquote><pre><strike><embed><object><param>"; |
3fe3851d |
88 | |
89 | |
0095d5cd |
90 | /// Functions |
91 | |
8553b700 |
92 | function s($var) { |
c1d57101 |
93 | /// returns $var with HTML characters (like "<", ">", etc.) properly quoted, |
f9903ed0 |
94 | |
7d8f674d |
95 | return htmlSpecialChars(stripslashes_safe($var)); |
f9903ed0 |
96 | } |
97 | |
98 | function p($var) { |
c1d57101 |
99 | /// prints $var with HTML characters (like "<", ">", etc.) properly quoted, |
f9903ed0 |
100 | |
7d8f674d |
101 | echo htmlSpecialChars(stripslashes_safe($var)); |
f9903ed0 |
102 | } |
103 | |
8553b700 |
104 | function nvl(&$var, $default="") { |
c1d57101 |
105 | /// if $var is undefined, return $default, otherwise return $var |
8553b700 |
106 | |
107 | return isset($var) ? $var : $default; |
108 | } |
f9903ed0 |
109 | |
110 | function strip_querystring($url) { |
c1d57101 |
111 | /// takes a URL and returns it without the querystring portion |
f9903ed0 |
112 | |
b9b8ab69 |
113 | if ($commapos = strpos($url, '?')) { |
114 | return substr($url, 0, $commapos); |
115 | } else { |
116 | return $url; |
117 | } |
f9903ed0 |
118 | } |
119 | |
120 | function get_referer() { |
c1d57101 |
121 | /// returns the URL of the HTTP_REFERER, less the querystring portion |
f9903ed0 |
122 | |
607809b3 |
123 | return strip_querystring(nvl($_SERVER["HTTP_REFERER"])); |
f9903ed0 |
124 | } |
125 | |
c1d57101 |
126 | |
f9903ed0 |
127 | function me() { |
c1d57101 |
128 | /// returns the name of the current script, WITH the querystring portion. |
eaa50dbc |
129 | /// this function is necessary because PHP_SELF and REQUEST_URI and SCRIPT_NAME |
c1d57101 |
130 | /// return different things depending on a lot of things like your OS, Web |
131 | /// server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.) |
f9903ed0 |
132 | |
607809b3 |
133 | if (!empty($_SERVER["REQUEST_URI"])) { |
134 | return $_SERVER["REQUEST_URI"]; |
c1d57101 |
135 | |
607809b3 |
136 | } else if (!empty($_SERVER["PHP_SELF"])) { |
fced815c |
137 | if (!empty($_SERVER["QUERY_STRING"])) { |
138 | return $_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"]; |
139 | } |
607809b3 |
140 | return $_SERVER["PHP_SELF"]; |
c1d57101 |
141 | |
fced815c |
142 | } else if (!empty($_SERVER["SCRIPT_NAME"])) { |
143 | if (!empty($_SERVER["QUERY_STRING"])) { |
144 | return $_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"]; |
145 | } |
146 | return $_SERVER["SCRIPT_NAME"]; |
147 | |
b9b8ab69 |
148 | } else { |
fced815c |
149 | notify("Warning: Could not find any of these web server variables: \$REQUEST_URI, \$PHP_SELF or \$SCRIPT_NAME"); |
bcdfe14e |
150 | return false; |
7fbd6b1c |
151 | } |
f9903ed0 |
152 | } |
153 | |
154 | |
f9903ed0 |
155 | function qualified_me() { |
c1d57101 |
156 | /// like me() but returns a full URL |
f9903ed0 |
157 | |
39e018b3 |
158 | if (!empty($_SERVER["HTTP_HOST"])) { |
159 | $hostname = $_SERVER["HTTP_HOST"]; |
160 | } else if (!empty($_ENV["HTTP_HOST"])) { |
161 | $hostname = $_ENV["HTTP_HOST"]; |
162 | } else if (!empty($_ENV["SERVER_NAME"])) { |
163 | $hostname = $_ENV["SERVER_NAME"]; |
164 | } else { |
165 | notify("Warning: could not find the name of this server!"); |
bcdfe14e |
166 | return false; |
c1d57101 |
167 | } |
f9903ed0 |
168 | |
607809b3 |
169 | $protocol = (isset($_SERVER["HTTPS"]) and $_SERVER["HTTPS"] == "on") ? "https://" : "http://"; |
39e018b3 |
170 | $url_prefix = $protocol.$hostname; |
b9b8ab69 |
171 | return $url_prefix . me(); |
f9903ed0 |
172 | } |
173 | |
174 | |
175 | function match_referer($good_referer = "") { |
c1d57101 |
176 | /// returns true if the referer is the same as the good_referer. If |
87a2fa03 |
177 | /// good_referer is not specified, use qualified_me as the good_referer |
60f18531 |
178 | global $CFG; |
179 | |
ce78926d |
180 | if (!empty($CFG->buggy_referer)) { |
60f18531 |
181 | return true; |
182 | } |
f9903ed0 |
183 | |
ce78926d |
184 | if (empty($good_referer)) { |
c1d57101 |
185 | $good_referer = qualified_me(); |
186 | } |
b9b8ab69 |
187 | return $good_referer == get_referer(); |
f9903ed0 |
188 | } |
189 | |
36b4f985 |
190 | function data_submitted($url="") { |
191 | /// Used on most forms in Moodle to check for data |
192 | /// Returns the data as an object, if it's found. |
607809b3 |
193 | /// This object can be used in foreach loops without |
194 | /// casting because it's cast to (array) automatically |
36b4f985 |
195 | /// |
196 | /// Checks that submitted POST data exists, and also |
197 | /// checks the referer against the given url (it uses |
198 | /// the current page if none was specified. |
199 | |
37208cd2 |
200 | global $CFG; |
201 | |
607809b3 |
202 | if (empty($_POST)) { |
36b4f985 |
203 | return false; |
607809b3 |
204 | |
36b4f985 |
205 | } else { |
206 | if (match_referer($url)) { |
607809b3 |
207 | return (object)$_POST; |
36b4f985 |
208 | } else { |
209 | if ($CFG->debug > 10) { |
210 | notice("The form did not come from this page! (referer = ".get_referer().")"); |
211 | } |
212 | return false; |
213 | } |
214 | } |
215 | } |
216 | |
7d8f674d |
217 | function stripslashes_safe($string) { |
218 | /// stripslashes() removes ALL backslashes even from strings |
219 | /// so C:\temp becomes C:temp ... this isn't good. |
220 | /// The following should work as a fairly safe replacement |
221 | /// to be called on quoted AND unquoted strings (to be sure) |
222 | |
223 | $string = str_replace("\\'", "'", $string); |
224 | $string = str_replace('\\"', '"', $string); |
225 | $string = str_replace('\\\\', '\\', $string); |
226 | return $string; |
227 | } |
f9903ed0 |
228 | |
3fe3851d |
229 | function stri_replace($find, $replace, $string ) { |
c1d57101 |
230 | /// This does a search and replace, ignoring case |
231 | /// This function is only here because one doesn't exist yet in PHP |
232 | /// Unlike str_replace(), this only works on single values (not arrays) |
3fe3851d |
233 | |
234 | $parts = explode(strtolower($find), strtolower($string)); |
235 | |
236 | $pos = 0; |
237 | |
238 | foreach ($parts as $key => $part) { |
239 | $parts[$key] = substr($string, $pos, strlen($part)); |
240 | $pos += strlen($part) + strlen($find); |
241 | } |
242 | |
243 | return (join($replace, $parts)); |
244 | } |
245 | |
f9903ed0 |
246 | function read_template($filename, &$var) { |
c1d57101 |
247 | /// return a (big) string containing the contents of a template file with all |
248 | /// the variables interpolated. all the variables must be in the $var[] array or |
249 | /// object (whatever you decide to use). |
250 | /// |
251 | /// WARNING: do not use this on big files!! |
f9903ed0 |
252 | |
b9b8ab69 |
253 | $temp = str_replace("\\", "\\\\", implode(file($filename), "")); |
254 | $temp = str_replace('"', '\"', $temp); |
255 | eval("\$template = \"$temp\";"); |
256 | return $template; |
f9903ed0 |
257 | } |
258 | |
259 | function checked(&$var, $set_value = 1, $unset_value = 0) { |
c1d57101 |
260 | /// if variable is set, set it to the set_value otherwise set it to the |
261 | /// unset_value. used to handle checkboxes when you are expecting them from |
262 | /// a form |
f9903ed0 |
263 | |
b9b8ab69 |
264 | if (empty($var)) { |
265 | $var = $unset_value; |
266 | } else { |
267 | $var = $set_value; |
268 | } |
f9903ed0 |
269 | } |
270 | |
271 | function frmchecked(&$var, $true_value = "checked", $false_value = "") { |
c1d57101 |
272 | /// prints the word "checked" if a variable is true, otherwise prints nothing, |
273 | /// used for printing the word "checked" in a checkbox form input |
f9903ed0 |
274 | |
b9b8ab69 |
275 | if ($var) { |
276 | echo $true_value; |
277 | } else { |
278 | echo $false_value; |
279 | } |
f9903ed0 |
280 | } |
281 | |
282 | |
65cf9fc3 |
283 | function link_to_popup_window ($url, $name="popup", $linkname="click here", $height=400, $width=500, $title="Popup window") { |
c1d57101 |
284 | /// This will create a HTML link that will work on both |
285 | /// Javascript and non-javascript browsers. |
286 | /// Relies on the Javascript function openpopup in javascript.php |
287 | /// $url must be relative to home page eg /mod/survey/stuff.php |
f9903ed0 |
288 | |
ff80e012 |
289 | global $CFG; |
290 | |
b9b8ab69 |
291 | echo "\n<SCRIPT language=\"Javascript\">"; |
f9903ed0 |
292 | echo "\n<!--"; |
b9b8ab69 |
293 | echo "\ndocument.write('<A TITLE=\"$title\" HREF=javascript:openpopup(\"$url\",\"$name\",\"$height\",\"$width\") >$linkname</A>');"; |
f9903ed0 |
294 | echo "\n//-->"; |
b9b8ab69 |
295 | echo "\n</SCRIPT>"; |
296 | echo "\n<NOSCRIPT>\n<A TARGET=\"$name\" TITLE=\"$title\" HREF=\"$CFG->wwwroot/$url\">$linkname</A>\n</NOSCRIPT>\n"; |
f9903ed0 |
297 | |
298 | } |
299 | |
300 | function close_window_button() { |
c1d57101 |
301 | /// Prints a simple button to close a window |
302 | |
f9903ed0 |
303 | echo "<FORM><CENTER>"; |
e5dfd0f3 |
304 | echo "<INPUT TYPE=button onClick=\"self.close();\" VALUE=\"".get_string("closewindow")."\">"; |
f9903ed0 |
305 | echo "</CENTER></FORM>"; |
306 | } |
307 | |
308 | |
08056730 |
309 | function choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", $nothingvalue="0", $return=false) { |
c1d57101 |
310 | /// Given an array of value, creates a popup menu to be part of a form |
311 | /// $options["value"]["label"] |
f9903ed0 |
312 | |
618b22c5 |
313 | if ($nothing == "choose") { |
314 | $nothing = get_string("choose")."..."; |
315 | } |
316 | |
f9903ed0 |
317 | if ($script) { |
318 | $javascript = "onChange=\"$script\""; |
9c9f7d77 |
319 | } else { |
320 | $javascript = ""; |
f9903ed0 |
321 | } |
9c9f7d77 |
322 | |
08056730 |
323 | $output = "<SELECT NAME=$name $javascript>\n"; |
bda8d43a |
324 | if ($nothing) { |
08056730 |
325 | $output .= " <OPTION VALUE=\"$nothingvalue\"\n"; |
bda8d43a |
326 | if ($nothingvalue == $selected) { |
08056730 |
327 | $output .= " SELECTED"; |
bda8d43a |
328 | } |
08056730 |
329 | $output .= ">$nothing</OPTION>\n"; |
873960de |
330 | } |
607809b3 |
331 | if (!empty($options)) { |
332 | foreach ($options as $value => $label) { |
333 | $output .= " <OPTION VALUE=\"$value\""; |
334 | if ($value == $selected) { |
335 | $output .= " SELECTED"; |
336 | } |
337 | if ($label) { |
338 | $output .= ">$label</OPTION>\n"; |
339 | } else { |
340 | $output .= ">$value</OPTION>\n"; |
341 | } |
f9903ed0 |
342 | } |
343 | } |
08056730 |
344 | $output .= "</SELECT>\n"; |
345 | |
346 | if ($return) { |
347 | return $output; |
348 | } else { |
349 | echo $output; |
350 | } |
f9903ed0 |
351 | } |
352 | |
d897cae4 |
353 | function popup_form ($common, $options, $formname, $selected="", $nothing="choose", $help="", $helptext="", $return=false) { |
c1d57101 |
354 | /// Implements a complete little popup form |
355 | /// $common = the URL up to the point of the variable that changes |
356 | /// $options = A list of value-label pairs for the popup list |
357 | /// $formname = name must be unique on the page |
358 | /// $selected = the option that is already selected |
359 | /// $nothing = The label for the "no choice" option |
e5dfd0f3 |
360 | /// $help = The name of a help page if help is required |
361 | /// $helptext = The name of the label for the help button |
f9903ed0 |
362 | |
618b22c5 |
363 | if ($nothing == "choose") { |
364 | $nothing = get_string("choose")."..."; |
365 | } |
366 | |
08b36063 |
367 | $output = "<FORM TARGET=\"{$CFG->main_frame}\" NAME=$formname>"; |
3e50a139 |
368 | $output .= "<SELECT NAME=popup onChange=\"top.location=document.$formname.popup.options[document.$formname.popup.selectedIndex].value\">\n"; |
f9903ed0 |
369 | |
370 | if ($nothing != "") { |
d897cae4 |
371 | $output .= " <OPTION VALUE=\"javascript:void(0)\">$nothing</OPTION>\n"; |
f9903ed0 |
372 | } |
373 | |
374 | foreach ($options as $value => $label) { |
d897cae4 |
375 | if (substr($label,0,1) == "-") { |
376 | $output .= " <OPTION VALUE=\"\""; |
377 | } else { |
378 | $output .= " <OPTION VALUE=\"$common$value\""; |
379 | if ($value == $selected) { |
380 | $output .= " SELECTED"; |
381 | } |
f9903ed0 |
382 | } |
383 | if ($label) { |
d897cae4 |
384 | $output .= ">$label</OPTION>\n"; |
f9903ed0 |
385 | } else { |
d897cae4 |
386 | $output .= ">$value</OPTION>\n"; |
f9903ed0 |
387 | } |
388 | } |
d897cae4 |
389 | $output .= "</SELECT>"; |
d897cae4 |
390 | $output .= "</FORM>\n"; |
391 | |
392 | if ($return) { |
393 | return $output; |
394 | } else { |
9c9f7d77 |
395 | if ($help) { |
396 | helpbutton($help, $helptext); |
397 | } |
d897cae4 |
398 | echo $output; |
399 | } |
f9903ed0 |
400 | } |
401 | |
402 | |
403 | |
404 | function formerr($error) { |
c1d57101 |
405 | /// Prints some red text |
f9903ed0 |
406 | if (!empty($error)) { |
407 | echo "<font color=#ff0000>$error</font>"; |
408 | } |
409 | } |
410 | |
411 | |
412 | function validate_email ($address) { |
c1d57101 |
413 | /// Validates an email to make it makes sense. |
f9903ed0 |
414 | return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'. |
415 | '@'. |
416 | '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'. |
417 | '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', |
418 | $address)); |
419 | } |
420 | |
6ed3da1d |
421 | function get_slash_arguments($file="file.php") { |
422 | /// Searches the current environment variables for some slash arguments |
f9903ed0 |
423 | |
eaa50dbc |
424 | if (!$string = me()) { |
f9903ed0 |
425 | return false; |
426 | } |
eaa50dbc |
427 | |
6ed3da1d |
428 | $pathinfo = explode($file, $string); |
429 | |
bcdfe14e |
430 | if (!empty($pathinfo[1])) { |
431 | return $pathinfo[1]; |
6ed3da1d |
432 | } else { |
433 | return false; |
434 | } |
435 | } |
436 | |
437 | function parse_slash_arguments($string, $i=0) { |
438 | /// Extracts arguments from "/foo/bar/something" |
439 | /// eg http://mysite.com/script.php/foo/bar/something |
f9903ed0 |
440 | |
6ed3da1d |
441 | if (strpos($string, "..")) { // check for parent URLs |
780db230 |
442 | return false; |
443 | } |
6ed3da1d |
444 | if (strpos($string, "|")) { // check for pipes |
780db230 |
445 | return false; |
446 | } |
6ed3da1d |
447 | if (strpos($string, "`")) { // check for backquotes |
e2d89725 |
448 | return false; |
449 | } |
450 | |
6ed3da1d |
451 | $args = explode("/", $string); |
f9903ed0 |
452 | |
453 | if ($i) { // return just the required argument |
454 | return $args[$i]; |
455 | |
456 | } else { // return the whole array |
457 | array_shift($args); // get rid of the empty first one |
458 | return $args; |
459 | } |
460 | } |
461 | |
0095d5cd |
462 | function format_text_menu() { |
c1d57101 |
463 | /// Just returns an array of formats suitable for a popup menu |
0095d5cd |
464 | return array (FORMAT_MOODLE => get_string("formattext"), |
465 | FORMAT_HTML => get_string("formathtml") ); |
466 | } |
467 | |
60f18531 |
468 | function format_text($text, $format=FORMAT_MOODLE, $options=NULL) { |
c1d57101 |
469 | /// Given text in a variety of format codings, this function returns |
470 | /// the text as safe HTML. |
471 | /// |
472 | /// $text is raw text (originally from a user) |
473 | /// $format is one of the format constants, defined above |
0095d5cd |
474 | |
475 | switch ($format) { |
73f8658c |
476 | case FORMAT_HTML: |
477 | $text = replace_smilies($text); |
478 | return $text; |
479 | break; |
480 | |
481 | default: // FORMAT_MOODLE or anything else |
c9dda990 |
482 | if (!isset($options->smiley)) { |
483 | $options->smiley=true; |
484 | } |
485 | if (!isset($options->para)) { |
1a072208 |
486 | $options->para=true; |
c9dda990 |
487 | } |
0095d5cd |
488 | return text_to_html($text, $options->smiley, $options->para); |
489 | break; |
0095d5cd |
490 | } |
491 | } |
492 | |
493 | |
494 | function clean_text($text, $format) { |
c1d57101 |
495 | /// Given raw text (eg typed in by a user), this function cleans it up |
496 | /// and removes any nasty tags that could mess up Moodle pages. |
b7a3cf49 |
497 | |
3fe3851d |
498 | global $JAVASCRIPT_TAGS, $ALLOWED_TAGS; |
499 | |
500 | switch ($format) { // Does the same thing, currently, but it's nice to have the option |
0095d5cd |
501 | case FORMAT_MOODLE: |
3fe3851d |
502 | $text = strip_tags($text, $ALLOWED_TAGS); |
503 | foreach ($JAVASCRIPT_TAGS as $tag) { |
504 | $text = stri_replace($tag, "", $text); |
505 | } |
506 | return $text; |
0095d5cd |
507 | |
508 | case FORMAT_HTML: |
3fe3851d |
509 | $text = strip_tags($text, $ALLOWED_TAGS); |
510 | foreach ($JAVASCRIPT_TAGS as $tag) { |
511 | $text = stri_replace($tag, "", $text); |
512 | } |
513 | return $text; |
0095d5cd |
514 | } |
b7a3cf49 |
515 | } |
f9903ed0 |
516 | |
1a072208 |
517 | function replace_smilies($text) { |
c1d57101 |
518 | /// Replaces all known smileys in the text with image equivalents |
519 | |
1a072208 |
520 | global $CFG, $SMILEY_TEXT, $SMILEY_IMAGE; |
b7a3cf49 |
521 | |
1a072208 |
522 | return str_replace($SMILEY_TEXT, $SMILEY_IMAGE, $text); |
523 | } |
0095d5cd |
524 | |
909f539d |
525 | function text_to_html($text, $smiley=true, $para=true) { |
c1d57101 |
526 | /// Given plain text, makes it into HTML as nicely as possible. |
527 | /// May contain HTML tags already |
f9903ed0 |
528 | |
c1d57101 |
529 | /// Remove any whitespace that may be between HTML tags |
7b3be1b1 |
530 | $text = eregi_replace(">([[:space:]]+)<", "><", $text); |
531 | |
c1d57101 |
532 | /// Remove any returns that precede or follow HTML tags |
0eae8049 |
533 | $text = eregi_replace("([\n\r])<", " <", $text); |
534 | $text = eregi_replace(">([\n\r])", "> ", $text); |
7b3be1b1 |
535 | |
c1d57101 |
536 | /// Make lone URLs into links. eg http://moodle.com/ |
0be05df0 |
537 | $text = eregi_replace("([\n\r ([])([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", |
3fe3851d |
538 | "\\1<A HREF=\"\\2://\\3\\4\" TARGET=\"newpage\">\\2://\\3\\4</A>", $text); |
f9903ed0 |
539 | |
c1d57101 |
540 | /// eg www.moodle.com |
e1bf736f |
541 | $text = eregi_replace("([[:space:]])www\.([^[:space:]]*)([[:alnum:]#?/&=])", |
f9903ed0 |
542 | "\\1<A HREF=\"http://www.\\2\\3\" TARGET=\"newpage\">www.\\2\\3</A>", $text); |
543 | |
c1d57101 |
544 | /// Make returns into HTML newlines. |
f9903ed0 |
545 | $text = nl2br($text); |
546 | |
c1d57101 |
547 | /// Turn smileys into images. |
d69cb7f4 |
548 | if ($smiley) { |
1a072208 |
549 | $text = replace_smilies($text); |
d69cb7f4 |
550 | } |
f9903ed0 |
551 | |
c1d57101 |
552 | /// Wrap the whole thing in a paragraph tag if required |
909f539d |
553 | if ($para) { |
554 | return "<P>".$text."</P>"; |
555 | } else { |
556 | return $text; |
557 | } |
f9903ed0 |
558 | } |
559 | |
5af78ed2 |
560 | function highlight($needle, $haystack) { |
c1d57101 |
561 | /// This function will highlight instances of $needle in $haystack |
5af78ed2 |
562 | |
563 | $parts = explode(strtolower($needle), strtolower($haystack)); |
564 | |
565 | $pos = 0; |
566 | |
567 | foreach ($parts as $key => $part) { |
568 | $parts[$key] = substr($haystack, $pos, strlen($part)); |
569 | $pos += strlen($part); |
570 | |
571 | $parts[$key] .= "<SPAN CLASS=highlight>".substr($haystack, $pos, strlen($needle))."</SPAN>"; |
572 | $pos += strlen($needle); |
573 | } |
574 | |
575 | return (join('', $parts)); |
576 | } |
577 | |
f9903ed0 |
578 | |
9fa49e22 |
579 | |
580 | /// STANDARD WEB PAGE PARTS /////////////////////////////////////////////////// |
581 | |
582 | function print_header ($title="", $heading="", $navigation="", $focus="", $meta="", $cache=true, $button=" ", $menu="") { |
583 | // $title - appears top of window |
584 | // $heading - appears top of page |
585 | // $navigation - premade navigation string |
586 | // $focus - indicates form element eg inputform.password |
587 | // $meta - meta tags in the header |
588 | // $cache - should this page be cacheable? |
589 | // $button - HTML code for a button (usually for module editing) |
590 | // $menu - HTML code for a popup menu |
591 | global $USER, $CFG, $THEME; |
592 | |
593 | if (file_exists("$CFG->dirroot/theme/$CFG->theme/styles.php")) { |
594 | $styles = $CFG->stylesheet; |
595 | } else { |
596 | $styles = "$CFG->wwwroot/theme/standard/styles.php"; |
597 | } |
598 | |
599 | if ($navigation == "home") { |
600 | $home = true; |
601 | $navigation = ""; |
9d378732 |
602 | } else { |
603 | $home = false; |
9fa49e22 |
604 | } |
605 | |
606 | if ($button == "") { |
607 | $button = " "; |
608 | } |
609 | |
610 | if (!$menu and $navigation) { |
611 | if (isset($USER->id)) { |
612 | $menu = "<FONT SIZE=2><A TARGET=_parent HREF=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</A></FONT>"; |
613 | } else { |
614 | $menu = "<FONT SIZE=2><A TARGET=_parent HREF=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</A></FONT>"; |
615 | } |
616 | } |
617 | |
618 | // Specify character set ... default is iso-8859-1 but some languages might need something else |
619 | // Could be optimised by carrying the charset variable around in $USER |
620 | if (current_language() == "en") { |
621 | $meta = "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=iso-8859-1\">\n$meta\n"; |
622 | } else { |
623 | $meta = "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=".get_string("thischarset")."\">\n$meta\n"; |
624 | } |
625 | |
626 | if ($CFG->langdir == "RTL") { |
627 | $direction = " DIR=\"RTL\""; |
628 | } else { |
629 | $direction = " DIR=\"LTR\""; |
630 | } |
631 | |
632 | if (!$cache) { // Do everything we can to prevent clients and proxies caching |
633 | @header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
634 | @header("Pragma: no-cache"); |
635 | $meta .= "\n<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">"; |
636 | $meta .= "\n<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">"; |
637 | } |
638 | |
639 | include ("$CFG->dirroot/theme/$CFG->theme/header.html"); |
640 | } |
641 | |
642 | function print_footer ($course=NULL) { |
643 | // Can provide a course object to make the footer contain a link to |
644 | // to the course home page, otherwise the link will go to the site home |
645 | global $USER, $CFG, $THEME; |
646 | |
647 | |
648 | /// Course links |
649 | if ($course) { |
650 | if ($course == "home") { // special case for site home page - please do not remove |
9a9a2907 |
651 | $homelink = "<P ALIGN=center><A TITLE=\"Moodle $CFG->release ($CFG->version)\" HREF=\"http://moodle.com/\" TARGET=\"_blank\">"; |
9fa49e22 |
652 | $homelink .= "<BR><IMG WIDTH=130 HEIGHT=19 SRC=\"pix/madewithmoodle2.gif\" BORDER=0></A></P>"; |
653 | $course = get_site(); |
654 | $homepage = true; |
655 | } else { |
08b36063 |
656 | $homelink = "<A TARGET=\"{$CFG->main_frame}\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>"; |
9fa49e22 |
657 | } |
658 | } else { |
08b36063 |
659 | $homelink = "<A TARGET=\"{$CFG->main_frame}\" HREF=\"$CFG->wwwroot\">".get_string("home")."</A>"; |
9fa49e22 |
660 | $course = get_site(); |
661 | } |
662 | |
663 | /// User links |
9d378732 |
664 | if (isset($USER->realuser)) { |
9fa49e22 |
665 | if ($realuser = get_record("user", "id", $USER->realuser)) { |
666 | $realuserinfo = " [<A HREF=\"$CFG->wwwroot/course/loginas.php?id=$course->id&return=$realuser->id\">$realuser->firstname $realuser->lastname</A>] "; |
667 | } |
9d378732 |
668 | } else { |
669 | $realuserinfo = ""; |
9fa49e22 |
670 | } |
671 | |
9c9f7d77 |
672 | if (isset($USER->id) and $USER->id) { |
9fa49e22 |
673 | $username = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id\">$USER->firstname $USER->lastname</A>"; |
674 | $loggedinas = $realuserinfo.get_string("loggedinas", "moodle", "$username"). |
675 | " (<A HREF=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</A>)"; |
676 | } else { |
677 | $loggedinas = get_string("loggedinnot", "moodle"). |
678 | " (<A HREF=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</A>)"; |
679 | } |
680 | |
681 | include ("$CFG->dirroot/theme/$CFG->theme/footer.html"); |
682 | } |
683 | |
684 | |
685 | |
686 | function print_navigation ($navigation) { |
687 | global $CFG; |
688 | |
689 | if ($navigation) { |
690 | if (! $site = get_site()) { |
691 | $site->shortname = get_string("home");; |
692 | } |
08b36063 |
693 | echo "<A TARGET=\"{$CFG->main_frame}\" HREF=\"$CFG->wwwroot/\">$site->shortname</A> -> $navigation"; |
9fa49e22 |
694 | } |
695 | } |
696 | |
697 | function print_heading($text, $align="CENTER", $size=3) { |
7d8f674d |
698 | echo "<P ALIGN=\"$align\"><FONT SIZE=\"$size\"><B>".stripslashes_safe($text)."</B></FONT></P>"; |
9fa49e22 |
699 | } |
700 | |
701 | function print_heading_with_help($text, $helppage, $module="moodle") { |
702 | // Centered heading with attached help button (same title text) |
7d8f674d |
703 | echo "<P ALIGN=\"CENTER\"><FONT SIZE=\"3\"><B>".stripslashes_safe($text); |
9fa49e22 |
704 | helpbutton($helppage, $text, $module); |
705 | echo "</B></FONT></P>"; |
706 | } |
707 | |
708 | function print_continue($link) { |
9fa49e22 |
709 | |
710 | if (!$link) { |
607809b3 |
711 | $link = $_SERVER["HTTP_REFERER"]; |
9fa49e22 |
712 | } |
713 | |
714 | print_heading("<A HREF=\"$link\">".get_string("continue")."</A>"); |
715 | } |
716 | |
717 | |
718 | function print_simple_box($message, $align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") { |
719 | print_simple_box_start($align, $width, $color, $padding, $class); |
7d8f674d |
720 | echo stripslashes_safe($message); |
9fa49e22 |
721 | print_simple_box_end(); |
722 | } |
723 | |
724 | function print_simple_box_start($align="", $width="", $color="#FFFFFF", $padding=5, $class="generalbox") { |
725 | global $THEME; |
726 | |
727 | if ($align) { |
9d378732 |
728 | $align = "ALIGN=\"$align\""; |
9fa49e22 |
729 | } |
730 | if ($width) { |
9d378732 |
731 | $width = "WIDTH=\"$width\""; |
9fa49e22 |
732 | } |
9d378732 |
733 | echo "<table $align $width class=\"$class\" border=\"0\" cellpadding=\"$padding\" cellspacing=\"0\"><tr><td bgcolor=\"$color\" class=\"$class"."content\">"; |
9fa49e22 |
734 | } |
735 | |
736 | function print_simple_box_end() { |
737 | echo "</td></tr></table>"; |
738 | } |
739 | |
740 | function print_single_button($link, $options, $label="OK") { |
741 | echo "<FORM ACTION=\"$link\" METHOD=GET>"; |
742 | if ($options) { |
743 | foreach ($options as $name => $value) { |
744 | echo "<INPUT TYPE=hidden NAME=\"$name\" VALUE=\"$value\">"; |
745 | } |
746 | } |
747 | echo "<INPUT TYPE=submit VALUE=\"$label\"></FORM>"; |
748 | } |
749 | |
750 | function print_spacer($height=1, $width=1, $br=true) { |
751 | global $CFG; |
752 | echo "<IMG HEIGHT=\"$height\" WIDTH=\"$width\" SRC=\"$CFG->wwwroot/pix/spacer.gif\" ALT=\"\">"; |
753 | if ($br) { |
754 | echo "<BR \>\n"; |
755 | } |
756 | } |
757 | |
758 | function print_file_picture($path, $courseid=0, $height="", $width="", $link="") { |
759 | // Given the path to a picture file in a course, or a URL, |
760 | // this function includes the picture in the page. |
761 | global $CFG; |
762 | |
763 | if ($height) { |
764 | $height = "HEIGHT=\"$height\""; |
765 | } |
766 | if ($width) { |
767 | $width = "WIDTH=\"$width\""; |
768 | } |
769 | if ($link) { |
770 | echo "<A HREF=\"$link\">"; |
771 | } |
772 | if (substr(strtolower($path), 0, 7) == "http://") { |
773 | echo "<IMG BORDER=0 $height $width SRC=\"$path\">"; |
774 | |
775 | } else if ($courseid) { |
776 | echo "<IMG BORDER=0 $height $width SRC=\""; |
777 | if ($CFG->slasharguments) { // Use this method if possible for better caching |
778 | echo "$CFG->wwwroot/file.php/$courseid/$path"; |
779 | } else { |
3f396065 |
780 | echo "$CFG->wwwroot/file.php?file=/$courseid/$path"; |
9fa49e22 |
781 | } |
782 | echo "\">"; |
783 | } else { |
784 | echo "Error: must pass URL or course"; |
785 | } |
786 | if ($link) { |
787 | echo "</A>"; |
788 | } |
789 | } |
790 | |
791 | function print_user_picture($userid, $courseid, $picture, $large=false, $returnstring=false, $link=true) { |
792 | global $CFG; |
793 | |
794 | if ($link) { |
795 | $output = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$userid&course=$courseid\">"; |
796 | } else { |
797 | $output = ""; |
798 | } |
799 | if ($large) { |
800 | $file = "f1.jpg"; |
801 | $size = 100; |
802 | } else { |
803 | $file = "f2.jpg"; |
804 | $size = 35; |
805 | } |
806 | if ($picture) { |
807 | if ($CFG->slasharguments) { // Use this method if possible for better caching |
808 | $output .= "<IMG SRC=\"$CFG->wwwroot/user/pix.php/$userid/$file\" BORDER=0 WIDTH=$size HEIGHT=$size ALT=\"\">"; |
809 | } else { |
810 | $output .= "<IMG SRC=\"$CFG->wwwroot/user/pix.php?file=/$userid/$file\" BORDER=0 WIDTH=$size HEIGHT=$size ALT=\"\">"; |
811 | } |
812 | } else { |
813 | $output .= "<IMG SRC=\"$CFG->wwwroot/user/default/$file\" BORDER=0 WIDTH=$size HEIGHT=$size ALT=\"\">"; |
814 | } |
815 | if ($link) { |
816 | $output .= "</A>"; |
817 | } |
818 | |
819 | if ($returnstring) { |
820 | return $output; |
821 | } else { |
822 | echo $output; |
823 | } |
824 | } |
825 | |
826 | function print_table($table) { |
827 | // Prints a nicely formatted table. |
828 | // $table is an object with several properties. |
829 | // $table->head is an array of heading names. |
830 | // $table->align is an array of column alignments |
831 | // $table->size is an array of column sizes |
832 | // $table->data[] is an array of arrays containing the data. |
833 | // $table->width is an percentage of the page |
834 | // $table->cellpadding padding on each cell |
835 | // $table->cellspacing spacing between cells |
836 | |
837 | if (isset($table->align)) { |
838 | foreach ($table->align as $key => $aa) { |
839 | if ($aa) { |
840 | $align[$key] = " ALIGN=\"$aa\""; |
841 | } else { |
842 | $align[$key] = ""; |
843 | } |
844 | } |
845 | } |
846 | if (isset($table->size)) { |
847 | foreach ($table->size as $key => $ss) { |
848 | if ($ss) { |
849 | $size[$key] = " WIDTH=\"$ss\""; |
850 | } else { |
851 | $size[$key] = ""; |
852 | } |
853 | } |
854 | } |
855 | |
9d378732 |
856 | if (empty($table->width)) { |
9fa49e22 |
857 | $table->width = "80%"; |
858 | } |
859 | |
9d378732 |
860 | if (empty($table->cellpadding)) { |
9fa49e22 |
861 | $table->cellpadding = "5"; |
862 | } |
863 | |
9d378732 |
864 | if (empty($table->cellspacing)) { |
9fa49e22 |
865 | $table->cellspacing = "1"; |
866 | } |
867 | |
868 | print_simple_box_start("CENTER", "$table->width", "#FFFFFF", 0); |
869 | echo "<TABLE WIDTH=100% BORDER=0 valign=top align=center "; |
870 | echo " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"generaltable\">\n"; |
871 | |
b79f41cd |
872 | if (!empty($table->head)) { |
9fa49e22 |
873 | echo "<TR>"; |
874 | foreach ($table->head as $key => $heading) { |
9d378732 |
875 | if (!isset($size[$key])) { |
876 | $size[$key] = ""; |
877 | } |
878 | if (!isset($align[$key])) { |
879 | $align[$key] = ""; |
880 | } |
9fa49e22 |
881 | echo "<TH VALIGN=top ".$align[$key].$size[$key]." NOWRAP class=\"generaltableheader\">$heading</TH>"; |
882 | } |
883 | echo "</TR>\n"; |
884 | } |
885 | |
886 | foreach ($table->data as $row) { |
887 | echo "<TR VALIGN=TOP>"; |
888 | foreach ($row as $key => $item) { |
9d378732 |
889 | if (!isset($size[$key])) { |
890 | $size[$key] = ""; |
891 | } |
892 | if (!isset($align[$key])) { |
893 | $align[$key] = ""; |
894 | } |
9fa49e22 |
895 | echo "<TD ".$align[$key].$size[$key]." class=\"generaltablecell\">$item</TD>"; |
896 | } |
897 | echo "</TR>\n"; |
898 | } |
899 | echo "</TABLE>\n"; |
900 | print_simple_box_end(); |
901 | |
902 | return true; |
903 | } |
904 | |
905 | function print_editing_switch($courseid) { |
906 | global $CFG, $USER; |
907 | |
908 | if (isteacher($courseid)) { |
909 | if ($USER->editing) { |
910 | echo "<A HREF=\"$CFG->wwwroot/course/view.php?id=$courseid&edit=off\">Turn editing off</A>"; |
911 | } else { |
912 | echo "<A HREF=\"$CFG->wwwroot/course/view.php?id=$courseid&edit=on\">Turn editing on</A>"; |
913 | } |
914 | } |
915 | } |
916 | |
917 | function print_textarea($richedit, $rows, $cols, $width, $height, $name, $value="") { |
7d8f674d |
918 | /// Prints a richtext field or a normal textarea |
9fa49e22 |
919 | global $CFG, $THEME; |
920 | |
921 | if ($richedit) { |
922 | echo "<object id=richedit style=\"BACKGROUND-COLOR: buttonface\""; |
923 | echo " data=\"$CFG->wwwroot/lib/rte/richedit.html\""; |
924 | echo " width=\"$width\" height=\"$height\" "; |
925 | echo " type=\"text/x-scriptlet\" VIEWASTEXT></object>\n"; |
926 | echo "<TEXTAREA style=\"display:none\" NAME=\"$name\" ROWS=1 COLS=1>"; |
927 | p($value); |
928 | echo "</TEXTAREA>\n"; |
929 | } else { |
930 | echo "<TEXTAREA name=\"$name\" rows=\"$rows\" cols=\"$cols\" wrap=virtual>"; |
931 | p($value); |
932 | echo "</TEXTAREA>\n"; |
933 | } |
934 | } |
935 | |
936 | function print_richedit_javascript($form, $name, $source="no") { |
937 | echo "<SCRIPT language=\"JavaScript\" event=\"onload\" for=\"window\">\n"; |
938 | echo " document.richedit.options = \"history=no;source=$source\";"; |
939 | echo " document.richedit.docHtml = $form.$name.innerText;"; |
940 | echo "</SCRIPT>"; |
941 | } |
942 | |
943 | |
944 | function update_course_icon($courseid) { |
945 | // Used to be an icon, but it's now a simple form button |
946 | global $CFG, $USER; |
947 | |
948 | if (isteacher($courseid)) { |
9c9f7d77 |
949 | if (!empty($USER->editing)) { |
9fa49e22 |
950 | $string = get_string("turneditingoff"); |
951 | $edit = "off"; |
952 | } else { |
953 | $string = get_string("turneditingon"); |
954 | $edit = "on"; |
955 | } |
956 | return "<FORM TARGET=_parent METHOD=GET ACTION=\"$CFG->wwwroot/course/view.php\">". |
957 | "<INPUT TYPE=hidden NAME=id VALUE=\"$courseid\">". |
958 | "<INPUT TYPE=hidden NAME=edit VALUE=\"$edit\">". |
959 | "<INPUT TYPE=submit VALUE=\"$string\"></FORM>"; |
960 | } |
961 | } |
962 | |
963 | function update_module_button($moduleid, $courseid, $string) { |
964 | // Prints the editing button on a module "view" page |
965 | global $CFG; |
966 | |
967 | if (isteacher($courseid)) { |
968 | $string = get_string("updatethis", "", $string); |
969 | return "<FORM TARGET=_parent METHOD=GET ACTION=\"$CFG->wwwroot/course/mod.php\">". |
970 | "<INPUT TYPE=hidden NAME=update VALUE=\"$moduleid\">". |
971 | "<INPUT TYPE=hidden NAME=return VALUE=\"true\">". |
972 | "<INPUT TYPE=submit VALUE=\"$string\"></FORM>"; |
973 | } |
974 | } |
975 | |
976 | |
977 | function navmenu($course, $cm=NULL) { |
978 | // Given a course and a (current) coursemodule |
979 | // This function returns a small popup menu with all the |
980 | // course activity modules in it, as a navigation menu |
981 | // The data is taken from the serialised array stored in |
982 | // the course record |
983 | |
984 | global $CFG; |
985 | |
986 | if ($cm) { |
987 | $cm = $cm->id; |
988 | } |
989 | |
990 | if ($course->format == 'weeks') { |
991 | $strsection = get_string("week"); |
992 | } else { |
993 | $strsection = get_string("topic"); |
994 | } |
995 | |
996 | if (!$modinfo = unserialize($course->modinfo)) { |
997 | return ""; |
998 | } |
999 | $section = -1; |
1000 | $selected = ""; |
1001 | foreach ($modinfo as $mod) { |
1002 | if ($mod->section > 0 and $section <> $mod->section) { |
1003 | $menu[] = "-------------- $strsection $mod->section --------------"; |
1004 | } |
1005 | $section = $mod->section; |
1006 | $url = "$mod->mod/view.php?id=$mod->cm"; |
1007 | if ($cm == $mod->cm) { |
1008 | $selected = $url; |
1009 | } |
1010 | $mod->name = urldecode($mod->name); |
1011 | if (strlen($mod->name) > 55) { |
1012 | $mod->name = substr($mod->name, 0, 50)."..."; |
1013 | } |
1014 | $menu[$url] = $mod->name; |
1015 | } |
1016 | |
1017 | return popup_form("$CFG->wwwroot/mod/", $menu, "navmenu", $selected, get_string("jumpto"), "", "", true); |
1018 | } |
1019 | |
1020 | |
1021 | |
1022 | function print_date_selector($day, $month, $year, $currenttime=0) { |
1023 | // Currenttime is a default timestamp in GMT |
1024 | // Prints form items with the names $day, $month and $year |
1025 | |
1026 | if (!$currenttime) { |
1027 | $currenttime = time(); |
1028 | } |
1029 | $currentdate = usergetdate($currenttime); |
1030 | |
1031 | for ($i=1; $i<=31; $i++) { |
1032 | $days[$i] = "$i"; |
1033 | } |
1034 | for ($i=1; $i<=12; $i++) { |
39e018b3 |
1035 | $months[$i] = userdate(gmmktime(12,0,0,$i,1,2000), "%B"); |
9fa49e22 |
1036 | } |
1037 | for ($i=2000; $i<=2010; $i++) { |
1038 | $years[$i] = $i; |
1039 | } |
47f1da80 |
1040 | choose_from_menu($days, $day, $currentdate['mday'], ""); |
1041 | choose_from_menu($months, $month, $currentdate['mon'], ""); |
1042 | choose_from_menu($years, $year, $currentdate['year'], ""); |
9fa49e22 |
1043 | } |
1044 | |
1045 | function print_time_selector($hour, $minute, $currenttime=0) { |
1046 | // Currenttime is a default timestamp in GMT |
1047 | // Prints form items with the names $hour and $minute |
1048 | |
1049 | if (!$currenttime) { |
1050 | $currenttime = time(); |
1051 | } |
1052 | $currentdate = usergetdate($currenttime); |
1053 | for ($i=0; $i<=23; $i++) { |
1054 | $hours[$i] = sprintf("%02d",$i); |
1055 | } |
1056 | for ($i=0; $i<=59; $i++) { |
1057 | $minutes[$i] = sprintf("%02d",$i); |
1058 | } |
47f1da80 |
1059 | choose_from_menu($hours, $hour, $currentdate['hours'], ""); |
1060 | choose_from_menu($minutes, $minute, $currentdate['minutes'], ""); |
9fa49e22 |
1061 | } |
1062 | |
1063 | function error ($message, $link="") { |
1064 | global $CFG, $SESSION; |
1065 | |
1066 | print_header(get_string("error")); |
1067 | echo "<BR>"; |
1068 | print_simple_box($message, "center", "", "#FFBBBB"); |
1069 | |
1070 | if (!$link) { |
1071 | if ( !empty($SESSION->fromurl) ) { |
1072 | $link = "$SESSION->fromurl"; |
1073 | unset($SESSION->fromurl); |
9fa49e22 |
1074 | } else { |
1075 | $link = "$CFG->wwwroot"; |
1076 | } |
1077 | } |
1078 | print_continue($link); |
1079 | print_footer(); |
1080 | die; |
1081 | } |
1082 | |
1083 | function helpbutton ($page, $title="", $module="moodle", $image=true, $linktext=false, $text="") { |
1084 | // $page = the keyword that defines a help page |
1085 | // $title = the title of links, rollover tips, alt tags etc |
1086 | // $module = which module is the page defined in |
1087 | // $image = use a help image for the link? (true/false/"both") |
1088 | // $text = if defined then this text is used in the page, and |
1089 | // the $page variable is ignored. |
1090 | global $CFG; |
1091 | |
1092 | if ($module == "") { |
1093 | $module = "moodle"; |
1094 | } |
1095 | |
1096 | if ($image) { |
1097 | if ($linktext) { |
1098 | $linkobject = "$title<IMG align=\"absmiddle\" BORDER=0 HEIGHT=17 WIDTH=22 ALT=\"\" SRC=\"$CFG->wwwroot/pix/help.gif\">"; |
1099 | } else { |
1100 | $linkobject = "<IMG align=\"absmiddle\" BORDER=0 HEIGHT=17 WIDTH=22 ALT=\"$title\" SRC=\"$CFG->wwwroot/pix/help.gif\">"; |
1101 | } |
1102 | } else { |
1103 | $linkobject = $title; |
1104 | } |
1105 | if ($text) { |
1106 | $url = "/help.php?module=$module&text=".htmlentities(urlencode($text)); |
1107 | } else { |
1108 | $url = "/help.php?module=$module&file=$page.html"; |
1109 | } |
1110 | link_to_popup_window ($url, "popup", $linkobject, 400, 500, $title); |
1111 | } |
1112 | |
1113 | function notice ($message, $link="") { |
750ab759 |
1114 | global $CFG, $THEME; |
9fa49e22 |
1115 | |
1116 | if (!$link) { |
750ab759 |
1117 | if (!empty($_SERVER["HTTP_REFERER"])) { |
1118 | $link = $_SERVER["HTTP_REFERER"]; |
1119 | } else { |
1120 | $link = $CFG->wwwroot; |
1121 | } |
9fa49e22 |
1122 | } |
1123 | |
1124 | echo "<BR>"; |
1125 | print_simple_box($message, "center", "", "$THEME->cellheading"); |
1126 | print_heading("<A HREF=\"$link\">".get_string("continue")."</A>"); |
1127 | print_footer(get_site()); |
1128 | die; |
1129 | } |
1130 | |
1131 | function notice_yesno ($message, $linkyes, $linkno) { |
1132 | global $THEME; |
1133 | |
1134 | print_simple_box_start("center", "", "$THEME->cellheading"); |
1135 | echo "<P ALIGN=CENTER><FONT SIZE=3>$message</FONT></P>"; |
1136 | echo "<P ALIGN=CENTER><FONT SIZE=3><B>"; |
1137 | echo "<A HREF=\"$linkyes\">".get_string("yes")."</A>"; |
1138 | echo " "; |
1139 | echo "<A HREF=\"$linkno\">".get_string("no")."</A>"; |
1140 | echo "</B></FONT></P>"; |
1141 | print_simple_box_end(); |
1142 | } |
1143 | |
08b36063 |
1144 | /* This is an alternate beginning to the redirect function |
1145 | function redirect($url, $message="", $delay=false) { |
1146 | // Uses META tags to redirect the user, after printing a notice |
1147 | if(!empty($message) && $delay == false): |
1148 | $delay = 10; |
1149 | else: |
1150 | $delay = 0; |
1151 | endif; |
1152 | */ |
1153 | |
9fa49e22 |
1154 | function redirect($url, $message="", $delay=0) { |
1155 | // Uses META tags to redirect the user, after printing a notice |
1156 | |
1157 | echo "<META HTTP-EQUIV='Refresh' CONTENT='$delay; URL=$url'>"; |
1158 | |
1159 | if (!empty($message)) { |
1160 | print_header(); |
1161 | echo "<CENTER>"; |
1162 | echo "<P>$message</P>"; |
1163 | echo "<P>( <A HREF=\"$url\">".get_string("continue")."</A> )</P>"; |
1164 | echo "</CENTER>"; |
1165 | } |
1166 | die; |
1167 | } |
1168 | |
99988d1a |
1169 | function notify ($message, $color="red", $align="center") { |
1170 | echo "<p align=\"$align\"><b><font color=\"$color\">$message</font></b></p>\n"; |
9fa49e22 |
1171 | } |
1172 | |
1173 | |
f9903ed0 |
1174 | ?> |