From c215b32be74d6386aa09738ea02473a1bfdb283b Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Thu, 4 Jan 2007 03:22:51 +0000 Subject: [PATCH] mnet: courses and activity log know about mnet remote users --- course/lib.php | 197 +++++++++++++++++++++++++ course/report/log/index.php | 41 ++++-- course/report/log/lib.php | 284 ++++++++++++++++++++++++++++++++++++ 3 files changed, 510 insertions(+), 12 deletions(-) diff --git a/course/lib.php b/course/lib.php index eb3b708afc..d488074827 100644 --- a/course/lib.php +++ b/course/lib.php @@ -260,6 +260,96 @@ function make_log_url($module, $url) { } +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) { @@ -453,6 +543,113 @@ function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $per } +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) { diff --git a/course/report/log/index.php b/course/report/log/index.php index 1ed62287e7..2927912df7 100644 --- a/course/report/log/index.php +++ b/course/report/log/index.php @@ -6,7 +6,18 @@ require_once('lib.php'); require_once($CFG->libdir.'/adminlib.php'); - $id = required_param('id', PARAM_INT);// Course ID + $id = optional_param('id', PARAM_INT);// Course ID + + $host_course = optional_param('host_course', PARAM_PATH);// Course ID + + list($hostid, $id) = explode('/', $host_course); + + $course_stub = get_record('mnet_log', 'hostid', $hostid, 'course', $id); + $course->id = $id; + $course->shortname = $course_stub->coursename; + $course->fullname = $course_stub->coursename; + + $group = optional_param('group', -1, PARAM_INT); // Group to display $user = optional_param('user', 0, PARAM_INT); // User to display $date = optional_param('date', 0, PARAM_FILE); // Date to display - number or some string @@ -22,10 +33,12 @@ require_login(); - if (! $course = get_record('course', 'id', $id) ) { - error('That\'s an invalid course id'); + if ($hostid == $CFG->mnet_localhost_id) { + if (!$course = get_record('course', 'id', $id) ) { + error('That\'s an invalid course id'.$id); + } } - + $context = get_context_instance(CONTEXT_COURSE, $course->id); require_capability('moodle/site:viewreports', $context); @@ -51,10 +64,10 @@ if ($date) { $dateinfo = userdate($date, get_string('strftimedaydate')); } - + switch ($logformat) { case 'showashtml': - if ($course->id == SITEID) { + if ($hostid != $CFG->mnet_localhost_id || $course->id == SITEID) { $adminroot = admin_get_root(); admin_externalpage_setup('reportlog', $adminroot); admin_externalpage_print_header($adminroot); @@ -67,11 +80,15 @@ } print_heading("$course->fullname: $userinfo, $dateinfo (".usertimezone().")"); - print_log_selector_form($course, $user, $date, $modname, $modid, $modaction, $group, $showcourses, $showusers, $logformat); + print_mnet_log_selector_form($hostid, $course, $user, $date, $modname, $modid, $modaction, $group, $showcourses, $showusers, $logformat); - print_log($course, $user, $date, 'l.time DESC', $page, $perpage, - "index.php?id=$course->id&chooselog=1&user=$user&date=$date&modid=$modid&modaction=$modaction&group=$group", - $modname, $modid, $modaction, $group); + if($hostid == $CFG->mnet_localhost_id) { + print_log($course, $user, $date, 'l.time DESC', $page, $perpage, + "index.php?id=$course->id&chooselog=1&user=$user&date=$date&modid=$modid&modaction=$modaction&group=$group", + $modname, $modid, $modaction, $group); + } else { + print_mnet_log($hostid, $id, $user, $date, 'l.time DESC', $page, $perpage, "", $modname, $modid, $modaction, $group); + } break; case 'downloadascsv': if (!print_log_csv($course, $user, $date, 'l.time DESC', $modname, @@ -98,7 +115,7 @@ } else { - if ($course->id == SITEID) { + if ($hostid != $CFG->mnet_localhost_id || $course->id == SITEID) { $adminroot = admin_get_root(); admin_externalpage_setup('reportlog', $adminroot); admin_externalpage_print_header($adminroot); @@ -122,7 +139,7 @@ } - if ($course->id == SITEID) { + if ($hostid != $CFG->mnet_localhost_id || $course->id == SITEID) { admin_externalpage_print_footer($adminroot); } else { print_footer($course); diff --git a/course/report/log/lib.php b/course/report/log/lib.php index e3a61efb99..cbe0001b91 100644 --- a/course/report/log/lib.php +++ b/course/report/log/lib.php @@ -1,5 +1,289 @@ dirroot.'/mnet/peer.php'; + + $mnet_peer = new mnet_peer(); + $mnet_peer->set_id($hostid); + + $sql = "select distinct course, hostid, coursename from {$CFG->prefix}mnet_log"; + $courses = get_records_sql($sql); + $remotecoursecount = count($courses); + + // first check to see if we can override showcourses and showusers + $numcourses = $remotecoursecount + count_records_select("course", "", "COUNT(id)"); + if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$showcourses) { + $showcourses = 1; + } + + $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); + + // Context for remote data is always SITE + // Groups for remote data are always OFF + if ($hostid == $CFG->mnet_localhost_id) { + $context = get_context_instance(CONTEXT_COURSE, $course->id); + + /// Setup for group handling. + if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) { + $selectedgroup = get_current_group($course->id); + $showgroups = false; + } + else if ($course->groupmode) { + $selectedgroup = ($selectedgroup == -1) ? get_current_group($course->id) : $selectedgroup; + $showgroups = true; + } + else { + $selectedgroup = 0; + $showgroups = false; + } + + } else { + $context = $sitecontext; + } + + // Get all the possible users + $users = array(); + + // If looking at a different host, we're interested in all our site users + if ($hostid == $CFG->mnet_localhost_id && $course->category) { + if ($selectedgroup) { // If using a group, only get users in that group. + $courseusers = get_group_users($selectedgroup, 'u.lastname ASC', '', 'u.id, u.firstname, u.lastname, u.idnumber'); + } else { + $courseusers = get_course_users($course->id, '', '', 'u.id, u.firstname, u.lastname, u.idnumber'); + } + } else { + $courseusers = get_site_users("u.lastaccess DESC", "u.id, u.firstname, u.lastname, u.idnumber"); + } + + if (count($courseusers) < COURSE_MAX_USERS_PER_DROPDOWN && !$showusers) { + $showusers = 1; + } + + if ($showusers) { + if ($courseusers) { + foreach ($courseusers as $courseuser) { + $users[$courseuser->id] = fullname($courseuser, has_capability('moodle/site:viewfullnames', $context)); + } + } + if ($guest = get_guest()) { + $users[$guest->id] = fullname($guest); + } + } + + // Get all the hosts that we SSO with + $sql = "SELECT DISTINCT + h.id, + h.name + s.name as servicename + FROM + {$CFG->prefix}mnet_host h + LEFT OUTER JOIN + {$CFG->prefix}mnet_rpc2host rh ON + (h.id=rh.host_id AND rh.subscribe!=0) + LEFT OUTER JOIN + {$CFG->prefix}mnet_service2rpc sr ON + sr.rpcid=rh.rpc_id + LEFT OUTER JOIN + {$CFG->prefix}mnet_service s ON + (sr.serviceid=s.id AND s.name='sso')"; + $hosts = get_records_sql($sql); + + foreach($hosts as $host) { + $hostarray[$host->id] = $host->name; + } + + $hostarray[$CFG->mnet_localhost_id] = $SITE->fullname; + asort($hostarray); + + foreach($hostarray as $hostid => $name) { + $courses = array(); + $sites = array(); + if ($CFG->mnet_localhost_id == $hostid) { + if (has_capability('moodle/site:viewreports', $sitecontext) && $showcourses) { + if ($ccc = get_records("course", "", "", "fullname","id,fullname,category")) { + foreach ($ccc as $cc) { + if ($cc->category) { + $courses["$hostid/$cc->id"] = "- $cc->fullname"; + } else { + $sites["$hostid/$cc->id"] = "$cc->fullname Site"; + } + } + } + } + } else { + if (has_capability('moodle/site:viewreports', $sitecontext) && $showcourses) { + $sql = "select distinct course, coursename from mdl_mnet_log where hostid = '$hostid'"; + if ($ccc = get_records_sql($sql)) { + foreach ($ccc as $cc) { + if (1 == $cc->course) { + $sites["$hostid/$cc->course"] = "$cc->coursename Site"; + } else { + $courses["$hostid/$cc->course"] = "- $cc->coursename"; + } + } + } + } + } + + asort($courses); + $dropdown[$name] = $sites + $courses; + } + + + $activities = array(); + $selectedactivity = ""; + +/// 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') { // Bodgy + $strsection = get_string("week"); + } else { + $strsection = get_string("topic"); + } + foreach ($modinfo as $mod) { + if ($mod->mod == "label") { + 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"; + } + } + } + + if (has_capability('moodle/site:viewreports', $sitecontext) && !$course->category) { + $activities["site_errors"] = get_string("siteerrors"); + if ($modid === "site_errors") { + $selectedactivity = "site_errors"; + } + } + + $strftimedate = get_string("strftimedate"); + $strftimedaydate = get_string("strftimedaydate"); + + asort($users); + + // Prepare the list of action options. + $actions = array( + 'view' => get_string('view'), + 'add' => get_string('add'), + 'update' => get_string('update'), + 'delete' => get_string('delete'), + '-view' => get_string('allchanges') + ); + + // 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 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 == "today") { + $selecteddate = $today; + } + + $cid = empty($course->id)? '1' : $course->id; + echo "
\n"; + echo "
wwwroot/course/report/log/index.php\" method=\"get\">\n"; + echo "\n"; + echo "\n"; + echo "\n"; + if (has_capability('moodle/site:viewreports', $sitecontext) && $showcourses) { + $cid = empty($course->id)? '1' : $course->id; + choose_from_menu_nested($dropdown, "host_course", $hostid.'/'.$cid, ""); + } else { + $courses = array(); + $courses[$course->id] = $course->fullname . ((empty($course->category)) ? ' (Site) ' : ''); + choose_from_menu($courses,"id",$course->id,false); + if (has_capability('moodle/site:viewreports', $sitecontext)) { + $a->url = "$CFG->wwwroot/course/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser" + ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showcourses=1&showusers=$showusers"; + print_string('logtoomanycourses','moodle',$a); + } + } + + if ($showgroups) { + if ($cgroups = get_groups($course->id)) { + foreach ($cgroups as $cgroup) { + $groups[$cgroup->id] = $cgroup->name; + } + } + else { + $groups = array(); + } + choose_from_menu ($groups, "group", $selectedgroup, get_string("allgroups") ); + } + + if ($showusers) { + choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") ); + } + else { + $users = array(); + if (!empty($selecteduser)) { + $user = get_record('user','id',$selecteduser); + $users[$selecteduser] = fullname($user); + } + else { + $users[0] = get_string('allparticipants'); + } + choose_from_menu($users, 'user', $selecteduser, false); + $a->url = "$CFG->wwwroot/course/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser" + ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showusers=1&showcourses=$showcourses"; + print_string('logtoomanyusers','moodle',$a); + } + choose_from_menu ($dates, "date", $selecteddate, get_string("alldays")); + choose_from_menu ($activities, "modid", $selectedactivity, get_string("allactivities"), "", ""); + choose_from_menu ($actions, 'modaction', $modaction, get_string("allactions")); + + $logformats = array('showashtml' => get_string('displayonpage'), + 'downloadascsv' => get_string('downloadtext'), + 'downloadasexcel' => get_string('downloadexcel')); + /* + $logformats = array('showashtml' => get_string('displayonpage'), + 'downloadascsv' => get_string('downloadtext'), + 'downloadasexcel' => get_string('downloadexcel'), + 'downloadasooo' => get_string('downloadasooo')); + */ + choose_from_menu ($logformats, 'logformat', $logformat, false); + echo ''; + echo "
"; + echo "
"; +} + function print_log_selector_form($course, $selecteduser=0, $selecteddate='today', $modname="", $modid=0, $modaction='', $selectedgroup=-1, $showcourses=0, $showusers=0, $logformat='showashtml') { -- 2.39.5