Skip to content

Commit

Permalink
add front end for creating MP alerts without votes
Browse files Browse the repository at this point in the history
  • Loading branch information
struan committed Dec 5, 2024
1 parent 075ce8d commit 649e4d8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
6 changes: 3 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 @@ -169,7 +169,7 @@ private function getBasicData() {

$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 @@ -470,7 +470,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
9 changes: 6 additions & 3 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 Down Expand Up @@ -87,7 +87,7 @@ 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];
if ($alert_criteria) {
Expand Down Expand Up @@ -137,6 +137,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
17 changes: 13 additions & 4 deletions www/includes/easyparliament/alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ALERT {
private $alert_id = "";
public $email = "";
public $criteria = ""; // Sets the terms that are used to produce the search results.
public $ignore_speaker_votes = 0;

private $db;

Expand Down Expand Up @@ -123,15 +124,17 @@ public function get_related_terms($term) {

public function update($id, $details) {
$criteria = \MySociety\TheyWorkForYou\Utility\Alert::detailsToCriteria($details);
$ignore_speaker_votes = $details['ignore_speaker_votes'] ? 1 : 0;

$q = $this->db->query("SELECT * FROM alerts
WHERE alert_id = :id", [
':id' => $id,
])->first();
if ($q) {
$q = $this->db->query("UPDATE alerts SET deleted = 0, criteria = :criteria, confirmed = 1
$q = $this->db->query("UPDATE alerts SET deleted = 0, criteria = :criteria, ignore_speaker_votes = :ignore_speaker_votes, confirmed = 1
WHERE alert_id = :id", [
":criteria" => $criteria,
":ignore_speaker_votes" => $ignore_speaker_votes,
":id" => $id,
]);

Expand All @@ -154,6 +157,7 @@ public function add($details, $confirmation_email = false, $instantly_confirm =
// )

$criteria = \MySociety\TheyWorkForYou\Utility\Alert::detailsToCriteria($details);
$ignore_speaker_votes = $details['ignore_speaker_votes'] ? 1 : 0;

$q = $this->db->query("SELECT * FROM alerts
WHERE email = :email
Expand All @@ -164,12 +168,13 @@ public function add($details, $confirmation_email = false, $instantly_confirm =
])->first();
if ($q) {
if ($q['deleted']) {
$this->db->query("UPDATE alerts SET deleted=0
$this->db->query("UPDATE alerts SET deleted=0, ignore_speaker_votes=:ignore_speaker_votes
WHERE email = :email
AND criteria = :criteria
AND confirmed=1", [
':email' => $details['email'],
':criteria' => $criteria,
':ignore_speaker_votes' => $ignore_speaker_votes,
]);
return 1;
} else {
Expand All @@ -178,17 +183,19 @@ public function add($details, $confirmation_email = false, $instantly_confirm =
}

$q = $this->db->query("INSERT INTO alerts (
email, criteria, postcode, lang, deleted, confirmed, created
email, criteria, postcode, lang, ignore_speaker_votes, deleted, confirmed, created
) VALUES (
:email,
:criteria,
:pc,
:lang,
:ignore_speaker_votes,
'0', '0', NOW()
)
", [
':email' => $details['email'],
':criteria' => $criteria,
':ignore_speaker_votes' => $ignore_speaker_votes,
':pc' => $details['pc'],
':lang' => LANGUAGE,
]);
Expand All @@ -199,6 +206,7 @@ public function add($details, $confirmation_email = false, $instantly_confirm =

$this->alert_id = $q->insert_id();
$this->criteria = $criteria;
$this->ignore_speaker_votes = $ignore_speaker_votes;

// We have to set the alert's registration token.
// This will be sent to them via email, so we can confirm they exist.
Expand Down Expand Up @@ -381,7 +389,7 @@ public function check_token($token) {
return false;
}

$q = $this->db->query("SELECT alert_id, email, criteria
$q = $this->db->query("SELECT alert_id, email, criteria, ignore_speaker_votes
FROM alerts
WHERE alert_id = :alert_id
AND registrationtoken = :registration_token
Expand All @@ -396,6 +404,7 @@ public function check_token($token) {
'id' => $q['alert_id'],
'email' => $q['email'],
'criteria' => $q['criteria'],
'ignore_speaker_votes' => $q['ignore_speaker_votes'],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
<?php } ?>
</p>

<div class="alert-page-subsection">
<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>

<p>
<?php if ($pid || $keyword) { ?>
<button type="submit" class="button" name="mp_step" value="mp_confirm">
Expand Down

0 comments on commit 649e4d8

Please sign in to comment.