Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Adhoc task that updates all of the existing forum_post records with no wordcount or no charcount.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2019 David Monllao
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_forum\task;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Adhoc task that updates all of the existing forum_post records with no wordcount or no charcount.
  31   *
  32   * @package     mod_forum
  33   * @copyright   2019 David Monllao
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class refresh_forum_post_counts extends \core\task\adhoc_task {
  37  
  38      /**
  39       * Run the task to populate word and character counts on existing forum posts.
  40       * If the maximum number of records are updated, the task re-queues itself,
  41       * as there may be more records to process.
  42       */
  43      public function execute() {
  44          if ($this->update_null_forum_post_counts()) {
  45              \core\task\manager::queue_adhoc_task(new refresh_forum_post_counts());
  46          }
  47      }
  48  
  49      /**
  50       * Updates null forum post counts according to the post message.
  51       *
  52       * @return bool Whether there may be more rows to process
  53       */
  54      protected function update_null_forum_post_counts(): bool {
  55          global $CFG, $DB;
  56  
  57          // Default to chunks of 5000 records per run, unless overridden in config.php.
  58          $chunksize = $CFG->forumpostcountchunksize ?? 5000;
  59  
  60          // Initialize counter.
  61          $recordscount = 0;
  62  
  63          $select = 'wordcount IS NULL OR charcount IS NULL';
  64          $recordset = $DB->get_recordset_select('forum_posts', $select, null, 'discussion', 'id, message', 0, $chunksize);
  65  
  66          if (!$recordset->valid()) {
  67              $recordset->close();
  68              return false;
  69          }
  70  
  71          foreach ($recordset as $record) {
  72              \mod_forum\local\entities\post::add_message_counts($record);
  73              $DB->update_record('forum_posts', $record);
  74              $recordscount++;
  75          }
  76  
  77          $recordset->close();
  78  
  79          return ($recordscount == $chunksize);
  80      }
  81  }