Commit be9284d6 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

In view stats, fix comment count for comment artefact by doing an extra query


Signed-off-by: default avatarRichard Mansfield <richardm@catalyst.net.nz>
parent 109f669c
......@@ -300,6 +300,25 @@ class ArtefactTypeComment extends ArtefactType {
return $result;
}
public static function count_comments(&$viewids=null, &$artefactids=null) {
if (!empty($viewids)) {
return get_records_sql_assoc('
SELECT c.onview, COUNT(c.artefact) AS comments
FROM {artefact_comment_comment} c
WHERE c.onview IN (' . join(',', $viewids) . ')
GROUP BY c.onview
', array());
}
if (!empty($artefactids)) {
return get_records_sql_assoc('
SELECT c.onartefact, COUNT(c.artefact) AS comments
FROM {artefact_comment_comment} c
WHERE c.onartefact IN (' . join(',', $artefactids) . ')
GROUP BY c.onartefact
', array());
}
}
public static function last_public_comment($view=null, $artefact=null) {
if (!empty($artefact)) {
$where = 'c.onartefact = ?';
......
......@@ -564,7 +564,7 @@ function view_statistics($limit, $offset) {
get_string('view'),
get_string('Owner', 'view'),
get_string('Visits'),
get_string('feedback', 'view'),
get_string('Comments', 'artefact.comment'),
);
$data['table'] = view_stats_table($limit, $offset);
......@@ -611,26 +611,26 @@ function view_stats_table($limit, $offset) {
return $result;
}
$viewdata = get_records_sql_array(
$viewdata = get_records_sql_assoc(
"SELECT
v.id, v.title, v.owner, v.group, v.institution, v.visits,
u.firstname, u.lastname,
COUNT(vf.*) AS comments
u.firstname, u.lastname
FROM {view} v
LEFT JOIN {view_feedback} vf ON v.id = vf.view
LEFT JOIN {usr} u ON v.owner = u.id
LEFT JOIN {group} g ON v.group = g.id
LEFT JOIN {institution} i ON v.institution = i.name
GROUP BY v.id, v.title, v.owner, v.group, v.institution, v.visits,
u.firstname, u.lastname
ORDER BY v.visits DESC",
array(),
$offset,
$limit
);
safe_require('artefact', 'comment');
$comments = ArtefactTypeComment::count_comments(array_keys($viewdata));
foreach ($viewdata as &$v) {
$v->author = $v->owner ? display_name($v->owner) : null;
$v->comments = isset($comments[$v->id]) ? (int) $comments[$v->id]->comments : 0;
}
$smarty = smarty_core();
......
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