Rails form with dynamic test bank - mysql

I'm looking to create a question bank where the test creator can specify the number of question they would like to see for each category of the form.
Currently I loop through all of the question to build the form, and I'm not sure how to simply pull out only x number of questions per category.
#questions = AuditQuestions.where(question_active: 'Y', audit_forms_id: session[:audit_forms_id]).order(question_order_number: :asc)
Table structure:
id audit_forms_id question_category question_title question_text question_order_number question_active

Related

How can I ask questions randomly to different user in django?

I want to create a list of questions and from it I want to ask random 5 questions
to each logged in user.How can i do it. where can I create a list of quetions
and options.How can I render questions and answer to html page ?
I tried to create a db table and used it to get values in html template
But it did not work.
class Quetions(models.Model):
quetion =models.CharField(max_length=100,default='')
option1=models.CharField(max_length=20,default='')
option2=models.CharField(max_length=20,default='')
option3=models.CharField(max_length=20,default='')
option4 =models.CharField(max_length=20, default='')
While fetching the question, you can do something like
pks = Quetions.objects.values_list('pk', flat=True)
random_idx = random.sample(range(0, len(pks)-1), 5)
random_questions = Quetions.objects.filter(pk__in=random_idx)

Joining 2 tables together and using the where function based on a separate mysql query

I am building a training platform for work. I have created the requirements for a user to be trained based on a role given to them. If that role is aligned to a document it will sit against the user. I have managed to get most of the way but am struglling on the best way to finish the where statement within mysqli.
tbldocfiles is a list of my files. I am looking at docid (could be multiple files associated to the document)
tbltrainingaccess sets the roles (driver, warehouseman, customer services) and shows which role (by id) is associated to the document in docfiles.
tblusertraining is the list of users and what role they have associated to them. (driver, warehouseman, customer services).
I am listing the documents associated to the user so have thought the following is the best way:
Look at the user and how many roles he/she is allocated
Look at the roles returned in point 1 (where function)
Identify and match the documents that have the same roles as the user (Join function)
create the list, then look at the unique values for docid. (distinct value)
Example User Bri has the driver and warehouseman role.
There are 5 documents in the db, 3 of them are associated to the driver role (docid 1,2,3) and 2 of them are associated to the warehouseman role (docid 2,4) the 5th document is associayted to customerservice.
My query should do this:
List all documents associated to the roles, that are associated to the user Bri
1
2
3
2
4
Now select unique values (using docid) from the above list:
1,2,3,4.
So my answer will be a used as a count function at the end using mysql_fetch_rows
SELECT DISTINCT tbldocfiles.docid FROM tbldocfiles LEFT JOIN tbltrainingaccess ON (tbldocfiles.docid = tbltrainingaccess.docid) where groupid='1' or groupid='9'
The above code works. but i've got myself confused.
The where statement needs to be the result of a query similar to :
select * from tblusertrainingrole where userid='1' (1 will be a variable based on page selection)
the result in this would be 1, 9 which are the groupid results.
Basically any help would be appreciated! I am sure it will be simple but have burnt myself out on this for a while and most answers in here helped with joining but not the where statement (that I could find)
Thank you in advance everyone!
You can do a select statement in the where. Since it is an or statement you can use in for the results. Please replace * with the column name for the value you need. Should look like
where groupid in (select * from tblusertrainingrole where userid = '1')

How to store and display with HTML m:n array(two dimensional table)

How to organize information(in MVC model) in order to represent this real example:
Survey - can have multiple questions and multiple people who will answer them
Question - only single answer - yes or no
PersonAnswer - the one that answer the question
Sample display:
My implementation so far is:
DB
Survey - Question - one to many
Question - PersonAnswer - one to many
controller code: - pseudo code
Survey.FindAllQuestions
Loop per each Question and find all answers
result is map - Survey = [Question1: AnswerList1, Question2: AnswerList2]
In the HTML looping over the map I'm able to diplay the information
Problems of my approach:
the header - people names should be calculated
sort of the lists is needed otherwise the answers will be mixed
How to manage properly if the person didn't answer a question - should I store it in the database?
Update: I'm using java and mysql.
My approach to this question would first deal with the database:
In my case this is how i would do it:
Have a table of
users->represents db users
surveyitems->with item(questions) and id(probaby autoincrement integer pkey),
surveycheks->representing the users and their checks having({
id(pkey),
checkid(foreign_key ->to surveyitems table id)
yeschecked(representing the yes)
nochecked(representi)ng the nos
})
Then in your views eg when using yii php framework
use gii tool to generate models and crud //here you can use your own
logic of saving data to db
On your survey form
1. load all surveys from table surveyitems with id and items()// display item
2. Have radio buttons with values of id(from survey items);
3.. when saving loop through all checked yes or nos and store them in surveycheks
I hope you get the idea

Check latest id and add 1 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Since I got several "projects" that should contain many questions each, I have a question-page where I fill a videolink, four answers and four drow-down lists where the user is able to set points for every answer.
However, in the database I have two tables.
When I fill and execute my question page I have made so every answer gets an id. In the table "answer_det", Pid, Aid, answer and points is being set. This is how it looks like:
The "question" table when I insert the first question for the first project(Pid=1) :
What I want to do now is to also set the qid(question-id). I'm not sure how to do it, but I think that I should have a code that checks the maximum qid of the pid and add 1 to it so every new question for the same project get a new qid. If the pid isn't in the table, then the qid should get the value "1".
So if you look at the first picture, the qid should be 1 on every showed row since all the four answers belongs to the same question, which is the first one for the project with pid=1. So if I would like to add a question to the same project, it should look the same but with the qid=2 and so on. If I then add a new(first) question for the project 2, the qid should begin on 1 and so on. Then, if i would like to add a new question again for the first project, the code should check that the maximum qid is 2 where pid is 1, and then insert a new question with answers but with qid=3.
It should work the same way on the table "question", which you see on the second picture. When the first question is created, I want the first question for the first project(the one with pid=1) to also have qid=1 and the link that I filled. The second question for the project with pid=1 should then get qid=2. If i add a first question for a new project, it should then be pid=2 and qid=1.
This is the code that I have now, and nothing of it inserts anything in the qid in neither of the two tables.
I suppose you have a predefined set of projects:
Project 1> Science
Project 2> Maths
Project 3> History.....and so on.
I suggest you create a separate table for projects that would have projectID and projectName columns.
Now in your question-page, have these projects in a dropdown(select option) format with projectID as the option value and projectName as the dropdown select name.
Now you have a select option for project name, an input field for question_link and other select options for answers and their weight-age. Now when you insert this form (this question) try coding the following logic (I will just try to write in pseudo-code manner)
//first check if this project already has questions in the question table.
So do something like
SELECT* FROM question WHERE pid = $post['projectID'] ORDER BY qid DESC
// or you could do SELECT TOP 1
// CASE1: if this project never had any questions, the select would return null.
//Hence this is a first question for this project. Then simply write an insert query that
//would insert $post['projectID'] to pid and 1 in qid (since this is the first question).
//Also, insert a series of rows in the answer_det table that will have $post['projectID']
//to pid and 1 for qid and the answer and points as submitted.
// CASE2: if this project already has 1 or more questions, your select query from above
//will return some rows and if you pick $row[0]['qid'], this would be the highest number
//value for question (qid) as we selected using ORDER BY qid DESC. Now, simply do inserts
//in both the tables like you would do as above but this time, in the qid field in both
//the tables, instead of inserting 1, you would insert qid => $row[0]['qid'] + 1.
//You wouldn't need to know what the qid was, all you need is to get the last qid and add
//1 to it.
//Also, you can create a separate page to add the projectName as you feel to add new
//projects and insert this into the project_table and this will begin to show up in the
//select options when you are adding new questions.

How can I see full order details for each time a certain coupon code was used in magento?

We are trying to collect data on each person that used a certain coupon code "NEWCUSTOMER". We are trying to get the order details including their name, email address, and what they ordered.
Is the rule_id connected to an order in any way in the database? The magento databases don't seem to be all that friendly when you are trying to write your own mySQL statement to figure this information out.
Thanks!
This is similar to your previous question which i have also answered for you: Find the name and email address of non-members who used coupon code in Magento
The coupon code used on an order is actually a property of the order: coupon_code
It sounds from your question that you are directly querying the db, if so then you are looking for the coupon_code field in the sales_flat_order table.
Here is the sql:
SELECT `customer_firstname`, `customer_lastname`, `customer_email` FROM `sales_flat_order` WHERE `coupon_code` = 'your_awesome_coupon_code' AND `customer_group_id` = 0
Or, via magento...
$orderCollection = Mage::getModel('sales/order')->getCollection()
->addAttributeToSelect('customer_firstname')
->addAttributeToSelect('customer_lastname')
->addAttributeToSelect('customer_email')
->addAttributeToSelect('customer_group_id')
->addAttributeToSelect('coupon_code')
->addAttributeToFilter('customer_group_id', Mage_Customer_Model_Group::NOT_LOGGED_IN_ID)
->addAttributeToFilter('coupon_code', 'NEWCUSTOMER');