Forums | Mahara Community

Developers /
Blocks and View Types


anonymous profile picture
Deleted user
Posts: 117

18 July 2009, 5:59 PM

I'm a little confused about how the what determines which view types a block may appear in (profile or portfolio).

The database has a table called blocktype_installed_viewtype, but as far as I can tell, it isn't actually used anywhere. Is this just something that hadn't been coded yet?

Then, in the lib.php file for each blocktype, there's the get_viewtypes() function:

    public static function get_viewtypes() {
        return array('profile', 'portfolio');
    }

But, this doesn't appear to be used anywhere, either.

Finally, there's the allowed_in_view() function:

    public static function allowed_in_view(View $view) {
        return $view->get('owner') != null;
    }

This one definitely does restrict where a view can appear.

So, if I want to restrict a blocktype to, say, any view other than the profile, I would just need to specify that in the allowed_in_view() function? Is the database table used at all or is that something that was started but not completed yet?

Thanks

anonymous profile picture
Deleted user
Posts: 228

19 July 2009, 1:34 AM

Hi Stephen!

The get_viewtypes method is used, and that's what populates the blocktype_installed_viewtype table - it happens when a blocktype plugin is installed or upgraded, take a look at lib/upgrade.php around line 863, in the install_blocktype_viewtypes_for_plugin method.

About the other method, allowed_in_view - that's related to copying views mostly.

Hope that helps!

Penny

anonymous profile picture
Deleted user
Posts: 117

19 July 2009, 10:36 AM

Right. get_viewtypes does install the viewtypes into the database, but the blocktype_installed_viewtype table is never queried when displaying the available blocktypes when creating or editing a view. It is queried nowhere other than in lib/upgrade.php.  allowed_in_view seems to be the only thing that affects whether or not a blocktype is available to a view.

To make it so blocktype_installed_viewtype is queried and the blocktypes will only appear for the view types specified in the table, I made the following changes:

In lib/view.php I changed:

    private function get_category_data() {
        if (isset($this->category_data)) {
            return $this->category_data;
        }

        require_once(get_config('docroot') . '/blocktype/lib.php');
        $categories = array();
        foreach (get_records_array('blocktype_installed_category') as $blocktypecategory) {

to:

    private function get_category_data() {
        if (isset($this->category_data)) {
            return $this->category_data;
        }

        require_once(get_config('docroot') . '/blocktype/lib.php');
        $categories = array();
        $sql = 'SELECT btic.* 
            FROM {blocktype_installed_category} btic
            JOIN {blocktype_installed_viewtype} btiv ON btiv.blocktype = btic.blocktype
             WHERE btiv.viewtype = ?';
        foreach (get_records_sql_array($sql, array($this->get('type'))) as $blocktypecategory) {

and in blocktype/lib.php I changed:

    public static function get_blocktypes_for_category($category, View $view) {
        $sql = 'SELECT bti.name, bti.artefactplugin
            FROM {blocktype_installed} bti
            JOIN {blocktype_installed_category} btic ON btic.blocktype = bti.name
            WHERE btic.category = ?
            ORDER BY bti.name';
        if (!$bts = get_records_sql_array($sql, array($category))) {
            return false;
        }

 to:

    public static function get_blocktypes_for_category($category, View $view) {
        $sql = 'SELECT bti.name, bti.artefactplugin
            FROM {blocktype_installed} bti
            JOIN {blocktype_installed_category} btic ON btic.blocktype = bti.name
            JOIN {blocktype_installed_viewtype} btiv ON btiv.blocktype = bti.name
            WHERE btic.category = ? AND btiv.viewtype = ?
            ORDER BY bti.name';
        if (!$bts = get_records_sql_array($sql, array($category, $view->get('type')))) {
            return false;
        }

That seems to correct the issue.

anonymous profile picture
Deleted user
Posts: 228

19 July 2009, 11:38 AM

Hi Stephan,

It might pay to have a look in unstable (the "master" branch in git), because there are JOINS on that table in both lib/view.php and blocktype/lib.php.

Cheers,

Penny

anonymous profile picture
Deleted user
Posts: 117

19 July 2009, 12:58 PM

Yeah, I don't use git. Prior to making the changes, I made sure the table wasn't being used at all by completely renaming it and it had absolutely no effect on the blocktypes being displayed.

Both functions need the JOINs.

get_category_data needs the join to ensure that there's at least one blocktype for each category returned. Otherwise the blocktype category tab will still show up, but with no blocktypes listed.

get_blocktypes_for_category needs it, obviously, to make sure only the blocktypes available for that view type are displayed.

I could probably alter the code more to make it so there's only the need for one join, but the tables are so small, there's very little performance impact for doing the joins anyway.

anonymous profile picture
Deleted user
Posts: 808

19 July 2009, 6:30 PM

Yeah, I noticed a while ago that blocktype_installed_viewtype was never used.  It was fixed on the master branch about a month ago.  I guess I could dig out those patches and see if they apply easily to stable, otherwise I'll post them up here for you.
anonymous profile picture
Deleted user
Posts: 808

19 July 2009, 6:52 PM

They don't apply cleanly to stable, I think because of changes we made (for 1.2) so that blocktypes can be enabled/disabled.

But yes I think the exact changes you described are what I did. I uploaded the patches here in case you find them useful.

anonymous profile picture
Deleted user
Posts: 117

20 July 2009, 3:41 PM

No problem. The changes I did are working great. Thanks!
anonymous profile picture
Deleted user
Posts: 92

17 May 2010, 3:52 AM

Hi , stephen

I have also the problem of  view the information my view.

 

Can you help me?

anonymous profile picture
Deleted user
Posts: 117

19 May 2010, 4:43 AM

I don't understand what you are asking.  This thread is about defining which block types can appear in which view types.  There is a database table in 1.1.x that looks like it was originally supposed to make those determinations, but was never implemented in the code. The code that I added above enables the use of that table.

19 results