lib.php 6.19 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) {
33
34
35
36
37
38
39
40
41
42
        require_once(get_config('docroot') . 'artefact/lib.php');
        $smarty = smarty_core();
        // 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) {
43
                $resumefield = $instance->get_artefact_instance($artefact->artefact);
44
45
46
                $rendered = $resumefield->render_self(array('viewid' => $instance->get('view')));
                $result = $rendered['html'];
                if (!empty($rendered['javascript'])) {
47
                    $result .= '<script type="application/javascript">' . $rendered['javascript'] . '</script>';
48
49
50
51
                }
                $smarty->assign($artefact->artefacttype, $result);
            }
        }
52
53
54
55
        else {
            $smarty->assign('editing', $editing);
            $smarty->assign('noresume', get_string('noresumeselectone', 'blocktype.resume/entireresume'));
        }
56
57
58
59
        return $smarty->fetch('blocktype:entireresume:content.tpl');
    }

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

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

89
    public static function artefactchooser_element($default=null) {
90
91
92
    }

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

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

125
126
127
128
129
130
131
132
133
134
            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'),
                    );
                    ensure_record_exists('view_artefact', $record, $record);
                }
135
136
137
            }
        }
    }
Richard Mansfield's avatar
Richard Mansfield committed
138

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    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;
    }

161
162
163
164
    public static function default_copy_type() {
        return 'shallow';
    }

165
    /**
Aaron Wells's avatar
Aaron Wells committed
166
     * Entireresume blocktype is only allowed in personal views, because
167
168
169
     * there's no such thing as group/site resumes
     */
    public static function allowed_in_view(View $view) {
170
        return true;
171
172
    }

173
}