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]

   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   * Helper functions and callbacks.
  19   *
  20   * @package    qbank_comment
  21   * @copyright  2021 Catalyst IT Australia Pty Ltd
  22   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->dirroot. '/comment/lib.php');
  29  
  30  /**
  31   * Validate comment parameter before perform other comments actions.
  32   *
  33   * @param stdClass $commentparam
  34   * {
  35   * context     => context the context object
  36   * courseid    => int course id
  37   * cm          => stdClass course module object
  38   * commentarea => string comment area
  39   * itemid      => int itemid
  40   * }
  41   * @return boolean
  42   */
  43  function qbank_comment_comment_validate($commentparam): bool {
  44      if ($commentparam->commentarea != 'question' && $commentparam->component != 'qbank_comment') {
  45          throw new comment_exception('invalidcommentarea');
  46      }
  47      return true;
  48  }
  49  
  50  /**
  51   * Running additional permission check on plugins.
  52   *
  53   * @param stdClass $args
  54   * @return array
  55   */
  56  function qbank_comment_comment_permissions($args): array {
  57      return ['post' => true, 'view' => true];
  58  }
  59  
  60  /**
  61   * Validate comment data before displaying comments.
  62   *
  63   * @param array $comments
  64   * @param stdClass $args
  65   * @return array $comments
  66   */
  67  function qbank_comment_comment_display($comments, $args): array {
  68      if ($args->commentarea != 'question' && $args->component != 'qbank_comment') {
  69          throw new comment_exception('core_question');
  70      }
  71      return $comments;
  72  }
  73  
  74  /**
  75   * Comment content for callbacks.
  76   *
  77   * @param question_definition $question
  78   * @param int $courseid
  79   * @return string
  80   */
  81  function qbank_comment_preview_display($question, $courseid): string {
  82      global $CFG, $PAGE;
  83      if (question_has_capability_on($question, 'comment') && $CFG->usecomments
  84              && core\plugininfo\qbank::is_plugin_enabled('qbank_comment')) {
  85          \comment::init($PAGE);
  86          $args = new \stdClass;
  87          $args->contextid = context_system::instance()->id; // Static data to bypass comment sql as context is not needed.
  88          $args->courseid  = $courseid;
  89          $args->area      = 'question';
  90          $args->itemid    = $question->id;
  91          $args->component = 'qbank_comment';
  92          $args->notoggle  = true;
  93          $args->autostart = true;
  94          $args->displaycancel = false;
  95          $args->linktext = get_string('commentheader', 'qbank_comment');
  96          $comment = new \comment($args);
  97          $comment->set_view_permission(true);
  98          $comment->set_fullwidth();
  99          return $comment->output();
 100      } else {
 101          return '';
 102      }
 103  }
 104  
 105  /**
 106   * Question comment fragment callback.
 107   *
 108   * @param array $args
 109   * @return string rendered output
 110   */
 111  function qbank_comment_output_fragment_question_comment($args): string {
 112      global $USER, $PAGE, $CFG, $DB;
 113      $displaydata = [];
 114      require_once($CFG->dirroot . '/question/engine/bank.php');
 115      $question = question_bank::load_question($args['questionid']);
 116      $quba = question_engine::make_questions_usage_by_activity(
 117              'core_question_preview', context_user::instance($USER->id));
 118  
 119      // Just in case of any regression, it should not break the modal, just show the comments.
 120      if (class_exists('\\qbank_previewquestion\\question_preview_options')) {
 121          $options = new \qbank_previewquestion\question_preview_options($question);
 122          $quba->set_preferred_behaviour($options->behaviour);
 123          $slot = $quba->add_question($question, $options->maxmark);
 124          $quba->start_question($slot, $options->variant);
 125          $transaction = $DB->start_delegated_transaction();
 126          question_engine::save_questions_usage_by_activity($quba);
 127          $transaction->allow_commit();
 128          $displaydata['question'] = $quba->render_question($slot, $options, '1');
 129      }
 130      $displaydata['comment'] = qbank_comment_preview_display($question, $args['courseid']);
 131      $displaydata['commenstdisabled'] = false;
 132      if (empty($displaydata['comment']) && !$CFG->usecomments) {
 133          $displaydata['commenstdisabled'] = true;
 134      }
 135  
 136      $selector = \core_question\output\question_version_selection::make_for_question('question_comment_version_dropdown',
 137          $args['questionid']);
 138      $qbankrenderer = $PAGE->get_renderer('core_question', 'bank');
 139      $displaydata['versionselection'] = $selector->export_for_template($qbankrenderer);
 140  
 141      return $PAGE->get_renderer('qbank_comment')->render_comment_fragment($displaydata);
 142  }