Index: mod/quiz/report/overview/report.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/quiz/report/overview/report.php,v
retrieving revision 1.98.2.30
diff -u -r1.98.2.30 report.php
--- mod/quiz/report/overview/report.php 30 May 2008 12:22:20 -0000 1.98.2.30
+++ mod/quiz/report/overview/report.php 3 Jun 2008 09:06:30 -0000
@@ -10,6 +10,7 @@
require_once($CFG->libdir.'/tablelib.php');
require_once($CFG->dirroot.'/mod/quiz/report/overview/overviewsettings_form.php');
+require_once($CFG->dirroot.'/mod/quiz/report/overview/overview_table.php');
class quiz_report extends quiz_default_report {
@@ -17,18 +18,19 @@
* Display the report.
*/
function display($quiz, $cm, $course) {
- global $CFG, $db;
- // Define some strings
- $strreallydel = addslashes(get_string('deleteattemptcheck','quiz'));
- $strtimeformat = get_string('strftimedatetime');
- $strreviewquestion = get_string('reviewresponse', 'quiz');
+ global $CFG, $COURSE;
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
- // Only print headers if not asked to download data
- if (!$download = optional_param('download', NULL)) {
- $this->print_header_and_tabs($cm, $course, $quiz, "overview");
- }
+ // Work out some display options - whether there is feedback, and whether scores should be shown.
+ $hasfeedback = quiz_has_feedback($quiz->id) && $quiz->grade > 1.e-7 && $quiz->sumgrades > 1.e-7;
+ $fakeattempt = new stdClass();
+ $fakeattempt->preview = false;
+ $fakeattempt->timefinish = $quiz->timeopen;
+ $reviewoptions = quiz_get_reviewoptions($quiz, $fakeattempt, $context);
+ $showgrades = $quiz->grade && $quiz->sumgrades && $reviewoptions->scores;
+
+ $download = optional_param('download', '', PARAM_ALPHA);
if($attemptids = optional_param('attemptid', array(), PARAM_INT)) {
//attempts need to be deleted
@@ -44,13 +46,6 @@
//twice.
}
- // Work out some display options - whether there is feedback, and whether scores should be shown.
- $hasfeedback = quiz_has_feedback($quiz->id) && $quiz->grade > 1.e-7 && $quiz->sumgrades > 1.e-7;
- $fakeattempt = new stdClass();
- $fakeattempt->preview = false;
- $fakeattempt->timefinish = $quiz->timeopen;
- $reviewoptions = quiz_get_reviewoptions($quiz, $fakeattempt, $context);
- $showgrades = $quiz->grade && $quiz->sumgrades && $reviewoptions->scores;
$pageoptions = array();
$pageoptions['id'] = $cm->id;
@@ -59,7 +54,7 @@
$reporturl = new moodle_url($CFG->wwwroot.'/mod/quiz/report.php', $pageoptions);
$qmsubselect = quiz_report_qm_filter_subselect($quiz);
- $mform = new mod_quiz_report_overview_settings($reporturl, compact('qmsubselect', 'quiz'));
+ $mform = new mod_quiz_report_overview_settings($reporturl, array('qmsubselect'=> $qmsubselect, 'quiz'=>$quiz));
if ($fromform = $mform->get_data()){
$attemptsmode = $fromform->attemptsmode;
if ($qmsubselect){
@@ -95,19 +90,43 @@
$displayoptions = array();
$displayoptions['attemptsmode'] = $attemptsmode;
$displayoptions['qmfilter'] = $qmfilter;
- $reporturlwithdisplayoptions = new moodle_url($CFG->wwwroot.'/mod/quiz/report.php', $pageoptions + $displayoptions);
/// find out current groups mode
$currentgroup = groups_get_activity_group($cm, true);
+ //work out the sql for this table.
+ $students = join(',',array_keys(get_users_by_capability($context, 'mod/quiz:attempt','','','','','','',false)));
+ if (empty($currentgroup)) {
+ // all users who can attempt quizzes
+ $groupstudents = '';
+ $allowed = $students;
+ } else {
+ // all users who can attempt quizzes and who are in the currently selected group
+ $groupstudents = join(',',array_keys(get_users_by_capability($context, 'mod/quiz:attempt','','','','',$currentgroup,'',false)));
+ $allowed = $groupstudents;
+ }
+ if ($detailedmarks) {
+ $questions = quiz_report_load_questions($quiz);
+ } else {
+ $questions = array();
+ }
+ $table = new quiz_report_overview_table($quiz , $qmsubselect, $groupstudents,
+ $students, $detailedmarks, $questions, $candelete, $reporturl, $displayoptions);
+ $table->is_downloading($download, get_string('reportoverview','quiz'),
+ "$COURSE->shortname ".format_string($quiz->name,true));
+ if (!$table->is_downloading()) {
+ // Only print headers if not asked to download data
+ $this->print_header_and_tabs($cm, $course, $quiz, "overview");
+ }
+
if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
- if (!$download) {
- groups_print_activity_menu($cm, $reporturlwithdisplayoptions->out());
+ if (!$table->is_downloading()) {
+ groups_print_activity_menu($cm, $reporturl->out(false, $displayoptions));
}
}
// Print information on the number of existing attempts
- if (!$download) { //do not print notices when downloading
+ if (!$table->is_downloading()) { //do not print notices when downloading
if ($strattemptnum = quiz_num_attempt_summary($quiz, $cm, false, $currentgroup)) {
echo '
' . $strattemptnum . '
';
}
@@ -115,35 +134,86 @@
// Print information on the grading method and whether we are displaying
//
- if (!$download) { //do not print notices when downloading
+ if (!$table->is_downloading()) { //do not print notices when downloading
if ($strattempthighlight = quiz_report_highlighting_grading_method($quiz, $qmsubselect, $qmfilter)) {
echo '
' . $strattempthighlight . '
';
}
}
+
+
+ $showgrades = $quiz->grade && $quiz->sumgrades && $reviewoptions->scores;
+ $hasfeedback = quiz_has_feedback($quiz->id) && $quiz->grade > 1.e-7 && $quiz->sumgrades > 1.e-7;
+
- // Now check if asked download of data
- if ($download) {
- $filename = clean_filename("$course->shortname ".format_string($quiz->name,true));
+ // Construct the SQL
+ $fields = sql_concat('u.id', '\'#\'', 'COALESCE(qa.attempt, \'0\')').' AS uniqueid, '.
+ ($qmsubselect?$qmsubselect.' AS gradedattempt, ':'').
+ 'qa.uniqueid AS attemptuniqueid, qa.id AS attempt, u.id AS userid, u.idnumber, u.firstname, u.lastname, u.picture, '.
+ 'qa.sumgrades, qa.timefinish, qa.timestart, qa.timefinish - qa.timestart AS duration ';
+
+ // This part is the same for all cases - join users and quiz_attempts tables
+ $from = $CFG->prefix.'user u ';
+ $from .= 'LEFT JOIN '.$CFG->prefix.'quiz_attempts qa ON qa.userid = u.id AND qa.quiz = '.$quiz->id;
+ if ($qmsubselect && $qmfilter){
+ $from .= ' AND '.$qmsubselect;
}
+ switch ($attemptsmode){
+ case QUIZ_REPORT_ATTEMPTS_ALL:
+ // Show all attempts, including students who are no longer in the course
+ $where = 'qa.id IS NOT NULL AND qa.preview = 0';
+ break;
+ case QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH:
+ // Show only students with attempts
+ $where = 'u.id IN (' .$allowed. ') AND qa.preview = 0 AND qa.id IS NOT NULL';
+ break;
+ case QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH_NO:
+ // Show only students without attempts
+ $where = 'u.id IN (' .$allowed. ') AND qa.id IS NULL';
+ break;
+ case QUIZ_REPORT_ATTEMPTS_ALL_STUDENTS:
+ // Show all students with or without attempts
+ $where = 'u.id IN (' .$allowed. ') AND (qa.preview = 0 OR qa.preview IS NULL)';
+ break;
+ }
+ $table->set_count_sql("SELECT COUNT(1) FROM $from WHERE $where");
+ if ($detailedmarks) {
+ // we want to display marks for all questions
+ // Add extra sql to get question grades
+ $from .= ' ';
+ // we want to display marks for all questions
+ foreach (array_keys($questions) as $qid) {
+ $fields .= ", qs$qid.grade AS qsgrade$qid, qs$qid.event AS qsevent$qid, qs$qid.id AS qsid$qid";
+ $from .= "LEFT JOIN {$CFG->prefix}question_sessions qns$qid ON qns$qid.attemptid = qa.uniqueid AND qns$qid.questionid = $qid ";
+ $from .= "LEFT JOIN {$CFG->prefix}question_states qs$qid ON qs$qid.id = qns$qid.newgraded ";
+ }
+ }
+
+ $table->set_sql($fields, $from, $where);
// Define table columns
$columns = array();
$headers = array();
-
-
- if (!$download && $candelete) {
+
+
+ if (!$table->is_downloading() && $candelete) {
$columns[]= 'checkbox';
$headers[]= NULL;
}
- if (!$download && $CFG->grade_report_showuserimage) {
+ if (!$table->is_downloading() && $CFG->grade_report_showuserimage) {
$columns[]= 'picture';
$headers[]= '';
}
-
- $columns[]= 'fullname';
- $headers[]= get_string('name');
-
+ if (!$table->is_downloading()){
+ $columns[]= 'fullname';
+ $headers[]= get_string('name');
+ } else {
+ $columns[]= 'lastname';
+ $headers[]= get_string('lastname');
+ $columns[]= 'firstname';
+ $headers[]= get_string('firstname');
+ }
+
if ($CFG->grade_report_showuseridnumber) {
$columns[]= 'idnumber';
$headers[]= get_string('idnumber');
@@ -154,470 +224,67 @@
$columns[]= 'timefinish';
$headers[]= get_string('timecompleted','quiz');
-
+
$columns[]= 'duration';
$headers[]= get_string('attemptduration', 'quiz');
- if ($showgrades) {
- $columns[] = 'sumgrades';
- $headers[] = get_string('grade', 'quiz').'/'.$quiz->grade;
- }
-
if ($detailedmarks) {
- // we want to display marks for all questions
- $questions = quiz_report_load_questions($quiz);
foreach ($questions as $id => $question) {
// Ignore questions of zero length
$columns[] = 'qsgrade'.$id;
$headers[] = '#'.$question->number;
- }
+ }
}
-
+
+ if ($showgrades) {
+ $columns[] = 'sumgrades';
+ $headers[] = get_string('grade', 'quiz').'/'.$quiz->grade;
+ }
+
if ($hasfeedback) {
$columns[] = 'feedbacktext';
$headers[] = get_string('feedback', 'quiz');
- }
-
- if (!$download) {
- // Set up the table
-
- $table = new flexible_table('mod-quiz-report-overview-report');
-
- $table->define_columns($columns);
- $table->define_headers($headers);
- $table->define_baseurl($reporturlwithdisplayoptions->out());
-
- $table->sortable(true);
- $table->collapsible(true);
-
- $table->column_suppress('picture');
- $table->column_suppress('fullname');
-
- $table->no_sorting('feedbacktext');
-
- $table->column_class('picture', 'picture');
- $table->column_class('fullname', 'bold');
- $table->column_class('sumgrades', 'bold');
-
- $table->set_attribute('cellspacing', '0');
- $table->set_attribute('id', 'attempts');
- $table->set_attribute('class', 'generaltable generalbox');
-
- // Start working -- this is necessary as soon as the niceties are over
- $table->setup();
- } else if ($download =='ODS') {
- require_once("$CFG->libdir/odslib.class.php");
-
- $filename .= ".ods";
- // Creating a workbook
- $workbook = new MoodleODSWorkbook("-");
- // Sending HTTP headers
- $workbook->send($filename);
- // Creating the first worksheet
- $sheettitle = get_string('reportoverview','quiz');
- $myxls =& $workbook->add_worksheet($sheettitle);
- // format types
- $format =& $workbook->add_format();
- $format->set_bold(0);
- $formatbc =& $workbook->add_format();
- $formatbc->set_bold(1);
- $formatbc->set_align('center');
- $formatb =& $workbook->add_format();
- $formatb->set_bold(1);
- $formaty =& $workbook->add_format();
- $formaty->set_bg_color('yellow');
- $formatc =& $workbook->add_format();
- $formatc->set_align('center');
- $formatr =& $workbook->add_format();
- $formatr->set_bold(1);
- $formatr->set_color('red');
- $formatr->set_align('center');
- $formatg =& $workbook->add_format();
- $formatg->set_bold(1);
- $formatg->set_color('green');
- $formatg->set_align('center');
- // Here starts workshhet headers
-
- $colnum = 0;
- foreach ($headers as $item) {
- $myxls->write(0,$colnum,$item,$formatbc);
- $colnum++;
- }
- $rownum=1;
- } else if ($download =='Excel') {
- require_once("$CFG->libdir/excellib.class.php");
-
- $filename .= ".xls";
- // Creating a workbook
- $workbook = new MoodleExcelWorkbook("-");
- // Sending HTTP headers
- $workbook->send($filename);
- // Creating the first worksheet
- $sheettitle = get_string('reportoverview','quiz');
- $myxls =& $workbook->add_worksheet($sheettitle);
- // format types
- $format =& $workbook->add_format();
- $format->set_bold(0);
- $formatbc =& $workbook->add_format();
- $formatbc->set_bold(1);
- $formatbc->set_align('center');
- $formatb =& $workbook->add_format();
- $formatb->set_bold(1);
- $formaty =& $workbook->add_format();
- $formaty->set_bg_color('yellow');
- $formatc =& $workbook->add_format();
- $formatc->set_align('center');
- $formatr =& $workbook->add_format();
- $formatr->set_bold(1);
- $formatr->set_color('red');
- $formatr->set_align('center');
- $formatg =& $workbook->add_format();
- $formatg->set_bold(1);
- $formatg->set_color('green');
- $formatg->set_align('center');
-
- $colnum = 0;
- foreach ($headers as $item) {
- $myxls->write(0,$colnum,$item,$formatbc);
- $colnum++;
- }
- $rownum=1;
- } else if ($download=='CSV') {
- $filename .= ".txt";
-
- header("Content-Type: application/download\n");
- header("Content-Disposition: attachment; filename=\"$filename\"");
- header("Expires: 0");
- header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
- header("Pragma: public");
-
- echo implode("\t", $headers)." \n";
- }
-
- $students = join(',',array_keys(get_users_by_capability($context, 'mod/quiz:attempt','','','','','','',false)));
- if (empty($currentgroup)) {
- // all users who can attempt quizzes
- $groupstudents = '';
- $allowed = $students;
- } else {
- // all users who can attempt quizzes and who are in the currently selected group
- $groupstudents = join(',',array_keys(get_users_by_capability($context, 'mod/quiz:attempt','','','','',$currentgroup,'',false)));
- $allowed = $groupstudents;
- }
-
- // Construct the SQL
- $select = 'SELECT '.sql_concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')).' AS uniqueid, '.
- ($qmsubselect?$qmsubselect.' AS gradedattempt, ':'').
- 'qa.uniqueid AS attemptuniqueid, qa.id AS attempt, u.id AS userid, u.idnumber, u.firstname, u.lastname, u.picture, '.
- 'qa.sumgrades, qa.timefinish, qa.timestart, qa.timefinish - qa.timestart AS duration ';
-
- // This part is the same for all cases - join users and quiz_attempts tables
- $from = 'FROM '.$CFG->prefix.'user u ';
- $from .= 'LEFT JOIN '.$CFG->prefix.'quiz_attempts qa ON qa.userid = u.id AND qa.quiz = '.$quiz->id;
- if ($qmsubselect && $qmfilter){
- $from .= ' AND '.$qmsubselect;
- }
- switch ($attemptsmode){
- case QUIZ_REPORT_ATTEMPTS_ALL:
- // Show all attempts, including students who are no longer in the course
- $where = ' WHERE qa.id IS NOT NULL AND qa.preview = 0';
- break;
- case QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH:
- // Show only students with attempts
- $where = ' WHERE u.id IN (' .$allowed. ') AND qa.preview = 0 AND qa.id IS NOT NULL';
- break;
- case QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH_NO:
- // Show only students without attempts
- $where = ' WHERE u.id IN (' .$allowed. ') AND qa.id IS NULL';
- break;
- case QUIZ_REPORT_ATTEMPTS_ALL_STUDENTS:
- // Show all students with or without attempts
- $where = ' WHERE u.id IN (' .$allowed. ') AND (qa.preview = 0 OR qa.preview IS NULL)';
- break;
- }
-
- // Add extra limits due to sorting by question grade
- if ($detailedmarks) {
- $from .= ' ';
- // we want to display marks for all questions
- foreach (array_keys($questions) as $qid) {
- $select .= ", qs$qid.grade AS qsgrade$qid, qs$qid.event AS qsevent$qid, qs$qid.id AS qsid$qid";
- $from .= "LEFT JOIN {$CFG->prefix}question_sessions qns$qid ON qns$qid.attemptid = qa.uniqueid AND qns$qid.questionid = $qid ";
- $from .= "LEFT JOIN {$CFG->prefix}question_states qs$qid ON qs$qid.id = qns$qid.newgraded ";
- }
- $select .= ' ';
- }
-
+ }
+
+ $table->define_columns($columns);
+ $table->define_headers($headers);
+ $table->sortable(true, 'uniqueid');
+ // Set up the table
+ $table->define_baseurl($reporturl->out(false, $displayoptions));
- $countsql = 'SELECT COUNT(DISTINCT('.sql_concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')).')) '.$from.$where;
+ $table->collapsible(true);
- if (!$download){
- $sort = $table->get_sql_sort();
- } else {
- $sort = '';
- }
- // Fix some wired sorting
- if (empty($sort)) {
- $sort = ' ORDER BY uniqueid';
- } else {
- $sort = ' ORDER BY '.$sort;
- }
- if (!$download) {
- // Add extra limits due to initials bar
- if($table->get_sql_where()) {
- $where .= ' AND '.$table->get_sql_where();
- }
-
- // Count the records NOW, before funky question grade sorting messes up $from
- if (!empty($countsql)) {
- $totalinitials = count_records_sql($countsql);
- if ($table->get_sql_where()) {
- $countsql .= ' AND '.$table->get_sql_where();
- }
- $total = count_records_sql($countsql);
+ $table->column_suppress('picture');
+ $table->column_suppress('fullname');
+ $table->column_suppress('idnumber');
+
+ $table->no_sorting('feedbacktext');
- }
+ $table->column_class('picture', 'picture');
+ $table->column_class('lastname', 'bold');
+ $table->column_class('firstname', 'bold');
+ $table->column_class('fullname', 'bold');
+ $table->column_class('sumgrades', 'bold');
- $table->pagesize($pagesize, $total);
- }
+ $table->set_attribute('id', 'attempts');
- // Fetch the attempts
- if (!$download) {
- $attempts = get_records_sql($select.$from.$where.$sort,
- $table->get_page_start(), $table->get_page_size());
- } else {
- $attempts = get_records_sql($select.$from.$where.$sort);
- }
+ $table->out($pagesize, true);
- // Build table rows
- if (!$download) {
- $table->initialbars($totalinitials>20);
- }
- if ($attempts) {
-
- foreach ($attempts as $attempt) {
-
- // Username columns.
- $row = array();
- if (in_array('checkbox', $columns)){
- if ($attempt->attempt){
- $row[] = '';
- } else {
- $row[] = '';
- }
- }
- if (in_array('picture', $columns)){
- $picture = print_user_picture($attempt->userid, $course->id, $attempt->picture, false, true);
- $row[] = $picture;
- }
- if (!$download){
- $userlink = ''.fullname($attempt).'';
- $row[] = $userlink;
- } else {
- $row[] = fullname($attempt);
- }
-
- if (in_array('idnumber', $columns)){
- $row[] = $attempt->idnumber;
- }
-
- // Timing columns.
- if ($attempt->attempt) {
- $startdate = userdate($attempt->timestart, $strtimeformat);
- if (!$download) {
- $row[] = ''.$startdate.'';
- } else {
- $row[] = $startdate;
- }
- if ($attempt->timefinish) {
- $timefinish = userdate($attempt->timefinish, $strtimeformat);
- $duration = format_time($attempt->duration);
- if (!$download) {
- $row[] = ''.$timefinish.'';
- } else {
- $row[] = $timefinish;
- }
- $row[] = $duration;
- } else {
- $row[] = '-';
- $row[] = get_string('unfinished', 'quiz');
- }
- } else {
- $row[] = '-';
- $row[] = '-';
- $row[] = '-';
- }
-
- // Grades columns.
- if ($showgrades) {
- if ($attempt->timefinish) {
- $grade = quiz_rescale_grade($attempt->sumgrades, $quiz);
- if (!$download) {
- $gradehtml = ''.$grade.'';
- if ($qmsubselect && $attempt->gradedattempt){
- $gradehtml = '