| 1 | efrain | 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 |   | 
        
           | 1441 | ariadna | 43 |         $versionsofeachquestionbankentry = $deleteallversions
 | 
        
           |  |  | 44 |             ? \question_bank::get_all_versions_of_questions($questionids)
 | 
        
           |  |  | 45 |             : \question_bank::get_version_of_questions($questionids);
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 |         foreach ($versionsofeachquestionbankentry as $entryid => $versions) {
 | 
        
           |  |  | 48 |             // Re-order to the oldest first.
 | 
        
           |  |  | 49 |             $versionsofeachquestionbankentry[$entryid] = array_reverse($versions, true);
 | 
        
           |  |  | 50 |             // Flip the array to list question by question id. [ qid => version ].
 | 
        
           |  |  | 51 |             $questionversions += array_flip($versions);
 | 
        
           | 1 | efrain | 52 |         }
 | 
        
           | 1441 | ariadna | 53 |         // Flatten an array.
 | 
        
           |  |  | 54 |         $questionids = array_merge(...$versionsofeachquestionbankentry);
 | 
        
           | 1 | efrain | 55 |   | 
        
           |  |  | 56 |         // Get the names of all the questions.
 | 
        
           |  |  | 57 |         $questions = $DB->get_records_list('question', 'id', $questionids, '', 'id, name');
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 |         // Build the message.
 | 
        
           |  |  | 60 |         foreach ($questionids as $questionid) {
 | 
        
           |  |  | 61 |             if (questions_in_use([$questionid])) {
 | 
        
           |  |  | 62 |                 $questionnames .= '* ';
 | 
        
           |  |  | 63 |                 $inuse = true;
 | 
        
           |  |  | 64 |             }
 | 
        
           |  |  | 65 |             $questionname = format_string($questions[$questionid]->name);
 | 
        
           | 1441 | ariadna | 66 |   | 
        
           |  |  | 67 |             $a = new \stdClass();
 | 
        
           |  |  | 68 |             $a->name = $questionname;
 | 
        
           |  |  | 69 |             $a->version = $questionversions[$questionid];
 | 
        
           |  |  | 70 |             $questionnames .= get_string('questionnameandquestionversion', 'question', $a) . '<br />';
 | 
        
           | 1 | efrain | 71 |         }
 | 
        
           |  |  | 72 |   | 
        
           |  |  | 73 |         // Add the in-use message if required.
 | 
        
           |  |  | 74 |         if ($inuse) {
 | 
        
           |  |  | 75 |             $questionnames .= '<br />'.get_string('questionsinuse', 'question');
 | 
        
           |  |  | 76 |         }
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |         // Add in the right tile and message text.
 | 
        
           |  |  | 79 |         $confirmtitle = [
 | 
        
           |  |  | 80 |             'confirmtitle' => $countselectedquestion > 1 ? get_string('deleteversiontitle_plural',
 | 
        
           |  |  | 81 |                 'question') : get_string('deleteversiontitle', 'question'),
 | 
        
           |  |  | 82 |         ];
 | 
        
           |  |  | 83 |         $message = get_string('deleteselectedquestioncheck', 'question', $questionnames);
 | 
        
           |  |  | 84 |         if ($deleteallversions) {
 | 
        
           |  |  | 85 |             $confirmtitle = [
 | 
        
           |  |  | 86 |                 'confirmtitle' => get_string('deletequestiontitle', 'question'),
 | 
        
           |  |  | 87 |             ];
 | 
        
           |  |  | 88 |             $message = get_string('deletequestioncheck', 'question', $questionnames);
 | 
        
           |  |  | 89 |             if ($countselectedquestion > 1) {
 | 
        
           |  |  | 90 |                 $confirmtitle = [
 | 
        
           |  |  | 91 |                     'confirmtitle' => get_string('deletequestiontitle_plural', 'question'),
 | 
        
           |  |  | 92 |                 ];
 | 
        
           |  |  | 93 |                 $message = get_string('deletequestionscheck', 'question', $questionnames);
 | 
        
           |  |  | 94 |             }
 | 
        
           |  |  | 95 |         }
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 |         return [$confirmtitle, $message];
 | 
        
           |  |  | 98 |     }
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 |     /**
 | 
        
           |  |  | 101 |      * Delete questions has (single/multiple) version.
 | 
        
           |  |  | 102 |      *
 | 
        
           |  |  | 103 |      * @param array $questionids List of questionid.
 | 
        
           |  |  | 104 |      * @param bool $deleteallversions Delete all question version or not.
 | 
        
           |  |  | 105 |      */
 | 
        
           |  |  | 106 |     public static function delete_questions(array $questionids, bool $deleteallversions): void {
 | 
        
           |  |  | 107 |         if ($deleteallversions) {
 | 
        
           |  |  | 108 |             // Get all the question id from multidimensional array.
 | 
        
           |  |  | 109 |             $listofquestions = \question_bank::get_all_versions_of_questions($questionids);
 | 
        
           |  |  | 110 |             // Flatten an array.
 | 
        
           |  |  | 111 |             $questionids = array_merge(...$listofquestions);
 | 
        
           |  |  | 112 |         }
 | 
        
           |  |  | 113 |         foreach ($questionids as $questionid) {
 | 
        
           |  |  | 114 |             $questionid = (int) $questionid;
 | 
        
           |  |  | 115 |             question_require_capability_on($questionid, 'edit');
 | 
        
           |  |  | 116 |             question_delete_question($questionid);
 | 
        
           |  |  | 117 |         }
 | 
        
           |  |  | 118 |     }
 | 
        
           |  |  | 119 | }
 |