From 21a0008f5809df1406351a85863aa81f7d8f4fa3 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Wed, 29 Apr 2020 01:19:51 +0100 Subject: [PATCH] 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). --- admin/tool/lp/classes/form/template_cohorts.php | 14 ++++++++------ admin/tool/lp/template_cohorts.php | 14 +++++++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/admin/tool/lp/classes/form/template_cohorts.php b/admin/tool/lp/classes/form/template_cohorts.php index bd61cc78d25..e4937f331d2 100644 --- a/admin/tool/lp/classes/form/template_cohorts.php +++ b/admin/tool/lp/classes/form/template_cohorts.php @@ -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')); } - } diff --git a/admin/tool/lp/template_cohorts.php b/admin/tool/lp/template_cohorts.php index 5667f3f4d6b..ad59ebe1b81 100644 --- a/admin/tool/lp/template_cohorts.php +++ b/admin/tool/lp/template_cohorts.php @@ -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; -- 2.43.0