From: mark-nielsen Date: Sat, 8 Apr 2006 18:56:39 +0000 (+0000) Subject: Updated viewquestion to be more generic and moved it to its own function X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=fbe4d5ce51ead1e6833fa564b2eb3c10b1911306;p=moodle.git Updated viewquestion to be more generic and moved it to its own function --- diff --git a/mod/quiz/report/grading/report.php b/mod/quiz/report/grading/report.php index 4105cdc994..81043c0ffa 100644 --- a/mod/quiz/report/grading/report.php +++ b/mod/quiz/report/grading/report.php @@ -206,112 +206,7 @@ class quiz_report extends quiz_default_report { $this->view_questions($quiz, $course, $userids); break; case 'viewquestion': - // gonna use flexible_table (first time!) - $tablecolumns = array('picture', 'fullname', 'attempt'); - $tableheaders = array('', get_string('fullname'), get_string("attempts", "quiz")); - - $table = new flexible_table('mod-quiz-report-grading'); - - $table->define_columns($tablecolumns); - $table->define_headers($tableheaders); - $table->define_baseurl($CFG->wwwroot.'/mod/quiz/report.php?mode=grading&q='.$quiz->id.'&action=viewquestion&questionid='.$question->id); - - $table->sortable(true); - $table->initialbars(count($users)>20); // will show initialbars if there are more than 20 users - $table->pageable(true); - - $table->column_suppress('fullname'); - $table->column_suppress('picture'); - - $table->column_class('picture', 'picture'); - - // attributes in the table tag - $table->set_attribute('cellspacing', '0'); - $table->set_attribute('id', 'grading'); - $table->set_attribute('class', 'generaltable generalbox'); - $table->set_attribute('align', 'center'); - $table->set_attribute('width', '50%'); - - // get it ready! - $table->setup(); - - // this sql is a join of the attempts table and the user table. I do this so I can sort by user name and attempt number (not id) - $select = 'SELECT '.$db->Concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')).' AS userattemptid, qa.id AS attemptid, qa.uniqueid, qa.attempt, u.id AS userid, u.firstname, u.lastname, u.picture '; - $from = 'FROM '.$CFG->prefix.'user u LEFT JOIN '.$CFG->prefix.'quiz_attempts qa ON (u.id = qa.userid AND qa.quiz = '.$quiz->id.') '; - $where = 'WHERE u.id IN ('.implode(',', array_keys($users)).') '; - $where .= 'AND '.$db->IfNull('qa.attempt', '0').' != 0 '; - $where .= 'AND '.$db->IfNull('qa.timefinish', '0').' != 0 '; - $where .= 'AND preview = 0 '; // ignore previews - - if($table->get_sql_where()) { // forgot what this does - $where .= 'AND '.$table->get_sql_where(); - } - - // sorting of the table - if($sort = $table->get_sql_sort()) { - $sort = 'ORDER BY '.$sort; // seems like I would need to have u. or qa. infront of the ORDER BY attribues... but seems to work.. - } else { - // my default sort rule - $sort = 'ORDER BY u.firstname, u.lastname, qa.attempt ASC'; - } - - // set up the pagesize - $total = count_records_sql('SELECT COUNT(DISTINCT('.$db->Concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')).')) '.$from.$where); - $table->pagesize(10, $total); - - // this is for getting the correct records for a given page - if($table->get_page_start() !== '' && $table->get_page_size() !== '') { - $limit = ' '.sql_paging_limit($table->get_page_start(), $table->get_page_size()); - } else { - $limit = ''; - } - //$number = 1; - // get the attempts and process them - if ($attempts = get_records_sql($select.$from.$where.$sort.$limit)) { - foreach($attempts as $attempt) { - - $picture = print_user_picture($attempt->userid, $course->id, $attempt->picture, false, true); - - // link here... grades all for this student - $userlink = "id&questionid=$question->id&userid=$attempt->userid\">". - $attempt->firstname.' '.$attempt->lastname.''; - - // nab the state of the attempt to see if it is graded or not - // TODO: should be changed to use the event field - if (!$neweststate = get_record('question_sessions', 'attemptid', $attempt->uniqueid, 'questionid', $question->id)) { - error("Can not find newest states for attempt $attempt->uniqueid for question $questionid"); - } - - if (!$questionstate = get_record('question_essay_states', 'stateid', $neweststate->newest)) { - error('Could not find question state'); - } - // change the color of the link based on being graded or not - if (!$questionstate->graded) { - $style = 'style="color:#FF0000"'; // red - } else { - $style = 'style="color:#008000"'; // green - } - - // link for the attempt - $attemptlink = "id&questionid=$question->id&attemptid=$attempt->attemptid\">". // &number=$number - $question->name." attempt $attempt->attempt"; - - $table->add_data( array($picture, $userlink, $attemptlink) ); - } - //$number += $question->length; - } - - // grade all and "back" links - $links = "
id&questionid=$questionid&gradeall=1\">".get_string('gradeall', 'quiz').' | '. - "id&action=viewquestions\">".get_string('backtoquestionlist', 'quiz').'
'. - - // print everything here - print_heading($question->name); - echo $links; - echo '
'; - $table->print_html(); - echo '
'; - echo $links; + $this->view_question($quiz, $question, $users); break; default: error("Invalid Action"); @@ -368,13 +263,7 @@ class quiz_report extends quiz_default_report { $ungraded = 0; foreach ($attempts as $attempt) { // grab the state then check if it is graded - if (!$state = get_record_sql("SELECT state.id, state.event FROM - {$CFG->prefix}question_states state, {$CFG->prefix}question_sessions sess - WHERE sess.newest = state.id AND - sess.attemptid = $attempt->uniqueid AND - sess.questionid = $question->id")) { - error('Could not find question state'); - } + $state = $this->get_newest_state($question, $attempt); if (!question_state_is_graded($state)) { $ungraded++; @@ -388,6 +277,135 @@ class quiz_report extends quiz_default_report { print_heading(get_string('noattempts', 'quiz')); } } + + /** + * Prints a table with users and their attempts + * + * @return void + * @todo Add current grade to the table + * Finnish documenting + **/ + function view_question($quiz, $question, $users) { + global $CFG, $db; + + // gonna use flexible_table (first time!) + $tablecolumns = array('picture', 'fullname', 'attempt'); + $tableheaders = array('', get_string('fullname'), get_string("attempts", "quiz")); + + $table = new flexible_table('mod-quiz-report-grading'); + + $table->define_columns($tablecolumns); + $table->define_headers($tableheaders); + $table->define_baseurl($CFG->wwwroot.'/mod/quiz/report.php?mode=grading&q='.$quiz->id.'&action=viewquestion&questionid='.$question->id); + + $table->sortable(true); + $table->initialbars(count($users)>20); // will show initialbars if there are more than 20 users + $table->pageable(true); + + $table->column_suppress('fullname'); + $table->column_suppress('picture'); + + $table->column_class('picture', 'picture'); + + // attributes in the table tag + $table->set_attribute('cellspacing', '0'); + $table->set_attribute('id', 'grading'); + $table->set_attribute('class', 'generaltable generalbox'); + $table->set_attribute('align', 'center'); + $table->set_attribute('width', '50%'); + + // get it ready! + $table->setup(); + + // this sql is a join of the attempts table and the user table. I do this so I can sort by user name and attempt number (not id) + $select = 'SELECT '.$db->Concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')).' AS userattemptid, qa.id AS attemptid, qa.uniqueid, qa.attempt, qa.timestart, u.id AS userid, u.firstname, u.lastname, u.picture '; + $from = 'FROM '.$CFG->prefix.'user u LEFT JOIN '.$CFG->prefix.'quiz_attempts qa ON (u.id = qa.userid AND qa.quiz = '.$quiz->id.') '; + $where = 'WHERE u.id IN ('.implode(',', array_keys($users)).') '; + $where .= 'AND '.$db->IfNull('qa.attempt', '0').' != 0 '; + $where .= 'AND '.$db->IfNull('qa.timefinish', '0').' != 0 '; + $where .= 'AND preview = 0 '; // ignore previews + + if($table->get_sql_where()) { // forgot what this does + $where .= 'AND '.$table->get_sql_where(); + } + + // sorting of the table + if($sort = $table->get_sql_sort()) { + $sort = 'ORDER BY '.$sort; // seems like I would need to have u. or qa. infront of the ORDER BY attribues... but seems to work.. + } else { + // my default sort rule + $sort = 'ORDER BY u.firstname, u.lastname, qa.attempt ASC'; + } + + // set up the pagesize + $total = count_records_sql('SELECT COUNT(DISTINCT('.$db->Concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')).')) '.$from.$where); + $table->pagesize(10, $total); + + // this is for getting the correct records for a given page + if($table->get_page_start() !== '' && $table->get_page_size() !== '') { + $limit = ' '.sql_paging_limit($table->get_page_start(), $table->get_page_size()); + } else { + $limit = ''; + } + // get the attempts and process them + if ($attempts = get_records_sql($select.$from.$where.$sort.$limit)) { + foreach($attempts as $attempt) { + + $picture = print_user_picture($attempt->userid, $quiz->course, $attempt->picture, false, true); + + // link here... grades all for this student + $userlink = "id&questionid=$question->id&userid=$attempt->userid\">". + fullname($attempt, true).''; + + // grab the state then check if it is graded + $state = $this->get_newest_state($question, $attempt); + + if (!question_state_is_graded($state)) { + $style = 'class="manual-ungraded"'; + } else { + $style = 'class="manual-graded"'; + } + + // link for the attempt + $attemptlink = "id&questionid=$question->id&attemptid=$attempt->attemptid\">". + userdate($attempt->timestart, get_string('strftimedatetime')).''; + + $table->add_data( array($picture, $userlink, $attemptlink) ); + } + } + + // grade all and "back" links + $links = "
id&questionid=$question->id&gradeall=1\">".get_string('gradeall', 'quiz').' | '. + "id&action=viewquestions\">".get_string('backtoquestionlist', 'quiz').'
'. + + // print everything here + print_heading($question->name); + echo $links; + echo '
'; + $table->print_html(); + echo '
'; + echo $links; + } + + /** + * undocumented function + * + * @return void + * @author Mark Nielsen + **/ + function get_newest_state($question, $attempt) { + global $CFG; + + if (!$state = get_record_sql("SELECT state.id, state.event FROM + {$CFG->prefix}question_states state, {$CFG->prefix}question_sessions sess + WHERE sess.newest = state.id AND + sess.attemptid = $attempt->uniqueid AND + sess.questionid = $question->id")) { + error('Could not find question state'); + } + + return $state; + } }