course-reports MDL-24173 Course reports now use the report page layout + minor tweaks...
[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->cellpadding = 5;
51     $outlinetable->id = 'outlinetable';
52     $outlinetable->head = array($stractivity, $strviews);
54     if ($CFG->useblogassociations) {
55         $outlinetable->head[] = $strrelatedblogentries;
56     }
58     if ($showlastaccess) {
59         $outlinetable->head[] = $strlast;
60     }
62     $modinfo = get_fast_modinfo($course);
63     $sections = get_all_sections($course->id);
65     $sql = "SELECT cm.id, COUNT('x') AS numviews, MAX(time) AS lasttime
66               FROM {course_modules} cm
67                    JOIN {modules} m ON m.id = cm.module
68                    JOIN {log} l     ON l.cmid = cm.id
69              WHERE cm.course = ? AND l.action LIKE 'view%' AND m.visible = 1
70           GROUP BY cm.id";
71     $views = $DB->get_records_sql($sql, array($course->id));
73     $prevsecctionnum = 0;
74     foreach ($modinfo->sections as $sectionnum=>$section) {
75         foreach ($section as $cmid) {
76             $cm = $modinfo->cms[$cmid];
77             if ($cm->modname == 'label') {
78                 continue;
79             }
80             if (!$cm->uservisible) {
81                 continue;
82             }
83             if ($prevsecctionnum != $sectionnum) {
84                 $sectionrow = new html_table_row();
85                 $sectionrow->attributes['class'] = 'section';
86                 $sectioncell = new html_table_cell();
87                 $sectioncell->colspan = count($outlinetable->head);
89                 $sectiontitle = get_section_name($course, $sections[$sectionnum]);
91                 $sectioncell->text = $OUTPUT->heading($sectiontitle, 3);
92                 $sectionrow->cells[] = $sectioncell;
93                 $outlinetable->data[] = $sectionrow;
95                 $prevsecctionnum = $sectionnum;
96             }
98             $dimmed = $cm->visible ? '' : 'class="dimmed"';
99             $modulename = get_string('modulename', $cm->modname);
101             $reportrow = new html_table_row();
102             $activitycell = new html_table_cell();
103             $activitycell->attributes['class'] = 'activity';
105             $activityicon = $OUTPUT->pix_icon('icon', $modulename, $cm->modname, array('class'=>'icon'));
107             $attributes = array();
108             if (!$cm->visible) {
109                 $attributes['class'] = 'dimmed';
110             }
112             $activitycell->text = $activityicon . html_writer::link("$CFG->wwwroot/mod/$cm->modname/view.php?id=$cm->id", format_string($cm->name), $attributes);;
114             $reportrow->cells[] = $activitycell;
116             $numviewscell = new html_table_cell();
117             $numviewscell->attributes['class'] = 'numviews';
119             if (!empty($views[$cm->id]->numviews)) {
120                 $numviewscell->text = $views[$cm->id]->numviews;
121             } else {
122                 $numviewscell->text = '-';
123             }
125             $reportrow->cells[] = $numviewscell;
127             if ($CFG->useblogassociations) {
128                 $blogcell = new html_table_cell();
129                 $blogcell->attributes['class'] = 'blog';
130                 if ($blogcount = blog_get_associated_count($course->id, $cm->id)) {
131                     $blogcell->text = html_writer::link('/blog/index.php?modid='.$cm->id, $blogcount);
132                 } else {
133                     $blogcell->text = '-';
134                 }
135                 $reportrow->cells[] = $blogcell;
136             }
138             if ($showlastaccess) {
139                 $lastaccesscell = new html_table_cell();
140                 $lastaccesscell->attributes['class'] = 'lastaccess';
142                 if (isset($views[$cm->id]->lasttime)) {
143                     $timeago = format_time(time() - $views[$cm->id]->lasttime);
144                     $lastaccesscell->text = userdate($views[$cm->id]->lasttime)." ($timeago)";
145                 }
146                 $reportrow->cells[] = $lastaccesscell;
147             }
148             $outlinetable->data[] = $reportrow;
149         }
150     }
151     echo html_writer::table($outlinetable);
153     echo $OUTPUT->footer();