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.
   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 related functions.
  19   *
  20   * This file was created just because Fragment API expects callbacks to be defined on lib.php.
  21   *
  22   * @package   qbank_tagquestion
  23   * @copyright 2018 Simey Lameze <simey@moodle.com>
  24   * @author    2021 Safat Shahin <safatshahin@catalyst-au.net>
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  use qbank_tagquestion\form\tags_form;
  31  
  32  /**
  33   * Question tags fragment callback.
  34   *
  35   * @param array $args Arguments to the form.
  36   * @return null|string The rendered form.
  37   */
  38  function qbank_tagquestion_output_fragment_tags_form($args) {
  39  
  40      if (!empty($args['id'])) {
  41          global $CFG, $DB;
  42          require_once($CFG->libdir . '/questionlib.php');
  43          $id = clean_param($args['id'], PARAM_INT);
  44          $editingcontext = $args['context'];
  45  
  46          // Load the question and some related information.
  47          $question = $DB->get_record('question', ['id' => $id]);
  48  
  49          if ($coursecontext = $editingcontext->get_course_context(false)) {
  50              $course = $DB->get_record('course', ['id' => $coursecontext->instanceid]);
  51              $filtercourses = [$course];
  52          } else {
  53              $filtercourses = null;
  54          }
  55  
  56          $sql = "SELECT qc.*
  57                    FROM {question} q
  58                    JOIN {question_versions} qv ON qv.questionid = q.id
  59                    JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
  60                    JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid
  61                   WHERE q.id = :id";
  62          $category = $DB->get_record_sql($sql, ['id' => $question->id]);
  63          $questioncontext = \context::instance_by_id($category->contextid);
  64          $contexts = new \core_question\local\bank\question_edit_contexts($editingcontext);
  65  
  66          // Load the question tags and filter the course tags by the current course.
  67          if (core_tag_tag::is_enabled('core_question', 'question')) {
  68              $tagobjectsbyquestion = core_tag_tag::get_items_tags('core_question', 'question', [$question->id]);
  69              if (!empty($tagobjectsbyquestion[$question->id])) {
  70                  $tagobjects = $tagobjectsbyquestion[$question->id];
  71                  $sortedtagobjects = question_sort_tags($tagobjects,
  72                          context::instance_by_id($category->contextid), $filtercourses);
  73              }
  74          }
  75          $formoptions = [
  76                  'editingcontext' => $editingcontext,
  77                  'questioncontext' => $questioncontext,
  78                  'contexts' => $contexts->all()
  79          ];
  80          $data = [
  81                  'id' => $question->id,
  82                  'questioncategory' => $category->name,
  83                  'questionname' => $question->name,
  84                  'categoryid' => $category->id,
  85                  'contextid' => $category->contextid,
  86                  'context' => $questioncontext->get_context_name(),
  87                  'tags' => $sortedtagobjects->tags ?? [],
  88                  'coursetags' => $sortedtagobjects->coursetags ?? [],
  89          ];
  90  
  91          $cantag = question_has_capability_on($question, 'tag');
  92          $mform = new tags_form(null, $formoptions, 'post', '', null, $cantag, $data);
  93          $mform->set_data($data);
  94  
  95          return $mform->render();
  96      }
  97  
  98  }