lib.php 6.7 KB
Newer Older
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-entireresume
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 PluginBlocktypeEntireresume extends MaharaCoreBlocktype {
15
16

    public static function get_title() {
Ghada El-Zoghbi's avatar
Ghada El-Zoghbi committed
17
        return get_string('title', 'blocktype.resume/entireresume');
18
19
20
21
22
23
24
    }

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

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

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

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

36
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
37
38
        require_once(get_config('docroot') . 'artefact/lib.php');
        $smarty = smarty_core();
Lisa Seeto's avatar
Lisa Seeto committed
39
40
41
42
        $configdata = $instance->get('configdata');
        $configdata['showcommentcount'] = true;
        $configdata['viewid'] = $instance->get('view');
        $configdata['editing'] = $editing;
43
44
45
46
47
48
49
50
        // Get data about the resume fields the user has
        if ($artefacts = get_records_sql_array('
            SELECT va.artefact, a.artefacttype
            FROM {view_artefact} va
            INNER JOIN {artefact} a ON (va.artefact = a.id)
            WHERE va.view = ?
            AND va.block = ?', array($instance->get('view'), $instance->get('id')))) {
            foreach ($artefacts as $artefact) {
51
                $resumefield = $instance->get_artefact_instance($artefact->artefact);
Lisa Seeto's avatar
Lisa Seeto committed
52
                $rendered = $resumefield->render_self($configdata);
53
54
                $result = $rendered['html'];
                if (!empty($rendered['javascript'])) {
55
                    $result .= '<script>' . $rendered['javascript'] . '</script>';
56
57
58
59
                }
                $smarty->assign($artefact->artefacttype, $result);
            }
        }
60
61
62
63
        else {
            $smarty->assign('editing', $editing);
            $smarty->assign('noresume', get_string('noresumeselectone', 'blocktype.resume/entireresume'));
        }
64
65
66
        return $smarty->fetch('blocktype:entireresume:content.tpl');
    }

67
    public static function has_instance_config(BlockInstance $instance) {
68
69
70
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
71
    public static function instance_config_form(BlockInstance $instance) {
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        $owner = $instance->get_view()->get('owner');
        if ($owner) {
            $elements = array(
                'tags'  => array(
                    'type'         => 'tags',
                    'title'        => get_string('tags'),
                    'description'  => get_string('tagsdescblock'),
                    'defaultvalue' => $instance->get('tags'),
                    'help'         => false,
                )
            );
        }
        else {
            $elements['blocktemplatehtml'] = array(
                'type' => 'html',
                'value' => get_string('blockinstanceconfigownerauto', 'mahara'),
            );
            $elements['blocktemplate'] = array(
                'type'    => 'hidden',
                'value'   => 1,
            );
        }
94
        return $elements;
95
96
    }

97
    public static function artefactchooser_element($default=null) {
98
99
100
    }

    /**
Aaron Wells's avatar
Aaron Wells committed
101
     * Subscribe to the blockinstancecommit event to make sure all artefacts
102
103
104
105
106
107
108
109
110
111
112
113
     * that should be in the blockinstance are
     */
    public static function get_event_subscriptions() {
        return array(
            (object)array(
                'event'        => 'blockinstancecommit',
                'callfunction' => 'ensure_resume_artefacts_in_blockinstance',
            ),
        );
    }

    /**
Aaron Wells's avatar
Aaron Wells committed
114
     * Hook for making sure that all resume artefacts are associated with a
115
116
117
     * blockinstance at blockinstance commit time
     */
    public static function ensure_resume_artefacts_in_blockinstance($event, $blockinstance) {
118
119
120
        if ($blockinstance->get('blocktype') == 'entireresume') {
            safe_require('artefact', 'resume');
            $artefacttypes = implode(', ', array_map('db_quote', PluginArtefactResume::get_artefact_types()));
121

122
123
124
125
126
            // Get all artefacts that are resume related and belong to the correct owner
            $artefacts = get_records_sql_array('
                SELECT id
                FROM {artefact}
                WHERE artefacttype IN(' . $artefacttypes . ')
Francois Marier's avatar
Francois Marier committed
127
128
                AND "owner" = (
                    SELECT "owner"
129
130
131
                    FROM {view}
                    WHERE id = ?
                )', array($blockinstance->get('view')));
132

133
134
135
136
137
138
139
140
            if ($artefacts) {
                // Make sure they're registered as being in this view
                foreach ($artefacts as $artefact) {
                    $record = (object)array(
                        'view' => $blockinstance->get('view'),
                        'artefact' => $artefact->id,
                        'block' => $blockinstance->get('id'),
                    );
141
142
143
                    /* There are multiple calls to the DB via asynch json calls causing multiple records to be created
                    * * until that issue is fixed, we ignore the warnigns for now */
                    ensure_record_exists('view_artefact', $record, $record, false, false , IGNORE_MULTIPLE);
144
                }
145
146
147
            }
        }
    }
Richard Mansfield's avatar
Richard Mansfield committed
148

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    public static function get_artefacts(BlockInstance $instance) {
        $configdata = $instance->get('configdata');
        $return = array();
        safe_require('artefact', 'resume');
        $artefacttypes = implode(', ', array_map('db_quote', PluginArtefactResume::get_artefact_types()));
        // Get all artefacts that are resume related and belong to the correct owner
        if ($artefacts = get_records_sql_array('
                SELECT id
                FROM {artefact}
                WHERE artefacttype IN(' . $artefacttypes . ')
                AND "owner" = (
                    SELECT "owner"
                    FROM {view}
                    WHERE id = ?
                )', array($instance->get('view')))) {
            foreach ($artefacts as $artefact) {
                $return[] = $artefact->id;
            }
        }
        return $return;
    }

171
172
173
174
    public static function default_copy_type() {
        return 'shallow';
    }

175
    /**
Aaron Wells's avatar
Aaron Wells committed
176
     * Entireresume blocktype is only allowed in personal views, because
177
178
179
     * there's no such thing as group/site resumes
     */
    public static function allowed_in_view(View $view) {
180
        return true;
181
    }
182
}