MDL-62387 tool_lp: switch to core 'cohort' form element.

The autocomplete AJAX is identical to the version in tool_lp, but also
includes support for excluding suggestions (i.e. cohorts already
synced with the current learning plan).
This commit is contained in:
Paul Holden
2020-04-29 01:19:51 +01:00
parent d4d798e684
commit 21a0008f58
2 changed files with 21 additions and 7 deletions
@@ -26,7 +26,6 @@ namespace tool_lp\form;
defined('MOODLE_INTERNAL') || die();
use moodleform;
use core\form\persistent;
require_once($CFG->libdir . '/formslib.php');
@@ -39,17 +38,20 @@ require_once($CFG->libdir . '/formslib.php');
*/
class template_cohorts extends moodleform {
/**
* Form definition
*
* @return void
*/
public function definition() {
$mform = $this->_form;
$options = array(
'ajax' => 'tool_lp/form-cohort-selector',
'multiple' => true,
'data-contextid' => $this->_customdata['pagecontextid'],
'data-includes' => 'parents'
'exclude' => implode(',', $this->_customdata['excludecohorts']),
'contextid' => $this->_customdata['pagecontextid'],
);
$mform->addElement('autocomplete', 'cohorts', get_string('selectcohortstosync', 'tool_lp'), array(), $options);
$mform->addElement('cohort', 'cohorts', get_string('selectcohortstosync', 'tool_lp'), $options);
$mform->addElement('submit', 'submit', get_string('addcohorts', 'tool_lp'));
}
}
+13 -1
View File
@@ -54,7 +54,19 @@ if ($canmanagetemplate && ($removecohort = optional_param('removecohort', false,
}
// Capture the form submission.
$form = new \tool_lp\form\template_cohorts($url->out(false), array('pagecontextid' => $pagecontextid));
$existingcohortsql =
'SELECT c.id
FROM {' . \core_competency\template_cohort::TABLE . '} tc
JOIN {cohort} c ON c.id = tc.cohortid
WHERE tc.templateid = :templateid';
$existingcohorts = $DB->get_records_sql_menu($existingcohortsql, ['templateid' => $template->get('id')]);
$form = new \tool_lp\form\template_cohorts($url->out(false), [
'pagecontextid' => $pagecontextid,
'excludecohorts' => array_keys($existingcohorts),
]);
if ($canmanagetemplate && ($data = $form->get_data()) && !empty($data->cohorts)) {
$maxtocreate = 50;
$maxreached = false;