course/report/outline $CFG typo
[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(new moodle_url($CFG->wwwroot.'/course/report/outline/index.php', array('id'=>$id)));
16     require_login($course);
17     $context = get_context_instance(CONTEXT_COURSE, $course->id);
18     require_capability('coursereport/outline:view', $context);
20     add_to_log($course->id, 'course', 'report outline', "report/outline/index.php?id=$course->id", $course->id);
22     $showlastaccess = true;
23     $hiddenfields = explode(',', $CFG->hiddenuserfields);
25     if (array_search('lastaccess', $hiddenfields) and !has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
26         $showlastaccess = false;
27     }
29     $stractivityreport = get_string('activityreport');
30     $stractivity       = get_string('activity');
31     $strlast           = get_string('lastaccess');
32     $strreports        = get_string('reports');
33     $strviews          = get_string('views');
34     $strrelatedblogentries = get_string('relatedblogentries', 'blog');
36     $PAGE->set_title($course->shortname .': '. $stractivityreport);
37     $PAGE->set_heading($course->fullname);
38     $PAGE->navbar->add($strreports, new moodle_url($CFG->wwwroot.'/course/report.php', array('id'=>$course->id)));
39     $PAGE->navbar->add($stractivityreport);
40     echo $OUTPUT->header();
41     echo $OUTPUT->heading(format_string($course->fullname));
43     if (!$logstart = $DB->get_field_sql("SELECT MIN(time) FROM {log}")) {
44         print_error('logfilenotavailable');
45     }
47     echo $OUTPUT->container(get_string('computedfromlogs', 'admin', userdate($logstart)), 'loginfo');
49     $outlinetable = new html_table();
50     $outlinetable->add_class('generaltable boxaligncenter');
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);
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->add_class('section');
86                 $sectioncell = new html_table_cell();
87                 $sectioncell->colspan = count($outlinetable->head);
89                 $sectiontitle = '';
90                 switch ($course->format) {
91                     case 'weeks': $sectiontitle = get_string('week'); break;
92                     case 'topics': $sectiontitle = get_string('topic'); break;
93                     default: $sectiontitle = get_string('section'); break;
94                 }
96                 $sectioncell->text = $OUTPUT->heading($sectiontitle . ' ' . $sectionnum, 3);
97                 $sectionrow->cells[] = $sectioncell;
98                 $outlinetable->data[] = $sectionrow;
100                 $prevsecctionnum = $sectionnum;
101             }
103             $dimmed = $cm->visible ? '' : 'class="dimmed"';
104             $modulename = get_string('modulename', $cm->modname);
106             $reportrow = new html_table_row();
107             $activitycell = new html_table_cell();
108             $activitycell->add_class('activity');
110             $activityicon = html_image::make($OUTPUT->mod_icon_url('icon', $cm->modname));
111             $activityicon->add_class('icon');
112             $activityicon->alt = $modulename;
114             $activitylink = html_link::make("$CFG->wwwroot/mod/$cm->modname/view.php?id=$cm->id", format_string($cm->name));
115             if (!$cm->visible) {
116                 $activitylink->add_class('dimmed');
117             }
119             $activitycell->text = $OUTPUT->image($activityicon) . $OUTPUT->link($activitylink);
121             $reportrow->cells[] = $activitycell;
123             $numviewscell = new html_table_cell();
124             $numviewscell->add_class('numviews');
126             if (!empty($views[$cm->id]->numviews)) {
127                 $numviewscell->text = $views[$cm->id]->numviews;
128             } else {
129                 $numviewscell->text = '-';
130             }
132             $reportrow->cells[] = $numviewscell;
134             if ($CFG->useblogassociations) {
135                 $blogcell = new html_table_cell();
136                 $blogcell->add_class('blog');
137                 if ($blogcount = blog_get_associated_count($course->id, $cm->id)) {
138                     $blogcell->text = $OUTPUT->link(html_link::make('/blog/index.php?modid='.$cm->id, $blogcount));
139                 } else {
140                     $blogcell->text = '-';
141                 }
142                 $reportrow->cells[] = $blogcell;
143             }
145             if ($showlastaccess) {
146                 $lastaccesscell = new html_table_cell();
147                 $lastaccesscell->add_class('lastaccess');
149                 if (isset($views[$cm->id]->lasttime)) {
150                     $timeago = format_time(time() - $views[$cm->id]->lasttime);
151                     $lastaccesscell->text = userdate($views[$cm->id]->lasttime)." ($timeago)";
152                 }
153                 $reportrow->cells[] = $lastaccesscell;
154             }
155             $outlinetable->data[] = $reportrow;
156         }
157     }
158     echo $OUTPUT->table($outlinetable);
160     echo $OUTPUT->footer();