Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  namespace gradereport_summary\local\systemreports;
  18  
  19  use gradereport_summary\local\entities\grade_items;
  20  use core_reportbuilder\local\helpers\database;
  21  use core_reportbuilder\system_report;
  22  
  23  /**
  24   * Grade summary system report class implementation
  25   *
  26   * @package    gradereport_summary
  27   * @copyright  2022 Ilya Tregubov <ilya@moodle.com>
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class summary extends system_report {
  31  
  32      /**
  33       * Initialise report, we need to set the main table, load our entities and set columns/filters
  34       */
  35      protected function initialise(): void {
  36          global $PAGE;
  37  
  38          // We need to ensure page context is always set, as required by output and string formatting.
  39          $course = get_course($this->get_context()->instanceid);
  40          $PAGE->set_context($this->get_context());
  41  
  42          // Our main entity, it contains all of the column definitions that we need.
  43          $entitymain = new grade_items($course);
  44          $entitymainalias = $entitymain->get_table_alias('grade_items');
  45  
  46          $this->set_main_table('grade_items', $entitymainalias);
  47          $this->add_entity($entitymain);
  48  
  49          $param1 = database::generate_param_name();
  50          $param2 = database::generate_param_name();
  51          $param3 = database::generate_param_name();
  52  
  53          // Exclude grade categories.
  54          // For now exclude course total as well.
  55          $wheresql = "$entitymainalias.courseid = :$param1";
  56          $wheresql .= " AND $entitymainalias.itemtype <> 'course'";
  57  
  58          // Not showing category items.
  59          $wheresql .= " AND $entitymainalias.itemtype <> 'category'";
  60  
  61          // Only value and scale grade types may be aggregated.
  62          $wheresql .= " AND ($entitymainalias.gradetype = :$param2 OR $entitymainalias.gradetype = :$param3)";
  63  
  64          $this->add_base_condition_sql($wheresql,
  65              [$param1 => $course->id, $param2 => GRADE_TYPE_VALUE, $param3 => GRADE_TYPE_SCALE]);
  66  
  67          // Now we can call our helper methods to add the content we want to include in the report.
  68          $this->add_columns();
  69          $this->add_filters();
  70  
  71      }
  72  
  73      /**
  74       * Validates access to view this report
  75       *
  76       * @return bool
  77       */
  78      protected function can_view(): bool {
  79          return has_capability('gradereport/summary:view', $this->get_context());
  80      }
  81  
  82      /**
  83       * Adds the columns we want to display in the report
  84       *
  85       * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
  86       * unique identifier
  87       */
  88      public function add_columns(): void {
  89          $columns = [
  90              'grade_items:name',
  91              'grade_items:average',
  92          ];
  93  
  94          $this->add_columns_from_entities($columns);
  95  
  96      }
  97  
  98      /**
  99       * Adds the filters we want to display in the report
 100       *
 101       * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
 102       * unique identifier
 103       */
 104      protected function add_filters(): void {
 105          $filters = [
 106              'grade_items:name',
 107          ];
 108  
 109          $this->add_filters_from_entities($filters);
 110      }
 111  }