MDL-81601 core_course: Add course index completion status behats
[moodle.git] / completion / classes / bulkedit_form.php
blob3826a83553e27d80ec2cfb3d1457320f18f2eccc
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 use core_completion\manager;
19 /**
20 * Bulk edit activity completion form
22 * @package core_completion
23 * @copyright 2017 Marina Glancy
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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;
32 /**
33 * Returns list of types of selected modules
35 * @return array modname=>modfullname
37 protected function get_module_names() {
38 if ($this->_modnames !== null) {
39 return $this->_modnames;
41 $this->_modnames = [];
42 foreach ($this->cms as $cm) {
43 $this->_modnames[$cm->modname] = $cm->modfullname;
45 return $this->_modnames;
48 /**
49 * It will return the course module when $cms has only one course module; otherwise, null will be returned.
51 * @return cm_info|null
53 protected function get_cm(): ?cm_info {
54 if (count($this->cms) === 1) {
55 return reset($this->cms);
58 // If there are multiple modules, so none will be selected.
59 return null;
62 /**
63 * Returns an instance of component-specific module form for the first selected module
65 * @return moodleform_mod|null
67 protected function get_module_form() {
68 global $CFG, $PAGE;
70 if ($this->_moduleform) {
71 return $this->_moduleform;
74 $cm = reset($this->cms);
75 $this->_moduleform = manager::get_module_form(
76 modname: $cm->modname,
77 course: $this->course,
78 cm: $cm,
81 return $this->_moduleform;
84 /**
85 * Form definition
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();
92 $mform = $this->_form;
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++;
101 parent::definition();
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);
117 * Form validation
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).
124 public function validation($data, $files) {
125 global $CFG;
126 $errors = parent::validation($data, $files);
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();
143 if ($moduleswithoutgradeitem) {
144 $errors['completionusegrade'] = get_string('nogradeitem', 'completion', join(', ', $moduleswithoutgradeitem));
148 return $errors;