Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   1  <?php
   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   * Display user activity reports for a course (totals)
  19   *
  20   * @package    report
  21   * @subpackage outline
  22   * @copyright  1999 onwards Martin Dougiamas  http://dougiamas.com
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require('../../config.php');
  27  require_once($CFG->dirroot.'/report/outline/locallib.php');
  28  require_once($CFG->dirroot.'/report/outline/lib.php');
  29  
  30  $userid   = required_param('id', PARAM_INT);
  31  $courseid = required_param('course', PARAM_INT);
  32  $mode     = optional_param('mode', 'outline', PARAM_ALPHA);
  33  
  34  if ($mode !== 'complete' and $mode !== 'outline') {
  35      $mode = 'outline';
  36  }
  37  
  38  $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
  39  $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
  40  
  41  $coursecontext   = context_course::instance($course->id);
  42  $personalcontext = context_user::instance($user->id);
  43  
  44  if ($courseid == SITEID) {
  45      $PAGE->set_context($personalcontext);
  46  }
  47  
  48  if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
  49          and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
  50      //TODO: do not require parents to be enrolled in courses - this is a hack!
  51      require_login();
  52      $PAGE->set_course($course);
  53  } else {
  54      require_login($course);
  55  }
  56  $PAGE->set_url('/report/outline/user.php', array('id'=>$userid, 'course'=>$courseid, 'mode'=>$mode));
  57  
  58  if (!report_outline_can_access_user_report($user, $course)) {
  59      throw new \moodle_exception('nocapability', 'report_outline');
  60  }
  61  
  62  $stractivityreport = get_string('activityreport');
  63  
  64  $PAGE->set_pagelayout('report');
  65  $PAGE->set_url('/report/outline/user.php', array('id'=>$user->id, 'course'=>$course->id, 'mode'=>$mode));
  66  $PAGE->navigation->extend_for_user($user);
  67  $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
  68  $PAGE->set_title("$course->shortname: $stractivityreport");
  69  
  70  // Create the appropriate breadcrumb.
  71  $navigationnode = array(
  72          'url' => new moodle_url('/report/outline/user.php', array('id' => $user->id, 'course' => $course->id, 'mode' => $mode))
  73      );
  74  if ($mode === 'complete') {
  75      $navigationnode['name'] = get_string('completereport');
  76  } else {
  77      $navigationnode['name'] = get_string('outlinereport');
  78  }
  79  $PAGE->add_report_nodes($user->id, $navigationnode);
  80  
  81  if ($courseid == SITEID) {
  82      $PAGE->set_heading(fullname($user));
  83  } else {
  84      $PAGE->set_heading($course->fullname);
  85  }
  86  
  87  // Trigger a report viewed event.
  88  $event = \report_outline\event\report_viewed::create(array('context' => context_course::instance($course->id),
  89          'relateduserid' => $userid, 'other' => array('mode' => $mode)));
  90  $event->trigger();
  91  
  92  echo $OUTPUT->header();
  93  if ($courseid != SITEID) {
  94      $backurl = new moodle_url('/user/view.php', ['id' => $userid, 'course' => $courseid]);
  95      echo $OUTPUT->single_button($backurl, get_string('back'), 'get', ['class' => 'mb-3']);
  96      echo $OUTPUT->context_header(
  97              array(
  98              'heading' => fullname($user),
  99              'user' => $user,
 100              'usercontext' => $personalcontext
 101          ), 2);
 102      if ($mode === 'outline') {
 103          echo $OUTPUT->heading(get_string('outlinereport', 'moodle'), 2, 'main mt-4 mb-4');
 104      } else {
 105          echo $OUTPUT->heading(get_string('completereport', 'moodle'), 2, 'main mt-4 mb-4');
 106      }
 107  }
 108  
 109  $modinfo = get_fast_modinfo($course, $user->id);
 110  $sections = $modinfo->get_section_info_all();
 111  $itemsprinted = false;
 112  
 113  foreach ($sections as $i => $section) {
 114  
 115          if ($section->uservisible) { // prevent hidden sections in user activity. Thanks to Geoff Wilbert!
 116              // Check the section has modules/resources, if not there is nothing to display.
 117              if (!empty($modinfo->sections[$i])) {
 118                  $itemsprinted = true;
 119                  echo '<div class="section">';
 120                  echo '<h2>';
 121                  echo get_section_name($course, $section);
 122                  echo "</h2>";
 123  
 124                  echo '<div class="content">';
 125  
 126                  if ($mode == "outline") {
 127                      echo "<table cellpadding=\"4\" cellspacing=\"0\">";
 128                  }
 129  
 130                  foreach ($modinfo->sections[$i] as $cmid) {
 131                      $mod = $modinfo->cms[$cmid];
 132  
 133                      if (empty($mod->uservisible)) {
 134                          continue;
 135                      }
 136  
 137                      $instance = $DB->get_record("$mod->modname", array("id"=>$mod->instance));
 138                      $libfile = "$CFG->dirroot/mod/$mod->modname/lib.php";
 139  
 140                      if (file_exists($libfile)) {
 141                          require_once($libfile);
 142  
 143                          switch ($mode) {
 144                              case "outline":
 145                                  $user_outline = $mod->modname."_user_outline";
 146                                  if (function_exists($user_outline)) {
 147                                      $output = $user_outline($course, $user, $mod, $instance);
 148                                  } else {
 149                                      $output = report_outline_user_outline($user->id, $cmid, $mod->modname, $instance->id);
 150                                  }
 151                                  report_outline_print_row($mod, $instance, $output);
 152                                  break;
 153                              case "complete":
 154                                  $user_complete = $mod->modname."_user_complete";
 155                                  $image = $OUTPUT->pix_icon('monologo', $mod->modfullname, 'mod_'.$mod->modname, array('class'=>'icon'));
 156                                  echo "<h4>$image $mod->modfullname: ".
 157                                       "<a href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
 158                                       format_string($instance->name,true)."</a></h4>";
 159  
 160                                  ob_start();
 161  
 162                                  echo "<ul>";
 163                                  if (function_exists($user_complete)) {
 164                                      $user_complete($course, $user, $mod, $instance);
 165                                  } else {
 166                                      echo report_outline_user_complete($user->id, $cmid, $mod->modname, $instance->id);
 167                                  }
 168                                  echo "</ul>";
 169  
 170                                  $output = ob_get_contents();
 171                                  ob_end_clean();
 172  
 173                                  if (str_replace(' ', '', $output) != '<ul></ul>') {
 174                                      echo $output;
 175                                  }
 176                                  break;
 177                              }
 178                          }
 179                      }
 180  
 181                  if ($mode == "outline") {
 182                      echo "</table>";
 183                  }
 184                  echo '</div>';  // content
 185                  echo '</div>';  // section
 186              }
 187          }
 188  }
 189  
 190  if (!$itemsprinted) {
 191      echo $OUTPUT->notification(get_string('nothingtodisplay'));
 192  }
 193  
 194  echo $OUTPUT->footer();