Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 402] [Versions 311 and 403]

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