Skip to content

Commit

Permalink
add Answer
Browse files Browse the repository at this point in the history
  • Loading branch information
skywalker512 committed Jun 30, 2018
1 parent 7ec43bb commit 93b4d1e
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 0 deletions.
37 changes: 37 additions & 0 deletions addons/plugins/Answers/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.buildpath
.project
.settings
.htaccess

# Compiled source
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases
*.log
*.sql
*.sqlite

# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
*~
24 changes: 24 additions & 0 deletions addons/plugins/Answers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Answers Plugin

Allow posters to mark a reply as having answered their question.

## Installation

[Download](https://github.com/esotalk/Answers/archive/master.zip) or clone the Answers plugin repo into your esoTalk plugin directory:

cd ESOTALK_DIR/addons/plugins/
git clone [email protected]:esotalk/Answers.git Answers

Navigate to the the admin/plugins page and activate the Answers plugin.

## Translation

Create `definitions.Answers.php` in your language pack with the following definitions:

$definitions["Answer"] = "Answer";
$definitions["Answered"] = "Answered";
$definitions["Answered by %s"] = "Answered by %s";
$definitions["label.answered"] = "Answered";
$definitions["Remove answer"] = "Remove answer";
$definitions["See post in context"] = "See post in context";
$definitions["This post answered my question"] = "This post answered my question";
Binary file added addons/plugins/Answers/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
110 changes: 110 additions & 0 deletions addons/plugins/Answers/plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
// Copyright 2013 Toby Zerner, Simon Zerner
// This file is part of esoTalk. Please see the included license file for usage information.

if (!defined("IN_ESOTALK")) exit;

ET::$pluginInfo["Answers"] = array(
"name" => "最佳回复",
"description" => "允许用户设置某个回帖为最佳回复,并显示在回帖列表的最前",
"version" => ESOTALK_VERSION,
"author" => "Toby Zerner",
"authorEmail" => "[email protected]",
"authorURL" => "http://esotalk.org",
"license" => "GPLv2",
"dependencies" => array(
"esoTalk" => "1.0.0g4"
)
);

class ETPlugin_Answers extends ETPlugin {

// Setup: add an answered column to the conversation table.
public function setup($oldVersion = "")
{
$structure = ET::$database->structure();
$structure->table("conversation")
->column("answered", "int(11) unsigned")
->exec(false);

return true;
}

public function init()
{
ET::conversationModel();
ETConversationModel::addLabel("answered", "IF(c.answered > 0,1,0)", "icon-ok-sign");

ET::define("label.answered", "Answered");
}

public function handler_renderBefore($sender)
{
$sender->addCSSFile($this->resource("answers.css"));
}

public function handler_conversationController_formatPostForTemplate($sender, &$formatted, $post, $conversation)
{
if ($post["deleteMemberId"]) return;

if ($conversation["startMemberId"] == ET::$session->userId or $conversation["canModerate"]) {
addToArray($formatted["controls"], "<a href='".URL("conversation/answer/".$post["postId"]."?token=".ET::$session->token)."' title='".T("This post answered my question")."' class='control-answer'><i class='icon-ok-sign'></i></a>");
}

// If this post is the answer...
if ($conversation["answered"] == $post["postId"]) {
addToArray($formatted["info"], "<span class='label label-answered'><i class='icon-ok-sign'></i> ".T("Answer")."</span>", 100);
}

// If this is the first post in the conversation and there is an answer...
if ($conversation["answered"] and $post["memberId"] == $conversation["startMemberId"] and $post["time"] == $conversation["startTime"]) {

// Get the answer post.
$answer = ET::postModel()->getById($conversation["answered"]);
$view = $sender->getViewContents("answers/answer", array("answer" => $answer, "conversation" => $conversation));

// Add this before the "likes" plugin. Bit hacky, but there's no way to prioritize event handlers in esoTalk :(
$pos = strpos($formatted["body"], "<p class='likes");
if (!$pos) $pos = strlen($formatted["body"]);
$formatted["body"] = substr_replace($formatted["body"], $view, $pos, 0);

}
}

public function action_conversationController_answer($sender, $postId)
{
$conversation = ET::conversationModel()->getByPostId($postId);

if (!$conversation or !$sender->validateToken()) return;

// Stop here with an error if the user isn't allowed to mark the conversation as answered.
if ($conversation["startMemberId"] != ET::$session->userId and !$conversation["canModerate"]) {
$sender->renderMessage(T("Error"), T("message.noPermission"));
return false;
}

$model = ET::conversationModel();
$model->updateById($conversation["conversationId"], array("answered" => $postId));

redirect(URL(R("return", postURL($postId))));
}

public function action_conversationController_unanswer($sender, $conversationId)
{
$conversation = ET::conversationModel()->getById($conversationId);

if (!$conversation or !$sender->validateToken()) return;

// Stop here with an error if the user isn't allowed to mark the conversation as answered.
if ($conversation["startMemberId"] != ET::$session->userId and !$conversation["canModerate"]) {
$sender->renderMessage(T("Error"), T("message.noPermission"));
return false;
}

$model = ET::conversationModel();
$model->updateById($conversation["conversationId"], array("answered" => 0));

redirect(URL(R("return", conversationURL($conversation["conversationId"], $conversation["title"]))));
}

}
11 changes: 11 additions & 0 deletions addons/plugins/Answers/resources/answers.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.label-answered, .gambit-answered {
color:green !important;
}

.answer {
border-color:#82d777;
margin:25px 0 10px;
}
.answer h3, .answer h3 a {
color:green;
}
29 changes: 29 additions & 0 deletions addons/plugins/Answers/views/answers/answer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
// Copyright 2013 Toby Zerner, Simon Zerner
// This file is part of esoTalk. Please see the included license file for usage information.

if (!defined("IN_ESOTALK")) exit;

$conversation = $data["conversation"];
$post = $data["answer"];
?>
<div class='answer thing hasControls'>
<div class='postHeader'>
<div class='info'>
<h3><i class="icon-ok-sign"></i> <?php printf(T("Answered by %s"), memberLink($post["memberId"], $post["username"])); ?></h3>
<a href='<?php echo URL(postURL($post["postId"])); ?>' rel='post' data-id='<?php echo $post["postId"]; ?>'><?php echo T("See post in context"); ?></a>
</div>
<div class='controls'>
<?php if ($conversation["startMemberId"] == ET::$session->userId or $conversation["canModerate"]): ?>
<a href='<?php echo URL("conversation/unanswer/".$conversation["conversationId"]."?token=".ET::$session->token); ?>' title='<?php echo T("Remove answer"); ?>' class='control-unanswer'><i class='icon-remove'></i></a>
<?php endif; ?>
</div>
</div>

<div class='postBody'>
<?php
$words = ET::$session->get("highlight");
echo ET::formatter()->init($post["content"])->highlight($words)->format()->get();
?>
</div>
</div>

0 comments on commit 93b4d1e

Please sign in to comment.