From 2d0bbd3b6e7662229b92eba9a41af905eed2ab01 Mon Sep 17 00:00:00 2001 From: Stephen Bourget Date: Thu, 23 Jun 2016 17:32:19 -0400 Subject: [PATCH] MDL-54910 mod_data: Open dates should be before close dates --- mod/data/lang/en/data.php | 2 ++ mod/data/mod_form.php | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/mod/data/lang/en/data.php b/mod/data/lang/en/data.php index a8e0d38f667c5..842ef634c1f94 100644 --- a/mod/data/lang/en/data.php +++ b/mod/data/lang/en/data.php @@ -50,6 +50,7 @@ Only the tags that are in the "Available tags" list may be used for the current template.'; $string['availabletodate'] = 'Available to'; +$string['availabletodatevalidation'] = 'The available to date cannot be before the available from date.'; $string['blank'] = 'Blank'; $string['buttons'] = 'Actions'; $string['bynameondate'] = 'by {$a->name} - {$a->date}'; @@ -368,4 +369,5 @@ $string['usestandard_help'] = 'To use a preset available to the whole site, select it from the list. (If you have added a preset to the list using the save as preset feature then you have the option of deleting it.)'; $string['viewfromdate'] = 'Read only from'; $string['viewtodate'] = 'Read only to'; +$string['viewtodatevalidation'] = 'The read only to date cannot be before the read only from date.'; $string['wrongdataid'] = 'Wrong data id provided'; diff --git a/mod/data/mod_form.php b/mod/data/mod_form.php index fb6fc195ffbe9..11c9db7d9fd44 100644 --- a/mod/data/mod_form.php +++ b/mod/data/mod_form.php @@ -81,6 +81,29 @@ function definition() { $this->add_action_buttons(); } + /** + * Enforce validation rules here + * + * @param array $data array of ("fieldname"=>value) of submitted data + * @param array $files array of uploaded files "element_name"=>tmp_file_path + * @return array + **/ + public function validation($data, $files) { + $errors = parent::validation($data, $files); + + // Check open and close times are consistent. + if ($data['timeavailablefrom'] && $data['timeavailableto'] && + $data['timeavailableto'] < $data['timeavailablefrom']) { + $errors['timeavailableto'] = get_string('availabletodatevalidation', 'data'); + } + if ($data['timeviewfrom'] && $data['timeviewto'] && + $data['timeviewto'] < $data['timeviewfrom']) { + $errors['timeviewto'] = get_string('viewtodatevalidation', 'data'); + } + + return $errors; + } + function data_preprocessing(&$default_values){ parent::data_preprocessing($default_values); }