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_deletequestion;
  18  
  19  /**
  20   * Class helper of qbank_deletequestion.
  21   *
  22   * @package qbank_deletequestion
  23   * @copyright 2023 The Open University
  24   * @since Moodle 4.2
  25   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class helper {
  28  
  29      /**
  30       * Get the confirmation message of delete question.
  31       *
  32       * @param array $questionids List of id questions.
  33       * @param bool $deleteallversions Delete all question version or not.
  34       * @return array List confirmation message.
  35       */
  36      public static function get_delete_confirmation_message(array $questionids, bool $deleteallversions): array {
  37          global $DB;
  38          $questionnames = '';
  39          $inuse = false;
  40          $questionversions = [];
  41          $countselectedquestion = count($questionids);
  42  
  43          if ($deleteallversions) {
  44              $versionsofeachquestionbankentry = \question_bank::get_all_versions_of_questions($questionids);
  45              foreach ($versionsofeachquestionbankentry as $entryid => $versions) {
  46                  // Re-order to oldest first.
  47                  $versionsofeachquestionbankentry[$entryid] = array_reverse($versions, true);
  48                  // Flip the array to list question by question id. [ qid => version ].
  49                  $questionversions += array_flip($versions);
  50              }
  51              // Flatten an array.
  52              $questionids = array_merge(...$versionsofeachquestionbankentry);
  53          }
  54  
  55          // Get the names of all the questions.
  56          $questions = $DB->get_records_list('question', 'id', $questionids, '', 'id, name');
  57  
  58          // Build the message.
  59          foreach ($questionids as $questionid) {
  60              if (questions_in_use([$questionid])) {
  61                  $questionnames .= '* ';
  62                  $inuse = true;
  63              }
  64              $questionname = format_string($questions[$questionid]->name);
  65              if (isset($questionversions[$questionid])) {
  66                  $a = new \stdClass();
  67                  $a->name = $questionname;
  68                  $a->version = $questionversions[$questionid];
  69                  $questionnames .= get_string('questionnameandquestionversion',
  70                      'question', $a) . '<br />';
  71              } else {
  72                  $questionnames .= $questionname . '<br />';
  73              }
  74          }
  75  
  76          // Add the in-use message if required.
  77          if ($inuse) {
  78              $questionnames .= '<br />'.get_string('questionsinuse', 'question');
  79          }
  80  
  81          // Add in the right tile and message text.
  82          $confirmtitle = [
  83              'confirmtitle' => $countselectedquestion > 1 ? get_string('deleteversiontitle_plural',
  84                  'question') : get_string('deleteversiontitle', 'question'),
  85          ];
  86          $message = get_string('deleteselectedquestioncheck', 'question', $questionnames);
  87          if ($deleteallversions) {
  88              $confirmtitle = [
  89                  'confirmtitle' => get_string('deletequestiontitle', 'question'),
  90              ];
  91              $message = get_string('deletequestioncheck', 'question', $questionnames);
  92              if ($countselectedquestion > 1) {
  93                  $confirmtitle = [
  94                      'confirmtitle' => get_string('deletequestiontitle_plural', 'question'),
  95                  ];
  96                  $message = get_string('deletequestionscheck', 'question', $questionnames);
  97              }
  98          }
  99  
 100          return [$confirmtitle, $message];
 101      }
 102  
 103      /**
 104       * Delete questions has (single/multiple) version.
 105       *
 106       * @param array $questionids List of questionid.
 107       * @param bool $deleteallversions Delete all question version or not.
 108       */
 109      public static function delete_questions(array $questionids, bool $deleteallversions): void {
 110          if ($deleteallversions) {
 111              // Get all the question id from multidimensional array.
 112              $listofquestions = \question_bank::get_all_versions_of_questions($questionids);
 113              // Flatten an array.
 114              $questionids = array_merge(...$listofquestions);
 115          }
 116          foreach ($questionids as $questionid) {
 117              $questionid = (int) $questionid;
 118              question_require_capability_on($questionid, 'edit');
 119              question_delete_question($questionid);
 120          }
 121      }
 122  }