Merge branch 'wip-MDL-60241-34' of git://github.com/marinaglancy/moodle into MOODLE_3...
[moodle.git] / report / outline / index.php
blobcd1340392d72c52cb5faeaf4f92a72afb9dae37f
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Display user activity reports for a course (totals)
20 * @package report
21 * @subpackage outline
22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require('../../config.php');
27 require_once($CFG->dirroot.'/report/outline/locallib.php');
29 $id = required_param('id',PARAM_INT); // course id
31 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
33 $PAGE->set_url('/report/outline/index.php', array('id'=>$id));
34 $PAGE->set_pagelayout('report');
36 require_login($course);
37 $context = context_course::instance($course->id);
38 require_capability('report/outline:view', $context);
40 // Trigger an activity report viewed event.
41 $event = \report_outline\event\activity_report_viewed::create(array('context' => $context));
42 $event->trigger();
44 $showlastaccess = true;
45 $hiddenfields = explode(',', $CFG->hiddenuserfields);
47 if (array_search('lastaccess', $hiddenfields) !== false and !has_capability('moodle/user:viewhiddendetails', $context)) {
48 $showlastaccess = false;
51 $stractivityreport = get_string('pluginname', 'report_outline');
52 $stractivity = get_string('activity');
53 $strlast = get_string('lastaccess');
54 $strreports = get_string('reports');
55 $strviews = get_string('views');
56 $strrelatedblogentries = get_string('relatedblogentries', 'blog');
58 $PAGE->set_title($course->shortname .': '. $stractivityreport);
59 $PAGE->set_heading($course->fullname);
60 echo $OUTPUT->header();
61 echo $OUTPUT->heading(format_string($course->fullname));
63 list($uselegacyreader, $useinternalreader, $minloginternalreader, $logtable) = report_outline_get_common_log_variables();
65 // If no legacy and no internal log then don't proceed.
66 if (!$uselegacyreader && !$useinternalreader) {
67 echo $OUTPUT->box_start('generalbox', 'notice');
68 echo $OUTPUT->notification(get_string('nologreaderenabled', 'report_outline'));
69 echo $OUTPUT->box_end();
70 echo $OUTPUT->footer();
71 die();
74 // We want to display the time we are beginning to get logs from in the heading.
75 // If we are using the legacy reader check the minimum time in that log table.
76 if ($uselegacyreader) {
77 $minlog = $DB->get_field_sql('SELECT min(time) FROM {log}');
80 // If we are using the internal reader check the minimum time in that table.
81 if ($useinternalreader) {
82 // If new log table has older data then don't use the minimum time obtained from the legacy table.
83 if (empty($minlog) || ($minloginternalreader <= $minlog)) {
84 $minlog = $minloginternalreader;
88 echo $OUTPUT->container(get_string('computedfromlogs', 'admin', userdate($minlog)), 'loginfo');
90 $outlinetable = new html_table();
91 $outlinetable->attributes['class'] = 'generaltable boxaligncenter';
92 $outlinetable->cellpadding = 5;
93 $outlinetable->id = 'outlinetable';
94 $outlinetable->head = array($stractivity, $strviews);
96 if (!empty($CFG->enableblogs) && $CFG->useblogassociations) {
97 $outlinetable->head[] = $strrelatedblogentries;
100 if ($showlastaccess) {
101 $outlinetable->head[] = $strlast;
104 $modinfo = get_fast_modinfo($course);
106 // If using legacy log then get users from old table.
107 if ($uselegacyreader) {
108 // If we are going to use the internal (not legacy) log table, we should only get records
109 // from the legacy table that exist before we started adding logs to the new table.
110 $limittime = '';
111 if (!empty($minloginternalreader)) {
112 $limittime = ' AND time < :timeto ';
114 // Check if we need to show the last access.
115 $sqllasttime = '';
116 if ($showlastaccess) {
117 $sqllasttime = ", MAX(time) AS lasttime";
119 $logactionlike = $DB->sql_like('l.action', ':action');
120 $sql = "SELECT cm.id, COUNT('x') AS numviews, COUNT(DISTINCT userid) AS distinctusers $sqllasttime
121 FROM {course_modules} cm
122 JOIN {modules} m
123 ON m.id = cm.module
124 JOIN {log} l
125 ON l.cmid = cm.id
126 WHERE cm.course = :courseid
127 AND $logactionlike
128 AND m.visible = :visible $limittime
129 GROUP BY cm.id";
130 $params = array('courseid' => $course->id, 'action' => 'view%', 'visible' => 1);
131 if (!empty($minloginternalreader)) {
132 $params['timeto'] = $minloginternalreader;
134 $views = $DB->get_records_sql($sql, $params);
137 // Get record from sql_internal_table_reader and merge with records obtained from legacy log (if needed).
138 if ($useinternalreader) {
139 // Check if we need to show the last access.
140 $sqllasttime = '';
141 if ($showlastaccess) {
142 $sqllasttime = ", MAX(timecreated) AS lasttime";
144 $sql = "SELECT contextinstanceid as cmid, COUNT('x') AS numviews, COUNT(DISTINCT userid) AS distinctusers $sqllasttime
145 FROM {" . $logtable . "} l
146 WHERE courseid = :courseid
147 AND anonymous = 0
148 AND crud = 'r'
149 AND contextlevel = :contextmodule
150 GROUP BY contextinstanceid";
151 $params = array('courseid' => $course->id, 'contextmodule' => CONTEXT_MODULE);
152 $v = $DB->get_records_sql($sql, $params);
154 if (empty($views)) {
155 $views = $v;
156 } else {
157 // Merge two view arrays.
158 foreach ($v as $key => $value) {
159 if (isset($views[$key]) && !empty($views[$key]->numviews)) {
160 $views[$key]->numviews += $value->numviews;
161 if ($value->lasttime > $views[$key]->lasttime) {
162 $views[$key]->lasttime = $value->lasttime;
164 } else {
165 $views[$key] = $value;
171 $prevsecctionnum = 0;
172 foreach ($modinfo->sections as $sectionnum=>$section) {
173 foreach ($section as $cmid) {
174 $cm = $modinfo->cms[$cmid];
175 if (!$cm->has_view()) {
176 continue;
178 if (!$cm->uservisible) {
179 continue;
181 if ($prevsecctionnum != $sectionnum) {
182 $sectionrow = new html_table_row();
183 $sectionrow->attributes['class'] = 'section';
184 $sectioncell = new html_table_cell();
185 $sectioncell->colspan = count($outlinetable->head);
187 $sectiontitle = get_section_name($course, $sectionnum);
189 $sectioncell->text = $OUTPUT->heading($sectiontitle, 3);
190 $sectionrow->cells[] = $sectioncell;
191 $outlinetable->data[] = $sectionrow;
193 $prevsecctionnum = $sectionnum;
196 $dimmed = $cm->visible ? '' : 'class="dimmed"';
197 $modulename = get_string('modulename', $cm->modname);
199 $reportrow = new html_table_row();
200 $activitycell = new html_table_cell();
201 $activitycell->attributes['class'] = 'activity';
203 $activityicon = $OUTPUT->pix_icon('icon', $modulename, $cm->modname, array('class'=>'icon'));
205 $attributes = array();
206 if (!$cm->visible) {
207 $attributes['class'] = 'dimmed';
210 $activitycell->text = $activityicon . html_writer::link("$CFG->wwwroot/mod/$cm->modname/view.php?id=$cm->id", format_string($cm->name), $attributes);
212 $reportrow->cells[] = $activitycell;
214 $numviewscell = new html_table_cell();
215 $numviewscell->attributes['class'] = 'numviews';
217 if (!empty($views[$cm->id]->numviews)) {
218 $numviewscell->text = get_string('numviews', 'report_outline', $views[$cm->id]);
219 } else {
220 $numviewscell->text = '-';
223 $reportrow->cells[] = $numviewscell;
225 if (!empty($CFG->enableblogs) && $CFG->useblogassociations) {
226 require_once($CFG->dirroot.'/blog/lib.php');
227 $blogcell = new html_table_cell();
228 $blogcell->attributes['class'] = 'blog';
229 if ($blogcount = blog_get_associated_count($course->id, $cm->id)) {
230 $blogurl = new moodle_url('/blog/index.php', array('modid' => $cm->id));
231 $blogcell->text = html_writer::link($blogurl, $blogcount);
232 } else {
233 $blogcell->text = '-';
235 $reportrow->cells[] = $blogcell;
238 if ($showlastaccess) {
239 $lastaccesscell = new html_table_cell();
240 $lastaccesscell->attributes['class'] = 'lastaccess';
242 if (isset($views[$cm->id]->lasttime)) {
243 $timeago = format_time(time() - $views[$cm->id]->lasttime);
244 $lastaccesscell->text = userdate($views[$cm->id]->lasttime)." ($timeago)";
246 $reportrow->cells[] = $lastaccesscell;
248 $outlinetable->data[] = $reportrow;
251 echo html_writer::table($outlinetable);
253 echo $OUTPUT->footer();