Added missing "("
[moodle.git] / course / lib.php
CommitLineData
e4027ac9 1<?php // $Id$
97c270e9 2 // Library of useful functions
f9903ed0 3
f9903ed0 4
3d891989 5if (defined('COURSE_MAX_LOG_DISPLAY')) { // Being included again - should never happen!!
9ae687af 6 return;
7}
8
3d891989 9define('COURSE_MAX_LOG_DISPLAY', 150); // days
d9d1c35d 10
3d891989 11define('COURSE_MAX_LOGS_PER_PAGE', 1000); // records
1c0200e0 12
3d891989 13define('COURSE_LIVELOG_REFRESH', 60); // Seconds
87ffed0d 14
cb29b020 15define('COURSE_MAX_RECENT_PERIOD', 604800); // A week, in seconds
16
17define('COURSE_MAX_SUMMARIES_PER_PAGE', 10); // courses
ef58b822 18
c2cb4545 19define("FRONTPAGENEWS", 0);
20define("FRONTPAGECOURSELIST", 1);
21define("FRONTPAGECATEGORYNAMES", 2);
f9903ed0 22
9ae687af 23
f9903ed0 24function print_log_selector_form($course, $selecteduser=0, $selecteddate="today") {
25
9481285b 26 global $USER, $CFG;
27
2ac64806 28
29 $isteacher = isteacher($course->id);
f9903ed0 30 // Get all the possible users
31 $users = array();
720a43ce 32
33 if ($course->category) {
4d744a22 34 $courseusers = get_course_users($course->id);
1a5ab449 35 } else {
36 $courseusers = get_site_users("u.lastaccess DESC", "u.id, u.firstname, u.lastname");
37 }
38
39 if ($courseusers) {
40 foreach ($courseusers as $courseuser) {
2ac64806 41 $users[$courseuser->id] = fullname($courseuser, $isteacher);
122cffc9 42 }
f9903ed0 43 }
1a5ab449 44 if ($guest = get_guest()) {
f5104a2b 45 $users[$guest->id] = fullname($guest);
1a5ab449 46 }
720a43ce 47
48 if (isadmin()) {
9fa49e22 49 if ($ccc = get_records("course", "", "", "fullname")) {
720a43ce 50 foreach ($ccc as $cc) {
cfa5d3f2 51 if ($cc->category) {
52 $courses["$cc->id"] = "$cc->fullname";
53 } else {
54 $courses["$cc->id"] = " $cc->fullname (Site)";
55 }
720a43ce 56 }
f9903ed0 57 }
cfa5d3f2 58 asort($courses);
f9903ed0 59 }
60
dcde9f02 61
62 $strftimedate = get_string("strftimedate");
63 $strftimedaydate = get_string("strftimedaydate");
64
f9903ed0 65 asort($users);
66
67 // Get all the possible dates
9481285b 68 // Note that we are keeping track of real (GMT) time and user time
69 // User time is only used in displays - all calcs and passing is GMT
70
71 $timenow = time(); // GMT
72
73 // What day is it now for the user, and when is midnight that day (in GMT).
9604ccb1 74 $timemidnight = $today = usergetmidnight($timenow);
9481285b 75
76 // Put today up the top of the list
dcde9f02 77 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate) );
9481285b 78
3695f5b4 79 if (!$course->startdate or ($course->startdate > $timenow)) {
9481285b 80 $course->startdate = $course->timecreated;
81 }
f9903ed0 82
9481285b 83 $numdates = 1;
84 while ($timemidnight > $course->startdate and $numdates < 365) {
f9903ed0 85 $timemidnight = $timemidnight - 86400;
9481285b 86 $timenow = $timenow - 86400;
dcde9f02 87 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
9481285b 88 $numdates++;
f9903ed0 89 }
90
91 if ($selecteddate == "today") {
92 $selecteddate = $today;
93 }
94
95 echo "<CENTER>";
96 echo "<FORM ACTION=log.php METHOD=get>";
519d369f 97 echo "<INPUT TYPE=hidden NAME=chooselog VALUE=\"1\">";
720a43ce 98 if (isadmin()) {
849bc02a 99 choose_from_menu ($courses, "id", $course->id, "");
720a43ce 100 } else {
101 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
102 }
1a5ab449 103 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
97c270e9 104 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
105 echo "<INPUT TYPE=submit VALUE=\"".get_string("showtheselogs")."\">";
f9903ed0 106 echo "</FORM>";
107 echo "</CENTER>";
108}
109
600149be 110function make_log_url($module, $url) {
111 switch ($module) {
112 case "course":
113 case "user":
114 case "file":
115 case "login":
116 case "lib":
117 case "admin":
118 return "/$module/$url";
119 break;
120 default:
121 return "/mod/$module/$url";
122 break;
123 }
124}
125
126
519d369f 127function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100, $url="") {
9481285b 128// It is assumed that $date is the GMT time of midnight for that day,
129// and so the next 86400 seconds worth of logs are printed.
f9903ed0 130
3d891989 131 global $CFG;
47f1da80 132
720a43ce 133 if ($course->category) {
519d369f 134 $selector = "l.course='$course->id' AND l.userid = u.id";
a2ab3b05 135
720a43ce 136 } else {
519d369f 137 $selector = "l.userid = u.id"; // Show all courses
1a5ab449 138 if ($ccc = get_courses("all", "c.id ASC", "c.id,c.shortname")) {
720a43ce 139 foreach ($ccc as $cc) {
140 $courses[$cc->id] = "$cc->shortname";
141 }
142 }
143 }
f9903ed0 144
145 if ($user) {
ebc3bd2b 146 $selector .= " AND l.userid = '$user'";
f9903ed0 147 }
148
149 if ($date) {
150 $enddate = $date + 86400;
151 $selector .= " AND l.time > '$date' AND l.time < '$enddate'";
152 }
153
d09f3c80 154 $totalcount = 0; // Initialise
155
519d369f 156 if (!$logs = get_logs($selector, $order, $page*$perpage, $perpage, $totalcount)) {
f9903ed0 157 notify("No logs found!");
158 print_footer($course);
159 exit;
160 }
161
162 $count=0;
163 $tt = getdate(time());
164 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
1c0200e0 165
dcde9f02 166 $strftimedatetime = get_string("strftimedatetime");
2ac64806 167 $isteacher = isteacher($course->id);
dcde9f02 168
f9d5fd3b 169 echo "<p align=center>";
519d369f 170 print_string("displayingrecords", "", $totalcount);
f9d5fd3b 171 echo "</p>";
1c0200e0 172
519d369f 173 print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&");
174
f9d5fd3b 175 echo "<table border=0 align=center cellpadding=3 cellspacing=3>";
f9903ed0 176 foreach ($logs as $log) {
600149be 177
9fa49e22 178 if ($ld = get_record("log_display", "module", "$log->module", "action", "$log->action")) {
565f7a95 179 $log->info = get_field($ld->mtable, $ld->field, "id", $log->info);
600149be 180 }
181
f9d5fd3b 182 echo "<tr nowrap>";
720a43ce 183 if (! $course->category) {
f9d5fd3b 184 echo "<td nowrap><font size=2><a href=\"view.php?id=$log->course\">".$courses[$log->course]."</a></td>";
720a43ce 185 }
f9d5fd3b 186 echo "<td nowrap align=right><font size=2>".userdate($log->time, "%a")."</td>";
187 echo "<td nowrap><font size=2>".userdate($log->time, $strftimedatetime)."</td>";
dc0dc7d5 188 echo "<td nowrap><font size=2>";
47f1da80 189 link_to_popup_window("/lib/ipatlas/plot.php?address=$log->ip&user=$log->userid", "ipatlas","$log->ip", 400, 700);
f9d5fd3b 190 echo "</td>";
2ac64806 191 $fullname = fullname($log, $isteacher);
192 echo "<td nowrap><font size=2><a href=\"../user/view.php?id=$log->userid&course=$log->course\"><b>$fullname</b></td>";
f9d5fd3b 193 echo "<td nowrap><font size=2>";
600149be 194 link_to_popup_window( make_log_url($log->module,$log->url), "fromloglive","$log->module $log->action", 400, 600);
f9d5fd3b 195 echo "</td>";
196 echo "<td nowrap><font size=2>$log->info</td>";
197 echo "</tr>";
f9903ed0 198 }
f9d5fd3b 199 echo "</table>";
519d369f 200
201 print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&");
f9903ed0 202}
203
204
c2cb4545 205function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
206 global $CFG;
207 if (empty($CFG->gdversion)) {
208 echo "(".get_string("gdneed").")";
d887b5a7 209 } else {
c2cb4545 210 echo "<IMG BORDER=0 SRC=\"$CFG->wwwroot/course/loggraph.php?id=$course->id&user=$userid&type=$type&date=$date\">";
d887b5a7 211 }
212}
213
214
f9903ed0 215
600149be 216function print_recent_activity($course) {
217 // $course is an object
218 // This function trawls through the logs looking for
219 // anything new since the user's last login
220
bf40f9c1 221 global $CFG, $USER, $THEME, $SESSION;
600149be 222
2ac64806 223 $isteacher = isteacher($course->id);
224
600149be 225 if (! $USER->lastlogin ) {
3d891989 226 echo "<p align=center><font size=1>";
4b1371a7 227 print_string("welcometocourse", "", $course->shortname);
3d891989 228 echo "</font></p>";
600149be 229 return;
4c654ee3 230 } else {
3d891989 231 echo "<p align=center><font size=1>";
4c654ee3 232 echo get_string("yourlastlogin").":<BR>";
dcde9f02 233 echo userdate($USER->lastlogin, get_string("strftimerecentfull"));
3d891989 234 echo "</font></p>";
235 }
236
237 $timestart = $USER->lastlogin;
b1992e65 238 $timemaxrecent = time() - COURSE_MAX_RECENT_PERIOD;
239 if ($timestart < $timemaxrecent) {
240 $timestart = $timemaxrecent;
600149be 241 }
242
600149be 243
244 // Firstly, have there been any new enrolments?
245
246 $heading = false;
247 $content = false;
1b5910c4 248
6c38b7e0 249 $users = get_recent_enrolments($course->id, $timestart);
1b5910c4 250
6c38b7e0 251 if ($users) {
e2b7251d 252 echo "<p>";
6c38b7e0 253 foreach ($users as $user) {
600149be 254 if (! $heading) {
4c654ee3 255 print_headline(get_string("newusers").":");
600149be 256 $heading = true;
257 $content = true;
258 }
2ac64806 259 $fullname = fullname($user, $isteacher);
260 echo "<font size=1><a href=\"../user/view.php?id=$user->id&course=$course->id\">$fullname</a></font><br />";
600149be 261 }
e2b7251d 262 echo "</p>";
600149be 263 }
264
1b5910c4 265 // Next, have there been any modifications to the course structure?
266
267 $logs = get_records_select("log", "time > '$timestart' AND course = '$course->id' AND
268 module = 'course' AND action LIKE '% mod'", "time ASC");
269
270 if ($logs) {
271 foreach ($logs as $key => $log) {
272 $info = split(" ", $log->info);
c9f6251e 273
274 if ($info[0] == "label") { // Labels are special activities
275 continue;
276 }
277
1b5910c4 278 $modname = get_field($info[0], "name", "id", $info[1]);
279 //Create a temp valid module structure (course,id)
280 $tempmod->course = $log->course;
281 $tempmod->id = $info[1];
282 //Obtain the visible property from the instance
283 $modvisible = instance_is_visible($info[0],$tempmod);
284
285 //Only if the mod is visible
286 if ($modvisible) {
287 switch ($log->action) {
288 case "add mod":
289 $stradded = get_string("added", "moodle", get_string("modulename", $info[0]));
290 $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
291 break;
292 case "update mod":
293 $strupdated = get_string("updated", "moodle", get_string("modulename", $info[0]));
294 if (empty($changelist["$log->info"])) {
295 $changelist["$log->info"] = array ("operation" => "update", "text" => "$strupdated:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
296 }
297 break;
298 case "delete mod":
299 if (!empty($changelist["$log->info"]["operation"]) and
300 $changelist["$log->info"]["operation"] == "add") {
301 $changelist["$log->info"] = NULL;
302 } else {
303 $strdeleted = get_string("deletedactivity", "moodle", get_string("modulename", $info[0]));
304 $changelist["$log->info"] = array ("operation" => "delete", "text" => $strdeleted);
305 }
306 break;
600149be 307 }
ef25340c 308 }
309 }
310 }
311
9c9f7d77 312 if (!empty($changelist)) {
ef25340c 313 foreach ($changelist as $changeinfo => $change) {
314 if ($change) {
315 $changes[$changeinfo] = $change;
316 }
317 }
8a59942e 318 if (isset($changes)){
319 if (count($changes) > 0) {
320 print_headline(get_string("courseupdates").":");
321 $content = true;
322 foreach ($changes as $changeinfo => $change) {
323 echo "<p><font size=1>".$change["text"]."</font></p>";
324 }
600149be 325 }
326 }
bf40f9c1 327 }
328
329
330 // If this site uses Library module, then print recent items
331 if (!empty($CFG->librarypath)) {
332 if (file_exists("$CFG->dirroot/$CFG->librarypath/librarylib.php")) {
333 include_once("$CFG->dirroot/$CFG->librarypath/librarylib.php");
334 if (librarysummarize(5, '', date('YmdHis',$USER->lastlogin))) {
335 $content = true;
336 }
337 }
600149be 338 }
339
3869a2ac 340 // Now display new things from each module
600149be 341
0fd7da81 342 $mods = get_records("modules", "visible", "1", "name", "id, name");
343
1b5910c4 344 foreach ($mods as $mod) { // Each module gets it's own logs and prints them
0fd7da81 345 include_once("$CFG->dirroot/mod/$mod->name/lib.php");
346 $print_recent_activity = $mod->name."_print_recent_activity";
1b5910c4 347 if (function_exists($print_recent_activity)) {
348 $modcontent = $print_recent_activity($course, $isteacher, $timestart);
3869a2ac 349 if ($modcontent) {
350 $content = true;
600149be 351 }
600149be 352 }
353 }
354
355 if (! $content) {
3d891989 356 echo "<font size=2>".get_string("nothingnew")."</font>";
600149be 357 }
600149be 358}
359
e1360728 360
d897cae4 361function get_array_of_activities($courseid) {
362// For a given course, returns an array of course activity objects
363// Each item in the array contains he following properties:
364// cm - course module id
365// mod - name of the module (eg forum)
366// section - the number of the section (eg week or topic)
367// name - the name of the instance
5867bfb5 368// visible - is the instance visible or not
86aa7ccf 369// extra - contains extra string to include in any link
d897cae4 370
8dddba42 371 global $CFG;
372
d897cae4 373 $mod = array();
374
9fa49e22 375 if (!$rawmods = get_course_mods($courseid)) {
d897cae4 376 return NULL;
377 }
378
379 if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) {
380 foreach ($sections as $section) {
74666583 381 if (!empty($section->sequence)) {
d897cae4 382 $sequence = explode(",", $section->sequence);
383 foreach ($sequence as $seq) {
7af6281f 384 if (empty($rawmods[$seq])) {
385 continue;
386 }
d897cae4 387 $mod[$seq]->cm = $rawmods[$seq]->id;
388 $mod[$seq]->mod = $rawmods[$seq]->modname;
389 $mod[$seq]->section = $section->section;
390 $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance));
fec5a6a6 391 $mod[$seq]->visible = $rawmods[$seq]->visible;
86aa7ccf 392 $mod[$seq]->extra = "";
8dddba42 393
394 $modname = $mod[$seq]->mod;
395 $functionname = $modname."_get_coursemodule_info";
396
397 include_once("$CFG->dirroot/mod/$modname/lib.php");
398
399 if (function_exists($functionname)) {
400 if ($extra = $functionname($rawmods[$seq])) {
401 $mod[$seq]->extra = $extra;
c9f6251e 402 }
403 }
d897cae4 404 }
405 }
406 }
407 }
408 return $mod;
409}
410
411
412
e1360728 413
90845098 414function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
415// Returns a number of useful structures for course displays
7468bf01 416
90845098 417 $mods = NULL; // course modules indexed by id
418 $modnames = NULL; // all course module names
94361e02 419 $modnamesplural= NULL; // all course module names (plural form)
90845098 420 $modnamesused = NULL; // course module names used
7468bf01 421
9fa49e22 422 if ($allmods = get_records("modules")) {
90845098 423 foreach ($allmods as $mod) {
5867bfb5 424 if ($mod->visible) {
425 $modnames[$mod->name] = get_string("modulename", "$mod->name");
426 $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
427 }
90845098 428 }
429 asort($modnames);
430 } else {
431 error("No modules are installed!");
432 }
433
9fa49e22 434 if ($rawmods = get_course_mods($courseid)) {
7468bf01 435 foreach($rawmods as $mod) { // Index the mods
436 $mods[$mod->id] = $mod;
90845098 437 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
1acfbce5 438 if ($mod->visible or isteacher($courseid)) {
439 $modnamesused[$mod->modname] = $modnames[$mod->modname];
440 }
7468bf01 441 }
c7da6f7a 442 if ($modnamesused) {
443 asort($modnamesused);
444 }
7468bf01 445 }
7468bf01 446}
447
9fa49e22 448
7468bf01 449function get_all_sections($courseid) {
450
d26d7ed0 451 return get_records("course_sections", "course", "$courseid", "section",
7d99d695 452 "section, id, course, summary, sequence, visible");
7468bf01 453}
454
b86fc0e2 455function course_set_display($courseid, $display=0) {
456 global $USER;
457
458 if (empty($USER)) {
459 return false;
460 }
461
462 if ($display == "all" or empty($display)) {
463 $display = 0;
464 }
465
466 if (record_exists("course_display", "userid", $USER->id, "course", $courseid)) {
467 set_field("course_display", "display", $display, "userid", $USER->id, "course", $courseid);
468 } else {
469 $record->userid = $USER->id;
470 $record->course = $courseid;
471 $record->display = $display;
472 if (!insert_record("course_display", $record)) {
473 notify("Could not save your course display!");
474 }
475 }
476
477 return $USER->display[$courseid] = $display; // Note: = not ==
478}
479
7d99d695 480function set_section_visible($courseid, $sectionnumber, $visibility) {
481/// For a given course section, markes it visible or hidden,
482/// and does the same for every activity in that section
483
484 if ($section = get_record("course_sections", "course", $courseid, "section", $sectionnumber)) {
485 set_field("course_sections", "visible", "$visibility", "id", $section->id);
486 if (!empty($section->sequence)) {
487 $modules = explode(",", $section->sequence);
488 foreach ($modules as $moduleid) {
489 set_field("course_modules", "visible", "$visibility", "id", $moduleid);
490 }
491 }
5867bfb5 492 rebuild_course_cache($courseid);
7d99d695 493 }
494}
ba2e5d73 495
5e367a2d 496function print_section_block($heading, $course, $section, $mods, $modnames, $modnamesused,
52dcc2f9 497 $absolute=true, $width="100%") {
5e367a2d 498
2c8cb30c 499 global $CFG, $USER, $THEME;
52dcc2f9 500 static $isteacher;
501 static $isediting;
2c8cb30c 502 static $ismoving;
503 static $strmovehere;
504 static $strmovefull;
52dcc2f9 505
506 if (!isset($isteacher)) {
507 $isteacher = isteacher($course->id);
508 }
509 if (!isset($isediting)) {
510 $isediting = isediting($course->id);
511 }
2c8cb30c 512 if (!isset($ismoving)) {
513 $ismoving = ismoving($course->id);
514 }
5e367a2d 515
516 $modinfo = unserialize($course->modinfo);
517 $moddata = array();
518 $modicon = array();
519 $editbuttons = "";
520
74666583 521 if (!empty($section->sequence)) {
5e367a2d 522
523 $sectionmods = explode(",", $section->sequence);
524
2c8cb30c 525 if ($ismoving) {
526 $strmovehere = get_string("movehere");
f36b1996 527 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
2c8cb30c 528 $stractivityclipboard = $USER->activitycopyname;
529 $strcancel= get_string("cancel");
c9f6251e 530 $modicon[] = "&nbsp;<img align=bottom src=\"$CFG->pixpath/t/move.gif\" height=\"11\" width=\"11\">";
2c8cb30c 531 $moddata[] = "$USER->activitycopyname&nbsp;(<a href=\"$CFG->wwwroot/course/mod.php?cancelcopy=true\">$strcancel</a>)";
532 }
533
5e367a2d 534 foreach ($sectionmods as $modnumber) {
52dcc2f9 535 if (empty($mods[$modnumber])) {
536 continue;
537 }
5e367a2d 538 $mod = $mods[$modnumber];
2c8cb30c 539 if ($isediting and !$ismoving) {
540 $editbuttons = "<br />".make_editing_buttons($mod->id, $absolute, $mod->visible, true);
541 } else {
542 $editbuttons = "";
1acfbce5 543 }
52dcc2f9 544 if ($mod->visible or $isteacher) {
2c8cb30c 545 if ($ismoving) {
546 if ($mod->id == $USER->activitycopy) {
547 continue;
548 }
549 $modicon[] = "";
6951a894 550 $moddata[] = "<a title=\"$strmovefull\"".
551 " href=\"$CFG->wwwroot/course/mod.php?moveto=$mod->id\">".
552 "<img height=\"16\" width=\"80\" src=\"$CFG->pixpath/movehere.gif\" ".
553 " alt=\"$strmovehere\" border=\"0\"></a>";
2c8cb30c 554 }
1acfbce5 555 $instancename = urldecode($modinfo[$modnumber]->name);
c2cb4545 556 $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
3ee3e01a 557 if (!empty($modinfo[$modnumber]->extra)) {
558 $extra = urldecode($modinfo[$modnumber]->extra);
559 } else {
560 $extra = "";
561 }
7977cffd 562
c9f6251e 563 if ($mod->modname == "label") {
564 $modicon[] = "";
565 $moddata[] = format_text($extra, FORMAT_HTML).$editbuttons;
566 } else {
e845dee2 567 $modicon[] = "<img src=\"$CFG->modpixpath/$mod->modname/icon.gif\"".
c9f6251e 568 " height=\"16\" width=\"16\" alt=\"$mod->modfullname\">";
569 $moddata[] = "<a title=\"$mod->modfullname\" $linkcss $extra".
570 "href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</a>".
571 "$editbuttons";
572 }
5e367a2d 573 }
5e367a2d 574 }
575 }
2c8cb30c 576 if ($ismoving) {
577 $modicon[] = "";
6951a894 578 $moddata[] = "<a title=\"$strmovefull\"".
579 " href=\"$CFG->wwwroot/course/mod.php?movetosection=$section->id\">".
580 "<img height=\"16\" width=\"80\" src=\"$CFG->pixpath/movehere.gif\" ".
581 " alt=\"$strmovehere\" border=\"0\"></a>";
2c8cb30c 582 }
a44d18e7 583 if ($isediting) {
15ac9065 584 $editmenu = popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section->section&add=",
5e367a2d 585 $modnames, "section0", "", get_string("add")."...", "mods", get_string("activities"), true);
dfec7b01 586 $editmenu = "<div align=right>$editmenu</div>";
47f1da80 587 } else {
588 $editmenu = "";
5e367a2d 589 }
590
591 print_side_block($heading, "", $moddata, $modicon, $editmenu, $width);
592}
593
594
d897cae4 595function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
52dcc2f9 596/// Prints a section full of activity modules
7977cffd 597 global $CFG, $USER;
598
52dcc2f9 599 static $isteacher;
600 static $isediting;
7977cffd 601 static $ismoving;
602 static $strmovehere;
603 static $strmovefull;
52dcc2f9 604
605 if (!isset($isteacher)) {
606 $isteacher = isteacher($course->id);
607 }
608 if (!isset($isediting)) {
609 $isediting = isediting($course->id);
610 }
7977cffd 611 if (!isset($ismoving)) {
612 $ismoving = ismoving($course->id);
613 }
614 if ($ismoving) {
615 $strmovehere = get_string("movehere");
f36b1996 616 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
7977cffd 617 }
94361e02 618
c408b0c4 619 $modinfo = unserialize($course->modinfo);
94361e02 620
567df742 621 echo "<table width=\"$width\" cellpadding=1 cellspacing=0>\n";
74666583 622 if (!empty($section->sequence)) {
94361e02 623
624 $sectionmods = explode(",", $section->sequence);
625
626 foreach ($sectionmods as $modnumber) {
9ae687af 627 if (empty($mods[$modnumber])) {
628 continue;
629 }
94361e02 630 $mod = $mods[$modnumber];
c9f6251e 631
52dcc2f9 632 if ($mod->visible or $isteacher) {
c9f6251e 633 echo "<tr><td class=\"activity$mod->modname\">";
7977cffd 634 if ($ismoving) {
635 if ($mod->id == $USER->activitycopy) {
636 continue;
637 }
6951a894 638 echo "<a title=\"$strmovefull\"".
639 " href=\"mod.php?moveto=$mod->id\">".
640 "<img height=\"16\" width=\"80\" src=\"$CFG->pixpath/movehere.gif\" ".
641 " alt=\"$strmovehere\" border=\"0\"></a><br />\n";
1acfbce5 642 }
7977cffd 643 $instancename = urldecode($modinfo[$modnumber]->name);
c9f6251e 644
86aa7ccf 645 if (!empty($modinfo[$modnumber]->extra)) {
646 $extra = urldecode($modinfo[$modnumber]->extra);
647 } else {
648 $extra = "";
649 }
c9f6251e 650
aac94fd0 651 if ($mod->indent) {
652 print_spacer(12, 20 * $mod->indent, false);
653 }
654
c9f6251e 655 if ($mod->modname == "label") {
aac94fd0 656 if (!$mod->visible) {
657 echo "<span class=\"dimmed_text\">";
658 }
c9f6251e 659 echo format_text($extra, FORMAT_HTML);
aac94fd0 660 if (!$mod->visible) {
661 echo "</span>";
662 }
c9f6251e 663
664 } else { // Normal activity
665 $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
e845dee2 666 echo "<img src=\"$CFG->modpixpath/$mod->modname/icon.gif\"".
c9f6251e 667 " height=16 width=16 alt=\"$mod->modfullname\">".
668 " <font size=2><a title=\"$mod->modfullname\" $linkcss $extra".
669 " href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</a></font>";
670 }
c9f6251e 671 if ($isediting) {
ffc69cd3 672 echo "&nbsp;&nbsp;";
aac94fd0 673 echo make_editing_buttons($mod->id, $absolute, $mod->visible, true, $mod->indent);
c9f6251e 674 }
ffc69cd3 675 echo "</td>";
c9f6251e 676 echo "</tr>";
94361e02 677 }
94361e02 678 }
679 }
7977cffd 680 if ($ismoving) {
6951a894 681 echo "<tr><td><a title=\"$strmovefull\"".
682 " href=\"mod.php?movetosection=$section->id\">".
683 "<img height=\"16\" width=\"80\" src=\"$CFG->pixpath/movehere.gif\" ".
684 " alt=\"$strmovehere\" border=\"0\"></a></td></tr>\n";
7977cffd 685 }
c9f6251e 686 echo "</table>\n\n";
a7ad3ea6 687}
688
5867bfb5 689
690function rebuild_course_cache($courseid=0) {
691// Rebuilds the cached list of course activities stored in the database
692// If a courseid is not specified, then all are rebuilt
693
694 if ($courseid) {
695 $select = "id = '$courseid'";
696 } else {
697 $select = "";
698 }
699
700 if ($courses = get_records_select("course", $select)) {
701 foreach ($courses as $course) {
702 $modinfo = serialize(get_array_of_activities($course->id));
703 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
704 notify("Could not cache module information for course '$course->fullname'!");
705 }
706 }
707 }
708}
709
710
7541bc3e 711function print_heading_block($heading, $width="100%", $class="headingblock") {
5e367a2d 712 global $THEME;
713
7541bc3e 714 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
715 echo "<tr><td bgcolor=\"$THEME->cellheading\" class=\"$class\">";
5e367a2d 716 echo stripslashes($heading);
717 echo "</td></tr></table>";
5e367a2d 718}
719
720function print_side_block($heading="", $content="", $list=NULL, $icons=NULL, $footer="", $width=180) {
721// Prints a nice side block with an optional header. The content can either
722// be a block of HTML or a list of text with optional icons.
a7ad3ea6 723
5e367a2d 724 global $THEME;
725
7541bc3e 726 print_side_block_start($heading, $width);
727
5e367a2d 728 if ($content) {
7541bc3e 729 echo "$content";
5e367a2d 730 } else {
5e367a2d 731 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
732 foreach ($list as $key => $string) {
7541bc3e 733 echo "<tr bgcolor=\"$THEME->cellcontent2\">";
5e367a2d 734 if ($icons) {
7541bc3e 735 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"16\">".$icons[$key]."</td>";
a7ad3ea6 736 }
7541bc3e 737 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"*\"><font size=\"-1\">$string</font></td>";
5e367a2d 738 echo "</tr>";
a7ad3ea6 739 }
5e367a2d 740 if ($footer) {
7541bc3e 741 echo "<tr bgcolor=\"$THEME->cellcontent2\">";
5e367a2d 742 if ($icons) {
7541bc3e 743 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"16\">&nbsp;</td>";
5e367a2d 744 }
7541bc3e 745 echo "<td class=\"sideblocklinks\"><font size=\"-1\">$footer</td>";
5e367a2d 746 echo "</tr>";
747 }
748 echo "</table>";
a7ad3ea6 749 }
5e367a2d 750
7541bc3e 751 print_side_block_end();
752}
753
754function print_side_block_start($heading="", $width=180, $class="sideblockmain") {
755// Starts a nice side block with an optional header.
756
757 global $THEME;
758
759 echo "<table class=\"sideblock\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
760 if ($heading) {
761 echo "<tr>";
762 echo "<td class=\"sideblockheading\" bgcolor=\"$THEME->cellheading\">$heading</td>";
763 echo "</tr>";
764 }
765 echo "<tr>";
766 echo "<td class=\"$class\" bgcolor=\"$THEME->cellcontent2\">";
767}
768
769function print_side_block_end() {
770 echo "</td></tr>";
7d99d695 771 echo "</table><br />";
94361e02 772}
773
5e367a2d 774
670fddf1 775function print_admin_links ($siteid, $width=180) {
dc0dc7d5 776 global $CFG, $THEME;
2b25f2a0 777
440eb0d7 778 if (isadmin()) {
2f4d324b 779 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/configure.php\">".get_string("configuration")."</a>...";
c9f6251e 780 $modicon[]="<img src=\"$CFG->pixpath/i/admin.gif\" height=16 width=16 alt=\"\" />";
2f4d324b 781
782 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/users.php\">".get_string("users")."</a>...";
c9f6251e 783 $modicon[]="<img src=\"$CFG->pixpath/i/users.gif\" height=16 width=16 alt=\"\" />";
440eb0d7 784 }
2f4d324b 785
55e4b5f9 786 if (iscreator()) {
787 $moddata[]="<a href=\"$CFG->wwwroot/course/index.php?edit=on\">".get_string("courses")."</a>";
c9f6251e 788 $modicon[]="<img src=\"$CFG->pixpath/i/course.gif\" height=16 width=16 alt=\"\" />";
55e4b5f9 789 $fulladmin = "";
790 }
2f4d324b 791
1924074c 792 if (isadmin()) {
55e4b5f9 793 $moddata[]="<a href=\"$CFG->wwwroot/course/log.php?id=$siteid\">".get_string("logs")."</a>";
c9f6251e 794 $modicon[]="<img src=\"$CFG->pixpath/i/log.gif\" height=16 width=16 alt=\"\" />";
2f4d324b 795
440eb0d7 796 $moddata[]="<a href=\"$CFG->wwwroot/files/index.php?id=$siteid\">".get_string("sitefiles")."</a>";
c9f6251e 797 $modicon[]="<img src=\"$CFG->pixpath/i/files.gif\" height=16 width=16 alt=\"\" />";
2f4d324b 798
be09831a 799 if (file_exists("$CFG->dirroot/$CFG->admin/$CFG->dbtype")) {
2f4d324b 800 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/$CFG->dbtype/frame.php\">".
801 get_string("managedatabase")."</a>";
73d0da80 802 $modicon[]="<img src=\"$CFG->pixpath/i/db.gif\" height=16 width=16 alt=\"\" />";
1924074c 803 }
5867bfb5 804 $fulladmin = "<p><a href=\"$CFG->wwwroot/$CFG->admin/\">".get_string("admin")."</a>...";
1924074c 805 }
5e367a2d 806
807 print_side_block(get_string("administration"), "", $moddata, $modicon, $fulladmin, $width);
808
dc0dc7d5 809 echo "<img src=\"$CFG->wwwroot/pix/spacer.gif\" width=\"$width\" height=1><br>";
2b25f2a0 810}
811
b4d7002e 812function print_course_admin_links($course, $width=180) {
dc0dc7d5 813 global $USER, $CFG, $THEME;
44dad735 814
fce1ce13 815 if (isguest()) {
816 return true;
817 }
13469b82 818 if (isteacher($course->id)) {
f3c6d219 819 if (isteacheredit($course->id)) {
c9f6251e 820 $adminicon[]="<img src=\"$CFG->pixpath/i/edit.gif\" height=16 width=16 alt=\"\">";
02ebf404 821 if (isediting($course->id)) {
822 $admindata[]="<a href=\"view.php?id=$course->id&edit=off\">".get_string("turneditingoff")."</a>";
823 } else {
824 $admindata[]="<a href=\"view.php?id=$course->id&edit=on\">".get_string("turneditingon")."</a>";
825 }
826 $admindata[]="<a href=\"edit.php?id=$course->id\">".get_string("settings")."...</a>";
c9f6251e 827 $adminicon[]="<img src=\"$CFG->pixpath/i/settings.gif\" height=16 width=16 alt=\"\">";
3b1bb969 828
829 if (iscreator() or !empty($CFG->teacherassignteachers)) {
830 if (!$course->teachers) {
831 $course->teachers = get_string("defaultcourseteachers");
832 }
833 $admindata[]="<a href=\"teacher.php?id=$course->id\">$course->teachers...</a>";
c9f6251e 834 $adminicon[]="<img src=\"$CFG->pixpath/i/users.gif\" height=16 width=16 alt=\"\">";
02ebf404 835 }
4d9d90c0 836 }
02ebf404 837
a33c4ae8 838 if (!$course->students) {
839 $course->students = get_string("defaultcoursestudents");
3b1bb969 840 }
4d9d90c0 841 $admindata[]="<a href=\"student.php?id=$course->id\">$course->students...</a>";
c9f6251e 842 $adminicon[]="<img src=\"$CFG->pixpath/i/users.gif\" height=16 width=16 alt=\"\">";
7d569d28 843
4d9d90c0 844 if (isteacheredit($course->id)) {
02ebf404 845 $admindata[]="<a href=\"$CFG->wwwroot/backup/backup.php?id=$course->id\">".get_string("backup")."...</a>";
c9f6251e 846 $adminicon[]="<img src=\"$CFG->pixpath/i/backup.gif\" height=16 width=16 alt=\"\">";
02ebf404 847
07bc2435 848 $admindata[]="<a href=\"$CFG->wwwroot/files/index.php?id=$course->id&wdir=/backupdata\">".get_string("restore")."...</a>";
c9f6251e 849 $adminicon[]="<img src=\"$CFG->pixpath/i/restore.gif\" height=16 width=16 alt=\"\">";
02ebf404 850 $admindata[]="<a href=\"scales.php?id=$course->id\">".get_string("scales")."...</a>";
c9f6251e 851 $adminicon[]="<img src=\"$CFG->pixpath/i/scales.gif\" height=16 width=16 alt=\"\">";
13469b82 852 }
13469b82 853
dc0dc7d5 854 $admindata[]="<a href=\"grades.php?id=$course->id\">".get_string("grades")."...</a>";
c9f6251e 855 $adminicon[]="<img src=\"$CFG->pixpath/i/grades.gif\" height=16 width=16 alt=\"\">";
13469b82 856
dc0dc7d5 857 $admindata[]="<a href=\"log.php?id=$course->id\">".get_string("logs")."...</a>";
c9f6251e 858 $adminicon[]="<img src=\"$CFG->pixpath/i/log.gif\" height=16 width=16 alt=\"\">";
dc0dc7d5 859
860 $admindata[]="<a href=\"$CFG->wwwroot/files/index.php?id=$course->id\">".get_string("files")."...</a>";
c9f6251e 861 $adminicon[]="<img src=\"$CFG->pixpath/i/files.gif\" height=16 width=16 alt=\"\">";
f354106d 862
dc0dc7d5 863 $admindata[]="<a href=\"$CFG->wwwroot/doc/view.php?id=$course->id&file=teacher.html\">".get_string("help")."...</a>";
c9f6251e 864 $adminicon[]="<img src=\"$CFG->modpixpath/resource/icon.gif\" height=16 width=16 alt=\"\">";
b4d7002e 865
13469b82 866 if ($teacherforum = forum_get_course_forum($course->id, "teacher")) {
eafc0b44 867 $admindata[]="<a href=\"$CFG->wwwroot/mod/forum/view.php?f=$teacherforum->id\">$teacherforum->name</a>";
c9f6251e 868 $adminicon[]="<img src=\"$CFG->modpixpath/forum/icon.gif\" height=16 width=16 alt=\"\">";
13469b82 869 }
f354106d 870
e550e2d6 871 } else if (!isguest()) { // Students menu
5b8540e0 872 if ($course->showgrades) {
fff79722 873 $admindata[]="<a href=\"grade.php?id=$course->id\">".get_string("grades")."...</a>";
c9f6251e 874 $adminicon[]="<img src=\"$CFG->pixpath/i/grades.gif\" height=16 width=16 alt=\"\">";
fff79722 875 }
3f125001 876 if ($course->showreports) {
877 $admindata[]="<a href=\"user.php?id=$course->id&user=$USER->id\">".get_string("activityreport")."...</a>";
878 $adminicon[]="<img src=\"$CFG->pixpath/i/report.gif\" height=16 width=16 alt=\"\">";
879 }
e550e2d6 880 if ($CFG->auth == "email" or $CFG->auth == "none" or $CFG->auth == "manual") {
881 $admindata[]="<a href=\"$CFG->wwwroot/login/change_password.php?id=$course->id\">".
882 get_string("changepassword")."...</a>";
c9f6251e 883 $adminicon[]="<img src=\"$CFG->pixpath/i/user.gif\" height=16 width=16 alt=\"\">";
e550e2d6 884 } else if ($CFG->changepassword) {
885 $admindata[]="<a href=\"$CFG->changepassword\">".get_string("changepassword")."...</a>";
c9f6251e 886 $adminicon[]="<img src=\"$CFG->pixpath/i/user.gif\" height=16 width=16 alt=\"\">";
e550e2d6 887 }
5204d831 888 if ($CFG->allowunenroll) {
889 $admindata[]="<a href=\"unenrol.php?id=$course->id\">".get_string("unenrolme", "", $course->shortname)."...</a>";
3f125001 890 $adminicon[]="<img src=\"$CFG->pixpath/i/user.gif\" height=16 width=16 alt=\"\">";
5204d831 891 }
13469b82 892 }
44dad735 893
f354106d 894 if (!empty($admindata)) {
895 print_side_block(get_string("administration"), "", $admindata, $adminicon, "", $width);
896 }
44dad735 897}
2b25f2a0 898
c2cb4545 899
900function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
901/// Given an empty array, this function recursively travels the
902/// categories, building up a nice list for display. It also makes
903/// an array that list all the parents for each category.
904
905 if ($category) {
906 if ($path) {
907 $path = "$path / $category->name";
908 } else {
909 $path = "$category->name";
910 }
911 $list[$category->id] = $path;
912 } else {
913 $category->id = 0;
914 }
915
916 if ($categories = get_categories("$category->id")) { // Print all the children recursively
917 foreach ($categories as $cat) {
918 if (!empty($category->id)) {
3bd4de22 919 if (isset($parents[$category->id])) {
2832badf 920 $parents[$cat->id] = $parents[$category->id];
921 }
c2cb4545 922 $parents[$cat->id][] = $category->id;
923 }
e92fe848 924 make_categories_list($list, $parents, $cat, $path);
c2cb4545 925 }
926 }
927}
928
929
c2cb4545 930function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1) {
931/// Recursive function to print out all the categories in a nice format
932/// with or without courses included
933
934 if (!$displaylist) {
e92fe848 935 make_categories_list($displaylist, $parentslist);
c2cb4545 936 }
937
938 if ($category) {
3af6e1db 939 if ($category->visible or iscreator()) {
d2b6ba70 940 print_category_info($category, $depth);
c2cb4545 941 } else {
942 return; // Don't bother printing children of invisible categories
943 }
944
945 } else {
c2cb4545 946 $category->id = "0";
947 }
948
949 if ($categories = get_categories($category->id)) { // Print all the children recursively
950 $countcats = count($categories);
951 $count = 0;
952 $first = true;
953 $last = false;
954 foreach ($categories as $cat) {
955 $count++;
956 if ($count == $countcats) {
957 $last = true;
958 }
959 $up = $first ? false : true;
960 $down = $last ? false : true;
961 $first = false;
962
963 print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1);
964 }
965 }
c2cb4545 966}
967
968
d2b6ba70 969function print_category_info($category, $depth) {
970/// Prints the category info in indented fashion
971/// This function is only used by print_whole_category_list() above
c2cb4545 972
973 global $CFG;
b48f834c 974 static $strallowguests, $strrequireskey, $strsummary;
c2cb4545 975
b48f834c 976 if (empty($strsummary)) {
977 $strallowguests = get_string("allowguests");
978 $strrequireskey = get_string("requireskey");
979 $strsummary = get_string("summary");
980 }
ba2e5d73 981
d5f26b07 982 $catlinkcss = $category->visible ? "" : " class=\"dimmed\" ";
983
8ef9cb56 984 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
c9f6251e 985 $catimage = "<img src=\"$CFG->pixpath/i/course.gif\" width=16 height=16 border=0>";
b48f834c 986 } else {
987 $catimage = "&nbsp";
8ef9cb56 988 }
b48f834c 989
990 echo "\n\n<table border=0 cellpadding=3 cellspacing=0 width=\"100%\"><tr>";
d2b6ba70 991
c2cb4545 992 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
b48f834c 993 $courses = get_courses($category->id);
994
995 echo "<tr>";
996
997 if ($depth) {
998 $indent = $depth*30;
999 $rows = count($courses) + 1;
1000 echo "<td rowspan=\"$rows\" valign=\"top\" width=\"$indent\">";
1001 print_spacer(10, $indent);
1002 echo "</td>";
1003 }
1004
1005 echo "<td valign=\"top\">$catimage</td>";
cb29b020 1006 echo "<td valign=\"top\" width=\"100%\" class=\"categoryname\">";
b48f834c 1007 echo "<a $catlinkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a>";
cb29b020 1008 echo "</td>";
1009 echo "<td class=\"categoryname\">&nbsp;</td>";
b48f834c 1010 echo "</tr>\n";
1011
1012 if ($courses) {
c2cb4545 1013 foreach ($courses as $course) {
1014 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
b48f834c 1015 echo "<tr><td valign=\"top\" width=\"30\">&nbsp;";
cb29b020 1016 echo "</td>\n<td valign=\"top\" width=\"100%\" class=\"coursename\">";
a6870f0c 1017 echo "<a $linkcss href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
cb29b020 1018 echo "</td>\n<td align=\"right\" valign=\"top\" nowrap class=\"coursename\">";
c2cb4545 1019 if ($course->guest ) {
1020 echo "<a title=\"$strallowguests\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
a9a070e0 1021 echo "<img hspace=1 alt=\"\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/guest.gif\"></a>";
ebe8ddc1 1022 } else {
c9f6251e 1023 echo "<img alt=\"\" height=16 width=18 border=0 src=\"$CFG->pixpath/spacer.gif\">";
0c656181 1024 }
c2cb4545 1025 if ($course->password) {
1026 echo "<a title=\"$strrequireskey\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
c9f6251e 1027 echo "<img hspace=1 alt=\"\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/key.gif\"></a>";
ebe8ddc1 1028 } else {
c9f6251e 1029 echo "<img alt=\"\" height=16 width=18 border=0 src=\"$CFG->pixpath/spacer.gif\">";
b48f834c 1030 }
1031 if ($course->summary) {
1032 link_to_popup_window ("/course/info.php?id=$course->id", "courseinfo",
c9f6251e 1033 "<img hspace=1 alt=\"info\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/info.gif\">",
b48f834c 1034 400, 500, $strsummary);
ebe8ddc1 1035 } else {
c9f6251e 1036 echo "<img alt=\"\" height=16 width=18 border=0 src=\"$CFG->pixpath/spacer.gif\">";
0c656181 1037 }
b48f834c 1038 echo "</td></tr>\n";
0c656181 1039 }
ba2e5d73 1040 }
d2b6ba70 1041 } else {
b48f834c 1042
1043 if ($depth) {
1044 $indent = $depth*20;
1045 echo "<td valign=\"top\" width=\"$indent\">";
1046 print_spacer(10, $indent);
1047 echo "</td>";
d2b6ba70 1048 }
b48f834c 1049
cb29b020 1050 echo "<td valign=\"top\" width=\"100%\" class=\"categoryname\">";
b48f834c 1051 echo "<a $catlinkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a>";
cb29b020 1052 echo "</td>";
a0456978 1053 echo "<td valign=\"top\" class=\"categorynumber\">$category->coursecount</td></tr>";
c2cb4545 1054 }
b48f834c 1055 echo "\n</table>\n";
c2cb4545 1056}
1057
1058function print_courses_sideblock($category=0, $width="100%") {
f3120024 1059 global $CFG, $THEME, $USER;
c2cb4545 1060
1061 if (empty($THEME->custompix)) {
1062 $icon = "<img src=\"$CFG->wwwroot/pix/i/course.gif\"".
1063 " height=\"16\" width=\"16\" alt=\"".get_string("course")."\">";
1064 } else {
1065 $icon = "<img src=\"$CFG->wwwroot/theme/$CFG->theme/pix/i/course.gif\"".
1066 " height=\"16\" width=\"16\" alt=\"".get_string("course")."\">";
1067 }
1068
f3120024 1069 if (isset($USER->id) and !isadmin()) { // Just print My Courses
1070 if ($courses = get_my_courses($USER->id)) {
1071 foreach ($courses as $course) {
c81696e5 1072 if (!$course->category) {
1073 continue;
1074 }
d1d74247 1075 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
f3120024 1076 $moddata[]="<a $linkcss title=\"$course->shortname\" ".
1077 "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
1078 $modicon[]=$icon;
1079 }
5dc3d80e 1080 $fulllist = "<p><a href=\"$CFG->wwwroot/course/index.php\">".get_string("fulllistofcourses")."</a>...";
f3120024 1081 print_side_block( get_string("mycourses"), "", $moddata, $modicon, $fulllist, $width);
1082 return;
1083 }
1084 }
1085
c571f3fc 1086 $categories = get_categories("0"); // Parent = 0 ie top-level categories only
c2cb4545 1087 if (count($categories) > 1) { // Just print top level category links
1088 foreach ($categories as $category) {
1089 $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
d2b6ba70 1090 $moddata[]="<a $linkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a>";
c2cb4545 1091 $modicon[]=$icon;
0c656181 1092 }
b5333476 1093 $fulllist = "<p><a href=\"$CFG->wwwroot/course/\">".get_string("searchcourses")."</a>...";
c2cb4545 1094 } else { // Just print course names of single category
90c2ca2e 1095 $category = array_shift($categories);
d2b6ba70 1096 $courses = get_courses($category->id);
1097
c2cb4545 1098 if ($courses) {
1099 foreach ($courses as $course) {
1100 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
1101 $moddata[]="<a $linkcss title=\"$course->shortname\" ".
1102 "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
1103 $modicon[]=$icon;
1104 }
5dc3d80e 1105 $fulllist = "<p><a href=\"$CFG->wwwroot/course/index.php\">".get_string("fulllistofcourses")."</a>...";
c2cb4545 1106 } else {
1107 $moddata = array();
1108 $modicon = array();
1109 $fulllist = get_string("nocoursesyet");
0c656181 1110 }
ba2e5d73 1111 }
c2cb4545 1112
f3120024 1113 print_side_block( get_string("courses"), "", $moddata, $modicon, $fulllist, $width);
ba2e5d73 1114}
94361e02 1115
c2cb4545 1116
1117function print_courses($category, $width="100%") {
1118/// Category is 0 (for all courses) or an object
1119
1120 global $CFG, $THEME;
1121
1122 if (empty($category)) {
90c2ca2e 1123 $categories = get_categories(0); // Parent = 0 ie top-level categories only
1124 if (count($categories) == 1) {
1125 $category = array_shift($categories);
d2b6ba70 1126 $courses = get_courses($category->id);
90c2ca2e 1127 } else {
d2b6ba70 1128 $courses = get_courses("all");
90c2ca2e 1129 }
1130 unset($categories);
607809b3 1131 } else {
c2cb4545 1132 $categories = get_categories($category->id); // sub categories
d2b6ba70 1133 $courses = get_courses($category->id);
c2cb4545 1134 }
1135
c2cb4545 1136 if ($courses) {
1137 foreach ($courses as $course) {
1138 print_course($course, $width);
1139 echo "<br />\n";
1140 }
1141 } else {
1142 print_heading(get_string("nocoursesyet"));
1143 }
1144
1145}
1146
1147
1148function print_course($course, $width="100%") {
1149
1150 global $CFG, $THEME;
1151
1152 if (! $site = get_site()) {
1153 error("Could not find a site!");
1154 }
1155
1b6a4b1d 1156 print_simple_box_start("center", "$width", $THEME->cellcontent, 5, "coursebox");
c2cb4545 1157
22288704 1158 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
1159
c2cb4545 1160 echo "<table width=\"100%\">";
1161 echo "<tr valign=top>";
1b6a4b1d 1162 echo "<td valign=top width=\"50%\" class=\"courseboxinfo\">";
c2cb4545 1163 echo "<p><font size=3><b><a title=\"".get_string("entercourse")."\"
22288704 1164 $linkcss href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a></b></font></p>";
c2cb4545 1165 if ($teachers = get_course_teachers($course->id)) {
1166 echo "<p><font size=\"1\">\n";
1167 foreach ($teachers as $teacher) {
1168 if ($teacher->authority > 0) {
1169 if (!$teacher->role) {
1170 $teacher->role = $course->teacher;
1171 }
2ac64806 1172 $fullname = fullname($teacher, isteacher($course->id)); // is the USER a teacher of that course
1173 echo "$teacher->role: <a href=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$fullname</a><br />";
c2cb4545 1174 }
1175 }
1176 echo "</font></p>";
1177 }
1178 if ($course->guest) {
1179 $strallowguests = get_string("allowguests");
1180 echo "<a title=\"$strallowguests\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
a9a070e0 1181 echo "<img vspace=4 alt=\"$strallowguests\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/guest.gif\"></a>&nbsp;&nbsp;";
c2cb4545 1182 }
1183 if ($course->password) {
1184 $strrequireskey = get_string("requireskey");
1185 echo "<a title=\"$strrequireskey\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
c9f6251e 1186 echo "<img vspace=4 alt=\"$strrequireskey\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/key.gif\"></a>";
c2cb4545 1187 }
1188
1189
1b6a4b1d 1190 echo "</td><td valign=top width=\"50%\" class=\"courseboxsummary\">";
c2cb4545 1191 echo "<p><font size=2>".text_to_html($course->summary)."</font></p>";
1192 echo "</td></tr>";
1193 echo "</table>";
1194
1195 print_simple_box_end();
1196}
1197
1198
1199function print_my_moodle() {
1200/// Prints custom user information on the home page.
1201/// Over time this can include all sorts of information
1202
1203 global $USER, $CFG;
1204
1205 if (!isset($USER->id)) {
1206 error("It shouldn't be possible to see My Moodle without being logged in.");
1207 }
1208
1209 if ($courses = get_my_courses($USER->id)) {
1210 foreach ($courses as $course) {
c81696e5 1211 if (!$course->category) {
1212 continue;
1213 }
c2cb4545 1214 print_course($course, "100%");
1215 echo "<br />\n";
1216 }
38a10939 1217
7f989948 1218 if (count_records("course") > (count($courses) + 1) ) { // Some courses not being displayed
1219 echo "<table width=\"100%\"><tr><td align=\"center\">";
1220 print_course_search("", false, "short");
1221 echo "</td><td align=\"center\">";
1222 print_single_button("$CFG->wwwroot/course/index.php", NULL, get_string("fulllistofcourses"), "get");
1223 echo "</td></tr></table>\n";
1224 }
26330001 1225 } else {
1226 if (count_records("course_categories") > 1) {
cb29b020 1227 print_simple_box_start("center", "100%", "#FFFFFF", 5, "categorybox");
26330001 1228 print_whole_category_list();
1229 print_simple_box_end();
1230 } else {
1231 print_courses(0, "100%");
1232 }
607809b3 1233 }
2b8cef80 1234}
1235
11b0c469 1236
a8b56716 1237function print_course_search($value="", $return=false, $format="plain") {
38a10939 1238
1239 global $CFG;
1240
1241 $strsearchcourses= get_string("searchcourses");
1242
a8b56716 1243 if ($format == "plain") {
1244 $output = "<center><p align=\"center\" class=\"coursesearchbox\">";
1245 $output .= "<form name=\"coursesearch\" action=\"$CFG->wwwroot/course/search.php\" method=\"get\">";
1246 $output .= "<input type=\"text\" size=30 name=\"search\" value=\"$value\">";
1247 $output .= "<input type=\"submit\" value=\"$strsearchcourses\">";
1248 $output .= "</form></p></center>";
e4027ac9 1249 } else if ($format == "short") {
1250 $output = "<center><p align=\"center\" class=\"coursesearchbox\">";
1251 $output .= "<form name=\"coursesearch\" action=\"$CFG->wwwroot/course/search.php\" method=\"get\">";
1252 $output .= "<input type=\"text\" size=12 name=\"search\" value=\"$value\">";
1253 $output .= "<input type=\"submit\" value=\"$strsearchcourses\">";
1254 $output .= "</form></p></center>";
a8b56716 1255 } else if ($format == "navbar") {
1256 $output = "<table border=0 cellpadding=0 cellspacing=0><tr><td nowrap>";
1257 $output .= "<form name=\"coursesearch\" action=\"$CFG->wwwroot/course/search.php\" method=\"get\">";
1258 $output .= "<input type=\"text\" size=20 name=\"search\" value=\"$value\">";
1259 $output .= "<input type=\"submit\" value=\"$strsearchcourses\">";
a8b56716 1260 $output .= "</form>";
1261 $output .= "</td></tr></table>";
1262 }
1263
1264 if ($return) {
1265 return $output;
1266 }
1267 echo $output;
38a10939 1268}
11b0c469 1269
1270/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1271
1272function add_course_module($mod) {
11b0c469 1273
e5dfd0f3 1274 $mod->added = time();
11b0c469 1275
e5dfd0f3 1276 return insert_record("course_modules", $mod);
11b0c469 1277}
1278
7977cffd 1279function add_mod_to_section($mod, $beforemod=NULL) {
1280/// Given a full mod object with section and course already defined
1281/// If $before is specified, then this is an existing ID which we
1282/// will insert the new module before
1283///
1284/// Returns the course_sections ID where the mod is inserted
11b0c469 1285
9fa49e22 1286 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
7977cffd 1287
1288 $section->sequence = trim($section->sequence);
1289
1290 if (empty($section->sequence)) {
11b0c469 1291 $newsequence = "$mod->coursemodule";
7977cffd 1292
1293 } else if ($beforemod) {
1294 $modarray = explode(",", $section->sequence);
1295
1296 if ($key = array_keys ($modarray, $beforemod->id)) {
1297 $insertarray = array($mod->id, $beforemod->id);
1298 array_splice($modarray, $key[0], 1, $insertarray);
1299 $newsequence = implode(",", $modarray);
1300
1301 } else { // Just tack it on the end anyway
1302 $newsequence = "$section->sequence,$mod->coursemodule";
1303 }
1304
1305 } else {
1306 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 1307 }
7977cffd 1308
e5dfd0f3 1309 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
1310 return $section->id; // Return course_sections ID that was used.
11b0c469 1311 } else {
e5dfd0f3 1312 return 0;
11b0c469 1313 }
1314
1315 } else { // Insert a new record
e5dfd0f3 1316 $section->course = $mod->course;
1317 $section->section = $mod->section;
1318 $section->summary = "";
1319 $section->sequence = $mod->coursemodule;
1320 return insert_record("course_sections", $section);
11b0c469 1321 }
1322}
1323
1acfbce5 1324function hide_course_module($mod) {
1325 return set_field("course_modules", "visible", 0, "id", $mod);
1326}
1327
1328function show_course_module($mod) {
1329 return set_field("course_modules", "visible", 1, "id", $mod);
1330}
1331
11b0c469 1332function delete_course_module($mod) {
1333 return set_field("course_modules", "deleted", 1, "id", $mod);
1334}
1335
1336function delete_mod_from_section($mod, $section) {
11b0c469 1337
e5dfd0f3 1338 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 1339
e5dfd0f3 1340 $modarray = explode(",", $section->sequence);
11b0c469 1341
1342 if ($key = array_keys ($modarray, $mod)) {
1343 array_splice($modarray, $key[0], 1);
1344 $newsequence = implode(",", $modarray);
e5dfd0f3 1345 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 1346 } else {
1347 return false;
1348 }
1349
11b0c469 1350 }
7977cffd 1351 return false;
11b0c469 1352}
1353
12905134 1354function move_section($course, $section, $move) {
1355/// Moves a whole course section up and down within the course
1356
1357 if (!$move) {
1358 return true;
1359 }
1360
1361 $sectiondest = $section + $move;
1362
1363 if ($sectiondest > $course->numsections or $sectiondest < 1) {
1364 return false;
1365 }
1366
1367 if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
1368 return false;
1369 }
1370
1371 if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
1372 return false;
1373 }
1374
56e34ee4 1375 if (!set_field("course_sections", "section", $sectiondest, "id", $sectionrecord->id)) {
12905134 1376 return false;
1377 }
56e34ee4 1378 if (!set_field("course_sections", "section", $section, "id", $sectiondestrecord->id)) {
12905134 1379 return false;
1380 }
1381 return true;
1382}
1383
1384
7977cffd 1385function moveto_module($mod, $section, $beforemod=NULL) {
1386/// All parameters are objects
1387/// Move the module object $mod to the specified $section
1388/// If $beforemod exists then that is the module
1389/// before which $modid should be inserted
1390
1391/// Remove original module from original section
1392
1393 if (! delete_mod_from_section($mod->id, $mod->section)) {
1394 notify("Could not delete module from existing section");
1395 }
1396
1397/// Update module itself if necessary
1398
1399 if ($mod->section != $section->id) {
1400 $mod->section = $section->id;
1401
1402 if (!update_record("course_modules", $mod)) {
1403 return false;
1404 }
1405 }
1406
1407/// Add the module into the new section
1408
1409 $mod->course = $section->course;
1410 $mod->section = $section->section; // need relative reference
1411 $mod->coursemodule = $mod->id;
1412
1413 if (! add_mod_to_section($mod, $beforemod)) {
1414 return false;
1415 }
1416
1417 return true;
1418
1419}
1420
1421
11b0c469 1422
7c0f2984 1423function move_module($cm, $move) {
12905134 1424/// Moves an activity module up and down within the course
11b0c469 1425
1426 if (!$move) {
1427 return true;
1428 }
1429
11b0c469 1430 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
1431 error("This course section doesn't exist");
1432 }
1433
1434 $mods = explode(",", $thissection->sequence);
1435
1436 $len = count($mods);
1437 $pos = array_keys($mods, $cm->id);
1438 $thepos = $pos[0];
1439
1440 if ($len == 0 || count($pos) == 0 ) {
1441 error("Very strange. Could not find the required module in this section.");
1442 }
1443
1444 if ($len == 1) {
1445 $first = true;
1446 $last = true;
1447 } else {
1448 $first = ($thepos == 0);
1449 $last = ($thepos == $len - 1);
1450 }
1451
1452 if ($move < 0) { // Moving the module up
1453
1454 if ($first) {
ad41694c 1455 if ($thissection->section == 0) { // First section, do nothing
11b0c469 1456 return true;
ad41694c 1457
11b0c469 1458 } else { // Push onto end of previous section
1459 $prevsectionnumber = $thissection->section - 1;
9fa49e22 1460 if (! $prevsection = get_record("course_sections", "course", "$thissection->course",
1461 "section", "$prevsectionnumber")) {
11b0c469 1462 error("Previous section ($prevsection->id) doesn't exist");
1463 }
1464
74666583 1465 if (!empty($prevsection->sequence)) {
11b0c469 1466 $newsequence = "$prevsection->sequence,$cm->id";
1467 } else {
1468 $newsequence = "$cm->id";
1469 }
1470
1471 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
1472 error("Previous section could not be updated");
1473 }
1474
1475 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
1476 error("Module could not be updated");
1477 }
1478
1479 array_splice($mods, 0, 1);
1480 $newsequence = implode(",", $mods);
1481 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1482 error("Module could not be updated");
1483 }
1484
1485 return true;
1486
1487 }
1488 } else { // move up within this section
1489 $swap = $mods[$thepos-1];
1490 $mods[$thepos-1] = $mods[$thepos];
1491 $mods[$thepos] = $swap;
1492
1493 $newsequence = implode(",", $mods);
1494 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1495 error("This section could not be updated");
1496 }
1497 return true;
1498 }
1499
1500 } else { // Moving the module down
1501
1502 if ($last) {
1503 $nextsectionnumber = $thissection->section + 1;
9fa49e22 1504 if ($nextsection = get_record("course_sections", "course", "$thissection->course",
1505 "section", "$nextsectionnumber")) {
11b0c469 1506
74666583 1507 if (!empty($nextsection->sequence)) {
11b0c469 1508 $newsequence = "$cm->id,$nextsection->sequence";
1509 } else {
1510 $newsequence = "$cm->id";
1511 }
1512
1513 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
1514 error("Next section could not be updated");
1515 }
1516
1517 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
1518 error("Module could not be updated");
1519 }
1520
1521 array_splice($mods, $thepos, 1);
1522 $newsequence = implode(",", $mods);
1523 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1524 error("This section could not be updated");
1525 }
1526 return true;
1527
1528 } else { // There is no next section, so just return
1529 return true;
1530
1531 }
1532 } else { // move down within this section
1533 $swap = $mods[$thepos+1];
1534 $mods[$thepos+1] = $mods[$thepos];
1535 $mods[$thepos] = $swap;
1536
1537 $newsequence = implode(",", $mods);
1538 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1539 error("This section could not be updated");
1540 }
1541 return true;
1542 }
1543 }
1544}
1545
aac94fd0 1546function make_editing_buttons($moduleid, $absolute=false, $visible=true, $moveselect=true, $indent=-1) {
dc0dc7d5 1547 global $CFG, $THEME;
94361e02 1548
4de4fdfe 1549 static $str = '';
1acfbce5 1550 if (empty($str)) {
1551 $str->delete = get_string("delete");
493486c4 1552 $str->move = get_string("move");
1acfbce5 1553 $str->moveup = get_string("moveup");
1554 $str->movedown = get_string("movedown");
aac94fd0 1555 $str->moveright = get_string("moveright");
1556 $str->moveleft = get_string("moveleft");
1acfbce5 1557 $str->update = get_string("update");
1558 $str->hide = get_string("hide");
1559 $str->show = get_string("show");
1560 }
94361e02 1561
1562 if ($absolute) {
dc0dc7d5 1563 $path = "$CFG->wwwroot/course";
1564 } else {
1565 $path = ".";
1566 }
1567
1568 if (empty($THEME->custompix)) {
1569 $pixpath = "$path/../pix";
94361e02 1570 } else {
dc0dc7d5 1571 $pixpath = "$path/../theme/$CFG->theme/pix";
94361e02 1572 }
1acfbce5 1573
1574 if ($visible) {
7977cffd 1575 $hideshow = "<a title=\"$str->hide\" href=\"$path/mod.php?hide=$moduleid\"><img".
1576 " src=\"$pixpath/t/hide.gif\" hspace=2 height=11 width=11 border=0></a> ";
1acfbce5 1577 } else {
7977cffd 1578 $hideshow = "<a title=\"$str->show\" href=\"$path/mod.php?show=$moduleid\"><img".
1579 " src=\"$pixpath/t/show.gif\" hspace=2 height=11 width=11 border=0></a> ";
1580 }
1581
1582 if ($moveselect) {
1583 $move = "<a title=\"$str->move\" href=\"$path/mod.php?copy=$moduleid\"><img".
bf1241ad 1584 " src=\"$pixpath/t/move.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\"></a>";
493486c4 1585 } else {
1586 $move = "<a title=\"$str->moveup\" href=\"$path/mod.php?id=$moduleid&move=-1\"><img".
bf1241ad 1587 " src=\"$pixpath/t/up.gif\" hspace=\"2\" height=11 width=11 border=0></a>".
493486c4 1588 "<a title=\"$str->movedown\" href=\"$path/mod.php?id=$moduleid&move=1\"><img".
bf1241ad 1589 " src=\"$pixpath/t/down.gif\" hspace=\"2\" height=11 width=11 border=0></a>";
7977cffd 1590 }
1591
aac94fd0 1592 $leftright = "";
1593 if ($indent > 0) {
1594 $leftright .= "<a title=\"$str->moveleft\" href=\"$path/mod.php?id=$moduleid&indent=-1\"><img".
bf1241ad 1595 " src=\"$pixpath/t/left.gif\" hspace=\"2\" height=11 width=11 border=0></a>";
aac94fd0 1596 }
1597 if ($indent >= 0) {
1598 $leftright .= "<a title=\"$str->moveright\" href=\"$path/mod.php?id=$moduleid&indent=1\"><img".
bf1241ad 1599 " src=\"$pixpath/t/right.gif\" hspace=\"2\" height=11 width=11 border=0></a>";
aac94fd0 1600 }
1601
bf1241ad 1602 return "$leftright$move".
7977cffd 1603 "<a title=\"$str->update\" href=\"$path/mod.php?update=$moduleid\"><img".
bf1241ad 1604 " src=\"$pixpath/t/edit.gif\" hspace=\"2\" height=11 width=11 border=0></a>".
1605 "<a title=\"$str->delete\" href=\"$path/mod.php?delete=$moduleid\"><img".
1606 " src=\"$pixpath/t/delete.gif\" hspace=\"2\" height=11 width=11 border=0></a>$hideshow";
90845098 1607}
1608
f9903ed0 1609?>