-
Notifications
You must be signed in to change notification settings - Fork 30
/
form_manual_allocation.php
221 lines (186 loc) · 7.88 KB
/
form_manual_allocation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Prints a particular instance of ratingallocate
*
* You can have a rather longer description of the file as well,
* if you like, and it can span multiple lines.
*
* @package mod_ratingallocate
* @copyright 2014 T Reischmann, C Usener
* @copyright based on code by M Schulze copyright (C) 2014 M Schulze
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/course/moodleform_mod.php');
/**
* Provides a form for manual allocations
*/
class manual_alloc_form extends moodleform {
/** @var $ratingallocate ratingallocate */
private $ratingallocate;
/**
* Constant for the action
*/
const FORM_ACTION = 'action';
/**
* Assign.
*/
const ASSIGN = 'assign';
/**
* Constructor
* @param mixed $url
* @param ratingallocate $ratingallocate
*/
public function __construct($url, ratingallocate $ratingallocate) {
$this->ratingallocate = $ratingallocate;
$url->params(["action" => "manual_allocation"]);
parent::__construct($url->out(false));
$this->definition_after_data();
}
/**
* Defines forms elements
*/
public function definition() {
global $COURSE;
$mform = $this->_form;
$mform->addElement('hidden', self::FORM_ACTION, ACTION_MANUAL_ALLOCATION);
$mform->setType(self::FORM_ACTION, PARAM_TEXT);
$mform->addElement('hidden', 'courseid', $COURSE->id);
$mform->setType('courseid', PARAM_INT);
$mform->addElement('hidden', 'page', 0);
$mform->setType('page', PARAM_INT);
$this->render_filter();
}
/**
* Render filter.
*
* @return void
* @throws coding_exception
* @throws dml_exception
*/
protected function render_filter() {
global $COURSE;
$mform = &$this->_form;
$mform->addElement('advcheckbox', 'hide_users_without_rating',
get_string('filter_hide_users_without_rating', RATINGALLOCATE_MOD_NAME),
null, [0, 1]);
$mform->setType('show_users_with_no_rating', PARAM_BOOL);
$mform->addElement('advcheckbox', 'show_alloc_necessary',
get_string('filter_show_alloc_necessary', RATINGALLOCATE_MOD_NAME),
null, [0, 1]);
$mform->setType('show_alloc_necessary', PARAM_BOOL);
// Filter by group.
$choicegroups = $this->ratingallocate->get_all_groups_of_choices();
$allgroups = [];
foreach ($choicegroups as $choicegroup) {
$allgroups[$choicegroup] = groups_get_group($choicegroup);
}
$groupsmenu[0] = get_string('allparticipants');
$groupsmenu[-1] = get_string('nogroup', 'enrol');
foreach ($allgroups as $gid => $unused) {
$groupsmenu[$gid] = $allgroups[$gid]->name;
}
if (count($groupsmenu) > 1) {
$mform->addElement('select', 'filtergroup', get_string('group'), $groupsmenu);
$mform->addHelpButton('filtergroup', 'filtergroup', RATINGALLOCATE_MOD_NAME);
}
$mform->addElement('submit', 'update_filter',
get_string('update_filter', RATINGALLOCATE_MOD_NAME));
$mform->registerNoSubmitButton('update_filter');
}
/**
* Fill table with data.
* @return void
* @throws coding_exception
* @throws dml_exception
*/
public function definition_after_data() {
parent::definition_after_data();
global $PAGE;
$mform = &$this->_form;
$ratingdata = $this->ratingallocate->get_ratings_for_rateable_choices();
$differentratings = [];
// Add actual rating data to userdata.
foreach ($ratingdata as $rating) {
if ($rating->rating != null) {
$differentratings[$rating->rating] = $rating->rating;
}
}
$hidenorating = null;
$showallocnecessary = null;
$groupselect = null;
// Get filter settings.
if ($this->is_submitted()) {
$hidenorating = $mform->getSubmitValue('hide_users_without_rating');
$showallocnecessary = $mform->getSubmitValue('show_alloc_necessary');
$groupselect = $mform->getSubmitValue('filtergroup');
}
// Create and set up the flextable for ratings and allocations.
$table = new mod_ratingallocate\ratings_and_allocations_table($this->ratingallocate->get_renderer(),
$this->ratingallocate->get_options_titles($differentratings), $this->ratingallocate,
'manual_allocation', 'mod_ratingallocate_manual_allocation', false);
$table->setup_table($this->ratingallocate->get_rateable_choices(),
$hidenorating, $showallocnecessary, $groupselect);
$filter = $table->get_filter();
$mform->setDefault('hide_users_without_rating', $filter['hidenorating']);
$mform->getElement('hide_users_without_rating')->setChecked($filter['hidenorating']);
$mform->setDefault('show_alloc_necessary', $filter['showallocnecessary']);
$mform->getElement('show_alloc_necessary')->setChecked($filter['showallocnecessary']);
$mform->setDefault('filtergroup', $filter['groupselect']);
$mform->getElement('filtergroup')->setSelected($filter['groupselect']);
$PAGE->requires->js_call_amd('mod_ratingallocate/radiobuttondeselect', 'init');
// The rest must be done through output buffering due to the way flextable works.
ob_start();
$table->build_table_by_sql($ratingdata, $this->ratingallocate->get_allocations(), true);
$tableoutput = ob_get_contents();
ob_end_clean();
$mform->addElement('html', html_writer::div($tableoutput, 'ratingallocate_ratings_table_container'));
$mform->setDefault('page', $table->get_page_start() / $table->get_page_size());
$this->add_special_action_buttons();
}
/**
* Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and continue" button.
*
* @param bool $cancel show cancel button
* @param string $submitlabel null means default, false means none, string is label text
* @param string $submit2label null means default, false means none, string is label text
* @return void
*/
public function add_special_action_buttons() {
$submitlabel = get_string('savechanges');
$submit2label = get_string('saveandcontinue', RATINGALLOCATE_MOD_NAME);
$mform = $this->_form;
// Elements in a row need a group.
$buttonarray = [];
$buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
$buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
$buttonarray[] = &$mform->createElement('cancel');
$mform->addGroup($buttonarray, 'buttonar', '', [' '], false);
$mform->setType('buttonar', PARAM_RAW);
$mform->closeHeaderBefore('buttonar');
}
/**
* Returns the forms HTML code.
* So we don't have to call display().
*/
public function to_html() {
$o = '';
$o .= $this->_form->getValidationScript();
$o .= $this->_form->toHtml();
return $o;
}
}