"terms          TEXT                NULL," .                        // contains the indexing tags (search engine)
        "search_did     INT                 NULL,".                         // contains the internal search-engine id of this element
        "audio          VARCHAR(250),
        PRIMARY KEY  (c_id, id)
        )" . $charset_clause;                   // contains the audio file that goes with the learning path step
    Database::query($sql);
    $sql = "ALTER TABLE `$TABLELPITEM` ADD INDEX (lp_id)";
    Database::query($sql);
    $sql = "ALTER TABLE $TABLELPITEM ADD INDEX idx_c_lp_item_cid_lp_id (c_id, lp_id)";
    Database::query($sql);
    $sql = "CREATE TABLE IF NOT EXISTS `$TABLELPITEMVIEW` (
        $add_to_all_tables
        " .
        "id             bigint unsigned auto_increment," .      // unique ID
        "lp_item_id     int unsigned    not null," .                        // item ID (MySQL id)
        "lp_view_id     int unsigned    not null," .                        // learning path view id (attempt)
        "view_count     int unsigned    not null default 0," .              // how many times this item has been viewed in the current attempt (generally 0 or 1)
        "start_time     int unsigned    not null," .                        // when did the user open it?
        "total_time     int unsigned    not null default 0," .              // after how many seconds did he close it?
        "score          float unsigned  not null default 0," .              // score returned by SCORM or other techs
        "status         char(32)        not null default 'not attempted'," .    // status for this item (SCORM)
        "suspend_data   longtext null default ''," .
        "lesson_location    text        null default ''," .
        "core_exit      varchar(32)     not null default 'none'," .
        "max_score      varchar(8)      default '',
        PRIMARY KEY  (c_id, id)
        )" . $charset_clause;
    Database::query($sql);
    $sql = "ALTER TABLE `$TABLELPITEMVIEW` ADD INDEX (lp_item_id) ";
    Database::query($sql);
    $sql = "ALTER TABLE `$TABLELPITEMVIEW` ADD INDEX (lp_view_id) ";
    Database::query($sql);
    $sql = "ALTER TABLE $TABLELPITEMVIEW ADD INDEX idx_c_lp_item_view_cid_lp_view_id_lp_item_id (c_id, lp_view_id, lp_item_id) ";
    Database::query($sql);
    $sql = "CREATE TABLE IF NOT EXISTS `$TABLELPIVINTERACTION`(
         $add_to_all_tables" .
        "id             bigint unsigned     auto_increment," .
        "order_id       int unsigned   not null default 0,".           // internal order (0->...) given by Dokeos
        "lp_iv_id       bigint unsigned     not null," .                    // identifier of the related sco_view
        "interaction_id varchar(255)        not null default ''," .         // sco-specific, given by the sco
        "interaction_type   varchar(255)    not null default ''," .         // literal values, SCORM-specific (see p.63 of SCORM 1.2 RTE)
        "weighting          double          not null default 0," .
        "completion_time    varchar(16)     not null default ''," .         // completion time for the interaction (timestamp in a day's time) - expected output format is scorm time
        "correct_responses  text            not null default ''," .         // actually a serialised array. See p.65 os SCORM 1.2 RTE)
        "student_response   text            not null default ''," .         // student response (format depends on type)
        "result         varchar(255)        not null default ''," .         // textual result
        "latency        varchar(16)         not null default ''," .          // time necessary for completion of the interaction
        "PRIMARY KEY  (c_id, id)".
        ")" . $charset_clause;
    Database::query($sql);
    $sql = "ALTER TABLE `$TABLELPIVINTERACTION` ADD INDEX (lp_iv_id) ";
    Database::query($sql);
    $sql = "CREATE TABLE IF NOT EXISTS `$TABLELPIVOBJECTIVE`(
        $add_to_all_tables" .
        "id             bigint unsigned     auto_increment," .
        "lp_iv_id       bigint unsigned     not null," .                    // identifier of the related sco_view
        "order_id       int unsigned   not null default 0,".           // internal order (0->...) given by Dokeos
        "objective_id   varchar(255)        not null default ''," .         // sco-specific, given by the sco
        "score_raw      float unsigned      not null default 0," .          // score
        "score_max      float unsigned      not null default 0," .          // max score
        "score_min      float unsigned      not null default 0," .          // min score
        "status         char(32)            not null default 'not attempted', " . //status, just as sco status
        "PRIMARY KEY  (c_id, id) ".
        ")" . $charset_clause;
    Database::query($sql);
    $sql = "ALTER TABLE `$TABLELPIVOBJECTIVE` ADD INDEX (lp_iv_id) ";
    Database::query($sql);
    /* Blogs */
    $sql = "
        CREATE TABLE `" . $tbl_blogs . "` (
            $add_to_all_tables
            blog_id int NOT NULL AUTO_INCREMENT ,
            blog_name varchar( 250 ) NOT NULL default '',
            blog_subtitle varchar( 250 ) default NULL ,
            date_creation datetime NOT NULL default '0000-00-00 00:00:00',
            visibility tinyint unsigned NOT NULL default 0,
            session_id int default 0,
            PRIMARY KEY (c_id, blog_id )
        )" . $charset_clause . " COMMENT = 'Table with blogs in this course';";
    Database::query($sql);
    $sql = "ALTER TABLE `".$tbl_blogs . "` ADD INDEX ( session_id ) ";
    Database::query($sql);
    $sql = "
        CREATE TABLE `" . $tbl_blogs_comments . "` (
            $add_to_all_tables
            comment_id int NOT NULL AUTO_INCREMENT ,
            title varchar( 250 ) NOT NULL default '',
            comment longtext NOT NULL ,
            author_id int NOT NULL default 0,
            date_creation datetime NOT NULL default '0000-00-00 00:00:00',
            blog_id int NOT NULL default 0,
            post_id int NOT NULL default 0,
            task_id int default NULL ,
            parent_comment_id int NOT NULL default 0,
            PRIMARY KEY (c_id, comment_id )
        )" . $charset_clause . " COMMENT = 'Table with comments on posts in a blog';";
    Database::query($sql);
    $sql = "
        CREATE TABLE `" . $tbl_blogs_posts . "` (
            $add_to_all_tables
            post_id int NOT NULL AUTO_INCREMENT ,
            title varchar( 250 ) NOT NULL default '',
            full_text longtext NOT NULL ,
            date_creation datetime NOT NULL default '0000-00-00 00:00:00',
            blog_id int NOT NULL default 0,
            author_id int NOT NULL default 0,
            PRIMARY KEY (c_id, post_id )
        )" . $charset_clause . " COMMENT = 'Table with posts / blog.';";
    Database::query($sql);
    $sql = "
        CREATE TABLE `" . $tbl_blogs_rating . "` (
            $add_to_all_tables
            rating_id int NOT NULL AUTO_INCREMENT ,
            blog_id int NOT NULL default 0,
            rating_type enum( 'post', 'comment' ) NOT NULL default 'post',
            item_id int NOT NULL default 0,
            user_id int NOT NULL default 0,
            rating int NOT NULL default 0,
            PRIMARY KEY (c_id, rating_id )
        )" . $charset_clause . " COMMENT = 'Table with ratings for post/comments in a certain blog';";
    Database::query($sql);
    $sql = "
        CREATE TABLE `" . $tbl_blogs_rel_user . "` (
            $add_to_all_tables
            blog_id int NOT NULL default 0,
            user_id int NOT NULL default 0,
            PRIMARY KEY ( c_id, blog_id , user_id )
        )" . $charset_clause . " COMMENT = 'Table representing users subscribed to a blog';";
    Database::query($sql);
    $sql = "
        CREATE TABLE `" . $tbl_blogs_tasks . "` (
            $add_to_all_tables
            task_id int NOT NULL AUTO_INCREMENT ,
            blog_id int NOT NULL default 0,
            title varchar( 250 ) NOT NULL default '',
            description text NOT NULL ,
            color varchar( 10 ) NOT NULL default '',
            system_task tinyint unsigned NOT NULL default 0,
            PRIMARY KEY (c_id, task_id )
        )" . $charset_clause . " COMMENT = 'Table with tasks for a blog';";
    Database::query($sql);
    $sql = "
        CREATE TABLE `" . $tbl_blogs_tasks_rel_user . "` (
            $add_to_all_tables
            blog_id int NOT NULL default 0,
            user_id int NOT NULL default 0,
            task_id int NOT NULL default 0,
            target_date date NOT NULL default '0000-00-00',
            PRIMARY KEY (c_id, blog_id , user_id , task_id )
        )" . $charset_clause . " COMMENT = 'Table with tasks assigned to a user in a blog';";
    Database::query($sql);
    $sql ="CREATE TABLE  `" .$tbl_blogs_attachment."` (
          $add_to_all_tables
          id int unsigned NOT NULL auto_increment,
          path varchar(255) NOT NULL COMMENT 'the real filename',
          comment text,
          size int NOT NULL default '0',
          post_id int NOT NULL,
          filename varchar(255) NOT NULL COMMENT 'the user s file name',
          blog_id int NOT NULL,
          comment_id int NOT NULL default '0',
          PRIMARY KEY  (c_id, id)
        )" . $charset_clause;
    Database::query($sql);
    $sql = "
        CREATE TABLE `" . $tbl_permission_group . "` (
            $add_to_all_tables
            id int NOT NULL AUTO_INCREMENT ,
            group_id int NOT NULL default 0,
            tool varchar( 250 ) NOT NULL default '',
            action varchar( 250 ) NOT NULL default '',
            PRIMARY KEY (c_id, id)
        )" . $charset_clause;
    Database::query($sql);
    $sql = "
        CREATE TABLE `" . $tbl_permission_user . "` (
            $add_to_all_tables
            id int NOT NULL AUTO_INCREMENT ,
            user_id int NOT NULL default 0,
            tool varchar( 250 ) NOT NULL default '',
            action varchar( 250 ) NOT NULL default '',
            PRIMARY KEY (c_id, id )
        )" . $charset_clause;
    Database::query($sql);
    $sql = "
        CREATE TABLE `" . $tbl_permission_task . "` (
            $add_to_all_tables
            id int NOT NULL AUTO_INCREMENT,
            task_id int NOT NULL default 0,
            tool varchar( 250 ) NOT NULL default '',
            action varchar( 250 ) NOT NULL default '',
            PRIMARY KEY (c_id, id )
        )" . $charset_clause;
    Database::query($sql);
    $sql = "
        CREATE TABLE `" . $tbl_role . "` (
            $add_to_all_tables
            role_id int NOT NULL AUTO_INCREMENT,
            role_name varchar( 250 ) NOT NULL default '',
            role_comment text,
            default_role tinyint default 0,
            PRIMARY KEY (c_id, role_id)
        )" . $charset_clause;
    Database::query($sql);
    $sql = "
        CREATE TABLE `" . $tbl_role_group . "` (
            $add_to_all_tables
            id int NOT NULL AUTO_INCREMENT,
            role_id int NOT NULL default 0,
            scope varchar( 20 ) NOT NULL default 'course',
            group_id int NOT NULL default 0,
            PRIMARY KEY  (id, c_id, group_id )
        )" . $charset_clause;
    Database::query($sql);
    $sql = "
        CREATE TABLE `" . $tbl_role_permissions . "` (
            $add_to_all_tables
            id int NOT NULL AUTO_INCREMENT,
            role_id int NOT NULL default 0,
            tool varchar( 250 ) NOT NULL default '',
            action varchar( 50 ) NOT NULL default '',
            default_perm tinyint NOT NULL default 0,
            PRIMARY KEY  (id, c_id, role_id, tool, action )
        )" . $charset_clause;
    Database::query($sql);
    $sql = "
        CREATE TABLE `" . $tbl_role_user . "` (
            $add_to_all_tables
            role_id int NOT NULL default 0,
            scope varchar( 20 ) NOT NULL default 'course',
            user_id int NOT NULL default 0,
            PRIMARY KEY  ( c_id, role_id, user_id )
        )" . $charset_clause;
    Database::query($sql);
    /*
     * Course Config Settings
     *
     */
    Database::query("
        CREATE TABLE `".$TABLESETTING . "` (
        $add_to_all_tables
        id          int unsigned NOT NULL auto_increment,
        variable    varchar(255) NOT NULL default '',
        subkey      varchar(255) default NULL,
        type        varchar(255) default NULL,
        category    varchar(255) default NULL,
        value       varchar(255) NOT NULL default '',
        title       varchar(255) NOT NULL default '',
        comment     varchar(255) default NULL,
        subkeytext  varchar(255) default NULL,
        PRIMARY KEY (c_id, id)
         )" . $charset_clause);
    /*
        Survey
    */
    $sql = "CREATE TABLE `".$TABLESURVEY."` (
            $add_to_all_tables
              survey_id int unsigned NOT NULL auto_increment,
              code varchar(20) default NULL,
              title text default NULL,
              subtitle text default NULL,
              author varchar(20) default NULL,
              lang varchar(20) default NULL,
              avail_from date default NULL,
              avail_till date default NULL,
              is_shared char(1) default '1',
              template varchar(20) default NULL,
              intro text,
              surveythanks text,
              creation_date datetime NOT NULL default '0000-00-00 00:00:00',
              invited int NOT NULL,
              answered int NOT NULL,
              invite_mail text NOT NULL,
              reminder_mail text NOT NULL,
              mail_subject VARCHAR( 255 ) NOT NULL,
              anonymous enum('0','1') NOT NULL default '0',
              access_condition TEXT DEFAULT NULL,
              shuffle bool NOT NULL default '0',
              one_question_per_page bool NOT NULL default '0',
              survey_version varchar(255) NOT NULL default '',
              parent_id int unsigned NOT NULL,
              survey_type int NOT NULL default 0,
              show_form_profile int NOT NULL default 0,
              form_fields TEXT NOT NULL,
              session_id int unsigned NOT NULL default 0,
              visible_results int unsigned DEFAULT 0,
              PRIMARY KEY  (c_id, survey_id)
            )" . $charset_clause;
    $result = Database::query($sql);
    $sql = "ALTER TABLE `".$TABLESURVEY."` ADD INDEX ( session_id )";
    Database::query($sql);
    $sql = "CREATE TABLE `".$TABLESURVEYINVITATION."` (
              $add_to_all_tables
              survey_invitation_id int unsigned NOT NULL auto_increment,
              survey_code varchar(20) NOT NULL,
              user varchar(250) NOT NULL,
              invitation_code varchar(250) NOT NULL,
              invitation_date datetime NOT NULL,
              reminder_date datetime NOT NULL,
              answered int NOT NULL default 0,
              session_id int UNSIGNED NOT NULL default 0,
              PRIMARY KEY  (c_id, survey_invitation_id)
            )" . $charset_clause;
    $result = Database::query($sql);
    $sql = "CREATE TABLE `".$TABLESURVEYQUESTION."` (
              $add_to_all_tables
              question_id int unsigned NOT NULL auto_increment,
              survey_id int unsigned NOT NULL,
              survey_question text NOT NULL,
              survey_question_comment text NOT NULL,
              type varchar(250) NOT NULL,
              display varchar(10) NOT NULL,
              sort int NOT NULL,
              shared_question_id int,
              max_value int,
              survey_group_pri int unsigned NOT NULL default '0',
              survey_group_sec1 int unsigned NOT NULL default '0',
              survey_group_sec2 int unsigned NOT NULL default '0',
              PRIMARY KEY  (c_id, question_id)
            )" . $charset_clause;
    $result = Database::query($sql);
    $sql ="CREATE TABLE `".$TABLESURVEYQUESTIONOPTION."` (
        $add_to_all_tables
      question_option_id int unsigned NOT NULL auto_increment,
      question_id int unsigned NOT NULL,
      survey_id int unsigned NOT NULL,
      option_text text NOT NULL,
      sort int NOT NULL,
      value int NOT NULL default '0',
      PRIMARY KEY  (c_id, question_option_id)
    )" . $charset_clause;
    $result = Database::query($sql);
    $sql = "CREATE TABLE `".$TABLESURVEYANSWER."` (
              $add_to_all_tables
              answer_id int unsigned NOT NULL auto_increment,
              survey_id int unsigned NOT NULL,
              question_id int unsigned NOT NULL,
              option_id TEXT NOT NULL,
              value int unsigned NOT NULL,
              user varchar(250) NOT NULL,
              PRIMARY KEY  (c_id, answer_id)
            )" . $charset_clause;
    $result = Database::query($sql);
    $sql = "CREATE TABLE `".$TABLESURVEYGROUP."` (
                $add_to_all_tables
              id int unsigned NOT NULL auto_increment,
              name varchar(20) NOT NULL,
              description varchar(255) NOT NULL,
              survey_id int unsigned NOT NULL,
              PRIMARY KEY  (c_id, id)
            )" . $charset_clause;
    $result = Database::query($sql);
    // Table glosary
    $sql = "CREATE TABLE `".$TBL_GLOSSARY."` (
              $add_to_all_tables
              glossary_id int unsigned NOT NULL auto_increment,
              name varchar(255) NOT NULL,
              description text not null,
              display_order int,
              session_id int default 0,
              PRIMARY KEY  (c_id, glossary_id)
            )" . $charset_clause;
    $result = Database::query($sql);
    $sql = "ALTER TABLE `".$TBL_GLOSSARY . "` ADD INDEX ( session_id ) ";
    Database::query($sql);
    // Table notebook
    $sql = "CREATE TABLE `".$TBL_NOTEBOOK."` (
              $add_to_all_tables
              notebook_id int unsigned NOT NULL auto_increment,
              user_id int unsigned NOT NULL,
              course varchar(40) not null,
              session_id int NOT NULL default 0,
              title varchar(255) NOT NULL,
              description text NOT NULL,
              creation_date datetime NOT NULL default '0000-00-00 00:00:00',
              update_date datetime NOT NULL default '0000-00-00 00:00:00',
              status int,
              PRIMARY KEY  (c_id, notebook_id)
            )" . $charset_clause;
    $result = Database::query($sql);
    /* Attendance tool */
    // Attendance table
    $sql = "
        CREATE TABLE `".$TBL_ATTENDANCE."` (
            $add_to_all_tables
            id int NOT NULL auto_increment,
            name text NOT NULL,
            description TEXT NULL,
            active tinyint NOT NULL default 1,
            attendance_qualify_title varchar(255) NULL,
            attendance_qualify_max int NOT NULL default 0,
            attendance_weight float(6,2) NOT NULL default '0.0',
            session_id int NOT NULL default 0,
            locked int NOT NULL default 0,
            PRIMARY KEY  (c_id, id)
        )" . $charset_clause;
    $result = Database::query($sql);
    $sql  = "ALTER TABLE `".$TBL_ATTENDANCE . "` ADD INDEX (session_id)";
    Database::query($sql);
    $sql  = "ALTER TABLE `".$TBL_ATTENDANCE . "` ADD INDEX (active)";
    Database::query($sql);
    // Attendance sheet table
    $sql = "
        CREATE TABLE `".$TBL_ATTENDANCE_SHEET."` (
            $add_to_all_tables
            user_id int NOT NULL,
            attendance_calendar_id int NOT NULL,
            presence tinyint NOT NULL DEFAULT 0,
            PRIMARY KEY(c_id, user_id, attendance_calendar_id)
        )" . $charset_clause;
    $result = Database::query($sql);
    $sql  = "ALTER TABLE `".$TBL_ATTENDANCE_SHEET . "` ADD INDEX (presence) ";
    Database::query($sql);
    // Attendance calendar table
    $sql = "
        CREATE TABLE `".$TBL_ATTENDANCE_CALENDAR."` (
            $add_to_all_tables
            id int NOT NULL auto_increment,
            attendance_id int NOT NULL ,
            date_time datetime NOT NULL default '0000-00-00 00:00:00',
            done_attendance tinyint NOT NULL default 0,
            PRIMARY KEY(c_id, id)
        )" . $charset_clause;
    $result = Database::query($sql);
    $sql  = "ALTER TABLE `".$TBL_ATTENDANCE_CALENDAR."` ADD INDEX (attendance_id)";
    Database::query($sql);
    $sql  = "ALTER TABLE `".$TBL_ATTENDANCE_CALENDAR."` ADD INDEX (done_attendance)";
    Database::query($sql);
    // Attendance result table
    $sql = "
        CREATE TABLE `".$TBL_ATTENDANCE_RESULT."` (
            $add_to_all_tables
            id int NOT NULL auto_increment,
            user_id int NOT NULL,
            attendance_id int NOT NULL,
            score int NOT NULL DEFAULT 0,
            PRIMARY KEY  (c_id, id)
        )" . $charset_clause;
    $result = Database::query($sql);
    $sql    = "ALTER TABLE `".$TBL_ATTENDANCE_RESULT."` ADD INDEX (attendance_id)";
    Database::query($sql);
    $sql    = "ALTER TABLE `".$TBL_ATTENDANCE_RESULT."` ADD INDEX (user_id)";
    Database::query($sql);
    // attendance sheet log table
    $sql = "CREATE TABLE `".$TBL_ATTENDANCE_SHEET_LOG."` (
                  $add_to_all_tables
                  id int  NOT NULL auto_increment,
                  attendance_id int  NOT NULL DEFAULT 0,
                  lastedit_date datetime  NOT NULL DEFAULT '0000-00-00 00:00:00',
                  lastedit_type varchar(200)  NOT NULL,
                  lastedit_user_id int  NOT NULL DEFAULT 0,
                  calendar_date_value datetime NULL,
                  PRIMARY KEY (c_id, id)
                )" . $charset_clause;
    $result = Database::query($sql) or die(Database::error());
    // Thematic table
    $sql = "CREATE TABLE `".$TBL_THEMATIC."` (
                $add_to_all_tables
                id int NOT NULL auto_increment,
                title varchar(255) NOT NULL,
                content text NULL,
                display_order int unsigned NOT NULL DEFAULT 0,
                active tinyint NOT NULL DEFAULT 0,
                session_id int NOT NULL DEFAULT 0,
                PRIMARY KEY  (c_id, id)
            )" . $charset_clause;
    $result = Database::query($sql);
    $sql    = "ALTER TABLE `".$TBL_THEMATIC."` ADD INDEX (active, session_id)";
    Database::query($sql);
    // thematic plan table
    $sql = "CREATE TABLE `".$TBL_THEMATIC_PLAN."` (
                $add_to_all_tables
                id int NOT NULL auto_increment,
                thematic_id int NOT NULL,
                title varchar(255) NOT NULL,
                description text NULL,
                description_type int NOT NULL,
                PRIMARY KEY  (c_id, id)
            )" . $charset_clause;
    $result = Database::query($sql);
    $sql    = "ALTER TABLE `".$TBL_THEMATIC_PLAN."` ADD INDEX (thematic_id, description_type)";
    Database::query($sql);
    // thematic advance table
    $sql = "
            CREATE TABLE `".$TBL_THEMATIC_ADVANCE."` (
                $add_to_all_tables
                id int NOT NULL auto_increment,
                thematic_id int NOT NULL,
                attendance_id int NOT NULL DEFAULT 0,
                content text NULL,
                start_date datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
                duration int NOT NULL DEFAULT 0,
                done_advance tinyint NOT NULL DEFAULT 0,
                PRIMARY KEY  (c_id, id)
            )" . $charset_clause;
    $result = Database::query($sql);
    $sql    = "ALTER TABLE `".$TBL_THEMATIC_ADVANCE."` ADD INDEX (thematic_id)";
    Database::query($sql);
    $sql = "CREATE TABLE IF NOT EXISTS " . $TBL_METADATA . " (    " .
            $add_to_all_tables.
            "eid VARCHAR(250) NOT NULL," .      // entry-id, e.g. doc.1
            "mdxmltext TEXT default ''," .      // MD-text, XML-formatted
            "md5 CHAR(32) default ''," .        // hash-validator
            "htmlcache1 TEXT default ''," .     // cached HTML, part 1
            "htmlcache2 TEXT default ''," .     // cached HTML, part 2
            "indexabletext TEXT default ''," .  // indexable for search
            "PRIMARY KEY (c_id, eid)           )".$charset_clause;
    Database::query($sql);
    // New course tables for 1.10.x come here
    return 0;
}
/**
 * Returns a list of all files in the given course directory. The requested
 * directory will be checked against a "checker" directory to avoid access to
 * protected/unauthorized files
 * @param string Complete path to directory we want to list
 * @param array A list of files to which we want to add the files found
 * @param string Type of base directory from which we want to recover the files
 * @return array
 * @assert (null,null,null) === false
 * @assert ('abc',array(),'') === array()
 */
function browse_folders($path, $files, $media) {
    if ($media == 'images') {
        $code_path = api_get_path(SYS_CODE_PATH).'default_course_document/images/';
    }
    if ($media == 'audio') {
        $code_path = api_get_path(SYS_CODE_PATH).'default_course_document/audio/';
    }
    if ($media == 'flash') {
        $code_path = api_get_path(SYS_CODE_PATH).'default_course_document/flash/';
    }
    if ($media == 'video') {
        $code_path = api_get_path(SYS_CODE_PATH).'default_course_document/video/';
    }
    if ($media == 'certificates') {
        $code_path = api_get_path(SYS_CODE_PATH).'default_course_document/certificates/';
    }
    if (is_dir($path)) {
        $handle = opendir($path);
        while (false !== ($file = readdir($handle))) {
            if (is_dir($path.$file) && strpos($file, '.') !== 0) {
                $files[]['dir'] = str_replace($code_path, '', $path.$file.'/');
                $files = browse_folders($path.$file.'/', $files, $media);
            } elseif (is_file($path.$file) && strpos($file, '.') !== 0) {
                $files[]['file'] = str_replace($code_path, '', $path.$file);
            }
        }
    }
    return $files;
}
/**
 * Sorts pictures by type (used?)
 * @param array List of files (sthg like array(0=>array('png'=>1)))
 * @param string File type
 * @return array The received array without files not matching type
 * @assert (array(),null) === array()
 */
function sort_pictures($files, $type) {
    $pictures = array();
    foreach ($files as $key => $value){
        if ($value[$type] != '') {
            $pictures[][$type] = $value[$type];
        }
    }
    return $pictures;
}
/**
 * Fills the course repository with some example content.
 * @param   string Course directory name (without prefix/suffix). eg "ABC"
 * @param   bool Whether we want to fill it with example content or not
 * @return  array The (structured) list of files created
 * @version 1.2
 * @deprecated this function has been merged into the fill_db_course
 * @assert (null, null) === false
 */
function fill_course_repository($course_repository, $fill_with_exemplary_content = null) {
    if (is_null($fill_with_exemplary_content)) {
        $fill_with_exemplary_content = api_get_setting('example_material_course_creation') != 'false';
    }
    $default_document_array = array();
    if ($fill_with_exemplary_content) {
        $sys_course_path = api_get_path(SYS_COURSE_PATH);
        $perm = api_get_permissions_for_new_directories();
        $perm_file = api_get_permissions_for_new_files();
        $img_code_path   = api_get_path(SYS_CODE_PATH).'default_course_document/images/';
        $audio_code_path = api_get_path(SYS_CODE_PATH).'default_course_document/audio/';
        $flash_code_path = api_get_path(SYS_CODE_PATH).'default_course_document/flash/';
        $video_code_path = api_get_path(SYS_CODE_PATH).'default_course_document/video/';
        $cert_code_path = api_get_path(SYS_CODE_PATH).'default_course_document/certificates/';
        $course_documents_folder_images = $sys_course_path.$course_repository.'/document/images/gallery/';
        $course_documents_folder_audio = $sys_course_path.$course_repository.'/document/audio/';
        $course_documents_folder_flash = $sys_course_path.$course_repository.'/document/flash/';
        $course_documents_folder_video = $sys_course_path.$course_repository.'/document/video/';
        $course_documents_folder_cert = $sys_course_path.$course_repository.'/document/certificates/';
        /* Images */
        $files = array();
        $files = browse_folders($img_code_path, $files, 'images');
        $pictures_array = sort_pictures($files, 'dir');
        $pictures_array = array_merge($pictures_array, sort_pictures($files, 'file'));
        if (!is_dir($course_documents_folder_images)) {
            mkdir($course_documents_folder_images,$perm);
        }
        $handle = opendir($img_code_path);
        foreach ($pictures_array as $key => $value) {
            if ($value['dir'] != '') {
                mkdir($course_documents_folder_images.$value['dir'], $perm);
            }
            if ($value['file'] != '') {
                copy($img_code_path.$value['file'], $course_documents_folder_images.$value['file']);
                chmod($course_documents_folder_images.$value['file'], $perm_file);
            }
        }
        // Trainer thumbnails fix.
        $path_thumb = mkdir($course_documents_folder_images.'trainer/.thumbs', $perm);
        $handle = opendir($img_code_path.'trainer/.thumbs/');
        while (false !== ($file = readdir($handle))) {
            if (is_file($img_code_path.'trainer/.thumbs/'.$file)) {
                copy($img_code_path.'trainer/.thumbs/'.$file, $course_documents_folder_images.'trainer/.thumbs/'.$file);
                chmod($course_documents_folder_images.'trainer/.thumbs/'.$file, $perm_file);
            }
        }
        $default_document_array['images'] = $pictures_array;
        /* Audio */
        $files = array();
        $files = browse_folders($audio_code_path, $files, 'audio');
        $audio_array = sort_pictures($files, 'dir');
        $audio_array = array_merge($audio_array,sort_pictures($files, 'file'));
        if (!is_dir($course_documents_folder_audio)) {
            mkdir($course_documents_folder_audio, $perm);
        }
        $handle = opendir($audio_code_path);
        foreach ($audio_array as $key => $value){
            if ($value['dir'] != '') {
                mkdir($course_documents_folder_audio.$value['dir'], $perm);
            }
            if ($value['file'] != '') {
                copy($audio_code_path.$value['file'], $course_documents_folder_audio.$value['file']);
                chmod($course_documents_folder_audio.$value['file'], $perm_file);
            }
        }
        $default_document_array['audio'] = $audio_array;
        /*
         * Flash
         */
        $files = array();
        $files = browse_folders($flash_code_path, $files, 'flash');
        $flash_array = sort_pictures($files, 'dir');
        $flash_array = array_merge($flash_array, sort_pictures($files, 'file'));
        if (!is_dir($course_documents_folder_flash)) {
            mkdir($course_documents_folder_flash, $perm);
        }
        $handle = opendir($flash_code_path);
        foreach ($flash_array as $key => $value) {
            if ($value['dir'] != '') {
                mkdir($course_documents_folder_flash.$value['dir'], $perm);
            }
            if ($value['file'] != '') {
                copy($flash_code_path.$value['file'], $course_documents_folder_flash.$value['file']);
                chmod($course_documents_folder_flash.$value['file'], $perm_file);
            }
        }
        $default_document_array['flash'] = $flash_array;
        /* Video */
        $files = browse_folders($video_code_path, $files, 'video');
        $video_array = sort_pictures($files, 'dir');
        $video_array = array_merge($video_array, sort_pictures($files, 'file'));
        if (!is_dir($course_documents_folder_video)) {
            mkdir($course_documents_folder_video, $perm);
        }
        $handle = opendir($video_code_path);
        foreach ($video_array as $key => $value) {
            if ($value['dir'] != '') {
                @mkdir($course_documents_folder_video.$value['dir'], $perm);
            }
            if ($value['file'] != '') {
                copy($video_code_path.$value['file'], $course_documents_folder_video.$value['file']);
                chmod($course_documents_folder_video.$value['file'], $perm_file);
            }
        }
        $default_document_array['video'] = $video_array;
        /* Certificates */
        $files = browse_folders($cert_code_path, $files, 'certificates');
        $cert_array = sort_pictures($files, 'dir'); //only one file so far
        $cert_array = array_merge($cert_array, sort_pictures($files, 'file'));
        if (!is_dir($course_documents_folder_cert)) {
            mkdir($course_documents_folder_cert, $perm);
        }
        $handle = opendir($cert_code_path);
        foreach ($cert_array as $key => $value) {
            if ($value['dir'] != '') {
                @mkdir($course_documents_folder_cert.$value['dir'], $perm);
            }
            if ($value['file'] != '') {
                copy($cert_code_path.$value['file'], $course_documents_folder_cert.$value['file']);
                chmod($course_documents_folder_cert.$value['file'], $perm_file);
            }
        }
        $default_document_array['cert'] = $cert_array;
    }
    return $default_document_array;
}
/**
 * Function to convert a string from the language files to a string ready
 * to insert into the database (escapes single quotes)
 * @author Bart Mollet (bart.mollet@hogent.be)
 * @param string $string The string to convert
 * @return string The string converted to insert into the database
 * @assert ('a\'b') === 'ab'
 */
function lang2db($string) {
    $string = str_replace("\\'", "'", $string);
    $string = Database::escape_string($string);
    return $string;
}
/**
 * Fills the course database with some required content and example content.
 * @param int Course (int) ID
 * @param string Course directory name (e.g. 'ABC')
 * @param string Language used for content (e.g. 'spanish')
 * @param bool Whether to fill the course with example content
 * @return bool False on error, true otherwise
 * @version 1.2
 * @assert (null, '', '', null) === false
 * @assert (1, 'ABC', null, null) === false
 * @assert (1, 'TEST', 'spanish', true) === true
 */
function fill_db_course($course_id, $course_repository, $language, $fill_with_exemplary_content = null) {
    if (is_null($fill_with_exemplary_content)) {
        $fill_with_exemplary_content = api_get_setting('example_material_course_creation') != 'false';
    }
    global $_configuration;
    $course_id = intval($course_id);
    if (empty($course_id)) {
        return false;
    }
    $now = api_get_utc_datetime(time());
    $tbl_course_homepage    = Database::get_course_table(TABLE_TOOL_LIST);
    $TABLEINTROS            = Database::get_course_table(TABLE_TOOL_INTRO);
    $TABLEGROUPCATEGORIES   = Database::get_course_table(TABLE_GROUP_CATEGORY);
    $TABLEITEMPROPERTY      = Database::get_course_table(TABLE_ITEM_PROPERTY);
    $TABLETOOLAGENDA        = Database::get_course_table(TABLE_AGENDA);
    $TABLETOOLANNOUNCEMENTS = Database::get_course_table(TABLE_ANNOUNCEMENT);
    $TABLETOOLDOCUMENT      = Database::get_course_table(TABLE_DOCUMENT);
    $TABLETOOLLINK          = Database::get_course_table(TABLE_LINK);
    $TABLEQUIZ              = Database::get_course_table(TABLE_QUIZ_TEST);
    $TABLEQUIZQUESTION      = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
    $TABLEQUIZQUESTIONLIST  = Database::get_course_table(TABLE_QUIZ_QUESTION);
    $TABLEQUIZANSWERSLIST   = Database::get_course_table(TABLE_QUIZ_ANSWER);
    $TABLESETTING           = Database::get_course_table(TABLE_COURSE_SETTING);
    $TABLEFORUMCATEGORIES   = Database::get_course_table(TABLE_FORUM_CATEGORY);
    $TABLEFORUMS            = Database::get_course_table(TABLE_FORUM);
    $TABLEFORUMTHREADS      = Database::get_course_table(TABLE_FORUM_THREAD);
    $TABLEFORUMPOSTS        = Database::get_course_table(TABLE_FORUM_POST);
    $TABLEGRADEBOOK         = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
    $TABLEGRADEBOOKLINK     = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
    $TABLEGRADEBOOKCERT     = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
    include api_get_path(SYS_CODE_PATH).'lang/english/create_course.inc.php';
    $file_to_include = api_get_path(SYS_CODE_PATH).'lang/'.$language.'/create_course.inc.php';
    if (file_exists($file_to_include)) {
        include $file_to_include;
    }
    $visible_for_all = 1;
    $visible_for_course_admin = 0;
    $visible_for_platform_admin = 2;
    /*    Course tools  */
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_COURSE_DESCRIPTION . "','course_description/','info.gif','".string2binary(api_get_setting('course_create_active_tools', 'course_description')) . "','0','squaregrey.gif','NO','_self','authoring','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_CALENDAR_EVENT . "','calendar/agenda.php','agenda.gif','".string2binary(api_get_setting('course_create_active_tools', 'agenda')) . "','0','squaregrey.gif','NO','_self','interaction','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_DOCUMENT . "','document/document.php','folder_document.gif','".string2binary(api_get_setting('course_create_active_tools', 'documents')) . "','0','squaregrey.gif','NO','_self','authoring','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_LEARNPATH . "','newscorm/lp_controller.php','scorms.gif','".string2binary(api_get_setting('course_create_active_tools', 'learning_path')) . "','0','squaregrey.gif','NO','_self','authoring','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_LINK . "','link/link.php','links.gif','".string2binary(api_get_setting('course_create_active_tools', 'links')) . "','0','squaregrey.gif','NO','_self','authoring','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_QUIZ . "','exercice/exercice.php','quiz.gif','".string2binary(api_get_setting('course_create_active_tools', 'quiz')) . "','0','squaregrey.gif','NO','_self','authoring','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_ANNOUNCEMENT . "','announcements/announcements.php','valves.gif','".string2binary(api_get_setting('course_create_active_tools', 'announcements')) . "','0','squaregrey.gif','NO','_self','authoring','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_FORUM . "','forum/index.php','forum.gif','".string2binary(api_get_setting('course_create_active_tools', 'forums')) . "','0','squaregrey.gif','NO','_self','interaction','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_DROPBOX . "','dropbox/index.php','dropbox.gif','".string2binary(api_get_setting('course_create_active_tools', 'dropbox')) . "','0','squaregrey.gif','NO','_self','interaction','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_USER . "','user/user.php','members.gif','".string2binary(api_get_setting('course_create_active_tools', 'users')) . "','0','squaregrey.gif','NO','_self','interaction','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_GROUP . "','group/group.php','group.gif','".string2binary(api_get_setting('course_create_active_tools', 'groups')) . "','0','squaregrey.gif','NO','_self','interaction','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_CHAT . "','chat/chat.php','chat.gif','".string2binary(api_get_setting('course_create_active_tools', 'chat')) . "','0','squaregrey.gif','NO','_self','interaction','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_STUDENTPUBLICATION . "','work/work.php','works.gif','".string2binary(api_get_setting('course_create_active_tools', 'student_publications')) . "','0','squaregrey.gif','NO','_self','interaction','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_SURVEY."','survey/survey_list.php','survey.gif','".string2binary(api_get_setting('course_create_active_tools', 'survey')) . "','0','squaregrey.gif','NO','_self','interaction','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_WIKI ."','wiki/index.php','wiki.gif','".string2binary(api_get_setting('course_create_active_tools', 'wiki')) . "','0','squaregrey.gif','NO','_self','interaction','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_GRADEBOOK."','gradebook/index.php','gradebook.gif','".string2binary(api_get_setting('course_create_active_tools', 'gradebook')). "','0','squaregrey.gif','NO','_self','authoring','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_GLOSSARY."','glossary/index.php','glossary.gif','".string2binary(api_get_setting('course_create_active_tools', 'glossary')). "','0','squaregrey.gif','NO','_self','authoring','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_NOTEBOOK."','notebook/index.php','notebook.gif','".string2binary(api_get_setting('course_create_active_tools', 'notebook'))."','0','squaregrey.gif','NO','_self','interaction','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_ATTENDANCE."','attendance/index.php','attendance.gif','".string2binary(api_get_setting('course_create_active_tools', 'attendances'))."','0','squaregrey.gif','NO','_self','authoring','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_COURSE_PROGRESS."','course_progress/index.php','course_progress.gif','".string2binary(api_get_setting('course_create_active_tools', 'course_progress'))."','0','squaregrey.gif','NO','_self','authoring','0')");
    if (api_get_setting('service_visio', 'active') == 'true') {
        $mycheck = api_get_setting('service_visio', 'visio_host');
        if (!empty($mycheck)) {
            Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_VISIO_CONFERENCE . "','conference/index.php?type=conference','visio_meeting.gif','1','0','squaregrey.gif','NO','_self','interaction','0')");
            Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_VISIO_CLASSROOM . "','conference/index.php?type=classroom','visio.gif','1','0','squaregrey.gif','NO','_self','authoring','0')");
        }
    }
    if (api_get_setting('search_enabled') == 'true') {
        Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '" . TOOL_SEARCH. "','search/','info.gif','".string2binary(api_get_setting('course_create_active_tools', 'enable_search')) . "','0','search.gif','NO','_self','authoring','0')");
    }
    // Smartblogs (Kevin Van Den Haute :: kevin@develop-it.be)
    $sql = "INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL,'" . TOOL_BLOGS . "','blog/blog_admin.php','blog_admin.gif','" . string2binary(api_get_setting('course_create_active_tools', 'blogs')) . "','1','squaregrey.gif','NO','_self','admin','0')";
    Database::query($sql);
    // end of Smartblogs
    /*  Course homepage tools for course admin only    */
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '".TOOL_TRACKING . "','tracking/courseLog.php','statistics.gif','$visible_for_course_admin','1','', 'NO','_self','admin','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '".TOOL_COURSE_SETTING . "','course_info/infocours.php','reference.gif','$visible_for_course_admin','1','', 'NO','_self','admin','0')");
    Database::query("INSERT INTO $tbl_course_homepage VALUES ($course_id, NULL, '".TOOL_COURSE_MAINTENANCE."','course_info/maintenance.php','backup.gif','$visible_for_course_admin','1','','NO','_self', 'admin','0')");
    $defaultEmailExerciseAlert = 1;
    if (isset($_configuration['email_alert_manager_on_new_quiz'])) {
        $defaultEmailExerciseAlert = intval($_configuration['email_alert_manager_on_new_quiz']);
    }
    /* course_setting table (courseinfo tool)   */
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'email_alert_manager_on_new_doc',0,'work')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'email_alert_on_new_doc_dropbox',0,'dropbox')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'allow_user_edit_agenda',0,'agenda')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'allow_user_edit_announcement',0,'announcement')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'email_alert_manager_on_new_quiz', $defaultEmailExerciseAlert,'quiz')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'allow_user_image_forum',1,'forum')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'course_theme','','theme')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'allow_learning_path_theme','1','theme')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'allow_open_chat_window',1,'chat')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'email_alert_to_teacher_on_new_user_in_course',0,'registration')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'allow_user_view_user_list',1,'user')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'display_info_advance_inside_homecourse',1,'thematic_advance')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'email_alert_students_on_new_homework',0,'work')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'enable_lp_auto_launch',0,'learning_path')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'pdf_export_watermark_text','','learning_path')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable,value,category) VALUES ($course_id, 'allow_public_certificates','','certificates')");
    Database::query("INSERT INTO $TABLESETTING (c_id, variable, value) VALUES ($course_id, 'documents_default_visibility', 'visible')");
    /* Course homepage tools for platform admin only */
    /* Group tool */
    Database::query("INSERT INTO $TABLEGROUPCATEGORIES  (c_id,  id , title , description , max_student , self_reg_allowed , self_unreg_allowed , groups_per_user , display_order )
            VALUES ($course_id, '2', '".lang2db(get_lang('DefaultGroupCategory')) . "', '', '8', '0', '0', '0', '0');");
    /*    Example Material  */
    global $language_interface;
    $language_interface = !empty($language_interface) ? $language_interface : api_get_setting('platformLanguage');
    // Example material should be in the same language as the course is.
    $language_interface_original = $language_interface;
    $language_interface          = $language;
    //Share folder
    Database::query("INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size) VALUES ($course_id,'/shared_folder','".get_lang('UserFolders')."','folder','0')");
    $example_doc_id = Database :: insert_id();
    Database::query("INSERT INTO $TABLEITEMPROPERTY (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ($course_id,'document',1,NOW(),NOW(),$example_doc_id,'DocumentAdded',1,0,NULL,0)");
    //Chat folder
    Database::query("INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size) VALUES ($course_id,'/chat_files','".get_lang('ChatFiles')."','folder','0')");
    $example_doc_id = Database :: insert_id();
    Database::query("INSERT INTO $TABLEITEMPROPERTY (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ($course_id,'document',1,NOW(),NOW(),$example_doc_id,'DocumentAdded',1,0,NULL,0)");
    $sys_course_path = api_get_path(SYS_COURSE_PATH);
    $perm = api_get_permissions_for_new_directories();
    $perm_file = api_get_permissions_for_new_files();
    $chat_path = $sys_course_path.$course_repository.'/document/chat_files';
    if (!is_dir($chat_path)) {
        @mkdir($chat_path, api_get_permissions_for_new_directories());
    }
    /*    Documents   */
    if ($fill_with_exemplary_content) {
        Database::query("INSERT INTO $TABLETOOLDOCUMENT (c_id,path,title,filetype,size) VALUES ($course_id,'/images','".get_lang('Images')."','folder','0')");
        $example_doc_id = Database :: insert_id();
        Database::query("INSERT INTO $TABLEITEMPROPERTY  (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ($course_id,'document',1,NOW(),NOW(),$example_doc_id,'DocumentAdded',1,0,NULL,0)");
        Database::query("INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size) VALUES ($course_id,'/images/gallery','".get_lang('DefaultCourseImages')."','folder','0')");
        $example_doc_id = Database :: insert_id();
        Database::query("INSERT INTO $TABLEITEMPROPERTY  (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ($course_id,'document',1,NOW(),NOW(),$example_doc_id,'DocumentAdded',1,0,NULL,0)");
        Database::query("INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size) VALUES ($course_id,'/audio','".get_lang('Audio')."','folder','0')");
        $example_doc_id = Database :: insert_id();
        Database::query("INSERT INTO $TABLEITEMPROPERTY  (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ($course_id,'document',1,NOW(),NOW(),$example_doc_id,'DocumentAdded',1,0,NULL,0)");
        Database::query("INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size) VALUES ($course_id,'/flash','".get_lang('Flash')."','folder','0')");
        $example_doc_id = Database :: insert_id();
        Database::query("INSERT INTO $TABLEITEMPROPERTY  (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ($course_id,'document',1,NOW(),NOW(),$example_doc_id,'DocumentAdded',1,0,NULL,0)");
        Database::query("INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size) VALUES ($course_id,'/video','".get_lang('Video')."','folder','0')");
        $example_doc_id = Database :: insert_id();
        Database::query("INSERT INTO $TABLEITEMPROPERTY  (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ($course_id,'document',1,NOW(),NOW(),$example_doc_id,'DocumentAdded',1,0,NULL,0)");
        Database::query("INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size) VALUES ($course_id,'/certificates','".get_lang('Certificates')."','folder','0')");
        $example_doc_id = Database :: insert_id();
        Database::query("INSERT INTO $TABLEITEMPROPERTY  (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ($course_id,'document',1,NOW(),NOW(),$example_doc_id,'DocumentAdded',1,0,NULL,0)");
        // FILL THE COURSE DOCUMENT WITH DEFAULT COURSE PICTURES
        $folders_to_copy_from_default_course =  array(
            'images',
            'audio',
            'flash',
            'video',
            'certificates',
        );
        $default_course_path = api_get_path(SYS_CODE_PATH).'default_course_document/';
        $default_document_array = array();
        foreach ($folders_to_copy_from_default_course as $folder) {
            $default_course_folder_path = $default_course_path.$folder.'/';
            $files = browse_folders($default_course_folder_path, array(), $folder);
            $sorted_array = sort_pictures($files, 'dir');
            $sorted_array = array_merge($sorted_array, sort_pictures($files, 'file'));
            $default_document_array[$folder] = $sorted_array;
        }
        //echo ''; print_r($default_document_array);exit;
        //Light protection (adding index.html in every document folder)
        $htmlpage = "\n\n \n \n Not authorized\n  \n  \n  \n";
        $example_cert_id = 0;
        if (is_array($default_document_array) && count($default_document_array) > 0) {
            foreach ($default_document_array as $media_type => $array_media) {
                $path_documents = "/$media_type/";
                //hack until feature #5242 is implemented
                if ($media_type == 'images') {
                    $media_type = 'images/gallery';
                    $images_folder = $sys_course_path.$course_repository."/document/images/";
                    if (!is_dir($images_folder)) {
                        //Creating index.html
                        mkdir($images_folder, $perm);
                        $fd = fopen($images_folder.'index.html', 'w');
                        fwrite($fd, $htmlpage);
                        @chmod($images_folder.'index.html', $perm_file);
                    }
                }
                $course_documents_folder = $sys_course_path.$course_repository."/document/$media_type/";
                $default_course_path = api_get_path(SYS_CODE_PATH).'default_course_document'.$path_documents;
                //echo 'try '.$course_documents_folder; echo '
';
                if (!is_dir($course_documents_folder)) {
                    //Creating index.html
                    mkdir($course_documents_folder, $perm);
                    $fd = fopen($course_documents_folder.'index.html', 'w');
                    fwrite($fd, $htmlpage);
                    @chmod($course_documents_folder.'index.html', $perm_file);
                }
                if (is_array($array_media) && count($array_media)>0) {
                    foreach ($array_media as $key => $value) {
                        if (isset($value['dir']) && !empty($value['dir'])) {
                            if (!is_dir($course_documents_folder.$value['dir'])) {
                                //Creating folder
                                mkdir($course_documents_folder.$value['dir'], $perm);
                                //Creating index.html (for light protection)
                                $index_html = $course_documents_folder.$value['dir'].'/index.html';
                                $fd = fopen($index_html, 'w');
                                fwrite($fd, $htmlpage);
                                @chmod($index_html, $perm_file);
                                //Inserting folder in the DB
                                $folder_path = substr($value['dir'], 0, strlen($value['dir']) - 1);
                                $temp = explode('/', $folder_path);
                                $title = $temp[count($temp)-1];
                                //hack until feature #5242 is implemented
                                if ($title == 'gallery') {
                                    $title = get_lang('DefaultCourseImages');
                                }
                                if ($media_type == 'images/gallery') {
                                    $folder_path = 'gallery/'.$folder_path;
                                }
                                Database::query("INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size) VALUES ($course_id,'$path_documents".$folder_path."','".$title."','folder','0')");
                                $image_id = Database :: insert_id();
                                Database::query("INSERT INTO $TABLEITEMPROPERTY (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ($course_id,'document',1,NOW(),NOW(),$image_id,'DocumentAdded',1,0,NULL,0)");
                            }
                        }
                        if (isset($value['file']) && !empty($value['file'])) {
                            if (!file_exists($course_documents_folder.$value['file'])) {
                                //Copying file
                                copy($default_course_path.$value['file'], $course_documents_folder.$value['file']);
                                chmod($course_documents_folder.$value['file'], $perm_file);
                                //echo $default_course_path.$value['file']; echo ' - '; echo $course_documents_folder.$value['file']; echo '
';
                                $temp = explode('/', $value['file']);
                                $file_size = filesize($course_documents_folder.$value['file']);
                                //hack until feature #5242 is implemented
                                if ($media_type == 'images/gallery') {
                                    $value["file"] = 'gallery/'.$value["file"];
                                }
                                //Inserting file in the DB
                                Database::query("INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size) VALUES ($course_id,'$path_documents".$value["file"]."','".$temp[count($temp)-1]."','file','$file_size')");
                                $image_id = Database :: insert_id();
                                if ($path_documents.$value['file'] == '/certificates/default.html') {
                                  $example_cert_id = $image_id;
                                }
                                Database::query("INSERT INTO $TABLEITEMPROPERTY (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ($course_id,'document',1,NOW(),NOW(),$image_id,'DocumentAdded',1,0,NULL,1)");
                            }
                        }
                    }
                }
            }
        }
        /* Agenda tool */
        Database::query("INSERT INTO $TABLETOOLAGENDA  VALUES ($course_id, NULL, '".lang2db(get_lang('AgendaCreationTitle')) . "', '".lang2db(get_lang('AgendaCreationContenu')) . "', now(), now(), NULL, 0, 0)");
        // We need to add the item properties too!
        $insert_id = Database :: insert_id();
        $sql = "INSERT INTO $TABLEITEMPROPERTY (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ($course_id, '" . TOOL_CALENDAR_EVENT . "',1,NOW(),NOW(),$insert_id,'AgendaAdded',1,0,NULL,1)";
        Database::query($sql);
        /*  Links tool */
        $add_google_link_sql = "INSERT INTO $TABLETOOLLINK  (c_id, url, title, description, category_id, display_order, on_homepage, target)
                VALUES ($course_id, 'http://www.google.com','Google','".lang2db(get_lang('Google')) . "','0','0','0','_self')";
        Database::query($add_google_link_sql);
        // We need to add the item properties too!
        $insert_id = Database :: insert_id();
        $sql = "INSERT INTO $TABLEITEMPROPERTY  (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility)
                VALUES ($course_id, '" . TOOL_LINK . "',1,NOW(),NOW(),$insert_id,'LinkAdded',1,0,NULL,1)";
        Database::query($sql);
        $add_wikipedia_link_sql = "INSERT INTO $TABLETOOLLINK  (c_id, url, title, description, category_id, display_order, on_homepage, target)
                VALUES ($course_id, 'http://www.wikipedia.org','Wikipedia','".lang2db(get_lang('Wikipedia')) . "','0','1','0','_self')";
        Database::query($add_wikipedia_link_sql);
        // We need to add the item properties too!
        $insert_id = Database :: insert_id();
        $sql = "INSERT INTO $TABLEITEMPROPERTY  (tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ('" . TOOL_LINK . "',1,NOW(),NOW(),$insert_id,'LinkAdded',1,0,NULL,1)";
        Database::query($sql);
        /* Annoucement tool */
        $sql = "INSERT INTO $TABLETOOLANNOUNCEMENTS  (c_id, title,content,end_date,display_order,email_sent)
                VALUES ($course_id, '".lang2db(get_lang('AnnouncementExampleTitle')) . "', '".lang2db(get_lang('AnnouncementEx')) . "', NOW(), '1','0')";
        Database::query($sql);
        // We need to add the item properties too!
        $insert_id = Database :: insert_id();
        $sql = "INSERT INTO $TABLEITEMPROPERTY  (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility)
                VALUES ($course_id, '" . TOOL_ANNOUNCEMENT . "',1,NOW(),NOW(),$insert_id,'AnnouncementAdded',1,0,NULL,1)";
        Database::query($sql);
        /* Introduction text */
        $intro_text='
                        
                        
'.lang2db(get_lang('IntroductionText')).'
                     ';
        Database::query("INSERT INTO $TABLEINTROS  VALUES ($course_id, '" . TOOL_COURSE_HOMEPAGE . "','".$intro_text."', 0)");
        Database::query("INSERT INTO $TABLEINTROS  VALUES ($course_id, '" . TOOL_STUDENTPUBLICATION . "','".lang2db(get_lang('IntroductionTwo')) . "', 0)");
        // Wiki intro
        $intro_wiki=' | '.lang2db(get_lang('IntroductionWiki')).' | 
';
        Database::query("INSERT INTO $TABLEINTROS  VALUES ($course_id, '" . TOOL_WIKI . "','".$intro_wiki. "', 0)");
        /*  Exercise tool */
        Database::query("INSERT INTO $TABLEQUIZANSWERSLIST (c_id, id, question_id, answer, correct, comment, ponderation, position) VALUES ($course_id, '1', '1', '".lang2db(get_lang('Ridiculise')) . "', '0', '".lang2db(get_lang('NoPsychology')) . "', '-5', '1')");
        Database::query("INSERT INTO $TABLEQUIZANSWERSLIST (c_id, id, question_id, answer, correct, comment, ponderation, position) VALUES ($course_id,  '2', '1', '".lang2db(get_lang('AdmitError')) . "', '0', '".lang2db(get_lang('NoSeduction')) . "', '-5', '2')");
        Database::query("INSERT INTO $TABLEQUIZANSWERSLIST (c_id, id, question_id, answer, correct, comment, ponderation, position) VALUES ($course_id,  '3', '1', '".lang2db(get_lang('Force')) . "', '1', '".lang2db(get_lang('Indeed')) . "', '5', '3')");
        Database::query("INSERT INTO $TABLEQUIZANSWERSLIST (c_id, id, question_id, answer, correct, comment, ponderation, position) VALUES ($course_id,  '4', '1', '".lang2db(get_lang('Contradiction')) . "', '1', '".lang2db(get_lang('NotFalse')) . "', '5', '4')");
        $html=Database::escape_string('.'default_course_document/images/mr_dokeos/thinking.jpg)  | '.get_lang('Antique').' | 
');
        Database::query('INSERT INTO '.$TABLEQUIZ .
          ' (c_id, title, description, type, random, random_answers, active, results_disabled ) ' .
          ' VALUES ('.$course_id.', "'.lang2db(get_lang('ExerciceEx')) . '",' .
          ' "'.$html.'", "1", "0", "0", "1", "0")');
        $exercise_id = Database :: insert_id();
        Database::query("INSERT INTO $TABLEQUIZQUESTIONLIST  (c_id, id, question, description, ponderation, position, type, picture, level)
                        VALUES ( '.$course_id.', '1', '".lang2db(get_lang('SocraticIrony')) . "', '".lang2db(get_lang('ManyAnswers')) . "', '10', '1', '2','',1)");
        Database::query("INSERT INTO $TABLEQUIZQUESTION  (c_id, question_id, exercice_id, question_order) VALUES ('.$course_id.', 1,1,1)");
        /* Forum tool */
        Database::query("INSERT INTO $TABLEFORUMCATEGORIES VALUES ($course_id, 1,'".lang2db(get_lang('ExampleForumCategory'))."', '', 1, 0, 0)");
        $insert_id = Database :: insert_id();
        Database::query("INSERT INTO $TABLEITEMPROPERTY  (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility)
                        VALUES ($course_id, 'forum_category',1,NOW(),NOW(),$insert_id,'ForumCategoryAdded',1,0,NULL,1)");
        Database::query("INSERT INTO $TABLEFORUMS (c_id, forum_title, forum_comment, forum_threads,forum_posts,forum_last_post,forum_category, allow_anonymous, allow_edit,allow_attachments, allow_new_threads,default_view,forum_of_group,forum_group_public_private, forum_order,locked,session_id )
                        VALUES ($course_id, '".lang2db(get_lang('ExampleForum'))."', '', 0, 0, 0, 1, 0, 1, '0', 1, 'flat','0', 'public', 1, 0,0)");
        $insert_id = Database :: insert_id();
        Database::query("INSERT INTO $TABLEITEMPROPERTY  (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility)
                         VALUES ($course_id, '".TOOL_FORUM."', 1,NOW(),NOW(),$insert_id,'ForumAdded',1,0,NULL,1)");
        Database::query("INSERT INTO $TABLEFORUMTHREADS (c_id, thread_id, thread_title, forum_id, thread_replies, thread_poster_id, thread_poster_name, thread_views, thread_last_post, thread_date, locked, thread_qualify_max, session_id)
                        VALUES ($course_id, 1, '".lang2db(get_lang('ExampleThread'))."', 1, 0, 1, '', 0, 1, NOW(), 0, 10, 0)");
        $insert_id = Database :: insert_id();
        Database::query("INSERT INTO $TABLEITEMPROPERTY  (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility)
                        VALUES ($course_id, 'forum_thread',1,NOW(),NOW(),$insert_id,'ForumThreadAdded',1,0,NULL,1)");
        Database::query("INSERT INTO $TABLEFORUMPOSTS VALUES ($course_id, 1, '".lang2db(get_lang('ExampleThread'))."', '".lang2db(get_lang('ExampleThreadContent'))."', 1, 1, 1, '', NOW(), 0, 0, 1)");
        /* Gradebook tool */
        $course = api_get_course_info_by_id($course_id);
        $course_code = $course['code'];
        // father gradebook
        Database::query("INSERT INTO $TABLEGRADEBOOK (name, description, user_id, course_code, parent_id, weight, visible, certif_min_score, session_id, document_id) VALUES ('$course_code','',1,'$course_code',0,100,0,75,NULL,$example_cert_id)");
        $gbid = Database :: insert_id();
        Database::query("INSERT INTO $TABLEGRADEBOOK (name, description, user_id, course_code, parent_id, weight, visible, certif_min_score, session_id, document_id) VALUES ('$course_code','',1,'$course_code',$gbid,100,1,75,NULL,$example_cert_id)");
        $gbid = Database :: insert_id();
        Database::query("INSERT INTO $TABLEGRADEBOOKLINK (type, ref_id, user_id, course_code, category_id, created_at, weight, visible, locked) VALUES (1,$exercise_id,1,'$course_code',$gbid,'$now',100,1,0)");
    }
    //Installing plugins in course
    $app_plugin = new AppPlugin();
    $app_plugin->install_course_plugins($course_id);
    $language_interface = $language_interface_original;
    return true;
}
/**
 * function string2binary converts the string "true" or "false" to the boolean true false (0 or 1)
 * This is used for the Chamilo Config Settings as these store true or false as string
 * and the api_get_setting('course_create_active_tools') should be 0 or 1 (used for
 * the visibility of the tool)
 * @param string    $variable
 * @author Patrick Cool, patrick.cool@ugent.be
 * @assert ('true') === true
 * @assert ('false') === false
 */
function string2binary($variable) {
    if ($variable == 'true') {
        return true;
    }
    if ($variable == 'false') {
        return false;
    }
}
/**
 * Function register_course to create a record in the course table of the main database
 * @param array Course details (see code for details)
 * @return int  Created course ID
 * @todo use an array called $params instead of lots of params
 * @assert (null) === false
 */
function register_course($params)
{
    global $error_msg, $firstExpirationDelay;
    $title              = $params['title'];
    $code               = $params['code'];
    $visual_code        = $params['visual_code'];
    $directory          = $params['directory'];
    $tutor_name         = isset($params['tutor_name']) ? $params['tutor_name'] : null;
    //$description        = $params['description'];
    $category_code      = $params['course_category'];
    $course_language = isset($params['course_language']) && !empty($params['course_language']) ? $params['course_language'] : api_get_setting('platformLanguage');
    $user_id = empty($params['user_id']) ? api_get_user_id() : intval($params['user_id']);
    $department_name = isset($params['department_name']) ?
        $params['department_name'] : null;
    $department_url = isset($params['department_url']) ?
        $params['department_url'] : null;
    $disk_quota = isset($params['disk_quota']) ?
        $params['disk_quota'] : null;
    if (!isset($params['visibility'])) {
        $default_course_visibility = api_get_setting('courses_default_creation_visibility');
        if (isset($default_course_visibility)) {
            $visibility = $default_course_visibility;
        } else {
            $visibility = COURSE_VISIBILITY_OPEN_PLATFORM;
        }
    } else {
        $visibility         = $params['visibility'];
    }
    $subscribe          = isset($params['subscribe']) ? intval($params['subscribe']) : ($visibility == COURSE_VISIBILITY_OPEN_PLATFORM ? 1 : 0);
    $unsubscribe        = isset($params['unsubscribe']) ? intval($params['unsubscribe']) : 0;
    $expiration_date    = isset($params['expiration_date']) ? $params['expiration_date'] : null;
    $teachers           = isset($params['teachers']) ? $params['teachers'] : null;
    $status             = isset($params['status']) ? $params['status'] : null;
    $TABLECOURSE            = Database :: get_main_table(TABLE_MAIN_COURSE);
    $TABLECOURSUSER         = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
    $ok_to_register_course = true;
    // Check whether all the needed parameters are present.
    if (empty($code)) {
        $error_msg[] = 'courseSysCode is missing';
        $ok_to_register_course = false;
    }
    if (empty($visual_code)) {
        $error_msg[] = 'courseScreenCode is missing';
        $ok_to_register_course = false;
    }
    if (empty($directory)) {
        $error_msg[] = 'courseRepository is missing';
        $ok_to_register_course = false;
    }
    if (empty($title)) {
        $error_msg[] = 'title is missing';
        $ok_to_register_course = false;
    }
    if (empty($expiration_date)) {
        $expiration_date = api_get_utc_datetime(time() + $firstExpirationDelay);
    } else {
        $expiration_date = api_get_utc_datetime($expiration_date);
    }
    if ($visibility < 0 || $visibility > 4) {
        $error_msg[] = 'visibility is invalid';
        $ok_to_register_course = false;
    }
    if (empty($disk_quota)) {
        $disk_quota = api_get_setting('default_document_quotum');
    }
    $time = api_get_utc_datetime();
    if (stripos($department_url, 'http://') === false && stripos($department_url, 'https://') === false) {
        $department_url = 'http://'.$department_url;
    }
    //just in case
    if ($department_url == 'http://') {
        $department_url = '';
    }
    $course_id = 0;
    if ($ok_to_register_course) {
       // Here we must add 2 fields.
      $sql = "INSERT INTO ".$TABLECOURSE . " SET
                    code            = '".Database :: escape_string($code) . "',
                    directory       = '".Database :: escape_string($directory) . "',
                    course_language = '".Database :: escape_string($course_language) . "',
                    title           = '".Database :: escape_string($title) . "',
                    description     = '".lang2db(get_lang('CourseDescription')) . "',
                    category_code   = '".Database :: escape_string($category_code) . "',
                    visibility      = '".$visibility . "',
                    show_score      = '1',
                    disk_quota      = '".intval($disk_quota) . "',
                    creation_date   = '$time',
                    expiration_date = '".$expiration_date . "',
                    last_edit       = '$time',
                    last_visit      = NULL,
                    tutor_name      = '".Database :: escape_string($tutor_name) . "',
                    department_name = '".Database :: escape_string($department_name) . "',
                    department_url  = '".Database :: escape_string($department_url) . "',
                    subscribe       = '".intval($subscribe) . "',
                    unsubscribe     = '".intval($unsubscribe) . "',
                    visual_code     = '".Database :: escape_string($visual_code) . "'";
        Database::query($sql);
        $course_id  = Database::insert_id();
        if ($course_id) {
            $sort = api_max_sort_value('0', api_get_user_id());
            // Default true
            $addTeacher = isset($params['add_user_as_teacher']) ? $params['add_user_as_teacher'] : true;
            if ($addTeacher) {
                $i_course_sort = CourseManager:: userCourseSort(
                    $user_id,
                    $code
                );
                if (!empty($user_id)) {
                    $sql = "INSERT INTO " . $TABLECOURSUSER . " SET
                                course_code     = '" . Database:: escape_string($code) . "',
                                user_id         = '" . intval($user_id) . "',
                                status          = '1',
                                role            = '" . lang2db(get_lang('Professor')) . "',
                                tutor_id        = '0',
                                sort            = '" . ($i_course_sort) . "',
                                user_course_cat = '0'";
                    Database::query($sql);
                }
            }
            if (!empty($teachers)) {
                if (!is_array($teachers)) {
                    $teachers = array($teachers);
                }
                foreach ($teachers as $key) {
                    //just in case
                    if ($key == $user_id) {
                        continue;
                    }
                    if (empty($key)) {
                        continue;
                    }
                    $sql = "INSERT INTO ".$TABLECOURSUSER . " SET
                        course_code     = '".Database::escape_string($code) . "',
                        user_id         = '".Database::escape_string($key) . "',
                        status          = '1',
                        role            = '',
                        tutor_id        = '0',
                        sort            = '". ($sort +1) . "',
                        user_course_cat = '0'";
                    Database::query($sql);
                }
            }
            // Adding the course to an URL.
            global $_configuration;
            if ($_configuration['multiple_access_urls']) {
                $url_id = 1;
                if (api_get_current_access_url_id() != -1) {
                    $url_id = api_get_current_access_url_id();
                }
                UrlManager::add_course_to_url($code, $url_id);
            } else {
                UrlManager::add_course_to_url($code, 1);
            }
            // Add event to the system log.
            $user_id = api_get_user_id();
            event_system(LOG_COURSE_CREATE, LOG_COURSE_CODE, $code, api_get_utc_datetime(), $user_id, $code);
            $send_mail_to_admin = api_get_setting('send_email_to_admin_when_create_course');
            // @todo Improve code to send to all current portal administrators.
            if ($send_mail_to_admin == 'true') {
                $siteName = api_get_setting('siteName');
                $recipient_email = api_get_setting('emailAdministrator');
                $recipient_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'));
                $iname = api_get_setting('Institution');
                $subject = get_lang('NewCourseCreatedIn').' '.$siteName.' - '.$iname;
                $message =  get_lang('Dear').' '.$recipient_name.",\n\n".get_lang('MessageOfNewCourseToAdmin').' '.$siteName.' - '.$iname."\n";
                $message .= get_lang('CourseName').' '.$title."\n";
                $message .= get_lang('Category').' '.$category_code."\n";
                $message .= get_lang('Tutor').' '.$tutor_name."\n";
                $message .= get_lang('Language').' '.$course_language;
                $userInfo = api_get_user_info($user_id);
                $additionalParameters = array(
                    'smsType' => ClockworksmsPlugin::NEW_COURSE_BEEN_CREATED,
                    'userId' => $user_id,
                    'courseName' => $title,
                    'creatorUsername' => $userInfo['username']
                );
                api_mail_html(
                    $recipient_name,
                    $recipient_email,
                    $subject,
                    $message,
                    $siteName,
                    $recipient_email,
                    null,
                    null,
                    null,
                    $additionalParameters
                );
            }
        }
    }
    return $course_id;
}
/**
 * Extract properties of the files from a ZIP package, write them to disk and
 * return them as an array.
 * @todo this function seems not to be used
 * @param string        Absolute path to the ZIP file
 * @param bool          Whether the ZIP file is compressed (not implemented). Defaults to TRUE.
 * @return array        List of files properties from the ZIP package
 * @assert (null) === false
 */
function readPropertiesInArchive($archive, $is_compressed = true) {
    include api_get_path(LIBRARY_PATH) . 'pclzip/pclzip.lib.php';
    debug::printVar(dirname($archive), 'Zip : ');
    $uid = api_get_user_id();
    /*
    string tempnam (string dir, string prefix)
    tempnam() creates a unique temporary file in the dir directory. If the
    directory doesn't existm tempnam() will generate a filename in the system's
    temporary directory.
    Before PHP 4.0.6, the behaviour of tempnam() depended of the underlying OS.
    Under Windows, the "TMP" environment variable replaces the dir parameter;
    under Linux, the "TMPDIR" environment variable has priority, while for the
    OSes based on system V R4, the dir parameter will always be used if the
    directory which it represents exists. Consult your documentation for more
    details.
    tempnam() returns the temporary filename, or the string NULL upon failure.
    */
    $zip_file = new PclZip($archive);
    $tmp_dir_name = dirname($archive) . '/tmp'.$uid.uniqid($uid);
    if (mkdir($tmp_dir_name, api_get_permissions_for_new_directories(), true)) {
        $unzipping_state = $zip_file->extract($tmp_dir_name);
    } else {
        die ('mkdir failed');
    }
    $path_to_archive_ini = dirname($tmp_dir_name) . '/archive.ini';
    //echo $path_to_archive_ini;
    $course_properties = parse_ini_file($path_to_archive_ini);
    rmdir($tmp_dir_name);
    return $course_properties;
}