lib.php 10.2 KB
Newer Older
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-resumefield
6
 * @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 PluginBlocktypeResumefield extends MaharaCoreBlocktype {
15
16
17
18
19
20
21
22
23
24

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

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

    public static function get_categories() {
25
        return array('internal' => 30000);
26
27
    }

28
29
30
31
    public static function get_viewtypes() {
        return array('dashboard', 'portfolio', 'profile');
    }

32
33
34
35
36
    public static function get_blocktype_type_content_types() {
        return array('resumefield' => array('resume'));
    }

    /**
37
38
39
40
41
42
43
     * 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['artefactid'])) {
44
            return $bi->get_artefact_instance($configdata['artefactid'])->get('title');
45
46
47
48
        }
        return '';
    }

49
50
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
        require_once(get_config('docroot') . 'artefact/lib.php');
51
52
        $smarty = smarty_core();
        $configdata = $instance->get('configdata');
53
        $configdata['viewid'] = $instance->get('view');
Lisa Seeto's avatar
Lisa Seeto committed
54
55
56
        $configdata['showcommentcount'] = true;
        $configdata['editing'] = $editing;
        $configdata['blockid'] = $instance->get('id');
57
58
59

        // Get data about the resume field in this blockinstance
        if (!empty($configdata['artefactid'])) {
60
            $resumefield = $instance->get_artefact_instance($configdata['artefactid']);
61
            $rendered = $resumefield->render_self($configdata);
62
63
            $result = $rendered['html'];
            if (!empty($rendered['javascript'])) {
64
                $result .= '<script>' . $rendered['javascript'] . '</script>';
65
            }
66
            $smarty->assign('content', $result);
67
        }
68
69
70
71
72
73
74
75
76
        else if ($editing && !empty($configdata['templateid'])) {
            $result = '<p><strong>' . get_string($configdata['templateid'], 'artefact.resume') . '</strong></p>';
            $smarty->assign('content', $result);
        }
        else {
            $smarty->assign('editing', $editing);
            $smarty->assign('nodata', get_string('noresumeitemselectone', 'blocktype.resume/resumefield'));
        }
        return $smarty->fetch('blocktype:resumefield:content.tpl');;
77
78
    }

79
    public static function has_instance_config(BlockInstance $instance) {
80
81
82
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
83
    public static function instance_config_form(BlockInstance $instance) {
84
85
86
        $configdata = $instance->get('configdata');

        $form = array();
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
        $owner = $instance->get_view()->get('owner');
        if ($owner) {
            // Which resume field does the user want
            $form[] = self::artefactchooser_element((isset($configdata['artefactid'])) ? $configdata['artefactid'] : null);
            $form['message'] = array(
                'type' => 'html',
                'value' => '<p class="alert alert-info">' . get_string('filloutyourresume', 'blocktype.resume/resumefield', '<a href="' . get_config('wwwroot') . 'artefact/resume/index.php">', '</a>') .'</p>',
            );
            $form['tags'] = array(
                'type'         => 'tags',
                'title'        => get_string('tags'),
                'description'  => get_string('tagsdescblock'),
                'defaultvalue' => $instance->get('tags'),
                'help'         => false,
            );
        }
        else {
            if (isset($configdata['templateid']) && !empty($configdata['templateid'])) {
                safe_require('artefact', 'resume');
                $types = PluginArtefactResume::get_artefact_types();
                $configdata['artefactid'] = array_search($configdata['templateid'], $types);
                unset($configdata['templateid']);
            }
            $form[] = self::artefactchooser_element((isset($configdata['artefactid'])) ? $configdata['artefactid'] : null, $owner);
            $form['blocktemplatehtml'] = array(
                'type' => 'html',
                'value' => get_string('blockinstanceconfigownerchange', 'mahara'),
            );
            $form['blocktemplate'] = array(
                'type'    => 'hidden',
                'value'   => 1,
            );
        }
120
121
122
        return $form;
    }

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    public static function instance_config_save($values, $instance) {
        if (isset($values['blocktemplate']) && !empty($values['blocktemplate'])) {
            // Need to adjust info to be a template
            $owner = $instance->get_view()->get('owner');
            $values['templateid'] = null;
            $element = self::artefactchooser_element(null, $owner);
            // Because the settings are radio we can treat empty response as picking the first option
            if (!empty($element['artefacttypes'][$values['artefactid']])) {
                $values['templateid'] = $element['artefacttypes'][$values['artefactid']];
            }
            else {
                $values['templateid'] = $element['artefacttypes'][0];
            }
            unset($values['artefactid']);
        }

139
140
141
142
        unset($values['message']);
        return $values;
    }

143
    // TODO: make decision on whether this should be abstract or not
144
    public static function artefactchooser_element($default=null,$owner=true) {
145
146
147
148
149
150
151
        safe_require('artefact', 'resume');
        return array(
            'name'  => 'artefactid',
            'type'  => 'artefactchooser',
            'title' => get_string('fieldtoshow', 'blocktype.resume/resumefield'),
            'defaultvalue' => $default,
            'blocktype' => 'resumefield',
152
            'blocktemplate' => empty($owner),
153
154
            'limit'     => 655360, // 640K profile fields is enough for anyone!
            'selectone' => true,
155
            'search'    => false,
156
157
158
159
160
161
            'artefacttypes' => PluginArtefactResume::get_artefact_types(),
            'template'  => 'artefact:resume:artefactchooser-element.tpl',
        );
    }

    /**
Aaron Wells's avatar
Aaron Wells committed
162
163
     * Deliberately enforce _no_ sort order. The database will return them in
     * the order they were inserted, which means roughly the order that they
164
165
166
167
168
     * are listed in the profile screen
     */
    public static function artefactchooser_get_sort_order() {
        return '';
    }
Richard Mansfield's avatar
Richard Mansfield committed
169

170
    public static function rewrite_blockinstance_config(View $view, $configdata) {
171
        $artefactid = null;
172
        if ($view->get('owner') !== null) {
173
            $artefacttype = null;
174
175
176
177
178
179
180
181
182
            if (!empty($configdata['blocktemplate'])) {
                if (!empty($configdata['templateid'])) {
                    $artefacttype = $configdata['templateid'];
                }
                unset($configdata['blocktemplatehtml']);
                unset($configdata['templateid']);
                unset($configdata['blocktemplate']);
            }
            else if (!empty($configdata['artefactid'])) {
183
184
                $artefacttype = get_field('artefact', 'artefacttype', 'id', $configdata['artefactid']);
            }
185

186
            if ($artefacttype) {
187
                $artefactid = get_field('artefact', 'id', 'artefacttype', $artefacttype, 'owner', $view->get('owner'));
188
189
190
191
192
193
            }
        }
        $configdata['artefactid'] = $artefactid;
        return $configdata;
    }

194
195
196
197
    public static function default_copy_type() {
        return 'shallow';
    }

198
    /**
Aaron Wells's avatar
Aaron Wells committed
199
     * Resumefield blocktype is only allowed in personal views, because
200
201
202
     * there's no such thing as group/site resumes
     */
    public static function allowed_in_view(View $view) {
203
        return true;
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
240
241
242
243
244
245
246
247
248
    /**
     * Export the name of the resume field being exported instead of a
     * reference to the artefact ID - mainly so that the fake "contact
     * information" field (which isn't exported) gets handled properly.
     *
     * @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) {
        $configdata = $bi->get('configdata');
        $result = array();

        if (!empty($configdata['artefactid'])) {
            if ($artefacttype = get_field('artefact', 'artefacttype', 'id', $configdata['artefactid'])) {
                $result['artefacttype'] = json_encode(array($artefacttype));
            }
        }

        return $result;
    }

    /**
     * Load the artefact ID for the field based on the field name that is in
     * the config (see export_blockinstance_config_leap).
     *
     * @param array $biconfig   The block instance config
     * @param array $viewconfig The view config
     * @return BlockInstance The newly made block instance
     */
    public static function import_create_blockinstance_leap(array $biconfig, array $viewconfig) {
        $configdata = array();

        // This blocktype is only allowed in personal views
        if (empty($viewconfig['owner'])) {
            return;
        }
        $owner = $viewconfig['owner'];

        if (isset($biconfig['config']) && is_array($biconfig['config'])) {
            $impcfg = $biconfig['config'];
            if (!empty($impcfg['artefacttype'])) {
                if ($artefactid = get_field_sql("SELECT id
                    FROM {artefact}
Francois Marier's avatar
Francois Marier committed
249
                    WHERE \"owner\" = ?
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
                    AND artefacttype = ?
                    AND artefacttype IN (
                        SELECT name
                        FROM {artefact_installed_type}
                        WHERE plugin = 'resume'
                    )", array($owner, $impcfg['artefacttype']))) {
                    $configdata['artefactid'] = $artefactid;
                }
            }
        }

        $bi = new BlockInstance(0,
            array(
                'blocktype'  => $biconfig['type'],
                'configdata' => $configdata,
            )
        );

        return $bi;
    }

271
}