Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RW-928] both feedback modes + new column #21

Merged
merged 7 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions modules/ocha_ai_chat/components/chat-form/chat-form.css
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ button doesn't overlay any text */
*/
.ocha-ai-chat-chat-form details {
display: block;
margin: 0 12px;
margin: 0;
padding: 12px;
border: 1px solid #ddd;
border-radius: 3px;
Expand Down Expand Up @@ -168,6 +168,10 @@ button doesn't overlay any text */
max-width: 100%;
}

.ocha-ai-chat-chat-form__advanced {
margin-inline: 12px;
}

/**
* Q/A history
*
Expand Down Expand Up @@ -337,12 +341,18 @@ button.feedback-button.feedback-button.feedback-button.feedback-button:focus {
background-color: var(--cd-green--light);
}

/* for copy button only */
.ocha-ai-chat-result-feedback .feedback-button--copy + [role="status"] {
font-size: .8em;
margin-inline-start: 0.5rem;
/**
* Clipboard feedback message
*/
.clipboard-feedback {
font-size: .75em;
position: relative;
top: -3px;
background: var(--cd-black);
color: var(--cd-white);
border-radius: 3px;
margin-inline-start: 0.5rem;
padding: 0.2rem 0.25rem;
}

/**
Expand Down
26 changes: 26 additions & 0 deletions modules/ocha_ai_chat/ocha_ai_chat.install
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ function ocha_ai_chat_schema() {
'size' => 'medium',
'not null' => FALSE,
],
'thumbs' => [
'description' => 'Whether user clicked thumbs up or down.',
'type' => 'text',
'size' => 'normal',
'not null' => TRUE,
'default' => '',
],
],
'primary key' => ['id'],
];
Expand Down Expand Up @@ -197,3 +204,22 @@ function ocha_ai_chat_update_10003(array &$sandbox) {
$schema->createTable('ocha_ai_chat_preferences', ocha_ai_chat_schema()['ocha_ai_chat_preferences']);
}
}

/**
* Implements hook_update_N().
*
* Add field to the OCHA AI chat logs table.
*/
function ocha_ai_chat_update_10004(array &$sandbox) {
$schema = \Drupal::database()->schema();

$thumbs = [
'description' => 'Whether user clicked thumbs up or down.',
'type' => 'text',
'size' => 'normal',
'not null' => TRUE,
'default' => '',
];

$schema->addField('ocha_ai_chat_logs', 'thumbs', $thumbs);
}
36 changes: 20 additions & 16 deletions modules/ocha_ai_chat/src/Form/OchaAiChatChatForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,11 @@ public function buildForm(array $form, FormStateInterface $form_state, ?bool $po
],
];

if ($feedback_type === 'simple') {
// There are multiple "modes" for feedback. We check the config value/
// before deciding what UI widgets to render.
if ($feedback_type === 'simple' || $feedback_type === 'both') {
// Container for simple feedback.
$form['chat'][$index]['feedback'] = [
$form['chat'][$index]['feedback_simple'] = [
'#type' => 'fieldset',
'#title' => $this->t('Provide feedback'),
'#title_display' => 'invisible',
Expand All @@ -206,10 +208,10 @@ public function buildForm(array $form, FormStateInterface $form_state, ?bool $po
];

// Thumbs up.
$form['chat'][$index]['feedback']['good'] = [
$form['chat'][$index]['feedback_simple']['good'] = [
'#type' => 'submit',
'#name' => 'chat-result-' . $index . '-simple-feedback-good',
'#value' => $this->t('Good'),
'#value' => $this->t('Like'),
'#attributes' => [
'class' => ['feedback-button', 'feedback-button--good'],
'data-result-id' => $record['id'],
Expand All @@ -222,10 +224,10 @@ public function buildForm(array $form, FormStateInterface $form_state, ?bool $po
];

// Thumbs down.
$form['chat'][$index]['feedback']['bad'] = [
$form['chat'][$index]['feedback_simple']['bad'] = [
'#type' => 'submit',
'#name' => 'chat-result-' . $index . '-simple-feedback-bad',
'#value' => $this->t('Bad'),
'#value' => $this->t('Dislike'),
'#attributes' => [
'class' => ['feedback-button', 'feedback-button--bad'],
'data-result-id' => $record['id'],
Expand All @@ -238,19 +240,21 @@ public function buildForm(array $form, FormStateInterface $form_state, ?bool $po
];

// Copy button.
$form['chat'][$index]['feedback']['copy'] = [
$form['chat'][$index]['feedback_simple']['copy'] = [
'#type' => 'inline_template',
'#template' => '<span><button class="feedback-button feedback-button--copy" data-for="{{ answer_id }}" data-message="{{ success_message }}"><span class="visually-hidden">Copy to clipboard</span></button><span hidden role="status" class="messages messages--status"></span></span>',
'#template' => '<span><button class="feedback-button feedback-button--copy" data-for="{{ answer_id }}" data-message="{{ success_message }}"><span class="visually-hidden">Copy to clipboard</span></button><span hidden role="status" class="clipboard-feedback"></span></span>',
'#context' => [
'answer_id' => $answer_id,
'success_message' => $this->t('Answer was copied to clipboard'),
],
];
}
else {

// Detailed feedback.
if ($feedback_type !== 'simple') {
$form['chat'][$index]['feedback'] = [
'#type' => 'details',
'#title' => $this->t('Please give feedback'),
'#title' => $this->t('Provide detailed feedback'),
'#id' => 'chat-result-' . $index . '-feedback',
'#open' => FALSE,
'#attributes' => [
Expand Down Expand Up @@ -447,19 +451,19 @@ public function submitSimpleFeedback(array &$form, FormStateInterface $form_stat

// Convert the thumbs up/down to an integer.
if ($feedback === 'good') {
$feedback_int = 4;
$feedback_msg = 'User clicked thumbs up';
$feedback_val = 'up';
$feedback_msg = $this->t('Glad you liked this answer.');
}
else {
$feedback_int = 2;
$feedback_msg = 'User clicked thumbs down';
$feedback_val = 'down';
$feedback_msg = $this->t('Thank you for your feedback.');
}

// Record the feedback.
$this->ochaAiChat->addAnswerFeedback($id, $feedback_int, $feedback_msg);
$this->ochaAiChat->addAnswerThumbs($id, $feedback_val);

$response = new AjaxResponse();
$response->addCommand(new MessageCommand($this->t('Feedback submitted, thank you.'), $selector));
$response->addCommand(new MessageCommand($feedback_msg, $selector));
return $response;
}

Expand Down
1 change: 1 addition & 0 deletions modules/ocha_ai_chat/src/Form/OchaAiChatConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#options' => [
'detailed' => $this->t('Detailed feedback'),
'simple' => $this->t('Simple feedback'),
'both' => $this->t('Both feedback modes'),
],
'#description' => $this->t('Simple feedback displays a thumbs up/down instead of offering open comment fields on each answer.'),
];
Expand Down
4 changes: 4 additions & 0 deletions modules/ocha_ai_chat/src/Form/OchaAiChatLogsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
'feedback' => [
'data' => $this->t('Feedback'),
],
'thumbs' => [
'data' => $this->t('Thumbs'),
],
'stats' => [
'data' => $this->t('Stats'),
],
Expand Down Expand Up @@ -265,6 +268,7 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
],
],
],
'thumbs' => $record->thumbs ?? '',
'stats' => [
'data' => [
'#type' => 'details',
Expand Down
23 changes: 23 additions & 0 deletions modules/ocha_ai_chat/src/Services/OchaAiChat.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,29 @@ public function addAnswerFeedback(int $id, int $satisfaction, string $feedback):
return !empty($updated);
}

/**
* Add thumbs up/down to an answer's log entry.
*
* @param int $id
* The ID of the answer log.
* @param string $value
* Up or down.
*
* @return bool
* TRUE if a record was updated.
*/
public function addAnswerThumbs(int $id, string $value): bool {
$updated = $this->database
->update('ocha_ai_chat_logs')
->fields([
'thumbs' => $value,
])
->condition('id', $id, '=')
->execute();

return !empty($updated);
}

/**
* Get a list of source documents for the given document source URL.
*
Expand Down