moodle/moodle · error · moodle_exception

Bad slot ID

Error message

Bad slot ID 

What it means

Thrown by structure::move_slot() when the id of the slot to move ($idmove) does not correspond to any slot record in this quiz — get_slot_by_id() returns empty. Uses \moodle_exception with a raw message 'Bad slot ID <id>' rather than a language string.

Solutions

  1. Verify the slot id belongs to the current quiz (query quiz_slots by id and check the quizid).
  2. Reload the quiz edit page / re-fetch the structure to get fresh slot ids before moving.
  3. Correct the id passed to move_slot(); ensure it comes from get_slot_by_id()/structure listing for this quiz.
  4. If concurrent edits are common, re-fetch and retry, or handle empty lookups gracefully before calling.

Example fix

// before
$structure->move_slot($slotid, $moveafter); // $slotid from stale UI
// after
$slot = $structure->get_slot_by_id($slotid);
if (!empty($slot)) {
    $structure->move_slot($slotid, $moveafter);
}
Defensive patterns

Strategy: validation

Validate before calling

$slot = $structure->get_slot_by_id($slotid);
if (empty($slot)) { // abort or refetch structure before moving
}

Try / catch

try {
    $structure->move_slot($slotid, $moveafter);
} catch (\moodle_exception $e) {
    if (str_starts_with($e->getMessage(), 'Bad slot ID')) {
        // reload structure and retry with fresh ids
    }
}

Prevention

When it happens

Trigger: Calling move_slot() with a slot id that does not exist, belongs to a different quiz, or was already deleted; passing an invalid/garbage id from client JS.

Common situations: Stale page data after another user restructured the quiz; AJAX sends an outdated slot id; ids mixed up between quizzes; copy-paste of a slotcord id from the wrong quiz id.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/6702f83573e4acfb. Report an issue: GitHub.

Appendix: source

Thrown at public/mod/quiz/classes/structure.php:854

    /**
     * Move a slot from its current location to a new location.
     *
     * After calling this method, this class will be in an invalid state, and
     * should be discarded if you want to manipulate the structure further.
     *
     * @param int $idmove id of slot to be moved
     * @param int $idmoveafter id of slot to come before slot being moved
     * @param int $page new page number of slot being moved
     */
    public function move_slot($idmove, $idmoveafter, $page) {
        global $DB;

        $this->check_can_be_edited();

        $movingslot = $this->get_slot_by_id($idmove);
        if (empty($movingslot)) {
            throw new \moodle_exception('Bad slot ID ' . $idmove);
        }
        $movingslotnumber = (int) $movingslot->slot;

        // Empty target slot means move slot to first.
        if (empty($idmoveafter)) {
            $moveafterslotnumber = 0;
        } else {
            $moveafterslotnumber = (int) $this->get_slot_by_id($idmoveafter)->slot;
        }

        // If the action came in as moving a slot to itself, normalise this to
        // moving the slot to after the previous slot.
        if ($moveafterslotnumber == $movingslotnumber) {
            $moveafterslotnumber = $moveafterslotnumber - 1;
        }

        $followingslotnumber = $moveafterslotnumber + 1;
        // Prevent checking against non-existence slot when already at the last slot.

View on GitHub (pinned to 48ea8c3322)