250f654d7ca978c36f494635b9f93d1ff3de8e04
[moodle.git] / grade / report / singleview / classes / local / ui / bulk_insert.php
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/>.
17 /**
18  * Checkbox element used for bulk inserting values in the gradebook.
19  *
20  * @package   gradereport_singleview
21  * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 namespace gradereport_singleview\local\ui;
27 use html_writer;
29 defined('MOODLE_INTERNAL') || die;
31 /**
32  * Checkbox element used for bulk inserting values in the gradebook.
33  *
34  * @package   gradereport_singleview
35  * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
36  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37  */
38 class bulk_insert extends element {
40     /**
41      * Constructor
42      *
43      * @param mixed $item The grade item or user.
44      */
45     public function __construct($item) {
46         $this->name = 'bulk_' . $item->id;
47         $this->applyname = $this->name_for('apply');
48         $this->selectname = $this->name_for('type');
49         $this->insertname = $this->name_for('value');
50     }
52     /**
53      * Is this checkbox checked?
54      *
55      * @param array $data The form data
56      * @return bool
57      */
58     public function is_applied($data) {
59         return isset($data->{$this->applyname});
60     }
62     /**
63      * Get the type of this input (user or grade)
64      *
65      * @param array $data The form data
66      * @return string
67      */
68     public function get_type($data) {
69         return $data->{$this->selectname};
70     }
72     /**
73      * Get the value from either the user or grade.
74      *
75      * @param array $data The form data
76      * @return string
77      */
78     public function get_insert_value($data) {
79         return $data->{$this->insertname};
80     }
82     /**
83      * Generate the html for this form element.
84      *
85      * @return string HTML
86      */
87     public function html() {
88         $insertvalue = get_string('bulkinsertvalue', 'gradereport_singleview');
89         $insertappliesto = get_string('bulkappliesto', 'gradereport_singleview');
91         $insertoptions = array(
92             'all' => get_string('all_grades', 'gradereport_singleview'),
93             'blanks' => get_string('blanks', 'gradereport_singleview')
94         );
96         $selectlabel = html_writer::label(
97             $insertappliesto,
98             $this->selectname
99         );
100         $select = html_writer::select(
101             $insertoptions, $this->selectname, 'blanks', false
102         );
104         $textlabel = html_writer::label(
105             $insertvalue,
106             $this->insertname
107         );
108         $text = new text_attribute($this->insertname, "0", 'bulk');
110         $inner = implode(' ', array(
111             $selectlabel,
112             $select,
113             $textlabel,
114             $text->html()
115         ));
117         $fieldset = html_writer::tag(
118             'fieldset',
119             html_writer::tag(
120                 'legend',
121                 get_string('bulklegend', 'gradereport_singleview'),
122                 array(
123                     'class' => 'accesshide'
124                 )
125             ) .
126             $inner
127         );
129         $apply = html_writer::checkbox(
130             $this->applyname,
131             1,
132             false,
133             get_string('bulkperform', 'gradereport_singleview')
134         );
135         $applydiv = html_writer::div($apply, 'enable');
137         return $applydiv . $fieldset;
138     }
140     /**
141      * This form element has 3 elements with different suffixes.
142      * Generate the name with the suffix.
143      *
144      * @param string $extend The suffix.
145      * @return string
146      */
147     private function name_for($extend) {
148         return "{$this->name}_$extend";
149     }