Skip to content

Commit

Permalink
MDL-66694 mod_forum: Word count and char count columns
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllaó authored and mickhawkins committed Oct 17, 2019
1 parent c77052f commit 74f94df
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
6 changes: 4 additions & 2 deletions mod/forum/db/install.xml
@@ -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"
>
Expand Down Expand Up @@ -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"/>
Expand Down Expand Up @@ -192,4 +194,4 @@
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>
24 changes: 24 additions & 0 deletions mod/forum/db/upgrade.php
Expand Up @@ -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;
}
47 changes: 46 additions & 1 deletion mod/forum/report/summary/classes/summary_table.php
Expand Up @@ -72,6 +72,11 @@ class summary_table extends table_sql {
*/
protected $context = null;

/**
* @var bool
*/
private $showwordcharcounts = null;

/**
* Forum report table constructor.
*
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}
4 changes: 3 additions & 1 deletion mod/forum/report/summary/lang/en/forumreport_summary.php
Expand Up @@ -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';
Expand All @@ -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';
$string['viewsdisclaimer'] = 'Number of views column is not filtered by group';
$string['wordcount'] = 'Word count';
2 changes: 1 addition & 1 deletion mod/forum/version.php
Expand Up @@ -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)

0 comments on commit 74f94df

Please sign in to comment.