*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
require_once dirname(__FILE__) . '/inc.php';
require_once dirname(__FILE__) . '/lib/sharelib.php';
$access = optional_param('access', 0, PARAM_TEXT);
require_login(0, true);
if (!($view = block_exabis_eportfolio_get_view_from_access($access))) {
    print_error("viewnotfound", "block_exabis_eportfolio");
}
if (!($user = get_record("user", "id", $view->userid))) {
    print_error("nouserforid", "block_exabis_eportfolio");
}
$portfolioUser = get_record("block_exabeporuser", "user_id", $view->userid);
// read blocks
$query = "select b.*" . " FROM {$CFG->prefix}block_exabeporviewblock b" . " WHERE b.viewid = " . $view->id . " ORDER BY b.positionx, b.positiony";
$blocks = get_records_sql($query);
// read columns
$columns = array();
foreach ($blocks as $block) {
    if (!isset($columns[$block->positionx])) {
        $columns[$block->positionx] = array();
    }
function block_exabis_eportfolio_get_item($itemid, $access)
{
    global $CFG, $USER;
    $itemid = clean_param($itemid, PARAM_INT);
    $item = null;
    if (preg_match('!^view/(.+)$!', $access, $matches)) {
        // in view mode
        if (!($view = block_exabis_eportfolio_get_view_from_access($matches[1]))) {
            print_error("viewnotfound", "block_exabis_eportfolio");
        }
        if (!($viewblock = get_record("block_exabeporviewblock", "viewid", $view->id, "type", "item", "itemid", $itemid))) {
            // item not linked to view -> no rights
        }
        if (!($item = get_record("block_exabeporitem", "id", $itemid, "userid", $view->userid))) {
            // item not found
            return;
        }
        $item->access = $view->access;
        $item->access->page = 'view';
        // comments allowed?
        if ($item->access->request == 'extern') {
            $item->allowComments = $view->externcomment;
            //false;
            $item->showComments = $view->externcomment;
            // TODO: comments anhand view einstellung zeigen
        } else {
            $item->allowComments = true;
            $item->showComments = true;
        }
    } elseif (preg_match('!^portfolio/(.+)$!', $access, $matches)) {
        // in user portfolio mode
        if (!($user = block_exabis_eportfolio_get_user_from_access($matches[1]))) {
            return;
        }
        if ($user->access->request == 'extern') {
            if (!($item = get_record("block_exabeporitem", "id", $itemid, "userid", $user->id, "externaccess", 1))) {
                // item not found
                return;
            }
        } else {
            // intern
            $item = get_record_sql("SELECT i.* FROM {$CFG->prefix}block_exabeporitem i" . " LEFT JOIN {$CFG->prefix}block_exabeporitemshar ishar ON i.id=ishar.itemid AND ishar.userid={$USER->id}" . " WHERE i.id='" . $itemid . "' AND" . " ((i.userid='" . $USER->id . "')" . "  OR (i.shareall=1 AND ishar.userid IS NULL)" . "  OR (i.shareall=0 AND ishar.userid IS NOT NULL))");
            // nobody, but me
            if (!$item) {
                // item not found
                return;
            }
        }
        $item->access = $user->access;
        $item->access->page = 'portfolio';
        // comments allowed?
        if ($item->access->request == 'extern') {
            $item->allowComments = false;
            $item->showComments = $item->externcomment;
        } else {
            $item->allowComments = true;
            $item->showComments = true;
        }
    } else {
        return;
    }
    $item->access->access = $access;
    $item->access->parentAccess = substr($item->access->access, strpos($item->access->access, '/') + 1);
    return $item;
}