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 * The renderable for core/checkbox-toggleall. 19 * 20 * @package core 21 * @copyright 2019 Jun Pataleta 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25namespace core\output; 26defined('MOODLE_INTERNAL') || die(); 27 28use renderable; 29use renderer_base; 30use stdClass; 31use templatable; 32 33/** 34 * The checkbox-toggleall renderable class. 35 * 36 * @package core 37 * @copyright 2019 Jun Pataleta 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40class checkbox_toggleall implements renderable, templatable { 41 42 /** @var string The name of the group of checkboxes to be toggled. */ 43 protected $togglegroup; 44 45 /** @var bool $ismaster Whether we're rendering for a master checkbox or a slave checkbox. */ 46 protected $ismaster; 47 48 /** @var array $options The options for the checkbox. */ 49 protected $options; 50 51 /** @var bool $isbutton Whether to render this as a button. Applies to master checkboxes only. */ 52 protected $isbutton; 53 54 /** 55 * Constructor. 56 * 57 * @param string $togglegroup The name of the group of checkboxes to be toggled. 58 * @param bool $ismaster Whether we're rendering for a master checkbox or a slave checkbox. 59 * @param array $options The options for the checkbox. Valid options are: 60 * <ul> 61 * <li><b>id </b> string - The element ID.</li> 62 * <li><b>name </b> string - The element name.</li> 63 * <li><b>classes </b> string - CSS classes that you want to add for your checkbox or toggle controls. 64 * For button type master toggle controls, this could be any Bootstrap 4 btn classes 65 * that you might want to add. Defaults to "btn-secondary".</li> 66 * <li><b>value </b> string|int - The element's value.</li> 67 * <li><b>checked </b> boolean - Whether to render this initially as checked.</li> 68 * <li><b>label </b> string - The label for the checkbox element.</li> 69 * <li><b>labelclasses</b> string - CSS classes that you want to add for your label.</li> 70 * <li><b>selectall </b> string - Master only. The language string that will be used to indicate that clicking on 71 * the master will select all of the slave checkboxes. Defaults to "Select all".</li> 72 * <li><b>deselectall </b> string - Master only. The language string that will be used to indicate that clicking on 73 * the master will select all of the slave checkboxes. Defaults to "Deselect all".</li> 74 * </ul> 75 * @param bool $isbutton Whether to render this as a button. Applies to master only. 76 */ 77 public function __construct(string $togglegroup, bool $ismaster, $options = [], $isbutton = false) { 78 $this->togglegroup = $togglegroup; 79 $this->ismaster = $ismaster; 80 $this->options = $options; 81 $this->isbutton = $ismaster && $isbutton; 82 } 83 84 /** 85 * Export for template. 86 * 87 * @param renderer_base $output The renderer. 88 * @return stdClass 89 */ 90 public function export_for_template(renderer_base $output) { 91 $data = (object)[ 92 'togglegroup' => $this->togglegroup, 93 'id' => $this->options['id'] ?? null, 94 'name' => $this->options['name'] ?? null, 95 'value' => $this->options['value'] ?? null, 96 'classes' => $this->options['classes'] ?? null, 97 'label' => $this->options['label'] ?? null, 98 'labelclasses' => $this->options['labelclasses'] ?? null, 99 'checked' => $this->options['checked'] ?? false, 100 ]; 101 102 if ($this->ismaster) { 103 $data->selectall = $this->options['selectall'] ?? get_string('selectall'); 104 $data->deselectall = $this->options['deselectall'] ?? get_string('deselectall'); 105 } 106 107 return $data; 108 } 109 110 /** 111 * Fetches the appropriate template for the checkbox toggle all element. 112 * 113 * @return string 114 */ 115 public function get_template() { 116 if ($this->ismaster) { 117 if ($this->isbutton) { 118 return 'core/checkbox-toggleall-master-button'; 119 } else { 120 return 'core/checkbox-toggleall-master'; 121 } 122 } 123 return 'core/checkbox-toggleall-slave'; 124 } 125} 126