editnote.php 8.85 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
        'allowcomments' => array(
106
            'type'         => 'switchbox',
107 108 109
            '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
            'type'         => 'submitcancel',
119
            'class'        => 'btn-success',
120 121 122 123
            'value'        => array(get_string('save'), get_string('cancel')),
            'goto'         => $goto,
        ),
    ),
124 125 126 127 128 129
);
if (!get_config('licensemetadata')) {
    unset($form['elements']['license']);
    unset($form['elements']['licensing_advanced']);
}
$form = pieform($form);
130

131 132 133 134 135 136 137 138 139 140 141 142
/*
 * 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;

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

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

165

166 167
function editnote_submit(Pieform $form, array $values) {
    global $SESSION, $artefact, $goto;
168
    require_once('embeddedimage.php');
169 170

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

    // 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) {}
            }
        }
    }
208 209 210 211 212 213 214 215 216 217 218 219
    // 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();
        }
    }
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    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);
    }
247
}