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.

Differences Between: [Versions 311 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 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          if ($form->no_submit_button_pressed()) {
  83              // If form has not been submitted, we have to recreate the form for being able to properly handle non-submit action
  84              // like "repeat elements" to include additional JS.
  85              /** @var \core_form\dynamic_form $form */
  86              $form = new $formclass(null, null, 'post', '', [], true, $formdata, true);
  87              $form->set_data_for_dynamic_submission();
  88          }
  89          // Hack alert: Forcing bootstrap_renderer to initiate moodle page.
  90          $OUTPUT->header();
  91  
  92          $PAGE->start_collecting_javascript_requirements();
  93          $data = $form->render();
  94          $jsfooter = $PAGE->requires->get_end_code();
  95          $output = ['submitted' => false, 'html' => $data, 'javascript' => $jsfooter];
  96          return $output;
  97      }
  98  
  99      /**
 100       * Return for modal
 101       * @return \external_single_structure
 102       */
 103      public static function execute_returns(): \external_single_structure {
 104          return new \external_single_structure(
 105              array(
 106                  'submitted' => new external_value(PARAM_BOOL, 'If form was submitted and validated'),
 107                  'data' => new external_value(PARAM_RAW, 'JSON-encoded return data from form processing method', VALUE_OPTIONAL),
 108                  'html' => new external_value(PARAM_RAW, 'HTML fragment of the form', VALUE_OPTIONAL),
 109                  'javascript' => new external_value(PARAM_RAW, 'JavaScript fragment of the form', VALUE_OPTIONAL)
 110              )
 111          );
 112      }
 113  }