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.
   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\menu_action_column_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_column extends menu_action_column_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      public function get_name() {
  61          return 'copyaction';
  62      }
  63  
  64      /**
  65       * Get the URL for duplicating a question as a moodle_url.
  66       *
  67       * @param int $questionid the question id.
  68       * @return \moodle_url the URL.
  69       */
  70      public function duplicate_question_moodle_url($questionid): moodle_url {
  71          return new \moodle_url($this->duplicatequestionurl, ['id' => $questionid, 'makecopy' => 1]);
  72      }
  73  
  74      protected function get_url_icon_and_label(\stdClass $question): array {
  75          if (!\question_bank::is_qtype_installed($question->qtype)) {
  76              // It sometimes happens that people end up with junk questions
  77              // in their question bank of a type that is no longer installed.
  78              // We cannot do most actions on them, because that leads to errors.
  79              return [null, null, null];
  80          }
  81  
  82          // To copy a question, you need permission to add a question in the same
  83          // category as the existing question, and ability to access the details of
  84          // the question being copied.
  85          if (question_has_capability_on($question, 'add') &&
  86                  (question_has_capability_on($question, 'edit') || question_has_capability_on($question, 'view'))) {
  87              return [$this->duplicate_question_moodle_url($question->id), 't/copy', $this->strcopy];
  88          }
  89          return [null, null, null];
  90      }
  91  }