Skip to content

Commit

Permalink
Merge branch 'MDL-79351-master' of https://github.com/ferranrecio/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
HuongNV13 committed Sep 18, 2023
2 parents c230741 + f652b76 commit 7ac9c99
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 32 deletions.
6 changes: 3 additions & 3 deletions completion/classes/bulkedit_form.php
Expand Up @@ -56,11 +56,11 @@ protected function get_module_names() {
/**
* 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.
Expand Down
10 changes: 0 additions & 10 deletions completion/classes/defaultedit_form.php
Expand Up @@ -138,16 +138,6 @@ public function definition() {
}
}

/**
* 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.
Expand Down
11 changes: 10 additions & 1 deletion completion/classes/edit_base_form.php
Expand Up @@ -256,11 +256,20 @@ public function definition() {
$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());
}

/**
Expand Down
21 changes: 4 additions & 17 deletions completion/classes/form/form_trait.php
Expand Up @@ -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.
Expand Down Expand Up @@ -80,21 +81,6 @@ public function get_suffix(): string {
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.
*
Expand Down Expand Up @@ -427,8 +413,10 @@ protected function validate_completion(array $data): array {

/**
* 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();

Expand All @@ -439,7 +427,6 @@ protected function definition_after_data_completion(): void {
$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;
Expand Down
6 changes: 5 additions & 1 deletion course/moodleform_mod.php
Expand Up @@ -324,7 +324,11 @@ function definition_after_data() {
}

// 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();
Expand Down

0 comments on commit 7ac9c99

Please sign in to comment.