You are here

function question_process_comment in Quiz 6.5

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

Process a manual grading action. That is, use $comment and $grade to update $state and $attempt. The attempt and the comment text are stored in the database. $state is only updated in memory, it is up to the call to store that, if appropriate.


object $question the question:

object $state the state to be updated.:

object $attempt the attempt the state belongs to, to be updated.:

string $comment the new comment from the teacher.:

mixed $grade the grade the teacher assigned, or '' to not change the grade.:

Return value

mixed true on success, a string error message if a problem is detected (for example score out of range).

1 call to question_process_comment()
regrade_question_in_attempt in includes/moodle/lib/questionlib.php
For a given question in an attempt we walk the complete history of states and recalculate the grades as we go along.


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


function question_process_comment($question, &$state, &$attempt, $comment, $grade) {
  $grade = trim($grade);
  if ($grade < 0 || $grade > $question->maxgrade) {
    $a = new stdClass();
    $a->grade = $grade;
    $a->maxgrade = $question->maxgrade;
    $a->name = $question->name;
    return get_string('errormanualgradeoutofrange', 'question', $a);

  // Update the comment and save it in the database
  $comment = trim($comment);
  $state->manualcomment = $comment;
  if (!set_field('question_sessions', 'manualcomment', $comment, 'attemptid', $attempt->uniqueid, 'questionid', $question->id)) {
    return get_string('errorsavingcomment', 'question', $question);

  // Update the attempt if the score has changed.
  if ($grade !== '' && (abs($state->last_graded->grade - $grade) > 0.002 || $state->last_graded->event != QUESTION_EVENTMANUALGRADE)) {
    $attempt->sumgrades = $attempt->sumgrades - $state->last_graded->grade + $grade;
    $attempt->timemodified = time();
    if (!update_record('quiz_attempts', $attempt)) {
      return get_string('errorupdatingattempt', 'question', $attempt);

    // We want to update existing state (rather than creating new one) if it
    // was itself created by a manual grading event.
    $state->update = $state->event == QUESTION_EVENTMANUALGRADE;

    // Update the other parts of the state object.
    $state->raw_grade = $grade;
    $state->grade = $grade;
    $state->penalty = 0;
    $state->timestamp = time();

    // Update the last graded state (don't simplify!)
    $state->last_graded = clone $state;

    // We need to indicate that the state has changed in order for it to be saved.
    $state->changed = 1;
  return true;