From da5e19a12816474ea054e4c68b206908d5b8791b Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Wed, 23 Jan 2019 16:29:52 +0800 Subject: [PATCH] MDL-63876 badges: Do not call tool_lp directly --- .../amd/build/form_competency_element.min.js | 1 + .../lp/amd/src/form_competency_element.js | 9 +- .../site_competencies_form_element.php | 99 +++++++++++++++++++ admin/tool/lp/lang/en/tool_lp.php | 1 + admin/tool/lp/lib.php | 60 +++++++++++ .../templates/form_competency_list.mustache | 6 +- badges/amd/build/competency.min.js | 1 - badges/criteria/award_criteria_competency.php | 37 ++++--- .../tests/behat/criteria_competency.feature | 12 +-- 9 files changed, 196 insertions(+), 30 deletions(-) create mode 100644 admin/tool/lp/amd/build/form_competency_element.min.js rename badges/amd/src/competency.js => admin/tool/lp/amd/src/form_competency_element.js (95%) create mode 100644 admin/tool/lp/classes/site_competencies_form_element.php rename badges/templates/award_criteria_competencies.mustache => admin/tool/lp/templates/form_competency_list.mustache (92%) delete mode 100644 badges/amd/build/competency.min.js diff --git a/admin/tool/lp/amd/build/form_competency_element.min.js b/admin/tool/lp/amd/build/form_competency_element.min.js new file mode 100644 index 0000000000000..20679d9384eb5 --- /dev/null +++ b/admin/tool/lp/amd/build/form_competency_element.min.js @@ -0,0 +1 @@ +define(["jquery","tool_lp/competencypicker","core/ajax","core/notification","core/templates"],function(a,b,c,d,e){var f=null,g=1,h=function(){var b=a('[data-action="competencies"]').val(),f=[],g=0;if(""!=b)for(b=b.split(","),g=0;g * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -60,7 +59,7 @@ define(['jquery', 'tool_lp/competencypicker', 'core/ajax', 'core/notification', competencies: competencies }; - return Templates.render('core_badges/award_criteria_competencies', context); + return Templates.render('tool_lp/form_competency_list', context); }).then(function(html, js) { Templates.replaceNode($('[data-region="competencies"]'), html, js); return true; @@ -119,7 +118,7 @@ define(['jquery', 'tool_lp/competencypicker', 'core/ajax', 'core/notification', pickerInstance.display(); }; - return /** @alias module:core_badges/competency */ { + return /** @alias module:tool_lp/form_competency_element */ { /** * Listen for clicks on the competency picker and push the changes to the form element. * diff --git a/admin/tool/lp/classes/site_competencies_form_element.php b/admin/tool/lp/classes/site_competencies_form_element.php new file mode 100644 index 0000000000000..270d8a221b46c --- /dev/null +++ b/admin/tool/lp/classes/site_competencies_form_element.php @@ -0,0 +1,99 @@ +. + + +/** + * Course competencies element. + * + * @package tool_lp + * @copyright 2016 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->libdir . '/form/hidden.php'); + +/** + * Course competencies element. + * + * @package tool_lp + * @copyright 2016 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class tool_lp_site_competencies_form_element extends MoodleQuickForm_hidden { + + /** + * Constructor + * + * @param string $elementName Element name + * @param mixed $elementLabel Label(s) for an element + * @param array $options Options to control the element's display + * @param mixed $attributes Either a typical HTML attribute string or an associative array. + */ + public function __construct($elementName=null, $value='', $attributes=null) { + global $OUTPUT; + + if ($elementName == null) { + // This is broken quickforms messing with the constructors. + return; + } + $attributes = array_merge(['data-action' => 'competencies'], $attributes?$attributes:[]); + + parent::__construct($elementName, $value, $attributes); + $this->setType('hidden'); + } + + /** + * Generate the hidden field and the controls to show and pick the competencies. + */ + function toHtml(){ + global $PAGE; + + $html = parent::toHTML(); + + if (!$this->isFrozen()) { + $context = context_system::instance(); + $params = [$context->id]; + // Require some JS to select the competencies. + $PAGE->requires->js_call_amd('tool_lp/form_competency_element', 'init', $params); + $html .= '
'; + $html .= '
'; + $html .= '
'; + $html .= '
'; + $html .= ''; + $html .= '
'; + $html .= '
'; + } + return $html; + } + + /** + * Accepts a renderer + * + * @param object An HTML_QuickForm_Renderer object + * @access public + * @return void + */ + function accept(&$renderer, $required=false, $error=null) + { + $renderer->renderElement($this, false, ''); + } +} diff --git a/admin/tool/lp/lang/en/tool_lp.php b/admin/tool/lp/lang/en/tool_lp.php index 8de80cdcee81d..156299cd95488 100644 --- a/admin/tool/lp/lang/en/tool_lp.php +++ b/admin/tool/lp/lang/en/tool_lp.php @@ -148,6 +148,7 @@ $string['nocompetenciesinevidence'] = 'No competencies have been linked to this evidence.'; $string['nocompetenciesinlearningplan'] = 'No competencies have been linked to this learning plan.'; $string['nocompetenciesintemplate'] = 'No competencies have been linked to this learning plan template.'; +$string['nocompetenciesinlist'] = 'No competencies have been selected.'; $string['nocompetencyframeworks'] = 'No competency frameworks have been created yet.'; $string['nocompetencyselected'] = 'No competency selected'; $string['nocrossreferencedcompetencies'] = 'No other competencies have been cross-referenced to this competency.'; diff --git a/admin/tool/lp/lib.php b/admin/tool/lp/lib.php index c68729b6c9561..55130165e1d21 100644 --- a/admin/tool/lp/lib.php +++ b/admin/tool/lp/lib.php @@ -217,3 +217,63 @@ function tool_lp_get_fontawesome_icon_map() { 'tool_lp:url' => 'fa-external-link' ]; } + +/** + * Render a short bit of information about a competency. + */ +function tool_lp_render_competency_summary(\core_competency\competency $competency, + \core_competency\competency_framework $framework, + $includerelated, + $includecourses) { + global $PAGE; + + if (!get_config('core_competency', 'enabled')) { + return; + } + + $summary = new \tool_lp\output\competency_summary($competency, $framework, $includerelated, $includecourses); + + $output = $PAGE->get_renderer('tool_lp'); + + return $output->render($summary); +} + +/** + * Inject a course competency picker into the form. + * + * @param MoodleQuickForm $mform The actual form object (required to modify the form). + * @param integer $courseid - SITEID or a course id + * @param context $context - The page context + * @param string $elementname - The name of the form element to create + */ +function tool_lp_competency_picker($mform, $courseid, $context, $elementname) { + global $CFG, $COURSE; + + if (!get_config('core_competency', 'enabled')) { + return; + } + + if ($courseid == SITEID) { + if (!has_capability('moodle/competency:competencymanage', $context)) { + return; + } + + MoodleQuickForm::registerElementType('site_competencies', + "$CFG->dirroot/$CFG->admin/tool/lp/classes/site_competencies_form_element.php", + 'tool_lp_site_competencies_form_element'); + $mform->addElement('site_competencies', $elementname); + } else { + if (!has_capability('moodle/competency:coursecompetencymanage', $context)) { + return; + } + + MoodleQuickForm::registerElementType('course_competencies', + "$CFG->dirroot/$CFG->admin/tool/lp/classes/course_competencies_form_element.php", + 'tool_lp_course_competencies_form_element'); + $options = array( + 'courseid' => $COURSE->id + ); + $mform->addElement('course_competencies', $elementname, get_string('modcompetencies', 'tool_lp'), $options); + } + $mform->setType($elementname, PARAM_SEQUENCE); +} diff --git a/badges/templates/award_criteria_competencies.mustache b/admin/tool/lp/templates/form_competency_list.mustache similarity index 92% rename from badges/templates/award_criteria_competencies.mustache rename to admin/tool/lp/templates/form_competency_list.mustache index 3113597f4f19d..b33d886afad87 100644 --- a/badges/templates/award_criteria_competencies.mustache +++ b/admin/tool/lp/templates/form_competency_list.mustache @@ -15,9 +15,9 @@ along with Moodle. If not, see . }} {{! - @template core_badges/award_criteria_competencies + @template tool_lp/form_competency_list - List of competencies for a badge. + List of competencies for a form element. Classes required for JS: * none @@ -60,7 +60,7 @@ {{/competencies}} {{^competencies}} - {{#str}}nocompetencies, core_badges{{/str}} + {{#str}}nocompetenciesinlist, tool_lp{{/str}} {{/competencies}} {{#js}} diff --git a/badges/amd/build/competency.min.js b/badges/amd/build/competency.min.js deleted file mode 100644 index d57565f190aec..0000000000000 --- a/badges/amd/build/competency.min.js +++ /dev/null @@ -1 +0,0 @@ -define(["jquery","tool_lp/competencypicker","core/ajax","core/notification","core/templates"],function(a,b,c,d,e){var f=null,g=1,h=function(){var b=a('[data-action="competencies"]').val(),f=[],g=0;if(""!=b)for(b=b.split(","),g=0;gset('description', ''); } - $summary = new \tool_lp\output\competency_summary($competency, $competency->get_framework(), !$short, !$short); - $str = $OUTPUT->render($summary); - $output[] = $str; + if ($pluginsfunction = get_plugins_with_function('render_competency_summary')) { + foreach ($pluginsfunction as $plugintype => $plugins) { + foreach ($plugins as $pluginfunction) { + $output[] = $pluginfunction($competency, $competency->get_framework(), !$short, !$short); + } + } + } } return '
' . @@ -83,21 +87,25 @@ public function get_options(&$mform) { if (count($this->params)) { $competencies = implode(',', array_keys($this->params)); } - $mform->addElement('static', 'competenciesdescription', '', '
'); - $mform->addElement('hidden', 'competency', $competencies, ['data-action' => 'competencies']); - - $mform->setType('competency', PARAM_RAW); $badge = $DB->get_record('badge', array('id' => $this->badgeid)); + $context = null; + $courseid = 0; + if ($badge->type == BADGE_TYPE_SITE) { $context = context_system::instance(); + $courseid = SITEID; } else if ($badge->type == BADGE_TYPE_COURSE) { $context = context_course::instance($badge->courseid); + $courseid = $badge->courseid; } - $params = [$context->id]; - // Require some JS to select the competencies. - $PAGE->requires->js_call_amd('core_badges/competency', 'init', $params); - - $mform->addElement('button', 'select_competencies', get_string('addcompetency', 'badges'), ['data-action' => 'select-competencies']); + if ($pluginsfunction = get_plugins_with_function('competency_picker')) { + foreach ($pluginsfunction as $plugintype => $plugins) { + foreach ($plugins as $pluginfunction) { + $output[] = $pluginfunction($mform, $courseid, $context, 'competency'); + } + } + } + $mform->getElement('competency')->setValue($competencies); // Add aggregation. if (!$none) { @@ -122,8 +130,11 @@ public function get_options(&$mform) { * @param array $params Values from the form or any other array. */ public function save($params = array()) { - $competencies = explode(',', $params['competency']); + $competencies = $params['competency']; unset($params['competency']); + if (is_string($competencies)) { + $competencies = explode(',', $competencies); + } foreach ($competencies as $competencyid) { $params["competency_{$competencyid}"] = $competencyid; } diff --git a/badges/tests/behat/criteria_competency.feature b/badges/tests/behat/criteria_competency.feature index 17b4eb230eca9..3a8874f3d511b 100644 --- a/badges/tests/behat/criteria_competency.feature +++ b/badges/tests/behat/criteria_competency.feature @@ -31,7 +31,9 @@ Feature: Award badges based on competency completion And "Competency picker" "dialogue" should be visible And I select "comp1" of the competency tree And I click on "Add" "button" in the "Competency picker" "dialogue" + And I wait until the page is ready And I click on "Edit" "link" in the "[data-region='configurecoursecompetencies']" "css_element" + And I wait until the page is ready And I click on "Rating a competency only updates the competency in this course" "text" And I click on "Save changes" "button" in the "Configure course competencies" "dialogue" # Add a badge to the course @@ -46,14 +48,10 @@ Feature: Award badges based on competency completion And I press "Create badge" # Set the competency as a criteria for the badge And I set the field "type" to "Competencies" - And I press "Add competency" - And "Competency picker" "dialogue" should be visible - And I select "comp1" of the competency tree - And I click on "Add" "button" in the "Competency picker" "dialogue" - # And I wait "1" seconds + When I open the autocomplete suggestions list + And I click on "ul[class='form-autocomplete-suggestions'] li" "css_element" And I wait until the page is ready And I press "Save" - # And I wait "1" seconds And I wait until the page is ready # Enable the badge And I press "Enable access" @@ -95,11 +93,9 @@ Feature: Award badges based on competency completion And "Competency picker" "dialogue" should be visible And I select "comp1" of the competency tree And I click on "Add" "button" in the "Competency picker" "dialogue" - # And I wait "1" seconds And I wait until the page is ready And I press "Save" # Enable the badge - # And I wait "1" seconds And I wait until the page is ready And I press "Enable access" And I press "Continue"