id, '', '', 'u.id, u.firstname, u.lastname')) { foreach ($courseusers as $courseuser) { $users[$courseuser->id] = fullname($courseuser, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id))); } } if ($guest = get_guest()) { $users[$guest->id] = fullname($guest); } if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) { 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); } $activities = array(); $selectedactivity = $modid; /// Casting $course->modinfo to string prevents one notice when the field is null if ($modinfo = unserialize((string)$course->modinfo)) { $section = 0; if ($course->format == 'weeks') { // Body $strsection = get_string("week"); } else { $strsection = get_string("topic"); } $activities["activity/All"] = "All activities"; $activities["activity/Assignments"] = "All assignments"; $activities["activity/Chats"] = "All chats"; $activities["activity/Forums"] = "All forums"; $activities["activity/Quizzes"] = "All quizzes"; $activities["activity/Workshops"] = "All workshops"; $activities["section/individual"] = "------------- Individual Activities --------------"; foreach ($modinfo as $mod) { if ($mod->mod == "label") { continue; } if (!$mod->visible and !has_capability('moodle/course:viewhiddenactivities',get_context_instance(CONTEXT_MODULE, $mod->cm))) { continue; } $mod->id = $mod->cm; if (!groups_course_module_visible($mod)) { continue; } if ($mod->section > 0 and $section <> $mod->section) { $activities["section/$mod->section"] = "-------------- $strsection $mod->section --------------"; } $section = $mod->section; $mod->name = strip_tags(format_string(urldecode($mod->name),true)); if (strlen($mod->name) > 55) { $mod->name = substr($mod->name, 0, 50)."..."; } if (!$mod->visible) { $mod->name = "(".$mod->name.")"; } $activities["$mod->cm"] = $mod->name; if ($mod->cm == $modid) { $selectedactivity = "$mod->cm"; } } } $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); $dates = array(); $dates["$USER->lastlogin"] = get_string("lastlogin").", ".userdate($USER->lastlogin, $strftimedate); $dates["$timemidnight"] = get_string("today").", ".userdate($timenow, $strftimedate); if (!$course->startdate or ($course->startdate > $timenow)) { $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 === "lastlogin") { $selecteddate = $USER->lastlogin; } echo '
'; echo ''; echo "
"; echo ""; if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) { echo ""; } else { echo ''; } $sortfields = array("default" => get_string("bycourseorder"),"dateasc" => get_string("datemostrecentlast"), "datedesc" => get_string("datemostrecentfirst")); echo ""; echo '"; echo ""; echo '"; echo ''; $groupmode = groups_get_course_groupmode($course); if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id)))) { if ($groups = groups_get_all_groups($course->id)) { $group_names = array(); foreach($groups as $group) { $group_names[$group->id] = format_string($group->name); } echo ''; } } echo '"; echo "
" . get_string("courses") . ""; choose_from_menu ($courses, "id", $course->id, ""); echo "
" . get_string("participants") . ""; choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") ); echo "' . get_string("since") . ''; choose_from_menu ($dates, "date", $selecteddate, get_string("alldays")); echo "
" . get_string("activities") . ""; choose_from_menu ($activities, "modid", $selectedactivity, ""); echo "' . get_string("sortby") . ""; choose_from_menu ($sortfields, "sortby", $selectedsort, ""); echo "
'; if ($groupmode == VISIBLEGROUPS) { print_string('groupsvisible'); } else { print_string('groupsseparate'); } echo ':'; choose_from_menu($group_names, "selectedgroup", $selectedgroup, get_string("allgroups"), "", ""); echo ''; echo ''; echo "
"; $advancedlink = "wwwroot/course/recent.php?id=$course->id&advancedfilter=0\">" . get_string("normalfilter") . ""; print_heading($advancedlink); echo "
"; echo "
"; } else { $day_list = array("1","7","14","21","30"); $strsince = get_string("since"); $strlastlogin = get_string("lastlogin"); $strday = get_string("day"); $strdays = get_string("days"); $heading = ""; foreach ($day_list as $count) { if ($count == "1") { $day = $strday; } else { $day = $strdays; } $tmpdate = time() - ($count * 3600 * 24); $heading = $heading . "wwwroot/course/recent.php?id=$course->id&date=$tmpdate\"> $count $day | "; } $heading = $strsince . ": wwwroot/course/recent.php?id=$course->id\">$strlastlogin" . " | " . $heading; print_heading($heading); $advancedlink = "wwwroot/course/recent.php?id=$course->id&advancedfilter=1\">" . get_string("advancedfilter") . ""; print_heading($advancedlink); } } function make_log_url($module, $url) { switch ($module) { case 'user': case 'course': case 'file': case 'login': case 'lib': case 'admin': case 'message': case 'calendar': case 'mnet course': return "/course/$url"; break; case 'blog': return "/$module/$url"; break; case 'upload': return $url; break; case 'library': case '': return '/'; break; default: return "/mod/$module/$url"; break; } } function build_mnet_logs_array($hostid, $course, $user=0, $date=0, $order="l.time ASC", $limitfrom='', $limitnum='', $modname="", $modid=0, $modaction="", $groupid=0) { global $CFG; // It is assumed that $date is the GMT time of midnight for that day, // and so the next 86400 seconds worth of logs are printed. /// Setup for group handling. // TODO: I don't understand group/context/etc. enough to be able to do // something interesting with it here // What is the context of a remote course? /// If the group mode is separate, and this user does not have editing privileges, /// then only the user's group can be viewed. //if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) { // $groupid = get_current_group($course->id); //} /// If this course doesn't have groups, no groupid can be specified. //else if (!$course->groupmode) { // $groupid = 0; //} $groupid = 0; $joins = array(); $qry = " SELECT l.*, u.firstname, u.lastname, u.picture FROM {$CFG->prefix}mnet_log l LEFT JOIN {$CFG->prefix}user u ON l.userid = u.id WHERE "; $where .= "l.hostid = '$hostid'"; // TODO: Is 1 really a magic number referring to the sitename? if ($course != 1 || $modid != 0) { $where .= " AND\n l.course='$course'"; } if ($modname) { $where .= " AND\n l.module = '$modname'"; } if ('site_errors' === $modid) { $where .= " AND\n ( l.action='error' OR l.action='infected' )"; } else if ($modid) { //TODO: This assumes that modids are the same across sites... probably //not true $where .= " AND\n l.cmid = '$modid'"; } if ($modaction) { $firstletter = substr($modaction, 0, 1); if (ctype_alpha($firstletter)) { $where .= " AND\n lower(l.action) LIKE '%" . strtolower($modaction) . "%'"; } else if ($firstletter == '-') { $where .= " AND\n lower(l.action) NOT LIKE '%" . strtolower(substr($modaction, 1)) . "%'"; } } if ($user) { $where .= " AND\n l.userid = '$user'"; } if ($date) { $enddate = $date + 86400; $where .= " AND\n l.time > '$date' AND l.time < '$enddate'"; } $result = array(); $result['totalcount'] = count_records_sql("SELECT COUNT(*) FROM {$CFG->prefix}mnet_log l WHERE $where"); if(!empty($result['totalcount'])) { $where .= "\n ORDER BY\n $order"; $result['logs'] = get_records_sql($qry.$where, $limitfrom, $limitnum); } else { $result['logs'] = array(); } return $result; } function build_logs_array($course, $user=0, $date=0, $order="l.time ASC", $limitfrom='', $limitnum='', $modname="", $modid=0, $modaction="", $groupid=0) { // It is assumed that $date is the GMT time of midnight for that day, // and so the next 86400 seconds worth of logs are printed. /// Setup for group handling. /// If the group mode is separate, and this user does not have editing privileges, /// then only the user's group can be viewed. if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) { $groupid = get_current_group($course->id); } /// If this course doesn't have groups, no groupid can be specified. else if (!$course->groupmode) { $groupid = 0; } $joins = array(); if ($course->id != SITEID || $modid != 0) { $joins[] = "l.course='$course->id'"; } if ($modname) { $joins[] = "l.module = '$modname'"; } if ('site_errors' === $modid) { $joins[] = "( l.action='error' OR l.action='infected' )"; } else if ($modid) { $joins[] = "l.cmid = '$modid'"; } if ($modaction) { $firstletter = substr($modaction, 0, 1); if (ctype_alpha($firstletter)) { $joins[] = "lower(l.action) LIKE '%" . strtolower($modaction) . "%'"; } else if ($firstletter == '-') { $joins[] = "lower(l.action) NOT LIKE '%" . strtolower(substr($modaction, 1)) . "%'"; } } /// Getting all members of a group. if ($groupid and !$user) { if ($gusers = groups_get_members($groupid)) { $gusers = array_keys($gusers); $joins[] = 'l.userid IN (' . implode(',', $gusers) . ')'; } else { $joins[] = 'l.userid = 0'; // No users in groups, so we want something that will always be false. } } else if ($user) { $joins[] = "l.userid = '$user'"; } if ($date) { $enddate = $date + 86400; $joins[] = "l.time > '$date' AND l.time < '$enddate'"; } $selector = implode(' AND ', $joins); $totalcount = 0; // Initialise $result = array(); $result['logs'] = get_logs($selector, $order, $limitfrom, $limitnum, $totalcount); $result['totalcount'] = $totalcount; return $result; } function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100, $url="", $modname="", $modid=0, $modaction="", $groupid=0) { global $CFG; if (!$logs = build_logs_array($course, $user, $date, $order, $page*$perpage, $perpage, $modname, $modid, $modaction, $groupid)) { notify("No logs found!"); print_footer($course); exit; } $courses = array(); if ($course->id == SITEID) { $courses[0] = ''; if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) { foreach ($ccc as $cc) { $courses[$cc->id] = $cc->shortname; } } } else { $courses[$course->id] = $course->shortname; } $totalcount = $logs['totalcount']; $count=0; $ldcache = array(); $tt = getdate(time()); $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]); $strftimedatetime = get_string("strftimedatetime"); echo "
\n"; print_string("displayingrecords", "", $totalcount); echo "
\n"; print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&"); echo ''."\n"; // echo "
\n"; echo ""; if ($course->id == SITEID) { echo "\n"; } echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; // Make sure that the logs array is an array, even it is empty, to avoid warnings from the foreach. if (empty($logs['logs'])) { $logs['logs'] = array(); } $row = 1; foreach ($logs['logs'] as $log) { $row = ($row + 1) % 2; if (isset($ldcache[$log->module][$log->action])) { $ld = $ldcache[$log->module][$log->action]; } else { $ld = get_record('log_display', 'module', $log->module, 'action', $log->action); $ldcache[$log->module][$log->action] = $ld; } if ($ld && is_numeric($log->info)) { // ugly hack to make sure fullname is shown correctly if (($ld->mtable == 'user') and ($ld->field == sql_concat('firstname', "' '" , 'lastname'))) { $log->info = fullname(get_record($ld->mtable, 'id', $log->info), true); } else { $log->info = get_field($ld->mtable, $ld->field, 'id', $log->info); } } //Filter log->info $log->info = format_string($log->info); $log->url = strip_tags(urldecode($log->url)); // Some XSS protection $log->info = strip_tags(urldecode($log->info)); // Some XSS protection $log->url = s($log->url); /// XSS protection and XHTML compatibility - should be in link_to_popup_window() instead!! echo ''; if ($course->id == SITEID) { echo "\n"; } echo "\n"; echo "\n"; $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id))); echo "\n"; echo "\n";; echo "\n"; echo "\n"; } echo "
".get_string('course')."".get_string('time')."".get_string('ip_address')."".get_string('fullname')."".get_string('action')."".get_string('info')."
\n"; if (empty($log->course)) { echo get_string('site') . "\n"; } else { echo " wwwroot}/course/view.php?id={$log->course}\">". format_string($courses[$log->course])."\n"; } echo "".userdate($log->time, '%a'). ' '.userdate($log->time, $strftimedatetime)."\n"; link_to_popup_window("/iplookup/index.php?ip=$log->ip&user=$log->userid", 'iplookup',$log->ip, 440, 700); echo "\n"; echo " wwwroot/user/view.php?id={$log->userid}&course={$log->course}\">$fullname\n"; echo "\n"; link_to_popup_window( make_log_url($log->module,$log->url), 'fromloglive',"$log->module $log->action", 440, 700); echo "{$log->info}
\n"; print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&"); } function print_mnet_log($hostid, $course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100, $url="", $modname="", $modid=0, $modaction="", $groupid=0) { global $CFG; if (!$logs = build_mnet_logs_array($hostid, $course, $user, $date, $order, $page*$perpage, $perpage, $modname, $modid, $modaction, $groupid)) { notify("No logs found!"); print_footer($course); exit; } if ($course->id == SITEID) { $courses[0] = ''; if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname,c.visible')) { foreach ($ccc as $cc) { $courses[$cc->id] = $cc->shortname; } } } $totalcount = $logs['totalcount']; $count=0; $ldcache = array(); $tt = getdate(time()); $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]); $strftimedatetime = get_string("strftimedatetime"); echo "
\n"; print_string("displayingrecords", "", $totalcount); echo "
\n"; print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&"); echo "\n"; echo ""; if ($course->id == SITEID) { echo "\n"; } echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; if (empty($logs['logs'])) { echo "
".get_string('course')."".get_string('time')."".get_string('ip_address')."".get_string('fullname')."".get_string('action')."".get_string('info')."
\n"; return; } $row = 1; foreach ($logs['logs'] as $log) { $log->info = $log->coursename; $row = ($row + 1) % 2; if (isset($ldcache[$log->module][$log->action])) { $ld = $ldcache[$log->module][$log->action]; } else { $ld = get_record('log_display', 'module', $log->module, 'action', $log->action); $ldcache[$log->module][$log->action] = $ld; } if (0 && $ld && !empty($log->info)) { // ugly hack to make sure fullname is shown correctly if (($ld->mtable == 'user') and ($ld->field == sql_concat('firstname', "' '" , 'lastname'))) { $log->info = fullname(get_record($ld->mtable, 'id', $log->info), true); } else { $log->info = get_field($ld->mtable, $ld->field, 'id', $log->info); } } //Filter log->info $log->info = format_string($log->info); $log->url = strip_tags(urldecode($log->url)); // Some XSS protection $log->info = strip_tags(urldecode($log->info)); // Some XSS protection $log->url = str_replace('&', '&', $log->url); /// XHTML compatibility echo ''; if ($course->id == SITEID) { echo "\n"; echo " wwwroot}/course/view.php?id={$log->course}\">".$courses[$log->course]."\n"; echo "\n"; } echo "".userdate($log->time, '%a'). ' '.userdate($log->time, $strftimedatetime)."\n"; echo "\n"; link_to_popup_window("/iplookup/index.php?ip=$log->ip&user=$log->userid", 'iplookup',$log->ip, 400, 700); echo "\n"; $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id))); echo "\n"; echo " wwwroot/user/view.php?id={$log->userid}\">$fullname\n"; echo "\n"; echo "\n"; echo $log->action .': '.$log->module; echo "\n";; echo "{$log->info}\n"; echo "\n"; } echo "\n"; print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&"); } function print_log_csv($course, $user, $date, $order='l.time DESC', $modname, $modid, $modaction, $groupid) { $text = get_string('course')."\t".get_string('time')."\t".get_string('ip_address')."\t". get_string('fullname')."\t".get_string('action')."\t".get_string('info'); if (!$logs = build_logs_array($course, $user, $date, $order, '', '', $modname, $modid, $modaction, $groupid)) { return false; } $courses = array(); if ($course->id == SITEID) { $courses[0] = ''; if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) { foreach ($ccc as $cc) { $courses[$cc->id] = $cc->shortname; } } } else { $courses[$course->id] = $course->shortname; } $count=0; $ldcache = array(); $tt = getdate(time()); $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]); $strftimedatetime = get_string("strftimedatetime"); $filename = 'logs_'.userdate(time(),get_string('backupnameformat'),99,false); $filename .= '.txt'; header("Content-Type: application/download\n"); header("Content-Disposition: attachment; filename=$filename"); header("Expires: 0"); header("Cache-Control: must-revalidate,post-check=0,pre-check=0"); header("Pragma: public"); echo get_string('savedat').userdate(time(), $strftimedatetime)."\n"; echo $text; if (empty($logs['logs'])) { return true; } foreach ($logs['logs'] as $log) { if (isset($ldcache[$log->module][$log->action])) { $ld = $ldcache[$log->module][$log->action]; } else { $ld = get_record('log_display', 'module', $log->module, 'action', $log->action); $ldcache[$log->module][$log->action] = $ld; } if ($ld && !empty($log->info)) { // ugly hack to make sure fullname is shown correctly if (($ld->mtable == 'user') and ($ld->field == sql_concat('firstname', "' '" , 'lastname'))) { $log->info = fullname(get_record($ld->mtable, 'id', $log->info), true); } else { $log->info = get_field($ld->mtable, $ld->field, 'id', $log->info); } } //Filter log->info $log->info = format_string($log->info); $log->url = strip_tags(urldecode($log->url)); // Some XSS protection $log->info = strip_tags(urldecode($log->info)); // Some XSS protection $log->url = str_replace('&', '&', $log->url); // XHTML compatibility $firstField = $courses[$log->course]; $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id))); $row = array($firstField, userdate($log->time, $strftimedatetime), $log->ip, $fullname, $log->module.' '.$log->action, $log->info); $text = implode("\t", $row); echo $text." \n"; } return true; } function print_log_xls($course, $user, $date, $order='l.time DESC', $modname, $modid, $modaction, $groupid) { global $CFG; require_once("$CFG->libdir/excellib.class.php"); if (!$logs = build_logs_array($course, $user, $date, $order, '', '', $modname, $modid, $modaction, $groupid)) { return false; } $courses = array(); if ($course->id == SITEID) { $courses[0] = ''; if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) { foreach ($ccc as $cc) { $courses[$cc->id] = $cc->shortname; } } } else { $courses[$course->id] = $course->shortname; } $count=0; $ldcache = array(); $tt = getdate(time()); $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]); $strftimedatetime = get_string("strftimedatetime"); $nroPages = ceil(count($logs)/(EXCELROWS-FIRSTUSEDEXCELROW+1)); $filename = 'logs_'.userdate(time(),get_string('backupnameformat'),99,false); $filename .= '.xls'; $workbook = new MoodleExcelWorkbook('-'); $workbook->send($filename); $worksheet = array(); $headers = array(get_string('course'), get_string('time'), get_string('ip_address'), get_string('fullname'), get_string('action'), get_string('info')); // Creating worksheets for ($wsnumber = 1; $wsnumber <= $nroPages; $wsnumber++) { $sheettitle = get_string('excel_sheettitle', 'logs', $wsnumber).$nroPages; $worksheet[$wsnumber] =& $workbook->add_worksheet($sheettitle); $worksheet[$wsnumber]->set_column(1, 1, 30); $worksheet[$wsnumber]->write_string(0, 0, get_string('savedat'). userdate(time(), $strftimedatetime)); $col = 0; foreach ($headers as $item) { $worksheet[$wsnumber]->write(FIRSTUSEDEXCELROW-1,$col,$item,''); $col++; } } if (empty($logs['logs'])) { $workbook->close(); return true; } $formatDate =& $workbook->add_format(); $formatDate->set_num_format(get_string('log_excel_date_format')); $row = FIRSTUSEDEXCELROW; $wsnumber = 1; $myxls =& $worksheet[$wsnumber]; foreach ($logs['logs'] as $log) { if (isset($ldcache[$log->module][$log->action])) { $ld = $ldcache[$log->module][$log->action]; } else { $ld = get_record('log_display', 'module', $log->module, 'action', $log->action); $ldcache[$log->module][$log->action] = $ld; } if ($ld && !empty($log->info)) { // ugly hack to make sure fullname is shown correctly if (($ld->mtable == 'user') and ($ld->field == sql_concat('firstname', "' '" , 'lastname'))) { $log->info = fullname(get_record($ld->mtable, 'id', $log->info), true); } else { $log->info = get_field($ld->mtable, $ld->field, 'id', $log->info); } } // Filter log->info $log->info = format_string($log->info); $log->info = strip_tags(urldecode($log->info)); // Some XSS protection if ($nroPages>1) { if ($row > EXCELROWS) { $wsnumber++; $myxls =& $worksheet[$wsnumber]; $row = FIRSTUSEDEXCELROW; } } $myxls->write($row, 0, $courses[$log->course], ''); // Excel counts from 1/1/1900 $excelTime=25569+$log->time/(3600*24); $myxls->write($row, 1, $excelTime, $formatDate); $myxls->write($row, 2, $log->ip, ''); $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id))); $myxls->write($row, 3, $fullname, ''); $myxls->write($row, 4, $log->module.' '.$log->action, ''); $myxls->write($row, 5, $log->info, ''); $row++; } $workbook->close(); return true; } function print_log_ods($course, $user, $date, $order='l.time DESC', $modname, $modid, $modaction, $groupid) { global $CFG; require_once("$CFG->libdir/odslib.class.php"); if (!$logs = build_logs_array($course, $user, $date, $order, '', '', $modname, $modid, $modaction, $groupid)) { return false; } $courses = array(); if ($course->id == SITEID) { $courses[0] = ''; if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) { foreach ($ccc as $cc) { $courses[$cc->id] = $cc->shortname; } } } else { $courses[$course->id] = $course->shortname; } $count=0; $ldcache = array(); $tt = getdate(time()); $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]); $strftimedatetime = get_string("strftimedatetime"); $nroPages = ceil(count($logs)/(EXCELROWS-FIRSTUSEDEXCELROW+1)); $filename = 'logs_'.userdate(time(),get_string('backupnameformat'),99,false); $filename .= '.ods'; $workbook = new MoodleODSWorkbook('-'); $workbook->send($filename); $worksheet = array(); $headers = array(get_string('course'), get_string('time'), get_string('ip_address'), get_string('fullname'), get_string('action'), get_string('info')); // Creating worksheets for ($wsnumber = 1; $wsnumber <= $nroPages; $wsnumber++) { $sheettitle = get_string('excel_sheettitle', 'logs', $wsnumber).$nroPages; $worksheet[$wsnumber] =& $workbook->add_worksheet($sheettitle); $worksheet[$wsnumber]->set_column(1, 1, 30); $worksheet[$wsnumber]->write_string(0, 0, get_string('savedat'). userdate(time(), $strftimedatetime)); $col = 0; foreach ($headers as $item) { $worksheet[$wsnumber]->write(FIRSTUSEDEXCELROW-1,$col,$item,''); $col++; } } if (empty($logs['logs'])) { $workbook->close(); return true; } $formatDate =& $workbook->add_format(); $formatDate->set_num_format(get_string('log_excel_date_format')); $row = FIRSTUSEDEXCELROW; $wsnumber = 1; $myxls =& $worksheet[$wsnumber]; foreach ($logs['logs'] as $log) { if (isset($ldcache[$log->module][$log->action])) { $ld = $ldcache[$log->module][$log->action]; } else { $ld = get_record('log_display', 'module', $log->module, 'action', $log->action); $ldcache[$log->module][$log->action] = $ld; } if ($ld && !empty($log->info)) { // ugly hack to make sure fullname is shown correctly if (($ld->mtable == 'user') and ($ld->field == sql_concat('firstname', "' '" , 'lastname'))) { $log->info = fullname(get_record($ld->mtable, 'id', $log->info), true); } else { $log->info = get_field($ld->mtable, $ld->field, 'id', $log->info); } } // Filter log->info $log->info = format_string($log->info); $log->info = strip_tags(urldecode($log->info)); // Some XSS protection if ($nroPages>1) { if ($row > EXCELROWS) { $wsnumber++; $myxls =& $worksheet[$wsnumber]; $row = FIRSTUSEDEXCELROW; } } $myxls->write_string($row, 0, $courses[$log->course]); $myxls->write_date($row, 1, $log->time); $myxls->write_string($row, 2, $log->ip); $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id))); $myxls->write_string($row, 3, $fullname); $myxls->write_string($row, 4, $log->module.' '.$log->action); $myxls->write_string($row, 5, $log->info); $row++; } $workbook->close(); return true; } function print_log_graph($course, $userid=0, $type="course.png", $date=0) { global $CFG, $USER; if (empty($CFG->gdversion)) { echo "(".get_string("gdneed").")"; } else { // MDL-10818, do not display broken graph when user has no permission to view graph if (has_capability('moodle/site:viewreports', get_context_instance(CONTEXT_COURSE, $course->id)) || ($course->showreports and $USER->id == $userid)) { echo ''; } } } function print_overview($courses) { global $CFG, $USER; $htmlarray = array(); if ($modules = get_records('modules')) { foreach ($modules as $mod) { if (file_exists(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php')) { require_once(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php'); $fname = $mod->name.'_print_overview'; if (function_exists($fname)) { $fname($courses,$htmlarray); } } } } foreach ($courses as $course) { print_simple_box_start('center', '100%', '', 5, "coursebox"); $linkcss = ''; if (empty($course->visible)) { $linkcss = 'class="dimmed"'; } print_heading(''. format_string($course->fullname).''); if (array_key_exists($course->id,$htmlarray)) { foreach ($htmlarray[$course->id] as $modname => $html) { echo $html; } } print_simple_box_end(); } } function print_recent_activity($course) { // $course is an object // This function trawls through the logs looking for // anything new since the user's last login global $CFG, $USER, $SESSION; $context = get_context_instance(CONTEXT_COURSE, $course->id); $timestart = time() - COURSE_MAX_RECENT_PERIOD; if (!has_capability('moodle/legacy:guest', $context, NULL, false)) { if (!empty($USER->lastcourseaccess[$course->id])) { if ($USER->lastcourseaccess[$course->id] > $timestart) { $timestart = $USER->lastcourseaccess[$course->id]; } } } echo '
'; echo get_string('activitysince', '', userdate($timestart)); echo '
'; echo '
'; echo ''.get_string('recentactivityreport').''; echo "
\n"; // Firstly, have there been any new enrolments? $heading = false; $content = false; $users = get_recent_enrolments($course->id, $timestart); //Accessibility: new users now appear in an
    list. if ($users) { echo '
    '; if (! $heading) { print_headline(get_string("newusers").':', 3); $heading = true; $content = true; } echo "
      \n"; foreach ($users as $user) { $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id))); echo '
    1. id&course=$course->id\">$fullname
    2. \n"; } echo "
    \n
    \n"; } // 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); if ($info[0] == 'label') { // Labels are special activities continue; } $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:
    wwwroot/course/$log->url\">".format_string($modname,true).""); break; case 'update mod': $strupdated = get_string('updated', 'moodle', get_string('modulename', $info[0])); if (empty($changelist[$log->info])) { $changelist[$log->info] = array ('operation' => 'update', 'text' => "$strupdated:
    wwwroot/course/$log->url\">".format_string($modname,true).""); } break; case 'delete mod': if (!empty($changelist[$log->info]['operation']) and $changelist[$log->info]['operation'] == 'add') { $changelist[$log->info] = NULL; } else { $strdeleted = get_string('deletedactivity', 'moodle', get_string('modulename', $info[0])); $changelist[$log->info] = array ('operation' => 'delete', 'text' => $strdeleted); } break; } } } } if (!empty($changelist)) { foreach ($changelist as $changeinfo => $change) { if ($change) { $changes[$changeinfo] = $change; } } if (isset($changes)){ if (count($changes) > 0) { print_headline(get_string('courseupdates').':', 3); $content = true; foreach ($changes as $changeinfo => $change) { echo '

    '.$change['text'].'

    '; } } } } // Now display new things from each module $mods = get_records('modules', 'visible', '1', 'name', 'id, name'); $viewfullnames = has_capability('moodle/site:viewfullnames', $context); foreach ($mods as $mod) { // Each module gets it's own logs and prints them if (file_exists($CFG->dirroot.'/mod/'.$mod->name.'/lib.php')) { include_once($CFG->dirroot.'/mod/'.$mod->name.'/lib.php'); $print_recent_activity = $mod->name.'_print_recent_activity'; if (function_exists($print_recent_activity)) { // // NOTE: // $isteacher (second parameter below) is to be deprecated! // // TODO: // 1) Make sure that all _print_recent_activity functions are // not using the $isteacher value. // 2) Eventually, remove the $isteacher parameter from the // function calls. // $modcontent = $print_recent_activity($course, $viewfullnames, $timestart); if ($modcontent) { $content = true; } } } else { /// Really need to indicate an error here to admins. Is there a way to do this? } } 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 // groupingid - grouping id // groupmembersonly - is this instance visible to group members only // extra - contains extra string to include in any link global $CFG; $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]->groupingid = $rawmods[$seq]->groupingid; $mod[$seq]->groupmembersonly = $rawmods[$seq]->groupmembersonly; $mod[$seq]->extra = ""; $modname = $mod[$seq]->mod; $functionname = $modname."_get_coursemodule_info"; include_once("$CFG->dirroot/mod/$modname/lib.php"); if (function_exists($functionname)) { if ($info = $functionname($rawmods[$seq])) { if (!empty($info->extra)) { $mod[$seq]->extra = $info->extra; } if (!empty($info->icon)) { $mod[$seq]->icon = $info->icon; } } } } } } } 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 (except resource!) $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, SORT_LOCALE_STRING); } else { error("No modules are installed!"); } if ($rawmods = get_course_mods($courseid)) { foreach($rawmods as $mod) { // Index the mods if (empty($modnames[$mod->modname])) { continue; } // Check groupings if (!groups_course_module_visible($mod)) { continue; } $mods[$mod->id] = $mod; $mods[$mod->id]->modfullname = $modnames[$mod->modname]; if ($mod->visible or has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE, $courseid))) { $modnamesused[$mod->modname] = $modnames[$mod->modname]; } } if ($modnamesused) { asort($modnamesused, SORT_LOCALE_STRING); } } } 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 ($display == "all" or empty($display)) { $display = 0; } if (empty($USER->id) or $USER->username == 'guest') { //do not store settings in db for guests } else 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_coursemodule_visible($moduleid, $visibility, true); } } rebuild_course_cache($courseid); } } function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") { /// Prints a section full of activity modules global $CFG, $USER; static $groupbuttons; static $groupbuttonslink; static $isteacher; static $isediting; static $ismoving; static $strmovehere; static $strmovefull; static $strunreadpostsone; static $untracked; static $usetracking; $labelformatoptions = New stdClass; if (!isset($isteacher)) { $groupbuttons = ($course->groupmode or (!$course->groupmodeforce)); $groupbuttonslink = (!$course->groupmodeforce); $isediting = isediting($course->id); $ismoving = $isediting && ismoving($course->id); if ($ismoving) { $strmovehere = get_string("movehere"); $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'")); } include_once($CFG->dirroot.'/mod/forum/lib.php'); if ($usetracking = forum_tp_can_track_forums()) { $strunreadpostsone = get_string('unreadpostsone', 'forum'); $untracked = forum_tp_get_untracked_forums($USER->id, $course->id); } } $labelformatoptions->noclean = true; /// Casting $course->modinfo to string prevents one notice when the field is null $modinfo = unserialize((string)$course->modinfo); $groupings = groups_get_all_groupings($course->id); //Acccessibility: replace table with list