public function test_quiz_format_question_grade() { $quiz = new stdClass(); $quiz->decimalpoints = 2; $quiz->questiondecimalpoints = 2; $this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.12, 2)); $this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 2)); $this->assertEquals(quiz_format_question_grade($quiz, 1.0), format_float(1, 2)); $quiz->decimalpoints = 3; $quiz->questiondecimalpoints = -1; $this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.123, 3)); $this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 3)); $this->assertEquals(quiz_format_question_grade($quiz, 1.0), format_float(1, 3)); $quiz->questiondecimalpoints = 4; $this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.1235, 4)); $this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 4)); $this->assertEquals(quiz_format_question_grade($quiz, 1.0), format_float(1, 4)); }
/** * Return the grade obtained on a particular question. * You must previously have called load_question_states to load the state * data about this question. * * @param int $slot the number used to identify this question within this attempt. * @return string the formatted grade, to the number of decimal places specified by the quiz. */ public function get_question_mark($slot) { return quiz_format_question_grade($this->get_quiz(), $this->quba->get_question_mark($slot)); }
/** * Get the maximum mark for a question, formatted for display. * @param int $slotnumber the index of the slot in question. * @return string the maximum mark for the question in this slot. */ public function formatted_question_grade($slotnumber) { return quiz_format_question_grade($this->get_quiz(), $this->slotsinorder[$slotnumber]->maxmark); }
require_capability('mod/quiz:manage', $modcontext); $slot = $DB->get_record('quiz_slots', array('id' => $id), '*', MUST_EXIST); echo json_encode(array('instancemaxmark' => quiz_format_question_grade($quiz, $slot->maxmark))); break; case 'updatemaxmark': require_capability('mod/quiz:manage', $modcontext); $slot = $structure->get_slot_by_id($id); if ($structure->update_slot_maxmark($slot, $maxmark)) { // Grade has really changed. quiz_delete_previews($quiz); quiz_update_sumgrades($quiz); quiz_update_all_attempt_sumgrades($quiz); quiz_update_all_final_grades($quiz); quiz_update_grades($quiz, 0, true); } echo json_encode(array('instancemaxmark' => quiz_format_question_grade($quiz, $maxmark), 'newsummarks' => quiz_format_grade($quiz, $quiz->sumgrades))); break; case 'updatepagebreak': require_capability('mod/quiz:manage', $modcontext); $slots = $structure->update_page_break($quiz, $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; case 'updatedependency': require_capability('mod/quiz:manage', $modcontext); $slot = $structure->get_slot_by_id($id); $value = (bool) $value; $structure->update_question_dependency($slot->id, $value);
/** * Convert the raw grade stored in $attempt into a grade out of the maximum * grade for this quiz. * * @param float $rawgrade the unadjusted grade, fof example $attempt->sumgrades * @param object $quiz the quiz 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 quiz_rescale_grade($rawgrade, $quiz, $format = true) { if (is_null($rawgrade)) { $grade = null; } else if ($quiz->sumgrades >= 0.000005) { $grade = $rawgrade * $quiz->grade / $quiz->sumgrades; } else { $grade = 0; } if ($format === 'question') { $grade = quiz_format_question_grade($quiz, $grade); } else if ($format) { $grade = quiz_format_grade($quiz, $grade); } return $grade; }
/** * Convert the raw grade stored in $attempt into a grade out of the maximum * grade for this quiz. * * @param float $rawgrade the unadjusted grade, fof example $attempt->sumgrades * @param object $quiz the quiz object. Only the fields grade, sumgrades, decimalpoints and questiondecimalpoints are used. * @param mixed $round false = don't round, true = round using quiz_format_grade, 'question' = round using quiz_format_question_grade. * @return float the rescaled grade. */ function quiz_rescale_grade($rawgrade, $quiz, $round = true) { if ($quiz->sumgrades != 0) { $grade = $rawgrade * $quiz->grade / $quiz->sumgrades; if ($round === 'question') { // === really necessary here true == 'question' is true in PHP! $grade = quiz_format_question_grade($quiz, $grade); } else { if ($round) { $grade = quiz_format_grade($quiz, $grade); } } } else { $grade = 0; } return $grade; }
/** * Display the 'marked out of' information for a question. * Along with the regrade action. * @param \stdClass $quiz the quiz settings from the database. * @param \stdClass $question data from the question and quiz_slots tables. * @return string HTML to output. */ public function marked_out_of_field($quiz, $question) { if ($question->length == 0) { $output = html_writer::span('', 'instancemaxmark decimalplaces_' . quiz_get_grade_format($quiz)); $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(quiz_format_question_grade($quiz, $question->maxmark), 'instancemaxmark decimalplaces_' . quiz_get_grade_format($quiz), array('title' => get_string('maxmark', 'quiz'))); $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', 'quiz')))); return html_writer::span($output, 'instancemaxmarkcontainer'); }
/** * Format an entry in an average row. * @param object $record with fields grade and numaveraged */ protected function format_average($record, $question = false) { if (is_null($record->grade)) { $average = '-'; } else { if ($question) { $average = quiz_format_question_grade($this->quiz, $record->grade); } else { $average = quiz_format_grade($this->quiz, $record->grade); } } if ($this->download) { return $average; } else { if (is_null($record->numaveraged) || $record->numaveraged == 0) { return html_writer::tag('span', html_writer::tag('span', $average, array('class' => 'average')), array('class' => 'avgcell')); } else { return html_writer::tag('span', html_writer::tag('span', $average, array('class' => 'average')) . ' ' . html_writer::tag('span', '(' . $record->numaveraged . ')', array('class' => 'count')), array('class' => 'avgcell')); } } }
/** * Return the grade obtained on a particular question, if the user is permitted to see it. * You must previously have called load_question_states to load the state data about this question. * * @param integer $questionid question id of a question that belongs to this quiz. * @return string the formatted grade, to the number of decimal places specified by the quiz. */ public function get_question_score($questionid) { $options = $this->get_render_options($questionid); if ($options->scores) { return quiz_format_question_grade($this->quiz, $this->states[$questionid]->last_graded->grade); } else { return ''; } }
echo json_encode(array('instancemaxmark' => quiz_format_question_grade($quiz, $slot->maxmark))); break; case 'updatemaxmark': require_capability('mod/quiz:manage', $modcontext); $slot = $structure->get_slot_by_id($id); if ($structure->update_slot_maxmark($slot, $maxmark)) { // Grade has really changed. quiz_delete_previews($quiz); quiz_update_sumgrades($quiz); quiz_update_all_attempt_sumgrades($quiz); quiz_update_all_final_grades($quiz); quiz_update_grades($quiz, 0, true); } echo json_encode(array('instancemaxmark' => quiz_format_question_grade($quiz, $maxmark), 'newsummarks' => quiz_format_grade($quiz, $quiz->sumgrades))); break; case 'linkslottopage': require_capability('mod/quiz:manage', $modcontext); $slots = $structure->update_page_break($quiz, $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;