lib.php 6.59 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_blocktype_type_content_types() {
        return array('entireresume' => array('resume'));
    }

32
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
33
34
        require_once(get_config('docroot') . 'artefact/lib.php');
        $smarty = smarty_core();
Lisa Seeto's avatar
Lisa Seeto committed
35
36
37
38
        $configdata = $instance->get('configdata');
        $configdata['showcommentcount'] = true;
        $configdata['viewid'] = $instance->get('view');
        $configdata['editing'] = $editing;
39
40
41
42
43
44
45
46
        // 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) {
47
                $resumefield = $instance->get_artefact_instance($artefact->artefact);
Lisa Seeto's avatar
Lisa Seeto committed
48
                $rendered = $resumefield->render_self($configdata);
49
50
                $result = $rendered['html'];
                if (!empty($rendered['javascript'])) {
51
                    $result .= '<script>' . $rendered['javascript'] . '</script>';
52
53
54
55
                }
                $smarty->assign($artefact->artefacttype, $result);
            }
        }
56
57
58
59
        else {
            $smarty->assign('editing', $editing);
            $smarty->assign('noresume', get_string('noresumeselectone', 'blocktype.resume/entireresume'));
        }
60
61
62
        return $smarty->fetch('blocktype:entireresume:content.tpl');
    }

63
    public static function has_instance_config(BlockInstance $instance) {
64
65
66
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
67
    public static function instance_config_form(BlockInstance $instance) {
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
        $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,
            );
        }
90
        return $elements;
91
92
    }

93
    public static function artefactchooser_element($default=null) {
94
95
96
    }

    /**
Aaron Wells's avatar
Aaron Wells committed
97
     * Subscribe to the blockinstancecommit event to make sure all artefacts
98
99
100
101
102
103
104
105
106
107
108
109
     * 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
110
     * Hook for making sure that all resume artefacts are associated with a
111
112
113
     * blockinstance at blockinstance commit time
     */
    public static function ensure_resume_artefacts_in_blockinstance($event, $blockinstance) {
114
115
116
        if ($blockinstance->get('blocktype') == 'entireresume') {
            safe_require('artefact', 'resume');
            $artefacttypes = implode(', ', array_map('db_quote', PluginArtefactResume::get_artefact_types()));
117

118
119
120
121
122
            // 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
123
124
                AND "owner" = (
                    SELECT "owner"
125
126
127
                    FROM {view}
                    WHERE id = ?
                )', array($blockinstance->get('view')));
128

129
130
131
132
133
134
135
136
            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'),
                    );
137
138
139
                    /* 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);
140
                }
141
142
143
            }
        }
    }
Richard Mansfield's avatar
Richard Mansfield committed
144

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    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;
    }

167
168
169
170
    public static function default_copy_type() {
        return 'shallow';
    }

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