EBernhardson has submitted this change and it was merged.

Change subject: Add option to skip a query
......................................................................


Add option to skip a query

Change-Id: I7d277b1f8b5754357a998be5b87a78873eae13a1
---
M schema.mysql.sql
M src/RelevanceScoring/Controller/QueriesController.php
M src/RelevanceScoring/RelevanceScoringProvider.php
M src/RelevanceScoring/Repository/QueriesRepository.php
M views/score_query.twig
5 files changed, 138 insertions(+), 67 deletions(-)

Approvals:
  EBernhardson: Verified; Looks good to me, approved



diff --git a/schema.mysql.sql b/schema.mysql.sql
index 9dd6ff8..7540caf 100644
--- a/schema.mysql.sql
+++ b/schema.mysql.sql
@@ -1,49 +1,57 @@
 CREATE TABLE IF NOT EXISTS `users` (
-       `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
-       `name` VARCHAR(500) NOT NULL,
-       `edit_count` INTEGER NOT NULL,
-       `created` INTEGER NOT NULL
+    `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+    `name` VARCHAR(500) NOT NULL,
+    `edit_count` INTEGER NOT NULL,
+    `created` INTEGER NOT NULL
 );
 CREATE TABLE IF NOT EXISTS `queries` (
-       `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
-       `user_id` INTEGER UNSIGNED NOT NULL,
-       `wiki` VARCHAR(100) NOT NULL,
-       `query` VARCHAR(500) NOT NULL,
-       `created` INTEGER NOT NULL,
-       `imported` TINYINT NOT NULL,
-       FOREIGN KEY `queries_user_id` (`user_id`) REFERENCES `users`(`id`),
-       UNIQUE KEY `queries_wiki_query` (`wiki`, `query`)
+    `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+    `user_id` INTEGER UNSIGNED NOT NULL,
+    `wiki` VARCHAR(100) NOT NULL,
+    `query` VARCHAR(500) NOT NULL,
+    `created` INTEGER NOT NULL,
+    `imported` TINYINT NOT NULL,
+    FOREIGN KEY `queries_user_id` (`user_id`) REFERENCES `users`(`id`),
+    UNIQUE KEY `queries_wiki_query` (`wiki`, `query`)
 );
 CREATE TABLE IF NOT EXISTS `results` (
-       id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
-       query_id INTEGER UNSIGNED NOT NULL,
-       title VARCHAR(10000) NOT NULL,
-       title_hash CHAR(32) NOT NULL,
-       created INTEGER UNSIGNED NOT NULL,
-       FOREIGN KEY `results_query_id` (`query_id`) REFERENCES `queries`(`id`),
-       UNIQUE KEY `results_unique_query_title` (`query_id`, `title_hash`)
+    id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+    query_id INTEGER UNSIGNED NOT NULL,
+    title VARCHAR(10000) NOT NULL,
+    title_hash CHAR(32) NOT NULL,
+    created INTEGER UNSIGNED NOT NULL,
+    FOREIGN KEY `results_query_id` (`query_id`) REFERENCES `queries`(`id`),
+    UNIQUE KEY `results_unique_query_title` (`query_id`, `title_hash`)
 );
 CREATE TABLE IF NOT EXISTS `results_sources` (
-       id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
-       results_id INTEGER UNSIGNED NOT NULL,
-       user_id INTEGER UNSIGNED NOT NULL,
-       source VARCHAR(32) NOT NULL,
-       position TINYINT UNSIGNED NOT NULL,
-       created INTEGER UNSIGNED NOT NULL,
-       FOREIGN KEY `results_user_id` (`user_id`) REFERENCES `users`(`id`),
-       FOREIGN KEY `results_source_results_id` (`results_id`) REFERENCES 
`results`(`id`),
-       UNIQUE KEY `results_source_results_id_source` (`results_id`, `source`)
+    id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+    results_id INTEGER UNSIGNED NOT NULL,
+    user_id INTEGER UNSIGNED NOT NULL,
+    source VARCHAR(32) NOT NULL,
+    position TINYINT UNSIGNED NOT NULL,
+    created INTEGER UNSIGNED NOT NULL,
+    FOREIGN KEY `results_user_id` (`user_id`) REFERENCES `users`(`id`),
+    FOREIGN KEY `results_source_results_id` (`results_id`) REFERENCES 
`results`(`id`),
+    UNIQUE KEY `results_source_results_id_source` (`results_id`, `source`)
 );
 CREATE TABLE IF NOT EXISTS scores (
-       id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
-       user_id INTEGER UNSIGNED NOT NULL,
-       result_id INTEGER UNSIGNED NOT NULL,
-       query_id INTEGER UNSIGNED NOT NULL,
-       score TINYINT UNSIGNED NOT NULL,
-       created INTEGER UNSIGNED NOT NULL,
-       FOREIGN KEY `scores_user_id` (user_id) REFERENCES users(id),
-       FOREIGN KEY `scores_result_id` (result_id) REFERENCES results(id),
-       FOREIGN KEY `scores_query_id` (query_id) REFERENCES queries(id),
-       UNIQUE KEY `scores_user_result` (`user_id`, `result_id`),
-       KEY `scores_user_queries` (`user_id`, `query_id`)
+    id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+    user_id INTEGER UNSIGNED NOT NULL,
+    result_id INTEGER UNSIGNED NOT NULL,
+    query_id INTEGER UNSIGNED NOT NULL,
+    score TINYINT UNSIGNED NOT NULL,
+    created INTEGER UNSIGNED NOT NULL,
+    FOREIGN KEY `scores_user_id` (user_id) REFERENCES users(id),
+    FOREIGN KEY `scores_result_id` (result_id) REFERENCES results(id),
+    FOREIGN KEY `scores_query_id` (query_id) REFERENCES queries(id),
+    UNIQUE KEY `scores_user_result` (`user_id`, `result_id`),
+    KEY `scores_user_queries` (`user_id`, `query_id`)
+);
+CREATE TABLE IF NOT EXISTS queries_skipped (
+    id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+    user_id INTEGER UNSIGNED NOT NULL,
+    query_id INTEGER UNSIGNED NOT NULL,
+    FOREIGN KEY `queries_skipped_user_id` (user_id) REFERENCES users(id),
+    FOREIGN KEY `queries_skipped_query_id` (query_id) REFERENCES queries(id),
+    UNIQUE KEY `queries_skipped_user_query` (`user_id`, `query_id`)
 );
diff --git a/src/RelevanceScoring/Controller/QueriesController.php 
b/src/RelevanceScoring/Controller/QueriesController.php
index 4012c2e..fce7071 100644
--- a/src/RelevanceScoring/Controller/QueriesController.php
+++ b/src/RelevanceScoring/Controller/QueriesController.php
@@ -67,6 +67,27 @@
         }
     }
 
+    public function skipQueryById(Request $request, $id)
+    {
+        $maybeQuery = $this->queriesRepo->getQuery($id);
+        if ($maybeQuery->isEmpty()) {
+            // @todo 404
+            throw new \Exception('Query not found');
+        }
+
+        $form = $this->createSkipForm($id);
+        $form->handleRequest($request);
+
+        // If the form isn't valid just do nothing, not a big deal. Should
+        // look into adding session based notifications to make it easier to
+        // tell users about this.
+        if ($form->isValid()) {
+            $this->queriesRepo->markQuerySkipped($this->user, $id);
+        }
+
+        return $this->app->redirect($this->app->path('random_query'));
+    }
+
     public function queryById(Request $request, $id)
     {
         $maybeQuery = $this->queriesRepo->getQuery($id);
@@ -88,6 +109,39 @@
             $this->user->uid
         );
 
+        $form = $this->createScoringForm($results);
+        $form->handleRequest($request);
+
+        if ($form->isValid()) {
+            $data = $form->getData();
+            $this->scoresRepo->storeQueryScores($this->user, $id, $data);
+
+            return $this->app->redirect($this->app->path('random_query', 
['saved' => 1]));
+        }
+
+        return $this->twig->render('score_query.twig', [
+            'query' => $query,
+            'results' => $results,
+            'form' => $form->createView(),
+            'saved' => (bool) $request->query->get('saved'),
+            'skipForm' => $this->createSkipForm($id)->createView(),
+        ]);
+    }
+
+    private function createSkipForm($queryId)
+    {
+        $builder = $this->formFactory->createBuilder('form')
+            ->setAction($this->app->path('skip_query_by_id', ['id' => 
$queryId]))
+            ->add('submit', 'submit', [
+                'label' => 'Skip this query',
+                'attr' => ['class' => 'btn btn-warning'],
+            ]);
+
+        return $builder->getForm();
+    }
+
+    private function createScoringForm(array $results)
+    {
         $builder = $this->formFactory->createBuilder('form', null, array(
             'constraints' => array(new MinimumSubmitted('80%')),
         ));
@@ -105,24 +159,8 @@
             ]);
         }
 
-        $form = $builder->getForm();
-        $form->handleRequest($request);
-
-        if ($form->isValid()) {
-            $data = $form->getData();
-            $this->scoresRepo->storeQueryScores($this->user, $id, $data);
-
-            return $this->app->redirect($this->app->path('random_query', 
['saved' => 1]));
-        }
-
-        return $this->twig->render('score_query.twig', [
-            'query' => $query,
-            'results' => $results,
-            'form' => $form->createView(),
-            'saved' => (bool) $request->query->get('saved'),
-        ]);
+        return $builder->getForm();
     }
-
 
     /**
      * PHP's shuffle function loses the keys. So sort the keys
diff --git a/src/RelevanceScoring/RelevanceScoringProvider.php 
b/src/RelevanceScoring/RelevanceScoringProvider.php
index 61b8d90..fff9ced 100644
--- a/src/RelevanceScoring/RelevanceScoringProvider.php
+++ b/src/RelevanceScoring/RelevanceScoringProvider.php
@@ -44,6 +44,8 @@
             ->bind('random_query_by_wiki');
         $controllers->match('/query/id/{id}',   
'search.controller.queries:queryById')
             ->bind('query_by_id');
+        $controllers->post('/query/skip/{id}',   
'search.controller.queries:skipQueryById')
+            ->bind('skip_query_by_id');
 
         return $controllers;
     }
diff --git a/src/RelevanceScoring/Repository/QueriesRepository.php 
b/src/RelevanceScoring/Repository/QueriesRepository.php
index c2d2993..7c24918 100644
--- a/src/RelevanceScoring/Repository/QueriesRepository.php
+++ b/src/RelevanceScoring/Repository/QueriesRepository.php
@@ -49,21 +49,31 @@
             ->from('queries', 'q')
             ->add('join', [
                 'q' => [
-                    'joinType' => 'left outer',
-                    'joinTable' => 'scores',
-                    'joinAlias' => 's',
-                    'joinCondition' => 'q.id = s.query_id AND s.user_id = ?',
+                    [
+                        'joinType' => 'left outer',
+                        'joinTable' => 'scores',
+                        'joinAlias' => 's',
+                        'joinCondition' => 'q.id = s.query_id AND s.user_id = 
?',
+                    ],
+                    [
+                        'joinType' => 'left outer',
+                        'joinTable' => 'queries_skipped',
+                        'joinAlias' => 'q_s',
+                        'joinCondition' => 'q.id = q_s.query_id AND 
q_s.user_id = ?',
+                    ],
                 ],
-            ], true)
+            ])
             ->setParameter(0, $user->uid)
+            ->setParameter(1, $user->uid)
             ->where('q.id > ?')
-            ->setParameter(1, $rand)
+            ->setParameter(2, $rand)
             ->andWhere('s.id IS NULL')
-                       ->andWhere('q.imported = 1')
+            ->andWhere('q_s.id IS NULL')
+            ->andWhere('q.imported = 1')
             ->orderBy('q.id', 'ASC');
         if ($wiki !== null) {
             $qb->andWhere('q.wiki = ?')
-                ->setParameter(2, $wiki);
+                ->setParameter(3, $wiki);
         }
 
         $id = $qb->execute()->fetchColumn();
@@ -113,7 +123,7 @@
     public function getQuery($id)
     {
         $result = $this->db->fetchAll(
-            'SELECT user_id, wiki, query, created, imported FROM queries WHERE 
id = ?',
+            'SELECT id, user_id, wiki, query, created, imported FROM queries 
WHERE id = ?',
             [$id]
         );
 
@@ -209,4 +219,13 @@
 
         return $this->db->fetchAll($sql, $params, $types);
     }
+
+    public function markQuerySkipped(User $user, $queryId)
+    {
+        $this->db->insert('queries_skipped', [
+            'user_id' => $user->uid,
+            'query_id' => $queryId,
+        ]);
+    }
+
 }
diff --git a/views/score_query.twig b/views/score_query.twig
index 3d9a9c0..2b8fa16 100644
--- a/views/score_query.twig
+++ b/views/score_query.twig
@@ -51,13 +51,16 @@
     {% endif %}
 
     <h3>Score Result</h3>
-    <div>
-        <dl class="dl-horizontal">
+    <div class="row">
+        <dl class="dl-horizontal col-md-8">
             <dt>Wiki</dt>
             <dd>{{ query.wiki }}</dd>
             <dt>Query</dt>
             <dd>{{ query.query }}</dd>
         </dl>
+        <div class="col-md-4">
+            {{ form(skipForm) }}
+        </div>
     </div>
 
     <noscript>
@@ -66,6 +69,7 @@
         </div>
     </noscript>
 
+
     <form method="post" novalidate>
         {{ form_errors(form) }}
         {{ form_row(form._token) }}

-- 
To view, visit https://gerrit.wikimedia.org/r/286096
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I7d277b1f8b5754357a998be5b87a78873eae13a1
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/discovery/discernatron
Gerrit-Branch: master
Gerrit-Owner: EBernhardson <ebernhard...@wikimedia.org>
Gerrit-Reviewer: EBernhardson <ebernhard...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to