f9903ed0 |
1 | <?PHP // $Id$ |
2 | |
3 | // |
4 | // moodlelib.php |
5 | // |
6 | // Large collection of useful functions used by many parts of Moodle. |
7 | // |
8 | // Martin Dougiamas, 2000 |
9 | // |
10 | |
f9903ed0 |
11 | /// STANDARD WEB PAGE PARTS /////////////////////////////////////////////////// |
12 | |
d897cae4 |
13 | function print_header ($title="", $heading="", $navigation="", $focus="", $meta="", $cache=true, $button="", $menu="") { |
f9903ed0 |
14 | // $title - appears top of window |
15 | // $heading - appears top of page |
16 | // $navigation - premade navigation string |
17 | // $focus - indicates form element eg inputform.password |
18 | // $meta - meta tags in the header |
c7e3ac2a |
19 | // $cache - should this page be cacheable? |
d897cae4 |
20 | // $button - HTML code for a button (usually for module editing) |
21 | // $menu - HTML code for a popup menu |
f9903ed0 |
22 | global $USER, $CFG, $THEME; |
23 | |
24 | if (file_exists("$CFG->dirroot/theme/$CFG->theme/styles.css")) { |
25 | $styles = "$CFG->wwwroot/theme/$CFG->theme/styles.css"; |
26 | } else { |
27 | $styles = "$CFG->wwwroot/theme/standard/styles.css"; |
28 | } |
4efeb8c3 |
29 | |
a28af1fe |
30 | if ($navigation == "home") { |
31 | $home = true; |
32 | $navigation = ""; |
33 | } |
34 | |
d897cae4 |
35 | if (!$menu and $navigation) { |
4efeb8c3 |
36 | if (isset($USER->id)) { |
d897cae4 |
37 | $menu = "<FONT SIZE=2><A TARGET=_parent HREF=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</A></FONT>"; |
4efeb8c3 |
38 | } else { |
d897cae4 |
39 | $menu = "<FONT SIZE=2><A TARGET=_parent HREF=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</A></FONT>"; |
4efeb8c3 |
40 | } |
41 | } |
4bfa92e7 |
42 | |
43 | // Specify character set ... default is iso-8859-1 but some languages might need something else |
44 | // Could be optimised by carrying the charset variable around in $USER |
45 | if (current_language() == "en") { |
46 | $meta .= "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=iso-8859-1\">\n"; |
47 | } else { |
48 | $meta .= "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=".get_string("thischarset")."\">\n"; |
49 | } |
541117b6 |
50 | |
51 | if ($CFG->langdir == "RTL") { |
52 | $direction = " DIR=\"RTL\""; |
53 | } else { |
54 | $direction = " DIR=\"LTR\""; |
55 | } |
f9903ed0 |
56 | |
57 | if (!$cache) { // Do everything we can to prevent clients and proxies caching |
58 | @header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
59 | @header("Pragma: no-cache"); |
60 | $meta .= "\n<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">"; |
61 | $meta .= "\n<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">"; |
62 | } |
4bfa92e7 |
63 | |
f9903ed0 |
64 | include ("$CFG->dirroot/theme/$CFG->theme/header.html"); |
65 | } |
66 | |
67 | function print_footer ($course=NULL) { |
68 | // Can provide a course object to make the footer contain a link to |
69 | // to the course home page, otherwise the link will go to the site home |
70 | global $USER, $CFG, $THEME; |
71 | |
65ccdd8c |
72 | |
73 | /// Course links |
f9903ed0 |
74 | if ($course) { |
28b1e3ec |
75 | if ($course == "home") { // special case for site home page - please do not remove |
a28af1fe |
76 | $homelink = "<P ALIGN=center><A TITLE=\"Moodle $CFG->release ($CFG->version)\" HREF=\"http://moodle.com/\">"; |
87a2fa03 |
77 | $homelink .= "<BR><IMG WIDTH=130 HEIGHT=19 SRC=\"pix/madewithmoodle2.gif\" BORDER=0></A></P>"; |
65ccdd8c |
78 | $course = get_site(); |
6f3ba7c9 |
79 | $homepage = true; |
d82c8545 |
80 | } else { |
81 | $homelink = "<A TARGET=_top HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>"; |
82 | } |
f9903ed0 |
83 | } else { |
d82c8545 |
84 | $homelink = "<A TARGET=_top HREF=\"$CFG->wwwroot\">".get_string("home")."</A>"; |
65ccdd8c |
85 | $course = get_site(); |
f9903ed0 |
86 | } |
65ccdd8c |
87 | |
88 | /// User links |
3ce2f1e0 |
89 | if ($USER->realuser) { |
90 | if ($realuser = get_record("user", "id", $USER->realuser)) { |
65ccdd8c |
91 | $realuserinfo = " [<A HREF=\"$CFG->wwwroot/course/loginas.php?id=$course->id&return=$realuser->id\">$realuser->firstname $realuser->lastname</A>] "; |
3ce2f1e0 |
92 | } |
93 | } |
65ccdd8c |
94 | |
3ce2f1e0 |
95 | if ($USER->id) { |
65ccdd8c |
96 | $username = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id\">$USER->firstname $USER->lastname</A>"; |
7d95ed97 |
97 | $loggedinas = $realuserinfo.get_string("loggedinas", "moodle", "$username"). |
3ce2f1e0 |
98 | " (<A HREF=\"$CFG->wwwroot/login/logout.php\">".get_string("logout")."</A>)"; |
65ccdd8c |
99 | } else { |
3ce2f1e0 |
100 | $loggedinas = get_string("loggedinnot", "moodle"). |
a7b9e8bc |
101 | " (<A HREF=\"$CFG->wwwroot/login/index.php\">".get_string("login")."</A>)"; |
65ccdd8c |
102 | } |
3ce2f1e0 |
103 | |
f9903ed0 |
104 | include ("$CFG->dirroot/theme/$CFG->theme/footer.html"); |
105 | } |
106 | |
65ccdd8c |
107 | |
108 | |
f9903ed0 |
109 | function print_navigation ($navigation) { |
110 | global $CFG; |
111 | |
112 | if ($navigation) { |
a83fded1 |
113 | if (! $site = get_site()) { |
bcc83c41 |
114 | $site->shortname = get_string("home");; |
115 | } |
a2eaeb91 |
116 | echo "<A TARGET=_top HREF=\"$CFG->wwwroot/\">$site->shortname</A> -> $navigation"; |
f9903ed0 |
117 | } |
118 | } |
119 | |
2a46a71b |
120 | function print_heading($text, $align="CENTER", $size=3) { |
08beb332 |
121 | echo "<P ALIGN=\"$align\"><FONT SIZE=\"$size\"><B>".stripslashes($text)."</B></FONT></P>"; |
f9903ed0 |
122 | } |
123 | |
377f3c9e |
124 | function print_heading_with_help($text, $helppage, $module="moodle") { |
125 | // Centered heading with attached help button (same title text) |
126 | echo "<P ALIGN=\"CENTER\"><FONT SIZE=\"3\"><B>".stripslashes($text); |
127 | helpbutton($helppage, $text, $module); |
128 | echo "</B></FONT></P>"; |
129 | } |
130 | |
dbcb136a |
131 | function print_continue($link) { |
132 | global $HTTP_REFERER; |
133 | |
134 | if (!$link) { |
135 | $link = $HTTP_REFERER; |
136 | } |
137 | |
138 | print_heading("<A HREF=\"$link\">".get_string("continue")."</A>"); |
139 | } |
140 | |
141 | |
4216daa6 |
142 | function print_simple_box($message, $align="", $width="", $color="#FFFFFF", $padding=5, $border=1) { |
143 | print_simple_box_start($align, $width, $color, $padding, $border); |
16c0beb0 |
144 | echo "<P>".stripslashes($message)."</P>"; |
f9903ed0 |
145 | print_simple_box_end(); |
146 | } |
147 | |
4216daa6 |
148 | function print_simple_box_start($align="", $width="", $color="#FFFFFF", $padding=5, $border=1) { |
f9903ed0 |
149 | global $THEME; |
150 | |
151 | if ($align) { |
152 | $tablealign = "ALIGN=\"$align\""; |
153 | } |
154 | if ($width) { |
155 | $tablewidth = "WIDTH=\"$width\""; |
156 | $innertablewidth = "WIDTH=\"100%\""; |
157 | } |
4216daa6 |
158 | echo "<TABLE $tablealign $tablewidth BORDER=0 CELLPADDING=\"$border\" CELLSPACING=0>"; |
f9903ed0 |
159 | echo "<TR><TD BGCOLOR=\"$THEME->borders\">\n"; |
160 | echo "<TABLE $innertablewidth BORDER=0 CELLPADDING=\"$padding\" CELLSPACING=0><TR><TD BGCOLOR=\"$color\">"; |
161 | } |
162 | |
163 | function print_simple_box_end() { |
164 | echo "</TD></TR></TABLE>"; |
165 | echo "</TD></TR></TABLE>"; |
166 | } |
167 | |
168 | function print_single_button($link, $options, $label="OK") { |
169 | echo "<FORM ACTION=\"$link\" METHOD=GET>"; |
dbcb136a |
170 | if ($options) { |
171 | foreach ($options as $name => $value) { |
172 | echo "<INPUT TYPE=hidden NAME=\"$name\" VALUE=\"$value\">"; |
173 | } |
f9903ed0 |
174 | } |
175 | echo "<INPUT TYPE=submit VALUE=\"$label\"></FORM>"; |
176 | } |
177 | |
19a55d67 |
178 | function print_spacer($height=1, $width=1, $br=true) { |
179 | global $CFG; |
180 | echo "<IMG HEIGHT=\"$height\" WIDTH=\"$width\" SRC=\"$CFG->wwwroot/pix/spacer.gif\" ALT=\"\">"; |
181 | if ($br) { |
182 | echo "<BR>\n"; |
183 | } |
184 | } |
185 | |
f8e4809a |
186 | function print_file_picture($path, $courseid=0, $height="", $width="", $link="") { |
187 | // Given the path to a picture file in a course, or a URL, |
188 | // this function includes the picture in the page. |
189 | global $CFG; |
190 | |
191 | if ($height) { |
192 | $height = "HEIGHT=\"$height\""; |
193 | } |
194 | if ($width) { |
195 | $width = "WIDTH=\"$width\""; |
196 | } |
197 | if ($link) { |
198 | echo "<A HREF=\"$link\">"; |
199 | } |
200 | if (substr(strtolower($path), 0, 7) == "http://") { |
201 | echo "<IMG BORDER=0 $height $width SRC=\"$path\">"; |
55c26207 |
202 | |
f8e4809a |
203 | } else if ($courseid) { |
204 | echo "<IMG BORDER=0 $height $width SRC=\""; |
205 | if ($CFG->slasharguments) { // Use this method if possible for better caching |
206 | echo "$CFG->wwwroot/file.php/$courseid/$path"; |
207 | } else { |
208 | echo "$CFG->wwwroot/file.php?file=$courseid/$path"; |
209 | } |
210 | echo "\">"; |
211 | } else { |
212 | echo "Error: must pass URL or course"; |
213 | } |
214 | if ($link) { |
215 | echo "</A>"; |
216 | } |
217 | } |
218 | |
136dabd8 |
219 | function print_user_picture($userid, $courseid, $picture, $large=false, $returnstring=false) { |
f9903ed0 |
220 | global $CFG; |
221 | |
136dabd8 |
222 | $output = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$userid&course=$courseid\">"; |
f9903ed0 |
223 | if ($large) { |
224 | $file = "f1.jpg"; |
225 | $size = 100; |
226 | } else { |
227 | $file = "f2.jpg"; |
228 | $size = 35; |
229 | } |
230 | if ($picture) { |
3f8247c2 |
231 | if ($CFG->slasharguments) { // Use this method if possible for better caching |
2447921f |
232 | $output .= "<IMG SRC=\"$CFG->wwwroot/user/pix.php/$userid/$file\" BORDER=0 WIDTH=$size HEIGHT=$size ALT=\"\">"; |
3f8247c2 |
233 | } else { |
234 | $output .= "<IMG SRC=\"$CFG->wwwroot/user/pix.php?file=/$userid/$file\" BORDER=0 WIDTH=$size HEIGHT=$size ALT=\"\">"; |
2447921f |
235 | } |
f9903ed0 |
236 | } else { |
136dabd8 |
237 | $output .= "<IMG SRC=\"$CFG->wwwroot/user/default/$file\" BORDER=0 WIDTH=$size HEIGHT=$size ALT=\"\">"; |
238 | } |
239 | $output .= "</A>"; |
240 | |
241 | if ($returnstring) { |
242 | return $output; |
243 | } else { |
244 | echo $output; |
f9903ed0 |
245 | } |
f9903ed0 |
246 | } |
247 | |
274f62e6 |
248 | function print_table($table) { |
f9903ed0 |
249 | // Prints a nicely formatted table. |
5b54db6e |
250 | // $table is an object with several properties. |
f9903ed0 |
251 | // $table->head is an array of heading names. |
252 | // $table->align is an array of column alignments |
5b54db6e |
253 | // $table->size is an array of column sizes |
f9903ed0 |
254 | // $table->data[] is an array of arrays containing the data. |
1e3e716f |
255 | // $table->width is an percentage of the page |
274f62e6 |
256 | // $table->cellpadding padding on each cell |
257 | // $table->cellspacing spacing between cells |
f9903ed0 |
258 | |
5b54db6e |
259 | if (isset($table->align)) { |
f9903ed0 |
260 | foreach ($table->align as $key => $aa) { |
261 | if ($aa) { |
5b54db6e |
262 | $align[$key] = " ALIGN=\"$aa\""; |
f9903ed0 |
263 | } else { |
264 | $align[$key] = ""; |
265 | } |
266 | } |
267 | } |
5b54db6e |
268 | if (isset($table->size)) { |
269 | foreach ($table->size as $key => $ss) { |
270 | if ($ss) { |
271 | $size[$key] = " WIDTH=\"$ss\""; |
272 | } else { |
273 | $size[$key] = ""; |
274 | } |
275 | } |
276 | } |
f9903ed0 |
277 | |
1e3e716f |
278 | if (!$table->width) { |
279 | $table->width = "80%"; |
280 | } |
281 | |
1e22462d |
282 | if (!$table->cellpadding) { |
283 | $table->cellpadding = "5"; |
284 | } |
285 | |
286 | if (!$table->cellspacing) { |
287 | $table->cellspacing = "1"; |
288 | } |
289 | |
1e3e716f |
290 | print_simple_box_start("CENTER", "$table->width", "#FFFFFF", 0); |
dccd1671 |
291 | echo "<TABLE WIDTH=100% BORDER=0 valign=top align=center "; |
1e22462d |
292 | echo " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\">\n"; |
f9903ed0 |
293 | |
294 | if ($table->head) { |
295 | echo "<TR>"; |
909f539d |
296 | foreach ($table->head as $key => $heading) { |
be025399 |
297 | echo "<TH VALIGN=top ".$align[$key].$size[$key].">$heading</TH>"; |
f9903ed0 |
298 | } |
299 | echo "</TR>\n"; |
300 | } |
301 | |
302 | foreach ($table->data as $row) { |
303 | echo "<TR VALIGN=TOP>"; |
304 | foreach ($row as $key => $item) { |
5b54db6e |
305 | echo "<TD ".$align[$key].$size[$key].">$item</TD>"; |
f9903ed0 |
306 | } |
307 | echo "</TR>\n"; |
308 | } |
309 | echo "</TABLE>\n"; |
310 | print_simple_box_end(); |
311 | |
312 | return true; |
313 | } |
314 | |
21ddaf60 |
315 | function print_editing_switch($courseid) { |
316 | global $CFG, $USER; |
317 | |
c7e3ac2a |
318 | if (isteacher($courseid)) { |
21ddaf60 |
319 | if ($USER->editing) { |
320 | echo "<A HREF=\"$CFG->wwwroot/course/view.php?id=$courseid&edit=off\">Turn editing off</A>"; |
321 | } else { |
322 | echo "<A HREF=\"$CFG->wwwroot/course/view.php?id=$courseid&edit=on\">Turn editing on</A>"; |
323 | } |
324 | } |
325 | } |
326 | |
55c26207 |
327 | function format_float($num, $places=0) { |
328 | return sprintf("%.$places"."f", $num); |
329 | } |
330 | |
e1947876 |
331 | function print_textarea($richedit, $rows, $cols, $width, $height, $name, $value="") { |
332 | global $CFG, $THEME; |
333 | |
334 | if ($richedit) { |
335 | echo "<object id=richedit style=\"BACKGROUND-COLOR: buttonface\""; |
336 | echo " data=\"$CFG->wwwroot/lib/rte/richedit.html\""; |
337 | echo " width=\"$width\" height=\"$height\" "; |
3df49694 |
338 | echo " type=\"text/x-scriptlet\" VIEWASTEXT></object>\n"; |
e1947876 |
339 | echo "<TEXTAREA style=\"display:none\" NAME=\"$name\" ROWS=1 COLS=1>"; |
340 | p($value); |
3df49694 |
341 | echo "</TEXTAREA>\n"; |
e1947876 |
342 | } else { |
343 | echo "<TEXTAREA name=\"$name\" rows=\"$rows\" cols=\"$cols\" wrap=virtual>"; |
344 | p($value); |
3df49694 |
345 | echo "</TEXTAREA>\n"; |
e1947876 |
346 | } |
347 | } |
348 | |
3df49694 |
349 | function print_richedit_javascript($form, $name, $source="no") { |
350 | echo "<SCRIPT language=\"JavaScript\" event=\"onload\" for=\"window\">\n"; |
351 | echo " document.richedit.options = \"history=no;source=$source\";"; |
352 | echo " document.richedit.docHtml = $form.$name.innerText;"; |
353 | echo "</SCRIPT>"; |
354 | } |
355 | |
e1947876 |
356 | |
6b174680 |
357 | function update_course_icon($courseid) { |
55c26207 |
358 | // Used to be an icon, but it's now a simple form button |
6b174680 |
359 | global $CFG, $USER; |
360 | |
361 | if (isteacher($courseid)) { |
362 | if ($USER->editing) { |
55c26207 |
363 | $string = get_string("turneditingoff"); |
364 | $edit = "off"; |
6b174680 |
365 | } else { |
55c26207 |
366 | $string = get_string("turneditingon"); |
367 | $edit = "on"; |
6b174680 |
368 | } |
7ce20f09 |
369 | return "<FORM TARGET=_parent METHOD=GET ACTION=\"$CFG->wwwroot/course/view.php\">". |
370 | "<INPUT TYPE=hidden NAME=id VALUE=\"$courseid\">". |
371 | "<INPUT TYPE=hidden NAME=edit VALUE=\"$edit\">". |
372 | "<INPUT TYPE=submit VALUE=\"$string\"></FORM>"; |
6b174680 |
373 | } |
374 | } |
375 | |
7ce20f09 |
376 | function update_module_button($moduleid, $courseid, $string) { |
377 | // Prints the editing button on a module "view" page |
6b174680 |
378 | global $CFG; |
379 | |
380 | if (isteacher($courseid)) { |
7ce20f09 |
381 | $string = get_string("updatethis", "", $string); |
382 | return "<FORM TARGET=_parent METHOD=GET ACTION=\"$CFG->wwwroot/course/mod.php\">". |
383 | "<INPUT TYPE=hidden NAME=update VALUE=\"$moduleid\">". |
384 | "<INPUT TYPE=hidden NAME=return VALUE=\"true\">". |
385 | "<INPUT TYPE=submit VALUE=\"$string\"></FORM>"; |
6b174680 |
386 | } |
387 | } |
388 | |
389 | |
dfc9ba9b |
390 | function navmenu($course, $cm=NULL) { |
d897cae4 |
391 | // Given a course and a (current) coursemodule |
392 | // This function returns a small popup menu with all the |
393 | // course activity modules in it, as a navigation menu |
394 | // The data is taken from the serialised array stored in |
395 | // the course record |
396 | |
397 | global $CFG; |
398 | |
dfc9ba9b |
399 | if ($cm) { |
400 | $cm = $cm->id; |
401 | } |
402 | |
4359e8f7 |
403 | if ($course->format == 'weeks') { |
d897cae4 |
404 | $strsection = get_string("week"); |
405 | } else { |
406 | $strsection = get_string("topic"); |
407 | } |
408 | |
7bee16d4 |
409 | if (!$modinfo = unserialize($course->modinfo)) { |
d897cae4 |
410 | return ""; |
411 | } |
412 | $section = -1; |
413 | $selected = ""; |
7bee16d4 |
414 | foreach ($modinfo as $mod) { |
d897cae4 |
415 | if ($mod->section > 0 and $section <> $mod->section) { |
416 | $menu[] = "-------------- $strsection $mod->section --------------"; |
417 | } |
418 | $section = $mod->section; |
419 | $url = "$mod->mod/view.php?id=$mod->cm"; |
dfc9ba9b |
420 | if ($cm == $mod->cm) { |
d897cae4 |
421 | $selected = $url; |
422 | } |
423 | $mod->name = urldecode($mod->name); |
424 | if (strlen($mod->name) > 55) { |
425 | $mod->name = substr($mod->name, 0, 50)."..."; |
426 | } |
427 | $menu[$url] = $mod->name; |
428 | } |
429 | |
430 | return popup_form("$CFG->wwwroot/mod/", $menu, "navmenu", $selected, get_string("jumpto"), "", "", true); |
431 | } |
432 | |
433 | |
39917a09 |
434 | function print_date_selector($day, $month, $year, $currenttime=0) { |
435 | // Currenttime is a default timestamp in GMT |
436 | // Prints form items with the names $day, $month and $year |
437 | |
438 | if (!$currenttime) { |
439 | $currenttime = time(); |
440 | } |
441 | $currentdate = usergetdate($currenttime); |
442 | |
443 | for ($i=1; $i<=31; $i++) { |
444 | $days[$i] = "$i"; |
445 | } |
446 | for ($i=1; $i<=12; $i++) { |
447 | $months[$i] = date("F", mktime(0,0,0,$i,1,2000)); |
448 | } |
449 | for ($i=2000; $i<=2010; $i++) { |
450 | $years[$i] = $i; |
451 | } |
452 | choose_from_menu($days, $day, $currentdate[mday], ""); |
453 | choose_from_menu($months, $month, $currentdate[mon], ""); |
454 | choose_from_menu($years, $year, $currentdate[year], ""); |
455 | } |
456 | |
457 | function print_time_selector($hour, $minute, $currenttime=0) { |
458 | // Currenttime is a default timestamp in GMT |
459 | // Prints form items with the names $hour and $minute |
460 | |
461 | if (!$currenttime) { |
462 | $currenttime = time(); |
463 | } |
464 | $currentdate = usergetdate($currenttime); |
465 | for ($i=0; $i<=23; $i++) { |
466 | $hours[$i] = sprintf("%02d",$i); |
467 | } |
468 | for ($i=0; $i<=59; $i++) { |
469 | $minutes[$i] = sprintf("%02d",$i); |
470 | } |
471 | choose_from_menu($hours, $hour, $currentdate[hours], ""); |
472 | choose_from_menu($minutes, $minute, $currentdate[minutes], ""); |
473 | } |
474 | |
475 | function make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0) { |
476 | // Given date parts in user time, produce a GMT timestamp |
477 | |
478 | return mktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year); |
479 | } |
480 | |
d897cae4 |
481 | |
8dbed6be |
482 | function format_time($totalsecs, $str=NULL) { |
6b174680 |
483 | // Given an amount of time in seconds, prints it |
484 | // nicely as months, days, hours etc as needed |
c7e3ac2a |
485 | |
6b174680 |
486 | $totalsecs = abs($totalsecs); |
c7e3ac2a |
487 | |
8dbed6be |
488 | if (!$str) { // Create the str structure the slow way |
489 | $str->day = get_string("day"); |
490 | $str->days = get_string("days"); |
491 | $str->hour = get_string("hour"); |
492 | $str->hours = get_string("hours"); |
493 | $str->min = get_string("min"); |
494 | $str->mins = get_string("mins"); |
495 | $str->sec = get_string("sec"); |
496 | $str->secs = get_string("secs"); |
497 | } |
498 | |
499 | $days = floor($totalsecs/86400); |
6b174680 |
500 | $remainder = $totalsecs - ($days*86400); |
8dbed6be |
501 | $hours = floor($remainder/3600); |
6b174680 |
502 | $remainder = $remainder - ($hours*3600); |
8dbed6be |
503 | $mins = floor($remainder/60); |
504 | $secs = $remainder - ($mins*60); |
505 | |
506 | $ss = ($secs == 1) ? $str->sec : $str->secs; |
507 | $sm = ($mins == 1) ? $str->min : $str->mins; |
508 | $sh = ($hours == 1) ? $str->hour : $str->hours; |
509 | $sd = ($days == 1) ? $str->day : $str->days; |
510 | |
511 | if ($days) $odays = "$days $sd"; |
512 | if ($hours) $ohours = "$hours $sh"; |
513 | if ($mins) $omins = "$mins $sm"; |
514 | if ($secs) $osecs = "$secs $ss"; |
6b174680 |
515 | |
516 | if ($days) return "$odays $ohours"; |
517 | if ($hours) return "$ohours $omins"; |
518 | if ($mins) return "$omins $osecs"; |
519 | if ($secs) return "$osecs"; |
520 | return get_string("now"); |
521 | } |
f9903ed0 |
522 | |
5fa51a39 |
523 | function userdate($date, $format="", $timezone=99) { |
d552ead0 |
524 | // Returns a formatted string that represents a date in user time |
7a302afc |
525 | // WARNING: note that the format is for strftime(), not date(). |
526 | |
873960de |
527 | global $USER; |
528 | |
5fa51a39 |
529 | if ($format == "") { |
7a302afc |
530 | $format = "%A, %e %B %Y, %I:%M %p"; |
5fa51a39 |
531 | } |
532 | if ($timezone == 99) { |
ab247495 |
533 | if (isset($USER->timezone)) { |
534 | $timezone = (float)$USER->timezone; |
535 | } |
5fa51a39 |
536 | } |
537 | if (abs($timezone) > 12) { |
6d9d8344 |
538 | return strftime("$format", $date); |
873960de |
539 | } |
7a302afc |
540 | return gmstrftime($format, $date + (int)($timezone * 3600)); |
873960de |
541 | } |
542 | |
5fa51a39 |
543 | function usergetdate($date, $timezone=99) { |
6b174680 |
544 | // Given a $date timestamp in GMT, returns an array |
545 | // that represents the date in user time |
546 | |
873960de |
547 | global $USER; |
548 | |
5fa51a39 |
549 | if ($timezone == 99) { |
550 | $timezone = (float)$USER->timezone; |
551 | } |
552 | if (abs($timezone) > 12) { |
873960de |
553 | return getdate($date); |
554 | } |
d2d6171f |
555 | //There is no gmgetdate so I have to fake it... |
556 | $date = $date + (int)($timezone * 3600); |
557 | $getdate["seconds"] = gmstrftime("%S", $date); |
558 | $getdate["minutes"] = gmstrftime("%M", $date); |
559 | $getdate["hours"] = gmstrftime("%H", $date); |
560 | $getdate["mday"] = gmstrftime("%d", $date); |
561 | $getdate["wday"] = gmstrftime("%u", $date); |
562 | $getdate["mon"] = gmstrftime("%m", $date); |
563 | $getdate["year"] = gmstrftime("%Y", $date); |
564 | $getdate["yday"] = gmstrftime("%j", $date); |
565 | $getdate["weekday"] = gmstrftime("%A", $date); |
566 | $getdate["month"] = gmstrftime("%B", $date); |
567 | return $getdate; |
d552ead0 |
568 | } |
569 | |
570 | function usertime($date, $timezone=99) { |
571 | // Given a GMT timestamp (seconds since epoch), offsets it by |
572 | // the timezone. eg 3pm in India is 3pm GMT - 7 * 3600 seconds |
573 | global $USER; |
574 | |
575 | if ($timezone == 99) { |
576 | $timezone = (float)$USER->timezone; |
577 | } |
578 | if (abs($timezone) > 12) { |
579 | return $date; |
580 | } |
581 | return $date - (int)($timezone * 3600); |
582 | } |
583 | |
edf7fe8c |
584 | function usergetmidnight($date, $timezone=99) { |
585 | // Given a time, return the GMT timestamp of the most recent midnight |
586 | // for the current user. |
edf7fe8c |
587 | global $USER; |
588 | |
4606d9bb |
589 | if ($timezone == 99) { |
590 | $timezone = (float)$USER->timezone; |
591 | } |
592 | |
edf7fe8c |
593 | $userdate = usergetdate($date, $timezone); |
4606d9bb |
594 | |
595 | if (abs($timezone) > 12) { |
596 | return mktime(0, 0, 0, $userdate["mon"], $userdate["mday"], $userdate["year"]); |
597 | } |
598 | |
edf7fe8c |
599 | $timemidnight = gmmktime (0, 0, 0, $userdate["mon"], $userdate["mday"], $userdate["year"]); |
600 | return usertime($timemidnight, $timezone); // Time of midnight of this user's day, in GMT |
601 | |
602 | } |
603 | |
d552ead0 |
604 | function usertimezone($timezone=99) { |
605 | // returns a string that prints the user's timezone |
606 | global $USER; |
607 | |
608 | if ($timezone == 99) { |
609 | $timezone = (float)$USER->timezone; |
610 | } |
611 | if (abs($timezone) > 12) { |
612 | return "server time"; |
613 | } |
614 | if (abs($timezone) < 0.5) { |
615 | return "GMT"; |
616 | } |
617 | if ($timezone > 0) { |
618 | return "GMT+$timezone"; |
619 | } else { |
620 | return "GMT$timezone"; |
621 | } |
f9903ed0 |
622 | } |
623 | |
624 | |
625 | function error ($message, $link="") { |
626 | global $CFG, $SESSION; |
627 | |
ae350059 |
628 | print_header(get_string("error")); |
f9903ed0 |
629 | echo "<BR>"; |
630 | print_simple_box($message, "center", "", "#FFBBBB"); |
631 | |
632 | if (!$link) { |
633 | if ( !empty($SESSION->fromurl) ) { |
634 | $link = "$SESSION->fromurl"; |
635 | unset($SESSION->fromurl); |
8223d271 |
636 | save_session("SESSION"); |
f9903ed0 |
637 | } else { |
638 | $link = "$CFG->wwwroot"; |
639 | } |
640 | } |
dbcb136a |
641 | print_continue($link); |
f9903ed0 |
642 | print_footer(); |
643 | die; |
644 | } |
645 | |
65cf9fc3 |
646 | function helpbutton ($page, $title="", $module="moodle", $image=true, $text="") { |
647 | // $page = the keyword that defines a help page |
648 | // $title = the title of links, rollover tips, alt tags etc |
649 | // $module = which module is the page defined in |
650 | // $image = use a help image for the link? (otherwise uses text) |
651 | // $text = if defined then this text is used in the page, and |
652 | // the $page variable is ignored. |
34c8915d |
653 | global $CFG; |
ff80e012 |
654 | |
65cf9fc3 |
655 | if ($module == "") { |
656 | $module = "moodle"; |
657 | } |
658 | if ($image) { |
e5dfd0f3 |
659 | $linkobject = "<IMG BORDER=0 HEIGHT=17 WIDTH=22 ALT=\"$title\" SRC=\"$CFG->wwwroot/pix/help.gif\">"; |
65cf9fc3 |
660 | } else { |
661 | $linkobject = $title; |
662 | } |
663 | if ($text) { |
664 | $url = "/help.php?module=$module&text=$text"; |
665 | } else { |
6ee45796 |
666 | $url = "/help.php?module=$module&file=$page.html"; |
65cf9fc3 |
667 | } |
668 | link_to_popup_window ($url, "popup", $linkobject, 400, 500, $title); |
34c8915d |
669 | } |
670 | |
f9903ed0 |
671 | function notice ($message, $link="") { |
672 | global $THEME, $HTTP_REFERER; |
673 | |
674 | if (!$link) { |
675 | $link = $HTTP_REFERER; |
676 | } |
677 | |
678 | echo "<BR>"; |
679 | print_simple_box($message, "center", "", "$THEME->cellheading"); |
ae350059 |
680 | print_heading("<A HREF=\"$link\">".get_string("continue")."</A>"); |
5ebe27e2 |
681 | print_footer(get_site()); |
f9903ed0 |
682 | die; |
683 | } |
684 | |
685 | function notice_yesno ($message, $linkyes, $linkno) { |
686 | global $THEME; |
687 | |
688 | print_simple_box_start("center", "", "$THEME->cellheading"); |
689 | echo "<P ALIGN=CENTER><FONT SIZE=3>$message</FONT></P>"; |
690 | echo "<P ALIGN=CENTER><FONT SIZE=3><B>"; |
ae350059 |
691 | echo "<A HREF=\"$linkyes\">".get_string("yes")."</A>"; |
f9903ed0 |
692 | echo " "; |
ae350059 |
693 | echo "<A HREF=\"$linkno\">".get_string("no")."</A>"; |
f9903ed0 |
694 | echo "</B></FONT></P>"; |
695 | print_simple_box_end(); |
696 | } |
697 | |
698 | function redirect($url, $message="", $delay=0) { |
699 | // Uses META tags to redirect the user, after printing a notice |
f9903ed0 |
700 | |
701 | echo "<META HTTP-EQUIV='Refresh' CONTENT='$delay; URL=$url'>"; |
702 | |
703 | if (!empty($message)) { |
704 | print_header(); |
705 | echo "<CENTER>"; |
706 | echo "<P>$message</P>"; |
ae350059 |
707 | echo "<P>( <A HREF=\"$url\">".get_string("continue")."</A> )</P>"; |
f9903ed0 |
708 | echo "</CENTER>"; |
709 | } |
710 | die; |
711 | } |
712 | |
713 | function notify ($message) { |
714 | echo "<P align=center><B><FONT COLOR=#FF0000>$message</FONT></B></P>\n"; |
715 | } |
716 | |
717 | |
718 | |
719 | /// PARAMETER HANDLING //////////////////////////////////////////////////// |
720 | |
721 | function require_variable($var) { |
f9903ed0 |
722 | if (! isset($var)) { |
723 | error("A required parameter was missing"); |
724 | } |
725 | } |
726 | |
727 | function optional_variable(&$var, $default=0) { |
728 | if (! isset($var)) { |
729 | $var = $default; |
730 | } |
731 | } |
732 | |
733 | |
734 | |
735 | |
736 | /// DATABASE HANDLING //////////////////////////////////////////////// |
737 | |
c74a0ca5 |
738 | function execute_sql($command, $feedback=true) { |
f9903ed0 |
739 | // Completely general |
740 | |
741 | global $db; |
742 | |
743 | $result = $db->Execute("$command"); |
744 | |
745 | if ($result) { |
c74a0ca5 |
746 | if ($feedback) { |
747 | echo "<P><FONT COLOR=green><B>".get_string("success")."</B></FONT></P>"; |
748 | } |
f9903ed0 |
749 | return true; |
750 | } else { |
c74a0ca5 |
751 | if ($feedback) { |
752 | echo "<P><FONT COLOR=red><B>".get_string("error")."</B></FONT></P>"; |
753 | } |
f9903ed0 |
754 | return false; |
755 | } |
756 | } |
757 | |
758 | function modify_database($sqlfile) { |
759 | // Assumes that the input text file consists of a number |
760 | // of SQL statements ENDING WITH SEMICOLONS. The semicolons |
761 | // MUST be the last character in a line. |
762 | // Lines that are blank or that start with "#" are ignored. |
763 | // Only tested with mysql dump files (mysqldump -p -d moodle) |
764 | |
765 | |
766 | if (file_exists($sqlfile)) { |
767 | $success = true; |
768 | $lines = file($sqlfile); |
769 | $command = ""; |
770 | |
771 | while ( list($i, $line) = each($lines) ) { |
772 | $line = chop($line); |
773 | $length = strlen($line); |
774 | |
775 | if ($length && substr($line, 0, 1) <> "#") { |
776 | if (substr($line, $length-1, 1) == ";") { |
777 | $line = substr($line, 0, $length-1); // strip ; |
778 | $command .= $line; |
779 | if (! execute_sql($command)) { |
780 | $success = false; |
781 | } |
782 | $command = ""; |
783 | } else { |
784 | $command .= $line; |
785 | } |
786 | } |
787 | } |
788 | |
789 | } else { |
790 | $success = false; |
791 | echo "<P>Tried to modify database, but \"$sqlfile\" doesn't exist!</P>"; |
792 | } |
793 | |
794 | return $success; |
795 | } |
796 | |
797 | |
798 | function record_exists($table, $field, $value) { |
799 | global $db; |
800 | |
801 | $rs = $db->Execute("SELECT * FROM $table WHERE $field = '$value' LIMIT 1"); |
802 | if (!$rs) return false; |
803 | |
804 | if ( $rs->RecordCount() ) { |
805 | return true; |
806 | } else { |
807 | return false; |
808 | } |
809 | } |
810 | |
811 | function record_exists_sql($sql) { |
812 | global $db; |
813 | |
814 | $rs = $db->Execute($sql); |
815 | if (!$rs) return false; |
816 | |
817 | if ( $rs->RecordCount() ) { |
818 | return true; |
819 | } else { |
820 | return false; |
821 | } |
822 | } |
823 | |
824 | |
825 | function count_records($table, $selector, $value) { |
826 | // Get all the records and count them |
827 | global $db; |
828 | |
829 | $rs = $db->Execute("SELECT COUNT(*) FROM $table WHERE $selector = '$value'"); |
830 | if (!$rs) return 0; |
831 | |
832 | return $rs->fields[0]; |
833 | } |
834 | |
835 | function count_records_sql($sql) { |
836 | // Get all the records and count them |
837 | global $db; |
838 | |
839 | $rs = $db->Execute("$sql"); |
840 | if (!$rs) return 0; |
841 | |
842 | return $rs->fields[0]; |
843 | } |
844 | |
845 | function get_record($table, $selector, $value) { |
846 | // Get a single record as an object |
847 | global $db; |
848 | |
849 | $rs = $db->Execute("SELECT * FROM $table WHERE $selector = '$value'"); |
850 | if (!$rs) return false; |
851 | |
852 | if ( $rs->RecordCount() == 1 ) { |
853 | return (object)$rs->fields; |
854 | } else { |
855 | return false; |
856 | } |
857 | } |
858 | |
859 | function get_record_sql($sql) { |
860 | // Get a single record as an object |
861 | // The sql statement is provided as a string. |
862 | |
863 | global $db; |
864 | |
865 | $rs = $db->Execute("$sql"); |
866 | if (!$rs) return false; |
867 | |
868 | if ( $rs->RecordCount() == 1 ) { |
869 | return (object)$rs->fields; |
870 | } else { |
871 | return false; |
872 | } |
873 | } |
874 | |
0095d5cd |
875 | function get_records($table, $selector, $value, $sort="", $fields="*") { |
876 | // Get a number of records as an array of objects |
877 | // Can optionally be sorted eg "time ASC" or "time DESC" |
878 | // If "fields" is specified, only those fields are returned |
879 | // The "key" is the first column returned, eg usually "id" |
880 | global $db; |
881 | |
882 | if ($sort) { |
883 | $sortorder = "ORDER BY $sort"; |
884 | } |
885 | $sql = "SELECT $fields FROM $table WHERE $selector = '$value' $sortorder"; |
886 | |
887 | return get_records_sql($sql); |
888 | } |
889 | |
890 | |
891 | function get_records_list($table, $selector, $values, $sort="", $fields="*") { |
f9903ed0 |
892 | // Get a number of records as an array of objects |
0095d5cd |
893 | // Differs from get_records() in that the values variable |
894 | // can be a comma-separated list of values eg "4,5,6,10" |
f9903ed0 |
895 | // Can optionally be sorted eg "time ASC" or "time DESC" |
896 | // The "key" is the first column returned, eg usually "id" |
897 | global $db; |
898 | |
899 | if ($sort) { |
900 | $sortorder = "ORDER BY $sort"; |
901 | } |
0095d5cd |
902 | $sql = "SELECT $fields FROM $table WHERE $selector in ($values) $sortorder"; |
f9903ed0 |
903 | |
904 | return get_records_sql($sql); |
905 | } |
906 | |
0095d5cd |
907 | |
f9903ed0 |
908 | function get_records_sql($sql) { |
909 | // Get a number of records as an array of objects |
910 | // The "key" is the first column returned, eg usually "id" |
911 | // The sql statement is provided as a string. |
912 | |
913 | global $db; |
914 | |
915 | $rs = $db->Execute("$sql"); |
916 | if (!$rs) return false; |
917 | |
918 | if ( $rs->RecordCount() > 0 ) { |
5c0bef5d |
919 | if ($records = $rs->GetAssoc(true)) { |
920 | foreach ($records as $key => $record) { |
921 | $objects[$key] = (object) $record; |
922 | } |
923 | return $objects; |
924 | } else { |
925 | return false; |
f9903ed0 |
926 | } |
f9903ed0 |
927 | } else { |
928 | return false; |
929 | } |
930 | } |
931 | |
932 | function get_records_sql_menu($sql) { |
933 | // Given an SQL select, this function returns an associative |
934 | // array of the first two columns. This is most useful in |
935 | // combination with the choose_from_menu function to create |
936 | // a form menu. |
937 | |
938 | global $db; |
939 | |
940 | $rs = $db->Execute("$sql"); |
941 | if (!$rs) return false; |
942 | |
943 | if ( $rs->RecordCount() > 0 ) { |
944 | while (!$rs->EOF) { |
945 | $menu[$rs->fields[0]] = $rs->fields[1]; |
946 | $rs->MoveNext(); |
947 | } |
948 | return $menu; |
949 | |
950 | } else { |
951 | return false; |
952 | } |
953 | } |
954 | |
955 | function get_field($table, $field, $selector, $value) { |
956 | global $db; |
957 | |
958 | $rs = $db->Execute("SELECT $field FROM $table WHERE $selector = '$value'"); |
959 | if (!$rs) return false; |
960 | |
961 | if ( $rs->RecordCount() == 1 ) { |
962 | return $rs->fields["$field"]; |
963 | } else { |
964 | return false; |
965 | } |
966 | } |
967 | |
968 | function set_field($table, $field, $newvalue, $selector, $value) { |
969 | global $db; |
970 | |
971 | return $db->Execute("UPDATE $table SET $field = '$newvalue' WHERE $selector = '$value'"); |
972 | } |
973 | |
a28af1fe |
974 | function set_config($name, $value) { |
975 | // No need for get_config because they are usually always available in $CFG |
976 | |
977 | if (get_field("config", "value", "name", $name)) { |
978 | return set_field("config", "value", $value, "name", $name); |
979 | } else { |
980 | $config->name = $name; |
981 | $config->value = $value; |
982 | return insert_record("config", $config); |
983 | } |
984 | } |
f9903ed0 |
985 | |
986 | function delete_records($table, $selector, $value) { |
987 | // Delete one or more records from a table |
988 | global $db; |
989 | |
990 | return $db->Execute("DELETE FROM $table WHERE $selector = '$value'"); |
991 | } |
992 | |
993 | function insert_record($table, $dataobject) { |
994 | // Insert a record into a table and return the "id" field |
995 | // $dataobject is an object containing needed data |
996 | |
997 | global $db; |
998 | |
999 | // Determine all the fields needed |
1000 | if (! $columns = $db->MetaColumns("$table")) { |
1001 | return false; |
1002 | } |
1003 | |
1004 | $data = (array)$dataobject; |
1005 | |
1006 | // Pull out data matching these fields |
1007 | foreach ($columns as $column) { |
e5a57e85 |
1008 | if ($column->name <> "id" && isset($data[$column->name]) ) { |
f9903ed0 |
1009 | $ddd[$column->name] = $data[$column->name]; |
1010 | } |
1011 | } |
1012 | |
1013 | // Construct SQL queries |
1014 | if (! $numddd = count($ddd)) { |
1015 | return 0; |
1016 | } |
1017 | |
1018 | $count = 0; |
1019 | $insert = ""; |
1020 | $select = ""; |
1021 | |
1022 | foreach ($ddd as $key => $value) { |
1023 | $count++; |
1024 | $insert .= "$key = '$value'"; |
1025 | $select .= "$key = '$value'"; |
1026 | if ($count < $numddd) { |
1027 | $insert .= ", "; |
1028 | $select .= " AND "; |
1029 | } |
1030 | } |
1031 | |
1032 | if (! $rs = $db->Execute("INSERT INTO $table SET $insert")) { |
1033 | return false; |
1034 | } |
1035 | |
1036 | // Pull it out again to find the id. This is the most cross-platform method. |
1037 | if ($rs = $db->Execute("SELECT id FROM $table WHERE $select")) { |
1038 | return $rs->fields[0]; |
1039 | } else { |
1040 | return false; |
1041 | } |
1042 | } |
1043 | |
1044 | |
1045 | function update_record($table, $dataobject) { |
1046 | // Update a record in a table |
1047 | // $dataobject is an object containing needed data |
1048 | |
1049 | global $db; |
1050 | |
5c0bef5d |
1051 | if (! isset($dataobject->id) ) { |
f9903ed0 |
1052 | return false; |
1053 | } |
1054 | |
1055 | // Determine all the fields in the table |
1afd014e |
1056 | if (!$columns = $db->MetaColumns($table)) { |
1057 | return false; |
1058 | } |
f9903ed0 |
1059 | $data = (array)$dataobject; |
1060 | |
1061 | // Pull out data matching these fields |
1062 | foreach ($columns as $column) { |
5c0bef5d |
1063 | if ($column->name <> "id" && isset($data[$column->name]) ) { |
f9903ed0 |
1064 | $ddd[$column->name] = $data[$column->name]; |
1065 | } |
1066 | } |
1067 | |
1068 | // Construct SQL queries |
1069 | $numddd = count($ddd); |
1070 | $count = 0; |
1071 | $update = ""; |
1072 | |
1073 | foreach ($ddd as $key => $value) { |
1074 | $count++; |
1075 | $update .= "$key = '$value'"; |
1076 | if ($count < $numddd) { |
1077 | $update .= ", "; |
1078 | } |
1079 | } |
1080 | |
1081 | if ($rs = $db->Execute("UPDATE $table SET $update WHERE id = '$dataobject->id'")) { |
1082 | return true; |
1083 | } else { |
1084 | return false; |
1085 | } |
1086 | } |
1087 | |
1088 | |
f93f848a |
1089 | function print_object($object) { |
1090 | // Mostly just for debugging |
1091 | |
1092 | $array = (array)$object; |
1093 | foreach ($array as $key => $item) { |
1094 | echo "$key -> $item <BR>"; |
1095 | } |
1096 | } |
1097 | |
1098 | |
f9903ed0 |
1099 | /// USER DATABASE //////////////////////////////////////////////// |
1100 | |
1101 | function get_user_info_from_db($field, $value) { |
1102 | |
1103 | global $db; |
1104 | |
1105 | if (!$field || !$value) |
1106 | return false; |
1107 | |
91ac0405 |
1108 | if (! $result = $db->Execute("SELECT * FROM user WHERE $field = '$value' AND deleted <> '1'")) { |
1109 | error("Could not find any active users!"); |
1110 | } |
f9903ed0 |
1111 | |
1112 | if ( $result->RecordCount() == 1 ) { |
1113 | $user = (object)$result->fields; |
1114 | |
1115 | $rs = $db->Execute("SELECT course FROM user_students WHERE user = '$user->id' "); |
1116 | while (!$rs->EOF) { |
1117 | $course = $rs->fields["course"]; |
1118 | $user->student["$course"] = true; |
1119 | $rs->MoveNext(); |
1120 | } |
1121 | |
1122 | $rs = $db->Execute("SELECT course FROM user_teachers WHERE user = '$user->id' "); |
1123 | while (!$rs->EOF) { |
1124 | $course = $rs->fields["course"]; |
1125 | $user->teacher["$course"] = true; |
1126 | $rs->MoveNext(); |
1127 | } |
1128 | |
1129 | $rs = $db->Execute("SELECT * FROM user_admins WHERE user = '$user->id' "); |
1130 | while (!$rs->EOF) { |
1131 | $user->admin = true; |
1132 | $rs->MoveNext(); |
1133 | } |
1134 | |
a83fded1 |
1135 | if ($course = get_site()) { |
f9903ed0 |
1136 | // Everyone is always a member of the top course |
1137 | $user->student["$course->id"] = true; |
1138 | } |
1139 | |
1140 | return $user; |
1141 | |
1142 | } else { |
1143 | return false; |
1144 | } |
1145 | } |
1146 | |
1147 | function update_user_in_db() { |
1148 | |
1149 | global $db, $USER, $REMOTE_ADDR; |
1150 | |
1151 | if (!isset($USER->id)) |
1152 | return false; |
1153 | |
1154 | $timenow = time(); |
faebaf0f |
1155 | if ($db->Execute("UPDATE user SET lastIP='$REMOTE_ADDR', lastaccess='$timenow' WHERE id = '$USER->id' ")) { |
f9903ed0 |
1156 | return true; |
1157 | } else { |
1158 | return false; |
1159 | } |
1160 | } |
1161 | |
da5c172a |
1162 | function require_login($courseid=0) { |
1163 | // This function checks that the current user is logged in, and optionally |
1164 | // whether they are "logged in" or allowed to be in a particular course. |
1165 | // If not, then it redirects them to the site login or course enrolment. |
f9903ed0 |
1166 | |
1167 | global $CFG, $SESSION, $USER, $FULLME, $HTTP_REFERER, $PHPSESSID; |
1168 | |
da5c172a |
1169 | // First check that the user is logged in to the site. |
7363ff91 |
1170 | |
c21c671d |
1171 | if (! (isset($USER->loggedin) and $USER->confirmed and ($USER->site == $CFG->wwwroot)) ) { // They're not |
f9903ed0 |
1172 | $SESSION->wantsurl = $FULLME; |
7363ff91 |
1173 | $SESSION->fromurl = $HTTP_REFERER; |
8223d271 |
1174 | save_session("SESSION"); |
c21c671d |
1175 | $USER = NULL; |
1176 | save_session("USER"); |
f9903ed0 |
1177 | if ($PHPSESSID) { // Cookies not enabled. |
a7b9e8bc |
1178 | redirect("$CFG->wwwroot/login/index.php?PHPSESSID=$PHPSESSID"); |
f9903ed0 |
1179 | } else { |
a7b9e8bc |
1180 | redirect("$CFG->wwwroot/login/index.php"); |
f9903ed0 |
1181 | } |
1182 | die; |
f9903ed0 |
1183 | } |
da5c172a |
1184 | |
1185 | // Next, check if the user can be in a particular course |
1186 | if ($courseid) { |
1187 | if ($USER->student[$courseid] || $USER->teacher[$courseid] || $USER->admin) { |
cb909d74 |
1188 | if (isset($USER->realuser)) { // Make sure the REAL person can also access this course |
1189 | if (!isteacher($courseid, $USER->realuser)) { |
1190 | print_header(); |
5ebe27e2 |
1191 | notice(get_string("studentnotallowed", "", "$USER->firstname $USER->lastname"), $CFG->wwwroot); |
cb909d74 |
1192 | } |
1193 | |
1194 | } else { // just update their last login time |
3ce2f1e0 |
1195 | update_user_in_db(); |
1196 | } |
ab998e5d |
1197 | if (!$USER->email) { // User logged in, but has not set up profile! |
1198 | // This can occur with external authentication |
ab998e5d |
1199 | redirect("$CFG->wwwroot/user/edit.php?id=$USER->id&course=$courseid"); |
1200 | die; |
1201 | } |
da5c172a |
1202 | return; // user is a member of this course. |
1203 | } |
1204 | if (! $course = get_record("course", "id", $courseid)) { |
1205 | error("That course doesn't exist"); |
1206 | } |
7363ff91 |
1207 | if ($USER->username == "guest") { |
1208 | switch ($course->guest) { |
1209 | case 0: // Guests not allowed |
1210 | print_header(); |
1211 | notice(get_string("guestsnotallowed", "", $course->fullname)); |
1212 | break; |
1213 | case 1: // Guests allowed |
1214 | update_user_in_db(); |
1215 | return; |
1216 | case 2: // Guests allowed with key (drop through) |
1217 | break; |
1218 | } |
da5c172a |
1219 | } |
f9903ed0 |
1220 | |
7363ff91 |
1221 | // Currently not enrolled in the course, so see if they want to enrol |
da5c172a |
1222 | $SESSION->wantsurl = $FULLME; |
8223d271 |
1223 | save_session("SESSION"); |
da5c172a |
1224 | redirect("$CFG->wwwroot/course/enrol.php?id=$courseid"); |
1225 | die; |
1226 | } |
f9903ed0 |
1227 | } |
1228 | |
1229 | |
1230 | |
1231 | function update_login_count() { |
1232 | global $SESSION; |
1233 | |
1234 | $max_logins = 10; |
1235 | |
1236 | if (empty($SESSION->logincount)) { |
1237 | $SESSION->logincount = 1; |
1238 | } else { |
1239 | $SESSION->logincount++; |
1240 | } |
8223d271 |
1241 | save_session("SESSION"); |
f9903ed0 |
1242 | |
1243 | if ($SESSION->logincount > $max_logins) { |
1244 | unset($SESSION->wantsurl); |
8223d271 |
1245 | save_session("SESSION"); |
f9903ed0 |
1246 | error("Sorry, you have exceeded the allowed number of login attempts. Restart your browser."); |
1247 | } |
1248 | } |
1249 | |
d578afc8 |
1250 | function remove_admin($user) { |
1251 | global $db; |
1252 | |
1253 | return $db->Execute("DELETE FROM user_admins WHERE user = '$user'"); |
1254 | } |
1255 | |
1256 | function remove_teacher($user, $course=0) { |
1257 | global $db; |
1258 | |
1259 | if ($course) { |
45aa6d56 |
1260 | /// First delete any crucial stuff that might still send mail |
1261 | if ($forums = get_records("forum", "course", $course)) { |
1262 | foreach ($forums as $forum) { |
1263 | $db->Execute("DELETE FROM forum_subscriptions WHERE forum = '$forum->id' AND user = '$user'"); |
1264 | } |
1265 | } |
d578afc8 |
1266 | return $db->Execute("DELETE FROM user_teachers WHERE user = '$user' AND course = '$course'"); |
1267 | } else { |
45aa6d56 |
1268 | delete_records("forum_subscriptions", "user", $user); |
1269 | return delete_records("user_teachers", "user", $user); |
d578afc8 |
1270 | } |
1271 | } |
1272 | |
1273 | |
1274 | function enrol_student($user, $course) { |
1275 | global $db; |
1276 | |
1277 | $timenow = time(); |
1278 | |
1279 | $rs = $db->Execute("INSERT INTO user_students (user, course, start, end, time) |
1280 | VALUES ($user, $course, 0, 0, $timenow)"); |
1281 | if ($rs) { |
1282 | return true; |
1283 | } else { |
1284 | return false; |
1285 | } |
1286 | } |
1287 | |
1288 | function unenrol_student($user, $course=0) { |
1289 | global $db; |
1290 | |
1291 | if ($course) { |
45aa6d56 |
1292 | /// First delete any crucial stuff that might still send mail |
1293 | if ($forums = get_records("forum", "course", $course)) { |
1294 | foreach ($forums as $forum) { |
1295 | $db->Execute("DELETE FROM forum_subscriptions WHERE forum = '$forum->id' AND user = '$user'"); |
1296 | } |
1297 | } |
d578afc8 |
1298 | return $db->Execute("DELETE FROM user_students WHERE user = '$user' AND course = '$course'"); |
45aa6d56 |
1299 | |
d578afc8 |
1300 | } else { |
45aa6d56 |
1301 | delete_records("forum_subscriptions", "user", $user); |
1302 | return delete_records("user_students", "user", $user); |
d578afc8 |
1303 | } |
1304 | } |
1305 | |
f9903ed0 |
1306 | |
1307 | function isadmin($userid=0) { |
1308 | global $USER; |
1309 | |
1310 | if (!$userid) { |
1311 | return $USER->admin; |
1312 | } |
1313 | |
1314 | return record_exists_sql("SELECT * FROM user_admins WHERE user='$userid'"); |
1315 | } |
1316 | |
8a9e3fd7 |
1317 | function isteacher($courseid, $userid=0) { |
f9903ed0 |
1318 | global $USER; |
1319 | |
d115a57f |
1320 | if (isadmin($userid)) { // admins can do anything the teacher can |
1321 | return true; |
1322 | } |
1323 | |
f9903ed0 |
1324 | if (!$userid) { |
8a9e3fd7 |
1325 | return $USER->teacher[$courseid]; |
f9903ed0 |
1326 | } |
1327 | |
8a9e3fd7 |
1328 | return record_exists_sql("SELECT * FROM user_teachers WHERE user='$userid' AND course='$courseid'"); |
f9903ed0 |
1329 | } |
1330 | |
1331 | |
8a9e3fd7 |
1332 | function isstudent($courseid, $userid=0) { |
f9903ed0 |
1333 | global $USER; |
1334 | |
1335 | if (!$userid) { |
8a9e3fd7 |
1336 | return $USER->student[$courseid]; |
f9903ed0 |
1337 | } |
1338 | |
1339 | $timenow = time(); // todo: add time check below |
1340 | |
8a9e3fd7 |
1341 | return record_exists_sql("SELECT * FROM user_students WHERE user='$userid' AND course='$courseid'"); |
f9903ed0 |
1342 | } |
1343 | |
da5c172a |
1344 | function isguest($userid=0) { |
1345 | global $USER; |
1346 | |
1347 | if (!$userid) { |
1348 | return ($USER->username == "guest"); |
1349 | } |
1350 | |
a3447e10 |
1351 | return record_exists_sql("SELECT * FROM user WHERE id='$userid' AND username = 'guest' "); |
da5c172a |
1352 | } |
1353 | |
2c309dc2 |
1354 | function isediting($courseid, $user=NULL) { |
1355 | global $USER; |
1356 | if (!$user){ |
1357 | $user = $USER; |
1358 | } |
1359 | return ($user->editing and isteacher($courseid, $user->id)); |
1360 | } |
1361 | |
f9903ed0 |
1362 | function reset_login_count() { |
1363 | global $SESSION; |
1364 | |
1365 | $SESSION->logincount = 0; |
8223d271 |
1366 | save_session("SESSION"); |
f9903ed0 |
1367 | } |
1368 | |
1369 | |
1370 | function set_moodle_cookie($thing) { |
1371 | |
1372 | $days = 60; |
1373 | $seconds = 60*60*24*$days; |
1374 | |
d115a57f |
1375 | setCookie ('MOODLEID', "", time() - 3600, "/"); |
f9903ed0 |
1376 | setCookie ('MOODLEID', rc4encrypt($thing), time()+$seconds, "/"); |
1377 | } |
1378 | |
1379 | |
1380 | function get_moodle_cookie() { |
1381 | global $MOODLEID; |
1382 | return rc4decrypt($MOODLEID); |
1383 | } |
1384 | |
1385 | |
8223d271 |
1386 | function save_session($VAR) { |
1387 | // Copies temporary session variable to permanent sesson variable |
1388 | // eg $_SESSION["USER"] = $USER; |
1389 | global $$VAR; |
1390 | $_SESSION[$VAR] = $$VAR; |
1391 | } |
1392 | |
f9903ed0 |
1393 | |
faebaf0f |
1394 | function create_user_record($username, $password) { |
1395 | // Creates a bare-bones user record |
e858f9da |
1396 | global $REMOTE_ADDR, $CFG; |
1397 | |
1398 | if ($CFG->auth_update_userinfo and function_exists(auth_get_userinfo)) { |
1399 | if ($newinfo = auth_get_userinfo($username)) { |
1400 | foreach ($newinfo as $key=>$value){ |
1401 | $newuser->$key = $value; |
1402 | } |
1403 | } |
1404 | } |
f9903ed0 |
1405 | |
faebaf0f |
1406 | $newuser->username = $username; |
1407 | $newuser->password = md5($password); |
a0bac19d |
1408 | $newuser->lang = $CFG->lang; |
faebaf0f |
1409 | $newuser->confirmed = 1; |
1410 | $newuser->lastIP = $REMOTE_ADDR; |
1411 | $newuser->timemodified = time(); |
f9903ed0 |
1412 | |
faebaf0f |
1413 | if (insert_record("user", $newuser)) { |
1414 | return get_user_info_from_db("username", $username); |
1415 | } |
1416 | return false; |
1417 | } |
1418 | |
1419 | function authenticate_user_login($username, $password) { |
1420 | // Given a username and password, this function looks them |
1421 | // up using the currently selected authentication mechanism, |
1422 | // and if the authentication is successful, it returns a |
1423 | // valid $user object from the 'user' table. |
1424 | // |
1425 | // Uses auth_ functions from the currently active auth module |
1426 | |
1427 | global $CFG; |
1428 | |
1429 | if (!isset($CFG->auth)) { |
1430 | $CFG->auth = "email"; // Default authentication module |
1431 | } |
1432 | |
e858f9da |
1433 | require_once("$CFG->dirroot/auth/$CFG->auth/lib.php"); |
faebaf0f |
1434 | |
1435 | if (auth_user_login($username, $password)) { // Successful authentication |
1436 | |
1437 | if ($user = get_user_info_from_db("username", $username)) { |
1438 | if (md5($password) <> $user->password) { |
1439 | set_field("user", "password", md5($password), "username", $username); |
1440 | } |
1441 | return $user; |
1442 | |
1443 | } else { |
1444 | return create_user_record($username, $password); |
1445 | } |
f9903ed0 |
1446 | } |
faebaf0f |
1447 | return false; |
f9903ed0 |
1448 | } |
1449 | |
faebaf0f |
1450 | |
f9903ed0 |
1451 | function get_site () { |
1452 | // Returns $course object of the top-level site. |
1453 | if ( $course = get_record("course", "category", 0)) { |
1454 | return $course; |
1455 | } else { |
1456 | return false; |
1457 | } |
1458 | } |
1459 | |
1460 | function get_admin () { |
1461 | // Returns $user object of the main admin user |
1462 | |
1463 | if ( $admins = get_records_sql("SELECT u.* FROM user u, user_admins a WHERE a.user = u.id ORDER BY u.id ASC")) { |
1464 | foreach ($admins as $admin) { |
bb09fb11 |
1465 | return $admin; // ie the first one |
f9903ed0 |
1466 | } |
1467 | } else { |
1468 | return false; |
1469 | } |
1470 | } |
1471 | |
1472 | function get_teacher($courseid) { |
1473 | // Returns $user object of the main teacher for a course |
1474 | if ( $teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t |
1475 | WHERE t.user = u.id AND t.course = '$courseid' |
1476 | ORDER BY t.authority ASC")) { |
1477 | foreach ($teachers as $teacher) { |
bb09fb11 |
1478 | if ($teacher->authority) { |
1479 | return $teacher; // the highest authority teacher |
1480 | } |
f9903ed0 |
1481 | } |
1482 | } else { |
1483 | return false; |
1484 | } |
1485 | } |
1486 | |
3c720cce |
1487 | function get_course_students($courseid, $sort="u.lastaccess DESC") { |
1488 | return get_records_sql("SELECT u.* FROM user u, user_students s |
bb09fb11 |
1489 | WHERE s.course = '$courseid' AND s.user = u.id AND u.deleted = '0' |
3c720cce |
1490 | ORDER BY $sort"); |
1491 | } |
1492 | |
1493 | function get_course_teachers($courseid, $sort="t.authority ASC") { |
b4d7002e |
1494 | return get_records_sql("SELECT u.*,t.authority,t.role FROM user u, user_teachers t |
bb09fb11 |
1495 | WHERE t.course = '$courseid' AND t.user = u.id AND u.deleted = '0' |
3c720cce |
1496 | ORDER BY $sort"); |
1497 | } |
1498 | |
1499 | function get_course_users($courseid, $sort="u.lastaccess DESC") { |
57507290 |
1500 | // Using this method because the direct SQL just would not always work! |
1501 | |
1502 | $teachers = get_course_teachers($courseid, $sort); |
1503 | $students = get_course_students($courseid, $sort); |
1504 | |
1505 | if ($teachers and $students) { |
1506 | return array_merge($teachers, $students); |
1507 | } else if ($teachers) { |
1508 | return $teachers; |
1509 | } else { |
1510 | return $students; |
1511 | } |
1512 | |
1513 | // return get_records_sql("SELECT u.* FROM user u, user_students s, user_teachers t |
1514 | // WHERE (s.course = '$courseid' AND s.user = u.id) OR |
1515 | // (t.course = '$courseid' AND t.user = u.id) |
1516 | // ORDER BY $sort"); |
3c720cce |
1517 | } |
1518 | |
f9903ed0 |
1519 | |
1520 | |
1521 | /// MODULE FUNCTIONS ///////////////////////////////////////////////// |
1522 | |
1ea8303c |
1523 | function get_coursemodule_from_instance($modulename, $instance, $courseid) { |
f9903ed0 |
1524 | // Given an instance of a module, finds the coursemodule description |
1525 | |
1526 | return get_record_sql("SELECT cm.*, m.name |
1527 | FROM course_modules cm, modules md, $modulename m |
1ea8303c |
1528 | WHERE cm.course = '$courseid' AND |
f9903ed0 |
1529 | cm.deleted = '0' AND |
1530 | cm.instance = m.id AND |
1531 | md.name = '$modulename' AND |
1532 | md.id = cm.module AND |
1533 | m.id = '$instance'"); |
1534 | |
1535 | } |
1536 | |
1ea8303c |
1537 | function get_all_instances_in_course($modulename, $courseid, $sort="cw.section") { |
f9903ed0 |
1538 | // Returns an array of all the active instances of a particular |
1539 | // module in a given course. Returns false on any errors. |
1540 | |
9ccbad71 |
1541 | return get_records_sql("SELECT m.*,cw.section,cm.id as coursemodule |
1542 | FROM course_modules cm, course_sections cw, modules md, $modulename m |
1ea8303c |
1543 | WHERE cm.course = '$courseid' AND |
f9903ed0 |
1544 | cm.instance = m.id AND |
1545 | cm.deleted = '0' AND |
9ccbad71 |
1546 | cm.section = cw.id AND |
f9903ed0 |
1547 | md.name = '$modulename' AND |
1548 | md.id = cm.module |
1549 | ORDER BY $sort"); |
1550 | |
1551 | } |
1552 | |
f9903ed0 |
1553 | |
1554 | |
1555 | |
f9903ed0 |
1556 | /// CORRESPONDENCE //////////////////////////////////////////////// |
1557 | |
5fa51a39 |
1558 | function email_to_user($user, $from, $subject, $messagetext, $messagehtml="", $attachment="", $attachname="") { |
1559 | // user - a user record as an object |
1560 | // from - a user record as an object |
1561 | // subject - plain text subject line of the email |
136dabd8 |
1562 | // messagetext - plain text version of the message |
1563 | // messagehtml - complete html version of the message (optional) |
1564 | // attachment - a file on the filesystem, relative to $CFG->dataroot |
1565 | // attachname - the name of the file (extension indicates MIME) |
f9903ed0 |
1566 | |
4216daa6 |
1567 | global $CFG, $_SERVER; |
f9903ed0 |
1568 | |
136dabd8 |
1569 | include_once("$CFG->libdir/phpmailer/class.phpmailer.php"); |
f9903ed0 |
1570 | |
5fa51a39 |
1571 | if (!$user) { |
f9903ed0 |
1572 | return false; |
1573 | } |
1574 | |
f9903ed0 |
1575 | $mail = new phpmailer; |
1576 | |
72c578ca |
1577 | $mail->Version = "Moodle $CFG->version"; // mailer version |
136dabd8 |
1578 | $mail->PluginDir = "$CFG->libdir/phpmailer/"; // plugin directory (eg smtp plugin) |
562bbe90 |
1579 | |
7f86ce17 |
1580 | if ($CFG->smtphosts) { |
1e411ffc |
1581 | $mail->IsSMTP(); // use SMTP directly |
1582 | $mail->Host = "$CFG->smtphosts"; // specify main and backup servers |
9f58537a |
1583 | |
1584 | if ($CFG->smtpuser) { // Use SMTP authentication |
1585 | $mail->SMTPAuth = true; |
1586 | $mail->Username = $CFG->smtpuser; |
1587 | $mail->Password = $CFG->smtppass; |
1588 | } |
7f86ce17 |
1589 | } else { |
1e411ffc |
1590 | $mail->IsMail(); // use PHP mail() = sendmail |
7f86ce17 |
1591 | } |
f9903ed0 |
1592 | |
136dabd8 |
1593 | $mail->From = "$from->email"; |
1594 | $mail->FromName = "$from->firstname $from->lastname"; |
1595 | $mail->Subject = stripslashes($subject); |
f9903ed0 |
1596 | |
6b174680 |
1597 | $mail->AddAddress("$user->email", "$user->firstname $user->lastname"); |
f9903ed0 |
1598 | |
f9903ed0 |
1599 | $mail->WordWrap = 70; // set word wrap |
f9903ed0 |
1600 | |
136dabd8 |
1601 | if ($messagehtml) { |
1602 | $mail->IsHTML(true); |
1603 | $mail->Body = $messagehtml; |
78681899 |
1604 | $mail->AltBody = "\n$messagetext\n"; |
136dabd8 |
1605 | } else { |
1606 | $mail->IsHTML(false); |
78681899 |
1607 | $mail->Body = "\n$messagetext\n"; |
f9903ed0 |
1608 | } |
1609 | |
136dabd8 |
1610 | if ($attachment && $attachname) { |
1611 | if (ereg( "\\.\\." ,$attachment )) { // Security check for ".." in dir path |
4216daa6 |
1612 | $adminuser = get_admin(); |
1613 | $mail->AddAddress("$adminuser->email", "$adminuser->firstname $adminuser->lastname"); |
1614 | $mail->AddStringAttachment("Error in attachment. User attempted to attach a filename with a unsafe name.", "error.txt", "8bit", "text/plain"); |
136dabd8 |
1615 | } else { |
1616 | include_once("$CFG->dirroot/files/mimetypes.php"); |
1617 | $mimetype = mimeinfo("type", $attachname); |
1618 | $mail->AddAttachment("$CFG->dataroot/$attachment", "$attachname", "base64", "$mimetype"); |
1619 | } |
f9903ed0 |
1620 | } |
1621 | |
136dabd8 |
1622 | if ($mail->Send()) { |
1623 | return true; |
1624 | } else { |
4216daa6 |
1625 | echo "ERROR: $mail->ErrorInfo\n"; |
1626 | $site = get_site(); |
1627 | add_to_log($site->id, "library", "mailer", $_SERVER["REQUEST_URI"], "ERROR: $mail->ErrorInfo"); |
f9903ed0 |
1628 | return false; |
1629 | } |
f9903ed0 |
1630 | } |
1631 | |
136dabd8 |
1632 | |
f9903ed0 |
1633 | /// FILE HANDLING ///////////////////////////////////////////// |
1634 | |
6b174680 |
1635 | function make_upload_directory($directory) { |
1636 | // $directory = a string of directory names under $CFG->dataroot |
1637 | // eg stuff/assignment/1 |
1638 | // Returns full directory if successful, false if not |
1639 | |
1640 | global $CFG; |
1641 | |
1642 | $currdir = $CFG->dataroot; |
1643 | if (!file_exists($currdir)) { |
1644 | if (! mkdir($currdir, 0750)) { |
1645 | notify("ERROR: You need to create the directory $currdir with web server write access"); |
1646 | return false; |
1647 | } |
1648 | } |
1649 | |
1650 | $dirarray = explode("/", $directory); |
1651 | |
1652 | foreach ($dirarray as $dir) { |
1653 | $currdir = "$currdir/$dir"; |
1654 | if (! file_exists($currdir)) { |
1655 | if (! mkdir($currdir, 0750)) { |
1656 | notify("ERROR: Could not find or create a directory ($currdir)"); |
1657 | return false; |
1658 | } |
1659 | } |
1660 | } |
1661 | |
1662 | return $currdir; |
1663 | } |
1664 | |
ca4f8eb8 |
1665 | function make_mod_upload_directory($courseid) { |
1666 | global $CFG; |
1667 | |
1668 | if (! $moddata = make_upload_directory("$courseid/$CFG->moddata")) { |
1669 | return false; |
1670 | } |
1671 | |
1672 | $strreadme = get_string("readme"); |
1673 | |
1674 | if (file_exists("$CFG->dirroot/lang/$CFG->lang/docs/module_files.txt")) { |
1675 | copy("$CFG->dirroot/lang/$CFG->lang/docs/module_files.txt", "$moddata/$strreadme.txt"); |
1676 | } else { |
1677 | copy("$CFG->dirroot/lang/en/docs/module_files.txt", "$moddata/$strreadme.txt"); |
1678 | } |
1679 | return $moddata; |
1680 | } |
1681 | |
6b174680 |
1682 | |
44e2d2bb |
1683 | function valid_uploaded_file($newfile) { |
1684 | // Returns current name of file on disk if true |
1685 | if (is_uploaded_file($newfile['tmp_name']) and $newfile['size'] > 0) { |
1686 | return $newfile['tmp_name']; |
1687 | } else { |
1688 | return ""; |
1689 | } |
1690 | } |
1691 | |
1692 | function get_max_upload_file_size() { |
1693 | if (! $filesize = ini_get("upload_max_filesize")) { |
1694 | $filesize = "5M"; |
1695 | } |
1696 | return get_real_size($filesize); |
1697 | } |
1698 | |
774ab660 |
1699 | function get_directory_list($rootdir, $excludefile="", $descend=true) { |
f9903ed0 |
1700 | // Returns an array with all the filenames in |
1701 | // all subdirectories, relative to the given rootdir. |
ca4f8eb8 |
1702 | // If excludefile is defined, then that file/directory is ignored |
f9903ed0 |
1703 | |
1704 | $dirs = array(); |
1705 | |
44e2d2bb |
1706 | $dir = opendir($rootdir); |
f9903ed0 |
1707 | |
d897cae4 |
1708 | if (!$dir) { |
1709 | notify("Error: unable to read this directory! : $rootdir"); |
1710 | return $dirs; |
1711 | } |
1712 | |
ca4f8eb8 |
1713 | while ($file = readdir($dir)) { |
774ab660 |
1714 | if ($file != "." and $file != ".." and $file != "CVS" and $file != $excludefile) { |
ca4f8eb8 |
1715 | $fullfile = $rootdir."/".$file; |
774ab660 |
1716 | if ($descend and filetype($fullfile) == "dir") { |
1717 | $subdirs = get_directory_list($fullfile, $excludefile, $descend); |
f9903ed0 |
1718 | foreach ($subdirs as $subdir) { |
1719 | $dirs[] = $file."/".$subdir; |
1720 | } |
1721 | } else { |
1722 | $dirs[] = $file; |
1723 | } |
1724 | } |
1725 | } |
44e2d2bb |
1726 | closedir($dir); |
f9903ed0 |
1727 | |
774ab660 |
1728 | asort($dirs); |
1729 | |
f9903ed0 |
1730 | return $dirs; |
1731 | } |
1732 | |
989bfa9d |
1733 | function get_real_size($size=0) { |
1734 | // Converts numbers like 10M into bytes |
1735 | if (!$size) { |
1736 | return 0; |
1737 | } |
1738 | $scan['MB'] = 1048576; |
1739 | $scan['M'] = 1048576; |
1740 | $scan['KB'] = 1024; |
1741 | $scan['K'] = 1024; |
1742 | |
1743 | while (list($key) = each($scan)) { |
1744 | if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) { |
1745 | $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key]; |
1746 | break; |
1747 | } |
1748 | } |
1749 | return $size; |
1750 | } |
1751 | |
44e2d2bb |
1752 | function display_size($size) { |
1753 | // Converts bytes into display form |
1754 | if ($size >= 1073741824) { |
1755 | $size = round($size / 1073741824 * 10) / 10 . "Gb"; |
1756 | } else if ($size >= 1048576) { |
1757 | $size = round($size / 1048576 * 10) / 10 . "Mb"; |
1758 | } else if ($size >= 1024) { |
1759 | $size = round($size / 1024 * 10) / 10 . "Kb"; |
1760 | } else { |
1761 | $size = $size . "b"; |
1762 | } |
1763 | return $size; |
1764 | } |
1765 | |
6b174680 |
1766 | function clean_filename($string) { |
1767 | $string = eregi_replace("\.\.", "", $string); |
1768 | $string = eregi_replace("[^([:alnum:]|\.)]", "_", $string); |
1769 | return eregi_replace("_+", "_", $string); |
1770 | } |
1771 | |
1772 | |
1180c6dc |
1773 | /// STRING TRANSLATION //////////////////////////////////////// |
1774 | |
a83fded1 |
1775 | function print_string($identifier, $module="", $a=NULL) { |
1776 | echo get_string($identifier, $module, $a); |
bcc83c41 |
1777 | } |
1778 | |
4bfa92e7 |
1779 | function current_language() { |
1780 | // Returns the code for the current language |
1781 | global $CFG, $USER; |
1782 | |
1783 | if (isset($USER->lang)) { // User language can override site language |
1784 | return $USER->lang; |
1785 | } else { |
1786 | return $CFG->lang; |
1787 | } |
1788 | } |
bcc83c41 |
1789 | |
a83fded1 |
1790 | function get_string($identifier, $module="", $a=NULL) { |
1180c6dc |
1791 | // Return the translated string specified by $identifier as |
1792 | // for $module. Uses the same format files as STphp. |
a83fded1 |
1793 | // $a is an object, string or number that can be used |
1180c6dc |
1794 | // within translation strings |
a83fded1 |
1795 | // |
1796 | // eg "hello \$a->firstname \$a->lastname" |
1797 | // or "hello \$a" |
1180c6dc |
1798 | |
4bfa92e7 |
1799 | global $CFG; |
1180c6dc |
1800 | |
4bfa92e7 |
1801 | $lang = current_language(); |
1180c6dc |
1802 | |
058eec18 |
1803 | if ($module == "") { |
1804 | $module = "moodle"; |
1180c6dc |
1805 | } |
1806 | |
058eec18 |
1807 | $langpath = "$CFG->dirroot/lang"; |
1808 | $langfile = "$langpath/$lang/$module.php"; |
1180c6dc |
1809 | |
1810 | if (!file_exists($langfile)) { // try English instead |
058eec18 |
1811 | $langfile = "$langpath/en/$module.php"; |
1180c6dc |
1812 | if (!file_exists($langfile)) { |
058eec18 |
1813 | return "ERROR: No lang file ($langpath/en/$module.php)!"; |
1180c6dc |
1814 | } |
1815 | } |
1816 | |
1817 | if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { |
1818 | |
1819 | eval($result); |
1820 | return $resultstring; |
1821 | |
1822 | } else { |
1823 | if ($lang == "en") { |
44e2d2bb |
1824 | return "[['$identifier']]"; |
1180c6dc |
1825 | |
1826 | } else { // Try looking in the english file. |
058eec18 |
1827 | $langfile = "$langpath/en/$module.php"; |
1180c6dc |
1828 | if (!file_exists($langfile)) { |
058eec18 |
1829 | return "ERROR: No lang file ($langpath/en/$module.php)!"; |
1180c6dc |
1830 | } |
1831 | if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { |
1832 | eval($result); |
1833 | return $resultstring; |
1834 | } else { |
44e2d2bb |
1835 | return "[['$identifier']]"; |
1180c6dc |
1836 | } |
1837 | } |
1838 | } |
1839 | } |
1840 | |
1841 | |
1180c6dc |
1842 | function get_string_from_file($identifier, $langfile, $destination) { |
1843 | // This function is only used from get_string(). |
1844 | include ($langfile); |
1845 | |
1846 | if (!isset ($string[$identifier])) { |
1847 | return false; |
1848 | } |
1849 | |
a83fded1 |
1850 | return "$destination = sprintf(\"".$string[$identifier]."\");"; |
1180c6dc |
1851 | } |
f9903ed0 |
1852 | |
1853 | |
1a72314d |
1854 | function get_list_of_languages() { |
1855 | /// Returns a list of language codes and their full names |
1856 | global $CFG; |
1857 | |
1858 | if (!$langdirs = get_list_of_plugins("lang")) { |
1859 | return false; |
1860 | } |
1861 | |
1862 | foreach ($langdirs as $lang) { |
1863 | include("$CFG->dirroot/lang/$lang/moodle.php"); |
1864 | $languages[$lang] = $string["thislanguage"]." ($lang)"; |
1865 | unset($string); |
1866 | } |
1867 | return $languages; |
1868 | } |
1869 | |
1870 | |
f9903ed0 |
1871 | /// ENCRYPTION //////////////////////////////////////////////// |
1872 | |
1873 | function rc4encrypt($data) { |
1874 | $password = "nfgjeingjk"; |
1875 | return endecrypt($password, $data, ""); |
1876 | } |
1877 | |
1878 | function rc4decrypt($data) { |
1879 | $password = "nfgjeingjk"; |
1880 | return endecrypt($password, $data, "de"); |
1881 | } |
1882 | |
1883 | function endecrypt ($pwd, $data, $case) { |
1884 | // Based on a class by Mukul Sabharwal [mukulsabharwal@yahoo.com] |
1885 | |
1886 | if ($case == 'de') { |
1887 | $data = urldecode($data); |
1888 | } |
1889 | |
1890 | $key[] = ""; |
1891 | $box[] = ""; |
1892 | $temp_swap = ""; |
1893 | $pwd_length = 0; |
1894 | |
1895 | $pwd_length = strlen($pwd); |
1896 | |
1897 | for ($i = 0; $i <= 255; $i++) { |
1898 | $key[$i] = ord(substr($pwd, ($i % $pwd_length), 1)); |
1899 | $box[$i] = $i; |
1900 | } |
1901 | |
1902 | $x = 0; |
1903 | |
1904 | for ($i = 0; $i <= 255; $i++) { |
1905 | $x = ($x + $box[$i] + $key[$i]) % 256; |
1906 | $temp_swap = $box[$i]; |
1907 | $box[$i] = $box[$x]; |
1908 | $box[$x] = $temp_swap; |
1909 | } |
1910 | |
1911 | $temp = ""; |
1912 | $k = ""; |
1913 | |
1914 | $cipherby = ""; |
1915 | $cipher = ""; |
1916 | |
1917 | $a = 0; |
1918 | $j = 0; |
1919 | |
1920 | for ($i = 0; $i < strlen($data); $i++) { |
1921 | $a = ($a + 1) % 256; |
1922 | $j = ($j + $box[$a]) % 256; |
1923 | $temp = $box[$a]; |
1924 | $box[$a] = $box[$j]; |
1925 | $box[$j] = $temp; |
1926 | $k = $box[(($box[$a] + $box[$j]) % 256)]; |
1927 | $cipherby = ord(substr($data, $i, 1)) ^ $k; |
1928 | $cipher .= chr($cipherby); |
1929 | } |
1930 | |
1931 | if ($case == 'de') { |
1932 | $cipher = urldecode(urlencode($cipher)); |
1933 | } else { |
1934 | $cipher = urlencode($cipher); |
1935 | } |
1936 | |
1937 | return $cipher; |
1938 | } |
1939 | |
1940 | |
1941 | /// MISCELLANEOUS //////////////////////////////////////////////////////////////////// |
1942 | |
39917a09 |
1943 | function count_words($string) { |
1944 | $string = strip_tags($string); |
1945 | return count(preg_split("/\w\b/", $string)) - 1; |
1946 | } |
1947 | |
f9903ed0 |
1948 | function getweek ($startdate, $thedate) { |
1949 | // Given dates in seconds, how many weeks is the date from startdate |
1950 | // The first week is 1, the second 2 etc ... |
1951 | |
1952 | if ($thedate < $startdate) { // error |
1953 | return 0; |
1954 | } |
1955 | |
1956 | return floor(($thedate - $startdate) / 604800.0) + 1; |
1957 | } |
1958 | |
4216daa6 |
1959 | function add_to_log($course, $module, $action, $url="", $info="") { |
1960 | // Add an entry to the log table. These are "action" focussed rather |
1961 | // than web server hits, and provide a way to easily reconstruct what |
1962 | // any particular student has been doing. |
1963 | // |
1964 | // course = the course id |
2a439ba7 |
1965 | // module = forum, journal, resource, course, user etc |
4216daa6 |
1966 | // action = view, edit, post (often but not always the same as the file.php) |
1967 | // url = the file and parameters used to see the results of the action |
1968 | // info = additional description information |
1969 | |
1970 | |
f9903ed0 |
1971 | global $db, $USER, $REMOTE_ADDR; |
1972 | |
9132e866 |
1973 | if (isset($USER->realuser)) { // Don't log |
1974 | return; |
1975 | } |
1976 | |
f9903ed0 |
1977 | $timenow = time(); |
4216daa6 |
1978 | $info = addslashes($info); |
1979 | |
1980 | $result = $db->Execute("INSERT INTO log |
1981 | SET time = '$timenow', |
1982 | user = '$USER->id', |
1983 | course = '$course', |
1984 | ip = '$REMOTE_ADDR', |
1985 | module = '$module', |
1986 | action = '$action', |
1987 | url = '$url', |
1988 | info = '$info'"); |
f9903ed0 |
1989 | if (!$result) { |
4216daa6 |
1990 | echo "<P>Error: Could not insert a new entry to the Moodle log</P>"; // Don't throw an error |
f9903ed0 |
1991 | } |
1992 | } |
1993 | |
1994 | function generate_password($maxlen=10) { |
53bfe78c |
1995 | // returns a randomly generated password of length $maxlen. inspired by |
1996 | // http://www.phpbuilder.com/columns/jesus19990502.php3 |
f9903ed0 |
1997 | |
1998 | global $CFG; |
1999 | |
2000 | $fillers = "1234567890!$-+"; |
2001 | $wordlist = file($CFG->wordlist); |
2002 | |
2003 | srand((double) microtime() * 1000000); |
2004 | $word1 = trim($wordlist[rand(0, count($wordlist) - 1)]); |
2005 | $word2 = trim($wordlist[rand(0, count($wordlist) - 1)]); |
2006 | $filler1 = $fillers[rand(0, strlen($fillers) - 1)]; |
2007 | |
2008 | return substr($word1 . $filler1 . $word2, 0, $maxlen); |
2009 | } |
2010 | |
53bfe78c |
2011 | function moodle_needs_upgrading() { |
2012 | // Checks version numbers of Main code and all modules to see |
2013 | // if there are any mismatches ... returns true or false |
2014 | global $CFG; |
2015 | |
2016 | include_once("$CFG->dirroot/version.php"); # defines $version and upgrades |
a28af1fe |
2017 | if ($CFG->version) { |
2018 | if ($version > $CFG->version) { |
53bfe78c |
2019 | return true; |
2020 | } |
1e3e716f |
2021 | if ($mods = get_list_of_plugins("mod")) { |
53bfe78c |
2022 | foreach ($mods as $mod) { |
2023 | $fullmod = "$CFG->dirroot/mod/$mod"; |
2024 | unset($module); |
2025 | include_once("$fullmod/version.php"); # defines $module with version etc |
2026 | if ($currmodule = get_record("modules", "name", $mod)) { |
2027 | if ($module->version > $currmodule->version) { |
2028 | return true; |
2029 | } |
2030 | } |
2031 | } |
2032 | } |
2033 | } else { |
2034 | return true; |
2035 | } |
2036 | return false; |
53bfe78c |
2037 | } |
2038 | |
2039 | |
1e3e716f |
2040 | function get_list_of_plugins($plugin="mod") { |
2041 | // Lists plugin directories within some directory |
2042 | |
53bfe78c |
2043 | global $CFG; |
2044 | |
1e3e716f |
2045 | $basedir = opendir("$CFG->dirroot/$plugin"); |
2046 | while ($dir = readdir($basedir)) { |
2047 | if ($dir == "." || $dir == ".." || $dir == "CVS") { |
53bfe78c |
2048 | continue; |
2049 | } |
1e3e716f |
2050 | if (filetype("$CFG->dirroot/$plugin/$dir") != "dir") { |
53bfe78c |
2051 | continue; |
2052 | } |
1e3e716f |
2053 | $plugins[] = $dir; |
53bfe78c |
2054 | } |
1e3e716f |
2055 | if ($plugins) { |
2056 | asort($plugins); |
2057 | } |
2058 | return $plugins; |
53bfe78c |
2059 | } |
2060 | |
1e3e716f |
2061 | |
b0cb5e22 |
2062 | function check_php_version($version="4.1.0") { |
2063 | // Returns true is the current version of PHP is greater that the specified one |
2064 | $minversion = intval(str_replace(".", "", $version)); |
2065 | $curversion = intval(str_replace(".", "", phpversion())); |
2066 | return ($curversion >= $minversion); |
2067 | } |
2068 | |
0095d5cd |
2069 | function check_browser_version($brand="MSIE", $version=5.5) { |
2070 | // Checks to see if is a browser matches the specified |
2071 | // brand and is equal or better version. |
2072 | global $HTTP_USER_AGENT; |
2073 | |
2074 | if (!$HTTP_USER_AGENT) { |
2075 | return false; |
2076 | } |
2077 | $string = explode(";", $HTTP_USER_AGENT); |
2078 | if (!isset($string[1])) { |
2079 | return false; |
2080 | } |
2081 | $string = explode(" ", trim($string[1])); |
2082 | if (!isset($string[0]) and !isset($string[1])) { |
2083 | return false; |
2084 | } |
2085 | if ($string[0] == $brand and (float)$string[1] >= $version ) { |
2086 | return true; |
2087 | } |
2088 | return false; |
2089 | } |
2090 | |
2091 | function can_use_richtext_editor() { |
2092 | global $USER, $CFG; |
7ce20f09 |
2093 | if ($USER->htmleditor and $CFG->htmleditor) { |
2094 | return check_browser_version("MSIE", 5.5); |
2095 | } |
2096 | return false; |
0095d5cd |
2097 | } |
2098 | |
f9903ed0 |
2099 | |
74944b73 |
2100 | function check_gd_version() { |
2101 | ob_start(); |
2102 | phpinfo(); |
2103 | $phpinfo = ob_get_contents(); |
2104 | ob_end_clean(); |
2105 | |
2106 | $phpinfo = explode("\n",$phpinfo); |
2107 | |
2108 | $gdversion = 0; |
2109 | |
2110 | foreach ($phpinfo as $text) { |
2111 | $parts = explode('</b>',$text); |
2112 | foreach ($parts as $key => $val) { |
2113 | $parts[$key] = strip_tags($val); |
2114 | } |
2115 | if ($parts[0]=="GD Version") { |
2116 | $gdversion = intval($parts[1]); |
2117 | } |
2118 | } |
2119 | |
2120 | return $gdversion; // 1, 2 or 0 |
2121 | } |
f9903ed0 |
2122 | |
0095d5cd |
2123 | |
2124 | |
f9903ed0 |
2125 | ?> |