Skip to content

Commit

Permalink
MDL-20636 Reveiw all throw statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Feb 23, 2011
1 parent f7970e3 commit 88f0eb1
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 80 deletions.
1 change: 1 addition & 0 deletions lang/en/question.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@
$string['correctfeedback'] = 'For any correct response';
$string['decimalplacesingrades'] = 'Decimal places in grades';
$string['defaultmark'] = 'Default mark';
$string['errorsavingflags'] = 'Error saving the flag state.';
$string['feedback'] = 'Feedback';
$string['fillincorrect'] = 'Fill in correct responses';
$string['flagged'] = 'Flagged';
Expand Down
37 changes: 10 additions & 27 deletions mod/quiz/attemptlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,9 @@ function __construct($quiz, $cm, $course, $getcontext = true) {
static public function create($quizid, $userid) {
global $DB;

if (!$quiz = $DB->get_record('quiz', array('id' => $quizid))) {
throw new moodle_exception('invalidquizid', 'quiz');
}
if (!$course = $DB->get_record('course', array('id' => $quiz->course))) {
throw new moodle_exception('invalidcoursemodule');
}
if (!$cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id)) {
throw new moodle_exception('invalidcoursemodule');
}
$quiz = $DB->get_record('quiz', array('id' => $quizid), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id, false, MUST_EXIST);

// Update quiz with override information
$quiz = quiz_update_effective_access($quiz, $userid);
Expand Down Expand Up @@ -385,18 +379,10 @@ static protected function create_helper($conditions) {
// quiz_upgrade_states($attempt);
// }

if (!$attempt = $DB->get_record('quiz_attempts', $conditions)) {
throw new moodle_exception('invalidattemptid', 'quiz');
}
if (!$quiz = $DB->get_record('quiz', array('id' => $attempt->quiz))) {
throw new moodle_exception('invalidquizid', 'quiz');
}
if (!$course = $DB->get_record('course', array('id' => $quiz->course))) {
throw new moodle_exception('invalidcoursemodule');
}
if (!$cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id)) {
throw new moodle_exception('invalidcoursemodule');
}
$attempt = $DB->get_record('quiz_attempts', $conditions, '*', MUST_EXIST);
$quiz = $DB->get_record('quiz', array('id' => $attempt->quiz), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id, false, MUST_EXIST);

// Update quiz with override information
$quiz = quiz_update_effective_access($quiz, $attempt->userid);
Expand Down Expand Up @@ -1002,9 +988,8 @@ public function process_all_actions($timestamp) {
if ($this->attempt->timefinish) {
$this->attempt->sumgrades = $this->quba->get_total_mark();
}
if (!$DB->update_record('quiz_attempts', $this->attempt)) {
throw new moodle_quiz_exception($this->get_quizobj(), 'saveattemptfailed');
}
$DB->update_record('quiz_attempts', $this->attempt);

if (!$this->is_preview() && $this->attempt->timefinish) {
quiz_save_best_grade($this->get_quiz(), $this->get_userid());
}
Expand All @@ -1029,9 +1014,7 @@ public function finish_attempt($timestamp) {
$this->attempt->timemodified = $timestamp;
$this->attempt->timefinish = $timestamp;
$this->attempt->sumgrades = $this->quba->get_total_mark();
if (!$DB->update_record('quiz_attempts', $this->attempt)) {
throw new moodle_quiz_exception($this->get_quizobj(), 'saveattemptfailed');
}
$DB->update_record('quiz_attempts', $this->attempt);

if (!$this->is_preview()) {
quiz_save_best_grade($this->get_quiz());
Expand Down
8 changes: 2 additions & 6 deletions mod/quiz/startattempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@

// Check login and sesskey.
require_login($quizobj->get_courseid(), false, $quizobj->get_cm());
if (!confirm_sesskey()) {
throw new moodle_exception('confirmsesskeybad', 'error', $quizobj->view_url());
}
require_sesskey();
$PAGE->set_pagelayout('base');

// if no questions have been set up yet redirect to edit.php
Expand Down Expand Up @@ -188,9 +186,7 @@
$transaction = $DB->start_delegated_transaction();
question_engine::save_questions_usage_by_activity($quba);
$attempt->uniqueid = $quba->get_id();
if (!$attempt->id = $DB->insert_record('quiz_attempts', $attempt)) {
throw new moodle_quiz_exception($quizobj, 'newattemptfail');
}
$attempt->id = $DB->insert_record('quiz_attempts', $attempt);

// Log the new attempt.
if ($attempt->preview) {
Expand Down
4 changes: 2 additions & 2 deletions question/behaviour/behaviourbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct(question_attempt $qa, $preferredbehaviour) {
$this->question = $qa->get_question();
$requiredclass = $this->required_question_definition_type();
if (!$this->question instanceof $requiredclass) {
throw new Exception('This behaviour (' . $this->get_name() .
throw new coding_exception('This behaviour (' . $this->get_name() .
') cannot work with this question (' . get_class($this->question) . ')');
}
}
Expand Down Expand Up @@ -543,7 +543,7 @@ public function process_save(question_attempt_pending_step $pendingstep) {
if ($this->qa->get_state()->is_finished()) {
return question_attempt::DISCARD;
} else if (!$this->qa->get_state()->is_active()) {
throw new Exception('Question is not active, cannot process_actions.');
throw new coding_exception('Question is not active, cannot process_actions.');
}

if ($this->is_same_response($pendingstep)) {
Expand Down
2 changes: 1 addition & 1 deletion question/behaviour/informationitem/behaviour.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function summarise_action(question_attempt_step $step) {

public function process_comment(question_attempt_pending_step $pendingstep) {
if ($pendingstep->has_behaviour_var('mark')) {
throw new Exception('Information items cannot be graded.');
throw new coding_exception('Information items cannot be graded.');
}
return parent::process_comment($pendingstep);
}
Expand Down
6 changes: 3 additions & 3 deletions question/behaviour/missing/behaviour.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public function summarise_action(question_attempt_step $step) {
}

public function init_first_step(question_attempt_step $step) {
throw new Exception('The behaviour used for this question is not available. No processing is possible.');
throw new coding_exception('The behaviour used for this question is not available. No processing is possible.');
}

public function process_action(question_attempt_pending_step $pendingstep) {
throw new Exception('The behaviour used for this question is not available. No processing is possible.');
throw new coding_exception('The behaviour used for this question is not available. No processing is possible.');
}

public function get_min_fraction() {
throw new Exception('The behaviour used for this question is not available. No processing is possible.');
throw new coding_exception('The behaviour used for this question is not available. No processing is possible.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function make_standard_om_question() {
global $DB;
$engineid = $DB->get_field('question_opaque_engines', 'MIN(id)', array());
if (empty($engineid)) {
throw new Exception('Cannot test Opaque. No question engines configured.');
throw new coding_exception('Cannot test Opaque. No question engines configured.');
}

question_bank::load_question_definition_classes('opaque');
Expand Down
8 changes: 4 additions & 4 deletions question/engine/bank.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static function get_qtype($qtypename, $mustexist = true) {
$file = get_plugin_directory('qtype', $qtypename) . '/questiontype.php';
if (!is_readable($file)) {
if ($mustexist || $qtypename == 'missingtype') {
throw new Exception('Unknown question type ' . $qtypename);
throw new coding_exception('Unknown question type ' . $qtypename);
} else {
return self::get_qtype('missingtype');
}
Expand Down Expand Up @@ -186,7 +186,7 @@ public static function load_question_definition_classes($qtypename) {
}
$file = $CFG->dirroot . '/question/type/' . $qtypename . '/question.php';
if (!is_readable($file)) {
throw new Exception('Unknown question type (no definition) ' . $qtypename);
throw new coding_exception('Unknown question type (no definition) ' . $qtypename);
}
include_once($file);
self::$loadedqdefs[$qtypename] = 1;
Expand Down Expand Up @@ -256,7 +256,7 @@ public static function end_unit_test() {

private static function return_test_question_data($questionid) {
if (!isset(self::$testdata[$questionid])) {
throw new Exception('question_bank::return_test_data(' . $questionid .
throw new coding_exception('question_bank::return_test_data(' . $questionid .
') called, but no matching question has been loaded by load_test_data.');
}
return self::$testdata[$questionid];
Expand All @@ -269,7 +269,7 @@ private static function return_test_question_data($questionid) {
*/
public static function load_test_question_data(question_definition $question) {
if (!self::$testmode) {
throw new Exception('question_bank::load_test_data called when not in test mode.');
throw new coding_exception('question_bank::load_test_data called when not in test mode.');
}
self::$testdata[$question->id] = $question;
}
Expand Down
18 changes: 7 additions & 11 deletions question/engine/datalib.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function load_question_attempt_step($stepid) {
", array('stepid' => $stepid));

if (!$records) {
throw new Exception('Failed to load question_attempt_step ' . $stepid);
throw new coding_exception('Failed to load question_attempt_step ' . $stepid);
}

return question_attempt_step::load_from_records($records, $stepid);
Expand Down Expand Up @@ -202,7 +202,7 @@ public function load_question_attempt($questionattemptid) {
", array('questionattemptid' => $questionattemptid));

if (!$records) {
throw new Exception('Failed to load question_attempt ' . $questionattemptid);
throw new coding_exception('Failed to load question_attempt ' . $questionattemptid);
}

$record = current($records);
Expand Down Expand Up @@ -259,7 +259,7 @@ public function load_questions_usage_by_activity($qubaid) {
", array('qubaid' => $qubaid));

if (!$records) {
throw new Exception('Failed to load questions_usage_by_activity ' . $qubaid);
throw new coding_exception('Failed to load questions_usage_by_activity ' . $qubaid);
}

return question_usage_by_activity::load_from_records($records, $qubaid);
Expand Down Expand Up @@ -599,9 +599,7 @@ public function update_questions_usage_by_activity(question_usage_by_activity $q
$record->component = addslashes($quba->get_owning_component());
$record->preferredbehaviour = addslashes($quba->get_preferred_behaviour());

if (!$this->db->update_record('question_usages', $record)) {
throw new Exception('Failed to update question_usage_by_activity ' . $record->id);
}
$this->db->update_record('question_usages', $record);
}

/**
Expand All @@ -620,9 +618,7 @@ public function update_question_attempt(question_attempt $qa) {
$record->responsesummary = addslashes($qa->get_response_summary());
$record->timemodified = time();

if (!$this->db->update_record('question_attempts', $record)) {
throw new Exception('Failed to update question_attempt ' . $record->id);
}
$this->db->update_record('question_attempts', $record);
}

/**
Expand Down Expand Up @@ -696,7 +692,7 @@ public function delete_previews($questionid) {
public function update_question_attempt_flag($qubaid, $questionid, $qaid, $slot, $newstate) {
if (!$this->db->record_exists('question_attempts', array('id' => $qaid,
'questionusageid' => $qubaid, 'questionid' => $questionid, 'slot' => $slot))) {
throw new Exception('invalid ids');
throw new moodle_exception('errorsavingflags', 'question');
}

$this->db->set_field('question_attempts', 'flagged', $newstate, array('id' => $qaid));
Expand Down Expand Up @@ -1002,7 +998,7 @@ public function where() {
global $DB;

if (is_null($this->columntotest)) {
throw new coding_exception('Must call another method that before where().');
throw new coding_exception('Must call from_question_attempts before where().');
}
if (empty($this->qubaids)) {
$this->params = array();
Expand Down
32 changes: 16 additions & 16 deletions question/engine/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static function make_archetypal_behaviour($preferredbehaviour, question_a
question_engine::load_behaviour_class($preferredbehaviour);
$class = 'qbehaviour_' . $preferredbehaviour;
if (!constant($class . '::IS_ARCHETYPAL')) {
throw new Exception('The requested behaviour is not actually an archetypal one.');
throw new coding_exception('The requested behaviour is not actually an archetypal one.');
}
return new $class($qa, $preferredbehaviour);
}
Expand Down Expand Up @@ -200,7 +200,7 @@ public static function load_behaviour_class($behaviour) {
}
$file = $CFG->dirroot . '/question/behaviour/' . $behaviour . '/behaviour.php';
if (!is_readable($file)) {
throw new Exception('Unknown question behaviour ' . $behaviour);
throw new coding_exception('Unknown question behaviour ' . $behaviour);
}
include_once($file);
self::$loadedbehaviours[$behaviour] = 1;
Expand Down Expand Up @@ -545,7 +545,7 @@ public static function update_flag($qubaid, $questionid, $qaid, $slot, $checksum
// probably makes it sufficiently difficult for malicious users to toggle
// other users flags.
if ($checksum != question_flags::get_toggle_checksum($qubaid, $questionid, $qaid, $slot)) {
throw new Exception('checksum failure');
throw new moodle_exception('errorsavingflags', 'question');
}

$dm = new question_engine_data_mapper();
Expand Down Expand Up @@ -785,7 +785,7 @@ public function get_attempt_iterator() {
*/
protected function check_slot($slot) {
if (!array_key_exists($slot, $this->questionattempts)) {
throw new exception("There is no question_attempt number $slot in this attempt.");
throw new coding_exception("There is no question_attempt number $slot in this attempt.");
}
}

Expand Down Expand Up @@ -1198,7 +1198,7 @@ public static function load_from_records(&$records, $qubaid) {
while ($record->qubaid != $qubaid) {
$record = next($records);
if (!$record) {
throw new Exception("Question usage $qubaid not found in the database.");
throw new coding_exception("Question usage $qubaid not found in the database.");
}
}

Expand Down Expand Up @@ -1280,10 +1280,10 @@ public function offsetGet($slot) {
return $this->quba->get_question_attempt($slot);
}
public function offsetSet($slot, $value) {
throw new Exception('You are only allowed read-only access to question_attempt::states through a question_attempt_step_iterator. Cannot set.');
throw new coding_exception('You are only allowed read-only access to question_attempt::states through a question_attempt_step_iterator. Cannot set.');
}
public function offsetUnset($slot) {
throw new Exception('You are only allowed read-only access to question_attempt::states through a question_attempt_step_iterator. Cannot unset.');
throw new coding_exception('You are only allowed read-only access to question_attempt::states through a question_attempt_step_iterator. Cannot unset.');
}
}

Expand Down Expand Up @@ -1561,7 +1561,7 @@ public function get_field_prefix() {
*/
public function get_step($i) {
if ($i < 0 || $i >= count($this->steps)) {
throw new Exception('Index out of bounds in question_attempt::get_step.');
throw new coding_exception('Index out of bounds in question_attempt::get_step.');
}
return $this->steps[$i];
}
Expand Down Expand Up @@ -1755,7 +1755,7 @@ public function get_max_mark() {
/** @return number the maximum mark possible for this question attempt. */
public function get_min_fraction() {
if (is_null($this->minfraction)) {
throw new Exception('This question_attempt has not been started yet, the min fraction is not yet konwn.');
throw new coding_exception('This question_attempt has not been started yet, the min fraction is not yet konwn.');
}
return $this->minfraction;
}
Expand Down Expand Up @@ -2207,7 +2207,7 @@ public static function load_from_records(&$records, $questionattemptid,
while ($record->questionattemptid != $questionattemptid) {
$record = next($records);
if (!$record) {
throw new Exception("Question attempt $questionattemptid not found in the database.");
throw new coding_exception("Question attempt $questionattemptid not found in the database.");
}
}

Expand Down Expand Up @@ -2384,10 +2384,10 @@ public function offsetGet($i) {
return $this->qa->get_step($i);
}
public function offsetSet($offset, $value) {
throw new Exception('You are only allowed read-only access to question_attempt::states through a question_attempt_step_iterator. Cannot set.');
throw new coding_exception('You are only allowed read-only access to question_attempt::states through a question_attempt_step_iterator. Cannot set.');
}
public function offsetUnset($offset) {
throw new Exception('You are only allowed read-only access to question_attempt::states through a question_attempt_step_iterator. Cannot unset.');
throw new coding_exception('You are only allowed read-only access to question_attempt::states through a question_attempt_step_iterator. Cannot unset.');
}
}

Expand Down Expand Up @@ -2554,7 +2554,7 @@ public function get_qt_var($name) {
*/
public function set_qt_var($name, $value) {
if ($name[0] != '_') {
throw new Exception('Cannot set question type data ' . $name . ' on an attempt step. You can only set variables with names begining with _.');
throw new coding_exception('Cannot set question type data ' . $name . ' on an attempt step. You can only set variables with names begining with _.');
}
$this->data[$name] = $value;
}
Expand Down Expand Up @@ -2599,7 +2599,7 @@ public function get_behaviour_var($name) {
*/
public function set_behaviour_var($name, $value) {
if ($name[0] != '_') {
throw new Exception('Cannot set question type data ' . $name . ' on an attempt step. You can only set variables with names begining with _.');
throw new coding_exception('Cannot set question type data ' . $name . ' on an attempt step. You can only set variables with names begining with _.');
}
return $this->data['-' . $name] = $value;
}
Expand Down Expand Up @@ -2659,7 +2659,7 @@ public static function load_from_records(&$records, $attemptstepid) {
while ($currentrec->attemptstepid != $attemptstepid) {
$currentrec = next($records);
if (!$currentrec) {
throw new Exception("Question attempt step $attemptstepid not found in the database.");
throw new coding_exception("Question attempt step $attemptstepid not found in the database.");
}
}

Expand Down Expand Up @@ -2750,7 +2750,7 @@ public function get_state() {
}

public function set_state($state) {
throw new Exception('This question has not been started.');
throw new coding_exception('This question has not been started.');
}

public function get_fraction() {
Expand Down
2 changes: 1 addition & 1 deletion question/engine/simpletest/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public static function build_db_records(array $table) {
$records = array();
foreach ($table as $row) {
if (count($row) != count($columns)) {
throw new Exception("Row contains the wrong number of fields.");
throw new coding_exception("Row contains the wrong number of fields.");
}
$rec = new stdClass();
foreach ($columns as $i => $name) {
Expand Down
Loading

0 comments on commit 88f0eb1

Please sign in to comment.