Skip to content

Commit

Permalink
Merge branch 'staging'
Browse files Browse the repository at this point in the history
  • Loading branch information
jringeisen committed Feb 10, 2024
2 parents 1420cf6 + 79bee6d commit 71da062
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 20 deletions.
3 changes: 1 addition & 2 deletions app/Http/Controllers/StudentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,10 @@ public function show(Request $request, User $user): Response
$timeframe = $request->get('timeframe', 'yearly');

return Inertia::render('Teachers/Students/Show', [
'student' => (new StudentResource($user->load('promptQuestions')))->resolve(),
'student' => (new StudentResource($user))->resolve(),
'totalQuestions' => $this->studentService->student($user)->totalQuestionsAsked($timeframe),
'dailyQuestions' => $this->studentService->student($user)->totalQuestionsAskedToday(),
'totalWordsRead' => $this->wordCountService->calculateTotalWordsRead($timeframe, $user->id),
'categoriesWithCounts' => $this->studentService->student($user)->categoriesWithCounts(),
'lineChartData' => $this->studentService->student($user)->lineChartData($timeframe),
'activeTime' => $this->studentService->student($user)->activeTime($timeframe),
'timeframe' => $timeframe,
Expand Down
30 changes: 24 additions & 6 deletions app/Models/PromptAnswer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -14,6 +15,7 @@ class PromptAnswer extends Model
protected $fillable = [
'subject_category',
'content',
'word_count',
'questions',
'outline',
];
Expand All @@ -23,17 +25,33 @@ public function promptQuestion(): BelongsTo
return $this->belongsTo(PromptQuestion::class);
}

protected function wordCount(): Attribute
protected function subjectCategory(): Attribute
{
return Attribute::make(
get: fn () => number_format(str_word_count($this->content)),
get: fn (?string $value) => $value ? $value : 'Not Categorized',
);
}

protected function subjectCategory(): Attribute
public function scopeFilterByTimeframe(Builder $query, string $timeframe): Builder
{
return Attribute::make(
get: fn (?string $value) => $value ? $value : 'Not Categorized',
);
return $query->when($timeframe, function (Builder $query) use ($timeframe) {
$usersTimezone = request()->user()->timezone;

$query->when($timeframe === 'yearly', function (Builder $query) use ($usersTimezone) {
$query->whereYear('prompt_answers.created_at', now($usersTimezone)->year);
})
->when($timeframe === 'monthly', function (Builder $query) use ($usersTimezone) {
$query->whereBetween('prompt_answers.created_at', [
now($usersTimezone)->startOfMonth(),
now($usersTimezone)->endOfMonth(),
]);
})
->when($timeframe === 'weekly', function (Builder $query) use ($usersTimezone) {
$query->whereBetween('prompt_answers.created_at', [
now($usersTimezone)->startOfWeek(),
now($usersTimezone)->endOfWeek(),
]);
});
});
}
}
2 changes: 1 addition & 1 deletion app/Services/Adapters/AI/OpenAI/OpenAIAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function () {
->promptAnswer()
->updateOrCreate(
['prompt_question_id' => $this->question->id],
['content' => $message]
['content' => $message, 'word_count' => str_word_count($message)]
);
}

Expand Down
15 changes: 5 additions & 10 deletions app/Services/WordCountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@

namespace App\Services;

use App\Models\PromptQuestion;
use App\Models\PromptAnswer;

class WordCountService
{
public function calculateTotalWordsRead(string $timeframe, mixed $userId = null): string
{
$count = PromptQuestion::query()
->when($userId, fn ($query) => $query->where('user_id', $userId))
->when(! $userId, fn ($query) => $query->whereHas('user', fn ($query) => $query->where('parent_id', request()->user()->id)))
$count = PromptAnswer::query()
->when($userId, fn ($query) => $query->whereHas('promptQuestion', fn ($query) => $query->where('user_id', $userId)))
->when(! $userId, fn ($query) => $query->whereHas('promptQuestion.user', fn ($query) => $query->where('parent_id', request()->user()->id)))
->filterByTimeframe($timeframe)
->with('promptAnswer')
->whereHas('promptAnswer')
->get()
->reduce(function (int $carry, PromptQuestion $promptQuestion) {
return $carry + (int) $promptQuestion->promptAnswer->word_count;
}, 0);
->sum('word_count');

return number_format($count);
}
Expand Down
31 changes: 31 additions & 0 deletions database/factories/PromptAnswerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Database\Factories;

use App\Models\PromptQuestion;
use Illuminate\Database\Eloquent\Factories\Factory;

class PromptAnswerFactory extends Factory
{
public function definition(): array
{
return [
'prompt_question_id' => PromptQuestion::factory()->create(),
'subject_category' => $this->faker->word,
'content' => $this->faker->paragraph,
'questions' => $this->createArrayOfQuestions(),
'word_count' => 0,
];
}

protected function createArrayOfQuestions(): array
{
$questions = [];

for ($i = 0; $i < 15; $i++) {
$questions[] = $this->faker->sentence;
}

return $questions;
}
}
18 changes: 18 additions & 0 deletions database/factories/PromptQuestionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Database\Factories;

use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

class PromptQuestionFactory extends Factory
{
public function definition(): array
{
return [
'user_id' => User::factory()->create(),
'question' => $this->faker->sentence,
'total_tokens' => $this->faker->numberBetween(1, 500),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use App\Models\PromptAnswer;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('prompt_answers', function (Blueprint $table) {
$table->integer('word_count')->default(0)->after('content');
});

PromptAnswer::all()->each(function (PromptAnswer $promptAnswer) {
$promptAnswer->update([
'word_count' => str_word_count($promptAnswer->content),
]);
});
}

public function down(): void
{
Schema::table('prompt_answers', function (Blueprint $table) {
$table->dropColumn('word_count');
});
}
};
1 change: 0 additions & 1 deletion resources/js/Pages/Teachers/Students/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ const { startCase } = pkg;
const props = defineProps({
student: Object,
categoriesWithCounts: Object,
totalQuestions: Number,
dailyQuestions: Number,
totalWordsRead: String,
Expand Down

0 comments on commit 71da062

Please sign in to comment.