Skip to content

Commit

Permalink
Improvement: Validation manager setup
Browse files Browse the repository at this point in the history
  • Loading branch information
WunderJacob committed Feb 10, 2025
1 parent b27e6dd commit 9096a7c
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 74 deletions.
85 changes: 14 additions & 71 deletions classes/filters/column_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,12 @@ class column_manager {
protected $filtersettings;
/** @var array */
protected $data;
/** @var array */
protected $errors;

/**
* Handles form definition of filter classes.
* @param array $params
*/
public function __construct($params) {
$this->errors = [];
$this->data = $params;
$this->filtercolumn = $params['filtercolumn'];
$this->table = wunderbyte_table::instantiate_from_tablecache_hash($params['encodedtable']);
Expand Down Expand Up @@ -94,7 +91,7 @@ public function get_filtered_column_form() {
* @return array
*/
public function get_filtered_column_form_persist_error() {
$this->set_data_validation();
//$this->errors = $this->get_data_validation();

$existingfilterdata = [];
foreach ($this->data['value'] as $key => $keyvalue) {
Expand All @@ -105,8 +102,8 @@ public function get_filtered_column_form_persist_error() {
$this->set_available_filter_types($existingfilterdata, $this->filtersettings[$this->filtercolumn]['wbfilterclass']);
$this->set_add_filter_key_value();
return [
'filtereditfields' => $this->mformedit->toHtml(),
'filteraddfields' => $this->mformadd->toHtml(),
'filtereditfields' => $this->mformedit,
'filteraddfields' => $this->mformadd,
];
}

Expand All @@ -122,16 +119,16 @@ private function set_available_filter_types($existingfilterdata, $filterclass) {
$this->mformedit->addElement('html', '<p id="no-pairs-message" class="alert alert-info">No pairs exist</p>');
}

foreach ($this->mformedit->_elements as $element) {
if ($element->_type === 'group') {
foreach ($element->_elements as $subelement) {
$label = str_replace('group_', '', $subelement->_name);
if (isset($this->errors['key'][$label])) {
$this->mformedit->setElementError($element->_name, $this->errors['key'][$label]);
}
}
}
}
// foreach ($this->mformedit->_elements as $element) {
// if ($element->_type === 'group') {
// foreach ($element->_elements as $subelement) {
// $label = str_replace('group_', '', $subelement->_name);
// if (isset($this->errors['key'][$label])) {
// $this->mformedit->setElementError($element->_name, $this->errors['key'][$label]);
// }
// }
// }
// }
}

/**
Expand Down Expand Up @@ -183,7 +180,7 @@ private function render_mandatory_fields($classname) {
$classname::$staticfunction($this->mformadd, $newvalue);
$parts = explode("\\", $classname);
$elementname = array_pop($parts) . 'group';
$this->mformadd->setElementError($elementname, $this->errors['key'][0]);
//$this->mformadd->setElementError($elementname, $this->errors['key'][0]);
}
}
}
Expand All @@ -200,60 +197,6 @@ private function non_kestringy_value_pair_properties() {
];
}

/**
* Handles form definition of filter classes.
* @param array $data
*/
public function return_validation() {
$this->set_data_validation();
return $this->errors;
}

/**
* Handles form definition of filter classes.
* @param array $data
*/
private function set_data_validation() {
if (isset($this->data['filter_columns'])) {
$errors = self::checked_selected_column($this->data['filter_columns']);
foreach ($this->data['key'] as $key => $keyvalue) {
if (self::only_partial_submitted($keyvalue, $this->data['value'][$key])) {
$errors['key'][$key] = get_string('standardfiltervaluekeyerror', 'local_wunderbyte_table');
$errors['value'][$key] = get_string('standardfiltervaluekeyerror', 'local_wunderbyte_table');
}
}
$this->errors = $errors;
}
}

/**
* The expected value.
* @param string $filtercolumns
* @return array
*/
private static function checked_selected_column($filtercolumns) {
$errros = [];
if (empty($filtercolumns)) {
$errros['filter_columns'] = get_string('columnemptyerror', 'local_wunderbyte_table');
}
return $errros;
}

/**
* The expected value.
* @param string $key
* @param string $value
* @return bool
*/
private static function only_partial_submitted($key, $value) {
if (
empty($key) !== empty($value)
) {
return true;
}
return false;
}

/**
* The expected value.
* @param string $filtercolumn
Expand Down
18 changes: 18 additions & 0 deletions classes/filters/filter_form_operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,30 @@ public static function persist_input_values($mform, $submitteddata) {
}

$submitteddata['filtercolumn'] = $submitteddata['filter_columns'];

$columnmanager = new column_manager($submitteddata);
$filteredcolumnform = $columnmanager->get_filtered_column_form_persist_error();

$validationmanager = new validation_manager($submitteddata);
$errors = $validationmanager->get_data_validation();
$validationmanager->set_errors($errors, $filteredcolumnform);

self::render_forms_to_html($filteredcolumnform);
self::set_dynamic_fields_inside_div($mform, $filteredcolumnform);
}
}

/**
* Validation.
* @param \MoodleQuickForm $mform
* @param array $dynamicforms
*/
private static function render_forms_to_html(&$filteredcolumnform) {
foreach ($filteredcolumnform as $key => $form) {
$filteredcolumnform[$key] = $form->toHtml();
}
}

/**
* Validation.
* @param \MoodleQuickForm $mform
Expand Down
31 changes: 31 additions & 0 deletions classes/filters/types/standardfilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,35 @@ public static function get_dynamic_values($fieldsandsubmitteddata) {
}
return $mandatoryfields;
}

/**
* The expected value.
* @param array $data
* @return array
*/
public static function validate_input($data) {
$errors = [];
foreach ($data['key'] as $key => $keyvalue) {
if (self::only_partial_submitted($keyvalue, $data['value'][$key])) {
$errors['key'][$key] = get_string('standardfiltervaluekeyerror', 'local_wunderbyte_table');
$errors['value'][$key] = get_string('standardfiltervaluekeyerror', 'local_wunderbyte_table');
}
}
return $errors;
}

/**
* The expected value.
* @param string $key
* @param string $value
* @return bool
*/
private static function only_partial_submitted($key, $value) {
if (
empty($key) !== empty($value)
) {
return true;
}
return false;
}
}
110 changes: 110 additions & 0 deletions classes/filters/validation_manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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 <http://www.gnu.org/licenses/>.

/**
* The Wunderbyte table class is an extension of the tablelib table_sql class.
*
* @package local_wunderbyte_table
* @copyright 2024 Wunderbyte Gmbh <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_wunderbyte_table\filters;

defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/formslib.php');

use ReflectionClass;

/**
* Handles the filter classes.
* @package local_wunderbyte_table
*/
class validation_manager {
/** @var array */
protected $data;
/** @var array */
protected $errors;

/**
* Handles form definition of filter classes.
* @param array $params
*/
public function __construct($submitteddata) {
$this->data = $submitteddata;
}

/**
* Handles form definition of filter classes.
*/
public function set_errors($errors, &$forms) {
foreach ($forms as $form) {
$testing = 10;
}
}

/**
* Handles form definition of filter classes.
* @param array $data
*/
public function get_data_validation() {
if (isset($this->data['filter_columns'])) {
$errors = self::checked_selected_column($this->data['filter_columns']);

$classname = $this->data['filter_options'];
$staticfunction = 'validate_input';
if (self::is_static_public_function($this->data['filter_options'], $staticfunction)) {
$fitertypeerrors = $classname::$staticfunction($this->data);
$errors = array_merge($errors, $fitertypeerrors);
}
return $errors;
}
}

/**
* Handles form definition of filter classes.
*/
private function is_static_public_function($classname, $functionname) {
if (class_exists($classname)) {
try {
$reflection = new ReflectionClass($classname);
if (!$reflection->isAbstract() && $reflection->isSubclassOf(base::class)) {
if ($reflection->hasMethod($functionname)) {
$method = $reflection->getMethod($functionname);
if ($method->isPublic() && $method->isStatic()) {
return true;
}
}
}
} catch (\ReflectionException $e) {
debugging("Reflection error for class $classname: " . $e->getMessage());
}
}
return false;
}

/**
* The expected value.
* @param string $filtercolumns
* @return array
*/
private static function checked_selected_column($filtercolumns) {
$errros = [];
if (empty($filtercolumns)) {
$errros['filter_columns'] = get_string('columnemptyerror', 'local_wunderbyte_table');
}
return $errros;
}
}
6 changes: 3 additions & 3 deletions classes/form/addfitlertable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

namespace local_wunderbyte_table\form;

use local_wunderbyte_table\filters\column_manager;
use local_wunderbyte_table\filters\filter_form_operator;
use local_wunderbyte_table\filters\validation_manager;
use local_wunderbyte_table\filters\wunderbyte_table_db_operator;

defined('MOODLE_INTERNAL') || die();
Expand Down Expand Up @@ -104,8 +104,8 @@ public function set_data_for_dynamic_submission(): void {
*
*/
public function validation($data, $files) {
$columnmanager = new column_manager($this->_ajaxformdata);
return $columnmanager->return_validation();
$validationmanager = new validation_manager($this->_ajaxformdata);
return $validationmanager->get_data_validation();
}


Expand Down

0 comments on commit 9096a7c

Please sign in to comment.