I need some advice on
I created a web app where a user can assign a form to multiple recipients simultaneously so that one assignment inserts multiple rows (one row per recipient) into the form_assignments
table like so:
foreach ($recipients as $row) {
$query = "INSERT INTO form_assignments
(sender_id, recipient_id, form_id, due_date, priority_id, comment, completed)
VALUES
('{$sender_id}', " . $row['id'] . ", '{$assigned_form_id}', '2014-04-30 00:00:00', '{$priority_id}', '{$comment}', '{$completed}')";
In the example above, $recipients
is an array with user ids referenced by $row['id']
.
So, one assignment can generate 100 of these rows (for 100 recipients), for example, and each recipient can edit his individual row as "completed". This is good so far.
The problem is, if I want to generate a list of assignments for the user that assigned them I want him to see only one row for a multiple-user assignment (not one row for each recipient he sent the assignment to).
I'd love to be able to show the user a table like:
You assigned: form 1 To: 50 users Due on: April 25, 2014 Users Completed: 10 View List of Users
And each assignee would see a table like:
User 1 assigned you the following form: form 1 Due on: April 25, 2014, Click to Mark as Completed
I'm not sure how to go about this. If anyone could point me in the right direction I would greatly appreciate it.
If i do understand you correctly, you want to present to the person that assigns, a list of assignments, grouped by assignment and date. For each Assignment/Date Result, you alos want to present how many Users already completed the Assignment right.
So you want to Select from assignments and Group your Result by sender_id
, form_id
and due_date
first:
SELECT sender_id, form_id, due_date
FROM assignments as groupedAssignments
GROUP BY
groupedAssignments.sender_id,
groupedAssignments.form_id,
groupedAssignments.due_date
Now you also want to JOIN
the assignments to get th count for how many students already finished this assignment. That's why i added the alias 'groupedAssignments'. So add a JOIN
After your FROM
JOIN assignments as finishedAssignments ON (
groupedAssignments.sender_id = finishedAssignments.sender_id
AND groupedAssignments.form_id = finishedAssignments.form_id
AND groupedAssignments.due_date = finishedAssignments.due_date
AND finishedAssignments.completed = TRUE )
Now you can add a Count on the finished assignments giving you the full Query:
SELECT
groupedAssignments.sender_id,
groupedAssignments.form_id,
groupedAssignments.due_date,
COUNT(finishedAssignments.completed) as finishedStudents
FROM assignments as groupedAssignments
JOIN assignments as finishedAssignments ON (
groupedAssignments.sender_id = finishedAssignments.sender_id
AND groupedAssignments.form_id = finishedAssignments.form_id
AND groupedAssignments.due_date = finishedAssignments.due_date
AND finishedAssignments.completed = TRUE )
GROUP BY
finishedAssignments.sender_id,
finishedAssignments.form_id,
finishedAssignments.due_date
Obviously i didn't test this and it might not work for you, but it should lead you in the right direction. Read up on GROUP BY
and JOIN
.
You need to decide when rows count as being part of the same assignment (this may mean adding this info to the table). If, for example, you decide that this can be done by matching the sender_id, form_id and due_date then you can group by these fields:
SELECT sender_id,form_id,due_date,COUNT(recipient_id)
FROM assignments
GROUP BY sender_id,,form_id,due_date
That will give a list of assignments done by the user as well as the count of recipients. Of course this may need tweaked or changed depending on how you decide to match assignments.