mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 14:03:52 +01:00
MDL-79351 completion: fix form_trait code smells
This commit is contained in:
parent
9b2c445143
commit
f652b769fe
@ -56,11 +56,11 @@ class core_completion_bulkedit_form extends core_completion_edit_base_form {
|
||||
/**
|
||||
* It will return the course module when $cms has only one course module; otherwise, null will be returned.
|
||||
*
|
||||
* @return \stdClass|null
|
||||
* @return cm_info|null
|
||||
*/
|
||||
protected function get_cm(): ?\stdClass {
|
||||
protected function get_cm(): ?cm_info {
|
||||
if (count($this->cms) === 1) {
|
||||
return reset($this->cms)->get_course_module_record();
|
||||
return reset($this->cms);
|
||||
}
|
||||
|
||||
// If there are multiple modules, so none will be selected.
|
||||
|
@ -138,16 +138,6 @@ class core_completion_defaultedit_form extends core_completion_edit_base_form {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* There is no course module for this form, because it is used to update default completion settings. So it will
|
||||
* always return null.
|
||||
*
|
||||
* @return \stdClass|null
|
||||
*/
|
||||
protected function get_cm(): ?\stdClass {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method has been overridden because the form identifier must be unique for each module type.
|
||||
* Otherwise, the form will display the same data for each module type once it's submitted.
|
||||
|
@ -218,11 +218,20 @@ abstract class core_completion_edit_base_form extends moodleform {
|
||||
$this->add_action_buttons($displaycancel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the course module of the form, if any.
|
||||
*
|
||||
* @return cm_info|null
|
||||
*/
|
||||
protected function get_cm(): ?cm_info {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Each module which defines definition_after_data() must call this method.
|
||||
*/
|
||||
public function definition_after_data() {
|
||||
$this->definition_after_data_completion();
|
||||
$this->definition_after_data_completion($this->get_cm());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,6 +17,7 @@
|
||||
namespace core_completion\form;
|
||||
|
||||
use core_grades\component_gradeitems;
|
||||
use cm_info;
|
||||
|
||||
/**
|
||||
* Completion trait helper, with methods to add completion elements and validate them.
|
||||
@ -80,21 +81,6 @@ trait form_trait {
|
||||
return $this->suffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the cm (course module) associated to this class.
|
||||
* This method must be overriden by the class using this trait if it doesn't include a _cm property.
|
||||
*
|
||||
* @return \stdClass|null
|
||||
* @throws \coding_exception If the class does not have a _cm property.
|
||||
*/
|
||||
protected function get_cm(): ?\stdClass {
|
||||
if (property_exists($this, '_cm')) {
|
||||
return $this->_cm;
|
||||
}
|
||||
|
||||
throw new \coding_exception('This class does not have a _cm property. Please, add it or override the get_cm() method.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add completion elements to the form.
|
||||
*
|
||||
@ -347,8 +333,10 @@ trait form_trait {
|
||||
|
||||
/**
|
||||
* It should be called from the definition_after_data() to setup the completion settings in the form.
|
||||
*
|
||||
* @param cm_info|null $cm The course module associated to this form.
|
||||
*/
|
||||
protected function definition_after_data_completion(): void {
|
||||
protected function definition_after_data_completion(?cm_info $cm = null): void {
|
||||
global $COURSE, $SITE;
|
||||
$mform = $this->get_form();
|
||||
|
||||
@ -359,7 +347,6 @@ trait form_trait {
|
||||
$suffix = $this->get_suffix();
|
||||
|
||||
// If anybody has completed the activity, these options will be 'locked'.
|
||||
$cm = $this->get_cm();
|
||||
// We use $SITE course for site default activity completion, so we don't need any unlock button.
|
||||
$completedcount = (empty($cm) || $COURSE->id == $SITE->id) ? 0 : $completion->count_user_data($cm);
|
||||
$freeze = false;
|
||||
|
@ -318,7 +318,11 @@ abstract class moodleform_mod extends moodleform {
|
||||
}
|
||||
|
||||
// Completion: If necessary, freeze fields.
|
||||
$this->definition_after_data_completion();
|
||||
$cm = null;
|
||||
if ($this->_cm) {
|
||||
$cm = get_fast_modinfo($COURSE)->get_cm($this->_cm->id);
|
||||
}
|
||||
$this->definition_after_data_completion($cm);
|
||||
|
||||
// Freeze admin defaults if required (and not different from default)
|
||||
$this->apply_admin_locked_flags();
|
||||
|
Loading…
x
Reference in New Issue
Block a user