| 1 | efrain | 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 |   | 
        
           | 1441 | ariadna | 46 |     protected function get_javascript_init_params($course, ?\cm_info $cm = null,
 | 
        
           |  |  | 47 |             ?\section_info $section = null) {
 | 
        
           | 1 | efrain | 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(
 | 
        
           |  |  | 56 |                 'id' => $rec->id,
 | 
        
           |  |  | 57 |                 'name' => format_string($rec->name, true, array('context' => $context)),
 | 
        
           |  |  | 58 |                 'visibility' => $rec->visibility
 | 
        
           |  |  | 59 |             );
 | 
        
           |  |  | 60 |         }
 | 
        
           |  |  | 61 |         return array($jsarray);
 | 
        
           |  |  | 62 |     }
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |     /**
 | 
        
           |  |  | 65 |      * Gets all groups for the given course.
 | 
        
           |  |  | 66 |      *
 | 
        
           |  |  | 67 |      * @param int $courseid Course id
 | 
        
           |  |  | 68 |      * @return array Array of all the group objects
 | 
        
           |  |  | 69 |      */
 | 
        
           |  |  | 70 |     protected function get_all_groups($courseid) {
 | 
        
           |  |  | 71 |         global $CFG;
 | 
        
           |  |  | 72 |         require_once($CFG->libdir . '/grouplib.php');
 | 
        
           |  |  | 73 |   | 
        
           |  |  | 74 |         if ($courseid != $this->allgroupscourseid) {
 | 
        
           | 1441 | ariadna | 75 |             $this->allgroups = groups_get_all_groups($courseid, 0, 0, 'g.id, g.name, g.visibility');
 | 
        
           | 1 | efrain | 76 |             $this->allgroupscourseid = $courseid;
 | 
        
           |  |  | 77 |         }
 | 
        
           |  |  | 78 |         return $this->allgroups;
 | 
        
           |  |  | 79 |     }
 | 
        
           |  |  | 80 |   | 
        
           | 1441 | ariadna | 81 |     protected function allow_add($course, ?\cm_info $cm = null,
 | 
        
           |  |  | 82 |             ?\section_info $section = null) {
 | 
        
           | 1 | efrain | 83 |         global $CFG;
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 |         // Only show this option if there are some groups.
 | 
        
           |  |  | 86 |         return count($this->get_all_groups($course->id)) > 0;
 | 
        
           |  |  | 87 |     }
 | 
        
           |  |  | 88 | }
 |