/**
 * 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;
}
             if ($structure->update_slot_maxmark($slot, $maxmark)) {
                 // Recalculate the sumgrades for all groups
                 if ($groups = $DB->get_records('offlinequiz_groups', array('offlinequizid' => $offlinequiz->id), 'number', '*', 0, $offlinequiz->numgroups)) {
                     foreach ($groups as $group) {
                         $sumgrade = offlinequiz_update_sumgrades($offlinequiz, $group->id);
                     }
                 }
                 // Grade has really changed.
                 //$offlinequiz->sumgrades = offlinequiz_update_sumgrades($offlinequiz);
                 offlinequiz_update_question_instance($offlinequiz, $slot->questionid, unformat_float($maxmark));
                 offlinequiz_update_all_attempt_sumgrades($offlinequiz);
                 //offlinequiz_update_all_final_grades($offlinequiz);
                 offlinequiz_update_grades($offlinequiz, 0, true);
             }
             $newsummarks = $DB->get_field('offlinequiz_groups', 'sumgrades', array('id' => $offlinequizgroup->id));
             echo json_encode(array('instancemaxmark' => offlinequiz_format_question_grade($offlinequiz, $slot->maxmark), 'newsummarks' => format_float($newsummarks, $offlinequiz->decimalpoints)));
             break;
         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;
 /**
  * Display the 'marked out of' information for a question.
  * Along with the regrade action.
  * @param \stdClass $offlinequiz the offlinequiz settings from the database.
  * @param \stdClass $question data from the question and offlinequiz_slots tables.
  * @return string HTML to output.
  */
 public function marked_out_of_field($offlinequiz, $question)
 {
     if ($question->length == 0) {
         $output = html_writer::span('', 'instancemaxmark decimalplaces_' . offlinequiz_get_grade_format($offlinequiz));
         $output .= html_writer::span($this->pix_icon('spacer', '', 'moodle', array('class' => 'editicon visibleifjs', 'title' => '')), 'editing_maxmark');
         return html_writer::span($output, 'instancemaxmarkcontainer infoitem');
     }
     $output = html_writer::span(offlinequiz_format_question_grade($offlinequiz, $question->maxmark), 'instancemaxmark decimalplaces_' . offlinequiz_get_grade_format($offlinequiz), array('title' => get_string('maxmark', 'offlinequiz')));
     $output .= html_writer::span(html_writer::link(new \moodle_url('#'), $this->pix_icon('t/editstring', '', 'moodle', array('class' => 'editicon visibleifjs', 'title' => '')), array('class' => 'editing_maxmark', 'data-action' => 'editmaxmark', 'title' => get_string('editmaxmark', 'offlinequiz'))));
     return html_writer::span($output, 'instancemaxmarkcontainer');
 }