editnote.php 8.58 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage artefact-internal
 * @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
 */

define('INTERNAL', 1);
12
13
14
define('SECTION_PLUGINTYPE', 'artefact');
define('SECTION_PLUGINNAME', 'internal');
define('SECTION_PAGE', 'editnote');
15
16

require(dirname(dirname(dirname(__FILE__))) . '/init.php');
17
require_once('license.php');
18
safe_require('artefact', 'internal');
19
safe_require('artefact', 'file');
20
21
22

define('TITLE', get_string('editnote', 'artefact.internal'));

23
24
$note = param_integer('id');
$artefact = new ArtefactTypeHtml($note);
25
if (!$USER->can_edit_artefact($artefact) || $artefact->get('locked')) {
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    throw new AccessDeniedException(get_string('accessdenied', 'error'));
}

$goto = get_config('wwwroot') . 'artefact/internal/notes.php';
if ($group = $artefact->get('group')) {
    define('MENUITEM', 'groups');
    define('GROUP', $group);
    $goto .= '?group=' . $group;
}
else if ($institution = $artefact->get('institution')) {
    define('INSTITUTIONALADMIN', 1);
    define('MENUITEM', 'manageinstitutions');
    $goto .= '?institution=' . $institution;
}
else {
    define('MENUITEM', 'content/notes');
}

44
45
46
47
48
49
50
51
$folder = param_integer('folder', 0);
$browse = (int) param_variable('browse', 0);
$highlight = null;
if ($file = param_integer('file', 0)) {
    $highlight = array($file);
}


52
$form = array(
53
54
55
56
57
58
59
60
61
    'name'              => 'editnote',
    'method'            => 'post',
    'jsform'            => true,
    'newiframeonsubmit' => true,
    'jssuccesscallback' => 'editnote_callback',
    'jserrorcallback'   => 'editnote_callback',
    'plugintype'        => 'artefact',
    'pluginname'        => 'internal',
    'configdirs'        => array(get_config('libroot') . 'form/', get_config('docroot') . 'artefact/file/form/'),
62
    'elements' => array(
63
64
65
66
67
        'title' => array(
            'type'         => 'text',
            'title'        => get_string('Title', 'artefact.internal'),
            'defaultvalue' => $artefact->get('title'),
        ),
68
69
        'description' => array(
            'type'         => 'wysiwyg',
70
            'title'        => get_string('Note', 'artefact.internal'),
71
72
73
74
            'rows'         => 10,
            'cols'         => 70,
            'defaultvalue' => $artefact->get('description'),
        ),
75
76
77
78
79
80
        'tags' => array(
            'type'         => 'tags',
            'title'        => get_string('tags'),
            'description'  => get_string('tagsdescprofile'),
            'defaultvalue' => $artefact->get('tags'),
        ),
81
82
        'license' => license_form_el_basic($artefact),
        'licensing_advanced' => license_form_el_advanced($artefact),
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        'filebrowser' => array(
            'type'         => 'filebrowser',
            'title'        => get_string('attachments', 'artefact.blog'),
            'folder'       => $folder,
            'highlight'    => $highlight,
            'browse'       => $browse,
            'page'         => get_config('wwwroot') . 'artefact/internal/editnote.php?id=' . $note . '&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_note_attachment',
            'unselectcallback'   => 'delete_note_attachment',
        ),
105
106
107
108
109
        'allowcomments' => array(
            'type'         => 'checkbox',
            'title'        => get_string('allowcomments', 'artefact.comment'),
            'defaultvalue' => $artefact->get('allowcomments'),
        ),
110
111
112
113
114
115
116
        'perms' => array(
            'type'         => 'rolepermissions',
            'title'        => get_string('Permissions'),
            'defaultvalue' => $artefact->get('rolepermissions'),
            'group'        => $group,
            'ignore'       => !$group,
        ),
117
        'submitnote' => array(
118
119
120
121
122
            'type'         => 'submitcancel',
            'value'        => array(get_string('save'), get_string('cancel')),
            'goto'         => $goto,
        ),
    ),
123
124
125
126
127
128
);
if (!get_config('licensemetadata')) {
    unset($form['elements']['license']);
    unset($form['elements']['licensing_advanced']);
}
$form = pieform($form);
129

130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Javascript specific to this page.  Creates the list of files
 * attached to the note.
 */
$wwwroot = get_config('wwwroot');
$noimagesmessage = json_encode(get_string('noimageshavebeenattachedtothispost', 'artefact.blog'));
$javascript = <<<EOF
function editnote_callback(form, data) {
    editnote_filebrowser.callback(form, data);
};
EOF;

142
143
144
$othernotecount = count_records('view_artefact', 'artefact', $artefact->get('id'));
$othernotesmsg = '<div class="warning">' . get_string('textusedinothernotes',  'blocktype.internal/textbox', $othernotecount) . '</div>';

145
$smarty = smarty(array(), array(), array(), array(
146
147
148
149
    'tinymceconfig' => '
        plugins: "textcolor,hr,link,maharaimage,table,emoticons,spellchecker,paste,code,fullscreen",
        image_filebrowser: "editnote_filebrowser",
    ',
150
151
152
153
154
155
156
157
158
159
    'sideblocks' => array(
        array(
            'name'   => 'quota',
            'weight' => -10,
            'data'   => array(),
        ),
    ),
));
$smarty->assign('INLINEJAVASCRIPT', $javascript);
$smarty->assign_by_ref('form', $form);
160
$smarty->assign('PAGEHEADING', $artefact->get('title'));
161
$smarty->assign('pagedescriptionhtml', $othernotesmsg);
162
163
$smarty->display('form.tpl');

164

165
166
function editnote_submit(Pieform $form, array $values) {
    global $SESSION, $artefact, $goto;
167
168

    db_begin();
169
    $artefact->set('title', $values['title']);
170
    $artefact->set('description', $values['description']);
171
    $artefact->set('tags', $values['tags']);
172
    $artefact->set('allowcomments', (int) $values['allowcomments']);
173
174
175
176
    if (isset($values['perms'])) {
        $artefact->set('rolepermissions', $values['perms']);
        $artefact->set('dirty', true);
    }
177
178
179
180
181
    if (get_config('licensemetadata')) {
        $artefact->set('license', $values['license']);
        $artefact->set('licensor', $values['licensor']);
        $artefact->set('licensorurl', $values['licensorurl']);
    }
182
    $artefact->commit();
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

    // Attachments
    $old = $artefact->attachment_id_list();
    $new = is_array($values['filebrowser']) ? $values['filebrowser'] : array();
    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) {}
            }
        }
    }
205
206
207
208
209
210
211
212
213
214
215
216
    // need to update the block_instances where this artefact is used - so they have
    // the correct configuration artefactids
    if ($blocks = get_column('view_artefact', 'block', 'artefact', $artefact->get('id'))) {
        require_once(get_config('docroot') . 'blocktype/lib.php');
        foreach ($blocks as $block) {
            $bi = new BlockInstance($block);
            $configdata = $bi->get('configdata');
            $configdata['artefactids'] = $new;
            $bi->set('configdata', $configdata);
            $bi->commit();
        }
    }
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
    db_commit();

    $result = array(
        'error'   => false,
        'message' => get_string('noteupdated', 'artefact.internal'),
        'goto'    => $goto,
    );
    if ($form->submitted_by_js()) {
        // Redirect back to the note page from within the iframe
        $SESSION->add_ok_msg($result['message']);
        $form->json_reply(PIEFORM_OK, $result, false);
    }
    $form->reply(PIEFORM_OK, $result);
}

function add_note_attachment($attachmentid) {
    global $artefact;
    if ($artefact) {
        $artefact->attach($attachmentid);
    }
}

function delete_note_attachment($attachmentid) {
    global $artefact;
    if ($artefact) {
        $artefact->detach($attachmentid);
    }
244
}