public function query_db($pagesize, $useinitialsbar = true)
 {
     parent::query_db($pagesize, $useinitialsbar);
     if ($this->quiz->gradebycategory) {
         $this->catgrades = new quizaccess_gradebycategory_calculator($this->quiz);
         // $this->lateststeps may or may not already have been loaded depending if the reoprt
         // is set to show question grades.
         if ($this->lateststeps === null) {
             $this->catgrades->load_latest_steps($this->attempts);
         } else {
             $this->catgrades->set_latest_steps($this->lateststeps);
         }
         $cats = $this->catgrades->load_cat_data();
         $this->add_extra_headers($cats);
         $this->add_extra_columns(array_keys($cats));
     }
 }
 /**
  * Generates the table of data
  *
  * @param array $quiz Array contining quiz data
  * @param int $context The page context ID
  * @param mod_quiz_view_object $viewobj
  */
 public function view_table($quiz, $context, $viewobj)
 {
     // Most of the following code is unfortunately a duplicate of the existing renderer. There was no way to reuse the existing
     // code and add the extra columns we wanted so I had to duplicate it.
     // New parts of the code are marked with a comment // Grade by category code start. and // Grade by category code end.
     global $DB;
     // Grade by category code start.
     // If gradebycategory setting is not on then just use the existing renderer.
     if (!$quiz->gradebycategory) {
         return parent::view_table($quiz, $context, $viewobj);
     }
     // Grade by category code end.
     if (!$viewobj->attempts) {
         return '';
     }
     // Prepare table header.
     $table = new html_table();
     $table->attributes['class'] = 'generaltable quizattemptsummary';
     $table->head = array();
     $table->align = array();
     $table->size = array();
     if ($viewobj->attemptcolumn) {
         $table->head[] = get_string('attemptnumber', 'quiz');
         $table->align[] = 'center';
         $table->size[] = '';
     }
     $table->head[] = get_string('attemptstate', 'quiz');
     $table->align[] = 'left';
     $table->size[] = '';
     if ($viewobj->markcolumn) {
         $table->head[] = get_string('marks', 'quiz') . ' / ' . quiz_format_grade($quiz, $quiz->sumgrades);
         $table->align[] = 'center';
         $table->size[] = '';
     }
     if ($viewobj->gradecolumn) {
         $table->head[] = get_string('grade') . ' / ' . quiz_format_grade($quiz, $quiz->grade);
         $table->align[] = 'center';
         $table->size[] = '';
     }
     // Grade by category code start.
     $catgrades = new quizaccess_gradebycategory_calculator($quiz);
     // $this->lateststeps may or may not already have been loaded depending if the reoprt
     // is set to show question grades.
     $catgrades->load_latest_steps($viewobj->attempts);
     $categorynames = $catgrades->load_cat_data();
     // Output column headings for category averages
     foreach ($categorynames as $catname) {
         $table->head[] = $catname . ' / ' . quiz_format_grade($quiz, 100);
         $table->align[] = 'center';
         $table->size[] = '';
     }
     // Grade by category code end.
     if ($viewobj->canreviewmine) {
         $table->head[] = get_string('review', 'quiz');
         $table->align[] = 'center';
         $table->size[] = '';
     }
     if ($viewobj->feedbackcolumn) {
         $table->head[] = get_string('feedback', 'quiz');
         $table->align[] = 'left';
         $table->size[] = '';
     }
     // One row for each attempt.
     foreach ($viewobj->attemptobjs as $attemptobj) {
         $attemptoptions = $attemptobj->get_display_options(true);
         $row = array();
         // Add the attempt number.
         if ($viewobj->attemptcolumn) {
             if ($attemptobj->is_preview()) {
                 $row[] = get_string('preview', 'quiz');
             } else {
                 $row[] = $attemptobj->get_attempt_number();
             }
         }
         $row[] = $this->attempt_state($attemptobj);
         if ($viewobj->markcolumn) {
             if ($attemptoptions->marks >= question_display_options::MARK_AND_MAX && $attemptobj->is_finished()) {
                 $row[] = quiz_format_grade($quiz, $attemptobj->get_sum_marks());
             } else {
                 $row[] = '';
             }
         }
         // Outside the if because we may be showing feedback but not grades.
         $attemptgrade = quiz_rescale_grade($attemptobj->get_sum_marks(), $quiz, false);
         if ($viewobj->gradecolumn) {
             if ($attemptoptions->marks >= question_display_options::MARK_AND_MAX && $attemptobj->is_finished()) {
                 // Highlight the highest grade if appropriate.
                 if ($viewobj->overallstats && !$attemptobj->is_preview() && $viewobj->numattempts > 1 && !is_null($viewobj->mygrade) && $attemptgrade == $viewobj->mygrade && $quiz->grademethod == QUIZ_GRADEHIGHEST) {
                     $table->rowclasses[$attemptobj->get_attempt_number()] = 'bestrow';
                 }
                 $row[] = quiz_format_grade($quiz, $attemptgrade);
             } else {
                 $row[] = '';
             }
         }
         // Grade by category code start.
         // Output cell contents for category average.
         foreach (array_keys($categorynames) as $catid) {
             $row[] = $catgrades->grade_by_category($attemptobj->get_uniqueid(), $catid);
         }
         // Grade by category code end.
         if ($viewobj->canreviewmine) {
             $row[] = $viewobj->accessmanager->make_review_link($attemptobj->get_attempt(), $attemptoptions, $this);
         }
         if ($viewobj->feedbackcolumn && $attemptobj->is_finished()) {
             if ($attemptoptions->overallfeedback) {
                 $row[] = quiz_feedback_for_grade($attemptgrade, $quiz, $context);
             } else {
                 $row[] = '';
             }
         }
         if ($attemptobj->is_preview()) {
             $table->data['preview'] = $row;
         } else {
             $table->data[$attemptobj->get_attempt_number()] = $row;
         }
     }
     // End of loop over attempts.
     $output = '';
     $output .= $this->view_table_heading();
     $output .= html_writer::table($table);
     return $output;
 }