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   * Course overview report
  19   *
  20   * @package    report
  21   * @subpackage courseoverview
  22   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once('../../config.php');
  27  require_once($CFG->dirroot.'/lib/statslib.php');
  28  require_once($CFG->libdir.'/adminlib.php');
  29  require_once($CFG->dirroot.'/report/courseoverview/locallib.php');
  30  
  31  $report     = optional_param('report', STATS_REPORT_ACTIVE_COURSES, PARAM_INT);
  32  $time       = optional_param('time', 0, PARAM_INT);
  33  $numcourses = optional_param('numcourses', 20, PARAM_INT);
  34  $systemcontext = context_system::instance();
  35  
  36  if (empty($CFG->enablestats)) {
  37      if (has_capability('moodle/site:config', $systemcontext)) {
  38          redirect("$CFG->wwwroot/$CFG->admin/search.php?query=enablestats", get_string('mustenablestats', 'admin'), 3);
  39      } else {
  40          throw new \moodle_exception('statsdisable');
  41      }
  42  }
  43  
  44  admin_externalpage_setup('reportcourseoverview', '', null, '', array('pagelayout'=>'report'));
  45  echo $OUTPUT->header();
  46  
  47  $course = get_site();
  48  stats_check_uptodate($course->id);
  49  
  50  $strreports = get_string('reports');
  51  $strcourseoverview = get_string('courseoverview');
  52  
  53  $reportoptions = stats_get_report_options($course->id,STATS_MODE_RANKED);
  54  
  55  $earliestday = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_daily}');
  56  $earliestweek = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_weekly}');
  57  $earliestmonth = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_monthly}');
  58  
  59  if (empty($earliestday)) $earliestday = time();
  60  if (empty($earliestweek)) $earliestweek = time();
  61  if (empty($earliestmonth)) $earliestmonth = time();
  62  
  63  $now = stats_get_base_daily();
  64  $lastweekend = stats_get_base_weekly();
  65  $lastmonthend = stats_get_base_monthly();
  66  
  67  $timeoptions = stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth);
  68  
  69  if (empty($timeoptions)) {
  70      throw new \moodle_exception('nostatstodisplay', 'error', $CFG->wwwroot.'/course/view.php?id='.$course->id);
  71  }
  72  
  73  echo html_writer::start_tag('form', array('action' => 'index.php', 'method' => 'post', 'class' => 'form-inline'));
  74  echo html_writer::start_tag('div');
  75  
  76  $table = new html_table();
  77  $table->width = '*';
  78  $table->align = array('left','left','left','left','left','left');
  79  
  80  $reporttypemenu = html_writer::label(get_string('statsreporttype'), 'menureport', false, array('class' => 'accesshide'));
  81  $reporttypemenu .= html_writer::select($reportoptions,'report',$report, false);
  82  $timeoptionsmenu = html_writer::label(get_string('time'), 'menutime', false, array('class' => 'accesshide'));
  83  $timeoptionsmenu .= html_writer::select($timeoptions,'time',$time, false);
  84  
  85  $table->data[] = array(get_string('statsreporttype'),$reporttypemenu,
  86                         get_string('statstimeperiod'),$timeoptionsmenu,
  87                         html_writer::label(get_string('numberofcourses'), 'numcourses', false, array('class' => 'accesshide')) .
  88                         html_writer::empty_tag('input', array('type' => 'text', 'class' => 'form-control',
  89                             'id' => 'numcourses', 'name' => 'numcourses', 'size' => '3', 'maxlength' => '2',
  90                             'value' => $numcourses)),
  91                         html_writer::empty_tag('input', array('type' => 'submit', 'class' => 'btn btn-secondary',
  92                             'value' => get_string('view'))));
  93  
  94  echo html_writer::table($table);
  95  echo html_writer::end_tag('div');
  96  echo html_writer::end_tag('form');
  97  
  98  echo $OUTPUT->heading($reportoptions[$report]);
  99  
 100  
 101  if (!empty($report) && !empty($time)) {
 102      $param = stats_get_parameters($time,$report,SITEID,STATS_MODE_RANKED);
 103      if (!empty($param->sql)) {
 104          $sql = $param->sql;
 105      } else {
 106          $sql = "SELECT courseid,".$param->fields."
 107                    FROM {".'stats_'.$param->table."}
 108                   WHERE timeend >= $param->timeafter AND stattype = 'activity' AND roleid = 0
 109                GROUP BY courseid
 110                         $param->extras
 111                ORDER BY $param->orderby";
 112      }
 113  
 114      $courses = $DB->get_records_sql($sql, $param->params, 0, $numcourses);
 115  
 116      if (empty($courses)) {
 117          echo $OUTPUT->notification(get_string('statsnodata'));
 118          echo '</td></tr></table>';
 119  
 120      } else {
 121          require_capability('report/courseoverview:view', $systemcontext);
 122          echo html_writer::start_div();
 123          report_courseoverview_print_chart($report, $time, $numcourses);
 124          echo html_writer::end_div();
 125  
 126          $table = new html_table();
 127          $table->align = array('left','center','center','center');
 128          $table->head = array(get_string('course'),$param->line1);
 129          if (!empty($param->line2)) {
 130              $table->head[] = $param->line2;
 131          }
 132          if (!empty($param->line3)) {
 133              $table->head[] = $param->line3;
 134          }
 135  
 136          foreach  ($courses as $c) {
 137              $a = array();
 138              $a[] = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$c->courseid.'">'.$DB->get_field('course', 'shortname', array('id'=>$c->courseid)).'</a>';
 139  
 140              $a[] = $c->line1;
 141              if (isset($c->line2)) {
 142                  $a[] = $c->line2;
 143              }
 144              if (isset($c->line3)) {
 145                  $a[] = round($c->line3,2);
 146              }
 147              $table->data[] = $a;
 148          }
 149          echo html_writer::table($table);
 150      }
 151  }
 152  echo $OUTPUT->footer();