Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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  /**
  18   * Question bank column for the duplicate action icon.
  19   *
  20   * @package   qbank_editquestion
  21   * @copyright 2013 The Open University
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace qbank_editquestion;
  26  
  27  use core_question\local\bank\question_action_base;
  28  use moodle_url;
  29  
  30  /**
  31   * Question bank column for the duplicate action icon.
  32   *
  33   * @copyright 2013 The Open University
  34   * @author    2021 Safat Shahin <safatshahin@catalyst-au.net>
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class copy_action extends question_action_base {
  38  
  39      /** @var string avoids repeated calls to get_string('duplicate'). */
  40      protected $strcopy;
  41  
  42      /**
  43       * Contains the url of the edit question page.
  44       * @var moodle_url|string
  45       */
  46      public $duplicatequestionurl;
  47  
  48      public function init(): void {
  49          parent::init();
  50          $this->strcopy = get_string('duplicate');
  51          $this->duplicatequestionurl = new \moodle_url('/question/bank/editquestion/question.php',
  52                  array('returnurl' => $this->qbank->returnurl));
  53          if ($this->qbank->cm !== null) {
  54              $this->duplicatequestionurl->param('cmid', $this->qbank->cm->id);
  55          } else {
  56              $this->duplicatequestionurl->param('courseid', $this->qbank->course->id);
  57          }
  58      }
  59  
  60      /**
  61       * Get the URL for duplicating a question as a moodle_url.
  62       *
  63       * @param int $questionid the question id.
  64       * @return \moodle_url the URL.
  65       */
  66      public function duplicate_question_moodle_url($questionid): moodle_url {
  67          return new \moodle_url($this->duplicatequestionurl, ['id' => $questionid, 'makecopy' => 1]);
  68      }
  69  
  70      protected function get_url_icon_and_label(\stdClass $question): array {
  71          if (!\question_bank::is_qtype_installed($question->qtype)) {
  72              // It sometimes happens that people end up with junk questions
  73              // in their question bank of a type that is no longer installed.
  74              // We cannot do most actions on them, because that leads to errors.
  75              return [null, null, null];
  76          }
  77  
  78          // To copy a question, you need permission to add a question in the same
  79          // category as the existing question, and ability to access the details of
  80          // the question being copied.
  81          if (question_has_capability_on($question, 'add') &&
  82                  (question_has_capability_on($question, 'edit') || question_has_capability_on($question, 'view'))) {
  83              return [$this->duplicate_question_moodle_url($question->id), 't/copy', $this->strcopy];
  84          }
  85          return [null, null, null];
  86      }
  87  }