}
    if ($showing == 'stats') {
        // The $offlinequiz objects returned by get_all_instances_in_course have the necessary $cm
        // fields set to make the following call work.
        $data[] = offlinequiz_attempt_summary_link_to_reports($offlinequiz, $cm, $context);
    } else {
        if ($showing == 'grades') {
            // Grade and feedback.
            list($someoptions, $alloptions) = offlinequiz_get_combined_reviewoptions($offlinequiz);
            $grade = '';
            $feedback = '';
            if ($offlinequiz->grade && array_key_exists($offlinequiz->id, $grades)) {
                if ($alloptions->marks >= question_display_options::MARK_AND_MAX) {
                    $a = new stdClass();
                    $a->grade = offlinequiz_format_grade($offlinequiz, $grades[$offlinequiz->id]);
                    $a->maxgrade = offlinequiz_format_grade($offlinequiz, $offlinequiz->grade);
                    $grade = get_string('outofshort', 'offlinequiz', $a);
                }
            }
            $data[] = $grade;
            if ($showfeedback) {
                $data[] = $feedback;
            }
        }
    }
    $table->data[] = $data;
}
// End of loop over offlinequiz instances.
// Display the table.
echo html_writer::table($table);
// Finish the page.
                    case 'updatepagebreak':
                        require_capability('mod/offlinequiz:manage', $modcontext);
                        offlinequiz_delete_template_usages($offlinequiz);
                        $slots = $structure->update_page_break($offlinequiz, $id, $value);
                        $json = array();
                        foreach ($slots as $slot) {
                            $json[$slot->slot] = array('id' => $slot->id, 'slot' => $slot->slot, 'page' => $slot->page);
                        }
                        echo json_encode(array('slots' => $json));
                        break;
                }
                break;
            case 'course':
                break;
        }
        break;
    case 'DELETE':
        switch ($class) {
            case 'resource':
                require_capability('mod/offlinequiz:manage', $modcontext);
                if (!($slot = $DB->get_record('offlinequiz_group_questions', array('offlinequizid' => $offlinequiz->id, 'id' => $id)))) {
                    throw new moodle_exception('AJAX commands.php: Bad slot ID ' . $id);
                }
                $structure->remove_slot($offlinequiz, $slot->slot);
                offlinequiz_delete_template_usages($offlinequiz);
                offlinequiz_update_sumgrades($offlinequiz);
                echo json_encode(array('newsummarks' => offlinequiz_format_grade($offlinequiz, $offlinequiz->sumgrades), 'deleted' => true, 'newnumquestions' => $structure->get_question_count()));
                break;
        }
        break;
}
/**
 * Convert the raw grade stored in $attempt into a grade out of the maximum
 * grade for this offlinequiz.
 *
 * @param float $rawgrade the unadjusted grade, fof example $attempt->sumgrades
 * @param object $offlinequiz the offlinequiz object. Only the fields grade, sumgrades and decimalpoints are used.
 * @param bool|string $format whether to format the results for display
 *      or 'question' to format a question grade (different number of decimal places.
 * @return float|string the rescaled grade, or null/the lang string 'notyetgraded'
 *      if the $grade is null.
 */
function offlinequiz_rescale_grade($rawgrade, $offlinequiz, $group, $format = true)
{
    if (is_null($rawgrade)) {
        $grade = null;
    } else {
        if ($group->sumgrades >= 5.0E-6) {
            $grade = $rawgrade / $group->sumgrades * $offlinequiz->grade;
        } else {
            $grade = 0;
        }
    }
    if ($format === 'question') {
        $grade = offlinequiz_format_question_grade($offlinequiz, $grade);
    } else {
        if ($format) {
            $grade = offlinequiz_format_grade($offlinequiz, $grade);
        }
    }
    return $grade;
}
 /**
  * Display a question for grading tab.
  *
  * @param structure $structure object containing the structure of the offlinequiz.
  * @param \stdClass $question data from the question and offlinequiz_slots tables.
  * @param \moodle_url $pageurl the canonical URL of this page.
  * @return string HTML to output.
  */
 public function question_for_grading(structure $structure, $question, \moodle_url $pageurl)
 {
     $output = '';
     $output .= html_writer::start_tag('div');
     $output .= html_writer::start_div('mod-indent-outer');
     $output .= $this->question_number($question->displayednumber);
     // This div is used to indent the content.
     $output .= html_writer::div('', 'mod-indent');
     // Display the link to the question (or do nothing if question has no url).
     if ($question->qtype == 'random') {
         $questionname = $this->random_question($structure, $question, $pageurl);
     } else {
         $questionname = $this->question_name($structure, $question, $pageurl);
     }
     // Start the div for the activity title, excluding the edit icons.
     $output .= html_writer::start_div('activityinstance');
     $output .= $questionname;
     // Closing the tag which contains everything but edit icons. Content part of the module should not be part of this.
     $output .= html_writer::end_div();
     // .activityinstance.
     // Action icons.
     $questionicons = '';
     $input = '<input class="gradeinput" id="inputq' . $question->id . '" type="text" value="' . offlinequiz_format_grade($structure->get_offlinequiz(), $question->maxmark) . '" size="4" tabindex="' . $question->slot . '" name="g' . $question->id . '"/>';
     $questionicons .= html_writer::span($input, 'instancemaxmark decimalplaces_' . offlinequiz_get_grade_format($structure->get_offlinequiz()));
     //        $questionicons .= $this->marked_out_of_field($structure->get_offlinequiz(), $question);
     $output .= html_writer::span($questionicons, 'actions');
     // Required to add js spinner icon.
     // End of indentation div.
     $output .= html_writer::end_tag('div');
     $output .= html_writer::end_tag('div');
     return $output;
 }
/**
 * Format a number as a percentage out of $offlinequiz->sumgrades
 * 
 * @param number $rawgrade the mark to format.
 * @param object $offlinequiz the offlinequiz settings
 * @param bool $round whether to round the results ot $offlinequiz->decimalpoints.
 */
function offlinequiz_report_scale_grade($rawmark, $offlinequiz, $round = true)
{
    if ($offlinequiz->sumgrades <= 0) {
        return '';
    }
    if (!is_numeric($rawmark)) {
        return $rawmark;
    }
    $mark = $rawmark / $offlinequiz->sumgrades * $offlinequiz->grade;
    if ($round) {
        $mark = offlinequiz_format_grade($offlinequiz, $mark);
    }
    return $mark;
}
 protected function get_formatted_offlinequiz_info_data($course, $cm, $offlinequiz, $offlinequizstats)
 {
     // You can edit this array to control which statistics are displayed.
     $todisplay = array('allattemptscount' => 'number', 'maxgrade' => 'number_format', 'bestgrade' => 'scale_to_maxgrade', 'worstgrade' => 'scale_to_maxgrade', 'allattemptsavg' => 'scale_to_maxgrade', 'median' => 'scale_to_maxgrade', 'standarddeviation' => 'scale_to_maxgrade', 'skewness' => 'number_format', 'kurtosis' => 'number_format', 'cic' => 'percent_to_number_format', 'errorratio' => 'number_format_percent', 'standarderror' => 'scale_to_maxgrade');
     if ($offlinequiz->sumgrades > 0) {
         $offlinequizstats->sumgrades = $offlinequiz->sumgrades;
     } else {
         if ($offlinequiz->sumgrades == -1) {
             $offlinequizstats->sumgrades = '';
             $offlinequizstats->bestgrade = '';
             $offlinequizstats->worstgrade = '';
             $offlinequizstats->allattemptsavg = '';
             $offlinequizstats->median = '';
             $offlinequizstats->standarddeviation = '';
         }
     }
     $offlinequizstats->maxgrade = $offlinequiz->grade;
     // General information about the offlinequiz.
     $offlinequizinfo = array();
     $offlinequizinfo[get_string('offlinequizname', 'offlinequiz_statistics')] = format_string($offlinequiz->name);
     if ($cm->idnumber) {
         $offlinequizinfo[get_string('idnumbermod')] = $cm->idnumber;
     }
     if ($offlinequiz->timeopen) {
         $offlinequizinfo[get_string('reviewopens', 'offlinequiz')] = userdate($offlinequiz->timeopen);
     }
     if ($offlinequiz->timeclose) {
         $offlinequizinfo[get_string('reviewcloses', 'offlinequiz')] = userdate($offlinequiz->timeclose);
     }
     if ($offlinequiz->timeopen && $offlinequiz->timeclose) {
         $offlinequizinfo[get_string('duration', 'offlinequiz_statistics')] = format_time($offlinequiz->timeclose - $offlinequiz->timeopen);
     }
     // The statistics.
     foreach ($todisplay as $property => $format) {
         if (!isset($offlinequizstats->{$property}) || empty($format)) {
             continue;
         }
         $value = $offlinequizstats->{$property};
         switch ($format) {
             case 'summarks_as_percentage':
                 $formattedvalue = offlinequiz_report_scale_summarks_as_percentage($value, $offlinequiz);
                 break;
             case 'scale_to_maxgrade':
                 $formattedvalue = offlinequiz_report_scale_grade($value, $offlinequiz);
                 break;
             case 'number_format_percent':
                 $formattedvalue = offlinequiz_format_grade($offlinequiz, $value) . '%';
                 break;
             case 'number_format':
                 // 2 extra decimal places, since not a percentage,
                 // and we want the same number of sig figs.???
                 $formattedvalue = format_float($value, $offlinequiz->decimalpoints);
                 break;
             case 'percent_to_number_format':
                 $formattedvalue = format_float($value / 100.0, $offlinequiz->decimalpoints);
                 break;
             case 'number':
                 $formattedvalue = $value + 0;
                 break;
             default:
                 $formattedvalue = $value;
         }
         $offlinequizinfo[get_string($property, 'offlinequiz_statistics', $this->using_attempts_string(!empty($offlinequizstats->allattempts)))] = $formattedvalue;
     }
     return $offlinequizinfo;
 }