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  namespace qbank_tagquestion\form;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  require_once($CFG->dirroot . '/lib/formslib.php');
  22  require_once($CFG->dirroot . '/lib/questionlib.php');
  23  
  24  /**
  25   * The mform class for  manage question tags.
  26   *
  27   * @package   qbank_tagquestion
  28   * @copyright 2018 Simey Lameze <simey@moodle.com>
  29   * @author    2021 Safat Shahin <safatshahin@catalyst-au.net>
  30   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class tags_form extends \moodleform {
  33  
  34      public function definition() {
  35          $mform = $this->_form;
  36          $customdata = $this->_customdata;
  37  
  38          $mform->disable_form_change_checker();
  39  
  40          $mform->addElement('hidden', 'id');
  41          $mform->setType('id', PARAM_INT);
  42  
  43          $mform->addElement('hidden', 'categoryid');
  44          $mform->setType('categoryid', PARAM_INT);
  45  
  46          $mform->addElement('hidden', 'contextid');
  47          $mform->setType('contextid', PARAM_INT);
  48  
  49          $mform->addElement('static', 'questionname', get_string('questionname', 'question'));
  50          $mform->addElement('static', 'questioncategory', get_string('categorycurrent', 'question'));
  51          $mform->addElement('static', 'context', '');
  52  
  53          if (\core_tag_tag::is_enabled('core_question', 'question')) {
  54              $tags = \core_tag_tag::get_tags_by_area_in_contexts('core_question', 'question', $customdata['contexts']);
  55              $tagstrings = [];
  56              foreach ($tags as $tag) {
  57                  $tagstrings[$tag->name] = $tag->name;
  58              }
  59  
  60              $options = [
  61                  'tags' => true,
  62                  'multiple' => true,
  63                  'noselectionstring' => get_string('anytags', 'quiz'),
  64              ];
  65              $mform->addElement('autocomplete', 'tags',  get_string('tags'), $tagstrings, $options);
  66  
  67              // Is the question category in a course context?
  68              $qcontext = $customdata['questioncontext'];
  69              $qcoursecontext = $qcontext->get_course_context(false);
  70              $iscourseoractivityquestion = !empty($qcoursecontext);
  71              // Is the current context we're editing in a course context?
  72              $editingcontext = $customdata['editingcontext'];
  73              $editingcoursecontext = $editingcontext->get_course_context(false);
  74              $iseditingcontextcourseoractivity = !empty($editingcoursecontext);
  75  
  76              if ($iseditingcontextcourseoractivity && !$iscourseoractivityquestion) {
  77                  // If the question is being edited in a course or activity context
  78                  // and the question isn't a course or activity level question then
  79                  // allow course tags to be added to the course.
  80                  $coursetagheader = get_string('questionformtagheader', 'core_question',
  81                      $editingcoursecontext->get_context_name(true));
  82                  $mform->addElement('autocomplete', 'coursetags',  $coursetagheader, $tagstrings, $options);
  83  
  84              }
  85          }
  86      }
  87  
  88  }