From a3f66bdeac7294447d14120fef4cc955a5ad7fe3 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Wed, 5 Dec 2012 09:55:16 +0800 Subject: [PATCH] MDL-36968 Move function print_recent_activity() to the block recent_activity --- .../recent_activity/block_recent_activity.php | 190 +++++++++++++++++- blocks/recent_activity/renderer.php | 128 ++++++++++++ blocks/recent_activity/version.php | 3 +- course/lib.php | 155 -------------- lib/deprecatedlib.php | 165 +++++++++++++++ mod/chat/lib.php | 2 +- 6 files changed, 480 insertions(+), 163 deletions(-) create mode 100644 blocks/recent_activity/renderer.php diff --git a/blocks/recent_activity/block_recent_activity.php b/blocks/recent_activity/block_recent_activity.php index 83b146afff7..846ab9816ed 100644 --- a/blocks/recent_activity/block_recent_activity.php +++ b/blocks/recent_activity/block_recent_activity.php @@ -1,12 +1,57 @@ . + +/** + * class block_recent_activity + * + * @package block_recent_activity + * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ require_once($CFG->dirroot.'/course/lib.php'); +/** + * class block_recent_activity + * + * @package block_recent_activity + * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ class block_recent_activity extends block_base { + + /** + * Use {@link block_recent_activity::get_timestart()} to access + * + * @var int stores the time since when we want to show recent activity + */ + protected $timestart = null; + + /** + * Initialises the block + */ function init() { $this->title = get_string('pluginname', 'block_recent_activity'); } + /** + * Returns the content object + * + * @return stdObject + */ function get_content() { if ($this->content !== NULL) { return $this->content; @@ -21,15 +66,150 @@ class block_recent_activity extends block_base { $this->content->text = ''; $this->content->footer = ''; - // Slightly hacky way to do it but... - ob_start(); - print_recent_activity($this->page->course); - $this->content->text = ob_get_contents(); - ob_end_clean(); + $renderer = $this->page->get_renderer('block_recent_activity'); + $this->content->text = $renderer->recent_activity($this->page->course, + $this->get_timestart(), + $this->get_recent_enrolments(), + $this->get_structural_changes(), + $this->get_modules_recent_activity()); return $this->content; } + /** + * Returns the time since when we want to show recent activity + * + * For guest users it is 2 days, for registered users it is the time of last access to the course + * + * @return int + */ + protected function get_timestart() { + global $USER; + if ($this->timestart === null) { + $this->timestart = round(time() - COURSE_MAX_RECENT_PERIOD, -2); // better db caching for guests - 100 seconds + + if (!isguestuser()) { + if (!empty($USER->lastcourseaccess[$this->page->course->id])) { + if ($USER->lastcourseaccess[$this->page->course->id] > $this->timestart) { + $this->timestart = $USER->lastcourseaccess[$this->page->course->id]; + } + } + } + } + return $this->timestart; + } + + /** + * Returns all recent enrollments + * + * @todo MDL-36993 this function always return empty array + * @return array array of entries from {user} table + */ + protected function get_recent_enrolments() { + return get_recent_enrolments($this->page->course->id, $this->get_timestart()); + } + + /** + * Returns list of recent changes in course structure + * + * It includes adding, editing or deleting of the resources or activities + * Excludes changes on labels, and also if activity was both added and deleted + * + * @return array array of changes. Each element is an array containing attributes: + * 'action' - one of: 'add mod', 'update mod', 'delete mod' + * 'module' - instance of cm_info (for 'delete mod' it is an object with attributes modname and modfullname) + */ + protected function get_structural_changes() { + global $DB, $CFG; + $course = $this->page->course; + $timestart = $this->get_timestart(); + $changelist = array(); + $logs = $DB->get_records_select('log', + "time > ? AND course = ? AND + module = 'course' AND + (action = 'add mod' OR action = 'update mod' OR action = 'delete mod')", + array($timestart, $course->id), "id ASC"); + if ($logs) { + $modinfo = get_fast_modinfo($course); + $newgones = array(); // added and later deleted items + foreach ($logs as $key => $log) { + $info = explode(' ', $log->info); + + // note: in most cases I replaced hardcoding of label with use of + // $cm->has_view() but it was not possible to do this here because + // we don't necessarily have the $cm for it + if ($info[0] == 'label') { // Labels are ignored in recent activity + continue; + } + + if (count($info) != 2) { + debugging("Incorrect log entry info: id = ".$log->id, DEBUG_DEVELOPER); + continue; + } + + $modname = $info[0]; + $instanceid = $info[1]; + + if ($log->action == 'delete mod') { + // unfortunately we do not know if the mod was visible + if (!array_key_exists($log->info, $newgones)) { + $changelist[$log->info] = array('action' => $log->action, + 'module' => (object)array( + 'modname' => $modname, + 'modfullname' => get_string('modulename', $modname) + )); + } + } else { + if (!isset($modinfo->instances[$modname][$instanceid])) { + if ($log->action == 'add mod') { + // do not display added and later deleted activities + $newgones[$log->info] = true; + } + continue; + } + $cm = $modinfo->instances[$modname][$instanceid]; + if ($cm->uservisible && empty($changelist[$log->info])) { + $changelist[$log->info] = array('action' => $log->action, 'module' => $cm); + } + } + } + } + return $changelist; + } + + /** + * Returns list of recent activity within modules + * + * For each used module type executes callback MODULE_print_recent_activity() + * + * @return array array of pairs moduletype => content + */ + protected function get_modules_recent_activity() { + $context = context_course::instance($this->page->course->id); + $viewfullnames = has_capability('moodle/site:viewfullnames', $context); + $hascontent = false; + + $modinfo = get_fast_modinfo($this->page->course); + $usedmodules = $modinfo->get_used_module_names(); + $recentactivity = array(); + foreach ($usedmodules as $modname => $modfullname) { + // Each module gets it's own logs and prints them + ob_start(); + $hascontent = component_callback('mod_'. $modname, 'print_recent_activity', + array($this->page->course, $viewfullnames, $this->get_timestart()), false); + if ($hascontent) { + $recentactivity[$modname] = ob_get_contents(); + } + ob_end_clean(); + } + return $recentactivity; + } + + /** + * Which page types this block may appear on. + * + * @return array page-type prefix => true/false. + */ function applicable_formats() { return array('all' => true, 'my' => false, 'tag' => false); } diff --git a/blocks/recent_activity/renderer.php b/blocks/recent_activity/renderer.php new file mode 100644 index 00000000000..b77f58413d0 --- /dev/null +++ b/blocks/recent_activity/renderer.php @@ -0,0 +1,128 @@ +. + +/** + * Renderer for block recent_activity + * + * @package block_recent_activity + * @copyright 2012 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +/** + * recent_activity block rendrer + * + * @package block_recent_activity + * @copyright 2012 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class block_recent_activity_renderer extends plugin_renderer_base { + + /** + * Renders HTML to display recent_activity block + * + * @param stdClass $course + * @param int $timestart + * @param array $recentenrolments array of changes in enrolments + * @param array $structuralchanges array of changes in course structure + * @param array $modulesrecentactivity array of changes in modules (provided by modules) + * @return string + */ + public function recent_activity($course, $timestart, $recentenrolments, $structuralchanges, + $modulesrecentactivity) { + + $output = html_writer::tag('div', + get_string('activitysince', '', userdate($timestart)), + array('class' => 'activityhead')); + + $output .= html_writer::tag('div', + html_writer::link(new moodle_url('/course/recent.php', array('id' => $course->id)), + get_string('recentactivityreport')), + array('class' => 'activityhead')); + + $content = false; + + // Firstly, have there been any new enrolments? + if ($recentenrolments) { + $content = true; + $context = context_course::instance($course->id); + $viewfullnames = has_capability('moodle/site:viewfullnames', $context); + $output .= html_writer::start_tag('div', array('class' => 'newusers')); + $output .= $this->heading(get_string("newusers").':', 3); + //Accessibility: new users now appear in an
    list. + $output .= html_writer::start_tag('ol', array('class' => 'list')); + foreach ($recentenrolments as $user) { + $output .= html_writer::tag('li', + html_writer::link(new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id)), + fullname($user, $viewfullnames)), + array('class' => 'name')); + } + $output .= html_writer::end_tag('ol'); + $output .= html_writer::end_tag('div'); + } + + // Next, have there been any modifications to the course structure? + if (!empty($structuralchanges)) { + $content = true; + $output .= $this->heading(get_string("courseupdates").':', 3); + foreach ($structuralchanges as $changeinfo => $change) { + $output .= $this->structural_change($change); + } + } + + // Now display new things from each module + foreach ($modulesrecentactivity as $modname => $moduleactivity) { + $content = true; + $output .= $moduleactivity; + } + + if (! $content) { + $output .= html_writer::tag('p', get_string('nothingnew'), array('class' => 'message')); + } + return $output; + } + + /** + * Renders HTML for one change in course structure + * + * @see block_recent_activity::get_structural_changes() + * @param array $change array containing attributes + * 'action' - one of: 'add mod', 'update mod', 'delete mod' + * 'module' - instance of cm_info (for 'delete mod' it is an object with attributes modname and modfullname) + * @return string + */ + protected function structural_change($change) { + $cm = $change['module']; + switch ($change['action']) { + case 'delete mod': + $text = get_string('deletedactivity', 'moodle', $cm->modfullname); + break; + case 'add mod': + $text = get_string('added', 'moodle', $cm->modfullname). '
    '. + html_writer::link($cm->get_url(), format_string($cm->name, true)); + break; + case 'update mod': + $text = get_string('updated', 'moodle', $cm->modfullname). '
    '. + html_writer::link($cm->get_url(), format_string($cm->name, true)); + break; + default: + return ''; + } + return html_writer::tag('p', $text, array('class' => 'activity')); + } +} diff --git a/blocks/recent_activity/version.php b/blocks/recent_activity/version.php index 1197d676e3f..4b549915846 100644 --- a/blocks/recent_activity/version.php +++ b/blocks/recent_activity/version.php @@ -17,8 +17,7 @@ /** * Version details * - * @package block - * @subpackage recent_activity + * @package block_recent_activity * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ diff --git a/course/lib.php b/course/lib.php index 29e0fa09f7d..2441d8f33c5 100644 --- a/course/lib.php +++ b/course/lib.php @@ -847,161 +847,6 @@ function print_log_ods($course, $user, $date, $order='l.time DESC', $modname, return true; } -/** - * This function trawls through the logs looking for - * anything new since the user's last login - */ -function print_recent_activity($course) { - // $course is an object - global $CFG, $USER, $SESSION, $DB, $OUTPUT; - - $context = context_course::instance($course->id); - - $viewfullnames = has_capability('moodle/site:viewfullnames', $context); - - $timestart = round(time() - COURSE_MAX_RECENT_PERIOD, -2); // better db caching for guests - 100 seconds - - if (!isguestuser()) { - if (!empty($USER->lastcourseaccess[$course->id])) { - if ($USER->lastcourseaccess[$course->id] > $timestart) { - $timestart = $USER->lastcourseaccess[$course->id]; - } - } - } - - echo '
    '; - echo get_string('activitysince', '', userdate($timestart)); - echo '
    '; - echo '
    '; - - echo ''.get_string('recentactivityreport').''; - - echo "
    \n"; - - $content = false; - -/// Firstly, have there been any new enrolments? - - $users = get_recent_enrolments($course->id, $timestart); - - //Accessibility: new users now appear in an
      list. - if ($users) { - echo '
      '; - echo $OUTPUT->heading(get_string("newusers").':', 3); - $content = true; - echo "
        \n"; - foreach ($users as $user) { - $fullname = fullname($user, $viewfullnames); - echo '
      1. wwwroot/user/view.php?id=$user->id&course=$course->id\">$fullname
      2. \n"; - } - echo "
      \n
      \n"; - } - -/// Next, have there been any modifications to the course structure? - - $modinfo = get_fast_modinfo($course); - - $changelist = array(); - - $logs = $DB->get_records_select('log', "time > ? AND course = ? AND - module = 'course' AND - (action = 'add mod' OR action = 'update mod' OR action = 'delete mod')", - array($timestart, $course->id), "id ASC"); - - if ($logs) { - $actions = array('add mod', 'update mod', 'delete mod'); - $newgones = array(); // added and later deleted items - foreach ($logs as $key => $log) { - if (!in_array($log->action, $actions)) { - continue; - } - $info = explode(' ', $log->info); - - // note: in most cases I replaced hardcoding of label with use of - // $cm->has_view() but it was not possible to do this here because - // we don't necessarily have the $cm for it - if ($info[0] == 'label') { // Labels are ignored in recent activity - continue; - } - - if (count($info) != 2) { - debugging("Incorrect log entry info: id = ".$log->id, DEBUG_DEVELOPER); - continue; - } - - $modname = $info[0]; - $instanceid = $info[1]; - - if ($log->action == 'delete mod') { - // unfortunately we do not know if the mod was visible - if (!array_key_exists($log->info, $newgones)) { - $strdeleted = get_string('deletedactivity', 'moodle', get_string('modulename', $modname)); - $changelist[$log->info] = array ('operation' => 'delete', 'text' => $strdeleted); - } - } else { - if (!isset($modinfo->instances[$modname][$instanceid])) { - if ($log->action == 'add mod') { - // do not display added and later deleted activities - $newgones[$log->info] = true; - } - continue; - } - $cm = $modinfo->instances[$modname][$instanceid]; - if (!$cm->uservisible) { - continue; - } - - if ($log->action == 'add mod') { - $stradded = get_string('added', 'moodle', get_string('modulename', $modname)); - $changelist[$log->info] = array('operation' => 'add', 'text' => "$stradded:
      wwwroot/mod/$cm->modname/view.php?id={$cm->id}\">".format_string($cm->name, true).""); - - } else if ($log->action == 'update mod' and empty($changelist[$log->info])) { - $strupdated = get_string('updated', 'moodle', get_string('modulename', $modname)); - $changelist[$log->info] = array('operation' => 'update', 'text' => "$strupdated:
      wwwroot/mod/$cm->modname/view.php?id={$cm->id}\">".format_string($cm->name, true).""); - } - } - } - } - - if (!empty($changelist)) { - echo $OUTPUT->heading(get_string("courseupdates").':', 3); - $content = true; - foreach ($changelist as $changeinfo => $change) { - echo '

      '.$change['text'].'

      '; - } - } - -/// Now display new things from each module - - $usedmodules = array(); - foreach($modinfo->cms as $cm) { - if (isset($usedmodules[$cm->modname])) { - continue; - } - if (!$cm->uservisible) { - continue; - } - $usedmodules[$cm->modname] = $cm->modname; - } - - foreach ($usedmodules as $modname) { // Each module gets it's own logs and prints them - if (file_exists($CFG->dirroot.'/mod/'.$modname.'/lib.php')) { - include_once($CFG->dirroot.'/mod/'.$modname.'/lib.php'); - $print_recent_activity = $modname.'_print_recent_activity'; - if (function_exists($print_recent_activity)) { - // NOTE: original $isteacher (second parameter below) was replaced with $viewfullnames! - $content = $print_recent_activity($course, $viewfullnames, $timestart) || $content; - } - } else { - debugging("Missing lib.php in lib/{$modname} - please reinstall files or uninstall the module"); - } - } - - if (! $content) { - echo '

      '.get_string('nothingnew').'

      '; - } -} - /** * For a given course, returns an array of course activity objects * Each item in the array contains he following properties: diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index aa16cd25f7b..327b03d48ed 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -351,6 +351,7 @@ function is_course_participant($userid, $courseid) { * * used to print recent activity * + * @todo MDL-36993 this function is still used in block_recent_activity, deprecate properly * @global object * @uses CONTEXT_COURSE * @param int $courseid The course in question. @@ -3272,3 +3273,167 @@ function print_overview($courses, array $remote_courses=array()) { echo $OUTPUT->box_end(); } } + +/** + * This function trawls through the logs looking for + * anything new since the user's last login + * + * This function was only used to print the content of block recent_activity + * All functionality is moved into class {@link block_recent_activity} + * and renderer {@link block_recent_activity_renderer} + * + * @deprecated since 2.5 + * @param stdClass $course + */ +function print_recent_activity($course) { + // $course is an object + global $CFG, $USER, $SESSION, $DB, $OUTPUT; + debugging('Function print_recent_activity() is deprecated. It is not recommended to'. + ' use it outside of block_recent_activity', DEBUG_DEVELOPER); + + $context = context_course::instance($course->id); + + $viewfullnames = has_capability('moodle/site:viewfullnames', $context); + + $timestart = round(time() - COURSE_MAX_RECENT_PERIOD, -2); // better db caching for guests - 100 seconds + + if (!isguestuser()) { + if (!empty($USER->lastcourseaccess[$course->id])) { + if ($USER->lastcourseaccess[$course->id] > $timestart) { + $timestart = $USER->lastcourseaccess[$course->id]; + } + } + } + + echo '
      '; + echo get_string('activitysince', '', userdate($timestart)); + echo '
      '; + echo '
      '; + + echo ''.get_string('recentactivityreport').''; + + echo "
      \n"; + + $content = false; + +/// Firstly, have there been any new enrolments? + + $users = get_recent_enrolments($course->id, $timestart); + + //Accessibility: new users now appear in an
        list. + if ($users) { + echo '
        '; + echo $OUTPUT->heading(get_string("newusers").':', 3); + $content = true; + echo "
          \n"; + foreach ($users as $user) { + $fullname = fullname($user, $viewfullnames); + echo '
        1. wwwroot/user/view.php?id=$user->id&course=$course->id\">$fullname
        2. \n"; + } + echo "
        \n
        \n"; + } + +/// Next, have there been any modifications to the course structure? + + $modinfo = get_fast_modinfo($course); + + $changelist = array(); + + $logs = $DB->get_records_select('log', "time > ? AND course = ? AND + module = 'course' AND + (action = 'add mod' OR action = 'update mod' OR action = 'delete mod')", + array($timestart, $course->id), "id ASC"); + + if ($logs) { + $actions = array('add mod', 'update mod', 'delete mod'); + $newgones = array(); // added and later deleted items + foreach ($logs as $key => $log) { + if (!in_array($log->action, $actions)) { + continue; + } + $info = explode(' ', $log->info); + + // note: in most cases I replaced hardcoding of label with use of + // $cm->has_view() but it was not possible to do this here because + // we don't necessarily have the $cm for it + if ($info[0] == 'label') { // Labels are ignored in recent activity + continue; + } + + if (count($info) != 2) { + debugging("Incorrect log entry info: id = ".$log->id, DEBUG_DEVELOPER); + continue; + } + + $modname = $info[0]; + $instanceid = $info[1]; + + if ($log->action == 'delete mod') { + // unfortunately we do not know if the mod was visible + if (!array_key_exists($log->info, $newgones)) { + $strdeleted = get_string('deletedactivity', 'moodle', get_string('modulename', $modname)); + $changelist[$log->info] = array ('operation' => 'delete', 'text' => $strdeleted); + } + } else { + if (!isset($modinfo->instances[$modname][$instanceid])) { + if ($log->action == 'add mod') { + // do not display added and later deleted activities + $newgones[$log->info] = true; + } + continue; + } + $cm = $modinfo->instances[$modname][$instanceid]; + if (!$cm->uservisible) { + continue; + } + + if ($log->action == 'add mod') { + $stradded = get_string('added', 'moodle', get_string('modulename', $modname)); + $changelist[$log->info] = array('operation' => 'add', 'text' => "$stradded:
        wwwroot/mod/$cm->modname/view.php?id={$cm->id}\">".format_string($cm->name, true).""); + + } else if ($log->action == 'update mod' and empty($changelist[$log->info])) { + $strupdated = get_string('updated', 'moodle', get_string('modulename', $modname)); + $changelist[$log->info] = array('operation' => 'update', 'text' => "$strupdated:
        wwwroot/mod/$cm->modname/view.php?id={$cm->id}\">".format_string($cm->name, true).""); + } + } + } + } + + if (!empty($changelist)) { + echo $OUTPUT->heading(get_string("courseupdates").':', 3); + $content = true; + foreach ($changelist as $changeinfo => $change) { + echo '

        '.$change['text'].'

        '; + } + } + +/// Now display new things from each module + + $usedmodules = array(); + foreach($modinfo->cms as $cm) { + if (isset($usedmodules[$cm->modname])) { + continue; + } + if (!$cm->uservisible) { + continue; + } + $usedmodules[$cm->modname] = $cm->modname; + } + + foreach ($usedmodules as $modname) { // Each module gets it's own logs and prints them + if (file_exists($CFG->dirroot.'/mod/'.$modname.'/lib.php')) { + include_once($CFG->dirroot.'/mod/'.$modname.'/lib.php'); + $print_recent_activity = $modname.'_print_recent_activity'; + if (function_exists($print_recent_activity)) { + // NOTE: original $isteacher (second parameter below) was replaced with $viewfullnames! + $content = $print_recent_activity($course, $viewfullnames, $timestart) || $content; + } + } else { + debugging("Missing lib.php in lib/{$modname} - please reinstall files or uninstall the module"); + } + } + + if (! $content) { + echo '

        '.get_string('nothingnew').'

        '; + } +} diff --git a/mod/chat/lib.php b/mod/chat/lib.php index ed272cf7e86..4fcb912af01 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -238,7 +238,7 @@ function chat_user_complete($course, $user, $mod, $chat) { /** * Given a course and a date, prints a summary of all chat rooms past and present - * This function is called from course/lib.php: print_recent_activity() + * This function is called from block_recent_activity * * @global object * @global object -- 2.43.0