protected function scale_to_grade($scaleid, $item) {
        global $DB;

        /** @var cache of numbers of scale items */
        static $numofscaleitems = array();

        if (!isset($numofscaleitems[$scaleid])) {
            $scale = $DB->get_field('scale', 'scale', array('id' => $scaleid), MUST_EXIST);
            $items = explode(',', $scale);
            $numofscaleitems[$scaleid] = count($items);
            unset($scale);
            unset($items);
        }

        if ($numofscaleitems[$scaleid] <= 1) {
            throw new coding_exception('Invalid scale definition, no scale items found');
        }

        if ($item <= 0 or $numofscaleitems[$scaleid] < $item) {
            throw new coding_exception('Invalid scale item number');
        }

        return ($item - 1) / ($numofscaleitems[$scaleid] - 1);
    }