Search moodle.org's
Developer Documentation

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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   * Adds a search area to the queue for indexing.
      19   *
      20   * @package core_search
      21   * @copyright 2017 The Open University
      22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      23   */
      24  
      25  define('NO_OUTPUT_BUFFERING', true);
      26  
      27  require(__DIR__ . '/../config.php');
      28  
      29  // Check access.
      30  require_once($CFG->libdir . '/adminlib.php');
      31  
      32  admin_externalpage_setup('searchareas', '', null, (new moodle_url('/admin/searchreindex.php'))->out(false));
      33  
      34  // Get area parameter and check it exists.
      35  $areaid = required_param('areaid', PARAM_ALPHAEXT);
      36  $area = \core_search\manager::get_search_area($areaid);
      37  if ($area === false) {
      38      throw new moodle_exception('invalidrequest');
      39  }
      40  $areaname = $area->get_visible_name();
      41  
      42  // Start page output.
      43  $heading = get_string('gradualreindex', 'search', '');
      44  $PAGE->set_title($PAGE->title . ': ' . $heading);
      45  $PAGE->navbar->add($heading);
      46  echo $OUTPUT->header();
      47  echo $OUTPUT->heading($heading);
      48  
      49  // If sesskey is supplied, actually carry out the action.
      50  if (optional_param('sesskey', '', PARAM_ALPHANUM)) {
      51      require_sesskey();
      52  
      53      // Get all contexts for search area. This query can take time in large cases.
      54      \core_php_time_limit::raise(0);
      55      $contextiterator = $area->get_contexts_to_reindex();
      56  
      57      $progress = new \core\progress\display_if_slow('');
      58      $progress->start_progress($areaname);
      59  
      60      // Request reindexing for each context (with low priority).
      61      $count = 0;
      62      foreach ($contextiterator as $context) {
      63          \core_php_time_limit::raise(30);
      64          \core_search\manager::request_index($context, $area->get_area_id(),
      65                  \core_search\manager::INDEX_PRIORITY_REINDEXING);
      66          $progress->progress();
      67          $count++;
      68      }
      69  
      70      // Unset the iterator which should close the recordset (if there is one).
      71      unset($contextiterator);
      72  
      73      $progress->end_progress();
      74  
      75      $a = (object)['name' => html_writer::tag('strong', $areaname), 'count' => $count];
      76      echo $OUTPUT->box(get_string('gradualreindex_queued', 'search', $a));
      77  
      78      echo $OUTPUT->continue_button(new moodle_url('/admin/searchareas.php'));
      79  } else {
      80      // Display confirmation prompt.
      81      echo $OUTPUT->confirm(get_string('gradualreindex_confirm', 'search', html_writer::tag('strong', $areaname)),
      82              new single_button(new moodle_url('/admin/searchreindex.php', ['areaid' => $areaid,
      83                  'sesskey' => sesskey()]), get_string('continue'), 'post', true),
      84              new single_button(new moodle_url('/admin/searchareas.php'), get_string('cancel'), 'get'));
      85  }
      86  
      87  echo $OUTPUT->footer();