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   * Defines the editing form for the randomsamatch question type.
  19   *
  20   * @package    qtype_randomsamatch
  21   * @copyright  2007 Jamie Pratt me@jamiep.org
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  
  29  /**
  30   * randomsamatch editing form definition.
  31   *
  32   * @copyright  2007 Jamie Pratt me@jamiep.org
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
  34   */
  35  class qtype_randomsamatch_edit_form extends question_edit_form {
  36      protected function definition_inner($mform) {
  37          $questionstoselect = array();
  38          for ($i = 2; $i <= qtype_randomsamatch::MAX_SUBQUESTIONS; $i++) {
  39              $questionstoselect[$i] = $i;
  40          }
  41  
  42          $mform->addElement('select', 'choose',
  43                  get_string('randomsamatchnumber', 'qtype_randomsamatch'), $questionstoselect);
  44          $mform->setType('feedback', PARAM_RAW);
  45  
  46          $mform->addElement('advcheckbox', 'subcats',
  47                  get_string('subcats', 'qtype_randomsamatch'), null, null, array(0, 1));
  48          $mform->addHelpButton('subcats', 'subcats', 'qtype_randomsamatch');
  49          $mform->setDefault('subcats', 1);
  50  
  51          $mform->addElement('hidden', 'fraction', 0);
  52          $mform->setType('fraction', PARAM_RAW);
  53  
  54          $this->add_combined_feedback_fields(true);
  55          $this->add_interactive_settings(true, true);
  56      }
  57  
  58      protected function data_preprocessing($question) {
  59          $question = parent::data_preprocessing($question);
  60          $question = $this->data_preprocessing_combined_feedback($question, true);
  61          $question = $this->data_preprocessing_hints($question, true, true);
  62  
  63          if (!empty($question->options)) {
  64              $question->choose = $question->options->choose;
  65              $question->subcats = $question->options->subcats;
  66          }
  67  
  68          if (empty($question->name)) {
  69              $question->name = get_string('randomsamatch', 'qtype_randomsamatch');
  70          }
  71  
  72          if (empty($question->questiontext)) {
  73              $question->questiontext = get_string('randomsamatchintro', 'qtype_randomsamatch');
  74          }
  75          return $question;
  76      }
  77  
  78      public function qtype() {
  79          return 'randomsamatch';
  80      }
  81  
  82      public function validation($data, $files) {
  83          global $DB;
  84          $errors = parent::validation($data, $files);
  85  
  86          if (isset($data->categorymoveto)) {
  87              list($category) = explode(',', $data['categorymoveto']);
  88          } else {
  89              list($category) = explode(',', $data['category']);
  90          }
  91          $saquestions = question_bank::get_qtype('randomsamatch')->get_available_saquestions_from_category(
  92                  $category, $data['subcats']);
  93          $numberavailable = count($saquestions);
  94          if ($saquestions === false) {
  95              $a = new stdClass();
  96              $a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
  97              $errors['choose'] = get_string('nosaincategory', 'qtype_randomsamatch', $a);
  98  
  99          } else if ($numberavailable < $data['choose']) {
 100              $a = new stdClass();
 101              $a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
 102              $a->nosaquestions = $numberavailable;
 103              $errors['choose'] = get_string('notenoughsaincategory', 'qtype_randomsamatch', $a);
 104          }
 105          return $errors;
 106      }
 107  }