/**
 * Import PDFs
 * @param   string  Filename
 * @param   string  The subdirectory in which to put the files in each course
 */
function import_pdfs($file, $subDir = '/')
{
    $baseDir = api_get_path(SYS_ARCHIVE_PATH);
    $uploadPath = 'pdfimport/';
    $errors = array();
    if (!is_dir($baseDir . $uploadPath)) {
        @mkdir($baseDir . $uploadPath);
    }
    if (!unzip_uploaded_file($_FILES['import_file'], $uploadPath, $baseDir, 1024 * 1024 * 1024)) {
        error_log('Could not unzip uploaded file in ' . __FILE__ . ', line ' . __LINE__);
        return $errors;
    }
    $list = scandir($baseDir . $uploadPath);
    $i = 0;
    foreach ($list as $file) {
        if (substr($file, 0, 1) == '.' or !is_file($baseDir . $uploadPath . $file)) {
            continue;
        }
        $parts = preg_split('/_/', $file);
        $course = api_get_course_info($parts[0]);
        if (count($course) > 0) {
            // Build file info because handle_uploaded_document() needs it (name, type, size, tmp_name)
            $fileSize = filesize($baseDir . $uploadPath . $file);
            $docId = add_document($course, $subDir . '/' . $file, 'file', $fileSize, $parts[1] . ' ' . substr($parts[2], 0, -4));
            if ($docId > 0) {
                if (!is_file($baseDir . $uploadPath . $file)) {
                    error_log($baseDir . $uploadPath . $file . ' does not exists in ' . __FILE__);
                }
                if (is_file(api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document' . $subDir . '/' . $file)) {
                    error_log(api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document' . $subDir . '/' . $file . ' exists at destination in ' . __FILE__);
                }
                if (!is_writeable(api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document' . $subDir)) {
                    error_log('Destination ' . api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document' . $subDir . ' is NOT writeable in ' . __FILE__);
                }
                // Place each file in its folder in each course
                $move = rename($baseDir . $uploadPath . $file, api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document' . $subDir . '/' . $file);
                api_item_property_update($course, TOOL_DOCUMENT, $docId, 'DocumentAdded', api_get_user_id());
                // Redo visibility
                api_set_default_visibility($docId, TOOL_DOCUMENT);
                $errors[] = array('Line' => 0, 'Code' => $course['code'], 'Title' => $course['title']);
                // Now add a link to the file from the Course description tool
                $link = '<p>Sílabo de la asignatura <a href="' . api_get_path(WEB_CODE_PATH) . 'document/document.php?cidReq=' . $course['code'] . '&id_session=0&gidReq=0&action=download&id=' . $docId . '" target="_blank"><img src="' . api_get_path(WEB_IMG_PATH) . 'icons/32/pdf.png"></a></p>';
                $course_description = new CourseDescription();
                $session_id = api_get_session_id();
                $course_description->set_course_id($course['real_id']);
                $course_description->set_session_id($session_id);
                $course_description->set_title('Presentación de la asignatura');
                $course_description->set_content($link);
                $course_description->set_description_type(1);
                $course_description->insert();
            }
        } else {
            error_log($parts[0] . ' is not a course, apparently');
            $errors[] = array('Line' => 0, 'Code' => $parts[0], 'Title' => $parts[0] . ' - ' . get_lang('CodeDoesNotExists'));
        }
        $i++;
        //found at least one entry that is not a dir or a .
    }
    if ($i == 0) {
        $errors[] = array('Line' => 0, 'Code' => '.', 'Title' => get_lang('NoPDFFoundAtRoot'));
    }
    return $errors;
}
/**
 *
 * @author Hugues Peeters <*****@*****.**>
 *
 * @param  array $uploaded_file - follows the $_FILES Structure
 * @param  string $base_work_dir - base working directory of the module
 * @param  string $upload_path  - destination of the upload.
 *                               This path is to append to $base_work_dir
 * @param  int $max_filled_space - amount of bytes to not exceed in the base
 *                               working directory
 *
 * @return boolean true if it succeds, false otherwise
 */
function treat_uploaded_file($uploaded_file, $base_work_dir, $upload_path, $max_filled_space, $uncompress = '')
{
    $uploaded_file['name'] = stripslashes($uploaded_file['name']);
    if (!enough_size($uploaded_file['size'], $base_work_dir, $max_filled_space)) {
        return api_failure::set_failure('not_enough_space');
    }
    if ($uncompress == 'unzip' && preg_match('/.zip$/', strtolower($uploaded_file['name']))) {
        return unzip_uploaded_file($uploaded_file, $upload_path, $base_work_dir, $max_filled_space);
    } else {
        $file_name = trim($uploaded_file['name']);
        // CHECK FOR NO DESIRED CHARACTERS
        $file_name = api_replace_dangerous_char($file_name, 'strict');
        // TRY TO ADD AN EXTENSION TO FILES WITOUT EXTENSION
        $file_name = add_ext_on_mime($file_name, $uploaded_file['type']);
        // HANDLE PHP FILES
        $file_name = $file_name;
        // COPY THE FILE TO THE DESIRED DESTINATION
        if (move_uploaded_file($uploaded_file['tmp_name'], $base_work_dir . $upload_path . '/' . $file_name)) {
            set_default_settings($upload_path, $file_name);
        }
        return true;
    }
}