3 // Display all recent activity in a flexible way
5 require_once('../config.php');
6 require_once('lib.php');
7 require_once('recent_form.php');
9 $id = required_param('id', PARAM_INT
);
11 if (!$course = get_record('course', 'id', $id) ) {
12 error("That's an invalid course id");
15 require_login($course);
17 add_to_log($course->id
, "course", "recent", "recent.php?id=$course->id", $course->id
);
19 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
21 $meta = '<meta name="robots" content="none" />'; // prevent duplicate content in search engines MDL-7299
23 $lastlogin = time() - COURSE_MAX_RECENT_PERIOD
;
24 if (!isguestuser() and !empty($USER->lastcourseaccess
[$COURSE->id
])) {
25 if ($USER->lastcourseaccess
[$COURSE->id
] > $lastlogin) {
26 $lastlogin = $USER->lastcourseaccess
[$COURSE->id
];
30 $param = new object();
32 $param->modid
= 'all';
34 $param->sortby
= 'default';
35 $param->date
= $lastlogin;
36 $param->id
= $COURSE->id
;
38 $mform = new recent_form();
39 $mform->set_data($param);
40 if ($formdata = $mform->get_data(false)) {
44 $userinfo = get_string('allparticipants');
45 $dateinfo = get_string('alldays');
47 if (!empty($param->user
)) {
48 if (!$u = get_record('user', 'id', $param->user
) ) {
49 error("That's an invalid user!");
51 $userinfo = fullname($u);
54 $strrecentactivity = get_string('recentactivity');
56 $navlinks[] = array('name' => $strrecentactivity, 'link' => "recent.php?id=$course->id", 'type' => 'misc');
57 $navlinks[] = array('name' => $userinfo, 'link' => null, 'type' => 'misc');
58 $navigation = build_navigation($navlinks);
59 print_header("$course->shortname: $strrecentactivity", $course->fullname
, $navigation, '', $meta);
60 print_heading(format_string($course->fullname
) . ": $userinfo", '', 3);
64 $modinfo =& get_fast_modinfo($course);
65 get_all_mods($course->id
, $mods, $modnames, $modnamesplural, $modnamesused);
67 if (has_capability('moodle/course:viewhiddensections', $context)) {
70 $hiddenfilter = "AND cs.visible = 1";
73 if ($ss = get_records_sql("SELECT cs.id, cs.section, cs.sequence, cs.summary, cs.visible
74 FROM {$CFG->prefix}course_sections cs
75 WHERE cs.course = $course->id AND cs.section <= $course->numsections
78 foreach ($ss as $section) {
79 $sections[$section->section
] = $section;
83 if ($param->modid
=== 'all') {
86 } else if (strpos($param->modid
, 'mod/') === 0) {
87 $modname = substr($param->modid
, strlen('mod/'));
88 if (array_key_exists($modname, $modnames) and file_exists("$CFG->dirroot/mod/$modname/lib.php")) {
92 } else if (strpos($param->modid
, 'section/') === 0) {
93 $sectionid = substr($param->modid
, strlen('section/'));
94 if (isset($sections[$sectionid])) {
95 $sections = array($sectionid=>$sections[$sectionid]);
98 } else if (is_numeric($param->modid
)) {
99 $section = $sections[$modinfo->cms
[$param->modid
]->sectionnum
];
100 $section->sequence
= $param->modid
;
101 $sections = array($section->sequence
=>$section);
104 switch ($course->format
) {
105 case 'weeks': $sectiontitle = get_string('week'); break;
106 case 'topics': $sectiontitle = get_string('topic'); break;
107 default: $sectiontitle = get_string('section'); break;
110 if (is_null($modinfo->groups
)) {
111 $modinfo->groups
= groups_get_user_groups($course->id
); // load all my groups and cache it in modinfo
114 $activities = array();
117 foreach ($sections as $section) {
119 $activity = new object();
120 $activity->type
= 'section';
121 if ($section->section
> 0) {
122 $activity->name
= $sectiontitle.' '.$section->section
;
124 $activity->name
= '';
127 $activity->visible
= $section->visible
;
128 $activities[$index++
] = $activity;
130 if (empty($section->sequence
)) {
134 $sectionmods = explode(",", $section->sequence
);
136 foreach ($sectionmods as $cmid) {
137 if (!isset($mods[$cmid]) or !isset($modinfo->cms
[$cmid])) {
141 $cm = $modinfo->cms
[$cmid];
143 if (!$cm->uservisible
) {
147 if (!empty($filter) and $cm->modname
!= $filter) {
151 $libfile = "$CFG->dirroot/mod/$cm->modname/lib.php";
153 if (file_exists($libfile)) {
154 require_once($libfile);
155 $get_recent_mod_activity = $cm->modname
."_get_recent_mod_activity";
157 if (function_exists($get_recent_mod_activity)) {
158 $activity = new object();
159 $activity->type
= 'activity';
160 $activity->cmid
= $cmid;
161 $activities[$index++
] = $activity;
162 $get_recent_mod_activity($activities, $index, $param->date
, $course->id
, $cmid, $param->user
, $param->group
);
170 switch ($param->sortby
) {
171 case 'datedesc' : usort($activities, 'compare_activities_by_time_desc'); break;
172 case 'dateasc' : usort($activities, 'compare_activities_by_time_asc'); break;
174 default : $detail = false; $param->sortby
= 'default';
178 if (!empty($activities)) {
188 $activity_count = count($activities);
189 $viewfullnames = array();
191 foreach ($activities as $key => $activity) {
193 if ($activity->type
== 'section') {
194 if ($param->sortby
!= 'default') {
195 continue; // no section if ordering by date
197 if ($activity_count == ($key +
1) or $activities[$key+
1]->type
== 'section') {
198 // peak at next activity. If it's another section, don't print this one!
199 // this means there are no activities in the current section
204 if (($activity->type
== 'section') && ($param->sortby
== 'default')) {
206 print_simple_box_end();
209 print_simple_box_start('center', '90%');
210 echo "<h2>$activity->name</h2>";
213 } else if ($activity->type
== 'activity') {
215 if ($param->sortby
== 'default') {
216 $cm = $modinfo->cms
[$activity->cmid
];
221 $linkformat = 'class="dimmed"';
223 $name = format_string($cm->name
);
224 $modfullname = $modnames[$cm->modname
];
226 $image = "<img src=\"$CFG->modpixpath/$cm->modname/icon.gif\" class=\"icon\" alt=\"$modfullname\" />";
227 echo "<h4>$image $modfullname".
228 " <a href=\"$CFG->wwwroot/mod/$cm->modname/view.php?id=$cm->id\" $linkformat>$name</a></h4>";
233 if (!isset($viewfullnames[$activity->cmid
])) {
234 $cm_context = get_context_instance(CONTEXT_MODULE
, $activity->cmid
);
235 $viewfullnames[$activity->cmid
] = has_capability('moodle/site:viewfullnames', $cm_context);
239 print_simple_box_start('center', '90%');
243 $print_recent_mod_activity = $activity->type
.'_print_recent_mod_activity';
245 if (function_exists($print_recent_mod_activity)) {
246 $print_recent_mod_activity($activity, $course->id
, $detail, $modnames, $viewfullnames[$activity->cmid
]);
252 print_simple_box_end();
258 echo '<h4><center>' . get_string('norecentactivity') . '</center></h2>';
262 print_footer($course);
264 function compare_activities_by_time_desc($a, $b) {
265 // make sure the activities actually have a timestamp property
266 if ((!array_key_exists('timestamp', $a)) or (!array_key_exists('timestamp', $b))) {
269 if ($a->timestamp
== $b->timestamp
)
271 return ($a->timestamp
> $b->timestamp
) ?
-1 : 1;
274 function compare_activities_by_time_asc($a, $b) {
275 // make sure the activities actually have a timestamp property
276 if ((!array_key_exists('timestamp', $a)) or (!array_key_exists('timestamp', $b))) {
279 if ($a->timestamp
== $b->timestamp
)
281 return ($a->timestamp
< $b->timestamp
) ?
-1 : 1;