Skip to content

Commit

Permalink
MDL-63876 badges: Do not call tool_lp directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Damyon Wiese committed Mar 29, 2019
1 parent f94786e commit da5e19a
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 30 deletions.
1 change: 1 addition & 0 deletions admin/tool/lp/amd/build/form_competency_element.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -16,9 +16,8 @@
/**
* Badge select competency actions
*
* @module core_badges/competency
* @package core
* @class competency
* @module tool_lp/form_competency_element
* @package tool_lp
* @copyright 2019 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
99 changes: 99 additions & 0 deletions admin/tool/lp/classes/site_competencies_form_element.php
@@ -0,0 +1,99 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.


/**
* 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 .= '<div class="form-group row">';
$html .= '<div class="col-md-3"></div>';
$html .= '<div class="col-md-9">';
$html .= '<div data-region="competencies"></div>';
$html .= '<div class="mt-3">';
$html .= '<a class="btn btn-secondary" role="button" data-action="select-competencies">' . get_string('addcompetency', 'tool_lp') . '</a>';
$html .= '</div>';
$html .= '</div>';
$html .= '</div>';
}
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, '');
}
}
1 change: 1 addition & 0 deletions admin/tool/lp/lang/en/tool_lp.php
Expand Up @@ -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.';
Expand Down
60 changes: 60 additions & 0 deletions admin/tool/lp/lib.php
Expand Up @@ -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);
}
Expand Up @@ -15,9 +15,9 @@
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@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
Expand Down Expand Up @@ -60,7 +60,7 @@
{{/competencies}}
</ul>
{{^competencies}}
{{#str}}nocompetencies, core_badges{{/str}}
{{#str}}nocompetenciesinlist, tool_lp{{/str}}
{{/competencies}}
</div>
{{#js}}
Expand Down
1 change: 0 additions & 1 deletion badges/amd/build/competency.min.js

This file was deleted.

37 changes: 24 additions & 13 deletions badges/criteria/award_criteria_competency.php
Expand Up @@ -56,9 +56,13 @@ public function get_details($short = '') {
if ($short) {
$competency->set('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 '<dl><dd class="p-3 mb-2 bg-light text-dark border">' .
Expand All @@ -83,21 +87,25 @@ public function get_options(&$mform) {
if (count($this->params)) {
$competencies = implode(',', array_keys($this->params));
}
$mform->addElement('static', 'competenciesdescription', '', '<div data-region="competencies"></div>');
$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) {
Expand All @@ -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;
}
Expand Down
12 changes: 4 additions & 8 deletions badges/tests/behat/criteria_competency.feature
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit da5e19a

Please sign in to comment.