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 |
use core_completion\manager;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Bulk edit activity completion form
|
|
|
21 |
*
|
|
|
22 |
* @package core_completion
|
|
|
23 |
* @copyright 2017 Marina Glancy
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
class core_completion_bulkedit_form extends core_completion_edit_base_form {
|
|
|
27 |
/** @var cm_info[] list of selected course modules */
|
|
|
28 |
protected $cms = [];
|
|
|
29 |
/** @var array Do not use directly, call $this->get_module_names() */
|
|
|
30 |
protected $_modnames = null;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Returns list of types of selected modules
|
|
|
34 |
*
|
|
|
35 |
* @return array modname=>modfullname
|
|
|
36 |
*/
|
|
|
37 |
protected function get_module_names() {
|
|
|
38 |
if ($this->_modnames !== null) {
|
|
|
39 |
return $this->_modnames;
|
|
|
40 |
}
|
|
|
41 |
$this->_modnames = [];
|
|
|
42 |
foreach ($this->cms as $cm) {
|
|
|
43 |
$this->_modnames[$cm->modname] = $cm->modfullname;
|
|
|
44 |
}
|
|
|
45 |
return $this->_modnames;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* It will return the course module when $cms has only one course module; otherwise, null will be returned.
|
|
|
50 |
*
|
|
|
51 |
* @return cm_info|null
|
|
|
52 |
*/
|
|
|
53 |
protected function get_cm(): ?cm_info {
|
|
|
54 |
if (count($this->cms) === 1) {
|
|
|
55 |
return reset($this->cms);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
// If there are multiple modules, so none will be selected.
|
|
|
59 |
return null;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Returns an instance of component-specific module form for the first selected module
|
|
|
64 |
*
|
|
|
65 |
* @return moodleform_mod|null
|
|
|
66 |
*/
|
|
|
67 |
protected function get_module_form() {
|
|
|
68 |
global $CFG, $PAGE;
|
|
|
69 |
|
|
|
70 |
if ($this->_moduleform) {
|
|
|
71 |
return $this->_moduleform;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
$cm = reset($this->cms);
|
|
|
75 |
$this->_moduleform = manager::get_module_form(
|
|
|
76 |
modname: $cm->modname,
|
|
|
77 |
course: $this->course,
|
|
|
78 |
cm: $cm,
|
|
|
79 |
);
|
|
|
80 |
|
|
|
81 |
return $this->_moduleform;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Form definition
|
|
|
86 |
*/
|
|
|
87 |
public function definition() {
|
|
|
88 |
$this->cms = $this->_customdata['cms'];
|
|
|
89 |
$cm = reset($this->cms); // First selected course module.
|
|
|
90 |
$this->course = $cm->get_course();
|
|
|
91 |
|
|
|
92 |
$mform = $this->_form;
|
|
|
93 |
|
|
|
94 |
$idx = 0;
|
|
|
95 |
foreach ($this->cms as $cm) {
|
|
|
96 |
$mform->addElement('hidden', 'cmid['.$idx.']', $cm->id);
|
|
|
97 |
$mform->setType('cmid['.$idx.']', PARAM_INT);
|
|
|
98 |
$idx++;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
parent::definition();
|
|
|
102 |
|
|
|
103 |
$modform = $this->get_module_form();
|
|
|
104 |
if ($modform) {
|
|
|
105 |
// Pre-fill the form with the current completion rules of the first selected module.
|
|
|
106 |
list($cmrec, $context, $module, $data, $cw) = get_moduleinfo_data($cm->get_course_module_record(), $this->course);
|
|
|
107 |
$data = (array)$data;
|
|
|
108 |
$modform->data_preprocessing($data);
|
|
|
109 |
// Unset fields that will conflict with this form and set data to this form.
|
|
|
110 |
unset($data['cmid']);
|
|
|
111 |
unset($data['id']);
|
|
|
112 |
$this->set_data($data);
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Form validation
|
|
|
118 |
*
|
|
|
119 |
* @param array $data array of ("fieldname"=>value) of submitted data
|
|
|
120 |
* @param array $files array of uploaded files "element_name"=>tmp_file_path
|
|
|
121 |
* @return array of "element_name"=>"error_description" if there are errors,
|
|
|
122 |
* or an empty array if everything is OK (true allowed for backwards compatibility too).
|
|
|
123 |
*/
|
|
|
124 |
public function validation($data, $files) {
|
|
|
125 |
global $CFG;
|
|
|
126 |
$errors = parent::validation($data, $files);
|
|
|
127 |
|
|
|
128 |
// Completion: Don't let them choose automatic completion without turning
|
|
|
129 |
// on some conditions.
|
|
|
130 |
if (array_key_exists('completion', $data) &&
|
|
|
131 |
$data['completion'] == COMPLETION_TRACKING_AUTOMATIC &&
|
|
|
132 |
(!empty($data['completionusegrade']) || !empty($data['completionpassgrade']))) {
|
|
|
133 |
require_once($CFG->libdir.'/gradelib.php');
|
|
|
134 |
$moduleswithoutgradeitem = [];
|
|
|
135 |
foreach ($this->cms as $cm) {
|
|
|
136 |
$item = grade_item::fetch(array('courseid' => $cm->course, 'itemtype' => 'mod',
|
|
|
137 |
'itemmodule' => $cm->modname, 'iteminstance' => $cm->instance,
|
|
|
138 |
'itemnumber' => 0));
|
|
|
139 |
if (!$item) {
|
|
|
140 |
$moduleswithoutgradeitem[] = $cm->get_formatted_name();
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
if ($moduleswithoutgradeitem) {
|
|
|
144 |
$errors['completionusegrade'] = get_string('nogradeitem', 'completion', join(', ', $moduleswithoutgradeitem));
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
return $errors;
|
|
|
149 |
}
|
|
|
150 |
}
|