/**
  * Update the file or directory path in the document db document table
  *
  * @author - Hugues Peeters <*****@*****.**>
  * @param  - action (string) - action type require : 'delete' or 'update'
  * @param  - old_path (string) - old path info stored to change
  * @param  - new_path (string) - new path info to substitute
  * @desc Update the file or directory path in the document db document table
  *
  */
 static function update_db_info($action, $old_path, $new_path = '')
 {
     $dbTable = Database::get_course_table(TABLE_DOCUMENT);
     $course_id = api_get_course_int_id();
     /* DELETE */
     if ($action == 'delete') {
         $old_path = Database::escape_string($old_path);
         $to_delete = "WHERE c_id = {$course_id} AND path LIKE BINARY '" . $old_path . "' OR path LIKE BINARY '" . $old_path . "/%'";
         $query = "DELETE FROM {$dbTable} " . $to_delete;
         $result = Database::query("SELECT id FROM {$dbTable} " . $to_delete);
         if (Database::num_rows($result)) {
             require_once api_get_path(INCLUDE_PATH) . '../metadata/md_funcs.php';
             $mdStore = new mdstore(true);
             // create if needed
             $md_type = substr($dbTable, -13) == 'scormdocument' ? 'Scorm' : 'Document';
             while ($row = Database::fetch_array($result)) {
                 $eid = $md_type . '.' . $row['id'];
                 $mdStore->mds_delete($eid);
                 $mdStore->mds_delete_offspring($eid);
             }
         }
     }
     /* UPDATE */
     if ($action == 'update') {
         if ($new_path[0] == '.') {
             $new_path = substr($new_path, 1);
         }
         $new_path = str_replace('//', '/', $new_path);
         // Attempt to update	- tested & working for root	dir
         $new_path = Database::escape_string($new_path);
         $query = "UPDATE {$dbTable}\n            SET path = CONCAT('" . $new_path . "', SUBSTRING(path, LENGTH('" . $old_path . "')+1) )\n            WHERE c_id = {$course_id} AND path LIKE BINARY '" . $old_path . "' OR path LIKE BINARY '" . $old_path . "/%'";
     }
     Database::query($query);
 }
 /**
  * @param int $document_id
  */
 public static function delete_document_metadata($document_id)
 {
     // needed to deleted medadata
     require_once api_get_path(SYS_CODE_PATH) . 'metadata/md_funcs.php';
     require_once api_get_path(LIBRARY_PATH) . 'fileManage.lib.php';
     $mdStore = new mdstore(true);
     //delete metadata
     $eid = 'Document' . '.' . $document_id;
     $mdStore->mds_delete($eid);
     $mdStore->mds_delete_offspring($eid);
 }
             $sdisub = '';
         } else {
             store_md_and_traverse_subitems($sdi, 0, 1, 0, $xht_doc->xmd_select_single_element(TREETOP), -1);
         }
         $playIt = $baseWorkDir . '/' . $workWith . '/index.php';
         $fileHandler = @fopen($playIt, 'w');
         @fwrite($fileHandler, content_for_index_php($sdi));
         @fclose($fileHandler);
         echo '<br>', htmlspecialchars($workWith, ENT_QUOTES, $charset);
         if (file_exists($playIt)) {
             echo '/index.php ', htmlspecialchars(date('Y/m/d H:i:s', filemtime($playIt)), ENT_QUOTES, $charset);
         }
     }
 } elseif ($smo == get_lang('Remove') && $sdisub) {
     $screm = EID_TYPE . '.' . $sdi . '.' . $sdisub;
     $mdStore->mds_delete_offspring($screm, '\\_');
     // SQL LIKE underscore
     echo htmlspecialchars($screm . '_*: ' . Database::affected_rows(), ENT_QUOTES, $charset), '<br />';
 } elseif ($smo == get_lang('Remove')) {
     $mdStore->mds_delete($screm = EID_TYPE . '.' . $sdi);
     echo htmlspecialchars($screm . ': ' . Database::affected_rows(), ENT_QUOTES, $charset), '<br />';
     $mdStore->mds_delete_offspring($screm);
     echo htmlspecialchars($screm . '.*: ' . Database::affected_rows(), ENT_QUOTES, $charset), '<br /><br />', '<b>' . get_lang('AllRemovedFor') . ' ' . $screm . '</b><br />';
 } elseif ($smo == get_lang('Index') && file_exists($phpDigIncCn) && ereg('^http://([^/]+)/(.+)/index\\.php$', $mdObj->mdo_url, $regs)) {
     $result = $mdStore->mds_get_many('eid,mdxmltext,indexabletext', "eid LIKE '" . EID_TYPE . "." . $sdi . ($sdisub ? "." . $sdisub . "\\_%'" : ".%'") . ($sdiall ? "" : " AND NOT INSTR(eid,'_')"));
     // SQL LIKE underscore
     while ($row = Database::fetch_array($result)) {
         // URL: index.php[?sid=xxx[&thumb=yyy]] (file[1]/@href: pptslnnn_t.jpg)
         $th = '';
         $indtxt = $row['indexabletext'];
         if (($fh = strpos($rx = $row['mdxmltext'], 'file href="')) !== false) {
	            $xht_doc->xmd_select_single_element(TREETOP), -1);

        $playIt = $baseWorkDir .'/'. $workWith . '/index.php';
        $fileHandler = @fopen($playIt, 'w');
        @fwrite($fileHandler, content_for_index_php($sdi));
        @fclose($fileHandler);

    	echo '<br>', htmlspecialchars($workWith, ENT_QUOTES, $charset);
    	if (file_exists($playIt)) echo '/index.php ',
    	    htmlspecialchars(date('Y/m/d H:i:s', filemtime($playIt)), ENT_QUOTES, $charset);
	}
}
elseif ($smo == get_lang('Remove') && $sdisub)
{
    $screm = EID_TYPE . '.' . $sdi . '.' . $sdisub;
    $mdStore->mds_delete_offspring($screm, '\_');  // SQL LIKE underscore
    echo htmlspecialchars($screm . '_*: ' . Database::affected_rows(), ENT_QUOTES, $charset), '<br />';
}
elseif ($smo == get_lang('Remove'))  // remove all, regardless of $sdiall
{
    $mdStore->mds_delete($screm = EID_TYPE . '.' . $sdi);
    echo htmlspecialchars($screm . ': ' . Database::affected_rows(), ENT_QUOTES, $charset), '<br />';
    $mdStore->mds_delete_offspring($screm);
    echo htmlspecialchars($screm . '.*: ' . Database::affected_rows(), ENT_QUOTES, $charset), '<br /><br />',
	'<b>' . get_lang('AllRemovedFor') . ' ' . $screm . '</b><br />';
}
elseif ($smo == get_lang('Index') && file_exists($phpDigIncCn) &&
        ereg('^http://([^/]+)/(.+)/index\.php$', $mdObj->mdo_url, $regs))
{
    $result = $mdStore->mds_get_many('eid,mdxmltext,indexabletext',
        "eid LIKE '" . EID_TYPE . "." . $sdi .