MDL-36968 Move function print_recent_activity() to the block recent_activity
[moodle.git] / blocks / recent_activity / block_recent_activity.php
CommitLineData
4ca6cfbf 1<?php
a3f66bde
MG
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.
13//
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/>.
16
17/**
18 * class block_recent_activity
19 *
20 * @package block_recent_activity
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
0f3fe4b6 24
b5042ff7
SH
25require_once($CFG->dirroot.'/course/lib.php');
26
a3f66bde
MG
27/**
28 * class block_recent_activity
29 *
30 * @package block_recent_activity
31 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
e89d741a 34class block_recent_activity extends block_base {
a3f66bde
MG
35
36 /**
37 * Use {@link block_recent_activity::get_timestart()} to access
38 *
39 * @var int stores the time since when we want to show recent activity
40 */
41 protected $timestart = null;
42
43 /**
44 * Initialises the block
45 */
9b4b78fd 46 function init() {
83d0f6ed 47 $this->title = get_string('pluginname', 'block_recent_activity');
0f3fe4b6 48 }
49
a3f66bde
MG
50 /**
51 * Returns the content object
52 *
53 * @return stdObject
54 */
0f3fe4b6 55 function get_content() {
675dbdd9 56 if ($this->content !== NULL) {
57 return $this->content;
58 }
59
9b4b78fd 60 if (empty($this->instance)) {
675dbdd9 61 $this->content = '';
0f3fe4b6 62 return $this->content;
63 }
64
9b4b78fd 65 $this->content = new stdClass;
0f3fe4b6 66 $this->content->text = '';
67 $this->content->footer = '';
68
a3f66bde
MG
69 $renderer = $this->page->get_renderer('block_recent_activity');
70 $this->content->text = $renderer->recent_activity($this->page->course,
71 $this->get_timestart(),
72 $this->get_recent_enrolments(),
73 $this->get_structural_changes(),
74 $this->get_modules_recent_activity());
0f3fe4b6 75
76 return $this->content;
77 }
0f3fe4b6 78
a3f66bde
MG
79 /**
80 * Returns the time since when we want to show recent activity
81 *
82 * For guest users it is 2 days, for registered users it is the time of last access to the course
83 *
84 * @return int
85 */
86 protected function get_timestart() {
87 global $USER;
88 if ($this->timestart === null) {
89 $this->timestart = round(time() - COURSE_MAX_RECENT_PERIOD, -2); // better db caching for guests - 100 seconds
90
91 if (!isguestuser()) {
92 if (!empty($USER->lastcourseaccess[$this->page->course->id])) {
93 if ($USER->lastcourseaccess[$this->page->course->id] > $this->timestart) {
94 $this->timestart = $USER->lastcourseaccess[$this->page->course->id];
95 }
96 }
97 }
98 }
99 return $this->timestart;
100 }
101
102 /**
103 * Returns all recent enrollments
104 *
105 * @todo MDL-36993 this function always return empty array
106 * @return array array of entries from {user} table
107 */
108 protected function get_recent_enrolments() {
109 return get_recent_enrolments($this->page->course->id, $this->get_timestart());
110 }
111
112 /**
113 * Returns list of recent changes in course structure
114 *
115 * It includes adding, editing or deleting of the resources or activities
116 * Excludes changes on labels, and also if activity was both added and deleted
117 *
118 * @return array array of changes. Each element is an array containing attributes:
119 * 'action' - one of: 'add mod', 'update mod', 'delete mod'
120 * 'module' - instance of cm_info (for 'delete mod' it is an object with attributes modname and modfullname)
121 */
122 protected function get_structural_changes() {
123 global $DB, $CFG;
124 $course = $this->page->course;
125 $timestart = $this->get_timestart();
126 $changelist = array();
127 $logs = $DB->get_records_select('log',
128 "time > ? AND course = ? AND
129 module = 'course' AND
130 (action = 'add mod' OR action = 'update mod' OR action = 'delete mod')",
131 array($timestart, $course->id), "id ASC");
132 if ($logs) {
133 $modinfo = get_fast_modinfo($course);
134 $newgones = array(); // added and later deleted items
135 foreach ($logs as $key => $log) {
136 $info = explode(' ', $log->info);
137
138 // note: in most cases I replaced hardcoding of label with use of
139 // $cm->has_view() but it was not possible to do this here because
140 // we don't necessarily have the $cm for it
141 if ($info[0] == 'label') { // Labels are ignored in recent activity
142 continue;
143 }
144
145 if (count($info) != 2) {
146 debugging("Incorrect log entry info: id = ".$log->id, DEBUG_DEVELOPER);
147 continue;
148 }
149
150 $modname = $info[0];
151 $instanceid = $info[1];
152
153 if ($log->action == 'delete mod') {
154 // unfortunately we do not know if the mod was visible
155 if (!array_key_exists($log->info, $newgones)) {
156 $changelist[$log->info] = array('action' => $log->action,
157 'module' => (object)array(
158 'modname' => $modname,
159 'modfullname' => get_string('modulename', $modname)
160 ));
161 }
162 } else {
163 if (!isset($modinfo->instances[$modname][$instanceid])) {
164 if ($log->action == 'add mod') {
165 // do not display added and later deleted activities
166 $newgones[$log->info] = true;
167 }
168 continue;
169 }
170 $cm = $modinfo->instances[$modname][$instanceid];
171 if ($cm->uservisible && empty($changelist[$log->info])) {
172 $changelist[$log->info] = array('action' => $log->action, 'module' => $cm);
173 }
174 }
175 }
176 }
177 return $changelist;
178 }
179
180 /**
181 * Returns list of recent activity within modules
182 *
183 * For each used module type executes callback MODULE_print_recent_activity()
184 *
185 * @return array array of pairs moduletype => content
186 */
187 protected function get_modules_recent_activity() {
188 $context = context_course::instance($this->page->course->id);
189 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
190 $hascontent = false;
191
192 $modinfo = get_fast_modinfo($this->page->course);
193 $usedmodules = $modinfo->get_used_module_names();
194 $recentactivity = array();
195 foreach ($usedmodules as $modname => $modfullname) {
196 // Each module gets it's own logs and prints them
197 ob_start();
198 $hascontent = component_callback('mod_'. $modname, 'print_recent_activity',
199 array($this->page->course, $viewfullnames, $this->get_timestart()), false);
200 if ($hascontent) {
201 $recentactivity[$modname] = ob_get_contents();
202 }
203 ob_end_clean();
204 }
205 return $recentactivity;
206 }
207
208 /**
209 * Which page types this block may appear on.
210 *
211 * @return array page-type prefix => true/false.
212 */
347d7d27 213 function applicable_formats() {
9591bc3c 214 return array('all' => true, 'my' => false, 'tag' => false);
347d7d27 215 }
216}
4ca6cfbf 217