From 3369d84146a344b8fae3ccf22859122a49f1e3df Mon Sep 17 00:00:00 2001
From: Frank Duncan <frank@kank.net>
Date: Thu, 22 Sep 2022 11:06:43 -0500
Subject: [PATCH] Add resetting choices

---
 extension.json                                |  7 ++---
 i18n/en.json                                  |  1 +
 includes/Deliberation.php                     | 12 ++++++++
 includes/api/DeliberationDelete.php           | 28 +++++++++++++++++++
 .../templates/DeliberationWidgetTemplate.php  |  3 ++
 resources/js/deliberation.js                  | 10 ++++++-
 6 files changed, 56 insertions(+), 5 deletions(-)
 create mode 100644 includes/api/DeliberationDelete.php

diff --git a/extension.json b/extension.json
index 29bf838..2cf4bb7 100644
--- a/extension.json
+++ b/extension.json
@@ -1,6 +1,6 @@
 {
 	"name": "Deliberation",
-	"version": "0.2.0",
+	"version": "0.2.1",
 	"author": [
 		"Open Tech Strategies"
 	],
@@ -44,6 +44,7 @@
 		"DeliberationSummary": "includes/specials/DeliberationSummary.php",
     "DeliberationHooks": "includes/DeliberationHooks.php",
     "DeliberationWidgetTemplate": "includes/templates/DeliberationWidgetTemplate.php",
+    "DeliberationDelete": "includes/api/DeliberationDelete.php",
     "DeliberationWrite": "includes/api/DeliberationWrite.php"
 	},
 	"ResourceFileModulePaths": {
@@ -53,13 +54,11 @@
     "ParserFirstCallInit": "DeliberationHooks::onParserFirstCallInit",
 		"LoadExtensionSchemaUpdates": "DeliberationHooks::onLoadExtensionSchemaUpdates"
 	},
-  "DeliberationWrite": {
-    "deliberationwrite": "DeliberationWrite"
-  },
   "config": {
     "DeliberationChoices": false
   },
   "APIModules": {
+    "deliberationdelete": "DeliberationDelete",
     "deliberationwrite": "DeliberationWrite"
   },
 	"manifest_version": 1,
diff --git a/i18n/en.json b/i18n/en.json
index 8fe5f0e..1a6b4b8 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -8,6 +8,7 @@
   "deliberation-desc": "Specialized Extension to choose pages for purposes of voting",
   "deliberation-choose": "Choose",
   "deliberation-reject": "Reject",
+  "deliberation-reset": "Reset",
   "deliberationsummary": "Deliberation Summary",
   "deliberationsummary-allpages": "All Pages"
 }
diff --git a/includes/Deliberation.php b/includes/Deliberation.php
index f8f53ab..10d6d9c 100644
--- a/includes/Deliberation.php
+++ b/includes/Deliberation.php
@@ -15,6 +15,18 @@ class Deliberation {
     }
   }
 
+  public static function remove($user, $title) {
+    // No deliberation on special pages!!
+    if($title->getArticleID() != 0) {
+      $lb = \MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer();
+      $dbw = $lb->getConnectionRef(DB_MASTER);
+      $dbw->delete("Deliberation", [
+        'page_id' => $title->getArticleID(),
+        'user_id' => $user->getId(),
+      ]);
+    }
+  }
+
   public static function limitHit($user, $value) {
     $limit = false;
     foreach(Self::getConfiguredChoices() as $choice) {
diff --git a/includes/api/DeliberationDelete.php b/includes/api/DeliberationDelete.php
new file mode 100644
index 0000000..bf49e2d
--- /dev/null
+++ b/includes/api/DeliberationDelete.php
@@ -0,0 +1,28 @@
+<?php
+class DeliberationDelete extends APIBase {
+  public function __construct($main, $action) {
+    parent::__construct($main, $action);
+  }
+
+  public function execute() {
+    parent::checkUserRightsAny(["deliberation-write"]);
+    $user = $this->getUser();
+    $title = Title::newFromText($this->getParameter('title'));
+
+    Deliberation::remove($user, $title);
+  }
+
+  public function mustBePosted() {
+    return true;
+  }
+
+  public function getAllowedParams() {
+    return [
+      "title" => [
+        ApiBase::PARAM_TYPE => 'string',
+        ApiBase::PARAM_REQUIRED => 'true'
+      ]
+    ];
+  }
+}
+?>
diff --git a/includes/templates/DeliberationWidgetTemplate.php b/includes/templates/DeliberationWidgetTemplate.php
index 1586b8b..c1f838c 100644
--- a/includes/templates/DeliberationWidgetTemplate.php
+++ b/includes/templates/DeliberationWidgetTemplate.php
@@ -27,6 +27,9 @@ class DeliberationWidgetTemplate extends QuickTemplate {
       }
 ?>
 </ul>
+<div class="deliberation-reset">
+<a href="#" class="reset-link"><?php echo wfMessage("deliberation-reset");?></span>
+</div>
 </div>
 <hr>
 <?php
diff --git a/resources/js/deliberation.js b/resources/js/deliberation.js
index a9aed08..554fe67 100644
--- a/resources/js/deliberation.js
+++ b/resources/js/deliberation.js
@@ -1,7 +1,7 @@
 (function ($, mw) {
   $(function() {
+    var api = new mw.Api();
     $(".deliberation input").change(function(e) {
-      var api = new mw.Api();
       var elem = $(this);
       api.post({
         action: "deliberationwrite",
@@ -9,6 +9,14 @@
         title: mw.config.values.wgTitle
       });
     });
+    $(".deliberation a.reset-link").click(function(e) {
+      $(".deliberation input").prop("checked", false);
+      api.post({
+        action: "deliberationdelete",
+        title: mw.config.values.wgTitle
+      });
+      return false;
+    });
   });
 }(jQuery, mediaWiki));
 
-- 
GitLab