MDL-66694 mod_forum: Word count and char count columns

This commit is contained in:
David Monllaó 2019-10-02 09:43:45 +08:00 committed by Michael Hawkins
parent c77052fb2b
commit 74f94dfc26
5 changed files with 78 additions and 5 deletions

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/forum/db" VERSION="20190404" COMMENT="XMLDB file for Moodle mod/forum"
<XMLDB PATH="mod/forum/db" VERSION="20191001" COMMENT="XMLDB file for Moodle mod/forum"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
@ -85,6 +85,8 @@
<FIELD NAME="mailnow" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="deleted" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="privatereplyto" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="wordcount" TYPE="int" LENGTH="20" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="charcount" TYPE="int" LENGTH="20" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
@ -192,4 +194,4 @@
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>

View File

@ -157,5 +157,29 @@ function xmldb_forum_upgrade($oldversion) {
// Automatically generated Moodle v3.7.0 release upgrade line.
// Put any upgrade step following this.
if ($oldversion < 2019071901) {
// Define field wordcount to be added to forum_posts.
$table = new xmldb_table('forum_posts');
$field = new xmldb_field('wordcount', XMLDB_TYPE_INTEGER, '20', null, null, null, null, 'privatereplyto');
// Conditionally launch add field wordcount.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Define field charcount to be added to forum_posts.
$table = new xmldb_table('forum_posts');
$field = new xmldb_field('charcount', XMLDB_TYPE_INTEGER, '20', null, null, null, null, 'wordcount');
// Conditionally launch add field charcount.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Forum savepoint reached.
upgrade_mod_savepoint(true, 2019071901, 'forum');
}
return true;
}

View File

@ -72,6 +72,11 @@ class summary_table extends table_sql {
*/
protected $context = null;
/**
* @var bool
*/
private $showwordcharcounts = null;
/**
* Forum report table constructor.
*
@ -120,6 +125,11 @@ class summary_table extends table_sql {
$columnheaders['viewcount'] = get_string('viewcount', 'forumreport_summary');
}
if ($this->show_word_char_counts()) {
$columnheaders['wordcount'] = get_string('wordcount', 'forumreport_summary');
$columnheaders['charcount'] = get_string('charcount', 'forumreport_summary');
}
$columnheaders['earliestpost'] = get_string('earliestpost', 'forumreport_summary');
$columnheaders['latestpost'] = get_string('latestpost', 'forumreport_summary');
@ -440,10 +450,16 @@ class summary_table extends table_sql {
$this->fill_log_summary_temp_table($this->context->id);
$this->sql->basefields .= ', CASE WHEN tmp.viewcount IS NOT NULL THEN tmp.viewcount ELSE 0 END AS viewcount';
$this->sql->basefromjoins .= ' LEFT JOIN {' . self::LOG_SUMMARY_TEMP_TABLE . '} tmp ON tmp.userid = u.id';
$this->sql->basefromjoins .= ' LEFT JOIN {' . self::LOG_SUMMARY_TEMP_TABLE . '} tmp ON tmp.userid = u.id ';
$this->sql->basegroupby .= ', tmp.viewcount';
}
if ($this->show_word_char_counts()) {
// All p.wordcount values should be NOT NULL, this CASE WHEN is an extra just-in-case.
$this->sql->basefields .= ', SUM(CASE WHEN p.wordcount IS NOT NULL THEN p.wordcount ELSE 0 END) AS wordcount';
$this->sql->basefields .= ', SUM(CASE WHEN p.charcount IS NOT NULL THEN p.charcount ELSE 0 END) AS charcount';
}
$this->sql->params = [
'component' => 'mod_forum',
'courseid' => $this->cm->course,
@ -663,4 +679,33 @@ class summary_table extends table_sql {
$this->is_downloading($format, $filename);
$this->out($this->perpage, false);
}
/*
* Should the word / char counts be displayed?
*
* We don't want to show word/char columns if there is any null value because this means
* that they have not been calculated yet.
* @return bool
*/
private function show_word_char_counts(): bool {
global $DB;
if (!is_null($this->showwordcharcounts)) {
return $this->showwordcharcounts;
}
// This should be really fast.
$sql = "SELECT 'x'
FROM {forum_posts} fp
JOIN {forum_discussions} fd ON fd.id = fp.discussion
WHERE fd.forum = :forumid AND (fp.wordcount IS NULL OR fp.charcount IS NULL)";
if ($DB->record_exists_sql($sql, ['forumid' => $this->cm->instance])) {
$this->showwordcharcounts = false;
} else {
$this->showwordcharcounts = true;
}
return $this->showwordcharcounts;
}
}

View File

@ -23,6 +23,7 @@
*/
$string['attachmentcount'] = 'Number of attachments';
$string['charcount'] = 'Char count';
$string['viewcount'] = 'Number of views';
$string['earliestpost'] = 'Earliest post';
$string['filter:groupsbuttonlabel'] = 'Open the groups filter';
@ -38,4 +39,5 @@ $string['replycount'] = 'Number of replies posted';
$string['summary:viewall'] = 'Access summary report data for each user within a given forum or forums';
$string['summary:view'] = 'Access summary report within a given forum or forums';
$string['summarytitle'] = 'Summary report - {$a}';
$string['viewsdisclaimer'] = 'Number of views column is not filtered by group';
$string['viewsdisclaimer'] = 'Number of views column is not filtered by group';
$string['wordcount'] = 'Word count';

View File

@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2019071900; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2019071901; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2019051100; // Requires this Moodle version
$plugin->component = 'mod_forum'; // Full name of the plugin (used for diagnostics)