$result->timemodified = time();
        $DB->update_record('offlinequiz_results', $result);
        // Log this action.
        $params = array('objectid' => $slotquestion->id, 'courseid' => $course->id, 'context' => context_module::instance($cm->id), 'other' => array('offlinequizid' => $offlinequiz->id, 'resultid' => $result->id, 'slot' => $slot));
        $event = \mod_offlinequiz\event\question_manually_graded::create($params);
        $event->trigger();
        // Update the gradebook.
        offlinequiz_update_grades($offlinequiz);
        echo $OUTPUT->notification(get_string('changessaved'), 'notifysuccess');
        close_window(2, true);
        die;
    }
}
// Print the comment form.
echo '<form method="post" class="mform" id="manualgradingform" action="' . $CFG->wwwroot . '/mod/offlinequiz/comment.php">';
$options = new mod_offlinequiz_display_options();
$options->hide_all_feedback();
$options->manualcomment = question_display_options::EDITABLE;
if (property_exists($slotquestion, '_number')) {
    echo $quba->render_question($slot, $options, $slotquestion->_number);
} else {
    echo $quba->render_question($slot, $options);
}
?>
  <div>
    <input type="hidden" name="resultid" value="<?php 
echo $result->id;
?>
" />
    <input type="hidden" name="slot" value="<?php 
echo $slot;
/**
 * @param object $offlinequiz the offlinequiz settings
 * @param object $question the question
 * @return moodle_url to preview this question with the options from this offlinequiz.
 */
function offlinequiz_question_preview_url($offlinequiz, $question)
{
    // Get the appropriate display options.
    $displayoptions = mod_offlinequiz_display_options::make_from_offlinequiz($offlinequiz);
    $maxmark = null;
    if (isset($question->maxmark)) {
        $maxmark = $question->maxmark;
    }
    // Work out the correct preview URL.
    return question_preview_url($question->id, null, $maxmark, $displayoptions);
}
/**
 * Create grade item for given offlinequiz
 *
 * @param object $offlinequiz object with extra cmidnumber
 * @param mixed $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
 * @return int 0 if ok, error code otherwise
 */
function offlinequiz_grade_item_update($offlinequiz, $grades = null)
{
    global $CFG, $OUTPUT, $DB;
    require_once $CFG->dirroot . '/mod/offlinequiz/locallib.php';
    require_once $CFG->libdir . '/gradelib.php';
    require_once $CFG->libdir . '/questionlib.php';
    if (array_key_exists('cmidnumber', $offlinequiz)) {
        // May not be always present.
        $params = array('itemname' => $offlinequiz->name, 'idnumber' => $offlinequiz->cmidnumber);
    } else {
        $params = array('itemname' => $offlinequiz->name);
    }
    $offlinequiz->grade = $DB->get_field('offlinequiz', 'grade', array('id' => $offlinequiz->id));
    if (property_exists($offlinequiz, 'grade') && $offlinequiz->grade > 0) {
        $params['gradetype'] = GRADE_TYPE_VALUE;
        $params['grademax'] = $offlinequiz->grade;
        $params['grademin'] = 0;
    } else {
        $params['gradetype'] = GRADE_TYPE_NONE;
    }
    // Description by Juergen Zimmer (Tim Hunt):
    // 1. If the offlinequiz is set to not show grades while the offlinequiz is still open,
    //    and is set to show grades after the offlinequiz is closed, then create the
    //    grade_item with a show-after date that is the offlinequiz close date.
    // 2. If the offlinequiz is set to not show grades at either of those times,
    //    create the grade_item as hidden.
    // 3. If the offlinequiz is set to show grades, create the grade_item visible.
    $openreviewoptions = mod_offlinequiz_display_options::make_from_offlinequiz($offlinequiz);
    $closedreviewoptions = mod_offlinequiz_display_options::make_from_offlinequiz($offlinequiz);
    if ($openreviewoptions->marks < question_display_options::MARK_AND_MAX && $closedreviewoptions->marks < question_display_options::MARK_AND_MAX) {
        $params['hidden'] = 1;
    } else {
        if ($openreviewoptions->marks < question_display_options::MARK_AND_MAX && $closedreviewoptions->marks >= question_display_options::MARK_AND_MAX) {
            if ($offlinequiz->timeclose) {
                $params['hidden'] = $offlinequiz->timeclose;
            } else {
                $params['hidden'] = 1;
            }
        } else {
            // A) both open and closed enabled
            // B) open enabled, closed disabled - we can not "hide after",
            //    grades are kept visible even after closing.
            $params['hidden'] = 0;
        }
    }
    if (!$params['hidden']) {
        // If the grade item is not hidden by the offlinequiz logic, then we need to
        // hide it if the offlinequiz is hidden from students.
        $cm = get_coursemodule_from_instance('offlinequiz', $offlinequiz->id);
        if ($cm) {
            $params['hidden'] = !$cm->visible;
        } else {
            $params['hidden'] = !$offlinequiz->visible;
        }
    }
    if ($grades === 'reset') {
        $params['reset'] = true;
        $grades = null;
    }
    $gradebookgrades = grade_get_grades($offlinequiz->course, 'mod', 'offlinequiz', $offlinequiz->id);
    if (!empty($gradebookgrades->items)) {
        $gradeitem = $gradebookgrades->items[0];
        if ($gradeitem->hidden) {
            $params['hidden'] = 1;
        }
        if ($gradeitem->locked) {
            $confirmregrade = optional_param('confirm_regrade', 0, PARAM_INT);
            if (!$confirmregrade) {
                if (!AJAX_SCRIPT) {
                    $message = get_string('gradeitemislocked', 'grades');
                    $backlink = $CFG->wwwroot . '/mod/offlinequiz/edit.php?q=' . $offlinequiz->id . '&amp;mode=overview';
                    $regradelink = qualified_me() . '&amp;confirm_regrade=1';
                    echo $OUTPUT->box_start('generalbox', 'notice');
                    echo '<p>' . $message . '</p>';
                    echo $OUTPUT->container_start('buttons');
                    echo $OUTPUT->single_button($regradelink, get_string('regradeanyway', 'grades'));
                    echo $OUTPUT->single_button($backlink, get_string('cancel'));
                    echo $OUTPUT->container_end();
                    echo $OUTPUT->box_end();
                }
                return GRADE_UPDATE_ITEM_LOCKED;
            }
        }
    }
    return grade_update('mod/offlinequiz', $offlinequiz->course, 'mod', 'offlinequiz', $offlinequiz->id, 0, $grades, $params);
}
 /**
  * Make some text into a link to review the offlinequiz, if that is appropriate.
  *
  * @param string $linktext some text.
  * @param object $attempt the attempt object
  * @return string some HTML, the $linktext either unmodified or wrapped in a
  *      link to the review page.
  */
 public function make_review_link($attempt, $reviewoptions, $output)
 {
     // If the attempt is still open, don't link.
     if (in_array($attempt->state, array(offlinequiz_attempt::IN_PROGRESS, offlinequiz_attempt::OVERDUE))) {
         return $output->no_review_message('');
     }
     $when = offlinequiz_attempt_state($this->offlinequizobj->get_offlinequiz(), $attempt);
     $reviewoptions = mod_offlinequiz_display_options::make_from_offlinequiz($this->offlinequizobj->get_offlinequiz(), $when);
     if (!$reviewoptions->attempt) {
         return $output->no_review_message($this->offlinequizobj->cannot_review_message($when, true));
     } else {
         return $output->review_link($this->offlinequizobj->review_url($attempt->id), $this->attempt_must_be_in_popup(), $this->get_popup_options());
     }
 }