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.

Differences Between: [Versions 400 and 401] [Versions 401 and 403]

   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   * Delete question page.
  19   *
  20   * This code is based on question/classes/bank/view.php
  21   *
  22   * @package    qbank_deletequestion
  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  
  28  require_once(__DIR__ . '/../../../config.php');
  29  require_once (__DIR__ . '/../../editlib.php');
  30  global $DB, $OUTPUT, $PAGE, $COURSE;
  31  
  32  $deleteselected = optional_param('deleteselected', false, PARAM_BOOL);
  33  $returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
  34  $cmid = optional_param('cmid', 0, PARAM_INT);
  35  $courseid = optional_param('courseid', 0, PARAM_INT);
  36  $deleteall = optional_param('deleteall', false, PARAM_BOOL);
  37  
  38  if ($returnurl) {
  39      $returnurl = new moodle_url($returnurl);
  40  }
  41  
  42  \core_question\local\bank\helper::require_plugin_enabled('qbank_deletequestion');
  43  
  44  if ($cmid) {
  45      list($module, $cm) = get_module_from_cmid($cmid);
  46      require_login($cm->course, false, $cm);
  47      $thiscontext = context_module::instance($cmid);
  48  } else if ($courseid) {
  49      require_login($courseid, false);
  50      $thiscontext = context_course::instance($courseid);
  51  } else {
  52      throw new moodle_exception('missingcourseorcmid', 'question');
  53  }
  54  
  55  $contexts = new core_question\local\bank\question_edit_contexts($thiscontext);
  56  $url = new moodle_url('/question/bank/deletequestion/delete.php');
  57  
  58  $PAGE->set_url($url);
  59  $streditingquestions = get_string('deletequestion', 'qbank_deletequestion');
  60  $PAGE->set_title($streditingquestions);
  61  $PAGE->set_heading($COURSE->fullname);
  62  $PAGE->activityheader->disable();
  63  $PAGE->set_secondary_active_tab("questionbank");
  64  
  65  // Unhide a question.
  66  if (($unhide = optional_param('unhide', '', PARAM_INT)) and confirm_sesskey()) {
  67      question_require_capability_on($unhide, 'edit');
  68      $DB->set_field('question_versions', 'status',
  69          \core_question\local\bank\question_version_status::QUESTION_STATUS_READY, ['questionid' => $unhide]);
  70  
  71      // Purge these questions from the cache.
  72      \question_bank::notify_question_edited($unhide);
  73  
  74      redirect($returnurl);
  75  }
  76  
  77  // If user has already confirmed the action.
  78  if ($deleteselected && ($confirm = optional_param('confirm', '', PARAM_ALPHANUM))
  79          && confirm_sesskey()) {
  80      $deleteselected = required_param('deleteselected', PARAM_RAW);
  81      if ($confirm == md5($deleteselected)) {
  82          if ($questionlist = explode(',', $deleteselected)) {
  83              \qbank_deletequestion\helper::delete_questions($questionlist, $deleteall);
  84          }
  85          redirect($returnurl);
  86      } else {
  87          throw new \moodle_exception('invalidconfirm', 'question');
  88      }
  89  }
  90  
  91  echo $OUTPUT->header();
  92  
  93  if ($deleteselected) {
  94      // Make a list of all the questions that are selected.
  95      $rawquestions = $_REQUEST; // This code is called by both POST forms and GET links, so cannot use data_submitted.
  96      $questionlist = '';  // Comma separated list of ids of questions to be deleted.
  97      foreach ($rawquestions as $key => $value) {    // Parse input for question ids.
  98          if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
  99              $key = $matches[1];
 100              $questionlist .= $key.',';
 101              question_require_capability_on((int)$key, 'edit');
 102          }
 103      }
 104      if (!$questionlist) { // No questions were selected.
 105          redirect($returnurl);
 106      }
 107      $questionlist = rtrim($questionlist, ',');
 108  
 109      $deleteurl = new \moodle_url('/question/bank/deletequestion/delete.php', [
 110          'deleteselected' => $questionlist, 'deleteall' => $deleteall, 'confirm' => md5($questionlist),
 111          'sesskey' => sesskey(), 'returnurl' => $returnurl, 'cmid' => $cmid, 'courseid' => $courseid,
 112      ]);
 113      $continue = new \single_button($deleteurl, get_string('delete'), 'post');
 114  
 115      $questionids = explode(',', $questionlist);
 116      [$displayoptions, $message] = qbank_deletequestion\helper::get_delete_confirmation_message($questionids,
 117          $deleteall);
 118  
 119      echo $OUTPUT->confirm($message, $continue, $returnurl, $displayoptions);
 120  }
 121  
 122  echo $OUTPUT->footer();