Merge branch 'MDL-73597-311' of https://github.com/HuongNV13/moodle into MOODLE_311_S...
[moodle.git] / completion / classes / edit_base_form.php
blob505c789168951afe746e4949dd88df15222f05e6
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.
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 * Base form for changing completion rules
20 * @package core_completion
21 * @copyright 2017 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die;
27 require_once($CFG->libdir.'/formslib.php');
28 require_once($CFG->libdir.'/completionlib.php');
29 require_once($CFG->dirroot.'/course/modlib.php');
31 /**
32 * Base form for changing completion rules. Used in bulk editing activity completion and editing default activity completion
34 * @package core_completion
35 * @copyright 2017 Marina Glancy
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 abstract class core_completion_edit_base_form extends moodleform {
39 /** @var moodleform_mod Do not use directly, call $this->get_module_form() */
40 protected $_moduleform = null;
41 /** @var bool */
42 protected $hascustomrules = false;
43 /** @var stdClass */
44 protected $course;
46 /**
47 * Returns list of types of selected module types
49 * @return array modname=>modfullname
51 abstract protected function get_module_names();
53 /**
54 * Returns true if all selected modules support tracking view.
56 * @return bool
58 protected function support_views() {
59 foreach ($this->get_module_names() as $modname => $modfullname) {
60 if (!plugin_supports('mod', $modname, FEATURE_COMPLETION_TRACKS_VIEWS, false)) {
61 return false;
64 return true;
67 /**
68 * Returns true if all selected modules support grading.
70 * @return bool
72 protected function support_grades() {
73 foreach ($this->get_module_names() as $modname => $modfullname) {
74 if (!plugin_supports('mod', $modname, FEATURE_GRADE_HAS_GRADE, false)) {
75 return false;
78 return true;
81 /**
82 * Returns an instance of component-specific module form for the first selected module
84 * @return moodleform_mod|null
86 abstract protected function get_module_form();
88 /**
89 * If all selected modules are of the same module type, adds custom completion rules from this module type
91 * @return array
93 protected function add_custom_completion_rules() {
94 $modnames = array_keys($this->get_module_names());
95 if (count($modnames) != 1 || !plugin_supports('mod', $modnames[0], FEATURE_COMPLETION_HAS_RULES, false)) {
96 return [];
99 try {
100 // Add completion rules from the module form to this form.
101 $moduleform = $this->get_module_form();
102 $moduleform->_form = $this->_form;
103 if ($customcompletionelements = $moduleform->add_completion_rules()) {
104 $this->hascustomrules = true;
106 return $customcompletionelements;
107 } catch (Exception $e) {
108 debugging('Could not add custom completion rule of module ' . $modnames[0] .
109 ' to this form, this has to be fixed by the developer', DEBUG_DEVELOPER);
110 return [];
115 * Checks if at least one of the custom completion rules is enabled
117 * @param array $data Input data (not yet validated)
118 * @return bool True if one or more rules is enabled, false if none are;
119 * default returns false
121 protected function completion_rule_enabled($data) {
122 if ($this->hascustomrules) {
123 return $this->get_module_form()->completion_rule_enabled($data);
125 return false;
129 * Returns list of modules that have automatic completion rules that are not shown on this form
130 * (because they are not present in at least one other selected module).
132 * @return array
134 protected function get_modules_with_hidden_rules() {
135 $modnames = $this->get_module_names();
136 if (count($modnames) <= 1) {
137 // No rules definitions conflicts if there is only one module type.
138 return [];
141 $conflicts = [];
143 if (!$this->support_views()) {
144 // If we don't display views rule but at least one module supports it - we have conflicts.
145 foreach ($modnames as $modname => $modfullname) {
146 if (empty($conflicts[$modname]) && plugin_supports('mod', $modname, FEATURE_COMPLETION_TRACKS_VIEWS, false)) {
147 $conflicts[$modname] = $modfullname;
152 if (!$this->support_grades()) {
153 // If we don't display grade rule but at least one module supports it - we have conflicts.
154 foreach ($modnames as $modname => $modfullname) {
155 if (empty($conflicts[$modname]) && plugin_supports('mod', $modname, FEATURE_GRADE_HAS_GRADE, false)) {
156 $conflicts[$modname] = $modfullname;
161 foreach ($modnames as $modname => $modfullname) {
162 // We do not display any custom completion rules, find modules that define them and add to conflicts list.
163 if (empty($conflicts[$modname]) && plugin_supports('mod', $modname, FEATURE_COMPLETION_HAS_RULES, false)) {
164 $conflicts[$modname] = $modfullname;
168 return $conflicts;
172 * Form definition
174 public function definition() {
175 $mform = $this->_form;
177 // Course id.
178 $mform->addElement('hidden', 'id', $this->course->id);
179 $mform->setType('id', PARAM_INT);
181 // Unlock completion automatically (this element can be used in validation).
182 $mform->addElement('hidden', 'completionunlocked', 1);
183 $mform->setType('completionunlocked', PARAM_INT);
185 $mform->addElement('select', 'completion', get_string('completion', 'completion'),
186 array(COMPLETION_TRACKING_NONE => get_string('completion_none', 'completion'),
187 COMPLETION_TRACKING_MANUAL => get_string('completion_manual', 'completion')));
188 $mform->addHelpButton('completion', 'completion', 'completion');
189 $mform->setDefault('completion', COMPLETION_TRACKING_NONE);
191 // Automatic completion once you view it.
192 $autocompletionpossible = false;
193 if ($this->support_views()) {
194 $mform->addElement('advcheckbox', 'completionview', get_string('completionview', 'completion'),
195 get_string('completionview_desc', 'completion'));
196 $mform->disabledIf('completionview', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
197 $autocompletionpossible = true;
200 // Automatic completion once it's graded.
201 if ($this->support_grades()) {
202 $mform->addElement('advcheckbox', 'completionusegrade', get_string('completionusegrade', 'completion'),
203 get_string('completionusegrade_desc', 'completion'));
204 $mform->disabledIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
205 $mform->addHelpButton('completionusegrade', 'completionusegrade', 'completion');
206 $autocompletionpossible = true;
209 // Automatic completion according to module-specific rules.
210 foreach ($this->add_custom_completion_rules() as $element) {
211 $mform->disabledIf($element, 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
212 $autocompletionpossible = true;
215 // Automatic option only appears if possible.
216 if ($autocompletionpossible) {
217 $mform->getElement('completion')->addOption(
218 get_string('completion_automatic', 'completion'),
219 COMPLETION_TRACKING_AUTOMATIC);
222 // Completion expected at particular date? (For progress tracking).
223 $mform->addElement('date_time_selector', 'completionexpected',
224 get_string('completionexpected', 'completion'), ['optional' => true]);
225 $mform->addHelpButton('completionexpected', 'completionexpected', 'completion');
226 $mform->disabledIf('completionexpected', 'completion', 'eq', COMPLETION_TRACKING_NONE);
228 if ($conflicts = $this->get_modules_with_hidden_rules()) {
229 $mform->addElement('static', 'qwerty', '', get_string('hiddenrules', 'completion', join(', ', $conflicts)));
232 $this->add_action_buttons();
236 * Form validation
238 * @param array $data array of ("fieldname"=>value) of submitted data
239 * @param array $files array of uploaded files "element_name"=>tmp_file_path
240 * @return array of "element_name"=>"error_description" if there are errors,
241 * or an empty array if everything is OK (true allowed for backwards compatibility too).
243 public function validation($data, $files) {
244 $errors = parent::validation($data, $files);
246 // Completion: Don't let them choose automatic completion without turning
247 // on some conditions.
248 if (array_key_exists('completion', $data) &&
249 $data['completion'] == COMPLETION_TRACKING_AUTOMATIC) {
250 if (empty($data['completionview']) && empty($data['completionusegrade']) &&
251 !$this->completion_rule_enabled($data)) {
252 $errors['completion'] = get_string('badautocompletion', 'completion');
256 return $errors;
260 * Returns if this form has custom completion rules. This is only possible if all selected modules have the same
261 * module type and this module type supports custom completion rules
263 * @return bool
265 public function has_custom_completion_rules() {
266 return $this->hascustomrules;
270 * Return submitted data if properly submitted or returns NULL if validation fails or
271 * if there is no submitted data.
273 * @return object submitted data; NULL if not valid or not submitted or cancelled
275 public function get_data() {
276 $data = parent::get_data();
277 if ($data && $this->hascustomrules) {
278 $this->get_module_form()->data_postprocessing($data);
280 return $data;