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