Skip to content

Commit 6f730ea

Browse files
authored
Merge pull request #672 from hhaccessibility/add-user-answer-deleted-field
Add user answer deleted field
2 parents b3f476a + b09fd3f commit 6f730ea

15 files changed

Lines changed: 592 additions & 465 deletions

app/app/AnswerRepository.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ public static function commitAnswersForLocation(string $location_id)
212212
$location_key = 'answers_'.$location_id.'_';
213213
$now = new DateTime('now', new DateTimeZone('UTC'));
214214
$user_answers = [];
215+
$question_ids = [];
215216
$review_comments = [];
216217
foreach (Session::all() as $key => $obj) {
217218
if (strpos($key, $location_key) === 0) {
@@ -236,6 +237,7 @@ public static function commitAnswersForLocation(string $location_id)
236237
} else {
237238
// Answer to a question
238239
$question_id = intval(substr($key, strlen($location_key)));
240+
$question_ids []= $question_id;
239241
$answer_value = $obj;
240242
$user_answers []= [
241243
'id' => Uuid::generate(4)->string,
@@ -248,6 +250,11 @@ public static function commitAnswersForLocation(string $location_id)
248250
}
249251
}
250252
if (!empty($user_answers) || !empty($review_comments)) {
253+
UserAnswer::where('location_id', '=', $location_id)
254+
->where('answered_by_user_id', '=', $user_id)
255+
->whereNull('deleted_at')
256+
->whereIn('question_id', $question_ids)
257+
->update(['deleted_at' => date('Y-m-d H:i:s')]);
251258
UserAnswer::insert($user_answers);
252259
ReviewComment::insert($review_comments);
253260
// invalidate the cache.

app/app/Question.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,12 @@ public function getAccessibilityRating($location_id, $ratingSystem)
3737
if ($location->ratings_cache && isset($location->ratings_cache[''.$this->id])) {
3838
return $location->ratings_cache[$this->id];
3939
}
40-
40+
4141
$answers = $this->answers()
42-
->join('user_answer as a2', 'a2.answered_by_user_id', '=', 'user_answer.answered_by_user_id')
43-
->where('a2.location_id', '=', $location_id)
44-
->whereRaw('a2.question_id = user_answer.question_id')
42+
->whereNull('deleted_at')
43+
->where('location_id', '=', $location_id)
4544
->where('user_answer.location_id', '=', $location_id)
46-
->groupBy(['user_answer.answered_by_user_id', 'a2.when_submitted'])
47-
->havingRaw('max(user_answer.when_submitted) = a2.when_submitted')
48-
->get([DB::raw('avg(a2.answer_value) as answer_value')]);
45+
->get(['answer_value']);
4946
$sum = 0;
5047
$totalCount = 0;
5148
foreach ($answers as $answer) {

app/app/UserAnswer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44

55
use Illuminate\Database\Eloquent\Model;
66
use Webpatser\Uuid\Uuid;
7+
use Illuminate\Database\Eloquent\SoftDeletes;
78

89
class UserAnswer extends Model
910
{
11+
use SoftDeletes;
1012
protected $fillable = [
1113
'answered_by_user_id', 'question_id', 'location_id', 'answer_value',
1214
];
1315
public $timestamps = false;
14-
16+
protected $softDelete = true;
17+
protected $dates = ['deleted_at'];
1518
protected $table = 'user_answer';
16-
19+
1720
public function __construct()
1821
{
1922
$this->attributes = array('id' => Uuid::generate(4)->string);

app/composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
],
5252
"all": [
5353
"@composer cs",
54+
"@composer cs-migrations",
5455
"@composer cs-tests",
5556
"@composer tests"
5657
],
@@ -60,6 +61,7 @@
6061
],
6162
"cs": "\"vendor/bin/phpcs\" --standard=phpcs.xml",
6263
"cs-fix": "\"vendor/bin/phpcbf\" --standard=phpcs.xml",
64+
"cs-migrations": "\"vendor/bin/phpcs\" --standard=phpcs.migrations.xml",
6365
"cs-tests": "\"vendor/bin/phpcs\" --standard=phpcs.tests.xml",
6466
"cs-tests-fix": "\"vendor/bin/phpcbf\" --standard=phpcs.tests.xml",
6567
"tests": "\"vendor/bin/phpunit\""

0 commit comments

Comments
 (0)