Skip to content

Commit

Permalink
MDL-50919 tags: new WS core_tag_get_tags
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Sep 2, 2015
1 parent 8e35585 commit ef4c23c
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 43 deletions.
1 change: 1 addition & 0 deletions lang/en/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
$string['namesalreadybeeingused'] = 'Tag names already being used';
$string['newnamefor'] = 'New name for tag {$a}';
$string['noresultsfor'] = 'No results for "{$a}"';
$string['nothingtoupdate'] = 'Nothing to update';
$string['officialtag'] = 'Official';
$string['otags'] = 'Official tags';
$string['othertags'] = 'Other tags (enter tags separated by commas)';
Expand Down
2 changes: 1 addition & 1 deletion lib/amd/build/tag.min.js

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

60 changes: 38 additions & 22 deletions lib/amd/src/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,25 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str'
currentvalue = target.attr('data-value'),
official = (currentvalue === "1") ? 0 : 1;

ajax.call([{
var promises = ajax.call([{
methodname: 'core_tag_update_tags',
args: { tags : [ { id : tagid , official : official } ] },
done: function(data) {
if (data.tags[0] !== undefined) {
args: { tags : [ { id : tagid , official : official } ] }
}, {
methodname: 'core_tag_get_tags',
args: { tags : [ { id : tagid } ] }
}], true);

$.when.apply($, promises)
.done( function(updateresult, data) {
if (updateresult.warnings[0] === undefined && data.tags[0] !== undefined) {
templates.render('core_tag/tagtype', data.tags[0]).done(function(html) {
update_modified(target);
var parent = target.parent();
target.replaceWith(html);
parent.find('.tagtype').get(0).focus();
});
}
}
}]);
});
});

// Click handler for flagging/resetting tag flag.
Expand All @@ -75,11 +80,17 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str'
currentvalue = target.attr('data-value'),
flag = (currentvalue === "0") ? 1 : 0;

ajax.call([{
var promises = ajax.call([{
methodname: 'core_tag_update_tags',
args: { tags : [ { id : tagid , flag : flag } ] },
done: function(data) {
if (data.tags[0] !== undefined) {
args: { tags : [ { id : tagid , flag : flag } ] }
}, {
methodname: 'core_tag_get_tags',
args: { tags : [ { id : tagid } ] }
}], true);

$.when.apply($, promises)
.done( function(updateresult, data) {
if (updateresult.warnings[0] === undefined && data.tags[0] !== undefined) {
var row = target.closest('tr').get(0);
if (row) {
if (data.tags[0].flag) {
Expand All @@ -95,8 +106,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str'
parent.find('.tagflag').get(0).focus();
});
}
}
}]);
});
});

// Confirmation for single tag delete link.
Expand Down Expand Up @@ -150,23 +160,29 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str'
tagid = target.attr('data-id');

var change_name = function(tagid, newname) {
ajax.call([{
var promises = ajax.call([{
methodname: 'core_tag_update_tags',
args: { tags : [ { id : tagid , rawname : newname } ] },
done: function(data) {
if (data.tags[0] !== undefined) {
args: { tags : [ { id : tagid , rawname : newname } ] }
}, {
methodname: 'core_tag_get_tags',
args: { tags : [ { id : tagid } ] }
}], true);

$.when.apply($, promises)
.done( function(updateresult, data) {
if (updateresult.warnings[0] !== undefined) {
str.get_string('error').done(function(s) {
notification.alert(s, updateresult.warnings[0].message);
});
} else if (data.tags[0] !== undefined) {
templates.render('core_tag/tagname', data.tags[0]).done(function(html) {
update_modified(tdelement);
tdelement.html(html);
$(tdelement.find('.tagnameedit').get(0)).focus();
});
} else if (data.warnings[0] !== undefined) {
str.get_string('error').done(function(s) {
notification.alert(s, data.warnings[0].message);
});
}
}
}]); };
});
};

var turn_editing_off = function() {
$('.tag-management-table td.tageditingon').each(function() {
Expand Down
7 changes: 7 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,13 @@
'type' => 'write',
),

'core_tag_get_tags' => array(
'classname' => 'core_tag_external',
'methodname' => 'get_tags',
'description' => 'Gets tags by their ids',
'type' => 'read',
),

);

$services = array(
Expand Down
117 changes: 105 additions & 12 deletions tag/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public static function update_tags($tags) {
$systemcontext = context_system::instance();
$canmanage = has_capability('moodle/tag:manage', $systemcontext);
$canedit = has_capability('moodle/tag:edit', $systemcontext);
$return = array();
$warnings = array();

if (empty($CFG->usetags)) {
Expand Down Expand Up @@ -112,7 +111,11 @@ public static function update_tags($tags) {
array('description' => 1, 'descriptionformat' => 1));
}
if (count($tag) <= 1) {
// Nothing to do.
$warnings[] = array(
'item' => $tag['id'],
'warningcode' => 'nothingtoupdate',
'message' => get_string('nothingtoupdate', 'core_tag')
);
continue;
}
if (!$tagobject = $DB->get_record('tag', array('id' => $tag['id']))) {
Expand Down Expand Up @@ -155,10 +158,8 @@ public static function update_tags($tags) {
)
));
$event->trigger();
$tagoutput = new \core_tag\output\tag($tagobject);
$return[] = $tagoutput->export_for_template($renderer);
}
return array('tags' => $return, 'warnings' => $warnings);
return array('warnings' => $warnings);
}

/**
Expand All @@ -167,6 +168,99 @@ public static function update_tags($tags) {
* @return external_description
*/
public static function update_tags_returns() {
return new external_single_structure(
array(
'warnings' => new external_warnings()
)
);
}

/**
* Parameters for function get_tags()
*
* @return external_function_parameters
*/
public static function get_tags_parameters() {
return new external_function_parameters(
array(
'tags' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'tag id'),
)
)
)
)
);
}

/**
* Expose to AJAX
*
* @return boolean
*/
public static function get_tags_is_allowed_from_ajax() {
return true;
}

/**
* Get tags by their ids
*
* @param array $tags
*/
public static function get_tags($tags) {
global $CFG, $PAGE, $DB;
require_once($CFG->dirroot.'/tag/lib.php');

// Validate and normalize parameters.
$tags = self::validate_parameters(self::get_tags_parameters(), array('tags' => $tags));

require_login(null, false, null, false, true);

$systemcontext = context_system::instance();
$canmanage = has_capability('moodle/tag:manage', $systemcontext);
$canedit = has_capability('moodle/tag:edit', $systemcontext);

$return = array();
$warnings = array();

if (empty($CFG->usetags)) {
throw new moodle_exception('tagsaredisabled', 'tag');
}

$renderer = $PAGE->get_renderer('core');
foreach ($tags['tags'] as $tag) {
$tag = (array)$tag;
if (!$tagobject = $DB->get_record('tag', array('id' => $tag['id']))) {
$warnings[] = array(
'item' => $tag['id'],
'warningcode' => 'tagnotfound',
'message' => get_string('tagnotfound', 'error')
);
continue;
}
$tagoutput = new \core_tag\output\tag($tagobject);
// Do not return some information to users without permissions.
$rv = $tagoutput->export_for_template($renderer);
if (!$canmanage) {
if (!$canedit) {
unset($rv->official);
}
unset($rv->flag);
unset($rv->changetypeurl);
unset($rv->changeflagurl);
}
$return[] = $rv;
}
return array('tags' => $return, 'warnings' => $warnings);
}

/**
* Return structure for get_tag()
*
* @return external_description
*/
public static function get_tags_returns() {
return new external_single_structure(
array(
'tags' => new external_multiple_structure( new external_single_structure(
Expand All @@ -175,17 +269,16 @@ public static function update_tags_returns() {
'name' => new external_value(PARAM_TAG, 'name'),
'rawname' => new external_value(PARAM_RAW, 'tag raw name (may contain capital letters)'),
'description' => new external_value(PARAM_RAW, 'tag description'),
'descriptionformat' => new external_value(PARAM_INT, 'tag description format'),
'flag' => new external_value(PARAM_INT, 'flag'),
'official' => new external_value(PARAM_INT, 'whether this flag is official'),
'descriptionformat' => new external_format_value(PARAM_INT, 'tag description format'),
'flag' => new external_value(PARAM_INT, 'flag', VALUE_OPTIONAL),
'official' => new external_value(PARAM_INT, 'whether this flag is official', VALUE_OPTIONAL),
'viewurl' => new external_value(PARAM_URL, 'URL to view'),
'changetypeurl' => new external_value(PARAM_URL, 'URL to change type (official or not)'),
'changeflagurl' => new external_value(PARAM_URL, 'URL to set or reset flag'),
), 'event')
'changetypeurl' => new external_value(PARAM_URL, 'URL to change type (official or not)', VALUE_OPTIONAL),
'changeflagurl' => new external_value(PARAM_URL, 'URL to set or reset flag', VALUE_OPTIONAL),
), 'information about one tag')
),
'warnings' => new external_warnings()
)
);
}

}
1 change: 1 addition & 0 deletions tag/tests/behat/edit_tag.feature
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Feature: Users can edit tags to add description or rename
And I click on "Edit tag name" "link" in the "Cat" "table_row"
And I set the field "New name for tag Cat" to "Kitten"
And I press key "13" in the field "New name for tag Cat"
And I wait until the page is ready
Then I should not see "Cat"
And "New name for tag" "field" should not be visible
And I wait until "Kitten" "link" exists
Expand Down
Loading

0 comments on commit ef4c23c

Please sign in to comment.