editgoalsandskills.php 7.46 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage artefact-resume
 * @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
14
15
16
17
18
19
20
 *
 */

define('INTERNAL', true);
define('MENUITEM', 'content/resume');
define('SECTION_PLUGINTYPE', 'artefact');
define('SECTION_PLUGINNAME', 'resume');
define('RESUME_SUBPAGE', 'goalsandskills');

require_once(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once('pieforms/pieform/elements/calendar.php');
require_once(get_config('docroot') . 'artefact/lib.php');
21
safe_require('artefact', 'resume');
22
23
safe_require('artefact', 'file');

24
25
26
27
if (!PluginArtefactResume::is_active()) {
    throw new AccessDeniedException(get_string('plugindisableduser', 'mahara', get_string('resume','artefact.resume')));
}

28
define('TITLE', get_string('resume', 'artefact.resume'));
29
define('SUBSECTIONHEADING', get_string('goalsandskills',  'artefact.resume'));
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
$id = param_integer('id', 0);
$type = param_variable('type', '');

if ($id > 0) {
    $artefact = artefact_instance_from_id($id);
    $type = $artefact->get('artefacttype');
}
else if ($id == 0 && !empty($type)) {
    $classname = generate_artefact_class_name($type);
    try {
        $artefact = artefact_instance_from_type($type);
    }
    catch (Exception $e) {
        $artefact = new $classname(0, array('owner' => $USER->get('id')));
        $artefact->commit();
    }
}
else {
    throw new ArtefactNotFoundException(get_string('cannotfindcreateartefact', 'artefact.resume'));
}

if ($artefact->get('owner') != $USER->get('id')) {
    throw new AccessDeniedException(get_string('notartefactowner', 'error'));
}

$folder = param_integer('folder', 0);
$browse = (int) param_variable('browse', 0);
$highlight = null;
if ($file = param_integer('file', 0)) {
    $highlight = array($file);
}


$form = pieform(array(
    'name'              => 'editgoalsandskills',
    'method'            => 'post',
    'jsform'            => true,
    'newiframeonsubmit' => true,
    'jssuccesscallback' => 'editgoalsandskills_callback',
    'jserrorcallback'   => 'editgoalsandskills_callback',
    'plugintype'        => 'artefact',
    'pluginname'        => 'resume',
    'configdirs'        => array(get_config('libroot') . 'form/', get_config('docroot') . 'artefact/file/form/'),
    'elements' => array(
        'description' => array(
            'type' => 'wysiwyg',
            'title' => get_string('description', 'artefact.resume'),
            'rows' => 20,
            'cols' => 65,
            'defaultvalue' => $artefact->get('description'),
            'rules' => array('maxlength' => 65536),
        ),
        'filebrowser' => array(
            'type'         => 'filebrowser',
            'title'        => get_string('attachments', 'artefact.blog'),
            'folder'       => $folder,
            'highlight'    => $highlight,
            'browse'       => $browse,
            'page'         => get_config('wwwroot') . 'artefact/resume/editgoalsandskills.php?id=' . $artefact->get('id') . '&browse=1',
            'browsehelp'   => 'browsemyfiles',
            'config'       => array(
                'upload'          => true,
                'uploadagreement' => get_config_plugin('artefact', 'file', 'uploadagreement'),
                'resizeonuploaduseroption' => get_config_plugin('artefact', 'file', 'resizeonuploaduseroption'),
                'resizeonuploaduserdefault' => $USER->get_account_preference('resizeonuploaduserdefault'),
                'createfolder'    => false,
                'edit'            => false,
                'select'          => true,
            ),
            'defaultvalue'       => $artefact->attachment_id_list(),
            'selectlistcallback' => 'artefact_get_records_by_id',
            'selectcallback'     => 'add_resume_attachment',
            'unselectcallback'   => 'delete_resume_attachment',
        ),
        'artefacttype' => array(
            'type' => 'hidden',
            'value' => $artefact->get('artefacttype'),
        ),
        'submitform' => array(
            'type' => 'submitcancel',
110
            'class' => 'btn-primary',
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
            'value' => array(get_string('save'), get_string('cancel')),
            'goto' => get_config('wwwroot') . 'artefact/resume/goalsandskills.php',
        ),
    )
));

/*
 * Javascript specific to this page.  Creates the list of files
 * attached to the resume goals or skills.
 */
$wwwroot = get_config('wwwroot');
$noimagesmessage = json_encode(get_string('noimageshavebeenattachedtothispost', 'artefact.blog'));
$javascript = <<<EOF
function editgoalsandskills_callback(form, data) {
    editgoalsandskills_filebrowser.callback(form, data);
};
127
128
129
130
131
132
133
134
135

$(function($) {
    $('#page-modal').on('hidden.bs.modal', function (e) {
        // check if the upload file modal is still visible and if so put the body class back to allow scrolling
        if ($('#editgoalsandskills_filebrowser_upload_browse').hasClass('show')) {
            $('body').addClass('modal-open');
        }
    });
});
136
137
138
EOF;

$smarty = smarty(array(), array(), array(), array(
139
140
141
    'tinymceconfig' => '
        image_filebrowser: "editgoalsandskills_filebrowser",
    ',
142
143
144
145
146
147
148
149
150
151
    'sideblocks' => array(
        array(
            'name'   => 'quota',
            'weight' => -10,
            'data'   => array(),
        ),
    ),
));
$smarty->assign('INLINEJAVASCRIPT', $javascript);
$smarty->assign('SUBPAGENAV', PluginArtefactResume::submenu_items());
152
$smarty->assign('artefactform', $form);
153
154
155
156
157
$smarty->assign('artefacttype', $type);
$smarty->display('artefact:resume:editgoalsandskills.tpl');


function editgoalsandskills_submit(Pieform $form, array $values) {
158
    global $SESSION, $artefact, $USER;
159
160
161
    require_once('embeddedimage.php');

    $newdescription = EmbeddedImage::prepare_embedded_images($values['description'], $values['artefacttype'], $USER->get('id'));
162
163
164

    db_begin();
    $artefact->set('title', get_string($values['artefacttype'], 'artefact.resume'));
165
    $artefact->set('description', $newdescription);
166
167
168
169
170
    $artefact->commit();

    // Attachments
    $old = $artefact->attachment_id_list();
    $new = is_array($values['filebrowser']) ? $values['filebrowser'] : array();
171
172
173
174
175
176
177
    // only allow the attaching of files that exist and are editable by user
    foreach ($new as $key => $fileid) {
        $file = artefact_instance_from_id($fileid);
        if (!($file instanceof ArtefactTypeFile) || !$USER->can_publish_artefact($file)) {
            unset($new[$key]);
        }
    }
178
179
180
181
182
183
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
    if (!empty($new) || !empty($old)) {
        foreach ($old as $o) {
            if (!in_array($o, $new)) {
                try {
                    $artefact->detach($o);
                }
                catch (ArtefactNotFoundException $e) {}
            }
        }
        foreach ($new as $n) {
            if (!in_array($n, $old)) {
                try {
                    $artefact->attach($n);
                }
                catch (ArtefactNotFoundException $e) {}
            }
        }
    }
    db_commit();

    $result = array(
        'error'   => false,
        'message' => get_string('goalandskillsaved', 'artefact.resume'),
        'goto'    => get_config('wwwroot') . 'artefact/resume/goalsandskills.php',
    );
    if ($form->submitted_by_js()) {
        // Redirect back to the resume goals and skills page from within the iframe
        $SESSION->add_ok_msg($result['message']);
        $form->json_reply(PIEFORM_OK, $result, false);
    }
    $form->reply(PIEFORM_OK, $result);
209
}