Skip to content

Commit

Permalink
show suggested search terms when creating an alert
Browse files Browse the repository at this point in the history
If you are creating a keyword alert that has related terms in the search
suggestions table then offer the chance to add them to the alert.
  • Loading branch information
struan committed Oct 31, 2024
1 parent e52992b commit 09528b1
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 22 deletions.
39 changes: 38 additions & 1 deletion classes/AlertView/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,20 @@ private function processAction() {
}

private function processStep() {
if ($this->data['step'] == 'confirm') {
if (($this->data['step'] == 'review' || $this->data['step'] == 'define') && !$this->data['shown_related']) {
$suggestions = [];
foreach ($this->data['keywords'] as $word) {
$terms = $this->alert->get_related_terms($word);
if ($terms && count($terms)) {
$suggestions = array_merge($suggestions, $terms);
}
}

if (count($suggestions) > 0) {
$this->data['step'] = 'add_vector_related';
$this->data['suggestions'] = $suggestions;
}
} elseif ($this->data['step'] == 'confirm') {
$success = true;
if ($this->data['alert']) {
$success = $this->updateAlert($this->data['alert']['id'], $this->data);
Expand Down Expand Up @@ -122,6 +135,7 @@ private function getBasicData() {
$this->data['step'] = trim(get_http_var("step"));
$this->data['addword'] = trim(get_http_var("addword"));
$this->data['this_step'] = trim(get_http_var("this_step"));
$this->data['shown_related'] = get_http_var('shown_related');

if ($this->data['addword'] || $this->data['step']) {
$alert = $this->alert->check_token($this->data['token']);
Expand Down Expand Up @@ -155,6 +169,29 @@ private function getBasicData() {
$this->data['words'][] = $this->wrap_phrase_in_quotes($word);
}
}

$add_all_related = get_http_var('add_all_related');
$this->data['add_all_related'] = $add_all_related;
$this->data['skip_keyword_terms'] = [];

$selected_related_terms = get_http_var('selected_related_terms', [], true);
$this->data['selected_related_terms'] = $selected_related_terms;

if ($add_all_related) {
$this->data['selected_related_terms'] = [];
$related_terms = get_http_var('related_terms', [], true);
foreach ($related_terms as $term) {
$this->data['skip_keyword_terms'][] = $term;
$this->data['keywords'][] = $term;
$this->data['words'][] = $this->wrap_phrase_in_quotes($term);
}
} elseif ($this->data['step'] !== 'define') {
$this->data['skip_keyword_terms'] = $selected_related_terms;
foreach ($selected_related_terms as $term) {
$this->data['keywords'][] = $term;
$this->data['words'][] = $this->wrap_phrase_in_quotes($term);
}
}
$this->data['exclusions'] = trim(get_http_var("exclusions", implode('', $this->data['alert_parts']['exclusions'])));
$this->data['representative'] = trim(get_http_var("representative", $existing_rep));

Expand Down
16 changes: 16 additions & 0 deletions www/includes/easyparliament/alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,22 @@ public function fetch($confirmed, $deleted) {
return $data;
}

public function get_related_terms($term) {
$q = $this->db->query("SELECT
search_suggestion
FROM vector_search_suggestions
WHERE search_term = :term", [
':term' => $term,
]);

$data = $q->fetchAll();
$related = [];
foreach ($data as $d) {
$related[] = $d['search_suggestion'];
}
return $related;
}

public function update($id, $details) {
$criteria = \MySociety\TheyWorkForYou\Utility\Alert::detailsToCriteria($details);

Expand Down
63 changes: 42 additions & 21 deletions www/includes/easyparliament/templates/html/alert/_alert_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,45 +105,60 @@
</div>
<?php } elseif ($step == "add_vector_related") { ?>

<?php foreach ($keywords as $word) { ?>
<input type="hidden" name="words[]" value="<?= $word ?>">
<?php } ?>
<input type="hidden" name="shown_related" value="1">
<?php foreach ($keywords as $word) {
if (!in_array($word, $skip_keyword_terms)) { ?>
<input type="hidden" name="words[]" value="<?= _htmlspecialchars($word) ?>">
<?php }
} ?>
<input type="hidden" name="this_step" value="add_vector_related">
<input type="hidden" name="keyword" value="<?= _htmlspecialchars($keyword) ?>">
<input type="hidden" name="exclusions" value="<?= _htmlspecialchars($exclusions) ?>">
<input type="hidden" name="representative" value="<?= _htmlspecialchars($representative) ?>">
<input type="hidden" name="search_section" value="<?= _htmlspecialchars($search_section) ?>">
<input type="hidden" name="email" id="email" value="<?= _htmlentities($email) ?>">
<div class="alert-step" id="step2" role="region" aria-labelledby="step2-header">
<h2 id="step2-header">Adding some extras</h2>
<h2 id="step2-header"><?= gettext('Adding some extras') ?></h2>
<div class="keyword-list alert-page-subsection">
<h3 class="heading-with-bold-word">Current keywords in this alert:</h3>
<h3 class="heading-with-bold-word"><?= gettext('Current keywords in this alert:') ?></h3>
<ul>
<?php foreach ($keywords as $word) { ?>
<li class="label label--primary-light"><?= _htmlspecialchars($word) ?>
<i aria-hidden="true" role="img" class="fi-x"></i></li>
<?php } ?>
<?php foreach ($keywords as $word) {
if (!in_array($word, $skip_keyword_terms)) { ?>
<li class="label label--primary-light"><?= _htmlspecialchars($word) ?>
<i aria-hidden="true" role="img" class="fi-x"></i></li>
<?php }
} ?>
</ul>
<div class="add-remove-tool">
<input type="text" placeholder="e.g.'freedom of information'">
<button type="submit" class="prefix"><?= gettext('Add') ?></button>
</div>
</div>

<p><?= gettext('We have also found the following related terms. Pick the ones you’d like to include alert?') ?></p>

<fieldset>
<legend><?= gettext('Related Terms') ?></legend>
<div>
<?php foreach ($suggestions as $suggestion) { ?>
<input type="hidden" name="related_terms[]" value="<?= _htmlspecialchars($suggestion) ?>">
<label><input type="checkbox" name="selected_related_terms[]" value="<?= _htmlspecialchars($suggestion) ?>"<?= in_array($suggestion, $selected_related_terms) ? ' checked' : '' ?>><?= _htmlspecialchars($suggestion) ?></label><br>
<?php } ?>
<label><input type="checkbox" name="add_all_related" id="add-all"<?= $add_all_related == 'on' ? ' checked' : '' ?>><?= gettext('Add all related terms') ?></label>
</div>
</fieldset>

<dl class="alert-meta-info">
<?php if ($search_result_count > 0) { ?>
<div class="content-header-item">
<dt>This week</dt>
<dd><?= $search_result_count ?> mentions</dd>
<dt><?= gettext('This week') ?></dt>
<dd><?= sprintf(gettext('%d mentions'), $search_result_count) ?></dd>
</div>
<?php } ?>

<div class="content-header-item">
<dt>Date of last mention</dt>
<dd>30 May 2024</dd>
</div>
<?php if (isset($lastmention)) { ?>
<div class="content-header-item">
<dt><?= gettext('Date of last mention') ?></dt>
<dd>30 May 2024</dd>
</div>
<?php } ?>

<!-- Takes you to the result page of this query -->
<a href="/search/?q=<?= _htmlspecialchars($criteria) ?>" target="_blank" class="button small"><?= gettext('See results for this alert') ?></a>
</dl>

Expand All @@ -152,9 +167,15 @@
</div>
<?php } elseif ($step == "review") { ?>

<?php foreach ($keywords as $word) { ?>
<?php foreach ($keywords as $word) {
if (!in_array($word, $skip_keyword_terms)) { ?>
<input type="hidden" name="words[]" value="<?= _htmlspecialchars($word) ?>">
<?php }
} ?>
<?php foreach ($selected_related_terms as $word) { ?>
<input type="hidden" name="selected_related_terms[]" value="<?= _htmlspecialchars($word) ?>">
<?php } ?>
<input type="hidden" name="add_all_related" value="<?= $add_all_related ?>">
<input type="hidden" name="this_step" value="review">
<input type="hidden" name="keyword" value="<?= _htmlspecialchars($keyword) ?>">
<input type="hidden" name="exclusions" value="<?= _htmlspecialchars($exclusions) ?>">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
</form>
<form action="<?= $actionurl ?>" method="POST">
<input type="hidden" name="step" value="define">
<input type="hidden" name="shown_related" value="1">
<input type="hidden" name="t" value="<?= _htmlspecialchars($alert['token']) ?>">
<button type="submit" class="button small" value="Edit">
<span><?= gettext('Edit alert') ?></span>
Expand Down Expand Up @@ -199,6 +200,7 @@
</form>
<form action="<?= $actionurl ?>" method="POST">
<input type="hidden" name="step" value="define">
<input type="hidden" name="shown_related" value="1">
<input type="hidden" name="t" value="<?= _htmlspecialchars($alert['token']) ?>">
<button type="submit" class="button small" value="Edit">
<span><?= gettext('Edit alert') ?></span>
Expand Down

0 comments on commit 09528b1

Please sign in to comment.