]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15170 "convert to use new tablelib api"
authorjamiesensei <jamiesensei>
Fri, 6 Jun 2008 12:09:14 +0000 (12:09 +0000)
committerjamiesensei <jamiesensei>
Fri, 6 Jun 2008 12:09:14 +0000 (12:09 +0000)
mod/quiz/report/overview/overview_table.php [new file with mode: 0644]
mod/quiz/report/overview/report.php

diff --git a/mod/quiz/report/overview/overview_table.php b/mod/quiz/report/overview/overview_table.php
new file mode 100644 (file)
index 0000000..7610072
--- /dev/null
@@ -0,0 +1,206 @@
+<?php  // $Id$
+
+class quiz_report_overview_table extends table_sql {
+    
+    var $useridfield = 'userid';
+    
+    var $candelete;
+    var $reporturl;
+    var $displayoptions;
+    
+    function quiz_report_overview_table($quiz , $qmsubselect, $groupstudents,
+                $students, $detailedmarks, $questions, $candelete, $reporturl, $displayoptions){
+        parent::table_sql('mod-quiz-report-overview-report');
+        $this->quiz = $quiz;
+        $this->qmsubselect = $qmsubselect;
+        $this->groupstudents = $groupstudents;
+        $this->students = $students;
+        $this->detailedmarks = $detailedmarks;
+        $this->questions = $questions;
+        $this->candelete = $candelete;
+        $this->reporturl = $reporturl;
+        $this->displayoptions = $displayoptions;
+    }
+    function build_table(){
+        global $CFG;
+        if ($this->rawdata) {
+            // Define some things we need later to process raw data from db.
+            $this->strtimeformat = get_string('strftimedatetime');
+            parent::build_table();
+            //end of adding data from attempts data to table / download
+            //now add averages at bottom of table :
+            $averagesql = "SELECT AVG(qg.grade) AS grade " .
+                    "FROM {$CFG->prefix}quiz_grades qg " .
+                    "WHERE quiz=".$this->quiz->id;
+                    
+            $this->add_separator();
+            if ($this->is_downloading()){
+                $namekey = 'lastname';
+            } else {
+                $namekey = 'fullname';
+            }
+            if ($this->groupstudents){
+                $groupaveragesql = $averagesql." AND qg.userid IN ($this->groupstudents)";
+                $groupaverage = get_record_sql($groupaveragesql);
+                $groupaveragerow = array($namekey => get_string('groupavg', 'grades'),
+                        'sumgrades' => round($groupaverage->grade, $this->quiz->decimalpoints),
+                        'feedbacktext'=> strip_tags(quiz_report_feedback_for_grade($groupaverage->grade, $this->quiz->id)));
+                if($this->detailedmarks && $this->qmsubselect) {
+                    $avggradebyq = quiz_get_average_grade_for_questions($this->quiz, $this->groupstudents);
+                    $groupaveragerow += quiz_format_average_grade_for_questions($avggradebyq, $this->questions, $this->quiz, $this->is_downloading());
+                }
+                $this->add_data_keyed($groupaveragerow);
+            }
+            $overallaverage = get_record_sql($averagesql." AND qg.userid IN ($this->students)");
+            $overallaveragerow = array($namekey => get_string('overallaverage', 'grades'),
+                        'sumgrades' => round($overallaverage->grade, $this->quiz->decimalpoints),
+                        'feedbacktext'=> strip_tags(quiz_report_feedback_for_grade($overallaverage->grade, $this->quiz->id)));
+            if($this->detailedmarks && $this->qmsubselect) {
+                $avggradebyq = quiz_get_average_grade_for_questions($this->quiz, $this->students);
+                $overallaveragerow += quiz_format_average_grade_for_questions($avggradebyq, $this->questions, $this->quiz, $this->is_downloading());
+            }
+            $this->add_data_keyed($overallaveragerow);
+        }
+    }
+    
+    function wrap_html_start(){
+        if (!$this->is_downloading()) {
+            if ($this->candelete) {
+                // Start form
+                $strreallydel  = addslashes_js(get_string('deleteattemptcheck','quiz'));
+                echo '<div id="tablecontainer">';
+                echo '<form id="attemptsform" method="post" action="' . $this->reporturl->out(true) .
+                        '" onsubmit="confirm(\''.$strreallydel.'\');">';
+                echo $this->reporturl->hidden_params_out(array(), 0, $this->displayoptions);
+                echo '<div>';
+            }
+        }
+    }
+    function wrap_html_finish(){
+        if (!$this->is_downloading()) {
+            // Print "Select all" etc.
+            if ($this->candelete) {
+                echo '<table id="commands">';
+                echo '<tr><td>';
+                echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.
+                        get_string('selectall', 'quiz').'</a> / ';
+                echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.
+                        get_string('selectnone', 'quiz').'</a> ';
+                echo '&nbsp;&nbsp;';
+                echo '<input type="submit" value="'.get_string('deleteselected', 'quiz_overview').'"/>';
+                echo '</td></tr></table>';
+                // Close form
+                echo '</div>';
+                echo '</form></div>';
+            }
+        }
+    }
+
+    
+    function col_checkbox($attempt){
+        if ($attempt->attempt){
+            return '<input type="checkbox" name="attemptid[]" value="'.$attempt->attempt.'" />';
+        } else {
+            return '';
+        }
+    }
+    
+    function col_picture($attempt){
+        global $COURSE;
+        return print_user_picture($attempt->userid, $COURSE->id, $attempt->picture, false, true);
+    }
+
+    
+    function col_timestart($attempt){
+        if ($attempt->attempt) {
+            $startdate = userdate($attempt->timestart, $this->strtimeformat);
+            if (!$this->is_downloading()) {
+                return  '<a href="review.php?q='.$this->quiz->id.'&amp;attempt='.$attempt->attempt.'">'.$startdate.'</a>';
+            } else {
+                return  $startdate;
+            }
+        } else {
+            return  '-';
+        }
+    }
+    function col_timefinish($attempt){
+        if ($attempt->attempt) {
+            if ($attempt->timefinish) {
+                $timefinish = userdate($attempt->timefinish, $this->strtimeformat);
+                if (!$this->is_downloading()) {
+                    return '<a href="review.php?q='.$this->quiz->id.'&amp;attempt='.$attempt->attempt.'">'.$timefinish.'</a>';
+                } else {
+                    return $timefinish;
+                }
+            } else {
+                return  '-';
+            }
+        } else {
+            return  '-';
+        }
+    }
+    
+    function col_duration($attempt){
+        if ($attempt->timefinish) {
+            return format_time($attempt->duration);
+        } elseif ($attempt->timestart) {
+            return get_string('unfinished', 'quiz');
+        } else {
+            return '-';
+        }
+    }
+    function col_sumgrades($attempt){
+        if ($attempt->timefinish) {
+            $grade = quiz_rescale_grade($attempt->sumgrades, $this->quiz);
+            if (!$this->is_downloading()) {
+                $gradehtml = '<a href="review.php?q='.$this->quiz->id.'&amp;attempt='.$attempt->attempt.'">'.$grade.'</a>';
+                if ($this->qmsubselect && $attempt->gradedattempt){
+                    $gradehtml = '<div class="highlight">'.$gradehtml.'</div>';
+                }
+                return $gradehtml;
+            } else {
+                return $grade;
+            }
+        } else {
+            return '-';
+        }
+    }
+    function other_cols($colname, $attempt){
+        if (preg_match('/^qsgrade([0-9]+)$/', $colname, $matches)){
+            $questionid = $matches[1];
+            $question = $this->questions[$questionid];
+            $state = new object();
+            $state->event = $attempt->{'qsevent'.$questionid};
+            if (question_state_is_graded($state)) {
+                $grade = quiz_rescale_grade($attempt->{'qsgrade'.$questionid}, $this->quiz);
+            } else {
+                $grade = '--';
+            }
+            if (!$this->is_downloading()) {
+                $grade = $grade.'/'.quiz_rescale_grade($question->grade, $this->quiz);
+                return link_to_popup_window('/mod/quiz/reviewquestion.php?state='.
+                        $attempt->{'qsid'.$questionid}.'&amp;number='.$question->number,
+                        'reviewquestion', $grade, 450, 650, get_string('reviewresponse', 'quiz'),
+                        'none', true);
+            } else {
+                return $grade;
+            }     
+        } else {
+            return NULL;
+        }
+    }
+    
+    function col_feedbacktext($attempt){
+        if ($attempt->timefinish) {
+            if (!$this->is_downloading()) {
+                return quiz_report_feedback_for_grade(quiz_rescale_grade($attempt->sumgrades, $this->quiz), $this->quiz->id);
+            } else {
+                return strip_tags(quiz_report_feedback_for_grade(quiz_rescale_grade($attempt->sumgrades, $this->quiz), $this->quiz->id));
+            }
+        } else {
+            return '-';
+        }
+    
+    }
+}
+?>
index eb93a46d9afcf2666ee30fabeb87a35f4effb98b..8bc1d399186e615b67956aee59268ccd30a518e4 100644 (file)
@@ -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 @@ class quiz_report extends quiz_default_report {
      * 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 @@ class quiz_report extends quiz_default_report {
             //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 @@ class quiz_report extends quiz_default_report {
 
         $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 @@ class quiz_report extends quiz_default_report {
         $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 '<div class="quizattemptcounts">' . $strattemptnum . '</div>';
             }
@@ -115,35 +134,86 @@ class quiz_report extends quiz_default_report {
 
         // 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 '<div class="quizattemptcounts">' . $strattempthighlight . '</div>';
             }
         }
+        
+        
+        $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 @@ class quiz_report extends quiz_default_report {
 
         $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', '\'#\'', '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 = '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;
-        }
+         }
+         
+        $table->define_columns($columns);
+        $table->define_headers($headers);
+        $table->sortable(true, 'uniqueid');
         
-        // 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 .= ' ';
-        }
-
-        
-
-        $countsql = 'SELECT COUNT(DISTINCT('.sql_concat('u.id', '\'#\'', 'COALESCE(qa.attempt, 0)').')) '.$from.$where;
-
-        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);
-
-            }
+        // Set up the table
+        $table->define_baseurl($reporturl->out(false, $displayoptions));
 
-            $table->pagesize($pagesize, $total);
-        }
+        $table->collapsible(true);
 
-        // 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->column_suppress('picture');
+        $table->column_suppress('fullname');
+        $table->column_suppress('idnumber');
         
-        // Build table rows
-        if (!$download) {
-            $table->initialbars($totalinitials>20);
-        }
-        if ($attempts) {
-
-            foreach ($attempts as $attempt) {
+        $table->no_sorting('feedbacktext');
 
-                // Username columns.
-                $row = array();
-                if (in_array('checkbox', $columns)){
-                    if ($attempt->attempt){
-                        $row[] = '<input type="checkbox" name="attemptid[]" value="'.$attempt->attempt.'" />';
-                    } else {
-                        $row[] = '';
-                    }
-                }
-                if (in_array('picture', $columns)){
-                    $picture = print_user_picture($attempt->userid, $course->id, $attempt->picture, false, true);
-                    $row[] = $picture;
-                }
-                if (!$download){
-                    $userlink = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$attempt->userid.
-                            '&amp;course='.$course->id.'">'.fullname($attempt).'</a>';
-                    $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[] = '<a href="review.php?q='.$quiz->id.'&amp;attempt='.$attempt->attempt.'">'.$startdate.'</a>';
-                    } else {
-                        $row[] = $startdate;
-                    }
-                    if ($attempt->timefinish) {
-                        $timefinish = userdate($attempt->timefinish, $strtimeformat);
-                        $duration = format_time($attempt->duration);
-                        if (!$download) {
-                            $row[] = '<a href="review.php?q='.$quiz->id.'&amp;attempt='.$attempt->attempt.'">'.$timefinish.'</a>';
-                        } 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 = '<a href="review.php?q='.$quiz->id.'&amp;attempt='.$attempt->attempt.'">'.$grade.'</a>';
-                            if ($qmsubselect && $attempt->gradedattempt){
-                                $gradehtml = '<div class="highlight">'.$gradehtml.'</div>';
-                            }
-                            $row[] = $gradehtml;
-                        } else {
-                            $row[] = $grade;
-                        }
-                    } else {
-                        $row[] = '-';
-                    }
-                    
-                }
-
-                if($detailedmarks) {
-                    if(empty($attempt->attempt)) {
-                        foreach($questions as $question) {
-                            $row[] = '-';
-                        }
-                    } else {
-                        foreach($questions as $questionid => $question) {
-                            $state = new object();
-                            $state->event = $attempt->{'qsevent'.$questionid};
-                            if (question_state_is_graded($state)) {
-                                $grade = quiz_rescale_grade($attempt->{'qsgrade'.$questionid}, $quiz);
-                            } else {
-                                $grade = '--';
-                            }
-                            if (!$download) {
-                                $grade = $grade.'/'.quiz_rescale_grade($question->grade, $quiz);
-                                $row[] = link_to_popup_window('/mod/quiz/reviewquestion.php?state='.
-                                        $attempt->{'qsid'.$questionid}.'&amp;number='.$question->number,
-                                        'reviewquestion', $grade, 450, 650, $strreviewquestion, 'none', true);
-                            } else {
-                                $row[] = $grade;
-                            }
-                        }
-                    }
-                }
-
-                // Feedback column.
-                if ($hasfeedback) {
-                    if ($attempt->timefinish) {
-                        $row[] = quiz_report_feedback_for_grade(quiz_rescale_grade($attempt->sumgrades, $quiz), $quiz->id);
-                    } else {
-                        $row[] = '-';
-                    }
-                }
-                if (!$download) {
-                    $table->add_data($row);
-                } else if ($download == 'Excel' or $download == 'ODS') {
-                    $colnum = 0;
-                    foreach($row as $item){
-                        $myxls->write($rownum,$colnum,$item,$format);
-                        $colnum++;
-                    }
-                    $rownum++;
-                } else if ($download=='CSV') {
-                    $text = implode("\t", $row);
-                    echo $text." \n";
-                }
-            }
-            //end of adding data from attempts data to table / download
-            //now add averages :
-            if (!$download && $attempts){
-    
-                $averagesql = "SELECT AVG(qg.grade) AS grade " .
-                        "FROM {$CFG->prefix}quiz_grades qg " .
-                        "WHERE quiz=".$quiz->id;
-                        
-                $table->add_separator();
-                if ($groupstudents){
-                    $groupaveragesql = $averagesql." AND qg.userid IN ($groupstudents)";
-                    $groupaverage = get_record_sql($groupaveragesql);
-                    $groupaveragerow = array('fullname' => get_string('groupavg', 'grades'),
-                            'sumgrades' => round($groupaverage->grade, $quiz->decimalpoints),
-                            'feedbacktext'=> quiz_report_feedback_for_grade($groupaverage->grade, $quiz->id));
-                    if($detailedmarks && $qmsubselect) {
-                        $avggradebyq = quiz_get_average_grade_for_questions($quiz, $groupstudents);
-                        $groupaveragerow += quiz_format_average_grade_for_questions($avggradebyq, $questions, $quiz, $download);
-                    }
-                    $table->add_data_keyed($groupaveragerow);
-                }
-                $overallaverage = get_record_sql($averagesql." AND qg.userid IN ($students)");
-                $overallaveragerow = array('fullname' => get_string('overallaverage', 'grades'),
-                            'sumgrades' => round($overallaverage->grade, $quiz->decimalpoints),
-                            'feedbacktext'=> quiz_report_feedback_for_grade($overallaverage->grade, $quiz->id));
-                if($detailedmarks && $qmsubselect) {
-                    $avggradebyq = quiz_get_average_grade_for_questions($quiz, $students);
-                    $overallaveragerow += quiz_format_average_grade_for_questions($avggradebyq, $questions, $quiz, $download);
-                }
-                $table->add_data_keyed($overallaveragerow);
-            }    
-            if (!$download) {
-                // Start form
-                echo '<div id="tablecontainer">';
-                echo '<form id="attemptsform" method="post" action="' . $reporturlwithdisplayoptions->out(true) .
-                        '" onsubmit="confirm(\''.$strreallydel.'\');">';
-                echo $reporturlwithdisplayoptions->hidden_params_out();
-                echo '<div>';
+        $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');
 
-                // Print table
-                $table->print_html();
+        $table->set_attribute('id', 'attempts');
 
-                // Print "Select all" etc.
-                if (!empty($attempts) && $candelete) {
-                    echo '<table id="commands">';
-                    echo '<tr><td>';
-                    echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.
-                            get_string('selectall', 'quiz').'</a> / ';
-                    echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.
-                            get_string('selectnone', 'quiz').'</a> ';
-                    echo '&nbsp;&nbsp;';
-                    echo '<input type="submit" value="'.get_string('deleteselected', 'quiz_overview').'"/>';
-                    echo '</td></tr></table>';
-                }
-                // Close form
-                echo '</div>';
-                echo '</form></div>';
-
-                if (!empty($attempts)) {
-                    echo '<table class="boxaligncenter"><tr>';
-                    echo '<td>';
-                    print_single_button($reporturl->out(true), $pageoptions + $displayoptions + array('download' => 'ODS'),
-                                         get_string('downloadods'));
-                    echo "</td>\n";
-                    echo '<td>';
-                    print_single_button($reporturl->out(true), $pageoptions + $displayoptions + array('download' => 'Excel'),
-                                         get_string('downloadexcel'));
-                    echo "</td>\n";
-                    echo '<td>';
-                    print_single_button($reporturl->out(true), $pageoptions + $displayoptions + array('download' => 'CSV'),
-                                         get_string('downloadtext'));
-                    echo "</td>\n";
-                    echo "<td>";
-                    helpbutton('overviewdownload', get_string('overviewdownload', 'quiz_overview'), 'quiz');
-                    echo "</td>\n";
-                    echo '</tr></table>';
-                }
-            }
-        } else {
-            if (!$download) {
-                $table->print_html();
-            }
-        }
-        if ($download == 'Excel' or $download == 'ODS') {
-            $workbook->close();
-            exit;
-        } else if ($download == 'CSV') {
-            exit;
-        }
-        if (!$download) {
+        $table->out($pagesize, true);
+        
+        if (!$table->is_downloading()) {
             // Print display options
             $mform->set_data($displayoptions +compact('detailedmarks', 'pagesize'));
             $mform->display();
-            if ($attempts){
-                $imageurl = $CFG->wwwroot.'/mod/quiz/report/overview/overviewgraph.php?id='.$quiz->id;
-                print_heading(get_string('overviewreportgraph', 'quiz_overview'));
-                echo '<div class="mdl-align"><img src="'.$imageurl.'" alt="'.get_string('overviewreportgraph', 'quiz_overview').'" /></div>';
+            if (count($table->totalrows)){
+                 $imageurl = $CFG->wwwroot.'/mod/quiz/report/overview/overviewgraph.php?id='.$quiz->id;
+                 print_heading(get_string('overviewreportgraph', 'quiz_overview'));
+                 echo '<div class="mdl-align"><img src="'.$imageurl.'" alt="'.get_string('overviewreportgraph', 'quiz_overview').'" /></div>';
             }
         }
         return true;
     }
+
 }
 
+
 ?>