2020use core \output \renderable ;
2121use core \output \renderer_base ;
2222use core \output \templatable ;
23- use core_user ;
2423use format_ucl \local \data \custom_contact ;
2524
2625/**
3332 * @author Amanda Doughty <m.doughty@ucl.ac.uk>
3433 */
3534class custom_contact_form extends \core \form \persistent implements renderable, templatable {
35+ /** @var string */
36+ public const DELETE = 'delete ' ;
37+ /** @var string */
38+ public const SAVE = 'save ' ;
39+
3640 /** @var string The fully qualified classname. */
3741 protected static $ persistentclass = custom_contact::class;
3842
@@ -42,9 +46,8 @@ class custom_contact_form extends \core\form\persistent implements renderable, t
4246 * @return void
4347 */
4448 protected function definition () {
45- global $ OUTPUT ;
46-
4749 $ mform = $ this ->_form ;
50+ $ contactid = $ this ->get_persistent ()->get ('id ' );
4851
4952 $ mform ->addElement ('hidden ' , 'id ' );
5053 $ mform ->addElement ('hidden ' , 'courseid ' );
@@ -54,42 +57,103 @@ protected function definition() {
5457
5558 $ mform ->addElement ('html ' , '<div class="d-flex align-items-center w-100"> ' );
5659
60+ // Role.
5761 $ attributes = [
5862 'placeholder ' => get_string ('role:placeholder ' , 'format_ucl ' ),
5963 'class ' => 'm-3 flex-fill ' ,
6064 ];
6165 $ mform ->addElement ('text ' , 'role ' , get_string ('role ' , 'format_ucl ' ), $ attributes );
6266 $ mform ->setType ('role ' , PARAM_TEXT );
6367
68+ // Name.
6469 $ attributes = [
6570 'placeholder ' => get_string ('name:placeholder ' , 'format_ucl ' ),
6671 'class ' => 'm-3 flex-fill ' ,
6772 'required ' => 'required ' ,
6873 ];
6974 $ mform ->addElement ('text ' , 'name ' , get_string ('name ' , 'format_ucl ' ), $ attributes );
7075 $ mform ->setType ('name ' , PARAM_TEXT );
71- $ mform ->addRule ('name ' , '' , 'required ' );
7276
77+ // Email.
7378 $ attributes = [
7479 'placeholder ' => get_string ('email:placeholder ' , 'format_ucl ' ),
7580 'class ' => 'm-3 flex-fill ' ,
7681 'required ' => 'required ' ,
82+ 'pattern ' => "[^@\s]+@[^@\s]+\.[^@\s]+ " , // Email as pattern till moodle gets native input type=email.
7783 ];
7884 $ mform ->addElement ('text ' , 'email ' , get_string ('email ' ), $ attributes );
79- $ mform ->setType ('email ' , core_user::get_property_type ('email ' ));
80- $ mform ->addRule ('email ' , '' , 'required ' );
81- $ mform ->setForceLtr ('email ' );
85+ $ mform ->setType ('email ' , PARAM_NOTAGS );
8286
8387 $ mform ->addElement ('html ' , '</div> ' );
8488
89+ // Description.
8590 $ attributes = [
8691 'placeholder ' => get_string ('description:placeholder ' , 'format_ucl ' ),
8792 ];
8893 $ mform ->addElement ('text ' , 'description ' , get_string ('description ' , 'format_ucl ' ), $ attributes );
8994 $ mform ->setType ('description ' , PARAM_TEXT );
90-
9195 $ this ->set_display_vertical ();
92- $ this ->add_action_buttons (true , get_string ('save ' ));
96+
97+ // Custom action buttons to allow delete and cancel.
98+ $ mform ->addElement ('html ' , '<div class="d-flex"> ' );
99+
100+ // Save.
101+ $ options = [
102+ 'customclassoverride ' => 'btn btn-primary ' ,
103+ ];
104+ $ mform ->addElement ('submit ' , 'submitbutton ' , get_string ('save ' ), [], null , $ options );
105+
106+ // Delete.
107+ if ($ contactid ) {
108+ // We will add a second submit button to the form that will be used to delete a contact.
109+ $ mform ->registerNoSubmitButton ('deletebutton ' );
110+ $ options = [
111+ 'customclassoverride ' => 'btn btn-danger mx-2 ' ,
112+ ];
113+ $ params = [
114+ 'courseid ' => $ this ->get_persistent ()->get ('courseid ' ),
115+ 'contactid ' => $ contactid ,
116+ 'action ' => self ::DELETE ,
117+ 'sesskey ' => sesskey (),
118+ ];
119+ $ url = new \moodle_url ('/course/format/ucl/editcustomcontact.php ' , $ params );
120+ $ attributes = [
121+ 'formnovalidate ' => 'formnovalidate ' ,
122+ 'data-confirmation ' => 'modal ' ,
123+ 'data-confirmation-title-str ' => json_encode (["customcontact " , "format_ucl " ]),
124+ 'data-confirmation-content-str ' => json_encode (["deletecustomcontact " , "format_ucl " ]),
125+ 'data-confirmation-yes-button-str ' => json_encode (["delete " ]),
126+ 'data-confirmation-destination ' => $ url ->out (false ),
127+ ];
128+
129+ $ mform ->addElement (
130+ 'submit ' ,
131+ 'deletebutton ' ,
132+ get_string ('delete ' ),
133+ $ attributes ,
134+ null ,
135+ $ options
136+ );
137+ }
138+
139+ $ mform ->addElement ('html ' , '<div class="ml-auto"> ' );
140+
141+ // Cancel button - closes the collapse.
142+ // Target collapse container - if contactid exists, target that specific form, otherwise target the generic form container.
143+ $ targetid = $ contactid ? 'ucl-format-customcontact-form- ' . $ contactid : 'ucl-format-customcontact-form ' ;
144+ $ options = [
145+ 'customclassoverride ' => 'btn btn-secondary ' ,
146+ ];
147+ $ attributes = [
148+ 'data-toggle ' => 'collapse ' ,
149+ 'href ' => "# $ targetid " ,
150+ 'aria-expanded ' => 'false ' ,
151+ 'aria-controls ' => '$targetid ' ,
152+ ];
153+ $ mform ->addElement ('button ' , 'cancelbutton ' , get_string ('cancel ' ), $ attributes , $ options );
154+
155+ $ mform ->addElement ('html ' , '</div> ' );
156+ $ mform ->addElement ('html ' , '</div> ' );
93157 }
94158
95159 /**
@@ -119,6 +183,7 @@ public function process(): bool {
119183 return true ;
120184 }
121185
186+ // Run the data assignment first so $data is defined.
122187 if ($ data = $ this ->get_data ()) {
123188 /** @var custom_contact $customcontact */
124189 $ customcontact = $ this ->get_persistent ();
@@ -134,6 +199,17 @@ public function process(): bool {
134199 return true ;
135200 }
136201
202+ // If validation fails on creation ($data is empty), check the errors here.
203+ // NB we can't override persistent::validation to save the errors.
204+ if ($ this ->is_submitted ()) {
205+ $ validationerrors = $ this ->validation ($ this ->_form ->getSubmitValues (), []);
206+ if (!empty ($ validationerrors )) {
207+ foreach ($ validationerrors as $ field => $ error ) {
208+ notification::error ($ field . ': ' . $ error );
209+ }
210+ }
211+ }
212+
137213 return false ;
138214 }
139215}
0 commit comments