public static function add_type($extension, $mimetype, $coreicon,
            array $groups = array(), $corestring = '', $customdescription = '',
            $defaulticon = false) {
        // Check for blank extensions or incorrectly including the dot.
        $extension = (string)$extension;
        if ($extension === '' || $extension[0] === '.') {
            throw new coding_exception('Invalid extension .' . $extension);
        }

        // Check extension not already used.
        $mimetypes = get_mimetypes_array();
        if (array_key_exists($extension, $mimetypes)) {
            throw new coding_exception('Extension ' . $extension . ' already exists');
        }

        // For default icon, check there isn't already something with default icon
        // set for that MIME type.
        if ($defaulticon) {
            foreach ($mimetypes as $type) {
                if ($type['type'] === $mimetype && !empty($type['defaulticon'])) {
                    throw new coding_exception('MIME type ' . $mimetype .
                            ' already has a default icon set');
                }
            }
        }

        // Get existing custom filetype list.
        $customs = self::get_custom_types();

        // Check if there's a 'deleted' entry for the extension, if so then get
        // rid of it.
        foreach ($customs as $key => $custom) {
            if ($custom->extension === $extension) {
                unset($customs[$key]);
            }
        }

        // Set up config record for new type.
        $newtype = self::create_config_record($extension, $mimetype, $coreicon, $groups,
                $corestring, $customdescription, $defaulticon);

        // See if there's a default value with this extension.
        $needsadding = true;
        $defaults = self::get_default_types();
        if (array_key_exists($extension, $defaults)) {
            // If it has the same values, we don't need to add it.
            $defaultvalue = $defaults[$extension];
            $modified = (array)$newtype;
            unset($modified['extension']);
            ksort($defaultvalue);
            ksort($modified);
            if ($modified === $defaultvalue) {
                $needsadding = false;
            }
        }

        // Add to array and set in config.
        if ($needsadding) {
            $customs[] = $newtype;
        }
        self::set_custom_types($customs);
    }