6b1073e4eef3719f9f5996d08ccd40a1519cb6dc
[moodle.git] / course / report / outline / index.php
1 <?php
3 // Display user activity reports for a course (totals)
5     require_once('../../../config.php');
6     require_once($CFG->dirroot.'/course/lib.php');
8     $id = required_param('id',PARAM_INT);       // course id
10     if (!$course = $DB->get_record('course', array('id'=>$id))) {
11         print_error('invalidcourseid');
12     }
14     $PAGE->set_url('/course/report/outline/index.php', array('id'=>$id));
15     $PAGE->set_pagelayout('report');
17     require_login($course);
18     $context = get_context_instance(CONTEXT_COURSE, $course->id);
19     require_capability('coursereport/outline:view', $context);
21     add_to_log($course->id, 'course', 'report outline', "report/outline/index.php?id=$course->id", $course->id);
23     $showlastaccess = true;
24     $hiddenfields = explode(',', $CFG->hiddenuserfields);
26     if (array_search('lastaccess', $hiddenfields) and !has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
27         $showlastaccess = false;
28     }
30     $stractivityreport = get_string('activityreport');
31     $stractivity       = get_string('activity');
32     $strlast           = get_string('lastaccess');
33     $strreports        = get_string('reports');
34     $strviews          = get_string('views');
35     $strrelatedblogentries = get_string('relatedblogentries', 'blog');
37     $PAGE->set_title($course->shortname .': '. $stractivityreport);
38     $PAGE->set_heading($course->fullname);
39     echo $OUTPUT->header();
40     echo $OUTPUT->heading(format_string($course->fullname));
42     if (!$logstart = $DB->get_field_sql("SELECT MIN(time) FROM {log}")) {
43         print_error('logfilenotavailable');
44     }
46     echo $OUTPUT->container(get_string('computedfromlogs', 'admin', userdate($logstart)), 'loginfo');
48     $outlinetable = new html_table();
49     $outlinetable->attributes['class'] = 'generaltable boxaligncenter';
50     $outlinetable->attributes['style'] = 'width:2000px;';
51     $outlinetable->cellpadding = 5;
52     $outlinetable->id = 'outlinetable';
53     $outlinetable->head = array($stractivity, $strviews);
55     if ($CFG->useblogassociations) {
56         $outlinetable->head[] = $strrelatedblogentries;
57     }
59     if ($showlastaccess) {
60         $outlinetable->head[] = $strlast;
61     }
63     $modinfo = get_fast_modinfo($course);
64     $sections = get_all_sections($course->id);
66     $sql = "SELECT cm.id, COUNT('x') AS numviews, MAX(time) AS lasttime
67               FROM {course_modules} cm
68                    JOIN {modules} m ON m.id = cm.module
69                    JOIN {log} l     ON l.cmid = cm.id
70              WHERE cm.course = ? AND l.action LIKE 'view%' AND m.visible = 1
71           GROUP BY cm.id";
72     $views = $DB->get_records_sql($sql, array($course->id));
74     $prevsecctionnum = 0;
75     foreach ($modinfo->sections as $sectionnum=>$section) {
76         foreach ($section as $cmid) {
77             $cm = $modinfo->cms[$cmid];
78             if ($cm->modname == 'label') {
79                 continue;
80             }
81             if (!$cm->uservisible) {
82                 continue;
83             }
84             if ($prevsecctionnum != $sectionnum) {
85                 $sectionrow = new html_table_row();
86                 $sectionrow->attributes['class'] = 'section';
87                 $sectioncell = new html_table_cell();
88                 $sectioncell->colspan = count($outlinetable->head);
90                 $sectiontitle = get_section_name($course, $sections[$sectionnum]);
92                 $sectioncell->text = $OUTPUT->heading($sectiontitle, 3);
93                 $sectionrow->cells[] = $sectioncell;
94                 $outlinetable->data[] = $sectionrow;
96                 $prevsecctionnum = $sectionnum;
97             }
99             $dimmed = $cm->visible ? '' : 'class="dimmed"';
100             $modulename = get_string('modulename', $cm->modname);
102             $reportrow = new html_table_row();
103             $activitycell = new html_table_cell();
104             $activitycell->attributes['class'] = 'activity';
106             $activityicon = $OUTPUT->pix_icon('icon', $modulename, $cm->modname, array('class'=>'icon'));
108             $attributes = array();
109             if (!$cm->visible) {
110                 $attributes['class'] = 'dimmed';
111             }
113             $activitycell->text = $activityicon . html_writer::link("$CFG->wwwroot/mod/$cm->modname/view.php?id=$cm->id", format_string($cm->name), $attributes);;
115             $reportrow->cells[] = $activitycell;
117             $numviewscell = new html_table_cell();
118             $numviewscell->attributes['class'] = 'numviews';
120             if (!empty($views[$cm->id]->numviews)) {
121                 $numviewscell->text = $views[$cm->id]->numviews;
122             } else {
123                 $numviewscell->text = '-';
124             }
126             $reportrow->cells[] = $numviewscell;
128             if ($CFG->useblogassociations) {
129                 $blogcell = new html_table_cell();
130                 $blogcell->attributes['class'] = 'blog';
131                 if ($blogcount = blog_get_associated_count($course->id, $cm->id)) {
132                     $blogcell->text = html_writer::link('/blog/index.php?modid='.$cm->id, $blogcount);
133                 } else {
134                     $blogcell->text = '-';
135                 }
136                 $reportrow->cells[] = $blogcell;
137             }
139             if ($showlastaccess) {
140                 $lastaccesscell = new html_table_cell();
141                 $lastaccesscell->attributes['class'] = 'lastaccess';
143                 if (isset($views[$cm->id]->lasttime)) {
144                     $timeago = format_time(time() - $views[$cm->id]->lasttime);
145                     $lastaccesscell->text = userdate($views[$cm->id]->lasttime)." ($timeago)";
146                 }
147                 $reportrow->cells[] = $lastaccesscell;
148             }
149             $outlinetable->data[] = $reportrow;
150         }
151     }
152     echo html_writer::table($outlinetable);
154     echo $OUTPUT->footer();