Skip to content

Commit

Permalink
Merge branch 'MDL-78528-master' of https://github.com/sarjona/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta authored and HuongNV13 committed Aug 10, 2023
2 parents 4ef1f2f + a2db074 commit 44787c3
Show file tree
Hide file tree
Showing 28 changed files with 906 additions and 519 deletions.
37 changes: 35 additions & 2 deletions completion/classes/defaultedit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ class core_completion_defaultedit_form extends core_completion_edit_base_form {
/** @var array */
protected $_modnames;

public function __construct(
$action = null,
$customdata = null,
$method = 'post',
$target = '',
$attributes = null,
$editable = true,
$ajaxformdata = null
) {
$this->modules = $customdata['modules'];
if ($modname = $this->get_module_name()) {
// Set the form suffix to the module name so that the form identifier is unique for each module type.
$this->set_suffix('_' . $modname);
}

parent::__construct($action, $customdata, $method, $target, $attributes, $editable, $ajaxformdata);
}


/**
* Returns list of types of selected modules
*
Expand Down Expand Up @@ -66,7 +85,7 @@ protected function get_module_form() {
throw new \moodle_exception('noformdesc');
}

list($module, $context, $cw, $cmrec, $data) = prepare_new_moduleinfo_data($course, $modname, 0);
list($module, $context, $cw, $cmrec, $data) = prepare_new_moduleinfo_data($course, $modname, 0, $this->get_suffix());
$data->return = 0;
$data->sr = 0;
$data->add = $modname;
Expand All @@ -75,6 +94,7 @@ protected function get_module_form() {
$mformclassname = 'mod_'.$modname.'_mod_form';
$PAGE->start_collecting_javascript_requirements();
$this->_moduleform = new $mformclassname($data, 0, $cmrec, $course);
$this->_moduleform->set_suffix('_' . $modname);
$PAGE->end_collecting_javascript_requirements();

return $this->_moduleform;
Expand All @@ -101,7 +121,12 @@ public function definition() {
$modnames = array_keys($this->get_module_names());
$modname = $modnames[0];
// Pre-fill the form with the current completion rules of the first selected module type.
list($module, $context, $cw, $cmrec, $data) = prepare_new_moduleinfo_data($this->course, $modname, 0);
list($module, $context, $cw, $cmrec, $data) = prepare_new_moduleinfo_data(
$this->course,
$modname,
0,
$this->get_suffix()
);
$data = (array)$data;
$modform->data_preprocessing($data);
// Unset fields that will conflict with this form and set data to this form.
Expand All @@ -121,4 +146,12 @@ public function definition() {
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.
*/
protected function get_form_identifier() {
return parent::get_form_identifier() . $this->get_suffix();
}
}
17 changes: 16 additions & 1 deletion completion/classes/edit_base_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ protected function add_completion_rules() {
$moduleform->_form = $this->_form;
if ($customcompletionelements = $moduleform->add_completion_rules()) {
$this->hascustomrules = true;
foreach ($customcompletionelements as $customcompletionelement) {
// Instead of checking for the suffix at the end of the element name, we need to check for its presence
// because some modules, like SCORM, are adding things at the end.
if (!str_contains($customcompletionelement, $this->get_suffix())) {
debugging(
'Custom completion rule ' . $customcompletionelement . ' of module ' . $modnames[0] .
' has wrong suffix and has been removed from the form. This has to be fixed by the developer',
DEBUG_DEVELOPER
);
$moduleform->_form->removeElement($customcompletionelement);
}
}

}
return $customcompletionelements;
} catch (Exception $e) {
Expand Down Expand Up @@ -198,7 +211,9 @@ public function definition() {
$mform->addElement('static', 'qwerty', '', get_string('hiddenrules', 'completion', join(', ', $conflicts)));
}

$this->add_action_buttons();
// Whether to show the cancel button or not in the form.
$displaycancel = $this->_customdata['displaycancel'] ?? true;
$this->add_action_buttons($displaycancel);
}

/**
Expand Down
Loading

0 comments on commit 44787c3

Please sign in to comment.