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 311 and 400] [Versions 311 and 401] [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   * Front-end class.
  19   *
  20   * @package availability_grouping
  21   * @copyright 2014 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace availability_grouping;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Front-end class.
  31   *
  32   * @package availability_grouping
  33   * @copyright 2014 The Open University
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class frontend extends \core_availability\frontend {
  37      /** @var array Array of grouping info for course */
  38      protected $allgroupings;
  39      /** @var int Course id that $allgroupings is for */
  40      protected $allgroupingscourseid;
  41  
  42      protected function get_javascript_init_params($course, \cm_info $cm = null,
  43              \section_info $section = null) {
  44          // Get all groups for course.
  45          $groupings = $this->get_all_groupings($course->id);
  46  
  47          // Change to JS array format and return.
  48          $jsarray = array();
  49          $context = \context_course::instance($course->id);
  50          foreach ($groupings as $rec) {
  51              $jsarray[] = (object)array('id' => $rec->id, 'name' =>
  52                      format_string($rec->name, true, array('context' => $context)));
  53          }
  54          return array($jsarray);
  55      }
  56  
  57      /**
  58       * Gets all the groupings on the course.
  59       *
  60       * @param int $courseid Course id
  61       * @return array Array of grouping objects
  62       */
  63      protected function get_all_groupings($courseid) {
  64          global $DB;
  65          if ($courseid != $this->allgroupingscourseid) {
  66              $this->allgroupings = $DB->get_records('groupings',
  67                      array('courseid' => $courseid), 'id, name');
  68              $this->allgroupingscourseid = $courseid;
  69          }
  70          return $this->allgroupings;
  71      }
  72  
  73      protected function allow_add($course, \cm_info $cm = null,
  74              \section_info $section = null) {
  75          global $CFG, $DB;
  76  
  77          // Check if groupings are in use for the course. (Unlike the 'group'
  78          // condition there is no case where you might want to set up the
  79          // condition before you set a grouping - there is no 'any grouping'
  80          // option.)
  81          return count($this->get_all_groupings($course->id)) > 0;
  82      }
  83  }