/**
 * 
 * 
 */
function bookmarks_update_bookmark($itemid, $name, $description, $url, $tags, $userid = null)
{
    global $USER;
    $item = bookmarks_get_item($itemid);
    if ($userid == null) {
        $userid = $USER->id;
    }
    if ($userid = !$item->userid) {
        notice(get_string('notyours', 'bookmarks'));
    }
    if ($item->name != $name) {
        // check for a bookmark with the same name
        $exists = count_records("bookmarks_items", 'userid', $userid, 'name', $name, 'bookmarksid', $item->bookmarksid);
        if ($exists != 0) {
            notice(get_string('repeatedname', 'bookmarks'));
        }
    }
    // check for a bookmark to the same link
    $reg = '/^(http|https):\\/\\/([a-z0-9-]\\.+)*/i';
    $link = '';
    if (preg_match($reg, $url)) {
        $link = get_record('bookmarks_links', 'url', $url);
        if (!empty($link)) {
            if ($item->linkid != $link->id) {
                $exists = count_records("bookmarks_items", 'userid', $userid, 'linkid', $link->id);
                if ($exists != 0) {
                    notice(get_string('repeatedurl', 'bookmarks'));
                }
            }
        }
    } else {
        notice(get_string('validurl', 'bookmarks'));
    }
    $item->name = $name;
    $item->description = $description;
    if (!empty($link)) {
        $item->linkid = $link->id;
    } else {
        if (!($item->linkid = bookmarks_add_link($url))) {
            error("There was an error in link creation");
        }
    }
    if (!($itemid = update_record('bookmarks_items', $item))) {
        error("There was an error in bookmark creation");
    }
    return tag_set('bookmark', $item->id, explode(',', $tags));
}
require_login($course->id);
add_to_log($course->id, "bookmarks", "del", "del.php?id={$cm->id}", "{$bookmarks->id}", $cm->id);
/// Print the page header
if ($course->category) {
    $navigation = "<a href=\"../../course/view.php?id={$course->id}\">{$course->shortname}</a> ->";
} else {
    $navigation = '';
}
/// Print header.
$navlinks = array();
$navlinks[] = array('name' => get_string('modulenameplural', 'bookmarks'), 'link' => $CFG->wwwroot . '/mod/bookmarks/index.php?id=' . $course->id, 'type' => 'activity');
$navlinks[] = array('name' => format_string($bookmarks->name), 'link' => "view.php?id={$cm->id}", 'type' => 'activityinstance');
$navlinks[] = array('name' => get_string('delete'));
$navigation = build_navigation($navlinks);
print_header_simple(format_string($bookmarks->name), "", $navigation, "", "", true, update_module_button($cm->id, $course->id, get_string("modulename", "bookmarks")), navmenu($course, $cm));
/// Print the main part of the page
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/bookmarks:deleteitem', $context);
if (isset($itemid)) {
    $item = bookmarks_get_item($itemid);
    if ($USER->id == $item->userid && $item->bookmarksid == $bookmarks->id) {
        bookmarks_delete_item($item);
    } else {
        notice("Error ocurred");
    }
} else {
    notice("Item not specified");
}
redirect("view.php?id={$id}", get_string('deleting', 'bookmarks'), 3);
/// Finish the page
print_footer($course);