Skip to content

Commit

Permalink
enable turning off votes in alerts
Browse files Browse the repository at this point in the history
This also tidies up editing MP alerts so it's mostly buttons rather than
having to go through a form.
  • Loading branch information
struan committed Dec 9, 2024
1 parent 649e4d8 commit 3a66f7f
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 32 deletions.
24 changes: 23 additions & 1 deletion classes/AlertView/Standard.php
Original file line number Diff line number Diff line change
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, $alert['ignore_speaker_votes'], 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
11 changes: 9 additions & 2 deletions classes/Utility/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -89,7 +90,7 @@ public static function forUser($email) {

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, $ignore_speaker_votes =
$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 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
2 changes: 1 addition & 1 deletion www/includes/easyparliament/alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function add($details, $confirmation_email = false, $instantly_confirm =
':criteria' => $criteria,
])->first();
if ($q) {
if ($q['deleted']) {
if ($q['deleted'] || $q['ignore_speaker_votes'] != $ignore_speaker_votes) {
$this->db->query("UPDATE alerts SET deleted=0, ignore_speaker_votes=:ignore_speaker_votes
WHERE email = :email
AND criteria = :criteria
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@
<p><?= gettext("Or edit the name") ?></p>
<?php } ?>
<input type="text" id="representative" name="representative" value="<?= _htmlspecialchars($representative) ?>" aria-required="true">
<div class="checkbox-wrapper">
<input type="checkbox" id="ignore_speaker_votes" name="ignore_speaker_votes"<?= $ignore_speaker_votes ? ' checked' : ''?>>
<label for="ignore_speaker_votes"><?= gettext('Do not include votes') ?></label>
</div>
</div>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,35 +186,53 @@
<span><?= gettext('Confirm alert') ?></span>
</button>
<?php } elseif ($alert['status'] == 'suspended') { ?>
<button type="submit" class="button button-outline small" name="action" value="Resume">
<span><?= gettext('Resume alert') ?></span>
<i aria-hidden="true" class="fi-play"></i>
</button>
<button type="submit" class="button button-outline small" name="action" value="Resume">
<span><?= gettext('Resume alert') ?></span>
<i aria-hidden="true" class="fi-play"></i>
</button>
<?php } else { ?>
<button type="submit" class="button button--outline small" name="action" value="Suspend">
<i aria-hidden="true" class="fi-pause"></i>
<span><?= gettext('Suspend alert') ?></span>
</button>
<button type="submit" class="button small button--outline-red" name="action" value="Delete">
<i aria-hidden="true" class="fi-trash"></i>
<span><?= gettext('Delete alert') ?></span>
</button>
<button type="submit" class="button button--outline small" name="action" value="Suspend">
<i aria-hidden="true" class="fi-pause"></i>
<span><?= gettext('Suspend alert') ?></span>
</button>
<button type="submit" class="button small button--outline-red" name="action" value="Delete">
<i aria-hidden="true" class="fi-trash"></i>
<span><?= gettext('Delete alert') ?></span>
</button>
<?php } ?>
</form>
<form action="<?= $actionurl ?>" method="POST">
<input type="hidden" name="step" value="define">
<input type="hidden" name="t" value="<?= _htmlspecialchars($alert['token']) ?>">
<button type="submit" class="button button--outline small" value="Edit">
<i aria-hidden="true" class="fi-page-edit"></i>
<span><?= gettext('Edit alert') ?></span>
</button>
<?php if (count($alert['words'])) { ?>
<input type="hidden" name="step" value="define">
<input type="hidden" name="shown_related" value="1">
<button type="submit" class="button button--outline small" value="Edit">
<i aria-hidden="true" class="fi-page-edit"></i>
<span><?= gettext('Edit alert') ?></span>
</button>
<?php } else { ?>
<input type="hidden" name="mp_step" value="mp_confirm">
<input type="hidden" name="pid" value="<?= $alert['pid'] ?>">
<?php if ($alert['ignore_speaker_votes'] == 1) { ?>
<input type="hidden" name="ignore_speaker_votes" value="0">
<button type="submit" class="button button--outline small" value="Edit">
<i aria-hidden="true" class="fi-page-edit"></i>
<span><?= gettext('Include votes') ?></span>
</button>
<?php } else { ?>
<input type="hidden" name="ignore_speaker_votes" value="1">
<button type="submit" class="button button--outline small" value="Edit">
<i aria-hidden="true" class="fi-page-edit"></i>
<span><?= gettext('Ignore votes') ?></span>
</button>
<?php } ?>
<?php } ?>
</form>
</div>
<?php } ?>
<?php if (!in_array(implode('', $person_alerts[0]['spokenby']), $all_keywords)) { ?>
<p class="alert-page-subsection--subtitle">Alert when <?= _htmlspecialchars(implode(', ', $person_alerts[0]['spokenby'])) ?> is <strong>mentioned</strong></p>
<form action="<?= $actionurl ?>" method="post">
<input type="hidden" name="keyword" value="<?= _htmlentities(implode('', $person_alerts[0]['spokenby'])) ?>">
<input type="hidden" name="words[]" value="<?= _htmlentities(implode('', $person_alerts[0]['spokenby'])) ?>">
<button type="submit" class="button small" name="action" value="Subscribe">
<?= gettext('Create new alert') ?>
<i aria-hidden="true" role="img" class="fi-megaphone"></i>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<input type="hidden" name="t" value="<?= _htmlspecialchars($token) ?>">
<input type="hidden" name="email" value="<?= _htmlspecialchars($email) ?>">
<input type="hidden" name="pid" value="<?= $member->person_id() ?>">
<input type="hidden" name="ignore_speaker_votes" value="<?= $ignore_speaker_votes ?>">
<?= $member->full_name() ?>
(<?= _htmlspecialchars($constituency) ?>) in <?= _htmlspecialchars($member->house_text($member->house_disp)) ?>
<input type="submit" class="button small" value="<?= gettext('Subscribe') ?>"></form>
Expand All @@ -25,12 +26,16 @@
</p>
<?php } ?>

<?php if ($token) { ?>
<input type="hidden" name="t" value="<?= _htmlspecialchars($token) ?>">
<?php } ?>

<p>
<?php if ($pid) { ?>
<input type="text" class="form-control" name="mp_search" id="mp_search" disabled="disabled"
value="<?= $pid_member->full_name() ?><?php if ($pid_member->constituency()) { ?> (<?= _htmlspecialchars($pid_member->constituency()) ?>)<?php } ?>">
<?php } elseif ($keyword) { ?>
<input type="text" class="form-control" name="mp_search" id="mp_search" disabled="disabled" value="<?= _htmlspecialchars($display_keyword) ?>">
<input type="text" class="form-control" name="mp_search" id="mp_search" value="<?= _htmlspecialchars($keyword) ?>">
<?php } else { ?>
<label for="mp-postcode">Search postcode, or MP name</label>
<input id="mp-postcode" type="text" class="form-control" placeholder="<?= gettext('e.g. ‘B2 4QA’ or ‘John Doe’') ?>" name="mp_search" id="mp_search" value="<?= _htmlentities($search_text) ?>" style="min-width:300px;">
Expand Down
Loading

0 comments on commit 3a66f7f

Please sign in to comment.