moodle_local_treestudyplan/classes/form/formbase.php
2024-04-19 16:46:30 +02:00

78 lines
2.9 KiB
PHP

<?php
namespace local_treestudyplan\form;
require_once($CFG->dirroot."/lib/formslib.php");
abstract class formbase extends \moodleform {
/**
* Create the form while providing the correct defaults for our type of forms
* @param object $params Custom parameters this form needs
* @param array $ajaxformdata Provide submitted form data through Ajax here
* @throws \moodle_exception if parameters are invalid or access is denied for a specific reason.
*/
public function __construct($params, $ajaxformdata=null) {
$customdata = static::init_customdata($params);
if (static::check_security($customdata) !== false) {
parent::__construct(null, (array)$customdata, 'post', '', null, true, $ajaxformdata);
// Initialize the initial data based on the parameter validation
$this->set_data($this->init_formdata((object)$this->_customdata));
} else {
throw new \moodle_exception('accessexception', 'core');
}
}
/**
* Generate custom data from parameters
* Also validate parameters and access permissions here
*
* @param object $params The parameters for form initialization
* @return object Form data based on parameters
*/
abstract public static function init_customdata(object $params);
/**
* Generate form data from parameters
* Also validate parameters and access permissions here
*
* @param object $customdata The parameters for form initialization
* @return array Form data based on parameters
*/
abstract public function init_formdata(object $customdata);
/**
* Validate security access for this form based on the provided parameters
* Return true if validation passes, false or throw an exception if it does not.
*
* @param object $params The parameters for form initialization
* @return bool True if security validation passes.
* @throws \moodle_exception if access denied for a specific reason.
*/
abstract public static function check_security(object $customdata);
/**
* Process the submission and perform necessary actions
* @param object $entry The processed form data
* @return object|array Data to pass to receiver if submission successful
* @throws \moodle_exception if an error must be given for a specific reason.
*/
abstract protected function process_submitted_data(object $entry);
/**
* Process the submission and perform necessary actions
* @return object|array Data to pass to receiver if submission successful
* @throws \moodle_exception if an error must be given for a specific reason.
*/
public function process_submission() {
$data = $this->get_data();
if($data) {
return $this->process_submitted_data($data);
} else {
throw new \moodle_exception('no_form_data','local_treestudyplan','',null,$data);
}
}
}