Skip to content

Commit

Permalink
MDL-53254 tags: tag areas inplace editable
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Mar 18, 2016
1 parent fed66ad commit 131b78b
Show file tree
Hide file tree
Showing 16 changed files with 355 additions and 90 deletions.
2 changes: 2 additions & 0 deletions lang/en/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
$string['deleted'] = 'Tag(s) deleted';
$string['deletedcoursetags'] = 'Deleted - Course tags';
$string['description'] = 'Description';
$string['editisstandard'] = 'Change standard tag usage';
$string['editname'] = 'Edit tag name';
$string['edittag'] = 'Edit this tag';
$string['edittagcollection'] = 'Change tag collection';
$string['entertags'] = 'Enter tags...';
$string['edittagcoll'] = 'Edit tag collection {$a}';
$string['errortagfrontpage'] = 'Tagging the site main page is not allowed';
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.

41 changes: 37 additions & 4 deletions lib/amd/src/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str'
/**
* Initialises tag index page.
*
* @method init_tagindex_page
* @method initTagindexPage
*/
init_tagindex_page: function() {
initTagindexPage: function() {
// Click handler for changing tag type.
$('body').delegate('.tagarea[data-ta] a[data-quickload=1]', 'click', function(e) {
e.preventDefault();
Expand All @@ -57,9 +57,9 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str'
/**
* Initialises tag management page.
*
* @method init_manage_page
* @method initManagePage
*/
init_manage_page: function() {
initManagePage: function() {

var update_modified = function(el) {
var row = el.closest('tr').get(0);
Expand Down Expand Up @@ -174,6 +174,39 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str'
}
);
});
},

/**
* Initialises tag collection management page.
*
* @method initManageCollectionsPage
*/
initManageCollectionsPage: function() {
$('body').on('updated', '[data-inplaceeditable]', function(e) {
var ajaxreturn = e.ajaxreturn;
var oldvalue = e.oldvalue;
if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareaenable') {
var areaid = $(this).attr('data-itemid');
$(".tag-collections-table ul[data-collectionid] li[data-areaid="+areaid+"]").addClass('hidden');
var isenabled = ajaxreturn.value;
if (isenabled === '1') {
$(this).closest('tr').removeClass('dimmed_text');
var collid = $(this).closest('tr').find('[data-itemtype="tagareacollection"]').attr("data-value");
$(".tag-collections-table ul[data-collectionid="+collid+"] li[data-areaid="+areaid+"]").removeClass('hidden');
} else {
$(this).closest('tr').addClass('dimmed_text');
}
}
if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareacollection') {
var areaid = $(this).attr('data-itemid');
$(".tag-collections-table ul[data-collectionid] li[data-areaid="+areaid+"]").addClass('hidden');
var collid = $(this).attr('data-value');
var isenabled = $(this).closest('tr').find('[data-itemtype="tagareaenable"]').attr("data-value");
if (isenabled === "1") {
$(".tag-collections-table ul[data-collectionid="+collid+"] li[data-areaid="+areaid+"]").removeClass('hidden');
}
}
});
}
};
});
37 changes: 9 additions & 28 deletions tag/classes/areas_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,15 @@ public function __construct($pageurl) {
foreach ($tagareas as $itemtype => $it) {
foreach ($it as $component => $record) {
$areaname = core_tag_area::display_name($record->component, $record->itemtype);
$baseurl = new moodle_url($pageurl, array('ta' => $record->id, 'sesskey' => sesskey()));
if ($record->enabled) {
$enableurl = new moodle_url($baseurl, array('action' => 'areadisable'));
$enabled = html_writer::link($enableurl, $OUTPUT->pix_icon('i/hide', get_string('disable')));
} else {
$enableurl = new moodle_url($baseurl, array('action' => 'areaenable'));
$enabled = html_writer::link($enableurl, $OUTPUT->pix_icon('i/show', get_string('enable')));
}

if ($record->enabled && empty($record->locked) && count($tagcollections) > 1) {
$changecollurl = new moodle_url($baseurl, array('action' => 'areasetcoll'));

$select = new single_select($changecollurl, 'areacollid', $tagcollections, $record->tagcollid, null);
$select->set_label(get_string('changetagcoll', 'core_tag', $areaname), array('class' => 'accesshide'));
$collectionselect = $OUTPUT->render($select);
} else {
$collectionselect = $tagcollectionsall[$record->tagcollid];
}

if ($record->enabled) {
$changeshowstandardurl = new moodle_url($baseurl, array('action' => 'areasetshowstandard'));
$select = new single_select($changeshowstandardurl, 'showstandard', $standardchoices,
$record->showstandard, null);
$select->set_label(get_string('changeshowstandard', 'core_tag', $areaname), array('class' => 'accesshide'));
$showstandardselect = $OUTPUT->render($select);
} else {
$showstandardselect = $standardchoices[$record->showstandard];
}

$tmpl = new \core_tag\output\tagareaenabled($record);
$enabled = $OUTPUT->render_from_template('core/inplace_editable', $tmpl->export_for_template($OUTPUT));

$tmpl = new \core_tag\output\tagareacollection($record);
$collectionselect = $OUTPUT->render_from_template('core/inplace_editable', $tmpl->export_for_template($OUTPUT));

$tmpl = new \core_tag\output\tagareashowstandard($record);
$showstandardselect = $OUTPUT->render_from_template('core/inplace_editable', $tmpl->export_for_template($OUTPUT));

$this->data[] = array(
$areaname,
Expand Down
6 changes: 3 additions & 3 deletions tag/classes/collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ public static function get_areas($tagcollid) {
* @param int $tagcollid
* @return array
*/
public static function get_areas_names($tagcollid) {
$allitemtypes = core_tag_area::get_areas($tagcollid, true);
public static function get_areas_names($tagcollid, $enabledonly = true) {
$allitemtypes = core_tag_area::get_areas($tagcollid, $enabledonly);
$itemtypes = array();
foreach ($allitemtypes as $itemtype => $it) {
foreach ($it as $component => $v) {
$itemtypes[] = core_tag_area::display_name($component, $itemtype);
$itemtypes[$v->id] = core_tag_area::display_name($component, $itemtype);
}
}
return $itemtypes;
Expand Down
9 changes: 8 additions & 1 deletion tag/classes/collections_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,17 @@ public function __construct($pageurl) {
$component = ($tagcoll->component === 'core' || preg_match('/^core_/', $tagcoll->component)) ?
get_string('coresystem') : get_string('pluginname', $tagcoll->component);
}
$allareas = core_tag_collection::get_areas_names(null, false);
$validareas = core_tag_collection::get_areas_names($tagcoll->id);
$areaslist = array_map(function($key) use ($allareas, $validareas) {
return "<li data-areaid=\"{$key}\" " .
(array_key_exists($key, $validareas) ? "" : "class=\"hidden\"") .
">{$allareas[$key]}</li>";
}, array_keys($allareas));
$this->data[] = array(
html_writer::link($manageurl, $name),
$component,
join(', ', core_tag_collection::get_areas_names($tagcoll->id)),
"<ul data-collectionid=\"{$tagcoll->id}\">" . join('', $areaslist) . '</ul>',
$tagcoll->searchable ? get_string('yes') : '-',
$actions);
$idx++;
Expand Down
2 changes: 1 addition & 1 deletion tag/classes/manage_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function __construct($tagcollid) {

$this->collapsible(true);

$PAGE->requires->js_call_amd('core/tag', 'init_manage_page', array());
$PAGE->requires->js_call_amd('core/tag', 'initManagePage', array());

}

Expand Down
81 changes: 81 additions & 0 deletions tag/classes/output/tagareacollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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/>.

/**
* Contains class core_tag\output\tagareacollection
*
* @package core_tag
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_tag\output;

use context_system;
use lang_string;
use core_tag_area;

/**
* Class to display collection select for the tag area
*
* @package core_tag
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tagareacollection extends \core\output\inplace_editable {

/**
* Constructor.
*
* @param \stdClass $tagarea
*/
public function __construct($tagarea) {
$tagcollections = \core_tag_collection::get_collections_menu(true);
$editable = (count($tagcollections) > 1) && empty($tagarea->locked) &&
has_capability('moodle/tag:manage', context_system::instance());
$areaname = core_tag_area::display_name($tagarea->component, $tagarea->itemtype);
$edithint = new lang_string('edittagcollection', 'core_tag');
$editlabel = new lang_string('changetagcoll', 'core_tag', $areaname);
$value = $tagarea->tagcollid;

parent::__construct('core_tag', 'tagareacollection', $tagarea->id, $editable,
null, $value, $edithint, $editlabel);
$this->set_type_select($tagcollections);
}

/**
* Updates the value in database and returns itself, called from inplace_editable callback
*
* @param int $itemid
* @param mixed $newvalue
* @return \self
*/
public static function update($itemid, $newvalue) {
global $DB;
require_capability('moodle/tag:manage', \context_system::instance());
$tagarea = $DB->get_record('tag_area', array('id' => $itemid), '*', MUST_EXIST);
$newvalue = clean_param($newvalue, PARAM_INT);
$tagcollections = \core_tag_collection::get_collections_menu(true);
if (!array_key_exists($newvalue, $tagcollections)) {
throw new \moodle_exception('invalidparameter', 'debug');
}
$data = array('tagcollid' => $newvalue);
core_tag_area::update($tagarea, $data);
$tagarea->tagcollid = $newvalue;
$tmpl = new self($tagarea);
return $tmpl;
}
}
Loading

0 comments on commit 131b78b

Please sign in to comment.