get_records_sql problem - only one record returned.

get_records_sql problem - only one record returned.

by Matt Gibson -
Number of replies: 4
I've written this query to retrieve all the essay questions that need grading, but oddly, it only gives me 1 record back, whereas there are actually 3 to be had.

I know this, because if I insert echo $sql; I get the SQL pasted below it, which when I put it through PHPMyadmin gives me 3 records. Is there a limit to how complex I can make my queries before the abstraction layer tanks, or have I missed/confused something?

 $sql = "
 SELECT 
 qsess.questionid, qst.id as stateid, qa.userid, qz.id, qz.intro as description, qz.name, c.id as cmid
 FROM
 {$CFG->prefix}quiz qz
 INNER JOIN {$CFG->prefix}course_modules c
 ON qz.id = c.instance
 INNER JOIN
 {$CFG->prefix}quiz_attempts qa
 ON 
 qz.id = qa.quiz
 INNER JOIN
 {$CFG->prefix}question_sessions qsess 
 ON
 qsess.attemptid = qa.id
 INNER JOIN 
 {$CFG->prefix}question_states qst
 ON 
 qsess.newest = qst.id 
 INNER JOIN {$CFG->prefix}question q
 ON 
 qsess.questionid = q.id 
 WHERE
 qa.userid 
 IN ($this->student_ids) 
 AND qa.timefinish > 0 
 AND qa.preview = 0 
 AND c.module = {$this->modules['quiz']->id}
 AND c.visible = 1
 AND qz.course = {$this->id}
 AND q.qtype = 'essay'
 AND qst.event NOT IN (
 ".QUESTION_EVENTGRADE.", 
 ".QUESTION_EVENTCLOSEANDGRADE.", 
 ".QUESTION_EVENTMANUALGRADE.") 
 ORDER BY q.id
 ";
 //echo $sql;
 $quiz_submissions = get_records_sql($sql);
The SQL from the echo:
 SELECT 
 qsess.questionid, qst.id as stateid, qa.userid, qz.id, qz.intro as description, qz.name, c.id as cmid
 FROM
 mdl_quiz qz
 INNER JOIN mdl_course_modules c
 ON qz.id = c.instance
 INNER JOIN
 mdl_quiz_attempts qa
 ON 
 qz.id = qa.quiz
 INNER JOIN
 mdl_question_sessions qsess 
 ON
 qsess.attemptid = qa.id
 INNER JOIN 
 mdl_question_states qst
 ON 
 qsess.newest = qst.id 
 INNER JOIN mdl_question q
 ON 
 qsess.questionid = q.id 
 WHERE
 qa.userid 
 IN (698,3456,3457,3458,3460,3459) 
 AND qa.timefinish > 0 
 AND qa.preview = 0 
 AND c.module = 13
 AND c.visible = 1
 AND qz.course = 236
 AND q.qtype = 'essay'
 AND qst.event NOT IN (
 3, 
 6, 
 9) 
 ORDER BY q.id
 
 



Average of ratings: -
In reply to Matt Gibson

Re: get_records_sql problem - only one record returned.

by Adam Zapletal -
The first column of your select query must be unique. This is mentioned in the documentation of get_records_sql.

In this case, is mdl_question_sessions.questionid always unique?
In reply to Adam Zapletal

Re: get_records_sql problem - only one record returned.

by Matt Gibson -
Doh! Can't believe I missed that.

Thanks Adam, it works now that I've included qsess.id as the first field.
In reply to Matt Gibson

Re: get_records_sql problem - only one record returned.

by Dan Poltawski -
Matt are you doing this on 1.9? I

f so, turn debugging up to DEBUG_DEVELOPER - Tim helpfully put in a warning to help stop everyone banging their heads against the wall a while ago: MDL-12438
In reply to Dan Poltawski

Re: get_records_sql problem - only one record returned.

by Matt Gibson -
Yes, I am. I already had it turned right up though and there was no sign of the warning.I haven't updated my install that recently, so maybe I missed the fix.