. /** * This file contains functions used by the log reports * * @package course-report * @copyright 1999 onwards Martin Dougiamas http://moodle.com * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ function print_mnet_log_selector_form($hostid, $course, $selecteduser=0, $selecteddate='today', $modname="", $modid=0, $modaction='', $selectedgroup=-1, $showcourses=0, $showusers=0, $logformat='showashtml') { global $USER, $CFG, $SITE, $DB, $OUTPUT; require_once $CFG->dirroot.'/mnet/peer.php'; $mnet_peer = new mnet_peer(); $mnet_peer->set_id($hostid); $sql = "SELECT DISTINCT course, hostid, coursename FROM {mnet_log}"; $courses = $DB->get_records_sql($sql); $remotecoursecount = count($courses); // first check to see if we can override showcourses and showusers $numcourses = $remotecoursecount + $DB->count_records('course'); if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$showcourses) { $showcourses = 1; } $sitecontext = get_context_instance(CONTEXT_SYSTEM); // 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(); // Define limitfrom and limitnum for queries below // If $showusers is enabled... don't apply limitfrom and limitnum $limitfrom = empty($showusers) ? 0 : ''; $limitnum = empty($showusers) ? COURSE_MAX_USERS_PER_DROPDOWN + 1 : ''; // If looking at a different host, we're interested in all our site users if ($hostid == $CFG->mnet_localhost_id && $course->id != SITEID) { $courseusers = get_users_by_capability($context, 'moodle/course:view', 'u.id, u.firstname, u.lastname, u.idnumber', 'lastname ASC, firstname ASC', $limitfrom, $limitnum, $selectedgroup,'', false); } else { // this may be a lot of users :-( $courseusers = $DB->get_records('user', array('deleted'=>0), 'lastaccess DESC', 'id, firstname, lastname, idnumber', $limitfrom, $limitnum); } 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 have log records $sql = "select distinct h.id, h.name from {mnet_host} h, {mnet_log} l where h.id = l.hostid order by h.name"; if ($hosts = $DB->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('coursereport/log:view', $sitecontext) && $showcourses) { if ($ccc = $DB->get_records("course", null, "fullname","id,fullname,category")) { foreach ($ccc as $cc) { if ($cc->id == SITEID) { $sites["$hostid/$cc->id"] = format_string($cc->fullname).' ('.get_string('site').')'; } else { $courses["$hostid/$cc->id"] = format_string($cc->fullname); } } } } } else { if (has_capability('coursereport/log:view', $sitecontext) && $showcourses) { $sql = "SELECT DISTINCT course, coursename FROM {mnet_log} where hostid = ?"; if ($ccc = $DB->get_records_sql($sql, array($hostid))) { foreach ($ccc as $cc) { if (1 == $cc->course) { // TODO: this might be wrong - site course may have another id $sites["$hostid/$cc->course"] = $cc->coursename.' ('.get_string('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('coursereport/log:view', $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; } echo "
'; } function print_log_selector_form($course, $selecteduser=0, $selecteddate='today', $modname="", $modid=0, $modaction='', $selectedgroup=-1, $showcourses=0, $showusers=0, $logformat='showashtml') { global $USER, $CFG, $DB, $OUTPUT; // first check to see if we can override showcourses and showusers $numcourses = $DB->count_records("course"); if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$showcourses) { $showcourses = 1; } $sitecontext = get_context_instance(CONTEXT_SYSTEM); $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; } // Get all the possible users $users = array(); if ($course->id != SITEID) { $courseusers = get_users_by_capability($context, 'moodle/course:view', 'u.id, u.firstname, u.lastname, u.idnumber', 'lastname ASC, firstname ASC', '','',$selectedgroup,null, false); } else { // this may be a lot of users :-( $courseusers = $DB->get_records('user', array('deleted'=>0), 'lastaccess DESC', 'id, firstname, lastname, 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); } } if (has_capability('coursereport/log:view', $sitecontext) && $showcourses) { if ($ccc = $DB->get_records("course", null, "fullname", "id,fullname,category")) { foreach ($ccc as $cc) { if ($cc->category) { $courses["$cc->id"] = format_string($cc->fullname); } else { $courses["$cc->id"] = format_string($cc->fullname) . ' (Site)'; } } } asort($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('coursereport/log:view', $sitecontext) && ($course->id == SITEID)) { $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; } echo "'; } /** * This function extends the navigation with the report items * * @param navigation_node $navigation The navigation node to extend * @param stdClass $course The course to object for the report * @param stdClass $context The context of the course */ function log_report_extend_navigation($navigation, $course, $context) { global $CFG, $OUTPUT; if (has_capability('coursereport/log:view', $context)) { $url = new moodle_url($CFG->wwwroot.'/course/report/log/index.php', array('id'=>$course->id)); $navigation->add(get_string('log:view', 'coursereport_log'), $url, navigation_node::TYPE_SETTING, null, null, $OUTPUT->old_icon_url('i/report')); } if (has_capability('coursereport/log:viewlive', $context)) { $livelogs = get_string('livelogs'); $link = html_link::make('/course/report/log/live.php?id='. $course->id, $livelogs); $link->add_action(new popup_action('click', $link->url, 'livelog', array('height' => 500, 'width' => 800))); $navigation->add($livelogs, $link, navigation_node::TYPE_SETTING, null, null, $OUTPUT->old_icon_url('i/report')); } }