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] [Versions 401 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   * This file contains functions used by the participation reports
  19   *
  20   * @package   report_participation
  21   * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Returns log table name of preferred reader, if leagcy then return empty string.
  29   *
  30   * @return string table name
  31   */
  32  function report_participation_get_log_table_name() {
  33      // Get prefered sql_internal_table_reader reader (if enabled).
  34      $logmanager = get_log_manager();
  35      $readers = $logmanager->get_readers();
  36      $logtable = '';
  37  
  38      // Get preferred reader.
  39      if (!empty($readers)) {
  40          foreach ($readers as $readerpluginname => $reader) {
  41              // If sql_internal_table_reader is preferred reader.
  42              if ($reader instanceof \core\log\sql_internal_table_reader) {
  43                  $logtable = $reader->get_internal_log_table_name();
  44                  break;
  45              }
  46          }
  47      }
  48      return $logtable;
  49  }
  50  
  51  /**
  52   * Return time options, which should be shown for record filtering.
  53   *
  54   * @param int $minlog Time of first log record available.
  55   * @return array time options.
  56   */
  57  function report_participation_get_time_options($minlog) {
  58      $timeoptions = array();
  59      $now = usergetmidnight(time());
  60  
  61      // Days.
  62      for ($i = 1; $i < 7; $i++) {
  63          if (strtotime('-'.$i.' days',$now) >= $minlog) {
  64              $timeoptions[strtotime('-'.$i.' days',$now)] = get_string('numdays','moodle',$i);
  65          }
  66      }
  67      // Weeks.
  68      for ($i = 1; $i < 10; $i++) {
  69          if (strtotime('-'.$i.' weeks',$now) >= $minlog) {
  70              $timeoptions[strtotime('-'.$i.' weeks',$now)] = get_string('numweeks','moodle',$i);
  71          }
  72      }
  73      // Months.
  74      for ($i = 2; $i < 12; $i++) {
  75          if (strtotime('-'.$i.' months',$now) >= $minlog) {
  76              $timeoptions[strtotime('-'.$i.' months',$now)] = get_string('nummonths','moodle',$i);
  77          }
  78      }
  79      // Try a year.
  80      if (strtotime('-1 year',$now) >= $minlog) {
  81          $timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
  82      }
  83      return $timeoptions;
  84  }
  85  
  86  /**
  87   * Return action sql and params.
  88   *
  89   * @param string $action action to be filtered.
  90   * @param string $modname module name.
  91   * @return array actionsql and actionparams.
  92   */
  93  function report_participation_get_action_sql($action, $modname) {
  94      global $CFG, $DB;
  95  
  96      $actionsql = '';
  97      $actionparams = array();
  98  
  99      $viewnames = array();
 100      $postnames = array();
 101      include_once($CFG->dirroot.'/mod/' . $modname . '/lib.php');
 102  
 103      $viewfun = $modname.'_get_view_actions';
 104      $postfun = $modname.'_get_post_actions';
 105  
 106      if (function_exists($viewfun)) {
 107          $viewnames = $viewfun();
 108      }
 109  
 110      if (function_exists($postfun)) {
 111          $postnames = $postfun();
 112      }
 113  
 114      switch ($action) {
 115          case 'view':
 116              $actions = $viewnames;
 117              break;
 118          case 'post':
 119              $actions = $postnames;
 120              break;
 121          default:
 122              // Some modules have stuff we want to hide, ie mail blocked etc so do actually need to limit here.
 123              $actions = array_merge($viewnames, $postnames);
 124      }
 125  
 126      if (!empty($actions)) {
 127          list($actionsql, $actionparams) = $DB->get_in_or_equal($actions, SQL_PARAMS_NAMED, 'action');
 128          $actionsql = " AND action $actionsql";
 129      }
 130  
 131      return array($actionsql, $actionparams);
 132  }
 133  
 134  /**
 135   * Return crud sql and params.
 136   *
 137   * @param string $action action to be filtered.
 138   * @return array crudsql and crudparams.
 139   */
 140  function report_participation_get_crud_sql($action) {
 141      global $DB;
 142  
 143      switch ($action) {
 144          case 'view':
 145              $crud = 'r';
 146              break;
 147          case 'post':
 148              $crud = array('c', 'u', 'd');
 149              break;
 150          default:
 151              $crud = array('c', 'r', 'u', 'd');
 152      }
 153  
 154      list($crudsql, $crudparams) = $DB->get_in_or_equal($crud, SQL_PARAMS_NAMED, 'crud');
 155      $crudsql = " AND crud " . $crudsql;
 156      return array($crudsql, $crudparams);
 157  }
 158  
 159  /**
 160   * List of action filters.
 161   *
 162   * @return array
 163   */
 164  function report_participation_get_action_options() {
 165      return array('' => get_string('allactions'),
 166              'view' => get_string('view'),
 167              'post' => get_string('post'),);
 168  }
 169  
 170  /**
 171   * Print filter form.
 172   *
 173   * @param stdClass $course course object.
 174   * @param int $timefrom Time from which records should be fetched.
 175   * @param int $minlog Time of first record present in log store.
 176   * @param string $action action to be filtered.
 177   * @param int $roleid Role to be filtered.
 178   * @param int $instanceid Instance id of module.
 179   */
 180  function report_participation_print_filter_form($course, $timefrom, $minlog, $action, $roleid, $instanceid) {
 181      global $DB;
 182  
 183      $timeoptions = report_participation_get_time_options($minlog);
 184  
 185      $actionoptions = report_participation_get_action_options();
 186  
 187      $context = context_course::instance($course->id);
 188      $roles = get_roles_used_in_context($context);
 189      $rolesviewable = get_viewable_roles($context);
 190  
 191      $guestrole = get_guest_role();
 192      $roleoptions = array_intersect_key($rolesviewable, $roles) + [
 193          $guestrole->id => role_get_name($guestrole, $context),
 194      ];
 195  
 196      $modinfo = get_fast_modinfo($course);
 197  
 198      $modules = $DB->get_records_select('modules', "visible = 1", null, 'name ASC');
 199  
 200      $instanceoptions = array();
 201      foreach ($modules as $module) {
 202          if (empty($modinfo->instances[$module->name])) {
 203              continue;
 204          }
 205          $instances = array();
 206          foreach ($modinfo->instances[$module->name] as $cm) {
 207              // Skip modules such as label which do not actually have links;
 208              // this means there's nothing to participate in.
 209              if (!$cm->has_view()) {
 210                  continue;
 211              }
 212              $instances[$cm->id] = format_string($cm->name);
 213          }
 214          if (count($instances) == 0) {
 215              continue;
 216          }
 217          $instanceoptions[] = array(get_string('modulenameplural', $module->name)=>$instances);
 218      }
 219  
 220      echo '<form class="participationselectform form-inline" action="index.php" method="get"><div>'."\n".
 221          '<input type="hidden" name="id" value="'.$course->id.'" />'."\n";
 222      echo '<label for="menuinstanceid">'.get_string('activitymodule').'</label>'."\n";
 223      echo html_writer::select($instanceoptions, 'instanceid', $instanceid);
 224      echo '<label for="menutimefrom">'.get_string('lookback').'</label>'."\n";
 225      echo html_writer::select($timeoptions,'timefrom',$timefrom);
 226      echo '<label for="menuroleid">'.get_string('showonly').'</label>'."\n";
 227      echo html_writer::select($roleoptions,'roleid',$roleid,false);
 228      echo '<label for="menuaction">'.get_string('showactions').'</label>'."\n";
 229      echo html_writer::select($actionoptions, 'action', $action, false, ['class' => 'mr-1']);
 230      echo '<input type="submit" value="'.get_string('go').'" class="btn btn-primary"/>'."\n</div></form>\n";
 231  }