Commit bc2ad0c5 authored by Robert Lyon's avatar Robert Lyon

Bug 1409546: Alter annotation blocktype

To allow it to have the select2 for choosing the smart evidence.

Change-Id: I1019e6288f42e543ad199b3204f3b6e4480ad311
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent 093f298d
......@@ -36,10 +36,6 @@ class PluginBlocktypeAnnotation extends MaharaCoreBlocktype {
return false; // true; // need to do more work on aretfact/artefact.php before this can be switched on.
}
public static function override_instance_title(BlockInstance $instance) {
return get_string('Annotation', 'artefact.annotation');
}
public static function allowed_in_view(View $view) {
// Annotations don't make sense in groups?
return $view->get('group') == null;
......@@ -239,10 +235,58 @@ class PluginBlocktypeAnnotation extends MaharaCoreBlocktype {
// The annotation is displayed as html, need to populate its value.
$elements['text']['value'] = $text;
}
$collection = $view->get('collection');
if (is_object($collection) && $collection->get('framework')) {
safe_require('module', 'framework');
$framework = new Framework($collection->get('framework'));
$standards = $framework->standards();
$evidence = $framework->get_evidence($instance->get('id'));
$selectoptions = array();
$selectdesciptions = array();
foreach ($standards['standards'] as $standard) {
if (isset($standard->options)) {
$selectoptions[$standard->id] = array(
'label' => $standard->shortname . ' ' . $standard->name,
'options' => array(),
);
foreach ($standard->options as $option) {
$selectoptions[$standard->id]['options'][$option->id] = $option->shortname . ' ' . $option->name;
$selectdescriptions[$option->id] = $option->description;
}
}
}
$elements['smartevidence'] = array(
'type' => 'select',
'title' => get_string('standard', 'module.framework'),
'optgroups' => $selectoptions,
'isSelect2' => true,
'width' => '280px',
'class' => 'last', // to remove base border
'defaultvalue' => (($evidence) ? $evidence->element : null),
);
array_walk($selectdescriptions, function (&$a, $b) {
$a = '<div class="hidden" id="option_' . $b . '">' . $a . '</div>';
});
$elements['smartevidencedesc'] = array(
'type' => 'html',
'class' => 'htmldescription',
'value' => implode("\n", $selectdescriptions),
'description' => get_string('standarddesc', 'module.framework'), // have desc for 'smartevidence' here so html falls between them
);
if (isset($instance->option) && !empty($instance->option)) {
// Need to add a readonly SmartEvidence field
$elements['smartevidence']['defaultvalue'] = $instance->option;
}
}
return $elements;
}
public static function delete_instance(BlockInstance $instance) {
// If annotation is evidence for SmartEvidence framework we need to delete that as well
if (is_plugin_active('framework', 'module')) {
delete_records('framework_evidence', 'annotation', $instance->get('id'));
}
$configdata = $instance->get('configdata');
if (!empty($configdata)) {
$artefactid = $configdata['artefactid'];
......@@ -255,6 +299,29 @@ class PluginBlocktypeAnnotation extends MaharaCoreBlocktype {
}
}
public static function instance_config_validate(Pieform $form, $values) {
if (!empty($values['smartevidence'])) {
// Check that the new smartevidence standard we are changing to is not alreay covered by another annotation block
$block = $form->get_element('blockconfig');
$view = $form->get_element('id');
require_once('view.php');
$view = new View($view['value']);
$collection = $view->get('collection');
if (is_object($collection) && $collection->get('framework')) {
$annotationid = get_field('framework_evidence', 'annotation',
'view', $view->get('id'),
'framework', $collection->get('framework'),
'element', $values['smartevidence']);
if ($annotationid && $annotationid != $block['value']) {
$result['message'] = get_string('annotationclash', 'module.framework');
$form->set_error('smartevidence', $result['message']);
$form->reply(PIEFORM_ERR, $result);
}
}
}
}
public static function instance_config_save($values, $instance) {
require_once('embeddedimage.php');
......@@ -270,7 +337,7 @@ class PluginBlocktypeAnnotation extends MaharaCoreBlocktype {
// The title will always be Annotation.
$title = get_string('Annotation', 'artefact.annotation');
$data['title'] = $title;
$values['title'] = $title;
$values['title'] = !empty($values['title']) ? $values['title'] : $title;
if (empty($configdata['artefactid'])) {
// This is a new annotation.
$artefact = new ArtefactTypeAnnotation(0, $data);
......@@ -300,10 +367,17 @@ class PluginBlocktypeAnnotation extends MaharaCoreBlocktype {
$values['artefactid'] = $artefact->get('id');
$instance->save_artefact_instance($artefact);
if (is_plugin_active('framework', 'module') && !empty($values['smartevidence'])) {
safe_require('module', 'framework');
$title = get_field('framework_standard_element', 'shortname', 'id', $values['smartevidence']);
$values['title'] = get_string('Annotation', 'artefact.annotation') . ': ' . $title;
$result = Framework::save_evidence_in_block($instance->get('id'), $values['smartevidence']);
}
unset($values['text']);
unset($values['allowfeedback']);
unset($values['annotationreadonlymsg']);
unset($values['smartevidence']);
unset($values['smartevidencedesc']);
// Pass back a list of any other blocks that need to be rendered
// due to this change.
$values['_redrawblocks'] = array_unique(get_column(
......@@ -338,4 +412,22 @@ class PluginBlocktypeAnnotation extends MaharaCoreBlocktype {
set_field('blocktype_installed', 'active', 0, 'artefactplugin', 'annotation');
}
}
public static function get_instance_config_javascript(BlockInstance $instance) {
return <<<EOF
jQuery(function($) {
$("#instconf_smartevidence").select2();
function show_se_desc(id) {
$("#instconf_smartevidencedesc_container div:not(.description)").addClass('hidden');
$("#option_" + id).removeClass('hidden');
}
show_se_desc($("#instconf_smartevidence").val());
$("#instconf_smartevidence").on('change', function() {
show_se_desc($(this).val());
});
});
EOF;
}
}
......@@ -23,3 +23,7 @@ $string['accessdeniednoviews'] = 'You need to add some pages to your collection.
$string['noframeworkselected'] = 'None';
$string['matrixpointupdated'] = "SmartEvidence updated";
$string['matrixpointinserted'] = "SmartEvidence added";
$string['standard'] = 'Standard';
$string['standarddesc'] = 'Select the standard this evidence addresses. You can type into the box to search the standards.';
$string['annotationclash'] = 'There is already an annotation block on the page for this standard';
$string['needtoactivate'] = 'The annotation plugin needs activation. Please ask your site administrator to activate it.';
......@@ -426,13 +426,21 @@ class Framework {
/**
* Get the evidence state for the framework
*
* @param int $userid
* @param int $annotationid
*
* @return evidence(s)
*/
public function get_evidence() {
$outcomes = get_records_array('framework_evidence', 'framework', $this->id);
return $outcomes;
public function get_evidence($annotationid = false) {
$evidence = get_records_array('framework_evidence', 'framework', $this->id);
if (!empty($annotationid) && $evidence) {
foreach ($evidence as $e) {
if ($e->annotation === $annotationid) {
return $e;
}
}
return false;
}
return $evidence;
}
public static function annotation_config_form($data) {
......@@ -460,6 +468,8 @@ class Framework {
$new = false;
}
$title = $annotation->get_title();
$annotation->option = $data->option;
$annotation->frommatrix = true;
list($content, $js, $css) = array_values($annotation->build_configure_form($new));
$return = array(
......@@ -479,7 +489,7 @@ class Framework {
* @param string $element Framework_standard_element id } A unique grouping
* @param string $view View id }
* @param string $annotation Annotation block id (not artefact id)
* @param string $state Either 'begun', 'ready', 'completed'
* @param string $state See constants in this class
* @param string $reviewer The user marking the evidence as completed
*/
public static function save_evidence($id = null, $framework = null, $element = null, $view = null, $annotation = null, $state = Self::EVIDENCE_BEGUN, $reviewer = null) {
......@@ -501,6 +511,9 @@ class Framework {
'state' => $state);
if ($id) {
// update row
if (!empty($element)) {
$fordb['element'] = $element;
}
update_record('framework_evidence', (object) $fordb, (object) array('id' => $id));
}
else {
......@@ -513,6 +526,49 @@ class Framework {
}
return $id;
}
/**
* Save evidence when adding block to page on block edit view
*
* @param string $blockid Block id
* @param string $element The framework_standard_element id
*/
public static function save_evidence_in_block($blockid, $element) {
$evidence = get_record('framework_evidence', 'annotation', $blockid);
$id = !empty($evidence) ? $evidence->id : null;
if (!$id) {
// We need to find the view/framework info via the blockid
if ($records = get_records_sql_array("SELECT bi.view, c.framework FROM {block_instance} bi
JOIN {collection_view} cv ON cv.view = bi.view
JOIN {collection} c ON c.id = cv.collection
WHERE bi.id = ?", array($blockid))) {
$record = $records[0];
try {
$id = self::save_evidence(null, $record->framework, $element, $record->view, $blockid);
return $id;
}
catch (SQLException $e) {
// An error occured like an existing annotation block exist for this view/standard option
return false;
}
}
else {
// block not on a page that is in a collection that is using a framework
return false;
}
}
else {
try {
$id = self::save_evidence($id, null, $element, null, $blockid);
return $id;
}
catch (SQLException $e) {
// An error occured like an existing annotation block exist for this view/standard option
return false;
}
}
}
}
class FrameworkNotFoundException extends NotFoundException {}
......@@ -210,6 +210,19 @@ jQuery(function($) {
});
tinyMCE.idCounter=0;
tinyMCE.execCommand('mceAddEditor', false, "instconf_text");
// Only allow the point selected to be active in the 'Standard' dropdown
$("#instconf_smartevidence option:not(:selected)").prop('disabled', true);
$("#instconf_smartevidence").select2();
function show_se_desc(id) {
$("#instconf_smartevidencedesc_container div:not(.description)").addClass('hidden');
$("#option_" + id).removeClass('hidden');
}
show_se_desc($("#instconf_smartevidence").val());
$("#instconf_smartevidence").on('change', function() {
show_se_desc($(this).val());
});
$('#instconf').on('submit', function(se) {
se.preventDefault();
......
......@@ -19,6 +19,10 @@ safe_require('blocktype', 'annotation');
global $USER;
if (!is_plugin_active('annotation','blocktype')) {
json_reply(true, get_string('needtoactivate', 'module.framework'));
}
$framework = param_integer('framework');
$option = param_integer('option');
$view = param_integer('view');
......
......@@ -307,6 +307,9 @@
h4 {
margin: 0;
}
&.htmldescription {
border-bottom: 1px solid #f0f0f0;
}
}
&.double {
.description {
......
......@@ -68,3 +68,14 @@ Scenario: Installing framework module and activating for an institution
And I press "Save"
And I go to portfolio page "PageB"
Then I should see "Annotation"
# Add another compentency annotation block
And I follow "Edit"
And I expand "General" node
And I wait "2" seconds
And I follow "Annotation"
And I press "Add"
And I set the following fields to these values:
| Annotation | My three cents |
And I set the select2 value "1.1 one point one" for "instconf_smartevidence"
And I press "Save"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment