You are here

function question_apply_penalty_and_timelimit in Quiz 6.5

Same name and namespace in other branches
  1. 6.6 includes/moodle/lib/questionlib.php \question_apply_penalty_and_timelimit()

Applies the penalty from the previous graded responses to the raw grade for the current responses

The grade for the question in the current state is computed by subtracting the penalty accumulated over the previous graded responses at the question from the raw grade. If the timestamp is more than 1 minute beyond the end of the attempt the grade is set to zero. The ->grade field of the state object is modified to reflect the new grade but is never allowed to decrease.


object $question The question for which the penalty is to be applied.:

object $state The state for which the grade is to be set from the: raw grade and the cumulative penalty from the last graded state. The ->grade field is updated by applying the penalty scheme determined in $cmoptions to the ->raw_grade and ->last_graded->penalty fields.

object $cmoptions The options set by the course module.: The ->penaltyscheme field determines whether penalties for incorrect earlier responses are subtracted.

1 call to question_apply_penalty_and_timelimit()
question_process_responses in includes/moodle/lib/questionlib.php
Processes an array of student responses, grading and saving them as appropriate


includes/moodle/lib/questionlib.php, line 1436


function question_apply_penalty_and_timelimit(&$question, &$state, $attempt, $cmoptions) {

  // TODO. Quiz dependancy. The fact that the attempt that is passed in here
  // is from quiz_attempts, and we use things like $cmoptions->timelimit.
  // deal with penalty
  if ($cmoptions->penaltyscheme) {
    $state->grade = $state->raw_grade - $state->sumpenalty;
    $state->sumpenalty += (double) $state->penalty;
  else {
    $state->grade = $state->raw_grade;

  // deal with timelimit
  if ($cmoptions->timelimit) {

    // We allow for 5% uncertainty in the following test
    if ($state->timestamp - $attempt->timestart > $cmoptions->timelimit * 63) {
      $cm = get_coursemodule_from_instance('quiz', $cmoptions->id);
      if (!has_capability('mod/quiz:ignoretimelimits', get_context_instance(CONTEXT_MODULE, $cm->id), $attempt->userid, false)) {
        $state->grade = 0;

  // deal with closing time
  if ($cmoptions->timeclose and $state->timestamp > $cmoptions->timeclose + 60 and !$attempt->preview) {

    // ignore closing time for previews
    $state->grade = 0;

  // Ensure that the grade does not go down
  $state->grade = max($state->grade, $state->last_graded->grade);