From 74f94dfc26d0df2fdf6bc9684b408be6c3d43c98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Monlla=C3=B3?= Date: Wed, 2 Oct 2019 09:43:45 +0800 Subject: [PATCH] MDL-66694 mod_forum: Word count and char count columns --- mod/forum/db/install.xml | 6 ++- mod/forum/db/upgrade.php | 24 ++++++++++ .../report/summary/classes/summary_table.php | 47 ++++++++++++++++++- .../summary/lang/en/forumreport_summary.php | 4 +- mod/forum/version.php | 2 +- 5 files changed, 78 insertions(+), 5 deletions(-) diff --git a/mod/forum/db/install.xml b/mod/forum/db/install.xml index ce528a491d4fd..ac12f9bc3315e 100644 --- a/mod/forum/db/install.xml +++ b/mod/forum/db/install.xml @@ -1,5 +1,5 @@ - @@ -85,6 +85,8 @@ + + @@ -192,4 +194,4 @@ - + \ No newline at end of file diff --git a/mod/forum/db/upgrade.php b/mod/forum/db/upgrade.php index 918ec72d6fa67..272e44545e0bf 100644 --- a/mod/forum/db/upgrade.php +++ b/mod/forum/db/upgrade.php @@ -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; } diff --git a/mod/forum/report/summary/classes/summary_table.php b/mod/forum/report/summary/classes/summary_table.php index 117351bffc140..76153e50a77d0 100644 --- a/mod/forum/report/summary/classes/summary_table.php +++ b/mod/forum/report/summary/classes/summary_table.php @@ -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 @@ public function __construct(int $courseid, array $filters, bool $bulkoperations) $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 @@ protected function define_base_sql(): void { $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 @@ public function download($format) { $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; + } } diff --git a/mod/forum/report/summary/lang/en/forumreport_summary.php b/mod/forum/report/summary/lang/en/forumreport_summary.php index 4d5ac13d15aaa..1288ad9963ecf 100644 --- a/mod/forum/report/summary/lang/en/forumreport_summary.php +++ b/mod/forum/report/summary/lang/en/forumreport_summary.php @@ -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['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'; \ No newline at end of file +$string['viewsdisclaimer'] = 'Number of views column is not filtered by group'; +$string['wordcount'] = 'Word count'; diff --git a/mod/forum/version.php b/mod/forum/version.php index 1a5da4cd321af..e3eda7c04b19f 100644 --- a/mod/forum/version.php +++ b/mod/forum/version.php @@ -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)