Skip to content

Commit a3346e2

Browse files
Ctp 6306 delete custom contacts (#35)
* CTP-6304 - Custom contact form ui * CTP-6306 Custom contact deletion * Custom contacts button layout * CTP-6306 Coding style edit * CTP-6306 Removed redundant parameter * CTP-6306 Code review changes --------- Co-authored-by: Stuart Lamour <thisismyrice@gmail.com>
1 parent 1c29223 commit a3346e2

6 files changed

Lines changed: 140 additions & 16 deletions

File tree

classes/form/custom_contact_form.php

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use core\output\renderable;
2121
use core\output\renderer_base;
2222
use core\output\templatable;
23-
use core_user;
2423
use format_ucl\local\data\custom_contact;
2524

2625
/**
@@ -33,6 +32,11 @@
3332
* @author Amanda Doughty <m.doughty@ucl.ac.uk>
3433
*/
3534
class 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
}

classes/local/data/custom_contact.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected static function define_properties() {
5151
'type' => PARAM_TEXT,
5252
],
5353
'email' => [
54-
'type' => PARAM_EMAIL,
54+
'type' => PARAM_NOTAGS,
5555
],
5656
'description' => [
5757
'type' => PARAM_TEXT,

editcustomcontact.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* @author Amanda Doughty <m.doughty@ucl.ac.uk>
2525
*/
2626

27+
use format_ucl\form\custom_contact_form;
2728
use format_ucl\local\data\custom_contact;
2829

2930
require_once("../../../config.php");
@@ -32,9 +33,10 @@
3233

3334
$courseid = required_param('courseid', PARAM_INT);
3435
$contactid = optional_param('contactid', 0, PARAM_INT);
36+
$action = optional_param('action', custom_contact_form::SAVE, PARAM_ALPHA);
3537

3638
$course = get_course($courseid);
37-
$params = ['courseid' => $course->id, 'contactid' => $contactid];
39+
$params = ['courseid' => $course->id, 'contactid' => $contactid, 'action' => $action];
3840
$PAGE->set_url('/course/format/ucl/editcustomcontact.php', $params);
3941

4042
require_login($course);
@@ -45,9 +47,16 @@
4547
$redirect = new moodle_url('/course/view.php', ['id' => $course->id]);
4648

4749
$data = ['id' => $contactid];
48-
$customcontact = custom_contact::get_record($data) ?: new format_ucl\local\data\custom_contact();
50+
$customcontact = custom_contact::get_record($data) ?: new custom_contact();
51+
52+
if ($contactid && $action == custom_contact_form::DELETE) {
53+
require_sesskey();
54+
$customcontact->delete();
55+
redirect($redirect);
56+
}
57+
4958
$customdata = ['persistent' => $customcontact];
50-
$mform = new format_ucl\form\custom_contact_form(
59+
$mform = new custom_contact_form(
5160
new moodle_url('/course/format/ucl/editcustomcontact.php', $params),
5261
$customdata
5362
);

lang/en/format_ucl.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
$string['customcontact'] = 'Custom contact';
4444
$string['customcontact:desc'] = 'A custom contact is not linked to a Moodle user, and not listed in participants, or elsewhere in a course.';
4545
$string['delete'] = 'Delete';
46+
$string['deletecustomcontact'] = 'Are you sure you want to delete this custom contact?';
4647
$string['deletepresetconfirm'] = 'Are you sure you want to delete the section "{$a}"?';
4748
$string['deletesection'] = 'Delete';
4849
$string['description'] = 'Description';

styles.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ body.format-ucl {
236236
width: 100%;
237237
max-width: 100%;
238238
}
239+
/* No label elements - e.g. buttons. */
240+
.femptylabel {
241+
width: auto;
242+
}
243+
/* SHAME - remove padding on submit button. */
244+
#fitem_id_submitbutton {
245+
padding-right: 0;
246+
}
239247

240248
.form-label-addon,
241249
.fdescription.required {

tests/behat/initialsection.feature

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Feature: Initial section has custom layout
7878
And I am on "Course 1" course homepage with editing mode on
7979
Then "Show Teacher 1 to students" "checkbox" should exist
8080

81-
Scenario: In editing mode, user can add and edit custom contacts
81+
Scenario: In editing mode, user can add custom contacts
8282
When I log in as "teacher1"
8383
And I am on "Course 1" course homepage with editing mode on
8484
And I click on "Add custom contact" "link"
@@ -95,7 +95,17 @@ Feature: Initial section has custom layout
9595
And I should see "Jack Tucker"
9696
And I should see "zzucker@example.com"
9797
And I should see "Clown king"
98-
And I switch editing mode on
98+
99+
Scenario: In editing mode, user can edit custom contacts
100+
When I log in as "teacher1"
101+
And I am on "Course 1" course homepage with editing mode on
102+
And I click on "Add custom contact" "link"
103+
And I set the following fields to these values:
104+
| Role | Ring Master |
105+
| Name | Jack Tucker |
106+
| Email | zzucker@example.com |
107+
| Description | Clown king |
108+
And I press "Save"
99109
And I click on "Edit custom contact Jack Tucker" "link"
100110
And I set the following fields to these values:
101111
| Role | Director |
@@ -104,8 +114,28 @@ Feature: Initial section has custom layout
104114
And I press "Save"
105115
And I switch editing mode off
106116
And I click on "Course contacts" "link"
107-
And I should not see "Ring Master"
117+
Then I should not see "Ring Master"
108118
And I should see "Director"
109119
And I should see "Jonny Woolley"
110120
And I should see "jwoolley@example.com"
111121
And I should see "Clown king"
122+
123+
Scenario: In editing mode, user can delete custom contacts
124+
When I log in as "teacher1"
125+
And I am on "Course 1" course homepage with editing mode on
126+
And I click on "Add custom contact" "link"
127+
And I set the following fields to these values:
128+
| Role | Ring Master |
129+
| Name | Jack Tucker |
130+
| Email | zzucker@example.com |
131+
| Description | Clown king |
132+
And I press "Save"
133+
And I click on "Edit custom contact Jack Tucker" "link"
134+
And I press "Delete"
135+
And I click on "Delete" "button" in the "Custom contact" "dialogue"
136+
And I should not see "Ring Master"
137+
And I should not see "Jack Tucker"
138+
And I should not see "zzucker@example.com"
139+
And I should not see "Clown king"
140+
And I switch editing mode off
141+
And "Course contacts" "link" should not exist

0 commit comments

Comments
 (0)