Skip to content

Commit

Permalink
Merge branch 'removeDuplicates-567' into 'main'
Browse files Browse the repository at this point in the history
Normaliza palavras-chave para minúsculo e remove duplicadas (OJS/OMP 3.4)

See merge request softwares-pkp/plugins_ojs/keywordCloud!4
  • Loading branch information
thiagolepidus committed Aug 10, 2023
2 parents 71775bb + 75918d5 commit 228a435
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 91 deletions.
21 changes: 11 additions & 10 deletions KeywordCloudBlockPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

namespace APP\plugins\blocks\keywordCloud;

use PKP\plugins\BlockPlugin;
use PKP\cache\CacheManager;
use APP\facades\Repo;
use PKP\facades\Locale;
use APP\submission\Submission;
use PKP\cache\CacheManager;
use PKP\facades\Locale;
use PKP\plugins\BlockPlugin;

class KeywordCloudBlockPlugin extends BlockPlugin
{
Expand Down Expand Up @@ -58,17 +58,17 @@ public function getContents($templateMgr, $request = null)
$cacheManager = CacheManager::getManager();
$cache = $cacheManager->getFileCache(
$context->getId(),
'keywords_'. $locale,
'keywords_' . $locale,
[$this, 'cacheDismiss']
);

$keywords =& $cache->getContents();
$keywords = & $cache->getContents();
$currentCacheTime = time() - $cache->getCacheTime();

if ($currentCacheTime > self::TWO_DAYS_SECONDS) {
$cache->flush();
$cache->setEntireCache($this->getKeywordsJournal($context->getId()));
} elseif ($keywords == "[]") {
} elseif ($keywords == '[]') {
$cache->setEntireCache($this->getKeywordsJournal($context->getId()));
}

Expand All @@ -87,25 +87,26 @@ public function getKeywordsJournal(int $journalId): string
->filterByStatus([Submission::STATUS_PUBLISHED])
->getMany();

$keywords = array();
$keywords = [];
$locale = Locale::getLocale();
foreach ($submissions as $submission) {
$publications = $submission->getPublishedPublications();

foreach ($publications as $publication) {
$publicationKeywords = $publication->getData('keywords', $locale);

if(!is_null($publicationKeywords) and count($publicationKeywords) > 0) {
if (!is_null($publicationKeywords) and count($publicationKeywords) > 0) {
$keywords = array_merge($keywords, $publicationKeywords);
}
}
}

$countKeywords = array_count_values($keywords);
$uniqueKeywords = array_unique(array_map('strtolower', $keywords));
$countKeywords = array_count_values($uniqueKeywords);
arsort($countKeywords, SORT_NUMERIC);

$topKeywords = array_slice($countKeywords, 0, self::KEYWORD_BLOCK_MAX_ITEMS);
$keywords = array();
$keywords = [];

foreach ($topKeywords as $key => $countKey) {
$keywords[] = (object) ['text' => $key, 'size' => $countKey];
Expand Down
160 changes: 81 additions & 79 deletions templates/block.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,91 +19,93 @@
return cores[Math.floor(Math.random()*cores.length)];
{rdelim}
document.addEventListener("DOMContentLoaded", function() {ldelim}
var keywords = {$keywords};
var totalWeight = 0;
var blockWidth = 300;
var blockHeight = 200;
var transitionDuration = 200;
var length_keywords = keywords.length;
var layout = d3.layout.cloud();
{if $keywords}
document.addEventListener("DOMContentLoaded", function() {ldelim}
var keywords = {$keywords};
var totalWeight = 0;
var blockWidth = 300;
var blockHeight = 200;
var transitionDuration = 200;
var length_keywords = keywords.length;
var layout = d3.layout.cloud();
layout.size([blockWidth, blockHeight])
.words(keywords)
.fontSize(function(d)
{ldelim}
return fontSize(+d.size);
{rdelim})
.on('end', draw);
var svg = d3.select("#wordcloud").append("svg")
.attr("viewBox", "0 0 " + blockWidth + " " + blockHeight)
.attr("width", '100%');
function update() {ldelim}
var words = layout.words();
fontSize = d3.scaleLinear().range([16, 34]);
if (words.length) {ldelim}
fontSize.domain([+words[words.length - 1].size || 1, +words[0].size]);
layout.size([blockWidth, blockHeight])
.words(keywords)
.fontSize(function(d)
{ldelim}
return fontSize(+d.size);
{rdelim})
.on('end', draw);
var svg = d3.select("#wordcloud").append("svg")
.attr("viewBox", "0 0 " + blockWidth + " " + blockHeight)
.attr("width", '100%');
function update() {ldelim}
var words = layout.words();
fontSize = d3.scaleLinear().range([16, 34]);
if (words.length) {ldelim}
fontSize.domain([+words[words.length - 1].size || 1, +words[0].size]);
{rdelim}
{rdelim}
{rdelim}
keywords.forEach(function(item,index){ldelim}totalWeight += item.size;{rdelim});
update();
keywords.forEach(function(item,index){ldelim}totalWeight += item.size;{rdelim});
function draw(words, bounds) {ldelim}
var width = layout.size()[0],
height = layout.size()[1];
update();
scaling = bounds
? Math.min(
width / Math.abs(bounds[1].x - width / 2),
width / Math.abs(bounds[0].x - width / 2),
height / Math.abs(bounds[1].y - height / 2),
height / Math.abs(bounds[0].y - height / 2),
) / 2
: 1;
function draw(words, bounds) {ldelim}
var width = layout.size()[0],
height = layout.size()[1];
svg
.append("g")
.attr(
"transform",
"translate(" + [width >> 1, height >> 1] + ")scale(" + scaling + ")",
)
.selectAll("text")
.data(words)
.enter().append("text")
.style("font-size", function(d) {ldelim} return d.size + "px"; {rdelim})
.style("font-family", 'serif')
.style("fill", randomColor)
.style('cursor', 'pointer')
.style('opacity', 0.7)
.attr('class', 'keyword')
.attr("text-anchor", "middle")
.attr("transform", function(d) {ldelim}
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
{rdelim})
.text(function(d) {ldelim} return d.text; {rdelim})
.on("click", function(d, i){ldelim}
window.location = "{url router=$smarty.const.ROUTE_PAGE page="search" query="QUERY_SLUG"}".replace(/QUERY_SLUG/, encodeURIComponent(''+d.text+''));
{rdelim})
.on("mouseover", function(d, i) {ldelim}
d3.select(this).transition()
.duration(transitionDuration)
.style('font-size',function(d) {ldelim} return (d.size + 3) + "px"; {rdelim})
.style('opacity', 1);
{rdelim})
.on("mouseout", function(d, i) {ldelim}
d3.select(this).transition()
.duration(transitionDuration)
.style('font-size',function(d) {ldelim} return d.size + "px"; {rdelim})
.style('opacity', 0.7);
{rdelim})
.on('resize', function() {ldelim} update() {rdelim});
{rdelim}
scaling = bounds
? Math.min(
width / Math.abs(bounds[1].x - width / 2),
width / Math.abs(bounds[0].x - width / 2),
height / Math.abs(bounds[1].y - height / 2),
height / Math.abs(bounds[0].y - height / 2),
) / 2
: 1;
svg
.append("g")
.attr(
"transform",
"translate(" + [width >> 1, height >> 1] + ")scale(" + scaling + ")",
)
.selectAll("text")
.data(words)
.enter().append("text")
.style("font-size", function(d) {ldelim} return d.size + "px"; {rdelim})
.style("font-family", 'serif')
.style("fill", randomColor)
.style('cursor', 'pointer')
.style('opacity', 0.7)
.attr('class', 'keyword')
.attr("text-anchor", "middle")
.attr("transform", function(d) {ldelim}
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
{rdelim})
.text(function(d) {ldelim} return d.text; {rdelim})
.on("click", function(d, i){ldelim}
window.location = "{url router=$smarty.const.ROUTE_PAGE page="search" query="QUERY_SLUG"}".replace(/QUERY_SLUG/, encodeURIComponent(''+d.text+''));
{rdelim})
.on("mouseover", function(d, i) {ldelim}
d3.select(this).transition()
.duration(transitionDuration)
.style('font-size',function(d) {ldelim} return (d.size + 3) + "px"; {rdelim})
.style('opacity', 1);
{rdelim})
.on("mouseout", function(d, i) {ldelim}
d3.select(this).transition()
.duration(transitionDuration)
.style('font-size',function(d) {ldelim} return d.size + "px"; {rdelim})
.style('opacity', 0.7);
{rdelim})
.on('resize', function() {ldelim} update() {rdelim});
{rdelim}
layout.start();
{rdelim});
layout.start();
{rdelim});
{/if}
</script>
</div>
4 changes: 2 additions & 2 deletions version.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<version>
<application>keywordCloud</application>
<type>plugins.blocks</type>
<release>2.0.0.0</release>
<date>2023-07-17</date>
<release>2.0.1.0</release>
<date>2023-08-10</date>
<lazy-load>1</lazy-load>
<class>KeywordCloudBlockPlugin</class>
</version>

0 comments on commit 228a435

Please sign in to comment.