//If the content item belongs to the user
$query = "SELECT * FROM {$CONFIG->dbprefix}users_entity join {$CONFIG->dbprefix}_content_item_discrimination on {$CONFIG->dbprefix}users_entity.guid = {$CONFIG->dbprefix}_content_item_discrimination.guid and {$CONFIG->dbprefix}_content_item_discrimination.creator_guid = \"" . $_SESSION['id'] . "\"";
$result = get_data($query);
//echo $query;
$flag = false;
$total_users = count($result);
for ($i = 0; $i < $total_users; $i++) {
    $row = $result[$i];
    if ($row->guid == $selected_item) {
        $flag = true;
    }
}
if ($user->canEdit()) {
    //$address = "http://meducator.open.ac.uk/resourcesrestapi/rest/meducator/eidsearch?id=" . $user->guid;
    $address = $CONFIG->API_URL . "eidsearch?id=" . $user->guid;
    $rdf_info = connectToSesame($address);
    require_once dirname(dirname(__FILE__)) . "/mmsearch/custom/MeducatorParser.php";
    $medParser = new MeducatorParser($rdf_info, true, true);
    if (count($medParser->results) > 0) {
        foreach ($medParser->results as $key => $value) {
            $resourceSesameID = $key;
            $resourceData = $value;
        }
    } else {
        $resourceData = array();
        $resourceSesameID = "";
    }
    $area2 = elgg_view_title(elgg_echo('profile:edit'));
    $area2 .= elgg_view("profile/edit", array('entity' => $user, 'sesame_id' => $resourceSesameID, 'data' => $resourceData));
} else {
    if ($flag) {
<?php

// Load Elgg engine
include_once dirname(dirname(dirname(__FILE__))) . "/engine/start.php";
include_once dirname(__FILE__) . "/actions/API_connection.php";
include_once dirname(__FILE__) . "/custom/MMDSearchResults.php";
// make sure only logged in users can see this page
gatekeeper();
// get the form input
$keyword = get_input('keyword');
// set the title
$title = "Search for distributed educational resources";
// start building the main column of the page
$area2 = elgg_view_title($title);
//display submenus
$area1 = $address = $CONFIG->DISTRIBUTED_API_URL . "searchservices";
$result = connectToSesame($address);
$searchResults = new MMDSearchResults("services", $result, true);
// Add the list of results
$area2 .= elgg_view("mmdsearch/interface", array("services" => $searchResults->getServices()));
// layout the page
$body = elgg_view_layout('two_column_left_sidebar', '', $area2);
// draw the page
page_draw($title, $body);
function getfields($req)
{
    global $CONFIG;
    set_time_limit(0);
    //this avoids timeouts
    include $CONFIG->path . "mod/profile_manager/views/default/profile_manager/members/config.php";
    include $CONFIG->path . "mod/profile_manager/views/default/profile_manager/members/classes.php";
    if ($req["datasource"] == $data_source) {
        $suggest = 1;
    } else {
        $suggest = 0;
    }
    if ($req["datasource"] == 1) {
        //if we chose RDF as input
        include_once $CONFIG->path . "mod/mmsearch/custom/MeducatorParser.php";
        include_once $CONFIG->path . "mod/mmsearch/custom/MeducatorMetadata.php";
        include_once $CONFIG->path . "engine/lib/mmplus.php";
        //$address = $CONFIG->API_URL . "eidsearch?id=" . "8163";
        //$address =$CONFIG->API_URL . "searchall?properties=mdc:creator";
        $address = $CONFIG->API_URL . "propertysearch?property=mdc:title&value=";
        $rdf_info = connectToSesame($address);
        $medParser = new MeducatorParser($rdf_info, true);
        $elements = $medParser->results;
        $values = array();
        foreach ($elements as $element) {
            $values = array_merge($values, array_keys($element));
        }
        $values = array_unique($values);
        foreach ($values as $value) {
            $fields[] = array("name" => trim($value), "metadata" => 0, "uses" => 0, "tags" => 0);
        }
    } elseif ($req["datasource"] == 2) {
        //if we chose Elgg database as input
        $query = "SELECT distinct e.* from elggentities e join elggusers_entity u on e.guid = u.guid JOIN (SELECT subm1.*, s1.string FROM elggmetadata subm1 JOIN elggmetastrings s1 ON subm1.value_id = s1.id) AS m1 ON e.guid = m1.entity_guid where ((m1.name_id='440' AND m1.string IN ('356'))) and ( (1 = 1) and e.enabled='yes') and ( (1 = 1) and m1.enabled='yes')";
        $entities = get_data($query, "entity_row_to_elggstar");
        $entity = $entities[0];
        $fields = array();
        $categorized_fields = profile_manager_get_categorized_fields($entity, true);
        $cats = $categorized_fields['categories'];
        $db_fields = $categorized_fields['fields'];
        if (!empty($db_fields)) {
            foreach ($cats as $cat_guid => $cat) {
                foreach ($db_fields[$cat_guid] as $db_field) {
                    $metadata_name = $db_field->metadata_name;
                    // get options
                    $options = $db_field->getOptions();
                    // get type of field
                    if ($db_field->user_editable == "no") {
                        $valtype = "non_editable";
                    } else {
                        $valtype = $db_field->metadata_type;
                    }
                    $fields[] = array("name" => trim($db_field->getTitle()), "metadata" => 0, "uses" => 0, "tags" => 0);
                }
            }
        }
    }
    if ($suggest) {
        //if we decided to suggest values, suggest the values of the previous configuration
        include_once $CONFIG->path . "mod/profile_manager/views/default/profile_manager/members/config.php";
        $fields_metadata = explode(";", $metadata_fields);
        $fields_uses = explode(";", $uses_fields);
        $fields_tags = explode(";", $tags_fields);
        $i = 0;
        foreach ($fields as $field) {
            if (in_array($field["name"], $fields_metadata)) {
                $fields[$i]["metadata"] = 1;
            }
            if (in_array($field["name"], $fields_uses)) {
                $fields[$i]["uses"] = 1;
            }
            if (in_array($field["name"], $fields_tags)) {
                $fields[$i]["tags"] = 1;
            }
            $i++;
        }
    }
    return $fields;
}
 if (!in_array($nik, $changes["new"])) {
     //if it is in the list of the new resources (created after last classification), don't put it in the list of the edited resources
     require_once $CONFIG->path . "mod/mmsearch/custom/MeducatorParser.php";
     $rdf_old = connectToSesame($CONFIG->API_URL . "eidsearch?id={$nik}");
     $medParser = new MeducatorParser($rdf_old, true);
     $val = array_values($medParser->results);
     $old_data = $val[0];
 }
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //apply the modifications
 $area2 .= "INSTANCE ID FROM SESAME= " . connectToSesame($CONFIG->API_URL, $response, "", "YES");
 //forward ($vars['url']."mod/content_item/".$ourFileName);
 //ADDED BY GIACOMO FAZIO///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 if (!in_array($nik, $changes["new"])) {
     //if it is in the list of the new resources (created after last classification), don't put it in the list of the edited resources
     $rdf_new = connectToSesame($CONFIG->API_URL . "eidsearch?id={$nik}");
     $medParser = new MeducatorParser($rdf_new, true);
     $val = array_values($medParser->results);
     $new_data = $val[0];
     //var_dump($old_data);
     //var_dump($new_data);
     $metadatas_fields = explode(";", $metadata_fields);
     $tagss_fields = explode(";", $tags_fields);
     $usess_fields = explode(";", $uses_fields);
     if (!in_array($nik, $changes["edited"]["metadata"])) {
         //if it is already in the list, no need to continue
         foreach ($metadatas_fields as $field) {
             if ($old_data[$field] != $new_data[$field]) {
                 $changes["edited"]["metadata"][] = $nik;
                 break;
             }
 * @package Elgg
 * @subpackage Core
 * @author Curverider Ltd
 * @link http://elgg.org/
 */
require_once dirname(dirname(dirname(dirname(__FILE__)))) . "/engine/start.php";
// block non-admin users
gatekeeper();
action_gatekeeper();
global $CONFIG;
// Get the user
$guid = get_input('guid');
$obj = get_entity($guid);
$sesid = get_input('sesid');
$deladdress = $CONFIG->API_URL . $sesid;
if ($obj instanceof ElggUser && $obj->canEdit()) {
    if ($obj->issimpleuser == 'no') {
        if ($sesid) {
            connectToSesame($deladdress, "", "YES");
        }
    }
    if ($obj->delete()) {
        system_message(elgg_echo('admin:user:delete:yes'));
    } else {
        register_error(elgg_echo('admin:user:delete:no'));
    }
} else {
    register_error(elgg_echo('admin:user:delete:no'));
}
forward('http://metamorphosis.med.duth.gr');
exit;
    fwrite($ourFileHandle, "</mdc:repurposingDescription>\r\n");
    $cont = explode(";", $nik4);
    if (is_array($cont)) {
        foreach ($cont as $con) {
            fwrite($ourFileHandle, "<mdc:fromRepurposingContext>");
            fwrite($ourFileHandle, "{$con}");
            fwrite($ourFileHandle, "</mdc:fromRepurposingContext>\r\n");
        }
    } else {
        fwrite($ourFileHandle, "<mdc:fromRepurposingContext>");
        fwrite($ourFileHandle, "{$cont}");
        fwrite($ourFileHandle, "</mdc:fromRepurposingContext>\r\n");
    }
    fwrite($ourFileHandle, "</mdc:RepurposingContext>");
    fwrite($ourFileHandle, "</mdc:hasRepurposingContext>\r\n");
}
fwrite($ourFileHandle, "</mdc:Resource></rdf:RDF>");
fclose($ourFileHandle);
$response = file_get_contents($ourFileName);
if ($put == '') {
    $area2 .= "INSTANCE ID FROM SESAME= " . connectToSesame($CONFIG->API_URL, $response, "");
} else {
    $area2 .= "INSTANCE ID FROM SESAME= " . connectToSesame($CONFIG->API_URL, $response, "", "YES");
}
//		forward ($vars['url']."mod/content_item/".$ourFileName);
// layout the page
$body = elgg_view_layout('one_column', $area2);
//$body = elgg_view_layout('one_column', $area2);
//	forward ($vars['url']."pg/profile/".$entity->username);
// draw the page
page_draw($title, $body);
<?php

require_once dirname(dirname(dirname(__FILE__))) . "/engine/start.php";
require_once dirname(dirname(__FILE__)) . "/mmsearch/custom/MMSearchResults.php";
global $CONFIG;
//read all the resources for a specific user
$items = get_entities_from_metadata('creatorg', 939, 'user', '', '', 10000);
//read all the resources from the SESAME
$address = $CONFIG->API_URL . "searchall?properties=mdc:title";
$searchResults = new MMSearchResults(connectToSesame($address), true);
$SESAME_results = $searchResults->DisplayResourcesList();
//identify the SESAME id for the wanted resources
foreach ($items as $item) {
    $doru = $item->guid;
    for ($i = 0; $i + 1 < count($SESAME_results); $i++) {
        if ($doru == $SESAME_results[$i]->internalID) {
            echo $item->guid . " ----> " . str_replace("http://purl.org/meducator/resources/", "", $SESAME_results[$i]->ID) . "<br>";
            echo '<a href="' . $SESAME_results[$i]->ID . '">view</a><br><br>';
            //break;
            //$deladdress=$CONFIG->API_URL . str_replace("http://purl.org/meducator/resources/", "", $SESAME_results[$i]->ID);
            //echo connectToSesame($deladdress,"","YES");
        }
    }
}
function get_snapshot()
{
    global $IOdir;
    global $data_source;
    global $metadatas_fields, $usess_fields, $tagss_fields;
    //if we chose to use the Sesame RDF
    if ($data_source == 1) {
        global $CONFIG;
        $address_base = $CONFIG->API_URL . "searchall?properties=";
        $fields = array_unique(array_merge($metadatas_fields, $usess_fields, $tagss_fields));
        if (!in_array("hasRepurposingContext", $fields)) {
            $fields[] = "hasRepurposingContext";
        }
        //in order to return the "repurposedFrom" part as well, used for Replinks
        $finalresults = array();
        foreach ($fields as $field) {
            if ($field == "seeAlso" || $field == "type") {
                continue;
            }
            //the first one is included by default, the second causes error
            $address = $address_base . "mdc:{$field}";
            //I can't put all the fields together right now, since it will return only the resources that have all those fields, so I do a query field after field and then I make the union of the results
            $rdf_info = connectToSesame($address);
            $medParser = new MeducatorParser($rdf_info, true);
            $results = $medParser->results;
            foreach ($results as $key => $result) {
                if (array_key_exists($key, $finalresults)) {
                    $finalresults[$key] = array_merge($finalresults[$key], $result);
                } else {
                    $finalresults[$key] = $result;
                }
            }
        }
        //$b=print_r($finalresults,true);
        //file_put_contents("bbbb",$b);
        if (count($finalresults) > 0) {
            //we have to create a LearningResource object for each result and insert metadata into it
            foreach ($finalresults as $id => $data) {
                $seeAlso = $data["seeAlso"];
                if (is_array($seeAlso)) {
                    $values = array_unique(get_values($seeAlso, "seeAlso"));
                    foreach ($values as $value) {
                        if (strrchr($value, "#") != FALSE) {
                            $guid = substr(strrchr($value, "#"), 1);
                            break;
                        }
                    }
                } else {
                    $guid = substr(strrchr($seeAlso, "#"), 1);
                }
                $entity = get_entity($guid);
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                if (empty($entity)) {
                    continue;
                }
                //this is a temporary check from the database since we still need it in order not to have problem with the normal search that still takes info from it
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                $lr[$guid] = new LearningResource();
                $lr[$guid]->setName($entity->get('name'));
                $lr[$guid]->setUsername($entity->get('username'));
                $lr[$guid]->setGUID($guid);
                //we extract the important part from each field and insert in the $lr[$guid] object
                foreach ($fields as $field) {
                    $notused = array("type", "hasRepurposingContext");
                    if (in_array($field, $notused)) {
                        continue;
                    }
                    //"type" is more difficult to handle, anyway we don't need it; "hasRepurposingContext" is used later for Replinks
                    $value = "";
                    if (!is_array($data[$field])) {
                        $value = $data[$field];
                    } elseif (isset($data[$field]["name"])) {
                        $value = $data[$field]["name"];
                    } elseif (isset($data[$field]["label"])) {
                        $value = $data[$field]["label"];
                    } else {
                        //if we have different elements
                        foreach ($data[$field] as $subfield) {
                            if (!is_array($subfield)) {
                                $subvalue = $subfield;
                            } elseif (isset($subfield["name"])) {
                                $subvalue = $subfield["name"];
                            } elseif (isset($subfield["label"])) {
                                $subvalue = $subfield["label"];
                            } else {
                                continue;
                            }
                            //I think there are not other cases, but if so, don't handle them in order to avoid errors
                            if (is_array($subvalue)) {
                                $subvalue = implode(";", $subvalue);
                            }
                            //if it is still an array, transform it in a string
                            $value .= $subvalue . ";";
                        }
                        $value = substr($value, 0, -1);
                        //delete the last ;
                    }
                    if (is_array($value)) {
                        $value = implode(";", $value);
                    }
                    //if it is still an array, transform it in a string
                    if (strrchr($value, "#") != FALSE) {
                        $value = substr(strrchr($value, "#"), 1);
                    }
                    //take only the important part
                    if ($value != "") {
                        $lr[$guid]->insertMetadata($field, $value);
                    }
                }
                //now I add to the tags of the resource eventual tags added by the users
                $objs = get_entities_from_metadata('', '', 'object', '', '', 10000);
                foreach ($objs as $obj) {
                    if ($obj->subtype == '28') {
                        //I get only the objects that are bookmarks
                        $link = $CONFIG->url . "pg/profile/" . $entity->get('username');
                        if ($obj->address == $link) {
                            //I consider the bookmark only if it is a bookmark to the current resource
                            if (!empty($obj->tags)) {
                                if (is_array($obj->tags)) {
                                    $lr[$guid]->tags = array_merge($lr[$guid]->tags, $obj->tags);
                                } else {
                                    $lr[$guid]->tags[] = $obj->tags;
                                }
                            }
                        }
                    }
                }
                //insert $replinks["from"]
                if (is_array($data["hasRepurposingContext"])) {
                    $values = array_unique(get_values($data["hasRepurposingContext"], "seeAlso"));
                    foreach ($values as $value) {
                        $guid_from = substr(strrchr($value, "#"), 1);
                        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                        $entity = get_entity($guid_from);
                        if (empty($entity)) {
                            continue;
                        }
                        //this is a temporary check from the database since we still need it in order not to have problem with the normal search that still takes info from it
                        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                        $lr[$guid]->replinks["from"][] = $guid_from;
                    }
                }
            }
            //insert replinks["to"]
            foreach ($lr as $guid => $resource) {
                foreach ($resource->replinks["from"] as $guid_from) {
                    $lr[$guid_from]->replinks["to"][] = $guid;
                }
            }
        } else {
            echo "\n\nNo resources returned. There must be an error somewhere...";
            exit;
        }
    } elseif ($data_source == 2) {
        $fields = array_unique(array_merge($metadatas_fields, $usess_fields, $tagss_fields));
        $query = "SELECT distinct e.* from elggentities e join elggusers_entity u on e.guid = u.guid JOIN (SELECT subm1.*, s1.string FROM elggmetadata subm1 JOIN elggmetastrings s1 ON subm1.value_id = s1.id) AS m1 ON e.guid = m1.entity_guid where ((m1.name_id='440' AND m1.string IN ('356'))) and ( (1 = 1) and e.enabled='yes') and ( (1 = 1) and m1.enabled='yes') order by e.time_created desc";
        $entities = get_data($query, "entity_row_to_elggstar");
        foreach ($entities as $entity) {
            $guid = $entity->getGUID();
            $lr[$guid] = new LearningResource();
            $lr[$guid]->setName($entity->get('name'));
            $lr[$guid]->setUsername($entity->get('username'));
            $lr[$guid]->setGUID($guid);
            $categorized_fields = profile_manager_get_categorized_fields($entity, true);
            $cats = $categorized_fields['categories'];
            $db_fields = $categorized_fields['fields'];
            if (!empty($db_fields)) {
                foreach ($cats as $cat_guid => $cat) {
                    foreach ($db_fields[$cat_guid] as $db_field) {
                        $metadata_name = $db_field->metadata_name;
                        // get options
                        $options = $db_field->getOptions();
                        // get type of db_field
                        if ($db_field->user_editable == "no") {
                            $valtype = "non_editable";
                        } else {
                            $valtype = $db_field->metadata_type;
                        }
                        $field = $db_field->getTitle();
                        if (in_array($field, $fields)) {
                            // get value
                            if ($metadata = get_metadata_byname($entity->guid, $metadata_name)) {
                                //print_r($metadata);
                                if (is_array($metadata)) {
                                    $value = '';
                                    foreach ($metadata as $md) {
                                        if (!empty($value)) {
                                            $value .= ', ';
                                        }
                                        $value .= $md->value;
                                        $access_id = $md->access_id;
                                    }
                                } else {
                                    $value = $metadata->value;
                                    $access_id = $metadata->access_id;
                                }
                            } else {
                                $value = '';
                                $access_id = ACCESS_DEFAULT;
                            }
                            if ($value != "") {
                                $lr[$guid]->insertMetadata($field, $value);
                            }
                        }
                    }
                }
            }
            //now I add to the tags of the resource eventual tags added by the users
            $objs = get_entities_from_metadata('', '', 'object', '', '', 10000);
            foreach ($objs as $obj) {
                if ($obj->subtype == '28') {
                    //I get only the objects that are bookmarks
                    $link = $CONFIG->url . "pg/profile/" . $entity->get('username');
                    if ($obj->address == $link) {
                        //I consider the bookmark only if it is a bookmark to the current resource
                        if (!empty($obj->tags)) {
                            if (is_array($obj->tags)) {
                                $lr[$guid]->tags = array_merge($lr[$guid]->tags, $obj->tags);
                            } else {
                                $lr[$guid]->tags[] = $obj->tags;
                            }
                        }
                    }
                }
            }
        }
        //insert replinks
        foreach ($lr as $guid => $resource) {
            $entity = get_entity($guid);
            $lr[$guid]->insertReplinks($entity, $lr);
        }
    }
    krsort($lr);
    $guids = array_keys($lr);
    file_put_contents($IOdir . 'guids', serialize($guids));
    //it saves resources in the file 'lr'
    if (PHP_OS == "Linux" && getmyuid() == fileowner($IOdir . 'guids')) {
        chmod($IOdir . 'guids', 0666);
    }
    //set rw permissions for everybody for this file
    file_put_contents($IOdir . 'lr', serialize($lr));
    //it saves resources in the file 'lr'
    if (PHP_OS == "Linux" && getmyuid() == fileowner($IOdir . 'lr')) {
        chmod($IOdir . 'lr', 0666);
    }
    //set rw permissions for everybody for this file
    return $lr;
}
    	
    if (mysql_num_rows($result2) > 0) 
    	fwrite($ourFileHandle,"</med:Companion>\r\n");
    */
    fwrite($ourFileHandle, "</mdc:Resource></rdf:RDF>");
    fclose($ourFileHandle);
    $response = file_get_contents($ourFileName);
    if (!isset($rep_map[$entity->guid]) || $rep_map[$entity->guid] == NULL) {
        $URL = "http://meducator.open.ac.uk/resourcesrestapi/rest/meducator/auth/";
        $ch = curl_init($URL);
        curl_setopt($ch, CURLOPT_MUTE, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', 'Authorization: Basic ' . 'bWV0YW1vcnBob3NpczptM3RhbTBycGgwc2lz'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, "{$response}");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        $output = curl_exec($ch);
        curl_close($ch);
        $rep_map[$entity->guid] = $output;
    } else {
        $output = connectToSesame($CONFIG->API_URL, $response, "", "YES");
    }
    $area2 .= $output;
    //$area2 .= "THIS IS DONE";
    //forward ($vars['url']."mod/content_item/".$ourFileName);
    // layout the page
    $body = elgg_view_layout('one_column', $area2);
    //$body = elgg_view_layout('one_column', $area2);
}
// draw the page
page_draw($title, $body);