Skip to content

Commit

Permalink
MDL-66694 mod_forum: Upgrade forum post word and char counts
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 3eb0a82 commit 7d8f604
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
48 changes: 48 additions & 0 deletions mod/forum/classes/task/refresh_forum_post_counts.php
@@ -0,0 +1,48 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Adhoc task that updates all of the existing forum_post records with no wordcount or no charcount.
*
* @package mod_forum
* @copyright 2019 David Monllao
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_forum\task;

defined('MOODLE_INTERNAL') || die();

/**
* Adhoc task that updates all of the existing forum_post records with no wordcount or no charcount.
*
* @package mod_forum
* @copyright 2019 David Monllao
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class refresh_forum_post_counts extends \core\task\adhoc_task {

/**
* Run the task to refresh calendar events.
*/
public function execute() {
global $CFG;

require_once($CFG->dirroot . '/mod/forum/lib.php');

mod_forum_update_null_forum_post_counts(5000);
}
}
15 changes: 15 additions & 0 deletions mod/forum/db/upgrade.php
Expand Up @@ -181,5 +181,20 @@ function xmldb_forum_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2019071901, 'forum');
}

if ($oldversion < 2019071902) {
// Create adhoc task for upgrading of existing forum_posts.
$record = new \stdClass();
$record->classname = '\mod_forum\task\refresh_forum_post_counts';
$record->component = 'mod_forum';

// Next run time based from nextruntime computation in \core\task\manager::queue_adhoc_task().
$nextruntime = time() - 1;
$record->nextruntime = $nextruntime;
$DB->insert_record('task_adhoc', $record);

// Main savepoint reached.
upgrade_mod_savepoint(true, 2019071902, 'forum');
}

return true;
}
23 changes: 23 additions & 0 deletions mod/forum/lib.php
Expand Up @@ -6727,3 +6727,26 @@ function mod_forum_user_preferences() {

return $preferences;
}

/**
* Updates null forum post counts according to the post message.
*
* @param int $limit The number of records to update
* @return null
*/
function mod_forum_update_null_forum_post_counts(int $limit) {
global $DB;

$select = 'wordcount IS NULL OR charcount IS NULL';
$recordset = $DB->get_recordset_select('forum_posts', $select, null, 'discussion', 'id, message', 0, $limit);
if (!$recordset->valid()) {
$recordset->close();
return;
}

foreach ($recordset as $record) {
$countsupdate = \mod_forum\local\entities\post::add_message_counts($record);
$DB->update_record('forum_posts', $countsupdate);
}
$recordset->close();
}
2 changes: 1 addition & 1 deletion mod/forum/version.php
Expand Up @@ -24,6 +24,6 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2019071901; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2019071902; // 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 7d8f604

Please sign in to comment.