/**
 * clears and removes an entire dir
 * @param string $relativepath the path from dataroot
 * @param boolean $fulldelete if true, removes the dir root either
 * @param string $pathbase the base path
 * @return an array of entries wich are local names in path
 */
 function filesystem_clear_dir($relativepath, $fullDelete = false, $pathbase = null)
 {
     global $CFG;
     if (is_null($pathbase)) {
         $pathbase = $CFG->dataroot . '/';
     } elseif ($pathbase === '') {
         $pathbase = '';
     } else {
         $pathbase = $pathbase . '/';
     }
     if (@$CFG->filedebug) {
         mtrace("clearing dir <i>{$pathbase}{$relativepath}</i><br/>");
     }
     $exists = filesystem_is_dir($relativepath, $pathbase);
     if (!$exists && !$fullDelete) {
         return filesystem_create_dir($relativepath, $pathbase);
     }
     if (!$exists && $fullDelete) {
         return true;
     }
     $files = filesystem_scan_dir($relativepath, FS_SHOW_HIDDEN, FS_ALL_ENTRIES, $pathbase);
     foreach ($files as $aFile) {
         if ($aFile == "." || $aFile == "..") {
             continue;
         }
         if (filesystem_is_dir("{$relativepath}/{$aFile}", $pathbase)) {
             filesystem_clear_dir("{$relativepath}/{$aFile}", FS_FULL_DELETE, $pathbase);
             // fs_removeDir("{$relativepath}/{$aFile}");
         } else {
             filesystem_delete_file("{$relativepath}/{$aFile}", $pathbase);
         }
     }
     if (file_exists($pathbase . $relativepath) && $fullDelete) {
         return filesystem_remove_dir($relativepath, $pathbase);
     }
     return false;
 }
/**
* clears element recordings for an issue
* @TODO check if it is really used
* @param int $issueid the issue
*/
function tracker_clearelements($issueid)
{
    global $CFG, $COURSE;
    if (!($issue = get_record('tracker_issue', 'id', "{$issueid}"))) {
        return;
    }
    // find all files elements to protect
    $sql = "\n            SELECT\n                e.id,\n                e.type\n            FROM\n                {$CFG->prefix}tracker_element e,\n                {$CFG->prefix}tracker_elementused eu\n            WHERE\n                e.id = eu.elementid AND\n                e.type = 'file' AND\n                eu.trackerid = {$issue->trackerid}\n    ";
    $nofileclause = '';
    if ($fileelements = get_records_sql($sql)) {
        $fileelementlist = implode("','", array_keys($fileelements));
        $nofileclause = " AND elementid NOT IN ('{$fileelementlist}') ";
    }
    if (!delete_records_select('tracker_issueattribute', "issueid = {$issueid} {$nofileclause}")) {
        error("Could not clear elements for issue {$issueid}");
    }
    $storebase = "{$COURSE->id}/moddata/tracker/{$issue->trackerid}/{$issue->id}";
    // remove all deleted file attachements
    $keys = array_keys($_POST);
    $deletefilekeys = preg_grep('/deleteelement./', $keys);
    // filter out only the deleteelement keys
    if (!empty($deletefilekeys)) {
        foreach ($deletefilekeys as $deletedkey) {
            if (preg_match("/deleteelement(.*)\$/", $deletedkey, $matches)) {
                $elementname = $matches[1];
                $element = get_record('tracker_element', 'name', $elementname);
                if ($elementitem = get_record('tracker_issueattribute', 'elementid', $element->id, 'issueid', $issueid)) {
                    if (!empty($elementitem->elementitemid)) {
                        filesystem_delete_file($storebase . '/' . $elementitem->elementitemid);
                    }
                    delete_records('tracker_issueattribute', 'id', $elementitem->id);
                }
            }
        }
    }
    // remove all reloaded files
    $keys = array_keys($_FILES);
    $reloadedfilekeys = preg_grep('/element./', $keys);
    // filter out only the reloaded element keys
    if (!empty($reloadedfilekeys)) {
        foreach ($reloadedfilekeys as $reloadedkey) {
            if (preg_match("/element(.*)\$/", $reloadedkey, $matches)) {
                $elementname = $matches[1];
                $element = get_record('tracker_element', 'name', $elementname);
                if ($elementitem = get_record('tracker_issueattribute', 'elementid', $element->id, 'issueid', $issueid)) {
                    if (!empty($elementitem->elementitemid)) {
                        // echo "removing ".$storebase.'/'.$elementitem->elementitemid;
                        filesystem_delete_file($storebase . '/' . $elementitem->elementitemid);
                    }
                    delete_records('tracker_issueattribute', 'id', $elementitem->id);
                }
            }
        }
    }
}