Skip to content

Suggestions to use periodicEvents for AnonymousQuestions #554

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

Open
wants to merge 12 commits into
base: development
Choose a base branch
from

Conversation

kdmnk
Copy link
Contributor

@kdmnk kdmnk commented Jun 15, 2024

@viktorcsimma I think I know what you found confusing about the periodicEvents.

With PeriodicEvents, you cannot create a question for a previous semester, you can only create and answer a question for the current period (which makes sense to me, but let me know if you don't agree).
When the dates of the semesterEvaluation are set, the semester to which it applies must also be specified. You can use that semester via the periodicEvent which will also define the deadlines for the questions.

Questions for older semesters can still be exported as they are stored for their semesters.

This PR contains how I think the controller should utilise the PeriodicEvents. The other parts of the solution should be modified accordingly.

Feel free to take over this branch or create a new one.

@kdmnk kdmnk requested a review from viktorcsimma June 15, 2024 20:04
@kdmnk kdmnk requested a review from a team as a code owner June 15, 2024 20:04
Copy link
Contributor

coderabbitai bot commented Jun 15, 2024

Walkthrough

Walkthrough

The recent updates focus on refining the AnonymousQuestionController and related views, models, and routes. Key changes include renaming methods for clarity, adding new methods for handling question creation, and updating validation rules. Additionally, the migration alters the database schema to make certain columns nullable. Updates to language files introduce new messages to guide users, while route adjustments ensure consistency and simplicity.

Changes

File Path Change Summary
app/Http/Controllers/StudentsCouncil/AnonymousQuestionController.php Renamed methods, added new methods for question handling, and refined conditions and checks.
app/Models/Question.php Updated storeAnswers to accept null for answers and changed validation rules for long answers.
app/Models/Semester.php Updated namespace references in hasMany method calls.
database/migrations/2024_07_11_133614_make_long_answers_nullable.php New migration to make long_answers.text column nullable, then revert to non-nullable with default.
resources/lang/en/anonymous_questions.php Added new message creation_for_current_only.
resources/lang/hu/anonymous_questions.php Added new message creation_for_current_only in Hungarian.
resources/views/student-council/anonymous-questions/create.blade.php Updated breadcrumb route from index_semesters to index.
resources/views/student-council/anonymous-questions/index.blade.php Modified export button route, adjusted semester status conditions, and added logic for active semester.
routes/web.php Simplified route definitions and updated controller methods and route names for anonymous questions.
app/Http/Controllers/Secretariat/SemesterEvaluationController.php Updated functionality for setting question opening and closing dates.
resources/views/secretariat/evaluation-form/anonymous_questions.blade.php Changed form action to dynamically store answers based on the current semester.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant C as AnonymousQuestionController
    participant V as View
    participant M as Model

    U->>C: Request to create question (POST /questions/create)
    C->>M: Validate semester status, create question
    M-->>C: Question creation success/failure
    C-->>V: Render response view

    U->>C: Request to view questions (GET /questions)
    C->>M: Fetch questions list
    M-->>C: Return questions list
    C-->>V: Render questions list view

    U->>C: Request to store answers (POST /{semester}/sheets/)
    C->>M: Validate and store answers
    M-->>C: Answers storage success/failure
    C-->>V: Render response view
Loading

Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5c37f2a and 42ab4dc.

Files selected for processing (2)
  • app/Http/Controllers/Secretariat/SemesterEvaluationController.php (2 hunks)
  • resources/views/secretariat/evaluation-form/anonymous_questions.blade.php (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • app/Http/Controllers/Secretariat/SemesterEvaluationController.php
  • resources/views/secretariat/evaluation-form/anonymous_questions.blade.php

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@kdmnk kdmnk marked this pull request as draft June 15, 2024 20:04
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Outside diff range and nitpick comments (2)
app/Http/Controllers/StudentsCouncil/AnonymousQuestionController.php (2)

Line range hint 61-97: The refactoring in the store method removes the need for a Semester parameter, relying instead on the periodicEvent to determine the applicable semester. The validation logic is comprehensive, but consider using a more robust approach for managing optional fields to enhance maintainability.

- 'options.*' => 'exclude_if:has_long_answers,on|nullable|string|max:255',
+ 'options.*' => 'exclude_if:has_long_answers,on|required|string|max:255',

Additionally, ensure that the options array is always validated as an array of strings, even when has_long_answers is on, to prevent potential issues with data integrity.


[!TIP]
Codebase Verification

The route definition for the show method in AnonymousQuestionController still includes the {semester} parameter, which is inconsistent with the updated method signature that only accepts a Question parameter. This needs to be corrected to ensure consistency across the application.

  • routes/web.php: Update the route definition for AnonymousQuestionController::show to remove the {semester} parameter.
Analysis chain

Line range hint 116-123: The show method has been simplified to accept only a Question parameter. This change is consistent with the PR's theme of simplifying the handling of semesters. Ensure that all references to this method across the application are updated to reflect this change.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all method calls to `show` have been updated.

# Test: Search for the method usage. Expect: Only occurrences with the new signature.
rg --type php $'show'

Length of output: 14781



Script:

#!/bin/bash
# Description: Verify that the route definition for AnonymousQuestionController::show has been updated to reflect the new signature.

# Test: Search for the route definition in routes/web.php. Expect: No occurrences with the old signature.
rg 'Route::get\(\'/\{semester\}/questions/\{question\}\', \[AnonymousQuestionController::class, \'show\'\]\)' routes/web.php

Length of output: 93



Script:

#!/bin/bash
# Description: Verify that the route definition for AnonymousQuestionController::show has been updated to reflect the new signature.

# Test: Search for the route definition in routes/web.php. Expect: No occurrences with the old signature.
rg "Route::get\('/\{semester\}/questions/\{question\}', \[AnonymousQuestionController::class, 'show'\]\)" routes/web.php

Length of output: 249

Comment on lines 48 to 55
public function create()
{
$this->authorize('administer', AnswerSheet::class);

if ($semester->isClosed()) {
if (!$this->isActive()) {
abort(403, "tried to add a question to a closed semester");
}
return view('student-council.anonymous-questions.create', [
"semester" => $semester
]);
return view('student-council.anonymous-questions.create');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The create method now includes a check for active periodic events before allowing the creation of a new question. This is a sensible addition to prevent modifications outside the designated period. However, the error message could be more descriptive to enhance user understanding.

- abort(403, "tried to add a question to a closed semester");
+ abort(403, "Attempted to add a question outside the active period. Please check the current settings and try again.");
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function create()
{
$this->authorize('administer', AnswerSheet::class);
if ($semester->isClosed()) {
if (!$this->isActive()) {
abort(403, "tried to add a question to a closed semester");
}
return view('student-council.anonymous-questions.create', [
"semester" => $semester
]);
return view('student-council.anonymous-questions.create');
public function create()
{
$this->authorize('administer', AnswerSheet::class);
if (!$this->isActive()) {
abort(403, "Attempted to add a question outside the active period. Please check the current settings and try again.");
}
return view('student-council.anonymous-questions.create');

@@ -108,15 +105,15 @@ public function store(Request $request, Semester $semester)
}
}

session()->put('section', $semester->id);
//session()->put('section', $semester->id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The redirect route anonymous_questions.index_semesters appears to be outdated or incorrect as per the changes in this PR which suggest a simplification of route names.

- return redirect()->route('anonymous_questions.index_semesters')
+ return redirect()->route('anonymous_questions.index')

This change aligns the redirection with the updated routing structure.

Committable suggestion was skipped due to low confidence.

@viktorcsimma
Copy link
Contributor

Thank you:) I start to get the idea. So if I understand right, there is only at most one PeriodicEvent connected to a controller. If a new semester comes, the semester_id field is updated in updatePeriodicEvent. Still, because the things belonging to the event get the semester id when they get created, they can be retrieved later.

I think it's fine if they can only create questions for the current semester; I agree it isn't worth the fuss to allow them to manipulate future semesters which are half a year later. I just didn't understand then.

Thank you again; I think I'll pick it up here in the summer;)

Copy link
Contributor

@viktorcsimma viktorcsimma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really starting to get the idea; thank you:) I'll take over from here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

@viktorcsimma viktorcsimma marked this pull request as ready for review July 10, 2024 14:26
viktorcsimma and others added 2 commits July 10, 2024 18:48
This commit fixes the style issues introduced in 18b3041 according to the output
from PHP CS Fixer.

Details: #554
viktorcsimma and others added 7 commits July 10, 2024 18:51
This commit fixes the style issues introduced in 9085d63 according to the output
from PHP CS Fixer.

Details: #554
…he end date of the periodic event"

This reverts commit 18b3041.
This commit fixes the style issues introduced in a95736f according to the output
from PHP CS Fixer.

Details: #554
Comment on lines 48 to 71
/**
* Checks whether the form exists and has not yet been closed;
* aborts the request if necessary.
* If successful, it returns the periodic event.
*/
private function checkPeriodicEvent(): PeriodicEvent
{
$periodicEvent = $this->periodicEvent();
if (is_null($periodicEvent)) {
abort(404, "no evaluation form exists yet");
} elseif ($periodicEvent->endDate()?->isPast() ?? false) {
abort(403, "tried to add a question to a closed form");
} else {
return $periodicEvent;
}
}

/**
* Returns the 'new question' page.
*/
public function create(Semester $semester)
public function create()
{
$this->authorize('administer', AnswerSheet::class);
$this->checkPeriodicEvent();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this?

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Comment on lines +34 to +37
@php
$isActive = $currentSemester?->id == $semester->id
&& (!$periodicEvent->endDate()?->isPast() ?? false);
@endphp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle the scenario where $periodicEvent is null more gracefully.

Consider adding a check to handle the case where $periodicEvent is null to avoid potential issues.

- $isActive = $currentSemester?->id == $semester->id && (!$periodicEvent->endDate()?->isPast() ?? false);
+ $isActive = $currentSemester?->id == $semester->id && ($periodicEvent ? !$periodicEvent->endDate()?->isPast() : false);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@php
$isActive = $currentSemester?->id == $semester->id
&& (!$periodicEvent->endDate()?->isPast() ?? false);
@endphp
@php
$isActive = $currentSemester?->id == $semester->id
&& ($periodicEvent ? !$periodicEvent->endDate()?->isPast() : false);
@endphp

Comment on lines +8 to +12
@php
// let the current semester be found based on the periodic event itself
// beware: it might be null
$periodicEvent = app(\App\Http\Controllers\Secretariat\SemesterEvaluationController::class)->periodicEvent();
$currentSemester = $periodicEvent?->semester;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle the scenario where $periodicEvent is null more gracefully.

Consider adding a check to handle the case where $periodicEvent is null to avoid potential issues.

- $periodicEvent = app(\App\Http\Controllers\Secretariat\SemesterEvaluationController::class)->periodicEvent();
- $currentSemester = $periodicEvent?->semester;
+ $periodicEvent = app(\App\Http\Controllers\Secretariat\SemesterEvaluationController::class)->periodicEvent();
+ $currentSemester = $periodicEvent ? $periodicEvent->semester : null;
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@php
// let the current semester be found based on the periodic event itself
// beware: it might be null
$periodicEvent = app(\App\Http\Controllers\Secretariat\SemesterEvaluationController::class)->periodicEvent();
$currentSemester = $periodicEvent?->semester;
@php
// let the current semester be found based on the periodic event itself
// beware: it might be null
$periodicEvent = app(\App\Http\Controllers\Secretariat\SemesterEvaluationController::class)->periodicEvent();
$currentSemester = $periodicEvent ? $periodicEvent->semester : null;

@@ -136,6 +136,9 @@ public function show()
$this->authorize('fillOrManage', SemesterEvaluation::class);

return view('secretariat.evaluation-form.app', [
// let the current semester be found based on the periodic event itself
// we can safely assume it is not null
'semester' => app(\App\Http\Controllers\Secretariat\SemesterEvaluationController::class)->semester(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are already in the controlller. Use $this->semester().

@php
// let the current semester be found based on the periodic event itself
// beware: it might be null
$periodicEvent = app(\App\Http\Controllers\Secretariat\SemesterEvaluationController::class)->periodicEvent();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you pass this to the blade from the controller?

@horcsinbalint horcsinbalint force-pushed the development branch 2 times, most recently from 758a095 to 86e46a2 Compare February 15, 2025 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants