moodle_local_treestudyplan/classes/form/formbase.php

102 lines
3.9 KiB
PHP
Raw Permalink Normal View History

<?php
2024-06-02 23:23:32 +02:00
// This file is part of the Studyplan plugin for Moodle
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
2024-07-19 12:31:26 +02:00
2024-06-02 23:23:32 +02:00
/**
* No file description
* @package local_treestudyplan
* @copyright 2023 P.M. Kuipers
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_treestudyplan\form;
2024-06-02 23:23:32 +02:00
defined('MOODLE_INTERNAL') || die();
2023-10-20 15:08:54 +02:00
require_once($CFG->dirroot."/lib/formslib.php");
2024-06-02 23:23:32 +02:00
/**
* Base class for moodle forms used in this plugin.
*/
2023-10-20 15:08:54 +02:00
abstract class formbase extends \moodleform {
/**
* Create the form while providing the correct defaults for our type of forms
2023-10-20 15:08:54 +02:00
* @param object $params Custom parameters this form needs
* @param array $ajaxformdata Provide submitted form data through Ajax here
2023-10-20 14:03:13 +02:00
* @throws \moodle_exception if parameters are invalid or access is denied for a specific reason.
*/
2023-10-20 14:03:13 +02:00
public function __construct($params, $ajaxformdata=null) {
$customdata = static::init_customdata($params);
2024-06-02 23:23:32 +02:00
if (static::check_security($customdata) ) {
2023-10-20 14:03:13 +02:00
parent::__construct(null, (array)$customdata, 'post', '', null, true, $ajaxformdata);
2024-06-02 23:23:32 +02:00
// Initialize the initial data based on the parameter validation.
2023-10-20 14:03:13 +02:00
$this->set_data($this->init_formdata((object)$this->_customdata));
} else {
throw new \moodle_exception('accessexception', 'core');
}
}
2023-10-20 14:03:13 +02:00
/**
2023-10-20 14:03:13 +02:00
* Generate custom data from parameters
* Also validate parameters and access permissions here
2024-06-02 19:23:40 +02:00
*
2023-10-19 17:48:43 +02:00
* @param object $params The parameters for form initialization
2023-10-20 14:03:13 +02:00
* @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
2024-06-02 19:23:40 +02:00
*
2024-06-03 04:00:46 +02:00
* @param object $customdata The form customdata built on form initialization in self::init_customdata(...)
* @return array Form data based on parameters
*/
2023-10-20 14:03:13 +02:00
abstract public function init_formdata(object $customdata);
/**
2024-06-02 19:23:40 +02:00
* 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.
2024-06-02 19:23:40 +02:00
*
2024-06-03 04:00:46 +02:00
* @param object $customdata The form customdata built on form initialization
2024-06-02 19:23:40 +02:00
* @return bool True if security validation passes.
2024-06-03 04:00:46 +02:00
* @throws \moodle_exception if access denied for a specific reason in self::init_customdata(...)
*/
2024-06-02 23:23:32 +02:00
public static function check_security(object $customdata) {
return true;
}
2024-06-02 19:23:40 +02:00
/**
* Process the submission and perform necessary actions
2023-10-20 14:03:13 +02:00
* @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.
*/
2023-10-20 14:03:13 +02:00
abstract protected function process_submitted_data(object $entry);
2024-06-02 19:23:40 +02:00
/**
2023-10-20 14:03:13 +02:00
* Process the submission and perform necessary actions
* @return object|array Data to pass to receiver if submission successful
2023-10-20 14:03:13 +02:00
* @throws \moodle_exception if an error must be given for a specific reason.
*/
public function process_submission() {
$data = $this->get_data();
2023-10-21 23:24:25 +02:00
2024-06-02 19:23:40 +02:00
if ($data) {
2023-10-21 23:24:25 +02:00
return $this->process_submitted_data($data);
} else {
2024-06-02 23:23:32 +02:00
throw new \moodle_exception('no_form_data', 'local_treestudyplan', '', null, $data);
2023-10-20 14:03:13 +02:00
}
}
2024-06-02 23:23:32 +02:00
}