Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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  
  37  if ($returnurl) {
  38      $returnurl = new moodle_url($returnurl);
  39  }
  40  
  41  \core_question\local\bank\helper::require_plugin_enabled('qbank_deletequestion');
  42  
  43  if ($cmid) {
  44      list($module, $cm) = get_module_from_cmid($cmid);
  45      require_login($cm->course, false, $cm);
  46      $thiscontext = context_module::instance($cmid);
  47  } else if ($courseid) {
  48      require_login($courseid, false);
  49      $thiscontext = context_course::instance($courseid);
  50  } else {
  51      throw new moodle_exception('missingcourseorcmid', 'question');
  52  }
  53  
  54  $contexts = new core_question\local\bank\question_edit_contexts($thiscontext);
  55  $url = new moodle_url('/question/bank/deletequestion/delete.php');
  56  
  57  $PAGE->set_url($url);
  58  $streditingquestions = get_string('deletequestion', 'qbank_deletequestion');
  59  $PAGE->set_title($streditingquestions);
  60  $PAGE->set_heading($COURSE->fullname);
  61  $PAGE->activityheader->disable();
  62  $PAGE->set_secondary_active_tab("questionbank");
  63  
  64  // Unhide a question.
  65  if (($unhide = optional_param('unhide', '', PARAM_INT)) and confirm_sesskey()) {
  66      question_require_capability_on($unhide, 'edit');
  67      $DB->set_field('question_versions', 'status',
  68          \core_question\local\bank\question_version_status::QUESTION_STATUS_READY, ['questionid' => $unhide]);
  69  
  70      // Purge these questions from the cache.
  71      \question_bank::notify_question_edited($unhide);
  72  
  73      redirect($returnurl);
  74  }
  75  
  76  // If user has already confirmed the action.
  77  if ($deleteselected && ($confirm = optional_param('confirm', '', PARAM_ALPHANUM))
  78          && confirm_sesskey()) {
  79      $deleteselected = required_param('deleteselected', PARAM_RAW);
  80      if ($confirm == md5($deleteselected)) {
  81          if ($questionlist = explode(',', $deleteselected)) {
  82              // For each question either hide it if it is in use or delete it.
  83              foreach ($questionlist as $questionid) {
  84                  $questionid = (int)$questionid;
  85                  question_require_capability_on($questionid, 'edit');
  86                  question_delete_question($questionid);
  87              }
  88          }
  89          redirect($returnurl);
  90      } else {
  91          throw new \moodle_exception('invalidconfirm', 'question');
  92      }
  93  }
  94  
  95  echo $OUTPUT->header();
  96  
  97  if ($deleteselected) {
  98      // Make a list of all the questions that are selected.
  99      $rawquestions = $_REQUEST; // This code is called by both POST forms and GET links, so cannot use data_submitted.
 100      $questionlist = '';  // Comma separated list of ids of questions to be deleted.
 101      $questionnames = ''; // String with names of questions separated by <br/> with an asterix in front of those that are in use.
 102      $inuse = false;      // Set to true if at least one of the questions is in use.
 103      foreach ($rawquestions as $key => $value) {    // Parse input for question ids.
 104          if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
 105              $key = $matches[1];
 106              $questionlist .= $key.',';
 107              question_require_capability_on((int)$key, 'edit');
 108              if (questions_in_use(array($key))) {
 109                  $questionnames .= '* ';
 110                  $inuse = true;
 111              }
 112              $questionnames .= $DB->get_field('question', 'name', array('id' => $key)) . '<br />';
 113          }
 114      }
 115      if (!$questionlist) { // No questions were selected.
 116          redirect($returnurl);
 117      }
 118      $questionlist = rtrim($questionlist, ',');
 119  
 120      // Add an explanation about questions in use.
 121      if ($inuse) {
 122          $questionnames .= '<br />'.get_string('questionsinuse', 'question');
 123      }
 124      $deleteurl = new \moodle_url('/question/bank/deletequestion/delete.php',
 125              array('deleteselected' => $questionlist, 'confirm' => md5($questionlist),
 126              'sesskey' => sesskey(), 'returnurl' => $returnurl, 'cmid' => $cmid, 'courseid' => $courseid));
 127  
 128      $continue = new \single_button($deleteurl, get_string('delete'), 'post');
 129      echo $OUTPUT->confirm(get_string('deletequestionscheck', 'question', $questionnames), $continue, $returnurl);
 130  }
 131  
 132  echo $OUTPUT->footer();