Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to allow users to hide speaker votes in alerts #1844

Draft
wants to merge 21 commits into
base: alert-front-end-rebase
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
827fbed
add ignore_speaker_votes flag to alerts table
struan Oct 30, 2024
604cdc2
do not include votes in alerts if ignore_speaker_votes flag set
struan Oct 30, 2024
468353f
add front end for creating MP alerts without votes
struan Dec 5, 2024
73a6798
fix spelling in alerts page test
struan Dec 9, 2024
75a28b2
enable users to exclude votes from existing alerts
struan Dec 9, 2024
65b0dba
move alerts details from user page
lucascumsille Dec 11, 2024
f306543
Moved .label inside accordion
lucascumsille Dec 12, 2024
a72aa92
Added confirmation when deleting alerts
lucascumsille Dec 12, 2024
508d2ae
Improved copy for "delete all" button for alerts
lucascumsille Dec 16, 2024
b8af83f
Move delete all button to top of the page
lucascumsille Dec 16, 2024
89a2cdb
Removed float right from "delete all" button in user's page
lucascumsille Dec 16, 2024
118b764
CSS Class rename to fit TWFY
lucascumsille Dec 16, 2024
ccf1921
Fixed type alert page
lucascumsille Dec 17, 2024
dc9b04f
Added spacing for button with icon on the right side
lucascumsille Jan 13, 2025
32bd74d
Removed .button.red styling
lucascumsille Jan 13, 2025
cf04e8b
Replaced .label with .tags
lucascumsille Jan 13, 2025
5e73793
Added Accordion component
lucascumsille Jan 21, 2025
219850a
Tidy up alerts stylesheet
lucascumsille Jan 21, 2025
bf26bb0
Remove conditional to display "Search tips"
lucascumsille Jan 21, 2025
bf27e85
Moved megaphone icon to the left side of the button
lucascumsille Jan 21, 2025
e48ef1f
fix exclusions only using first word
struan Jan 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions classes/AlertView/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private function processAction() {
if ($success) {
$this->data['results'] = 'alert-confirmed';
$this->data['criteria'] = $this->alert->criteria;
$this->data['display_criteria'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($this->alert->criteria);
$this->data['display_criteria'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($this->alert->criteria, $this->alert->ignore_speaker_votes);
}
} elseif ($action == 'Suspend') {
$success = $this->suspendAlert($token);
Expand Down Expand Up @@ -158,18 +158,24 @@ private function getBasicData() {
$this->data['pid'] = trim(get_http_var("pid"));
$this->data['pc'] = get_http_var('pc');
$this->data['submitted'] = get_http_var('submitted') || $this->data['pid'] || $this->data['keyword'] || $this->data['step'];
$this->data['ignore_speaker_votes'] = get_http_var('ignore_speaker_votes');

if ($this->data['addword'] || $this->data['step']) {
$alert = $this->alert->check_token($this->data['token']);

$criteria = '';
$alert_ignore_speaker_votes = 0;
if ($alert) {
$criteria = $alert['criteria'];
$alert_ignore_speaker_votes = $alert['ignore_speaker_votes'];
}

$ignore_speaker_votes = get_http_var('ignore_speaker_votes', $alert_ignore_speaker_votes);
$this->data['ignore_speaker_votes'] = ($ignore_speaker_votes == 'on' || $ignore_speaker_votes == 1);

$this->data['alert'] = $alert;

$this->data['alert_parts'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($criteria, true);
$this->data['alert_parts'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($criteria, $alert_ignore_speaker_votes, true);

$existing_rep = '';
if (isset($this->data['alert_parts']['spokenby'])) {
Expand Down Expand Up @@ -220,7 +226,7 @@ private function getBasicData() {
}
}
}
$this->data['exclusions'] = trim(get_http_var("exclusions", implode('', $this->data['alert_parts']['exclusions'])));
$this->data['exclusions'] = trim(get_http_var("exclusions", implode(' ', $this->data['alert_parts']['exclusions'])));
$this->data['representative'] = trim(get_http_var("representative", $existing_rep));

$this->data['search_section'] = trim(get_http_var("search_section", $existing_section));
Expand All @@ -231,13 +237,29 @@ private function getBasicData() {
}
$this->data['keyword'] = implode($separator, $this->data['words']);
if ($this->data['exclusions']) {
$this->data['keyword'] = '(' . $this->data['keyword'] . ') -' . $this->data["exclusions"];
$this->data['keyword'] = '(' . $this->data['keyword'] . ') -' . implode(' -', explode(' ', $this->data["exclusions"]));
}

$this->data['results'] = '';

$this->getSearchSections();
} # XXX probably should do something here if $alertsearch is set
} else if ($this->data['mp_step'] == 'mp_alert') {
$alert = $this->alert->check_token($this->data['token']);
if ($alert) {
$ignore_speaker_votes = get_http_var('ignore_speaker_votes', $alert['ignore_speaker_votes']);
$this->data['ignore_speaker_votes'] = ($ignore_speaker_votes == 'on' || $ignore_speaker_votes == 1);

$existing_rep = '';
if (isset($alert['criteria'])) {
$alert_parts = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($alert['criteria'], $alert['ignore_speaker_votes'], true);
$existing_rep = $alert_parts['spokenby'][0];
$this->data['pid'] = $alert_parts['pid'];
}
$this->data['keyword'] = get_http_var('mp_search', $existing_rep);
} else {
$this->data['ignore_speaker_votes'] = get_http_var('ignore_speaker_votes');
}
}

$this->data['sign'] = get_http_var('sign');
$this->data['site'] = get_http_var('site');
Expand Down Expand Up @@ -470,7 +492,7 @@ private function addAlert() {

$this->data['results'] = $result;
$this->data['criteria'] = $this->alert->criteria;
$this->data['display_criteria'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($this->alert->criteria);
$this->data['display_criteria'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($this->alert->criteria, $this->alert->ignore_speaker_votes);
}


Expand Down
20 changes: 15 additions & 5 deletions classes/Utility/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public static function forUser($email) {

$alerts = [];
foreach ($q as $row) {
$criteria = self::prettifyCriteria($row['criteria']);
$parts = self::prettifyCriteria($row['criteria'], true);
$criteria = self::prettifyCriteria($row['criteria'], $row['ignore_speaker_votes']);
$parts = self::prettifyCriteria($row['criteria'], $row['ignore_speaker_votes'], true);
$token = $row['alert_id'] . '-' . $row['registrationtoken'];

$status = 'confirmed';
Expand All @@ -73,6 +73,7 @@ public static function forUser($email) {
'status' => $status,
'criteria' => $criteria,
'raw' => $row['criteria'],
'ignore_speaker_votes' => $row['ignore_speaker_votes'],
'keywords' => [],
'exclusions' => [],
'sections' => [],
Expand All @@ -86,9 +87,9 @@ public static function forUser($email) {
return $alerts;
}

public static function prettifyCriteria($alert_criteria, $as_parts = false) {
public static function prettifyCriteria($alert_criteria, $ignore_speaker_votes = false, $as_parts = false) {
$text = '';
$parts = ['words' => [], 'sections' => [], 'exclusions' => [], 'match_all' => true];
$parts = ['words' => [], 'sections' => [], 'exclusions' => [], 'match_all' => true, 'pid' => false];
if ($alert_criteria) {
# check for phrases
if (strpos($alert_criteria, ' OR ') !== false) {
Expand All @@ -115,7 +116,13 @@ public static function prettifyCriteria($alert_criteria, $as_parts = false) {
$exclusions = [];
$sections = [];
$sections_verbose = [];
$spokenby = array_values(\MySociety\TheyWorkForYou\Utility\Search::speakerNamesForIDs($alert_criteria));
$speaker_parts = \MySociety\TheyWorkForYou\Utility\Search::speakerNamesForIDs($alert_criteria);
$pids = array_keys($speaker_parts);
$spokenby = array_values($speaker_parts);

if (count($pids) == 1) {
$parts['pid'] = $pids[0];
}

foreach ($criteria as $c) {
if (preg_match('#^section:(\w+)#', $c, $m)) {
Expand All @@ -136,6 +143,9 @@ public static function prettifyCriteria($alert_criteria, $as_parts = false) {
$parts['words'] = $words;
} elseif ($spokenby) {
$text = implode(' or ', $spokenby) . " speaks";
if ($ignore_speaker_votes) {
$text .= " excluding votes";
}
$parts['spokenby'] = $spokenby;
}

Expand Down
2 changes: 1 addition & 1 deletion commonlib
Submodule commonlib updated from 10fbd9 to 5bb31d
1 change: 1 addition & 0 deletions db/0026-add-ignore-votes-alerts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `alerts` ADD `ignore_speaker_votes` tinyint(1) NOT NULL default '0';
1 change: 1 addition & 0 deletions db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ CREATE TABLE `alerts` (
`confirmed` tinyint(1) NOT NULL default '0',
`created` datetime NOT NULL default '0000-00-00 00:00:00',
`postcode` varchar(10) NOT NULL default '',
`ignore_speaker_votes` tinyint(1) NOT NULL default '0',
`lang` varchar(2) NOT NULL default 'en',
PRIMARY KEY (`alert_id`),
KEY `email` (`email`),
Expand Down
3 changes: 2 additions & 1 deletion scripts/alertmailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ function mlog($message) {
continue;
}
$criteria_raw = $alertitem['criteria'];
$include_votes = $alertitem['ignore_speaker_votes'] == 0;
if (preg_match('#\bOR\b#', $criteria_raw)) {
$criteria_raw = "($criteria_raw)";
}
Expand Down Expand Up @@ -249,7 +250,7 @@ function mlog($message) {
mlog(", hits " . $total_results . ", time " . (getmicrotime() - $start) . "\n");

# Divisions
if (preg_match('#^speaker:(\d+)$#', $criteria_raw, $m)) {
if ($include_votes && preg_match('#^speaker:(\d+)$#', $criteria_raw, $m)) {
$pid = $m[1];
$q = $db->query('SELECT * FROM persondivisionvotes pdv JOIN divisions USING(division_id)
WHERE person_id=:person_id AND pdv.lastupdate >= :time', [
Expand Down
12 changes: 6 additions & 6 deletions tests/AlertsPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testFetchPage() {

public function testKeywordOnly() {
$page = $this->fetch_page([ 'alertsearch' => 'elephant']);
$this->assertStringContainsString('What word or phrase would you like to recieve alerts about', $page);
$this->assertStringContainsString('What word or phrase would you like to receive alerts about', $page);
$this->assertStringContainsString('<input type="text" id="words0" name="words[]" aria-required="true" value="elephant"', $page);
}

Expand Down Expand Up @@ -62,7 +62,7 @@ public function testPostcodeAndKeywordWithNoSittingMP() {

public function testBasicKeyWordAlertsCreation() {
$page = $this->fetch_page([ 'step' => 'define']);
$this->assertStringContainsString('What word or phrase would you like to recieve alerts about', $page);
$this->assertStringContainsString('What word or phrase would you like to receive alerts about', $page);
$this->assertStringContainsString('<input type="text" id="words0" name="words[]" aria-required="true" value=""', $page);

$page = $this->fetch_page([ 'step' => 'review', 'email' => '[email protected]', 'words[]' => 'fish']);
Expand All @@ -76,7 +76,7 @@ public function testBasicKeyWordAlertsCreation() {

public function testMultipleKeyWordAlertsCreation() {
$page = $this->fetch_page([ 'step' => 'define']);
$this->assertStringContainsString('What word or phrase would you like to recieve alerts about', $page);
$this->assertStringContainsString('What word or phrase would you like to receive alerts about', $page);
$this->assertStringContainsString('<input type="text" id="words0" name="words[]" aria-required="true" value=""', $page);

$page = $this->fetch_page([ 'step' => 'review', 'email' => '[email protected]', 'words[]' => ['fish', 'salmon']]);
Expand All @@ -90,7 +90,7 @@ public function testMultipleKeyWordAlertsCreation() {

public function testMultipleKeyWordAlertsCreationLoggedIn() {
$page = $this->get_page(['step' => 'define']);
$this->assertStringContainsString('What word or phrase would you like to recieve alerts about', $page);
$this->assertStringContainsString('What word or phrase would you like to receive alerts about', $page);
$this->assertStringContainsString('<input type="text" id="words0" name="words[]" aria-required="true" value=""', $page);

$page = $this->get_page([ 'step' => 'review', 'words[]' => ['fish', 'salmon']]);
Expand All @@ -104,7 +104,7 @@ public function testMultipleKeyWordAlertsCreationLoggedIn() {

public function testKeyWordAndSectionAlertsCreationLoggedIn() {
$page = $this->get_page(['step' => 'define']);
$this->assertStringContainsString('What word or phrase would you like to recieve alerts about', $page);
$this->assertStringContainsString('What word or phrase would you like to receive alerts about', $page);
$this->assertStringContainsString('<input type="text" id="words0" name="words[]" aria-required="true" value=""', $page);

$page = $this->get_page(['step' => 'review', 'words[]' => 'fish', 'search_section' => 'debates']);
Expand All @@ -117,7 +117,7 @@ public function testKeyWordAndSectionAlertsCreationLoggedIn() {

public function testKeyWordAndSpeakerAlertsCreationLoggedIn() {
$page = $this->get_page(['step' => 'define']);
$this->assertStringContainsString('What word or phrase would you like to recieve alerts about', $page);
$this->assertStringContainsString('What word or phrase would you like to receive alerts about', $page);
$this->assertStringContainsString('<input type="text" id="words0" name="words[]" aria-required="true" value=""', $page);

$page = $this->get_page(['step' => 'review', 'words[]' => 'fish', 'representative' => 'Mrs Test Current-MP']);
Expand Down
4 changes: 4 additions & 0 deletions tests/AlertsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function testAdd() {
'email' => '[email protected]',
'keyword' => 'test',
'pc' => 'SW1A 1AA',
'ignore_speaker_votes' => 0,
];

$response = $ALERT->add($details, false, true);
Expand All @@ -96,6 +97,7 @@ public function testAddExisting() {
'email' => '[email protected]',
'keyword' => 'test3',
'pc' => 'SW1A 1AA',
'ignore_speaker_votes' => 0,
];

$response = $ALERT->add($details, false, true);
Expand All @@ -117,6 +119,7 @@ public function testAddDeleted() {
'email' => '[email protected]',
'keyword' => 'test6',
'pc' => 'SW1A 1AA',
'ignore_speaker_votes' => 0,
];

$response = $ALERT->add($details, false, true);
Expand Down Expand Up @@ -162,6 +165,7 @@ public function testCheckTokenCorrect() {
'id' => 1,
'email' => '[email protected]',
'criteria' => 'test1',
'ignore_speaker_votes' => '0',
], $response);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/_fixtures/alerts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<field name="alert_id">1</field>
<field name="deleted">0</field>
<field name="confirmed">0</field>
<field name="ignore_speaker_votes">0</field>
<field name="registrationtoken">token1</field>
<field name="email">[email protected]</field>
<field name="criteria">test1</field>
Expand All @@ -15,6 +16,7 @@
<field name="alert_id">3</field>
<field name="deleted">0</field>
<field name="confirmed">1</field>
<field name="ignore_speaker_votes">0</field>
<field name="registrationtoken">token3</field>
<field name="email">[email protected]</field>
<field name="criteria">test3</field>
Expand All @@ -24,6 +26,7 @@
<field name="alert_id">5</field>
<field name="deleted">0</field>
<field name="confirmed">1</field>
<field name="ignore_speaker_votes">0</field>
<field name="registrationtoken">token5</field>
<field name="email">[email protected]</field>
<field name="criteria">speaker:1234</field>
Expand All @@ -33,6 +36,7 @@
<field name="alert_id">6</field>
<field name="deleted">2</field>
<field name="confirmed">1</field>
<field name="ignore_speaker_votes">0</field>
<field name="registrationtoken">token6</field>
<field name="email">[email protected]</field>
<field name="criteria">test6</field>
Expand Down
16 changes: 15 additions & 1 deletion www/docs/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,23 @@ function createAccordion(triggerSelector, contentSelector) {

// Initialize accordion when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
createAccordion('.accordion-button', '.accordion-content');
createAccordion('.js-accordion-button', '.js-accordion-content');
});

// Comfirm deletion of alerts
function confirmDelete() {
var triggers = document.querySelectorAll('.js-confirm-delete');

triggers.forEach(function(trigger) {
trigger.addEventListener('click', function(event) {
var message = "Are you sure you want to delete all alerts?";
if (!confirm(message)) {
event.preventDefault();
}
});
});
}
confirmDelete();

$(function() {

Expand Down
8 changes: 8 additions & 0 deletions www/docs/style/sass/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ a:focus {
}
}

button {
// For buttons with an icon on the right side
i {
margin-right: 0.15rem;
}
}

/* forms */
.errorlist {
@include unstyled-list();
Expand Down Expand Up @@ -246,6 +253,7 @@ form {
@import "parts/panels";
@import "parts/promo-banner";
@import "parts/accordion";
@import "parts/tags";

@import "pages/mp";
@import "pages/topics";
Expand Down
Loading
Loading