diff --git a/lib/questionlib.php b/lib/questionlib.php index 712508227b3..88e5d347013 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -37,9 +37,14 @@ define('QUESTION_EVENTMANUALGRADE', '9'); // Grade was entered by teacher define('QUESTION_EVENTS_GRADED', QUESTION_EVENTGRADE.','. QUESTION_EVENTCLOSEANDGRADE.','. QUESTION_EVENTMANUALGRADE); -global $QUESTION_EVENTS_GRADED; -$QUESTION_EVENTS_GRADED = array(QUESTION_EVENTGRADE, QUESTION_EVENTCLOSEANDGRADE, - QUESTION_EVENTMANUALGRADE); + + +define('QUESTION_EVENTS_CLOSED', QUESTION_EVENTCLOSE.','. + QUESTION_EVENTCLOSEANDGRADE.','. + QUESTION_EVENTMANUALGRADE); + +define('QUESTION_EVENTS_CLOSED_OR_GRADED', QUESTION_EVENTGRADE.','. + QUESTION_EVENTS_CLOSED); /**#@-*/ @@ -1082,7 +1087,7 @@ function get_question_states(&$questions, $cmoptions, $attempt, $lastattemptid = * @return object the requested state. False on error. */ function question_load_specific_state($question, $cmoptions, $attempt, $stateid) { - global $DB, $QUESTION_EVENTS_GRADED; + global $DB; // Load specified states for the question. // sess.sumpenalty is probably wrong here shoul really be a sum of penalties from before the one we are asking for. $sql = 'SELECT st.*, sess.sumpenalty, sess.manualcomment, sess.flagged, sess.id as questionsessionid @@ -1099,7 +1104,6 @@ function question_load_specific_state($question, $cmoptions, $attempt, $stateid) restore_question_state($question, $state); // Load the most recent graded states for the questions before the specified one. - list($eventinsql, $params) = $DB->get_in_or_equal($QUESTION_EVENTS_GRADED); $sql = 'SELECT st.*, sess.sumpenalty, sess.manualcomment, sess.flagged, sess.id as questionsessionid FROM {question_states} st, {question_sessions} sess WHERE st.seq_number <= ? @@ -1107,10 +1111,9 @@ function question_load_specific_state($question, $cmoptions, $attempt, $stateid) AND sess.attemptid = st.attempt AND st.question = ? AND sess.questionid = st.question - AND st.event ' . $eventinsql . + AND st.event IN ('.QUESTION_EVENTS_GRADED.') '. 'ORDER BY st.seq_number DESC'; - $gradedstates = $DB->get_records_sql($sql, array_merge( - array($state->seq_number, $attempt->id, $question->id), $params), 0, 1); + $gradedstates = $DB->get_records_sql($sql, array($state->seq_number, $attempt->id, $question->id), 0, 1); if (empty($gradedstates)) { $state->last_graded = clone($state); } else { @@ -1240,8 +1243,11 @@ function save_question_session($question, $state) { * @param object $state */ function question_state_is_graded($state) { - $gradedevents = explode(',', QUESTION_EVENTS_GRADED); - return (in_array($state->event, $gradedevents)); + static $question_events_graded = array(); + if (!$question_events_graded){ + $question_events_graded = explode(',', QUESTION_EVENTS_GRADED); + } + return (in_array($state->event, $question_events_graded)); } /** @@ -1251,9 +1257,11 @@ function question_state_is_graded($state) { * @param object $state */ function question_state_is_closed($state) { - return ($state->event == QUESTION_EVENTCLOSE - or $state->event == QUESTION_EVENTCLOSEANDGRADE - or $state->event == QUESTION_EVENTMANUALGRADE); + static $question_events_closed = array(); + if (!$question_events_closed){ + $question_events_closed = explode(',', QUESTION_EVENTS_CLOSED); + } + return (in_array($state->event, $question_events_closed)); } diff --git a/lib/simpletest/testquestionlib.php b/lib/simpletest/testquestionlib.php new file mode 100644 index 00000000000..96a15e4980a --- /dev/null +++ b/lib/simpletest/testquestionlib.php @@ -0,0 +1,117 @@ +libdir . '/questionlib.php'); + +class questionlib_test extends MoodleUnitTestCase { + + + function setUp() { + } + + function tearDown() { + } + + function test_question_state_is_closed() { + $state = new object(); + $state->event = QUESTION_EVENTOPEN; + $this->assertFalse(question_state_is_closed($state)); + + $state->event = QUESTION_EVENTNAVIGATE; + $this->assertFalse(question_state_is_closed($state)); + + $state->event = QUESTION_EVENTSAVE; + $this->assertFalse(question_state_is_closed($state)); + + $state->event = QUESTION_EVENTGRADE; + $this->assertFalse(question_state_is_closed($state)); + + $state->event = QUESTION_EVENTDUPLICATE; + $this->assertFalse(question_state_is_closed($state)); + + $state->event = QUESTION_EVENTVALIDATE; + $this->assertFalse(question_state_is_closed($state)); + + $state->event = QUESTION_EVENTSUBMIT; + $this->assertFalse(question_state_is_closed($state)); + + $state->event = QUESTION_EVENTCLOSEANDGRADE; + $this->assertTrue(question_state_is_closed($state)); + + $state->event = QUESTION_EVENTCLOSE; + $this->assertTrue(question_state_is_closed($state)); + + $state->event = QUESTION_EVENTMANUALGRADE; + $this->assertTrue(question_state_is_closed($state)); + + } + function test_question_state_is_graded() { + $state = new object(); + $state->event = QUESTION_EVENTOPEN; + $this->assertFalse(question_state_is_graded($state)); + + $state->event = QUESTION_EVENTNAVIGATE; + $this->assertFalse(question_state_is_graded($state)); + + $state->event = QUESTION_EVENTSAVE; + $this->assertFalse(question_state_is_graded($state)); + + $state->event = QUESTION_EVENTDUPLICATE; + $this->assertFalse(question_state_is_graded($state)); + + $state->event = QUESTION_EVENTVALIDATE; + $this->assertFalse(question_state_is_graded($state)); + + $state->event = QUESTION_EVENTSUBMIT; + $this->assertFalse(question_state_is_graded($state)); + + $state->event = QUESTION_EVENTCLOSE; + $this->assertFalse(question_state_is_graded($state)); + + $state->event = QUESTION_EVENTCLOSEANDGRADE; + $this->assertTrue(question_state_is_graded($state)); + + $state->event = QUESTION_EVENTMANUALGRADE; + $this->assertTrue(question_state_is_graded($state)); + + $state->event = QUESTION_EVENTGRADE; + $this->assertTrue(question_state_is_graded($state)); + + } +} + +?> diff --git a/mod/quiz/report/grading/report.php b/mod/quiz/report/grading/report.php index 1b98749895c..2b55ea6c936 100644 --- a/mod/quiz/report/grading/report.php +++ b/mod/quiz/report/grading/report.php @@ -160,8 +160,6 @@ class quiz_grading_report extends quiz_default_report { groups_print_activity_menu($this->cm, $this->viewurl->out(false, array('userid'=>0, 'attemptid'=>0))); } - echo '
' . quiz_num_attempt_summary($quiz, $cm, true, $currentgroup) . '
'; - if(empty($this->users)) { if ($currentgroup){ notify(get_string('nostudentsingroup')); @@ -438,17 +436,16 @@ class quiz_grading_report extends quiz_default_report { '{quiz_attempts} qa '; $params = array(); - if (($wantstateevent|| $gradenextungraded || $gradeungraded) && $questionid){ - $from .= "LEFT JOIN {question_sessions} qns " . - "ON (qns.attemptid = qa.uniqueid AND qns.questionid = ?) "; - $params[] = $questionid; - $from .= "LEFT JOIN {question_states} qs " . - "ON (qs.id = qns.newgraded AND qs.question = ?) "; - $params[] = $questionid; - } + $from .= "LEFT JOIN {question_sessions} qns " . + "ON (qns.attemptid = qa.uniqueid AND qns.questionid = ?) "; + $params[] = $questionid; + $from .= "LEFT JOIN {question_states} qs " . + "ON (qs.id = qns.newest AND qs.question = ?) "; + $params[] = $questionid; + list($usql, $u_params) = $DB->get_in_or_equal(array_keys($this->users)); if ($gradenextungraded || $gradeungraded) { // get ungraded attempts - $where = "WHERE u.id $usql AND qs.event NOT IN (".QUESTION_EVENTS_GRADED.') '; + $where = "WHERE u.id $usql AND qs.event IN (".QUESTION_EVENTS_GRADED.")"; $params = array_merge($params, $u_params); } else if ($userid) { // get all the attempts for a specific user $where = 'WHERE u.id=?'; @@ -460,6 +457,8 @@ class quiz_grading_report extends quiz_default_report { $where = "WHERE u.id $usql "; $params = array_merge($params, $u_params); } + + $where .= ' AND qs.event IN ('.QUESTION_EVENTS_CLOSED_OR_GRADED.')'; $where .= ' AND u.id = qa.userid AND qa.quiz = ?'; $params[] = $quizid; diff --git a/mod/quiz/report/reportlib.php b/mod/quiz/report/reportlib.php index 4d99e07b6a6..ba77bd674dc 100644 --- a/mod/quiz/report/reportlib.php +++ b/mod/quiz/report/reportlib.php @@ -26,7 +26,7 @@ function quiz_get_newgraded_states($attemptidssql, $idxattemptq = true, $fields= "{question_sessions} qns, " . "{question_states} qs " . "WHERE qns.attemptid $usql AND " . - "qns.newgraded = qs.id"; + "qns.newest = qs.id"; $gradedstates = $DB->get_records_sql($gradedstatesql, $params); } else if ($attemptidssql && is_object($attemptidssql)){ $gradedstatesql = "SELECT $fields FROM " . @@ -35,7 +35,7 @@ function quiz_get_newgraded_states($attemptidssql, $idxattemptq = true, $fields= "{question_states} qs " . "WHERE qns.attemptid = qa.uniqueid AND " . $attemptidssql->where." AND ". - "qns.newgraded = qs.id"; + "qns.newest = qs.id"; $gradedstates = $DB->get_records_sql($gradedstatesql, $attemptidssql->params); } else { return array(); @@ -135,10 +135,10 @@ function quiz_get_total_qas_graded_and_ungraded($quiz, $questionids, $userids){ $params = array($quiz->id); list($u_sql, $u_params) = $DB->get_in_or_equal($userids); list($q_sql, $q_params) = $DB->get_in_or_equal($questionids); - + $params = array_merge($params, $u_params, $q_params); $sql = "SELECT qs.question, COUNT(1) AS totalattempts, - SUM(CASE WHEN (qs.event IN (".QUESTION_EVENTS_GRADED.")) THEN 1 ELSE 0 END) AS gradedattempts + SUM(CASE WHEN (qs.event IN(".QUESTION_EVENTS_GRADED.")) THEN 1 ELSE 0 END) AS gradedattempts FROM {quiz_attempts} qa, {question_sessions} qns, @@ -147,7 +147,8 @@ function quiz_get_total_qas_graded_and_ungraded($quiz, $questionids, $userids){ qa.quiz = ? AND qa.userid $u_sql AND qns.attemptid = qa.uniqueid AND - qns.newgraded = qs.id AND + qns.newest = qs.id AND + qs.event IN (".QUESTION_EVENTS_CLOSED_OR_GRADED.") AND qs.question $q_sql GROUP BY qs.question"; return $DB->get_records_sql($sql, $params);