| 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 | /**
 | 
        
           |  |  | 18 |  * Action to delete (or hide) a question, or restore a previously hidden question.
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package   qbank_deletequestion
 | 
        
           |  |  | 21 |  * @copyright 2009 Tim Hunt
 | 
        
           |  |  | 22 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | namespace qbank_deletequestion;
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | use core_question\local\bank\question_version_status;
 | 
        
           |  |  | 28 | use core_question\local\bank\question_action_base;
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 | /**
 | 
        
           |  |  | 31 |  * Action to delete (or hide) a question, or restore a previously hidden question.
 | 
        
           |  |  | 32 |  *
 | 
        
           |  |  | 33 |  * @package   qbank_deletequestion
 | 
        
           |  |  | 34 |  * @copyright 2009 Tim Hunt
 | 
        
           |  |  | 35 |  * @author    2021 Safat Shahin <safatshahin@catalyst-au.net>
 | 
        
           |  |  | 36 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 37 |  */
 | 
        
           |  |  | 38 | class delete_action extends question_action_base {
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 |     /**
 | 
        
           |  |  | 41 |      * @var string $strdelete
 | 
        
           |  |  | 42 |      */
 | 
        
           |  |  | 43 |     protected $strdelete;
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 |     /**
 | 
        
           |  |  | 46 |      * @var string $strrestore
 | 
        
           |  |  | 47 |      */
 | 
        
           |  |  | 48 |     protected $strrestore;
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |     /**
 | 
        
           |  |  | 51 |      * Contains the url of the delete question page.
 | 
        
           |  |  | 52 |      * @var \moodle_url|string
 | 
        
           |  |  | 53 |      */
 | 
        
           |  |  | 54 |     public $deletequestionurl;
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |     /**
 | 
        
           |  |  | 57 |      * Array of the return parameters.
 | 
        
           |  |  | 58 |      * @var array $returnparams
 | 
        
           |  |  | 59 |      */
 | 
        
           |  |  | 60 |     protected $returnparams;
 | 
        
           |  |  | 61 |   | 
        
           |  |  | 62 |     public function init(): void {
 | 
        
           |  |  | 63 |         parent::init();
 | 
        
           |  |  | 64 |         $this->strdelete = get_string('delete');
 | 
        
           |  |  | 65 |         $this->strrestore = get_string('restore');
 | 
        
           |  |  | 66 |         $this->deletequestionurl = new \moodle_url('/question/bank/deletequestion/delete.php');
 | 
        
           | 1441 | ariadna | 67 |         $this->returnparams['cmid'] = $this->qbank->cm->id;
 | 
        
           |  |  | 68 |   | 
        
           | 1 | efrain | 69 |         if (!empty($this->qbank->returnurl)) {
 | 
        
           |  |  | 70 |             $this->returnparams['returnurl'] = $this->qbank->returnurl;
 | 
        
           |  |  | 71 |         }
 | 
        
           |  |  | 72 |     }
 | 
        
           |  |  | 73 |   | 
        
           |  |  | 74 |     public function get_menu_position(): int {
 | 
        
           | 1441 | ariadna | 75 |         return 2000;
 | 
        
           | 1 | efrain | 76 |     }
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |     protected function get_url_icon_and_label(\stdClass $question): array {
 | 
        
           |  |  | 79 |         if (!question_has_capability_on($question, 'edit')) {
 | 
        
           |  |  | 80 |             return [null, null, null];
 | 
        
           |  |  | 81 |         }
 | 
        
           |  |  | 82 |         if ($question->status === question_version_status::QUESTION_STATUS_HIDDEN) {
 | 
        
           |  |  | 83 |             $hiddenparams = array(
 | 
        
           |  |  | 84 |                     'unhide' => $question->id,
 | 
        
           |  |  | 85 |                     'sesskey' => sesskey());
 | 
        
           |  |  | 86 |             $hiddenparams = array_merge($hiddenparams, $this->returnparams);
 | 
        
           |  |  | 87 |             $url = new \moodle_url($this->deletequestionurl, $hiddenparams);
 | 
        
           |  |  | 88 |             return [$url, 't/restore', $this->strrestore];
 | 
        
           |  |  | 89 |         } else {
 | 
        
           |  |  | 90 |             $deleteparams = array(
 | 
        
           |  |  | 91 |                     'deleteselected' => $question->id,
 | 
        
           |  |  | 92 |                     'q' . $question->id => 1,
 | 
        
           |  |  | 93 |                     'sesskey' => sesskey());
 | 
        
           |  |  | 94 |             $deleteparams = array_merge($deleteparams, $this->returnparams);
 | 
        
           | 1441 | ariadna | 95 |             if (!$this->qbank->is_listing_specific_versions()) {
 | 
        
           | 1 | efrain | 96 |                 $deleteparams['deleteall'] = 1;
 | 
        
           |  |  | 97 |             }
 | 
        
           |  |  | 98 |             $url = new \moodle_url($this->deletequestionurl, $deleteparams);
 | 
        
           |  |  | 99 |             return [$url, 't/delete', $this->strdelete];
 | 
        
           |  |  | 100 |         }
 | 
        
           |  |  | 101 |     }
 | 
        
           | 1441 | ariadna | 102 |   | 
        
           |  |  | 103 |     /**
 | 
        
           |  |  | 104 |      * Override method to get url and label for delete action to add the text-danger class.
 | 
        
           |  |  | 105 |      *
 | 
        
           |  |  | 106 |      * @param \stdClass $question
 | 
        
           |  |  | 107 |      * @return \action_menu_link|null
 | 
        
           |  |  | 108 |      */
 | 
        
           |  |  | 109 |     public function get_action_menu_link(\stdClass $question): ?\action_menu_link {
 | 
        
           |  |  | 110 |         $deletelink = parent::get_action_menu_link($question);
 | 
        
           |  |  | 111 |         if ($deletelink !== null) {
 | 
        
           |  |  | 112 |             $deletelink->add_class('text-danger');
 | 
        
           |  |  | 113 |         }
 | 
        
           |  |  | 114 |         return $deletelink;
 | 
        
           |  |  | 115 |     }
 | 
        
           | 1 | efrain | 116 | }
 |