mirrored from git://git.moodle.org/moodle.git
/
bulkedit_form.php
173 lines (152 loc) · 5.92 KB
/
bulkedit_form.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
<?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/>.
/**
* Bulk edit activity completion form
*
* @package core_completion
* @copyright 2017 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* Bulk edit activity completion form
*
* @package core_completion
* @copyright 2017 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_completion_bulkedit_form extends core_completion_edit_base_form {
/** @var cm_info[] list of selected course modules */
protected $cms = [];
/** @var array Do not use directly, call $this->get_module_names() */
protected $_modnames = null;
/**
* Returns list of types of selected modules
*
* @return array modname=>modfullname
*/
protected function get_module_names() {
if ($this->_modnames !== null) {
return $this->_modnames;
}
$this->_modnames = [];
foreach ($this->cms as $cm) {
$this->_modnames[$cm->modname] = $cm->modfullname;
}
return $this->_modnames;
}
/**
* It will return the course module when $cms has only one course module; otherwise, null will be returned.
*
* @return cm_info|null
*/
protected function get_cm(): ?cm_info {
if (count($this->cms) === 1) {
return reset($this->cms);
}
// If there are multiple modules, so none will be selected.
return null;
}
/**
* Returns an instance of component-specific module form for the first selected module
*
* @return moodleform_mod|null
*/
protected function get_module_form() {
global $CFG, $PAGE;
if ($this->_moduleform) {
return $this->_moduleform;
}
$cm = reset($this->cms);
$course = $this->course;
$modname = $cm->modname;
$modmoodleform = "$CFG->dirroot/mod/$modname/mod_form.php";
if (file_exists($modmoodleform)) {
require_once($modmoodleform);
} else {
throw new \moodle_exception('noformdesc');
}
list($cmrec, $context, $module, $data, $cw) = get_moduleinfo_data($cm, $course);
$data->return = 0;
$data->sr = 0;
$data->update = $modname;
// Initialise the form but discard all JS requirements it adds, our form has already added them.
$mformclassname = 'mod_'.$modname.'_mod_form';
$PAGE->start_collecting_javascript_requirements();
$this->_moduleform = new $mformclassname($data, 0, $cmrec, $course);
$PAGE->end_collecting_javascript_requirements();
return $this->_moduleform;
}
/**
* Form definition
*/
public function definition() {
$this->cms = $this->_customdata['cms'];
$cm = reset($this->cms); // First selected course module.
$this->course = $cm->get_course();
$mform = $this->_form;
$idx = 0;
foreach ($this->cms as $cm) {
$mform->addElement('hidden', 'cmid['.$idx.']', $cm->id);
$mform->setType('cmid['.$idx.']', PARAM_INT);
$idx++;
}
parent::definition();
$modform = $this->get_module_form();
if ($modform) {
// Pre-fill the form with the current completion rules of the first selected module.
list($cmrec, $context, $module, $data, $cw) = get_moduleinfo_data($cm->get_course_module_record(), $this->course);
$data = (array)$data;
$modform->data_preprocessing($data);
// Unset fields that will conflict with this form and set data to this form.
unset($data['cmid']);
unset($data['id']);
$this->set_data($data);
}
}
/**
* Form validation
*
* @param array $data array of ("fieldname"=>value) of submitted data
* @param array $files array of uploaded files "element_name"=>tmp_file_path
* @return array of "element_name"=>"error_description" if there are errors,
* or an empty array if everything is OK (true allowed for backwards compatibility too).
*/
public function validation($data, $files) {
global $CFG;
$errors = parent::validation($data, $files);
// Completion: Don't let them choose automatic completion without turning
// on some conditions.
if (array_key_exists('completion', $data) &&
$data['completion'] == COMPLETION_TRACKING_AUTOMATIC &&
(!empty($data['completionusegrade']) || !empty($data['completionpassgrade']))) {
require_once($CFG->libdir.'/gradelib.php');
$moduleswithoutgradeitem = [];
foreach ($this->cms as $cm) {
$item = grade_item::fetch(array('courseid' => $cm->course, 'itemtype' => 'mod',
'itemmodule' => $cm->modname, 'iteminstance' => $cm->instance,
'itemnumber' => 0));
if (!$item) {
$moduleswithoutgradeitem[] = $cm->get_formatted_name();
}
}
if ($moduleswithoutgradeitem) {
$errors['completionusegrade'] = get_string('nogradeitem', 'completion', join(', ', $moduleswithoutgradeitem));
}
}
return $errors;
}
}