Skip to content

Commit

Permalink
MDL-29030 move user log reporting to report_log plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Nov 6, 2011
1 parent 4d00fde commit fad8e02
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 148 deletions.
22 changes: 0 additions & 22 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
require_once($CFG->libdir.'/completionlib.php');
require_once($CFG->libdir.'/filelib.php');

define('COURSE_MAX_LOG_DISPLAY', 150); // days
define('COURSE_MAX_LOGS_PER_PAGE', 1000); // records
define('COURSE_MAX_RECENT_PERIOD', 172800); // Two days, in seconds
define('COURSE_MAX_SUMMARIES_PER_PAGE', 10); // courses
Expand Down Expand Up @@ -829,27 +828,6 @@ function print_log_ods($course, $user, $date, $order='l.time DESC', $modname,
}


function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
global $CFG, $USER;

// note: this is a hack - we should not use plugins from core
if (!file_exists($CFG->dirroot.'/report/log/graph.php')) {
return;
}

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('report/log:view', get_context_instance(CONTEXT_COURSE, $course->id)) ||
($course->showreports and $USER->id == $userid)) {
echo '<img src="'.$CFG->wwwroot.'/report/log/graph.php?id='.$course->id.
'&amp;user='.$userid.'&amp;type='.$type.'&amp;date='.$date.'" alt="" />';
}
}
}


function print_overview($courses, array $remote_courses=array()) {
global $CFG, $USER, $DB, $OUTPUT;

Expand Down
30 changes: 5 additions & 25 deletions course/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
if ($mode === 'outline' or $mode === 'complete') {
$url = new moodle_url('/report/outline/user.php', array('id'=>$user->id, 'course'=>$course->id, 'mode'=>$mode));
redirect($url);

} else if ($mode === 'todaylogs' or $mode === 'alllogs') {
$logmode = ($mode === 'todaylogs') ? 'today' : 'all';
$url = new moodle_url('/report/log/user.php', array('id'=>$user->id, 'course'=>$course->id, 'mode'=>$logmode));
redirect($url);
}

require_login();
Expand Down Expand Up @@ -82,14 +87,6 @@

$modes = array();

if ($myreports or $anyreport or has_capability('report/log:viewtoday', $coursecontext)) {
$modes[] = 'todaylogs';
}

if ($myreports or $anyreport or has_capability('report/log:view', $coursecontext)) {
$modes[] = 'alllogs';
}

if ($myreports or $anyreport or has_capability('report/stats:view', $coursecontext)) {
$modes[] = 'stats';
}
Expand Down Expand Up @@ -138,8 +135,6 @@
$stractivityreport = get_string("activityreport");
$strparticipants = get_string("participants");
$strcomplete = get_string("complete");
$stralllogs = get_string("alllogs");
$strtodaylogs = get_string("todaylogs");
$strmode = get_string($mode);
$fullname = fullname($user, true);

Expand Down Expand Up @@ -169,21 +164,6 @@
}
break;

case "todaylogs" :
echo '<div class="graph">';
print_log_graph($course, $user->id, "userday.png");
echo '</div>';
print_log($course, $user->id, usergetmidnight(time()), "l.time DESC", $page, $perpage,
"user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode");
break;

case "alllogs" :
echo '<div class="graph">';
print_log_graph($course, $user->id, "usercourse.png");
echo '</div>';
print_log($course, $user->id, 0, "l.time DESC", $page, $perpage,
"user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode");
break;
case 'stats':

if (empty($CFG->enablestats)) {
Expand Down
35 changes: 6 additions & 29 deletions lib/navigationlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1985,11 +1985,9 @@ protected function load_for_user($user=null, $forceforcontext=false) {
// Add a reports tab and then add reports the the user has permission to see.
$anyreport = has_capability('moodle/user:viewuseractivitiesreport', $usercontext);

$logtodayreport = ($anyreport || has_capability('report/log:viewtoday', $coursecontext));
$logreport = ($anyreport || has_capability('report/log:view', $coursecontext));
$statsreport = ($anyreport || has_capability('report/stats:view', $coursecontext));

$somereport = $logtodayreport || $logreport || $statsreport;
$somereport = $statsreport;

$viewreports = ($anyreport || $somereport || ($course->showreports && $iscurrentuser && $forceforcontext));
if ($viewreports) {
Expand All @@ -2000,14 +1998,6 @@ protected function load_for_user($user=null, $forceforcontext=false) {
$reportargs['id'] = SITEID;
}

if ($viewreports || $logtodayreport) {
$reporttab->add(get_string('todaylogs'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'todaylogs'))));
}

if ($viewreports || $logreport ) {
$reporttab->add(get_string('alllogs'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'alllogs'))));
}

if (!empty($CFG->enablestats)) {
if ($viewreports || $statsreport) {
$reporttab->add(get_string('stats'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'stats'))));
Expand Down Expand Up @@ -2109,20 +2099,10 @@ protected function load_for_user($user=null, $forceforcontext=false) {

//TODO: hacky area - migrate to plugin callbacks above

$logtodayreport = ($anyreport || has_capability('report/log:viewtoday', $usercoursecontext));
$logreport = ($anyreport || has_capability('report/log:view', $usercoursecontext));
$statsreport = ($anyreport || has_capability('report/stats:view', $usercoursecontext));
if ($logtodayreport || $logreport || $statsreport) {
if ($statsreport) {
$reportargs = array('user'=>$user->id, 'id'=>$usercourse->id);

if ($logtodayreport) {
$reporttab->add(get_string('todaylogs'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'todaylogs'))));
}

if ($logreport) {
$reporttab->add(get_string('alllogs'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'alllogs'))));
}

if (!empty($CFG->enablestats) && $statsreport) {
$reporttab->add(get_string('stats'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'stats'))));
}
Expand Down Expand Up @@ -3471,14 +3451,11 @@ protected function load_module_settings() {
$url = new moodle_url('/filter/manage.php', array('contextid'=>$this->page->cm->context->id));
$modulenode->add(get_string('filters', 'admin'), $url, self::TYPE_SETTING, null, 'filtermanage');
}

//TODO: all this is a hack - we can not link to plugins like this - this must be abstracted to plugin callbacks!

if (has_capability('report/log:view', get_context_instance(CONTEXT_COURSE, $this->page->cm->course))) {
$url = new moodle_url('/report/log/index.php', array('chooselog'=>'1','id'=>$this->page->cm->course,'modid'=>$this->page->cm->id));
$modulenode->add(get_string('logs'), $url, self::TYPE_SETTING, null, 'logreport');
// Add reports
$reports = get_plugin_list_with_function('report', 'extend_navigation_module', 'lib.php');
foreach ($reports as $reportfunction) {
$reportfunction($modulenode, $this->page->cm);
}

// Add a backup link
$featuresfunc = $this->page->activityname.'_supports';
if (function_exists($featuresfunc) && $featuresfunc(FEATURE_BACKUP_MOODLE2) && has_capability('moodle/backup:backupactivity', $this->page->cm->context)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/outputrenderers.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ public function login_info() {
} else {
$loggedinas .= get_string('failedloginattemptsall', '', $count);
}
if (has_capability('report/log:view', get_context_instance(CONTEXT_SYSTEM))) {
if (file_exists("$CFG->dirroot/report/log/index.php") and has_capability('report/log:view', get_context_instance(CONTEXT_SYSTEM))) {
$loggedinas .= ' (<a href="'.$CFG->wwwroot.'/report/log/index.php'.
'?chooselog=1&amp;id=1&amp;modid=site_errors">'.get_string('logs').'</a>)';
}
Expand Down
70 changes: 36 additions & 34 deletions report/log/graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Produces a graph of log accesses
* Produces a graph of log accesses for a user
*
* @package report
* @subpackage log
Expand All @@ -24,46 +24,55 @@
*/

require("../../config.php");
require_once("$CFG->dirroot/course/lib.php");
require_once("$CFG->libdir/graphlib.php");
require_once($CFG->dirroot.'/report/log/locallib.php');

$id = required_param('id', PARAM_INT); // Course ID
$type = required_param('type', PARAM_FILE); // Graph Type
$user = optional_param('user', 0, PARAM_INT); // Student ID
$id = required_param('id', PARAM_INT); // Course ID
$type = required_param('type', PARAM_FILE); // Graph Type
$user = required_param('user', PARAM_INT); // Student ID
$date = optional_param('date', 0, PARAM_INT); // A time of a day (in GMT)

$url = new moodle_url('/report/log/graph.php', array('id'=>$id,'type'=>$type));
if ($user !== 0) {
$url->param('user', $user);
}
if ($date !== 0) {
$url->param('date', $date);
}
$url = new moodle_url('/report/log/graph.php', array('id'=>$id,'type'=>$type,'user'=>$user,'date'=>$date));
$PAGE->set_url($url);

if (! $course = $DB->get_record("course", array("id"=>$id))) {
print_error('invalidcourseid');
if ($type !== "usercourse.png" and $type !== "userday.png") {
$type = 'userday.png';
}

require_login($course);
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$course = $DB->get_record("course", array("id"=>$id), '*', MUST_EXIST);
$user = $DB->get_record("user", array("id"=>$user, 'deleted'=>0), '*', MUST_EXIST);

$coursecontext = context_course::instance($course->id);
$personalcontext = context_user::instance($user->id);

if (!$course->showreports or $USER->id != $user) {
require_capability('report/log:view', $context);
if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
//TODO: do not require parents to be enrolled in courses - this is a hack!
require_login();
$PAGE->set_course($course);
} else {
require_login($course);
}

if ($user) {
if (! $user = $DB->get_record("user", array("id"=>$user))) {
print_error("nousers");
list($all, $today) = report_log_can_access_user_report($user, $course);

if ($type === "userday.png") {
if (!$today) {
require_capability('report/log:viewtoday', $coursecontext);
}
} else {
if (!$all) {
require_capability('report/log:view', $coursecontext);
}
}

add_to_log($course->id, 'course', 'report log', "report/log/graph.php?user=$user->id&id=$course->id&type=$type&date=$date", $course->id);

$logs = array();

$timenow = time();

switch ($type) {
case "usercourse.png":
if ($type === "usercourse.png") {

$site = get_site();

Expand All @@ -73,7 +82,7 @@
$courseselect = $course->id;
}

$maxseconds = COURSE_MAX_LOG_DISPLAY * 3600 * 24; // seconds
$maxseconds = REPORT_LOG_MAX_DISPLAY * 3600 * 24; // seconds
//$maxseconds = 60 * 3600 * 24; // seconds
if ($timenow - $course->startdate > $maxseconds) {
$course->startdate = $timenow - $maxseconds;
Expand Down Expand Up @@ -120,7 +129,7 @@

$graph = new graph(750, 400);

$a->coursename = format_string($course->shortname, true, array('context' => $context));
$a->coursename = format_string($course->shortname, true, array('context' => $coursecontext));
$a->username = fullname($user, true);
$graph->parameter['title'] = get_string("hitsoncourse", "", $a);

Expand Down Expand Up @@ -149,9 +158,7 @@
error_reporting(5); // ignore most warnings such as font problems etc
$graph->draw_stack();

break;

case "userday.png":
} else {

$site = get_site();

Expand Down Expand Up @@ -183,7 +190,7 @@

$graph = new graph(750, 400);

$a->coursename = format_string($course->shortname, true, array('context' => $context));
$a->coursename = format_string($course->shortname, true, array('context' => $coursecontext));
$a->username = fullname($user, true);
$graph->parameter['title'] = get_string("hitsoncoursetoday", "", $a);

Expand All @@ -207,10 +214,5 @@

error_reporting(5); // ignore most warnings such as font problems etc
$graph->draw_stack();

break;

default:
break;
}

Loading

0 comments on commit fad8e02

Please sign in to comment.