Skip to content

Commit

Permalink
Blog: screen loading slow #382997
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuvu authored and sammarshallou committed Jun 3, 2020
1 parent 8c10bf2 commit 1dc07f6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 17 deletions.
2 changes: 1 addition & 1 deletion amd/build/export.min.js

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

2 changes: 2 additions & 0 deletions lang/en/oublog.php
Original file line number Diff line number Diff line change
Expand Up @@ -737,3 +737,5 @@
$string['export:header_date_posted'] = 'Date posted';
$string['export:header_tags'] = 'Tags';
$string['export:header_author'] = 'Author';
$string['tagshowmore'] = 'Show more ...';
$string['tagshowless'] = 'Show less ...';
49 changes: 35 additions & 14 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
* Constant defining the max length of tags
*/
define('OUBLOG_EXPORT_TAGS_LENGTH', 40);
define('OUBLOG_TAGS_SHOW', 500);

/**
* Get a blog from a user id
Expand Down Expand Up @@ -1046,7 +1047,7 @@ function oublog_get_tags_csv($postid) {
* @return array Tag data
*/
function oublog_get_tags($oublog, $groupid, $cm, $oubloginstanceid=null, $individualid=-1, $tagorder = 'alpha',
$masterblog = null) {
$masterblog = null, $limit = null) {
global $CFG, $DB, $USER;
$tags = array();
$params = array();
Expand Down Expand Up @@ -1100,9 +1101,16 @@ function oublog_get_tags($oublog, $groupid, $cm, $oubloginstanceid=null, $indivi
INNER JOIN {oublog_posts} p ON ti.postid = p.id
INNER JOIN {user} u ON u.id = bi.userid
WHERE $sqlwhere
GROUP BY t.id, t.tag
ORDER BY count DESC";

GROUP BY t.id, t.tag";
if ($tagorder == 'alpha') {
$sort = ' ORDER BY t.tag ASC';
} else {
$sort = ' ORDER BY count DESC';
}
$sql = $sql . $sort;
if ($limit > -1) {
$sql = $sql . ' LIMIT ' . $limit;
}
if ($tags = $DB->get_records_sql($sql, $params)) {
$first = array_shift($tags);
$max = $first->count;
Expand All @@ -1117,11 +1125,6 @@ function oublog_get_tags($oublog, $groupid, $cm, $oubloginstanceid=null, $indivi
foreach ($tags as $idx => $tag) {
$tags[$idx]->weight = round(($tag->count-$min)/$delta*4);
}
if ($tagorder == 'alpha') {
uasort($tags, function($a, $b) {
return strcmp ($a->tag, $b->tag);
});
}
}
return($tags);
}
Expand All @@ -1140,27 +1143,45 @@ function oublog_get_tags($oublog, $groupid, $cm, $oubloginstanceid=null, $indivi
* @return array Tag cloud HTML, current filter tag
*/
function oublog_get_tag_cloud($baseurl, $oublog, $groupid, $cm, $oubloginstanceid=null, $individualid=-1, $tagorder,
$masterblog = null) {
$masterblog = null, $limit = null) {
global $PAGE;
$cloud = '';
$currenttag = $PAGE->url->get_param('tag');
$currentfiltertag = '';
$urlparts= array();

$baseurl = oublog_replace_url_param($baseurl, 'tag');
if (!$tags = oublog_get_tags($oublog, $groupid, $cm, $oubloginstanceid, $individualid, $tagorder, $masterblog)) {
$baseurl = oublog_replace_url_param($baseurl, 'taglimit');

if (!$tags = oublog_get_tags($oublog, $groupid, $cm, $oubloginstanceid, $individualid, $tagorder, $masterblog, $limit)) {
return [$cloud, $currentfiltertag];
}

$cloud .= html_writer::start_tag('div', array('class' => 'oublog-tag-items'));
foreach ($tags as $tag) {
$cloud .= '<a href="'.$baseurl.'&amp;tag='.urlencode($tag->tag).'" class="oublog-tag-cloud-'.
$tag->weight.'"><span class="oublog-tagname">'.strtr(($tag->tag), array(' '=>'&nbsp;')).
'</span><span class="oublog-tagcount">('.$tag->count.')</span></a> ';
if ($limit == -1) {
$cloud .= '<a href="'.$baseurl.'&amp;tag='.urlencode($tag->tag).
'&amp;taglimit='. urlencode($limit) . '" class="oublog-tag-cloud-'.
$tag->weight.'"><span class="oublog-tagname">'.strtr(($tag->tag), array(' '=>'&nbsp;')).
'</span><span class="oublog-tagcount">('.$tag->count.')</span></a> ';
} else {
$cloud .= '<a href="'.$baseurl.'&amp;tag='.urlencode($tag->tag).'" class="oublog-tag-cloud-'.
$tag->weight.'"><span class="oublog-tagname">'.strtr(($tag->tag), array(' '=>'&nbsp;')).
'</span><span class="oublog-tagcount">('.$tag->count.')</span></a> ';
}

if (!is_null($currenttag) && $tag->tag == $currenttag) {
$currentfiltertag = $tag;
}
}
if (count($tags) >= OUBLOG_TAGS_SHOW && $limit != -1) {
$showmore = get_string('tagshowmore', 'oublog');
$cloud .= '<a href="'. $baseurl . '&amp;taglimit=-1" class="mod-oublog-tags-show-more">' . $showmore . '</a>';
}
if ($limit == -1) {
$showless = get_string('tagshowless', 'oublog');
$cloud .= '<a href="'. $baseurl . '&amp;taglimit=' . OUBLOG_TAGS_SHOW . '" class="mod-oublog-tags-show-less">' . $showless . '</a>';
}
$cloud .= html_writer::end_tag('div');

return [$cloud, $currentfiltertag];
Expand Down
4 changes: 4 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -761,3 +761,7 @@ oublog-filter-tagcount {
height: 18px;
float: right;
}
.mod-oublog-tags-show-more,
.mod-oublog-tags-show-less {
display: inline-block;
}
8 changes: 6 additions & 2 deletions view.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
$tag = optional_param('tag', null, PARAM_TAG); // Tag to display.
$page = optional_param('page', 0, PARAM_INT);
$tagorder = optional_param('tagorder', '', PARAM_ALPHA);// Tag display order.
$taglimit = optional_param('taglimit', OUBLOG_TAGS_SHOW, PARAM_INT);// Tag display order.

// Set user value if u (username) set.
if ($username != '') {
Expand All @@ -53,7 +54,7 @@
}

$url = new moodle_url('/mod/oublog/view.php', array('id' => $id, 'user' => $user,
'page' => $page, 'tag' => $tag, 'tagorder' => $tagorder));
'page' => $page, 'tag' => $tag, 'tagorder' => $tagorder, 'taglimit' => $taglimit));

$PAGE->set_url($url);

Expand Down Expand Up @@ -180,6 +181,9 @@
if ($tag) {
$returnurl .= '&amp;tag='.urlencode($tag);
}
if ($taglimit) {
$returnurl .= '&amp;taglimit='.urlencode($taglimit);
}

// Set-up individual.
$currentindividual = -1;
Expand Down Expand Up @@ -332,7 +336,7 @@

// Tag Cloud.
list ($tags, $currentfiltertag) = oublog_get_tag_cloud($returnurl, $oublog, $currentgroup, $cm,
$oubloginstanceid, $currentindividual, $tagorder, $masterblog);
$oubloginstanceid, $currentindividual, $tagorder, $masterblog, $taglimit);
if ($tags) {
$bc = new block_contents();
$bc->attributes['id'] = 'oublog-tags';
Expand Down

0 comments on commit 1dc07f6

Please sign in to comment.