Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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  namespace qbank_bulkmove;
  18  
  19  /**
  20   * Bulk move helper.
  21   *
  22   * @package    qbank_bulkmove
  23   * @copyright  2021 Catalyst IT Australia Pty Ltd
  24   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class helper {
  28  
  29      /**
  30       * Bulk move questions to a category.
  31       *
  32       * @param string $movequestionselected comma separated string of questions to be moved.
  33       * @param \stdClass $tocategory the category where the questions will be moved to.
  34       */
  35      public static function bulk_move_questions(string $movequestionselected, \stdClass $tocategory): void {
  36          global $DB;
  37          if ($questionids = explode(',', $movequestionselected)) {
  38              list($usql, $params) = $DB->get_in_or_equal($questionids);
  39              $sql = "SELECT q.*, c.contextid
  40                        FROM {question} q
  41                        JOIN {question_versions} qv ON qv.questionid = q.id
  42                        JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
  43                        JOIN {question_categories} c ON c.id = qbe.questioncategoryid
  44                       WHERE q.id
  45                       {$usql}";
  46              $questions = $DB->get_records_sql($sql, $params);
  47              foreach ($questions as $question) {
  48                  question_require_capability_on($question, 'move');
  49              }
  50              question_move_questions_to_category($questionids, $tocategory->id);
  51          }
  52      }
  53  
  54      /**
  55       * Get the display data for the move form.
  56       *
  57       * @param array $addcontexts the array of contexts to be considered in order to render the category select menu.
  58       * @param \moodle_url $moveurl the url where the move script will point to.
  59       * @param \moodle_url $returnurl return url in case the form is cancelled.
  60       * @return array the data to be rendered in the mustache where it contains the dropdown, move url and return url.
  61       */
  62      public static function get_displaydata(array $addcontexts, \moodle_url $moveurl, \moodle_url $returnurl): array {
  63          $displaydata = [];
  64          $displaydata ['categorydropdown'] = \qbank_managecategories\helper::question_category_select_menu($addcontexts,
  65              false, 0, '', -1, true);
  66          $displaydata ['moveurl'] = $moveurl;
  67          $displaydata['returnurl'] = $returnurl;
  68          return $displaydata;
  69      }
  70  
  71      /**
  72       * Process the question came from the form post.
  73       *
  74       * @param array $rawquestions raw questions came as a part of post.
  75       * @return array question ids got from the post are processed and structured in an array.
  76       */
  77      public static function process_question_ids(array $rawquestions): array {
  78          $questionids = [];
  79          $questionlist = '';
  80          foreach ($rawquestions as $key => $notused) {
  81              // Parse input for question ids.
  82              if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
  83                  $key = $matches[1];
  84                  $questionids[] = $key;
  85              }
  86          }
  87          if (!empty($questionids)) {
  88              $questionlist = implode(',', $questionids);
  89          }
  90          return [$questionids, $questionlist];
  91      }
  92  }