// $Id$ // Library of useful functions if (defined('COURSE_MAX_LOG_DISPLAY')) { // Being included again - should never happen!! return; } define('COURSE_MAX_LOG_DISPLAY', 150); // days define('COURSE_MAX_LOGS_PER_PAGE', 1000); // records define('COURSE_LIVELOG_REFRESH', 60); // Seconds define('COURSE_MAX_RECENT_PERIOD', 604800); // A week, in seconds define("FRONTPAGENEWS", 0); define("FRONTPAGECOURSELIST", 1); define("FRONTPAGECATEGORYNAMES", 2); function print_log_selector_form($course, $selecteduser=0, $selecteddate="today") { global $USER, $CFG; // Get all the possible users $users = array(); if ($course->category) { if ($courseusers = get_course_users($course->id, "u.lastaccess DESC")) { foreach ($courseusers as $courseuser) { $users[$courseuser->id] = "$courseuser->firstname $courseuser->lastname"; } } if ($guest = get_guest()) { $users[$guest->id] = "$guest->firstname $guest->lastname"; } } if (isadmin()) { if ($ccc = get_records("course", "", "", "fullname")) { foreach ($ccc as $cc) { if ($cc->category) { $courses["$cc->id"] = "$cc->fullname"; } else { $courses["$cc->id"] = " $cc->fullname (Site)"; } } } asort($courses); } $strftimedate = get_string("strftimedate"); $strftimedaydate = get_string("strftimedaydate"); asort($users); // Get all the possible dates // Note that we are keeping track of real (GMT) time and user time // User time is only used in displays - all calcs and passing is GMT $timenow = time(); // GMT // What day is it now for the user, and when is midnight that day (in GMT). $timemidnight = $today = usergetmidnight($timenow); // Put today up the top of the list $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate) ); if (! $course->startdate) { $course->startdate = $course->timecreated; } $numdates = 1; while ($timemidnight > $course->startdate and $numdates < 365) { $timemidnight = $timemidnight - 86400; $timenow = $timenow - 86400; $dates["$timemidnight"] = userdate($timenow, $strftimedaydate); $numdates++; } if ($selecteddate == "today") { $selecteddate = $today; } echo "
"; print_string("displayingrecords", "", $totalcountlogs); echo "
"; echo "course\">".$courses[$log->course]." | "; } echo "".userdate($log->time, "%a")." | "; echo "".userdate($log->time, $strftimedatetime)." | "; echo ""; link_to_popup_window("/lib/ipatlas/plot.php?address=$log->ip&user=$log->userid", "ipatlas","$log->ip", 400, 700); echo " | "; echo "userid&course=$log->course\">$log->firstname $log->lastname | "; echo ""; link_to_popup_window( make_log_url($log->module,$log->url), "fromloglive","$log->module $log->action", 400, 600); echo " | "; echo "$log->info | "; echo "
"; print_string("welcometocourse", "", $course->shortname); echo "
"; return; } else { echo "";
echo get_string("yourlastlogin").":
";
echo userdate($USER->lastlogin, get_string("strftimerecentfull"));
echo "
id&course=$course->id\">$user->firstname $user->lastname
"; } } } // Next, have there been any modifications to the course structure? $logs = get_records_select("log", "time > '$timestart' AND course = '$course->id' AND module = 'course' AND action LIKE '% mod'", "time ASC"); if ($logs) { foreach ($logs as $key => $log) { $info = split(" ", $log->info); $modname = get_field($info[0], "name", "id", $info[1]); //Create a temp valid module structure (course,id) $tempmod->course = $log->course; $tempmod->id = $info[1]; //Obtain the visible property from the instance $modvisible = instance_is_visible($info[0],$tempmod); //Only if the mod is visible if ($modvisible) { switch ($log->action) { case "add mod": $stradded = get_string("added", "moodle", get_string("modulename", $info[0])); $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:".$change["text"]."
"; } } } // Now display new things from each module $mods = get_list_of_plugins("mod"); $isteacher = isteacher($course->id); foreach ($mods as $mod) { // Each module gets it's own logs and prints them include_once("$CFG->dirroot/mod/$mod/lib.php"); $print_recent_activity = $mod."_print_recent_activity"; if (function_exists($print_recent_activity)) { $modcontent = $print_recent_activity($course, $isteacher, $timestart); if ($modcontent) { $content = true; } } } if (! $content) { echo "".get_string("nothingnew").""; } } function get_array_of_activities($courseid) { // For a given course, returns an array of course activity objects // Each item in the array contains he following properties: // cm - course module id // mod - name of the module (eg forum) // section - the number of the section (eg week or topic) // name - the name of the instance // visible - is the instance visible or not // extra - contains extra string to include in any link $mod = array(); if (!$rawmods = get_course_mods($courseid)) { return NULL; } if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) { foreach ($sections as $section) { if (!empty($section->sequence)) { $sequence = explode(",", $section->sequence); foreach ($sequence as $seq) { if (empty($rawmods[$seq])) { continue; } $mod[$seq]->cm = $rawmods[$seq]->id; $mod[$seq]->mod = $rawmods[$seq]->modname; $mod[$seq]->section = $section->section; $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance)); $mod[$seq]->visible = $rawmods[$seq]->visible; $mod[$seq]->extra = ""; // This part is an ugly hack that doesn't belong here// if ($mod[$seq]->mod == "resource") { if ($resource = get_record("resource", "id", $rawmods[$seq]->instance)) { if ($resource->type == 5 and $resource->alltext) { $mod[$seq]->extra = urlencode("target=\"resource$resource->id\" onClick=\"return ". "openpopup('/mod/resource/view.php?id=". $mod[$seq]->cm. "','resource$resource->id','$resource->alltext');\""); } } } } } } } return $mod; } function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) { // Returns a number of useful structures for course displays $mods = NULL; // course modules indexed by id $modnames = NULL; // all course module names $modnamesplural= NULL; // all course module names (plural form) $modnamesused = NULL; // course module names used if ($allmods = get_records("modules")) { foreach ($allmods as $mod) { if ($mod->visible) { $modnames[$mod->name] = get_string("modulename", "$mod->name"); $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name"); } } asort($modnames); } else { error("No modules are installed!"); } if ($rawmods = get_course_mods($courseid)) { foreach($rawmods as $mod) { // Index the mods $mods[$mod->id] = $mod; $mods[$mod->id]->modfullname = $modnames[$mod->modname]; if ($mod->visible or isteacher($courseid)) { $modnamesused[$mod->modname] = $modnames[$mod->modname]; } } if ($modnamesused) { asort($modnamesused); } } } function get_all_sections($courseid) { return get_records("course_sections", "course", "$courseid", "section", "section, id, course, summary, sequence, visible"); } function course_set_display($courseid, $display=0) { global $USER; if (empty($USER)) { return false; } if ($display == "all" or empty($display)) { $display = 0; } if (record_exists("course_display", "userid", $USER->id, "course", $courseid)) { set_field("course_display", "display", $display, "userid", $USER->id, "course", $courseid); } else { $record->userid = $USER->id; $record->course = $courseid; $record->display = $display; if (!insert_record("course_display", $record)) { notify("Could not save your course display!"); } } return $USER->display[$courseid] = $display; // Note: = not == } function set_section_visible($courseid, $sectionnumber, $visibility) { /// For a given course section, markes it visible or hidden, /// and does the same for every activity in that section if ($section = get_record("course_sections", "course", $courseid, "section", $sectionnumber)) { set_field("course_sections", "visible", "$visibility", "id", $section->id); if (!empty($section->sequence)) { $modules = explode(",", $section->sequence); foreach ($modules as $moduleid) { set_field("course_modules", "visible", "$visibility", "id", $moduleid); } } rebuild_course_cache($courseid); } } function print_section_block($heading, $course, $section, $mods, $modnames, $modnamesused, $absolute=true, $width="100%") { global $CFG; static $isteacher; static $isediting; if (!isset($isteacher)) { $isteacher = isteacher($course->id); } if (!isset($isediting)) { $isediting = isediting($course->id); } $modinfo = unserialize($course->modinfo); $moddata = array(); $modicon = array(); $editbuttons = ""; if (!empty($section->sequence)) { $sectionmods = explode(",", $section->sequence); foreach ($sectionmods as $modnumber) { if (empty($mods[$modnumber])) { continue; } $mod = $mods[$modnumber]; if ($isediting) { $editbuttons = make_editing_buttons($mod->id, $absolute, $mod->visible, false); } if ($mod->visible or $isteacher) { $instancename = urldecode($modinfo[$modnumber]->name); $linkcss = $mod->visible ? "" : " class=\"dimmed\" "; if (!empty($modinfo[$modnumber]->extra)) { $extra = urldecode($modinfo[$modnumber]->extra); } else { $extra = ""; } $modicon[] = "\n";
if (!empty($section->sequence)) {
$sectionmods = explode(",", $section->sequence);
foreach ($sectionmods as $modnumber) {
if (empty($mods[$modnumber])) {
continue;
}
$mod = $mods[$modnumber];
if ($mod->visible or $isteacher) {
if ($ismoving) {
if ($mod->id == $USER->activitycopy) {
continue;
}
echo " -> id\">$strmovehere \n"; } $instancename = urldecode($modinfo[$modnumber]->name); if (!empty($modinfo[$modnumber]->extra)) { $extra = urldecode($modinfo[$modnumber]->extra); } else { $extra = ""; } $linkcss = $mod->visible ? "" : " class=\"dimmed\" "; echo " \n"; } } } if ($ismoving) { echo " -> id\">$strmovehere \n"; } echo " |
cellheading\" class=\"$class\">"; echo stripslashes($heading); echo " |
".$icons[$key]." | "; } echo "$string | "; echo "
"; } echo " | $footer | "; echo "
cellheading\">$heading | "; echo "
cellcontent2\">"; } function print_side_block_end() { echo " |
wwwroot/$CFG->admin/\">".get_string("admin")."...";
}
print_side_block(get_string("administration"), "", $moddata, $modicon, $fulladmin, $width);
echo "wwwroot/pix/spacer.gif\" width=\"$width\" height=1>
";
}
function print_course_admin_links($course, $width=180) {
global $USER, $CFG, $THEME;
if (isguest()) {
return true;
}
if (empty($THEME->custompix)) {
$pixpath = "$CFG->wwwroot/pix";
$modpixpath = "$CFG->wwwroot/mod";
} else {
$pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
$modpixpath = "$CFG->wwwroot/theme/$CFG->theme/pix/mod";
}
if (isteacher($course->id)) {
$adminicon[]="";
if (isediting($course->id)) {
$admindata[]="id&edit=off\">".get_string("turneditingoff")."";
} else {
$admindata[]="id&edit=on\">".get_string("turneditingon")."";
}
$admindata[]="id\">".get_string("settings")."...";
$adminicon[]="
";
if (!$course->teachers) {
$course->teachers = get_string("defaultcourseteachers");
}
$admindata[]="id\">$course->teachers...";
$adminicon[]="
";
$admindata[]="id\">".get_string("grades")."...";
$adminicon[]="
";
$admindata[]="id\">".get_string("logs")."...";
$adminicon[]="
";
$admindata[]="wwwroot/files/index.php?id=$course->id\">".get_string("files")."...";
$adminicon[]="
";
$admindata[]="wwwroot/backup/backup.php?id=$course->id\">".get_string("backup")."...";
$adminicon[]="
";
//Only showed if "backupdata" dir exists
if (is_dir($CFG->dataroot."/".$course->id."/".backupdata)) {
$admindata[]="wwwroot/files/index.php?id=$course->id&wdir=/backupdata\">".get_string("restore")."...";
$adminicon[]="
";
}
$admindata[]="wwwroot/doc/view.php?id=$course->id&file=teacher.html\">".get_string("help")."...";
$adminicon[]="
";
if ($teacherforum = forum_get_course_forum($course->id, "teacher")) {
$admindata[]="wwwroot/mod/forum/view.php?f=$teacherforum->id\">".get_string("nameteacher", "forum")."";
$adminicon[]="
";
}
} else {
$admindata[]="id\">".get_string("grades")."...";
$adminicon[]="
";
}
if (!empty($admindata)) {
print_side_block(get_string("administration"), "", $admindata, $adminicon, "", $width);
}
}
function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
/// Given an empty array, this function recursively travels the
/// categories, building up a nice list for display. It also makes
/// an array that list all the parents for each category.
if ($category) {
if ($path) {
$path = "$path / $category->name";
} else {
$path = "$category->name";
}
$list[$category->id] = $path;
} else {
$category->id = 0;
}
if ($categories = get_categories("$category->id")) { // Print all the children recursively
foreach ($categories as $cat) {
if (!empty($category->id)) {
$parents[$cat->id] = $parents[$category->id];
$parents[$cat->id][] = $category->id;
}
make_categories_list($list, $parents, $cat, $path);
}
}
}
function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1) {
/// Recursive function to print out all the categories in a nice format
/// with or without courses included
if (!$displaylist) {
make_categories_list($displaylist, $parentslist);
}
if ($category) {
if ($category->visible or isadmin()) {
print_category_info($category, $depth);
} else {
return; // Don't bother printing children of invisible categories
}
} else {
$category->id = "0";
echo "
wwwroot/course/\">".get_string("fulllistofcourses")."..."; } else { $moddata = array(); $modicon = array(); $fulllist = get_string("nocoursesyet"); } } print_side_block(get_string("courses"), "", $moddata, $modicon, $fulllist, $width); } function print_courses($category, $width="100%") { /// Category is 0 (for all courses) or an object global $CFG, $THEME; if (empty($category)) { $categories = get_categories(0); // Parent = 0 ie top-level categories only if (count($categories) == 1) { $category = array_shift($categories); $courses = get_courses($category->id); } else { $courses = get_courses("all"); } unset($categories); } else { $categories = get_categories($category->id); // sub categories $courses = get_courses($category->id); } if ($categories) { print_simple_box_start("center"); print_heading(get_string("subcategories")); foreach ($categories as $category) { $linkcss = $category->visible ? "" : " class=\"dimmed\" "; echo "
wwwroot/course/index.php?category=$category->id\">$category->name
"; } print_simple_box_end(); } if ($courses) { foreach ($courses as $course) { print_course($course, $width); echo "";
echo " wwwroot/course/view.php?id=$course->id\">$course->fullname "; if ($teachers = get_course_teachers($course->id)) { echo "\n";
foreach ($teachers as $teacher) {
if ($teacher->authority > 0) {
if (!$teacher->role) {
$teacher->role = $course->teacher;
}
echo "$teacher->role: wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname ![]() ![]() | ";
echo " ".text_to_html($course->summary)." "; echo " |
wwwroot/course/\">".get_string("fulllistofcourses")."...
"; } else { if (count_records("course_categories") > 1) { print_simple_box_start("center", "100%"); print_whole_category_list(); print_simple_box_end(); } else { print_courses(0, "100%"); } } } /// MODULE FUNCTIONS ///////////////////////////////////////////////////////////////// function add_course_module($mod) { $mod->added = time(); return insert_record("course_modules", $mod); } function add_mod_to_section($mod, $beforemod=NULL) { /// Given a full mod object with section and course already defined /// If $before is specified, then this is an existing ID which we /// will insert the new module before /// /// Returns the course_sections ID where the mod is inserted if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) { $section->sequence = trim($section->sequence); if (empty($section->sequence)) { $newsequence = "$mod->coursemodule"; } else if ($beforemod) { $modarray = explode(",", $section->sequence); if ($key = array_keys ($modarray, $beforemod->id)) { $insertarray = array($mod->id, $beforemod->id); array_splice($modarray, $key[0], 1, $insertarray); $newsequence = implode(",", $modarray); } else { // Just tack it on the end anyway $newsequence = "$section->sequence,$mod->coursemodule"; } } else { $newsequence = "$section->sequence,$mod->coursemodule"; } if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) { return $section->id; // Return course_sections ID that was used. } else { return 0; } } else { // Insert a new record $section->course = $mod->course; $section->section = $mod->section; $section->summary = ""; $section->sequence = $mod->coursemodule; return insert_record("course_sections", $section); } } function hide_course_module($mod) { return set_field("course_modules", "visible", 0, "id", $mod); } function show_course_module($mod) { return set_field("course_modules", "visible", 1, "id", $mod); } function delete_course_module($mod) { return set_field("course_modules", "deleted", 1, "id", $mod); } function delete_mod_from_section($mod, $section) { if ($section = get_record("course_sections", "id", "$section") ) { $modarray = explode(",", $section->sequence); if ($key = array_keys ($modarray, $mod)) { array_splice($modarray, $key[0], 1); $newsequence = implode(",", $modarray); return set_field("course_sections", "sequence", $newsequence, "id", $section->id); } else { return false; } } return false; } function move_section($course, $section, $move) { /// Moves a whole course section up and down within the course if (!$move) { return true; } $sectiondest = $section + $move; if ($sectiondest > $course->numsections or $sectiondest < 1) { return false; } if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) { return false; } if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) { return false; } $sectionrecord->section = $sectiondest; $sectiondestrecord->section = $section; if (!update_record("course_sections", $sectionrecord)) { return false; } if (!update_record("course_sections", $sectiondestrecord)) { return false; } return true; } function moveto_module($mod, $section, $beforemod=NULL) { /// All parameters are objects /// Move the module object $mod to the specified $section /// If $beforemod exists then that is the module /// before which $modid should be inserted /// Remove original module from original section if (! delete_mod_from_section($mod->id, $mod->section)) { notify("Could not delete module from existing section"); } /// Update module itself if necessary if ($mod->section != $section->id) { $mod->section = $section->id; if (!update_record("course_modules", $mod)) { return false; } } /// Add the module into the new section $mod->course = $section->course; $mod->section = $section->section; // need relative reference $mod->coursemodule = $mod->id; if (! add_mod_to_section($mod, $beforemod)) { return false; } return true; } function move_module($cm, $move) { /// Moves an activity module up and down within the course if (!$move) { return true; } if (! $thissection = get_record("course_sections", "id", $cm->section)) { error("This course section doesn't exist"); } $mods = explode(",", $thissection->sequence); $len = count($mods); $pos = array_keys($mods, $cm->id); $thepos = $pos[0]; if ($len == 0 || count($pos) == 0 ) { error("Very strange. Could not find the required module in this section."); } if ($len == 1) { $first = true; $last = true; } else { $first = ($thepos == 0); $last = ($thepos == $len - 1); } if ($move < 0) { // Moving the module up if ($first) { if ($thissection->section == 0) { // First section, do nothing return true; } else { // Push onto end of previous section $prevsectionnumber = $thissection->section - 1; if (! $prevsection = get_record("course_sections", "course", "$thissection->course", "section", "$prevsectionnumber")) { error("Previous section ($prevsection->id) doesn't exist"); } if (!empty($prevsection->sequence)) { $newsequence = "$prevsection->sequence,$cm->id"; } else { $newsequence = "$cm->id"; } if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) { error("Previous section could not be updated"); } if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) { error("Module could not be updated"); } array_splice($mods, 0, 1); $newsequence = implode(",", $mods); if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) { error("Module could not be updated"); } return true; } } else { // move up within this section $swap = $mods[$thepos-1]; $mods[$thepos-1] = $mods[$thepos]; $mods[$thepos] = $swap; $newsequence = implode(",", $mods); if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) { error("This section could not be updated"); } return true; } } else { // Moving the module down if ($last) { $nextsectionnumber = $thissection->section + 1; if ($nextsection = get_record("course_sections", "course", "$thissection->course", "section", "$nextsectionnumber")) { if (!empty($nextsection->sequence)) { $newsequence = "$cm->id,$nextsection->sequence"; } else { $newsequence = "$cm->id"; } if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) { error("Next section could not be updated"); } if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) { error("Module could not be updated"); } array_splice($mods, $thepos, 1); $newsequence = implode(",", $mods); if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) { error("This section could not be updated"); } return true; } else { // There is no next section, so just return return true; } } else { // move down within this section $swap = $mods[$thepos+1]; $mods[$thepos+1] = $mods[$thepos]; $mods[$thepos] = $swap; $newsequence = implode(",", $mods); if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) { error("This section could not be updated"); } return true; } } } function make_editing_buttons($moduleid, $absolute=false, $visible=true, $moveselect=true) { global $CFG, $THEME; static $str = ''; if (empty($str)) { $str->delete = get_string("delete"); $str->move = get_string("move"); $str->moveup = get_string("moveup"); $str->movedown = get_string("movedown"); $str->update = get_string("update"); $str->hide = get_string("hide"); $str->show = get_string("show"); } if ($absolute) { $path = "$CFG->wwwroot/course"; } else { $path = "."; } if (empty($THEME->custompix)) { $pixpath = "$path/../pix"; } else { $pixpath = "$path/../theme/$CFG->theme/pix"; } if ($visible) { $hideshow = "hide\" href=\"$path/mod.php?hide=$moduleid\">