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.
/question/ -> lib.php (source)

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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   * Question related functions.
  19   *
  20   * This file was created just because Fragment API expects callbacks to be defined on lib.php
  21   *
  22   * Please, do not add new functions to this file.
  23   *
  24   * @package   core_question
  25   * @copyright 2018 Simey Lameze <simey@moodle.com>
  26   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  require_once($CFG->dirroot . '/question/editlib.php');
  32  
  33  /**
  34   * Question tags fragment callback.
  35   *
  36   * @param array $args Arguments to the form.
  37   * @return null|string The rendered form.
  38   * @deprecated since Moodle 4.0
  39   * @see /question/bank/qbank_tagquestion/lib.php
  40   * @todo Final deprecation on Moodle 4.4 MDL-72438
  41   */
  42  function core_question_output_fragment_tags_form($args) {
  43      debugging('Function core_question_output_fragment_tags_form() is deprecated,
  44           please use core_question_output_fragment_tags_form() from qbank_tagquestion instead.', DEBUG_DEVELOPER);
  45  
  46      if (!empty($args['id'])) {
  47          global $CFG, $DB;
  48          require_once($CFG->libdir . '/questionlib.php');
  49          $id = clean_param($args['id'], PARAM_INT);
  50          $editingcontext = $args['context'];
  51  
  52          // Load the question and some related information.
  53          $question = $DB->get_record('question', ['id' => $id]);
  54  
  55          if ($coursecontext = $editingcontext->get_course_context(false)) {
  56              $course = $DB->get_record('course', ['id' => $coursecontext->instanceid]);
  57              $filtercourses = [$course];
  58          } else {
  59              $filtercourses = null;
  60          }
  61  
  62          $sql = "SELECT qc.*
  63                    FROM {question} q
  64                    JOIN {question_versions} qv ON qv.questionid = q.id
  65                    JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
  66                    JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid
  67                   WHERE q.id = :id";
  68          $category = $DB->get_record_sql($sql, ['id' => $question->id]);
  69          $questioncontext = \context::instance_by_id($category->contextid);
  70          $contexts = new \core_question\local\bank\question_edit_contexts($editingcontext);
  71  
  72          // Load the question tags and filter the course tags by the current course.
  73          if (core_tag_tag::is_enabled('core_question', 'question')) {
  74              $tagobjectsbyquestion = core_tag_tag::get_items_tags('core_question', 'question', [$question->id]);
  75              if (!empty($tagobjectsbyquestion[$question->id])) {
  76                  $tagobjects = $tagobjectsbyquestion[$question->id];
  77                  $sortedtagobjects = question_sort_tags($tagobjects,
  78                          context::instance_by_id($category->contextid), $filtercourses);
  79              }
  80          }
  81          $formoptions = [
  82              'editingcontext' => $editingcontext,
  83              'questioncontext' => $questioncontext,
  84              'contexts' => $contexts->all()
  85          ];
  86          $data = [
  87              'id' => $question->id,
  88              'questioncategory' => $category->name,
  89              'questionname' => $question->name,
  90              'categoryid' => $category->id,
  91              'contextid' => $category->contextid,
  92              'context' => $questioncontext->get_context_name(),
  93              'tags' => $sortedtagobjects->tags ?? [],
  94              'coursetags' => $sortedtagobjects->coursetags ?? [],
  95          ];
  96  
  97          $cantag = question_has_capability_on($question, 'tag');
  98          $mform = new \qbank_tagquestion\form\tags_form(null, $formoptions, 'post', '', null, $cantag, $data);
  99          $mform->set_data($data);
 100  
 101          return $mform->render();
 102      }
 103  }
 104  
 105  /**
 106   * Question data fragment to get the question html via ajax call.
 107   *
 108   * @param array $args Arguments for rendering the fragment. Expected keys:
 109   *  * view - the view class
 110   *  * cmid - if in an activity, the course module ID.
 111   *  * filterquery - the current filters encoded as a URL parameter.
 112   *  * lastchanged - the ID of the last edited question.
 113   *  * sortdata - Array of sorted columns.
 114   *  * filtercondition - the current filters encoded as an object.
 115   *  * extraparams - additional parameters required for a particular view class.
 116   *
 117   * @return array|string
 118   */
 119  function core_question_output_fragment_question_data(array $args): string {
 120      if (empty($args)) {
 121          return '';
 122      }
 123      [$params, $extraparams] = \core_question\local\bank\filter_condition_manager::extract_parameters_from_fragment_args($args);
 124      [
 125          $thispageurl,
 126          $contexts,
 127          ,
 128          $cm,
 129          ,
 130          $pagevars
 131      ] = question_build_edit_resources('questions', '/question/edit.php', $params);
 132  
 133      if (is_null($cm)) {
 134          $course = get_course(clean_param($args['courseid'], PARAM_INT));
 135      } else {
 136          $course = get_course($cm->course);
 137      }
 138  
 139      $viewclass = empty($args['view']) ? \core_question\local\bank\view::class : clean_param($args['view'], PARAM_NOTAGS);
 140  
 141      if (!empty($args['lastchanged'])) {
 142          $thispageurl->param('lastchanged', clean_param($args['lastchanged'], PARAM_INT));
 143      }
 144      // This is highly suspicious, but it is the same approach taken in /question/edit.php. See MDL-79281.
 145      $thispageurl->param('deleteall', 1);
 146      $questionbank = new $viewclass($contexts, $thispageurl, $course, $cm, $pagevars, $extraparams);
 147      $questionbank->add_standard_search_conditions();
 148      ob_start();
 149      $questionbank->display_question_list();
 150      return ob_get_clean();
 151  }