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 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_group
  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_group;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Front-end class.
  31   *
  32   * @package availability_group
  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 group info for course */
  38      protected $allgroups;
  39      /** @var int Course id that $allgroups is for */
  40      protected $allgroupscourseid;
  41  
  42      protected function get_javascript_strings() {
  43          return array('anygroup');
  44      }
  45  
  46      protected function get_javascript_init_params($course, \cm_info $cm = null,
  47              \section_info $section = null) {
  48          // Get all groups for course.
  49          $groups = $this->get_all_groups($course->id);
  50  
  51          // Change to JS array format and return.
  52          $jsarray = array();
  53          $context = \context_course::instance($course->id);
  54          foreach ($groups as $rec) {
  55              $jsarray[] = (object)array('id' => $rec->id, 'name' =>
  56                      format_string($rec->name, true, array('context' => $context)));
  57          }
  58          return array($jsarray);
  59      }
  60  
  61      /**
  62       * Gets all groups for the given course.
  63       *
  64       * @param int $courseid Course id
  65       * @return array Array of all the group objects
  66       */
  67      protected function get_all_groups($courseid) {
  68          global $CFG;
  69          require_once($CFG->libdir . '/grouplib.php');
  70  
  71          if ($courseid != $this->allgroupscourseid) {
  72              $this->allgroups = groups_get_all_groups($courseid, 0, 0, 'g.id, g.name');
  73              $this->allgroupscourseid = $courseid;
  74          }
  75          return $this->allgroups;
  76      }
  77  
  78      protected function allow_add($course, \cm_info $cm = null,
  79              \section_info $section = null) {
  80          global $CFG;
  81  
  82          // Only show this option if there are some groups.
  83          return count($this->get_all_groups($course->id)) > 0;
  84      }
  85  }