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 13 commits into
base: alert-front-end-small-fixes
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 25 additions & 3 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 @@ -237,6 +243,22 @@ private function getBasicData() {
$this->data['results'] = '';

$this->getSearchSections();
} 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');
}
} # XXX probably should do something here if $alertsearch is set

$this->data['sign'] = get_http_var('sign');
Expand Down Expand Up @@ -470,7 +492,7 @@ protected 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 @@ -58,8 +58,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 @@ -74,6 +74,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 @@ -87,9 +88,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 @@ -116,7 +117,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 @@ -137,6 +144,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
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' => 'test@example.org', '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' => 'test@example.org', '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' => 'test@theyworkforyou.com',
'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' => 'test3@theyworkforyou.com',
'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' => 'test6@theyworkforyou.com',
'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' => 'test@theyworkforyou.com',
'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">test@theyworkforyou.com</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">test3@theyworkforyou.com</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">test5@theyworkforyou.com</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">test6@theyworkforyou.com</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
1 change: 1 addition & 0 deletions www/docs/style/sass/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ form {
@import "parts/panels";
@import "parts/promo-banner";
@import "parts/accordion";
@import "parts/labels";

@import "pages/mp";
@import "pages/topics";
Expand Down
93 changes: 93 additions & 0 deletions www/docs/style/sass/pages/_alert.scss
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,96 @@
margin-left: 0.5em;
}
}

.alert-section__page-header {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
margin-bottom: 0.5rem;

button, h2 {
margin-bottom: 0;
margin-top: 0;
}
}

.alert-meta {
.alert-meta__results {
display: flex;
flex-direction: row;
flex-wrap: wrap;
column-gap: 1rem;
row-gap: 1rem;
align-items: center;
margin-bottom: 1rem;

.alert-meta__item {
border-radius: 0.5rem;
background-color: $primary-color-200;
padding: 1rem;

dt {
font-size: 0.7rem;
text-transform: uppercase;
margin-bottom: 0;
}

dd {
margin-bottom: 0;
font-size: 1.1rem;
}
}
}
}

// Accordion Keyword
.keyword-alert-accordion {
margin-top: 2rem;
}

.keyword-alert-accordion__button {
@extend .accordion__button;
}

.keyword-alert-accordion__content {
@extend .accordion__content;
}

.keyword-alert-accordion__button-content {
display: flex;
flex-direction: column;
align-content: center;
align-items: start;
gap: 0.25rem;

.keyword-alert-accordion__subtitle {
font-size: 0.75rem;
font-weight: bold;
}
}

.keyword-alert-accordion__keyword-list,
.alert-creation-steps {
margin-bottom: 2.5rem;
ul {
list-style: none;
display: flex;
flex-direction: row;
flex-wrap: wrap;
gap: 0.5rem;
margin-left: 0;

li {
font-weight: bold;
i {
margin-left: 0.25rem;
}
}
}

h3 {
font-weight: 400;
}
}
Loading
Loading