Laravel 5 Quiz Project - php

This is probably more of a conceptual question but I am trying to find the best way to make a view that displays one quiz question at a time and checks the answer.
Currently my MySQL table has the following columns: id, category_id, quiz_question, answer_one, answer_two, answer_three, correct_answer.
My controller uses the following to get the quiz questions:
$quizzes = Quiz::where('category_id',$category_id)->simplePaginate(1);
In my view, I go through the process of doing the asnwer ordering manually
{{$quiz->quiz_question }}
<a onclick="this.innerHTML='Wrong'">{{ $quiz->answer_one }}</a>
<a onclick="this.innerHTML='Right'">{{ $quiz->correct_answer }}</a>
<a onclick="this.innerHTML='Wrong'">{{ $quiz->answer_two }}</a>...etc.
and was probably just going to use JavaScript/JQuery to check for the right answer.
Conceptually this feels like a pretty bad way to do this and I am still not sure how to randomize the order of the answers if they are from the same DB table like this. Overall is there a better way to go about this (Display 1 quiz question, and display answers in random order, check without a DB query)? Thanks in advance.

Do not keep the answers on the client side in any form.
Instead, just get the questions and paginate it on the client side. If you absolutely need to validate answers one by one, then fire up AJAX requests.
Or you can just let the person attempt the whole quiz and post the questions with users' answers and validate them on the server side. Then you return the result/score/answers.
Regarding your query about checking without DB query, you shouldn't do it. If you absolutely need it then save answers in an object, rather than in the DOM. Again, it can't be any more "not recommended".
Edit:
You should first divide it into these tables. You can't randomize the order of display of the options without a hack from the first table.
And don't name the columns like answer_one, answer_two, answer_three, correct_answer. In this manner the only way to recognize the answer in by the column name correct_answer. Column name shouldn't give a hint about the correct answer. It should be stored somewhere else.
questions
id
question - the question body
categories
id
category - name of the category
question_categories (one question may belong to multiple categories)
id
question_id
category_id
options
id
question_id
option - option text
answers
id
question_id
option_id
Now what you do is you randomize the order of options
$questions = Question::where('category_id', $category_id)->
with(['options' => function ($query) {
$query->orderBy(DB::raw('RAND()'));
}])
->get();
To randomly order the options we used ORDER BY RAND().
Now you just send the option_id and can check if it is the correct answer on the server side.
Edit - 27th March
You can use the following models.
Question
QuestionCategory
Category
Option
Answer
You can use the following relationships.
Question belongsTo QuestionCategory
QuestionCategory belongsTo Category
Question hasMany Option
Question hasOne Answer
But creating a QuestionCategory model doesn't really scale well. So if you don't want to do that you can use Polymorphic relationships. You can use category table to store basically all types of categories, not just question categories. In that case, you need to modify the categories tables to add the type of taxonomy. If you don't understand any of this polymorphic relationship thing, please find it in the official Laravel documentation https://laravel.com/docs/5.1/eloquent-relationships#polymorphic-relations

Related

Best practice in CodeIgniter, when selecting data from multiple related models

I'm trying to figure out what is the best practice with selecting data from two related model.
I have a model (and a db table) "person" and then i have a model (and a db table) comment. There may be multiple comments for one person and comment has "person_id" column.
I have two cases in particular.
First i need to show persons profile with all his comments.
In my controller do I select the needed person through my person model and then select all the comments through my comments model? Or is it more correct for person model already return person with all comments?
I myself would guess that first option is ok.
Second case is where i need to show all latest comments with name of the person who made the comment.
So in my controller is it correct to select all latest comments from comments model and then select a person for each or them? Or is it more correct for the comments model to return comment with person name included?
In this case i would guess that second option is better. It seems really strange to first select comments and then iterate them and select a person for each of them.
So im kind of confused because case 1 and 2 seem similar but i would use different solutions for them. Which one is correct?

How do i link database relationship with the following information?

I have been relentlessly trying to edit my database relationships but to no avail. Here is the scenario:
i have an inventory table. an inventory can be classified into 2 categories(technical eg. guns, radios and general eg. uniforms). i was hoping there could be a way for me to add details(from different tables eg. technical_inventory_table and general_inventory_table) to the inventory table through the category since the two categories have different fields. is there any way for me to do that? please do note that a technical inventory will be listed individually since a single item will have a serial number attached to it.
or does the answer provided here - https://dba.stackexchange.com/questions/33099/storing-different-products-and-joining-on-table-names - be an already good solution for my problem? i would like to avoid creating a table where it will be made up of what usually are the fieldnames as described in the link to prevent confusion within my team.
thanks for any help in advance

Handling table attributes/properties using CakePHP and MySQL

First of all, happy new year :-)
I think my question would be obvious for good programmers but I didn't manage to find an answer.
Let's say I have a Customer table and I would like this Customer to have some attributes (properties). I don't want to add a new field in the Customer table every time I will add a new attribute. Moreover, customers don't have the same number of attributes.
All that we know is each attributes is known in advance (some day, I will add a 'referer', 'age', 'type_of_prefered_food' .... attributes that is not mandatory, so customers may fill the information or not).
My question is, which is the best way to manage it using CakePHP 3 and MySQL ?
Having a Customer HasMany Attributes (If think in this case I have to have a constant list of each properties ID)
OR
Having a Customer BelongsToMany Attributes (so, through a relation table)
Knowing that, which is the best :
Having a attribute ID referenced by an INTEGER or a STRING ?
How to retrieve a specific property ...
My question is mostly CakePHP oriented, I want to know if one approach is easier to work with. Is there a CakePHP Tools/function/library that will help me to retrieve/add/edit easily attributes to a specific Customer ?
The best question I managed to find is : How to pivot a MySQL entity-attribute-value schema
Thanks in advance for those having the time to respond.

Is it better to have separate tables for articles & comments, or one table for both?

I am working on a little project where a user submits an article to MySQL, and then PHP send the post to the screen. So far so good.
Now i want to extend it to a "two level" post system, where people can comment on the articles.
The case is, i dont know how to do that.
The table i use for storing articles:
TABLE: posts
id user date avatar signature post
Should i make a new row named comments in the posts table, or should i place the comments in a seperate table? one for articles, one for comments?
All help is much appreciated.
Depends on how you use it on your website. You have to ask: "are my articles and comments essentially the same concept?" If yes, then use one table, if no, use two. On most websites articles work differently, can be categorized, editted etc., and usually need a different fields which would clutter the comments table... so in that case two tables are more reasonable. But if you conclude that on your website articles and comments are generally the same (but try to think future proof: wouldn't you need to add some article functionality in 2 months?) then you can think of articles also as of comments and have one table for them.
If you decide to merge things to one table, and you realize that you need another column to distinguish type of the post, and that some columns remain unused for some types, it is a clear warning signal you should have two tables!
This is a little subjective, but I would just set up a parent/child relationship on your posts table, make a parent_id column that is a foreign key for the id column in the same table. for extra credit, you can make it a nested set relationship which will allow you to pull the parent and all the children in one query

need mySQL/php advice how to build specific mysql db

The following description is a simple example with questions and answers. But the logic of my site is similar.
Lets say tables are:
USERS table: USER_ID, etc
QUESTIONS table: QUESTION_ID, TEXT, CATEGORY, CORRECT_RESPONSE, AVAILABLE
RESPONSES table: QUESTION_ID, USER_ID, RESPONSE_VALUE
PROFILE table: USER_ID, CATEGORY_Questions, YEAR, NUMBER_OF_ANSWERED, Number_OF_CORRECT, POINTS
The questions will be available to be answered by users for few hours. Every question has the same 3 choices for answers YES/NO/DEPENDS.
Noone knows the answer(even admin) before some time passes. In the meantime USERS can choose their response which is stored to RESPONSES table.
After some time I will manually add to mysql table the CORRECT_RESPONSE and mark the question as AVAILABLE="NO". So users can't answer this question anymore.
Then I want to automatically check users' RESPONSE_VALUE for each question(ID) that isn't AVAILABLE to see if they chose correct or not. And then add them points for every correct answer.
I made another table called Profile that stores Userid , category of questions, how many answered, how many were correct, timeperiod and how many points got for that category of questions. I do this in order to get statistics faster for every user on every category etc.
So after I "mark" a question as finished(AVAILABLE="NO") and I have submitted the CORRECT_RESPONSE what is the best way to check for users' correct RESPONSE_VALUEs and then update the profile table?
To add on to what Galen said you would then want to add a variable
$num_correct++; while looping through the query
update PROFILE SET Number_OF_CORRECT = $num_correct WHERE USER_ID=responses.user_id
Loop through the users and run these queries:
Get total questions answered
select responses.question_id from responses where responses.user_id=USER_ID
Get amount of correct answers
select responses.question_id from responses where responses.user_id=USER_ID and responses.question_id = questions.question_id and responses.response_value=questions.correct_response
EDIT
I'm assuming you have an admin interface of some sort. I would just add a button "Correct Answers" that runs the code to correct all the answers. If you dont have an admin interface the easiest way would be to put a single script in a protected folder that would run the corrections.

Categories