lib.php 9.64 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-textbox
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9
10
11
12
13
 *
 */

defined('INTERNAL') || die();

14
class PluginBlocktypeNavigation extends MaharaCoreBlocktype {
15
16
17
18
19
20
21
22
23
24

    public static function get_title() {
        return get_string('title', 'blocktype.navigation');
    }

    public static function get_description() {
        return get_string('description', 'blocktype.navigation');
    }

    public static function get_categories() {
25
        return array('general' => 20000);
26
27
28
29
30
31
32
33
34
35
    }

     /**
     * Optional method. If exists, allows this class to decide the title for
     * all blockinstances of this type
     */
    public static function get_instance_title(BlockInstance $bi) {
        $configdata = $bi->get('configdata');

        if (!empty($configdata['collection'])) {
36
            return $bi->get_data('collection', (int) $configdata['collection'])->get('name');
37
38
39
40
        }
        return '';
    }

41
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
42
43
44
45
        $configdata = $instance->get('configdata');
        $smarty = smarty_core();

        if (!empty($configdata['collection'])) {
46
            $views = $instance->get_data('collection', (int) $configdata['collection'])->views();
47
48
            if (!empty($views)) {
                $smarty->assign('views', $views['views']);
49
50
51
52
53
54
            }
        }
        $smarty->assign('currentview',$instance->get('view'));
        return $smarty->fetch('blocktype:navigation:navigation.tpl');
    }

55
56
57
58
59
60
    // Called by $instance->get_data('collection', ...).
    public static function get_instance_collection($id) {
        require_once('collection.php');
        return new Collection($id);
    }

61
62
63
64
    public static function has_instance_config() {
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
65
    public static function instance_config_form(BlockInstance $instance) {
66
67
        $configdata = $instance->get('configdata');

68
69
70
71
        $view = $instance->get_view();
        $groupid = $view->get('group');
        $institutionid = $view->get('institution');
        $userid = $view->get('owner');
72
        $urlparams['new'] = 1;
73
74
        if (!empty($groupid)) {
            $where = 'c.group = ?'; $values = array($groupid);
75
            $urlparams['group'] = $groupid;
76
77
78
        }
        else if (!empty($institutionid)) {
            $where = 'c.institution = ?'; $values = array($institutionid);
79
            $urlparams['institution'] = $institutionid;
80
81
82
83
        }
        else {
            $where = 'c.owner = ?'; $values = array($userid);
        }
84
85
86
        ($collections = get_records_sql_array("
            SELECT c.id, c.name
                FROM {collection} c
87
88
            WHERE " . $where . "
            ORDER BY c.name, c.ctime ASC", $values))
89
90
91
92
            || ($collections = array());

        $default = false;
        $options = array();
93
94
95
96
97
98
        if (!empty($collections)) {
            foreach ($collections as $collection) {
                if (!$default) { // need to have an initially selected item
                    $default = $collection->id;
                }
                $options[$collection->id] = $collection->name;
99
            }
100
            $elements = array(
101
102
103
104
105
106
107
108
                'collection' => array(
                    'type' => 'select',
                    'title' => get_string('collection','blocktype.navigation'),
                    'rules' => array('required' => true),
                    'options' => $options,
                    'defaultvalue' => !empty($configdata['collection']) ? $configdata['collection'] : $default,
                ),
            );
109
110
111
112
113
114
115
116
117
            if ($pageincollection = $view->get_collection()) {
                $elements['copytoall'] = array(
                    'type' => 'switchbox',
                    'title' => get_string('copytoall', 'blocktype.navigation'),
                    'description' => get_string('copytoalldesc', 'blocktype.navigation'),
                    'defaultvalue' => false,
                );
            }
            return $elements;
118
119
        }
        else {
120
121
122
123
            $baseurl = get_config('wwwroot') . 'collection/edit.php';
            if ($urlparams) {
                $baseurl .= '?' . http_build_query($urlparams);
            }
124
125
126
127
            return array(
                'nocollections' => array(
                    'type'  => 'html',
                    'title' => get_string('collection', 'blocktype.navigation'),
128
                    'description' => get_string('nocollections1', 'blocktype.navigation', $baseurl),
129
130
131
                    'value' => '',
                ),
            );
132
133
134
135
        }

    }

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    public static function instance_config_save($values, $instance) {
        if (!empty($values['copytoall'])) {
            $view = $instance->get('view_obj');
            if ($collection = $view->get_collection()) {
                foreach ($viewids = $collection->get_viewids() as $vid) {
                    if ($vid !== (int)$view->get('id')) {
                        $needsblock = true;
                        if ($blocks = get_records_sql_array("SELECT id FROM {block_instance} WHERE blocktype = ? AND view = ?", array('navigation', $vid))) {
                            foreach ($blocks as $block) {
                                // need to check the block to see if it's for this navigation
                                $bi = new BlockInstance($block->id);
                                $configdata = $bi->get('configdata');
                                if (!empty($configdata['collection']) && $configdata['collection'] == $values['collection']) {
                                    $needsblock = false;
                                }
                            }
                        }
                        if ($needsblock) {
                            // need to add new navigation block
                            $otherview = new View($vid);
                            $bi = new BlockInstance(0,
                                array(
                                    'blocktype'  => 'navigation',
159
                                    'title'      => $values['title'],
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
                                    'row'        => $instance->get('row'),
                                    'column'     => $instance->get('column'),
                                    'order'      => 1,
                                    'configdata' => array(
                                        'collection' => $values['collection'],
                                        'retractable' => $values['retractable'],
                                        'retractedonload' => $values['retractedonload'],
                                    ),
                                )
                            );
                            $otherview->addblockinstance($bi);
                        }
                    }
                }
            }
        }
        unset($values['copytoall']);
        return $values;
    }

180
181
182
183
    public static function default_copy_type() {
        return 'full';
    }

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

    /**
     * Change the collection ID format to match the ID format we use in Leap2A,
     * e.g.: portfolio:collection23
     *
     * @param BlockInstance $bi The blockinstance to export the config for.
     * @return array The config for the blockinstance
     */
    public static function export_blockinstance_config_leap(BlockInstance $bi) {
        $jsonconfigdata = parent::export_blockinstance_config_leap($bi);
        if (isset($jsonconfigdata['collection'])) {
            // It should be a collection...
            $collection = json_decode($jsonconfigdata['collection']);
            if (is_array($collection)) {
                $collection = $collection[0];
            }
            $jsonconfigdata['collection'] = json_encode(array('portfolio:collection' . (int) $collection));
        }
        return $jsonconfigdata;
    }


    /**
     * After a leap2a import, rewrite the block instance's collection ID to the collection's new ID.
     * (If the collection was part of this import. If it's not, just remove it.)
     *
     * @param int $blockinstanceid
     * @param PluginLeapImport $importer
     */
    public static function import_rewrite_blockinstance_relationships_leap($blockinstanceid, $importer) {
        $bi = new BlockInstance($blockinstanceid);
        $configdata = $bi->get('configdata');

        // Rewrite the collection ID from the old one to the new one.
        if (isset($configdata['collection'])) {
            $oldcollectionid = $configdata['collection'];

            // Backwards-compatibility for Leap2a files before we started rewriting the collection ID
            if (strpos($oldcollectionid, 'portfolio:collection') !== 0) {
                $oldcollectionid = 'portfolio:collection' . (int) $oldcollectionid;
            }

            if (isset($importer->collectionids[$oldcollectionid])) {
                // If the collection was present in this import, point to its new ID
                $configdata['collection'] = $importer->collectionids[$oldcollectionid];
            }
            else {
                // If the collection was not present, then deactivate this block
                // TODO: Make some guesses about what it should point at?
                unset($configdata['collection']);
            }
        }
        $bi->set('configdata', $configdata);
        $bi->commit();
    }

240
241
242
243
244
245
246
247
248
    /**
     * Shouldn't be linked to any artefacts via the view_artefacts table.
     *
     * @param BlockInstance $instance
     * @return multitype:
     */
    public static function get_artefacts(BlockInstance $instance) {
        return array();
    }
249
}