Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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  namespace core_form\external;
  18  
  19  use core_search\engine_exception;
  20  use external_api;
  21  use external_function_parameters;
  22  use external_value;
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  require_once($CFG->libdir.'/externallib.php');
  27  
  28  /**
  29   * Implements the external functions provided by the core_form subsystem.
  30   *
  31   * @copyright 2020 Marina Glancy
  32   * @package   core_form
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class dynamic_form extends external_api {
  36  
  37      /**
  38       * Parameters for modal form
  39       *
  40       * @return external_function_parameters
  41       */
  42      public static function execute_parameters(): external_function_parameters {
  43          return new external_function_parameters([
  44              'form' => new external_value(PARAM_RAW_TRIMMED, 'Form class', VALUE_REQUIRED),
  45              'formdata' => new external_value(PARAM_RAW, 'url-encoded form data', VALUE_REQUIRED),
  46          ]);
  47      }
  48  
  49      /**
  50       * Submit a form from a modal dialogue.
  51       *
  52       * @param string $formclass
  53       * @param string $formdatastr
  54       * @return array
  55       * @throws \moodle_exception
  56       */
  57      public static function execute(string $formclass, string $formdatastr): array {
  58          global $PAGE, $OUTPUT;
  59  
  60          $params = self::validate_parameters(self::execute_parameters(), [
  61              'form' => $formclass,
  62              'formdata' => $formdatastr,
  63          ]);
  64          $formclass = $params['form'];
  65          parse_str($params['formdata'], $formdata);
  66  
  67          if (!class_exists($formclass) || !is_subclass_of($formclass, \core_form\dynamic_form::class)) {
  68              // For security reason we don't throw exception "class does not exist" but rather an access exception.
  69              throw new \moodle_exception('nopermissionform', 'core_form');
  70          }
  71  
  72          /** @var \core_form\dynamic_form $form */
  73          $form = new $formclass(null, null, 'post', '', [], true, $formdata, true);
  74          $form->set_data_for_dynamic_submission();
  75          if (!$form->is_cancelled() && $form->is_submitted() && $form->is_validated()) {
  76              // Form was properly submitted, process and return results of processing. No need to render it again.
  77              return ['submitted' => true, 'data' => json_encode($form->process_dynamic_submission())];
  78          }
  79  
  80          // Render actual form.
  81  
  82          // Hack alert: Forcing bootstrap_renderer to initiate moodle page.
  83          $OUTPUT->header();
  84  
  85          $PAGE->start_collecting_javascript_requirements();
  86          $data = $form->render();
  87          $jsfooter = $PAGE->requires->get_end_code();
  88          $output = ['submitted' => false, 'html' => $data, 'javascript' => $jsfooter];
  89          return $output;
  90      }
  91  
  92      /**
  93       * Return for modal
  94       * @return \external_single_structure
  95       */
  96      public static function execute_returns(): \external_single_structure {
  97          return new \external_single_structure(
  98              array(
  99                  'submitted' => new external_value(PARAM_BOOL, 'If form was submitted and validated'),
 100                  'data' => new external_value(PARAM_RAW, 'JSON-encoded return data from form processing method', VALUE_OPTIONAL),
 101                  'html' => new external_value(PARAM_RAW, 'HTML fragment of the form', VALUE_OPTIONAL),
 102                  'javascript' => new external_value(PARAM_RAW, 'JavaScript fragment of the form', VALUE_OPTIONAL)
 103              )
 104          );
 105      }
 106  }