*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
require_once dirname(__FILE__) . '/inc.php';
require_once dirname(__FILE__) . '/lib/sharelib.php';
$courseid = required_param('courseid', PARAM_INT);
$action = optional_param("action", "", PARAM_ALPHA);
$save_this = optional_param('save_this', '', PARAM_ALPHA);
$itemid = optional_param('itemid', 0, PARAM_INT);
$shareusers = optional_param('shareusers', '', PARAM_RAW);
// array of integer
$shareall = optional_param('shareall', 0, PARAM_INT);
$externaccess = optional_param('externaccess', 0, PARAM_INT);
$externcomment = optional_param('externcomment', 0, PARAM_INT);
$backtype = optional_param('backtype', 'all', PARAM_ALPHA);
$backtype = block_exabis_eportfolio_check_item_type($backtype, true);
$context = get_context_instance(CONTEXT_SYSTEM);
require_login($courseid);
require_capability('block/exabis_eportfolio:use', $context);
require_capability('block/exabis_eportfolio:shareintern', $context);
if (!($course = get_record("course", "id", $courseid))) {
    print_error("invalidcourseid", "block_exabis_eportfolio");
}
if ($action == 'userlist') {
    echo json_encode(exabis_eportfolio_get_shareable_courses_with_users());
    exit;
}
// get the bookmark if it is mine.
$bookmark = get_record_sql("select b.*, bc.name AS cname, bc2.name AS cname_parent, c.fullname As coursename" . " from {$CFG->prefix}block_exabeporitem b join {$CFG->prefix}block_exabeporcate bc on b.categoryid = bc.id" . " left join {$CFG->prefix}block_exabeporcate bc2 on bc.pid = bc2.id" . " left join {$CFG->prefix}course c on b.courseid = c.id" . " where b.userid = '{$USER->id}' and b.id='" . $itemid . "'");
if (!$bookmark) {
    print_error("bookmarknotfound", "block_exabis_eportfolio", 'view.php?courseid=' . $courseid);
    print_error("nocategories", "block_exabis_eportfolio", "view.php?courseid=" . $courseid);
}
$id = optional_param('id', 0, PARAM_INT);
if ($id) {
    if (!($existing = get_record('block_exabeporitem', 'id', $id, 'userid', $USER->id))) {
        print_error("wrong" . $type . "id", "block_exabis_eportfolio");
    }
} else {
    $existing = false;
}
// read + check type
if ($existing) {
    $type = $existing->type;
} else {
    $type = optional_param('type', 'all', PARAM_ALPHA);
    $type = block_exabis_eportfolio_check_item_type($type, false);
    if (!$type) {
        print_error("badtype", "block_exabis_eportfolio");
    }
}
$returnurl = $CFG->wwwroot . '/blocks/exabis_eportfolio/view_items.php?courseid=' . $courseid . "&type=" . $backtype;
// delete item
if ($action == 'delete') {
    if (!$existing) {
        print_error("bookmarknotfound", "block_exabis_eportfolio");
    }
    if (data_submitted() && $confirm && confirm_sesskey()) {
        block_exabis_eportfolio_do_delete($existing, $returnurl, $courseid);
        redirect($returnurl);
    } else {
        $optionsyes = array('id' => $id, 'action' => 'delete', 'confirm' => 1, 'backtype' => $backtype, 'sesskey' => sesskey(), 'courseid' => $courseid);