Ejemplo n.º 1
0
 /**
 	* Convert BB export course file to JLMS export course file and upload it to database
 	* @param String Name of form field with bb filename
 	* @return Array  ['lmsfile'] - Name of JLMS export course file (file placed in \media) (null if error)
 					 ['msg']	 - Message
 */
 function processBBfile($course_id = 0, $def_course_name = 'BB course', $do_lms_archive = true, $upload_to_db = true)
 {
     global $JLMS_DB, $JLMS_CONFIG, $my;
     if (!$this->is_prepared) {
         return array('lmsfile' => null, 'msg' => "Error! Invalid parameters passed");
     }
     if (!$do_lms_archive && !$upload_to_db || !$this->bb_file_media_name && !$this->bb_file_tmp_name) {
         return array('lmsfile' => null, 'msg' => "Error! Invalid parameters passed");
     }
     require_once $JLMS_CONFIG->get('absolute_path') . "/components/com_joomla_lms/includes/jlms_dir_operation.php";
     require_once $JLMS_CONFIG->get('absolute_path') . "/components/com_joomla_lms/includes/jlms_zip_operation.php";
     // for function "JLMS_parse_XML_elements()"
     require_once $JLMS_CONFIG->get('absolute_path') . "/components/com_joomla_lms/includes/jlms_course_import.php";
     @set_time_limit('3000');
     // Check that the zlib is available
     if (!extension_loaded('zlib')) {
         return array('lmsfile' => null, 'msg' => "Error! zlib library unavailable");
     }
     if (!$this->bb_file_media_name && !$this->bb_file_tmp_name) {
         return array('lmsfile' => null, 'msg' => _JLMS_EM_SELECT_FILE);
     }
     $config =& JFactory::getConfig();
     $extract_dir = $config->get('tmp_path') . DS . "bb_course_export_" . uniqid(rand()) . DS;
     if ($this->bb_file_tmp_name) {
         $archive = $this->bb_file_tmp_name;
     } else {
         $archive = $JLMS_CONFIG->get('absolute_path') . "/media/" . $this->bb_file_media_name;
     }
     //exstract archive in uniqfolder media
     extractBackupArchive($archive, $extract_dir);
     $xmlFile = $extract_dir . "imsmanifest.xml";
     if ($fp1 = fopen($xmlFile, "r")) {
         $fline = fgets($fp1);
         $match = array();
         preg_match('/encoding="([0-9a-zA-Z-]*)"/i', $fline, $match);
         //find quoted encoding
         if (isset($match[1]) && $match[1] != "ISO-8859-1") {
             $this->charset_lang = $match[1];
         }
         fclose($fp1);
     }
     $xmlDoc =& JLMSFactory::getXMLParser();
     if (!$xmlDoc->loadFile($xmlFile)) {
         return array('lmsfile' => null, 'msg' => 'Error during reading xml file');
     }
     $root =& $xmlDoc->document;
     if ($root->name() != 'manifest') {
         return array('lmsfile' => null, 'msg' => 'Not a Course installation file');
     }
     // ****************************************************************************************************
     //get config values
     $query = "SELECT * FROM `#__lms_config`";
     $JLMS_DB->SetQuery($query);
     $lms_cfg = $JLMS_DB->LoadObjectList();
     $lms_cfg_doc_folder = '';
     $lms_cfg_scorm = '';
     $lms_cfg_backup_folder = '';
     $lms_cfg_quiz_enabled = 0;
     foreach ($lms_cfg as $lcf) {
         if ($lcf->lms_config_var == 'plugin_quiz') {
             $lms_cfg_quiz_enabled = $lcf->lms_config_value;
         } elseif ($lcf->lms_config_var == 'jlms_doc_folder') {
             $lms_cfg_doc_folder = $lcf->lms_config_value;
         } elseif ($lcf->lms_config_var == 'scorm_folder') {
             $lms_cfg_scorm = $lcf->lms_config_value;
         } elseif ($lcf->lms_config_var == 'jlms_backup_folder') {
             $lms_cfg_backup_folder = $lcf->lms_config_value;
         }
     }
     // ***************************************************************************************************
     $course = new stdClass();
     $course->course_name = $def_course_name;
     $course->course_description = '';
     $course->metadesc = '';
     $course->metakeys = '';
     $course->self_reg = '';
     $course->paid = 0;
     $course->add_forum = 0;
     $course->add_chat = 0;
     $course->owner_id = $my->id;
     $course->published = 0;
     $course->publish_start = 0;
     $course->publish_end = 0;
     $course->start_date = '0000-00-00';
     $course->end_date = '0000-00-00';
     $course->spec_reg = 0;
     $course->cat_id = 0;
     // 02.03.2007 1.0.1 support
     $course->params = '';
     $course->gid = 0;
     $course->language = 0;
     //get resources
     $element =& $root->getElementByPath('resources');
     $resources = JLMS_parse_XML_elements($element->children(), array('identifier', 'type'), array());
     foreach ($resources as $resource) {
         if ($resource->type == 'course/x-bb-coursesetting') {
             $resourceFile = $extract_dir . $resource->identifier . ".dat";
             $resourceXmlDoc =& JLMSFactory::getXMLParser();
             ////$resourceXmlDoc->resolveErrors( true );
             if ($resourceXmlDoc->loadFile($resourceFile)) {
                 $resourceRoot =& $resourceXmlDoc->document;
                 $resourceElement =& $resourceRoot->getElementByPath('title');
                 $course->course_name = $this->attributes($resourceElement, 'value');
                 if ($def_course_name != 'BB course') {
                     $course->course_name = $def_course_name;
                 }
                 $resourceElement =& $resourceRoot->getElementByPath('description');
                 $course->course_description = $this->data($resourceElement);
             }
         }
     }
     //save course
     if ($upload_to_db) {
         if (!$course_id) {
             $JLMS_DB->insertObject("#__lms_courses", $course, "id");
             //get new Course_id
             $course_id = $JLMS_DB->insertid();
             // create teacher for imported course
             $default_teacher_role = 0;
             $query = "SELECT id FROM #__lms_usertypes WHERE roletype_id = 2 AND default_role = 1 LIMIT 0,1";
             $JLMS_DB->setQuery($query);
             $default_teacher_role = intval($JLMS_DB->LoadResult());
             if (!$default_teacher_role) {
                 $query = "SELECT id FROM #__lms_usertypes WHERE roletype_id = 2 LIMIT 0,1";
                 $JLMS_DB->setQuery($query);
                 $default_teacher_role = intval($JLMS_DB->LoadResult());
                 if (!$default_teacher_role) {
                     $default_teacher_role = 1;
                 }
             }
             $query = "INSERT INTO `#__lms_user_courses` (user_id, course_id, role_id) VALUES ('" . $my->id . "','" . $course_id . "','" . $default_teacher_role . "')";
             $JLMS_DB->setQuery($query);
             $JLMS_DB->query();
         }
     }
     // ******************************************************************************************************************
     // process resourses
     $i = 0;
     //get data from resource files
     while ($i < count($resources)) {
         //$resources[$i]->identifier .= ".dat";
         $resourceFile = $extract_dir . $resources[$i]->identifier . ".dat";
         //echo $resourceFile.'<br />';
         $resourceXmlDoc =& JLMSFactory::getXMLParser();
         //$resourceXmlDoc->resolveErrors( true );
         if ($resourceXmlDoc->loadFile($resourceFile)) {
             $resourceRoot =& $resourceXmlDoc->document;
             //file, folder, link or content
             $resources[$i]->resource_type = '';
             if ($resources[$i]->type == 'resource/x-bb-document') {
                 $resourceElement =& $resourceRoot->getElementByPath('contenthandler');
                 // resource/x-bb-document - content may be with file
                 // resource/x-bb-folder - foder
                 // resource/x-bb-externallink - link
                 $resources[$i]->resource_type = $this->attributes($resourceElement, 'value');
                 if ($resources[$i]->resource_type == 'resource/x-bb-document' || $resources[$i]->resource_type == 'resource/x-bb-folder' || $resources[$i]->resource_type == 'resource/x-bb-externallink') {
                     //save files
                     $resourceElement =& $resourceRoot->getElementByPath('files');
                     $files = JLMS_parse_XML_elements($resourceElement->children(), array('id'), array('name'));
                     if (count($files) > 0) {
                         $resources[$i]->files = $files;
                     }
                     //$resourceElement = &$resourceRoot->getElementByPath('CONTENT', 1);
                     $resources[$i]->id = $this->attributes($resourceRoot, 'id');
                     //save URLs
                     $resourceElement =& $resourceRoot->getElementByPath('url');
                     if ($this->attributes($resourceElement, 'value')) {
                         $resources[$i]->url = $this->attributes($resourceElement, 'value');
                     }
                     //save name and description
                     $resourceElement =& $resourceRoot->getElementByPath('title');
                     if ($this->attributes($resourceElement, 'value')) {
                         $resources[$i]->title = $this->attributes($resourceElement, 'value');
                     } else {
                         $resources[$i]->title = '';
                     }
                     $resourceElement =& $resourceRoot->getElementByPath('body');
                     $text =& $resourceElement->getElementByPath('text');
                     if ($text) {
                         $resources[$i]->text = $this->data($text);
                     } else {
                         $resources[$i]->text = '';
                     }
                     //save START and END date
                     $resourceElement =& $resourceRoot->getElementByPath('dates');
                     $tmp = $resourceElement->getElementByPath('start');
                     if ($this->attributes($tmp, 'value')) {
                         $resources[$i]->start = $this->attributes($tmp, 'value');
                     }
                     $tmp = $resourceElement->getElementByPath('end');
                     if ($this->attributes($tmp, 'value')) {
                         $resources[$i]->end = $this->attributes($tmp, 'value');
                     }
                 }
             } elseif ($resources[$i]->type == 'resource/x-bb-announcement') {
                 //save name and description
                 $resourceElement =& $resourceRoot->getElementByPath('title');
                 if ($this->attributes($resourceElement, 'value')) {
                     $resources[$i]->title = $this->attributes($resourceElement, 'value');
                 }
                 $resourceElement =& $resourceRoot->getElementByPath('description');
                 $text =& $resourceElement->getElementByPath('text');
                 if ($text) {
                     $resources[$i]->text = $this->data($text);
                 } else {
                     $resources[$i]->text = '';
                 }
                 //save START and END date
                 $resourceElement =& $resourceRoot->getElementByPath('dates');
                 $tmp = $resourceElement->getElementByPath('RESTRICTSTART');
                 if ($this->attributes($tmp, 'value')) {
                     $resources[$i]->restrictstart = $this->attributes($tmp, 'value');
                 }
                 $tmp = $resourceElement->getElementByPath('RESTRICTEND');
                 if ($this->attributes($tmp, 'value')) {
                     $resources[$i]->restrictend = $this->attributes($tmp, 'value');
                 }
             } elseif ($resources[$i]->type == 'resource/x-bb-task') {
                 //save name and description
                 $resourceElement =& $resourceRoot->getElementByPath('title');
                 if ($this->attributes($resourceElement, 'value')) {
                     $resources[$i]->title = $this->attributes($resourceElement, 'value');
                 }
                 $resourceElement =& $resourceRoot->getElementByPath('description');
                 $text =& $resourceElement->getElementByPath('text');
                 if ($text) {
                     $resources[$i]->text = $this->data($text);
                 } else {
                     $resources[$i]->text = '';
                 }
                 //save DUE date
                 $resourceElement =& $resourceRoot->getElementByPath('dates');
                 $tmp = $resourceElement->getElementByPath('due');
                 if ($this->attributes($tmp, 'value')) {
                     $resources[$i]->due = $this->attributes($tmp, 'value');
                 }
             } elseif ($resources[$i]->type == 'assessment/x-bb-qti-test' || $resources[$i]->type == 'assessment/x-bb-qti-pool' || $resources[$i]->type == 'assessment/x-bb-qti-survey') {
                 //save name and description
                 $resourceElement =& $resourceRoot->getElementByPath('assessment');
                 $resourceElement =& $resourceElement->getElementByPath('section');
                 $resourceElement =& $resourceElement->getElementByPath('sectionmetadata');
                 $resourceElement =& $resourceElement->getElementByPath('bbmd_assessmenttype');
                 $assess_type = $this->data($resourceElement);
                 $resources[$i]->assess_type = $assess_type;
                 if ($assess_type != 'Survey') {
                     $resourceElement =& $resourceRoot->getElementByPath('assessment');
                     if ($this->attributes($resourceElement, 'title')) {
                         $resources[$i]->title = $this->attributes($resourceElement, 'title');
                     }
                     $resourceElement =& $resourceElement->getElementByPath('presentation_material');
                     $resourceElement =& $resourceElement->getElementByPath('flow_mat');
                     $resourceElement =& $resourceElement->getElementByPath('material');
                     $resourceElement =& $resourceElement->getElementByPath('mat_extension');
                     $resourceElement =& $resourceElement->getElementByPath('mat_formattedtext');
                     $resources[$i]->text = $this->data($resourceElement);
                     //save max score
                     $resourceElement =& $resourceRoot->getElementByPath('assessment');
                     $resourceElement =& $resourceElement->getElementByPath('section');
                     $resourceElement =& $resourceElement->getElementByPath('sectionmetadata');
                     $tmp =& $resourceElement->getElementByPath('bbmd_asi_object_id');
                     $resources[$i]->q_id = $this->data($tmp);
                     $resourceElement =& $resourceElement->getElementByPath('qmd_absolutescore_max');
                     $resources[$i]->score_max = $this->data($resourceElement);
                     //save questions
                     $resourceElement =& $resourceRoot->getElementByPath('assessment');
                     $resourceElement =& $resourceElement->getElementByPath('section');
                     $resources[$i]->questions = $this->getQuestions($resourceElement->children(), $resources[$i]->q_id, $resources[$i]->identifier);
                     $resources[$i]->answers = $this->getAnswers($resourceElement->children(), $resources[$i]->q_id);
                 }
             } elseif ($resources[$i]->type == 'course/x-bb-gradebook') {
                 //get name and descriptoin
                 $resourceElement =& $resourceRoot->getElementByPath('outcomedefinitions');
                 $gb = array();
                 foreach ($resourceElement->children() as $item) {
                     $title = $item->getElementByPath('title');
                     $description = $item->getElementByPath('description');
                     if ($this->attributes($title, 'value') == 'OutcomeDefinition.Total.title' || $this->attributes($title, 'value') == 'OutcomeDefinition.WeightedTotal.title') {
                         continue;
                     }
                     $identifier = $item->getElementByPath('asidataid');
                     $identifier = $this->attributes($identifier, 'value');
                     if (!$identifier) {
                         $insert = new stdClass();
                         $insert->title = $this->attributes($title, 'value');
                         $insert->description = $this->data($description);
                         $gb[] = $insert;
                     }
                 }
                 $resources[$i]->gradebook = $gb;
             } elseif ($resources[$i]->type == 'course/x-bb-coursesetting') {
                 $resourceElement =& $resourceRoot->getElementByPath('title');
                 $course->course_name = $this->attributes($resourceElement, 'value');
                 $resourceElement =& $resourceRoot->getElementByPath('description');
                 $course->course_description = $this->data($resourceElement);
             }
             ##elseif ( $resources[$i]->type == 'course/x-bb-coursesetting' ) ....
         }
         ## if ($resourceXmlDoc->loadXML( $resourceFile, false, true )) ....
         $i++;
     }
     ## while( $i < count($resources) ) ....
     // *******************************************************************************************************************
     $i = 0;
     $announcements = array();
     $homeworks = array();
     $links = array();
     $files = array();
     $gradebook = array();
     $quizzes_no = array();
     $quizzes = array();
     $questions = array();
     $answers = array();
     //prepare data to save
     while ($i < count($resources)) {
         if (($resources[$i]->type == 'assessment/x-bb-qti-test' || $resources[$i]->type == 'assessment/x-bb-qti-pool' || $resources[$i]->type == 'assessment/x-bb-qti-survey') && $resources[$i]->assess_type != 'Survey') {
             $insert = new stdClass();
             $insert->_c_id = $resources[$i]->q_id;
             $insert->c_category_id = null;
             $insert->c_user_id = $my->id;
             $insert->c_full_score = abs($resources[$i]->score_max);
             // 17.12.2007 (DEN) - negative values bug - needed another solution - BB courses have negative points values !
             $insert->c_title = $resources[$i]->title;
             $insert->c_description = $resources[$i]->text;
             $insert->c_time_limit = 5;
             $insert->c_min_after = 5;
             $insert->c_passing_score = 1;
             $insert->c_created_time = date('Y-m-d');
             $insert->published = 0;
             $insert->c_right_message = 'correct';
             $insert->c_wrong_message = 'incorrect';
             $insert->c_pass_message = 'pass';
             $insert->c_unpass_message = 'fail';
             $insert->c_enable_review = null;
             $insert->c_email_to = 0;
             $insert->c_enable_print = null;
             $insert->c_enable_sertif = null;
             $insert->c_skin = 3;
             $insert->c_random = 0;
             $insert->c_guest = 0;
             $insert->c_slide = 1;
             $insert->c_language = 1;
             $insert->c_certificate = 0;
             $insert->c_gradebook = 1;
             $insert->course_id = $course_id;
             $quizzes[] = $insert;
             $quizzes_no[] = $i;
             $questions[] = $resources[$i]->questions;
             $answers[] = $resources[$i]->answers;
         }
         if (isset($resources[$i]->url)) {
             $insert = new stdClass();
             $insert->course_id = $course_id;
             $insert->owner_id = $my->id;
             $insert->link_name = $resources[$i]->title;
             $insert->link_href = $resources[$i]->url;
             $insert->link_description = $resources[$i]->text;
             $insert->link_type = 0;
             $insert->ordering = 0;
             $insert->published = 0;
             $insert->_id = $resources[$i]->id;
             $links[] = $insert;
         }
         if ($resources[$i]->type == 'resource/x-bb-announcement') {
             $insert = new stdClass();
             $insert->course_id = $course_id;
             $insert->owner_id = $my->id;
             $insert->title = $resources[$i]->title;
             $insert->content = $resources[$i]->text;
             $insert->start_date = $resources[$i]->restrictstart;
             $insert->end_date = isset($resources[$i]->restrictend) ? $resources[$i]->restrictend : $resources[$i]->restrictstart;
             $announcements[] = $insert;
         }
         if ($resources[$i]->type == 'resource/x-bb-task') {
             $insert = new stdClass();
             $insert->course_id = $course_id;
             $insert->hw_name = $resources[$i]->title;
             $insert->hw_description = $resources[$i]->text;
             $insert->hw_shortdescription = $resources[$i]->text;
             $insert->post_date = date("Y-m-d");
             $insert->end_date = $resources[$i]->due;
             $homeworks[] = $insert;
         }
         if ($resources[$i]->type == 'course/x-bb-gradebook') {
             foreach ($resources[$i]->gradebook as $item) {
                 $insert = new stdClass();
                 $insert->course_id = $course_id;
                 $insert->gbc_id = 0;
                 $insert->gbi_name = $item->title;
                 $insert->gbi_description = $item->description;
                 $insert->gbi_points = 0;
                 $insert->gbi_option = 0;
                 $insert->ordering = 0;
                 $gradebook[] = $insert;
             }
         }
         if (isset($resources[$i]->files)) {
             $file = $resources[$i]->files[0];
             if ($this->charset_lang != 'ISO-8859-1') {
                 $file->name = htmlentities($file->name, ENT_QUOTES, $this->charset_lang);
                 $file->name = str_replace('&lt;', '<', $file->name);
                 $file->name = str_replace('&gt;', '>', $file->name);
                 $file->name = str_replace('&amp;', '&', $file->name);
             }
             $fromDir = $extract_dir . $resources[$i]->identifier . "/";
             $toDir = $lms_cfg_doc_folder . "/";
             $insert_file = new stdClass();
             $insert_file->_fromDir = $extract_dir . $resources[$i]->identifier . "/";
             $insert_file->_toDir = $lms_cfg_doc_folder . "/";
             $insert_file->file_name = $file->name;
             $file_unique_name = str_pad($course_id, 4, '0', STR_PAD_LEFT) . '_' . md5(uniqid(rand(), true)) . '.' . substr($file->name, -3);
             $insert_file->file_srv_name = $file_unique_name;
             $insert_file->owner_id = $my->id;
             if (!file_exists($insert_file->_fromDir . $insert_file->file_name)) {
                 $res_dir = opendir($insert_file->_fromDir);
                 while (false !== ($res_file = readdir($res_dir))) {
                     if ($res_file != "." && $res_file != "..") {
                         rename($insert_file->_fromDir . $res_file, $insert_file->_fromDir . $insert_file->file_srv_name);
                         break;
                     }
                 }
                 closedir($res_dir);
             } else {
                 rename($insert_file->_fromDir . $insert_file->file_name, $insert_file->_fromDir . $insert_file->file_srv_name);
             }
             $insert_file->_id = $file->id;
             $file->file_srv_name = $insert_file->file_srv_name;
             $resources[$i]->files[0] = $file;
             $files[] = $insert_file;
         }
         $i++;
     }
     $element =& $root->getElementByPath('organizations');
     $element = $element->getElementByPath('organization');
     //$items = JLMS_parse_XML_elements($element->children(), array('identifierref'), array());
     $i = 0;
     $documents1 = array();
     while ($i < count($element->children())) {
         $nodes = $element->children();
         $node = $nodes[$i];
         $title = $node->getElementByPath('title');
         $title = substr($this->data($title), 15);
         $title = substr($title, 0, strpos($title, "."));
         foreach ($node->children() as $item) {
             if ($item->name() == 'item') {
                 $this->Process_Item($item, $documents1, $resources);
             }
         }
         $i++;
     }
     $documents1 = array_reverse($documents1);
     // insert data to database
     if ($upload_to_db) {
         if (in_array('links', $this->options)) {
             for ($i = 0, $n = count($links); $i < $n; $i++) {
                 $JLMS_DB->insertObject("#__lms_links", $links[$i], "id");
                 $links[$i]->_id = $JLMS_DB->insertid();
             }
         }
         if (in_array('announcements', $this->options)) {
             for ($i = 0, $n = count($announcements); $i < $n; $i++) {
                 $JLMS_DB->insertObject("#__lms_agenda", $announcements[$i], "id");
             }
         }
         if (in_array('homework', $this->options)) {
             for ($i = 0, $n = count($homeworks); $i < $n; $i++) {
                 $JLMS_DB->insertObject("#__lms_homework", $homeworks[$i], "id");
             }
         }
         if (in_array('gradebook_items', $this->options)) {
             for ($i = 0, $n = count($gradebook); $i < $n; $i++) {
                 $JLMS_DB->insertObject("#__lms_gradebook_items", $gradebook[$i], "id");
             }
         }
         if (in_array('documents', $this->options)) {
             for ($i = 0, $n = count($files); $i < $n; $i++) {
                 $JLMS_DB->insertObject("#__lms_files", $files[$i], "id");
                 $files[$i]->new_file_id = $JLMS_DB->insertid();
             }
         }
         if (in_array('quizzes', $this->options)) {
             $i = 0;
             while ($i < count($quizzes)) {
                 $JLMS_DB->insertObject("#__lms_quiz_t_quiz", $quizzes[$i], "c_id");
                 $new_quiz_id = $JLMS_DB->insertid();
                 $quizzes[$i]->new_quiz_id = $new_quiz_id;
                 $quizzes[$i]->_c_id = $new_quiz_id;
                 //questions processing
                 $j = 0;
                 while ($j < count($questions[$i])) {
                     $questions[$i][$j]->course_id = $course_id;
                     $questions[$i][$j]->c_quiz_id = $new_quiz_id;
                     $JLMS_DB->insertObject("#__lms_quiz_t_question", $questions[$i][$j], "c_id");
                     $questions[$i][$j]->new_id = $JLMS_DB->insertid();
                     $j++;
                 }
                 //choices processing
                 $j = 0;
                 while ($j < count($answers[$i]['choice_data'])) {
                     for ($k = 0; $k < count($questions[$i]); $k++) {
                         if ($questions[$i][$k]->_c_id == $answers[$i]['choice_data'][$j]->c_question_id) {
                             $answers[$i]['choice_data'][$j]->c_question_id = isset($questions[$i][$k]->new_id) ? $questions[$i][$k]->new_id : 0;
                             break;
                         }
                     }
                     if ($answers[$i]['choice_data'][$j]->c_question_id > 0) {
                         $JLMS_DB->insertObject("#__lms_quiz_t_choice", $answers[$i]['choice_data'][$j], "c_id");
                     }
                     $j++;
                 }
                 //matching processing
                 $j = 0;
                 while ($j < count($answers[$i]['match_data'])) {
                     for ($k = 0; $k < count($questions[$i]); $k++) {
                         if ($questions[$i][$k]->_c_id == $answers[$i]['match_data'][$j]->c_question_id) {
                             $answers[$i]['match_data'][$j]->c_question_id = isset($questions[$i][$k]->new_id) ? $questions[$i][$k]->new_id : 0;
                             break;
                         }
                     }
                     if ($answers[$i]['match_data'][$j]->c_question_id > 0) {
                         $JLMS_DB->insertObject("#__lms_quiz_t_matching", $answers[$i]['match_data'][$j], "c_id");
                     }
                     $j++;
                 }
                 //hotspot processing
                 $j = 0;
                 while ($j < count($answers[$i]['hotspot_data'])) {
                     for ($k = 0; $k < count($questions[$i]); $k++) {
                         if ($questions[$i][$k]->_c_id == $answers[$i]['hotspot_data'][$j]->c_question_id) {
                             $answers[$i]['hotspot_data'][$j]->c_question_id = isset($questions[$i][$k]->new_id) ? $questions[$i][$k]->new_id : 0;
                             break;
                         }
                     }
                     if ($answers[$i]['hotspot_data'][$j]->c_question_id > 0) {
                         $JLMS_DB->insertObject("#__lms_quiz_t_hotspot", $answers[$i]['hotspot_data'][$j], "c_id");
                     }
                     $j++;
                 }
                 //blank quests processing
                 $j = 0;
                 while ($j < count($answers[$i]['blank_data'])) {
                     for ($k = 0; $k < count($questions[$i]); $k++) {
                         if ($questions[$i][$k]->_c_id == $answers[$i]['blank_data'][$j]['obj']->c_question_id) {
                             $answers[$i]['blank_data'][$j]['obj']->c_question_id = isset($questions[$i][$k]->new_id) ? $questions[$i][$k]->new_id : 0;
                             break;
                         }
                     }
                     if ($answers[$i]['blank_data'][$j]['obj']->c_question_id > 0) {
                         $JLMS_DB->insertObject("#__lms_quiz_t_blank", $answers[$i]['blank_data'][$j]['obj'], "c_id");
                         $new_blank_id = $JLMS_DB->insertid();
                         foreach ($answers[$i]['blank_data'][$j]['ans'] as $ans) {
                             $q_blank_text = new stdClass();
                             $q_blank_text->c_blank_id = $new_blank_id;
                             $q_blank_text->c_text = $ans;
                             $q_blank_text->ordering = 0;
                             if ($q_blank_text->c_blank_id > 0) {
                                 $JLMS_DB->insertObject("#__lms_quiz_t_text", $q_blank_text, "c_id");
                             }
                         }
                     }
                     $j++;
                 }
                 $i++;
             }
         }
         if (in_array('documents', $this->options)) {
             $i = 0;
             while ($i < count($documents1)) {
                 $insert = new stdClass();
                 $insert->course_id = $course_id;
                 $insert->owner_id = $my->id;
                 $insert->file_id = 0;
                 if ($documents1[$i]->file_id) {
                     // search $files for new file_id
                     for ($j = 0; $j < count($files); $j++) {
                         if ($files[$j]->_id == $documents1[$i]->file_id) {
                             $insert->file_id = $files[$j]->new_file_id;
                             break;
                         }
                     }
                 }
                 $insert->folder_flag = $documents1[$i]->folder_flag;
                 $insert->doc_name = $documents1[$i]->doc_name;
                 $insert->doc_description = $documents1[$i]->doc_description;
                 $insert->ordering = $documents1[$i]->ordering;
                 $insert->published = $documents1[$i]->published;
                 $insert->publish_start = $documents1[$i]->publish_start;
                 $insert->start_date = $documents1[$i]->start_date;
                 $insert->publish_end = $documents1[$i]->publish_end;
                 $insert->end_date = $documents1[$i]->end_date;
                 // search processed $documents for parent_id
                 $parent = $documents1[$i]->parent_id;
                 if ($parent) {
                     $a = 0;
                     while ($a < $i) {
                         if ($documents1[$a]->id == $parent) {
                             $parent = $documents1[$a]->new_doc_id;
                             break;
                         }
                         $a++;
                     }
                 }
                 $insert->parent_id = $parent;
                 $JLMS_DB->insertObject("#__lms_documents", $insert, "id");
                 $documents1[$i]->new_doc_id = $JLMS_DB->insertid();
                 $i++;
             }
         }
         if (in_array('quizzes', $this->options)) {
             $toDir = $JLMS_CONFIG->get('absolute_path') . "/images/joomlaquiz/images/";
             foreach ($questions as $question) {
                 foreach ($question as $quest) {
                     if (isset($quest->_c_image)) {
                         $filename = $extract_dir . $quest->_identifier . "/" . $quest->_c_image;
                         //$pz->add($filename,'quiz_images', $extract_dir.$quest->identifier."/".substr($quest->_c_image, 0, strpos($quest->_c_image, '\\'))."/");
                         @copy($filename, $toDir . $quest->c_image);
                     }
                 }
             }
         }
         if (in_array('documents', $this->options)) {
             $toDir = $lms_cfg_doc_folder . "/";
             foreach ($files as $file) {
                 $filename = $file->_fromDir . $file->file_srv_name;
                 //$pz->add($filename,'files', $file->_fromDir);
                 copy($filename, $toDir . $file->file_srv_name);
             }
         }
     }
     if ($do_lms_archive) {
         $export_xml = "";
         $export_xml .= "<?xml version=\"1.0\" ?>\r\n";
         $export_xml .= "\t<course_backup lms_version=\"1.0.0\">\r\n";
         $export_xml .= "\t\t<name><![CDATA[" . $course->course_name . "]]></name>\r\n";
         $export_xml .= "\t\t<description><![CDATA[" . $course->course_description . "]]></description>\r\n";
         $export_xml .= "\t\t<course_category><![CDATA[]]></course_category>\r\n";
         $export_xml .= "\t\t<metadesc><![CDATA[" . $course->metadesc . "]]></metadesc>\r\n";
         $export_xml .= "\t\t<metakeys><![CDATA[" . $course->metakeys . "]]></metakeys>\r\n";
         $export_xml .= "\t\t<language_name><![CDATA[english]]></language_name>\r\n";
         $export_xml .= "\t\t<course_paid><![CDATA[" . $course->paid . "]]></course_paid>\r\n";
         $export_xml .= "\t\t<self_registration>" . $course->self_reg . "</self_registration>\r\n";
         $export_xml .= "\t\t<forum_enabled>" . $course->add_forum . "</forum_enabled>\r\n";
         $export_xml .= "\t\t<chat_enabled>" . $course->add_chat . "</chat_enabled>\r\n";
         $export_xml .= "\t\t<publish_start>" . $course->publish_start . "</publish_start>\r\n";
         $export_xml .= "\t\t<publish_start_date><![CDATA[" . $course->start_date . "]]></publish_start_date>\r\n";
         $export_xml .= "\t\t<publish_end>" . $course->publish_end . "</publish_end>\r\n";
         $export_xml .= "\t\t<publish_end_date><![CDATA[" . $course->end_date . "]]></publish_end_date>\r\n";
         $export_xml .= "\t\t<spec_reg>" . $course->spec_reg . "</spec_reg>\r\n";
         $export_xml .= "\t\t<course_question><![CDATA[]]></course_question>\r\n";
         $export_xml .= "\t\t<course_params><![CDATA[" . $course->params . "]]></course_params>\r\n";
         $export_xml .= "\t\t<hidden_menu_items></hidden_menu_items>\n";
         $export_xml .= "\t\t<certificates></certificates>\n";
         $export_xml .= "\t\t<scorms></scorms>\n";
         $export_xml .= "\t\t<zipped_documents></zipped_documents>\n";
         $export_xml .= "\t\t<learn_paths></learn_paths>\n";
         $export_xml .= "\t\t<gradebook_scale></gradebook_scale>\n";
         //GradeBook Items:
         $export_xml .= "\t\t<gradebook_items>\n";
         if (in_array('gradebook_items', $this->options)) {
             foreach ($gradebook as $gb_item) {
                 $export_xml .= "\t\t\t<gb_item gbi_option=\"" . $gb_item->gbi_option . "\" ordering=\"" . $gb_item->ordering . "\" >\n";
                 $export_xml .= "\t\t\t\t<gbi_name><![CDATA[" . $gb_item->gbi_name . "]]></gbi_name>\r\n";
                 $export_xml .= "\t\t\t\t<gbi_description><![CDATA[" . $gb_item->gbi_description . "]]></gbi_description>\r\n";
                 $export_xml .= "\t\t\t\t<gb_category><![CDATA[]]></gb_category>\r\n";
                 $export_xml .= "\t\t\t</gb_item>\n";
             }
         }
         $export_xml .= "\t\t</gradebook_items>\n";
         //links section
         $export_xml .= "\t\t<links>\n";
         if (in_array('links', $this->options)) {
             foreach ($links as $link) {
                 $export_xml .= "\t\t\t<link id=\"" . $link->_id . "\" link_type=\"" . $link->link_type . "\" ordering=\"" . $link->ordering . "\" published=\"" . $link->published . "\">\n";
                 $export_xml .= "\t\t\t\t<linkname><![CDATA[" . $link->link_name . "]]></linkname>\r\n";
                 $export_xml .= "\t\t\t\t<linkhref><![CDATA[" . $link->link_href . "]]></linkhref>\r\n";
                 $export_xml .= "\t\t\t\t<description><![CDATA[" . $link->link_description . "]]></description>\r\n";
                 $export_xml .= "\t\t\t</link>\n";
             }
         }
         $export_xml .= "\t\t</links>\n";
         //files section
         $export_xml .= "\t\t<files>\n";
         if (in_array('documents', $this->options)) {
             foreach ($files as $ff) {
                 $export_xml .= "\t\t\t<file id=\"" . $ff->_id . "\">\n";
                 $export_xml .= "\t\t\t\t<filename><![CDATA[" . $ff->file_name . "]]></filename>\r\n";
                 $export_xml .= "\t\t\t\t<servername><![CDATA[" . $ff->file_srv_name . "]]></servername>\r\n";
                 $export_xml .= "\t\t\t</file>\n";
             }
         }
         $export_xml .= "\t\t</files>\n";
         //documents section
         $export_xml .= "\t\t<documents>\n";
         if (in_array('documents', $this->options)) {
             foreach ($documents1 as $document) {
                 $export_xml .= "\t\t\t<document id=\"" . $document->id . "\" file_id=\"" . $document->file_id . "\" folder_flag=\"" . $document->folder_flag . "\" parent_id=\"" . $document->parent_id . "\" ordering=\"" . $document->ordering . "\" published=\"" . $document->published . "\" publish_start=\"" . $document->publish_start . "\" start_date=\"" . $document->start_date . "\" publish_end=\"" . $document->publish_end . "\" end_date=\"" . $document->end_date . "\" >\r\n";
                 $export_xml .= "\t\t\t\t<doc_name><![CDATA[" . $document->doc_name . "]]></doc_name>\n";
                 $export_xml .= "\t\t\t\t<doc_description><![CDATA[" . $document->doc_description . "]]></doc_description>\n";
                 $export_xml .= "\t\t\t</document>\n";
             }
         }
         $export_xml .= "\t\t</documents>\n";
         $export_xml .= "\t\t<quizzes>\n";
         if (in_array('quizzes', $this->options)) {
             for ($i = 0, $n = count($quizzes); $i < $n; $i++) {
                 $quiz = $quizzes[$i];
                 $question = $questions[$i];
                 $answer = $answers[$i];
                 $export_xml .= "\t\t\t<quiz c_id=\"" . $quiz->_c_id . "\" published=\"" . $quiz->published . "\" >\r\n";
                 $export_xml .= "\t\t\t\t<quiz_title><![CDATA[" . $quiz->c_title . "]]></quiz_title>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_description><![CDATA[" . $quiz->c_description . "]]></quiz_description>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_category><![CDATA[" . $quiz->c_category_id . "]]></quiz_category>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_full_score><![CDATA[" . $quiz->c_full_score . "]]></quiz_full_score>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_time_limit><![CDATA[" . $quiz->c_time_limit . "]]></quiz_time_limit>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_min_after><![CDATA[" . $quiz->c_min_after . "]]></quiz_min_after>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_passing_score><![CDATA[" . $quiz->c_passing_score . "]]></quiz_passing_score>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_right_message><![CDATA[" . $quiz->c_right_message . "]]></quiz_right_message>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_wrong_message><![CDATA[" . $quiz->c_wrong_message . "]]></quiz_wrong_message>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_pass_message><![CDATA[" . $quiz->c_pass_message . "]]></quiz_pass_message>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_unpass_message><![CDATA[" . $quiz->c_unpass_message . "]]></quiz_unpass_message>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_review>" . $quiz->c_enable_review . "</quiz_review>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_email>" . $quiz->c_email_to . "</quiz_email>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_print>" . $quiz->c_enable_print . "</quiz_print>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_certif>" . $quiz->c_enable_sertif . "</quiz_certif>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_skin>" . $quiz->c_skin . "</quiz_skin>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_random>" . $quiz->c_random . "</quiz_random>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_guest>" . $quiz->c_guest . "</quiz_guest>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_slide>" . $quiz->c_slide . "</quiz_slide>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_language>" . $quiz->c_language . "</quiz_language>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_certificate>" . $quiz->c_certificate . "</quiz_certificate>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_gradebook>" . $quiz->c_gradebook . "</quiz_gradebook>\r\n";
                 $export_xml .= "\t\t\t\t<quiz_questions>\n";
                 if (count($question)) {
                     foreach ($question as $quest) {
                         $export_xml .= "\t\t\t\t\t<quiz_question c_id=\"" . $quest->_c_id . "\" c_point=\"" . $quest->c_point . "\" c_attempts=\"" . $quest->c_attempts . "\" c_type=\"" . $quest->c_type . "\" ordering=\"" . $quest->ordering . "\">\n";
                         $export_xml .= "\t\t\t\t\t\t<question_text><![CDATA[" . $quest->c_question . "]]></question_text>\r\n";
                         $export_xml .= "\t\t\t\t\t\t<question_image><![CDATA[" . $quest->c_image . "]]></question_image>\r\n";
                         $export_xml .= "\t\t\t\t\t</quiz_question>\n";
                     }
                 }
                 $export_xml .= "\t\t\t\t</quiz_questions>\n";
                 $export_xml .= "\t\t\t\t<choice_data>\n";
                 if (count($answer['choice_data'])) {
                     foreach ($answer['choice_data'] as $qc_one) {
                         $export_xml .= "\t\t\t\t\t<quest_choice c_question_id=\"" . $qc_one->c_question_id . "\" c_right=\"" . $qc_one->c_right . "\" ordering=\"" . $qc_one->ordering . "\">\r\n";
                         $export_xml .= "\t\t\t\t\t\t<choice_text><![CDATA[" . $qc_one->c_choice . "]]></choice_text>\r\n";
                         $export_xml .= "\t\t\t\t\t</quest_choice>\n";
                     }
                 }
                 $export_xml .= "\t\t\t\t</choice_data>\n";
                 $export_xml .= "\t\t\t\t<match_data>\n";
                 if (count($answer['match_data'])) {
                     foreach ($answer['match_data'] as $qm_one) {
                         $export_xml .= "\t\t\t\t\t<quest_match c_question_id=\"" . $qm_one->c_question_id . "\" ordering=\"" . $qm_one->ordering . "\">\r\n";
                         $export_xml .= "\t\t\t\t\t\t<match_text_left><![CDATA[" . $qm_one->c_left_text . "]]></match_text_left>\r\n";
                         $export_xml .= "\t\t\t\t\t\t<match_text_right><![CDATA[" . $qm_one->c_right_text . "]]></match_text_right>\r\n";
                         $export_xml .= "\t\t\t\t\t</quest_match>\n";
                     }
                 }
                 $export_xml .= "\t\t\t\t</match_data>\n";
                 $export_xml .= "\t\t\t\t<blank_data>\n";
                 if (count($answer['blank_data'])) {
                     foreach ($answer['blank_data'] as $qb_one) {
                         $export_xml .= "\t\t\t\t\t<quest_blank c_question_id=\"" . $qb_one['obj']->c_question_id . "\" ordering=\"" . $qb_one['obj']->_ordering . "\">\r\n";
                         foreach ($qb_one['ans'] as $ans) {
                             $export_xml .= "\t\t\t\t\t\t<blank_text><![CDATA[" . $ans . "]]></blank_text>\r\n";
                         }
                         $export_xml .= "\t\t\t\t\t</quest_blank>\n";
                     }
                 }
                 $export_xml .= "\t\t\t\t</blank_data>\n";
                 $export_xml .= "\t\t\t\t<hotspot_data>\n";
                 if (count($answer['hotspot_data'])) {
                     foreach ($answer['hotspot_data'] as $qh_one) {
                         $export_xml .= "\t\t\t\t\t<quest_hotspot c_question_id=\"" . $qh_one->c_question_id . "\">\r\n";
                         $export_xml .= "\t\t\t\t\t\t<hs_start_x><![CDATA[" . $qh_one->c_start_x . "]]></hs_start_x>\r\n";
                         $export_xml .= "\t\t\t\t\t\t\t<hs_start_y><![CDATA[" . $qh_one->c_start_y . "]]></hs_start_y>\r\n";
                         $export_xml .= "\t\t\t\t\t\t<hs_width><![CDATA[" . $qh_one->c_width . "]]></hs_width>\r\n";
                         $export_xml .= "\t\t\t\t\t\t<hs_height><![CDATA[" . $qh_one->c_height . "]]></hs_height>\r\n";
                         $export_xml .= "\t\t\t\t\t</quest_hotspot>\n";
                     }
                 }
                 $export_xml .= "\t\t\t\t</hotspot_data>\n";
                 $export_xml .= "\t\t\t</quiz>\n";
             }
         }
         $export_xml .= "\t\t</quizzes>\n";
         //end of course backup
         $export_xml .= "\t</course_backup>";
         //end xml file
         $filename_xml = $JLMS_CONFIG->get('absolute_path') . '/media/export.xml';
         $handle = fopen($filename_xml, 'w');
         // try to write in XML file our xml-contents.
         if (fwrite($handle, $export_xml) === FALSE) {
             return array('lmsfile' => null, 'msg' => "Could not create writable XML file");
         }
         fclose($handle);
         $uniq = mktime();
         $dir = $lms_cfg_backup_folder . "/";
         //chmod($dir, '777');
         $backup_zip = $dir . 'course_backup_' . $course_id . '_' . $uniq . '.zip';
         $pz = new PclZip($backup_zip);
         //add _lms_course_files_ catalog
         $pz->create($filename_xml, '', $filename_xml = $JLMS_CONFIG->get('absolute_path') . '/media/');
         if (in_array('documents', $this->options)) {
             foreach ($files as $file) {
                 $filename = $file->_fromDir . $file->file_srv_name;
                 $pz->add($filename, 'files', $file->_fromDir);
             }
         }
         if (in_array('quizzes', $this->options)) {
             foreach ($questions as $question) {
                 foreach ($question as $quest) {
                     if (isset($quest->_c_image)) {
                         $filename = $extract_dir . $quest->_identifier . "/" . $quest->_c_image;
                         $pz->add($filename, 'quiz_images', $extract_dir . $quest->_identifier . "/" . substr($quest->_c_image, 0, strpos($quest->_c_image, '\\')) . "/");
                     }
                 }
             }
         }
     }
     // delete temporary files
     deldir($extract_dir);
     @unlink($archive);
     if (!$do_lms_archive) {
         return array('lmsfile' => 'none', 'msg' => _JLMS_COURSE_IMPORT_SUCCESS);
     }
     return array('lmsfile' => 'course_backup_' . $course_id . '_' . $uniq . '.zip', 'msg' => _JLMS_COURSE_IMPORT_SUCCESS);
 }
Ejemplo n.º 2
0
function JLMS_courseImport_main($backupfile, $option, $course_id, $imp_tools, $course_zname = '', $is_template = false)
{
    global $JLMS_DB, $JLMS_CONFIG, $my;
    if (!$backupfile) {
        mosErrorAlert(_JLMS_EM_SELECT_FILE);
    }
    $backupfile_name = $backupfile['name'];
    $filename = explode(".", $backupfile_name);
    if (empty($backupfile_name)) {
        mosErrorAlert(_JLMS_EM_SELECT_FILE);
    }
    //commented (12.01.2007 Bjarne request forum post #331)
    #if (eregi("[^0-9a-zA-Z_]", $filename[0])) {
    #	mosErrorAlert("File must only contain alphanumeric characters and no special symbols and spaces please.");
    #}
    if (strcmp(substr($backupfile_name, -4, 1), ".")) {
        mosErrorAlert(_JLMS_EM_BAD_FILEEXT);
    }
    if (strcmp(substr($backupfile_name, -4), ".zip")) {
        mosErrorAlert(_JLMS_EM_BAD_FILEEXT);
    }
    $tmp_name = $backupfile['tmp_name'];
    if (!file_exists($tmp_name)) {
        mosErrorAlert(_JLMS_EM_UPLOAD_SIZE_ERROR);
    }
    if (preg_match("/.zip\$/", strtolower($backupfile_name))) {
        $zipFile = new pclZip($tmp_name);
        $zipContentArray = $zipFile->listContent();
        $exp_xml_file = false;
        if (!empty($zipContentArray)) {
            foreach ($zipContentArray as $thisContent) {
                if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
                    mosErrorAlert(_JLMS_EM_READ_PACKAGE_ERROR);
                }
                if ($thisContent['filename'] == 'export.xml') {
                    $exp_xml_file = true;
                }
            }
        }
        if ($exp_xml_file == false) {
            mosErrorAlert("Could not find a Course XML setup file in the package.");
        }
    } else {
        mosErrorAlert(_JLMS_EM_BAD_FILEEXT);
    }
    $config =& JFactory::getConfig();
    $tmp_dest = $config->getValue('config.tmp_path') . DS . $backupfile['name'];
    $tmp_src = $backupfile['tmp_name'];
    // Move uploaded file
    jimport('joomla.filesystem.file');
    if ($is_template) {
        $uploaded = JFile::copy($tmp_src, $tmp_dest);
    } else {
        $uploaded = JFile::upload($tmp_src, $tmp_dest);
    }
    $extract_dir = $config->getValue('config.tmp_path') . DS . "course_backup_" . uniqid(rand(), true) . DS;
    $archive = $tmp_dest;
    //$JLMS_CONFIG->getCfg('absolute_path')."/media/".$backupfile['name'];
    if (!is_file($archive)) {
        $archive = $backupfile['tmp_name'];
    }
    //exstract archive in uniqfolder media
    JLMS_Zip::extractFile($archive, $extract_dir);
    $xmlFile = $extract_dir . "export.xml";
    $xmlDoc =& JLMSFactory::getXMLParser();
    //$xmlDoc->resolveErrors( true );
    if (!$xmlDoc->loadFile($xmlFile)) {
        echo "<script> alert('Error during reading xml file'); window.history.go(-1); </script>\n";
        exit;
    }
    $root =& $xmlDoc->document;
    if ($root->name() != 'course_backup') {
        echo "<script> alert('Not a Course installation file'); window.history.go(-1); </script>\n";
        exit;
    }
    $course = new stdClass();
    // ****************************************************************************************************
    //get config values
    $query = "SELECT * FROM `#__lms_config`";
    $JLMS_DB->SetQuery($query);
    $lms_cfg = $JLMS_DB->LoadObjectList();
    $lms_cfg_doc_folder = '';
    $lms_cfg_scorm = '';
    $lms_cfg_backup_folder = '';
    $lms_cfg_quiz_enabled = 0;
    foreach ($lms_cfg as $lcf) {
        if ($lcf->lms_config_var == 'plugin_quiz') {
            $lms_cfg_quiz_enabled = $lcf->lms_config_value;
        } elseif ($lcf->lms_config_var == 'jlms_doc_folder') {
            $lms_cfg_doc_folder = $lcf->lms_config_value;
        } elseif ($lcf->lms_config_var == 'scorm_folder') {
            $lms_cfg_scorm = $lcf->lms_config_value;
        } elseif ($lcf->lms_config_var == 'jlms_backup_folder') {
            $lms_cfg_backup_folder = $lcf->lms_config_value;
        }
    }
    // ****************************************************************************************************
    // Get course DATA and insert it into the 'lms_courses' table
    if (!$course_id) {
        $element =& $root->getElementByPath('name');
        if (!$course_zname) {
            $course->course_name = $element ? $element->data() : '';
        } else {
            $course->course_name = $course_zname;
        }
        if ($course->course_name) {
            $element =& $root->getElementByPath('description');
            $course->course_description = $element ? $element->data() : '';
            //	echo $xmlDoc->getErrorString();
            //	var_dump($course->course_description); die;
            $element =& $root->getElementByPath('metadesc');
            $course->metadesc = $element ? $element->data() : '';
            $element =& $root->getElementByPath('metakeys');
            $course->metakeys = $element ? $element->data() : '';
            $element =& $root->getElementByPath('self_registration');
            $course->self_reg = $element ? $element->data() : '';
            $element =& $root->getElementByPath('course_paid');
            $course->paid = $element ? $element->data() : 0;
            $course->add_forum = 0;
            $course->add_chat = 0;
            $course->owner_id = $my->id;
            $course->published = 0;
            $element =& $root->getElementByPath('publish_start');
            $course->publish_start = $element ? $element->data() : 0;
            $element =& $root->getElementByPath('publish_end');
            $course->publish_end = $element ? $element->data() : 0;
            $element =& $root->getElementByPath('publish_start_date');
            $course->start_date = $element ? $element->data() : '0000-00-00';
            $element =& $root->getElementByPath('publish_end_date');
            $course->end_date = $element ? $element->data() : '0000-00-00';
            $element =& $root->getElementByPath('spec_reg');
            $course->spec_reg = $element ? $element->data() : 0;
            $course->cat_id = 0;
            $element =& $root->getElementByPath('course_category');
            $course_category_txt = $element ? $element->data() : '';
            $element =& $root->getElementByPath('course_question');
            $course_question_txt = $element ? $element->data() : '';
            // 02.03.2007 1.0.1 support
            $element =& $root->getElementByPath('course_params');
            $params_txt = $element ? $element->data() : '';
            $course->params = '';
            $params = new JLMSParameters($params_txt);
            $params->def('lpath_redirect', 0);
            $params->def('agenda_view', 0);
            $params->def('dropbox_view', 0);
            $params->def('homework_view', 0);
            $params->def('learn_path', 0);
            $course_redirect_lp = $params->get('learn_path');
            $params->set('learn_path', 0);
            $params_ar = $params->toArray();
            if (is_array($params_ar)) {
                foreach ($params_ar as $k => $v) {
                    $txt[] = "{$k}={$v}";
                }
                $course->params = implode("\n", $txt);
            }
            // to do:
            // check lpath_redirect parameter !!!!!!!!!
            $course->gid = 0;
            if ($course_category_txt) {
                $query = "SELECT id FROM #__lms_course_cats WHERE c_category = '" . $course_category_txt . "'";
                $JLMS_DB->SetQuery($query);
                $course_cat_id = $JLMS_DB->LoadResult();
                if ($course_cat_id) {
                    $course->cat_id = $course_cat_id;
                }
            }
            $course->language = 0;
            $element =& $root->getElementByPath('language_name');
            $course_lang_txt = $element ? $element->data() : '';
            if ($course_lang_txt) {
                $query = "SELECT id FROM #__lms_languages WHERE lang_name = '" . $course_lang_txt . "'";
                $JLMS_DB->SetQuery($query);
                $course_lang_id = $JLMS_DB->LoadResult();
                if ($course_lang_id) {
                    $course->language = $course_lang_id;
                }
            }
            $JLMS_DB->insertObject("#__lms_courses", $course, "id");
            //get new Course_id
            $course_id = $JLMS_DB->insertid();
            $is_new_course = true;
            $default_teacher_role = 0;
            $query = "SELECT id FROM #__lms_usertypes WHERE roletype_id = 2 AND default_role = 1 LIMIT 0,1";
            $JLMS_DB->setQuery($query);
            $default_teacher_role = intval($JLMS_DB->LoadResult());
            if (!$default_teacher_role) {
                $query = "SELECT id FROM #__lms_usertypes WHERE roletype_id = 2 LIMIT 0,1";
                $JLMS_DB->setQuery($query);
                $default_teacher_role = intval($JLMS_DB->LoadResult());
                if (!$default_teacher_role) {
                    $default_teacher_role = 1;
                }
            }
            // create teacher for imported course
            $query = "INSERT INTO `#__lms_user_courses` (user_id, course_id, role_id) VALUES ('" . $my->id . "','" . $course_id . "','" . $default_teacher_role . "')";
            $JLMS_DB->setQuery($query);
            $JLMS_DB->query();
            // commented by DEN - 27.03.2008 - enrollment questions are moved into another section
            /*if ($course->spec_reg && $course_question_txt) {
            			$query = "INSERT INTO #__lms_spec_reg_questions (course_id, course_question) VALUES ($course_id, ".$JLMS_DB->Quote($course_question_txt).")";
            			$JLMS_DB->SetQuery( $query );
            			$JLMS_DB->query();
            		}*/
            // ****************************************************************************************************
            // Get hidden menu items DATA, insert it into 'lms_local_menu' table
            //get hidden menu items array from xml
            $element =& $root->getElementByPath('hidden_menu_items');
            $hidden_menus = JLMS_parse_XML_elements($element->children(), array('menu_id', 'user_access'), array());
            $i = 0;
            while ($i < count($hidden_menus)) {
                $hdmn = new stdClass();
                $hdmn->course_id = $course_id;
                $hdmn->menu_id = $hidden_menus[$i]->menu_id;
                $hdmn->user_access = $hidden_menus[$i]->user_access;
                $query = "INSERT INTO #__lms_local_menu (course_id, menu_id, user_access) " . "\n VALUES (" . $course_id . ", " . intval($hdmn->menu_id) . ", " . intval($hdmn->user_access) . ")";
                $JLMS_DB->SetQuery($query);
                $JLMS_DB->query();
                $i++;
            }
        }
    }
    if ($course_id) {
        //get hidden menu items array from xml
        $element =& $root->getElementByPath('course_questions');
        $course_questions = JLMS_parse_XML_elements($element->children(), array('is_optional', 'ordering'), array('question_text', 'default_answer'));
        if (isset($course_question_txt) && $course_question_txt) {
            $add = new stdClass();
            $add->is_optional = 0;
            $add->ordering = 0;
            $add->question_text = $course_question_txt;
            $add->default_answer = '';
            $course_questions[] = $add;
        }
        $i = 0;
        while ($i < count($course_questions)) {
            $insert = new stdClass();
            $insert->course_id = $course_id;
            $insert->is_optional = $course_questions[$i]->is_optional;
            $insert->ordering = $course_questions[$i]->ordering;
            $insert->question_text = $course_questions[$i]->question_text;
            $insert->default_answer = $course_questions[$i]->default_answer;
            $insert->role_id = 0;
            $JLMS_DB->insertObject("#__lms_spec_reg_questions", $insert, "id");
            $i++;
        }
        /*$element 					= &$root->getElementsByPath('course_price', 1);
        		$course_price				= $element ? $element->data() : 0;
        		$query = "INSERT INTO #__lms_course_price (course_id, price ) VALUES('".$course_id."', '".$course_price."')";
        		$JLMS_DB->SetQuery( $query );
        		$JLMS_DB->query();*/
        // ****************************************************************************************************
        // Get files DATA, insert it into 'lms_files' table and copy files to the 'lms_files' folder
        //get files array from xml
        $element =& $root->getElementByPath('files');
        $files_pre = JLMS_parse_XML_elements($element->children(), array('id'), array('filename', 'servername'));
        // 05 June 2007. Nugno sformirovat' spisok of files - tol'ko tex? kotoryi deistvitel'no budut importirovat'sya
        //					(t.e. s uchetom vybrannyx tools pri 'import').
        $documents = array();
        if (in_array(1, $imp_tools)) {
            $element =& $root->getElementByPath('documents');
            $documents = JLMS_parse_XML_elements($element->children(), array('id', 'file_id', 'folder_flag', 'collapsed_folder', 'parent_id', 'ordering', 'published', 'publish_start', 'start_date', 'publish_start', 'publish_end', 'end_date', 'is_time_related', 'show_period'), array('doc_name', 'doc_description'));
        }
        $element =& $root->getElementByPath('certificates');
        $certificates = JLMS_parse_XML_elements($element->children(), array('id', 'file_id', 'crtf_type', 'text_x', 'text_y', 'text_size'), array('certificate_text', 'certificate_font'));
        $element =& $root->getElementByPath('certificate_texts');
        $certificate_texts = JLMS_parse_XML_elements($element->children(), array('parent_id', 'crtf_type', 'text_x', 'text_y', 'text_size'), array('add_certificate_text', 'certificate_font'));
        $quiz_images2 = array();
        // for fields 'c_image' of the question (e.g. hostspot)
        $quiz_images = array();
        if (in_array(5, $imp_tools)) {
            $element_qimg =& $root->getElementByPath('quizzes_images');
            $quiz_images = JLMS_parse_XML_elements($element_qimg->children(), array('c_id', 'file_id'), array('quiz_image_name'));
        }
        /*if (in_array(5,$cid)) {
        			$query = "SELECT * FROM `#__lms_certificates` WHERE course_id = '$course_id'";
        		} else {
        			$query = "SELECT * FROM `#__lms_certificates` WHERE course_id = '$course_id' AND crtf_type <> 2";
        		}*/
        $files = array();
        foreach ($documents as $doc) {
            foreach ($files_pre as $file_pre) {
                if ($doc->file_id == $file_pre->id) {
                    $is_exists = false;
                    foreach ($files as $file_ex) {
                        if ($file_ex->id == $file_pre->id) {
                            $is_exists = true;
                            break;
                        }
                    }
                    if (!$is_exists) {
                        $files[] = $file_pre;
                    }
                    break;
                }
            }
        }
        foreach ($certificates as $crtf) {
            foreach ($files_pre as $file_pre) {
                if ($crtf->file_id == $file_pre->id) {
                    $do_add = false;
                    if ($crtf->crtf_type == 2) {
                        if (in_array(5, $imp_tools)) {
                            $do_add = true;
                        }
                    } else {
                        $do_add = true;
                    }
                    if ($do_add) {
                        $is_exists = false;
                        foreach ($files as $file_ex) {
                            if ($file_ex->id == $file_pre->id) {
                                $is_exists = true;
                                break;
                            }
                        }
                        if (!$is_exists) {
                            $files[] = $file_pre;
                        }
                        break;
                    }
                }
            }
        }
        foreach ($quiz_images as $qimg) {
            foreach ($files_pre as $file_pre) {
                if ($qimg->file_id == $file_pre->id) {
                    $is_exists = false;
                    foreach ($files as $file_ex) {
                        if ($file_ex->id == $file_pre->id) {
                            $is_exists = true;
                            break;
                        }
                    }
                    if (!$is_exists) {
                        $files[] = $file_pre;
                    }
                    break;
                }
            }
        }
        $i = 0;
        $fromDir = $extract_dir . "files/";
        $toDir = $lms_cfg_doc_folder . "/";
        while ($i < count($files)) {
            $insert_file = new stdClass();
            $insert_file->file_name = $files[$i]->filename;
            $file_unique_name = str_pad($course_id, 4, '0', STR_PAD_LEFT) . '_' . md5(uniqid(rand(), true)) . '.' . substr($files[$i]->servername, -3);
            $insert_file->file_srv_name = $file_unique_name;
            $insert_file->owner_id = $my->id;
            $JLMS_DB->insertObject("#__lms_files", $insert_file, "id");
            $files[$i]->new_file_id = $JLMS_DB->insertid();
            rename($fromDir . $files[$i]->servername, $toDir . $file_unique_name);
            $i++;
        }
        $zip_docs = array();
        if (in_array(1, $imp_tools)) {
            // ****************************************************************************************************
            // Get ZIPPACK's DATA, insert it into 'lms_documents_zip' table and copy zippack files to the 'lms_scorm' folder
            $fromDir = $extract_dir . "zippacks/";
            $toDir = $JLMS_CONFIG->getCfg('absolute_path') . "/" . $lms_cfg_scorm . "/";
            $element =& $root->getElementByPath('zipped_documents');
            $zip_docs = JLMS_parse_XML_elements($element->children(), array('id', 'upload_time', 'count_files', 'zip_size', 'zipfile_size', 'is_time_related', 'show_period'), array('zip_folder', 'zip_srv_name', 'zip_name', 'startup_file'));
            $i = 0;
            while ($i < count($zip_docs)) {
                $insert = new stdClass();
                $insert->owner_id = $my->id;
                $insert->course_id = $course_id;
                $folder_unique_name = str_pad($my->id, 4, '0', STR_PAD_LEFT) . '_zip_' . md5(uniqid(rand(), true));
                $insert->zip_folder = $folder_unique_name;
                $file_unique_name = $folder_unique_name . ".zip";
                $insert->zip_srv_name = $file_unique_name;
                $insert->zip_name = $zip_docs[$i]->zip_name;
                $insert->startup_file = $zip_docs[$i]->startup_file;
                $insert->count_files = $zip_docs[$i]->count_files;
                $insert->zip_size = $zip_docs[$i]->zip_size;
                $insert->zipfile_size = $zip_docs[$i]->zipfile_size;
                $insert->upload_time = date('Y-m-d H:i:s');
                //$scorms[$i]->upload_time;
                //insert into DB
                $JLMS_DB->insertObject("#__lms_documents_zip", $insert, "id");
                $zip_docs[$i]->new_zip_id = $JLMS_DB->insertid();
                //move scrom package
                rename($fromDir . $zip_docs[$i]->zip_srv_name, $toDir . $file_unique_name);
                //extract SCORM package archive
                extractBackupArchive($toDir . $file_unique_name, $toDir . $folder_unique_name);
                $i++;
            }
        }
        if (in_array(1, $imp_tools)) {
            // ****************************************************************************************************
            // Get documents DATA and insert it into the 'lms_documents' table
            //$element = &$root->getElementsByPath('documents', 1);
            //$documents = JLMS_parse_XML_elements($element->children(), array('id', 'file_id', 'folder_flag', 'parent_id', 'ordering', 'published', 'publish_start', 'start_date', 'publish_start', 'publish_end', 'end_date'), array('doc_name', 'doc_description'));
            // 05 June 2007 - this array is generated above (~~ line 270)
            $j = 0;
            $collapsed_folders = array();
            while ($j < count($documents)) {
                $insert = new stdClass();
                $insert->course_id = $course_id;
                $insert->owner_id = $my->id;
                $insert->file_id = 0;
                if ($documents[$j]->file_id) {
                    if ($documents[$j]->folder_flag == 2) {
                        // search $zip_docs for new file_id
                        for ($i = 0; $i < count($zip_docs); $i++) {
                            if ($zip_docs[$i]->id == $documents[$j]->file_id) {
                                $insert->file_id = $zip_docs[$i]->new_zip_id;
                                break;
                            }
                        }
                    } else {
                        // search $files for new file_id
                        for ($i = 0; $i < count($files); $i++) {
                            if ($files[$i]->id == $documents[$j]->file_id) {
                                $insert->file_id = $files[$i]->new_file_id;
                                break;
                            }
                        }
                    }
                }
                $insert->folder_flag = $documents[$j]->folder_flag;
                $insert->doc_name = $documents[$j]->doc_name;
                $insert->doc_description = $documents[$j]->doc_description;
                $insert->ordering = $documents[$j]->ordering;
                $insert->published = $documents[$j]->published;
                $insert->publish_start = $documents[$j]->publish_start;
                $insert->start_date = $documents[$j]->start_date;
                $insert->publish_end = $documents[$j]->publish_end;
                $insert->end_date = $documents[$j]->end_date;
                $insert->is_time_related = $documents[$i]->is_time_related;
                $insert->show_period = $documents[$i]->show_period;
                // search processed $documents for parent_id
                $parent = $documents[$j]->parent_id;
                if ($parent) {
                    $a = 0;
                    while ($a < $j) {
                        if ($documents[$a]->id == $parent) {
                            $parent = $documents[$a]->new_doc_id;
                            break;
                        }
                        $a++;
                    }
                }
                $insert->parent_id = $parent;
                $do_ins_object = true;
                $ins_object_folder_already_exists_id = 0;
                if ($insert->folder_flag == 1 && $course_id) {
                    //if we are merging imported course with already existeten course, then some folders can be already exists
                    $query = "SELECT id FROM #__lms_documents WHERE doc_name = " . $JLMS_DB->quote($insert->doc_name) . " AND parent_id = " . intval($insert->parent_id) . " AND folder_flag = 1 AND file_id = 0 AND course_id = {$course_id}";
                    $JLMS_DB->SetQuery($query);
                    $ins_object_folder_already_exists_id = $JLMS_DB->LoadResult();
                    if ($ins_object_folder_already_exists_id) {
                        $do_ins_object = false;
                    }
                }
                if ($do_ins_object) {
                    $JLMS_DB->insertObject("#__lms_documents", $insert, "id");
                    $documents[$j]->new_doc_id = $JLMS_DB->insertid();
                } else {
                    $documents[$j]->new_doc_id = $ins_object_folder_already_exists_id;
                }
                if ($do_ins_object) {
                    if ($documents[$j]->folder_flag == 1 && !$documents[$j]->file_id && $documents[$j]->collapsed_folder) {
                        $collapsed_folders[] = $documents[$j]->new_doc_id;
                    }
                }
                $j++;
            }
            if (!empty($collapsed_folders)) {
                $query = "INSERT INTO #__lms_documents_view (course_id, doc_id) VALUES";
                $s = '';
                $is_add = false;
                foreach ($collapsed_folders as $cf) {
                    if ($cf) {
                        $is_add = true;
                        $query .= $s . "\n ({$course_id}, {$cf})";
                        $s = ',';
                    }
                }
                if ($is_add) {
                    $JLMS_DB->SetQuery($query);
                    $JLMS_DB->query();
                }
            }
        }
        $scorms = array();
        if (in_array(3, $imp_tools)) {
            // ****************************************************************************************************
            // Get SCORM's DATA, insert it into 'lms_scorm_packages' table and copy scorm files to the 'lms_scorm' folder
            $fromDir = $extract_dir . "scorm/";
            $toDir = $JLMS_CONFIG->getCfg('absolute_path') . "/" . $lms_cfg_scorm . "/";
            $element =& $root->getElementByPath('scorms');
            $scorms = JLMS_parse_XML_elements($element->children(), array('id', 'upload_time'), array('foldersrvname', 'packagesrvname', 'packageusername'));
            $i = 0;
            while ($i < count($scorms)) {
                $insert = new stdClass();
                $insert->owner_id = $my->id;
                $insert->course_id = $course_id;
                $folder_unique_name = str_pad($my->id, 4, '0', STR_PAD_LEFT) . '_' . md5(uniqid(rand(), true));
                $insert->folder_srv_name = $folder_unique_name;
                $file_unique_name = $folder_unique_name . ".zip";
                $insert->package_srv_name = $file_unique_name;
                $insert->package_user_name = $scorms[$i]->packageusername;
                $insert->upload_time = date('Y-m-d H:i:s');
                //$scorms[$i]->upload_time;
                //insert into DB
                $JLMS_DB->insertObject("#__lms_scorm_packages", $insert, "id");
                $scorms[$i]->new_sco_id = $JLMS_DB->insertid();
                //move scrom package
                rename($fromDir . $scorms[$i]->packagesrvname, $toDir . $file_unique_name);
                //extract SCORM package archive
                extractBackupArchive($toDir . $file_unique_name, $toDir . $folder_unique_name);
                $i++;
            }
        }
        $links = array();
        if (in_array(4, $imp_tools)) {
            // ****************************************************************************************************
            // Get links DATA and insert it into the 'lms_links' table
            $element =& $root->getElementByPath('links');
            $links = JLMS_parse_XML_elements($element->children(), array('id', 'link_type', 'ordering', 'published', 'is_time_related', 'show_period'), array('linkname', 'linkhref', 'description', 'link_params'));
            $i = 0;
            while ($i < count($links)) {
                $insert = new stdClass();
                $insert->course_id = $course_id;
                $insert->owner_id = $my->id;
                $insert->link_name = $links[$i]->linkname;
                $insert->link_href = $links[$i]->linkhref;
                $insert->link_description = $links[$i]->description;
                $insert->link_type = $links[$i]->link_type;
                $insert->ordering = $links[$i]->ordering;
                $insert->published = $links[$i]->published;
                $insert->is_time_related = $links[$i]->is_time_related;
                $insert->show_period = $links[$i]->show_period;
                $insert->params = $links[$i]->link_params;
                $JLMS_DB->insertObject("#__lms_links", $insert, "id");
                $links[$i]->new_link_id = $JLMS_DB->insertid();
                $i++;
            }
        }
        $homeworks = array();
        if (in_array(7, $imp_tools)) {
            // ****************************************************************************************************
            // Get homeworks DATA and insert it into the 'lms_homework' table
            $element =& $root->getElementByPath('homework_tool');
            $homeworks = JLMS_parse_XML_elements($element->children(), array('id', 'post_date', 'end_date', 'is_time_related', 'show_period'), array('hw_name', 'description', 'short_description'));
            $i = 0;
            while ($i < count($homeworks)) {
                $insert = new stdClass();
                $insert->course_id = $course_id;
                $insert->hw_name = $homeworks[$i]->hw_name;
                $insert->hw_description = $homeworks[$i]->description;
                $insert->hw_shortdescription = $homeworks[$i]->short_description;
                $insert->post_date = $homeworks[$i]->post_date;
                $insert->end_date = $homeworks[$i]->end_date;
                $insert->is_time_related = $homeworks[$i]->is_time_related;
                $insert->show_period = $homeworks[$i]->show_period;
                $JLMS_DB->insertObject("#__lms_homework", $insert, "id");
                $homeworks[$i]->new_hw_id = $JLMS_DB->insertid();
                $i++;
            }
        }
        $announcements = array();
        if (in_array(6, $imp_tools)) {
            // ****************************************************************************************************
            // Get announcements DATA and insert it into the 'lms_agenda' table
            $element =& $root->getElementByPath('announcement_tool');
            $announcements = JLMS_parse_XML_elements($element->children(), array('id', 'start_date', 'end_date', 'is_time_related', 'show_period'), array('announcement_title', 'announcement_content'));
            $i = 0;
            while ($i < count($announcements)) {
                $insert = new stdClass();
                $insert->course_id = $course_id;
                $insert->owner_id = $my->id;
                $insert->title = $announcements[$i]->announcement_title;
                $insert->content = $announcements[$i]->announcement_content;
                $insert->start_date = $announcements[$i]->start_date;
                $insert->end_date = $announcements[$i]->end_date;
                $insert->is_time_related = $announcements[$i]->is_time_related;
                $insert->show_period = $announcements[$i]->show_period;
                $JLMS_DB->insertObject("#__lms_agenda", $insert, "agenda_id");
                $announcements[$i]->new_ag_id = $JLMS_DB->insertid();
                $i++;
            }
        }
        // ****************************************************************************************************
        // Get certificates DATA and insert it into the 'lms_certificates' table
        //$element = &$root->getElementsByPath('certificates', 1);
        //$certificates = JLMS_parse_XML_elements($element->children(), array('id', 'file_id', 'crtf_type', 'text_x', 'text_y', 'text_size'), array('certificate_text'));
        // 05 June 2007 - this array is generated above (~~ line 270)
        $i = 0;
        $crtf_ids_exp = array();
        while ($i < count($certificates)) {
            $do_add = false;
            if ($certificates[$i]->crtf_type == 2) {
                if (in_array(5, $imp_tools)) {
                    $do_add = true;
                }
            } else {
                $do_add = true;
            }
            if ($do_add) {
                $insert = new stdClass();
                $insert->course_id = $course_id;
                $insert->file_id = 0;
                $insert->parent_id = 0;
                if ($certificates[$i]->file_id) {
                    for ($k = 0; $k < count($files); $k++) {
                        if ($files[$k]->id == $certificates[$i]->file_id) {
                            $insert->file_id = $files[$k]->new_file_id;
                            break;
                        }
                    }
                }
                $insert->crtf_text = $certificates[$i]->certificate_text;
                $insert->crtf_font = $certificates[$i]->certificate_font;
                $insert->text_x = $certificates[$i]->text_x;
                $insert->text_y = $certificates[$i]->text_y;
                $insert->text_size = $certificates[$i]->text_size;
                $insert->crtf_type = $certificates[$i]->crtf_type;
                if ($insert->crtf_type == 1) {
                    $insert->published = 0;
                }
                $JLMS_DB->insertObject("#__lms_certificates", $insert, "id");
                $certificates[$i]->new_id = $JLMS_DB->insertid();
                $crtf_ids_exp[] = $certificates[$i]->id;
            }
            $i++;
        }
        // 27.03.2008 (DEN) - Additional certificate texts
        $i = 0;
        while ($i < count($certificate_texts)) {
            $do_add = false;
            if ($certificates[$i]->crtf_type == -2 && $certificates[$i]->parent_id) {
                if (in_array($certificates[$i]->parent_id, $crtf_ids_exp)) {
                    $do_add = true;
                }
            }
            if ($do_add) {
                $insert = new stdClass();
                $insert->course_id = $course_id;
                $insert->file_id = 0;
                $insert->parent_id = 0;
                for ($k = 0; $k < count($certificates); $k++) {
                    if ($certificates[$k]->id == $certificate_texts[$i]->parent_id) {
                        $insert->parent_id = $certificates[$k]->new_id;
                        break;
                    }
                }
                if ($insert->parent_id) {
                    $insert->crtf_text = $certificate_texts[$i]->add_certificate_text;
                    $insert->crtf_font = $certificate_texts[$i]->certificate_font;
                    $insert->text_x = $certificate_texts[$i]->text_x;
                    $insert->text_y = $certificate_texts[$i]->text_y;
                    $insert->text_size = $certificate_texts[$i]->text_size;
                    $insert->crtf_type = $certificate_texts[$i]->crtf_type;
                    $JLMS_DB->insertObject("#__lms_certificates", $insert, "id");
                    $certificate_texts[$i]->new_id = $JLMS_DB->insertid();
                }
            }
            $i++;
        }
        $quizzes = array();
        if (in_array(5, $imp_tools)) {
            // ****************************************************************************************************
            // Get Quizzes DATA and insert it into the DB
            //if ($lms_cfg_quiz_enabled) { // commented 05.03.2007 (to put exported quizzes into LP anywhere) (else we've got errors in LP)
            /* 28 April 2007 (DEN) - Question categories processing
             *
             */
            $element_qcat =& $root->getElementByPath('quizzes_quest_categories');
            $quest_cats = JLMS_parse_XML_elements($element_qcat->children(), array('c_id'), array('quest_category_name', 'quest_category_instr'));
            $i = 0;
            while ($i < count($quest_cats)) {
                $insert = new stdClass();
                $insert->course_id = $course_id;
                $insert->c_category = $quest_cats[$i]->quest_category_name;
                $insert->c_instruction = $quest_cats[$i]->quest_category_instr;
                $insert->is_quiz_cat = 0;
                $JLMS_DB->insertObject("#__lms_quiz_t_category", $insert, "c_id");
                $quest_cats[$i]->new_qcat_id = $JLMS_DB->insertid();
                $i++;
            }
            /* 27 March 2008 (DEN) - Quizzes images processing
             * (variable $quiz_images defined above)
             */
            $i = 0;
            while ($i < count($quiz_images)) {
                $insert = new stdClass();
                $insert->course_id = $course_id;
                $insert->imgs_name = $quiz_images[$i]->quiz_image_name;
                $insert->imgs_id = 0;
                if ($quiz_images[$i]->file_id) {
                    for ($k = 0; $k < count($files); $k++) {
                        if ($files[$k]->id == $quiz_images[$i]->file_id) {
                            $insert->imgs_id = $files[$k]->new_file_id;
                            break;
                        }
                    }
                }
                if ($insert->imgs_id) {
                    $JLMS_DB->insertObject("#__lms_quiz_images", $insert, "c_id");
                    $quiz_images[$i]->new_img_id = $JLMS_DB->insertid();
                    $quiz_images[$i]->imgs_new_id = $insert->imgs_id;
                    // Axtung!: vse tablicy nabora question options (like 't_choice', 't_matching') are stores imgs_id instead of id from #__lms_quiz_images
                }
                $i++;
            }
            /* 28 April 2007 (DEN) - Questions Pool processing
             *
             */
            $element_pool =& $root->getElementByPath('quizzes_question_pool');
            $q_pool = JLMS_parse_XML_elements($element_pool->children(), array(), array(), true, array('pool_questions', 'question_feedbacks', 'choice_data', 'match_data', 'scale_data', 'blank_data', 'hotspot_data'), array(array('c_id', 'c_point', 'c_attempts', 'c_type', 'c_pool', 'c_qcat', 'ordering'), array('quest_id', 'choice_id'), array('c_question_id', 'c_right', 'ordering'), array('c_question_id', 'ordering'), array('c_question_id', 'c_type', 'ordering'), array('c_question_id', 'ordering'), array('c_question_id')), array(array('question_text', 'question_image', 'question_params', 'question_explanation'), array('fb_text'), array('choice_text'), array('match_text_left', 'match_text_right'), array('scale_field'), array('blank_text', 'default_answer'), array('hs_start_x', 'hs_start_y', 'hs_width', 'hs_height')));
            $i = 0;
            while ($i < count($q_pool)) {
                //questions processing
                $j = 0;
                while ($j < count($q_pool[$i]->pool_questions)) {
                    $quest = new stdClass();
                    $quest->course_id = $course_id;
                    $quest->c_quiz_id = 0;
                    //$new_quiz_id;
                    $quest->c_point = $q_pool[$i]->pool_questions[$j]->c_point;
                    $quest->c_attempts = $q_pool[$i]->pool_questions[$j]->c_attempts;
                    $quest->c_question = $q_pool[$i]->pool_questions[$j]->question_text;
                    $quest->c_image = $q_pool[$i]->pool_questions[$j]->question_image;
                    $quest->params = $q_pool[$i]->pool_questions[$j]->question_params;
                    $quest->c_explanation = $q_pool[$i]->pool_questions[$j]->question_explanation;
                    // added 27.03.2008 (DEN)
                    $quest->c_type = $q_pool[$i]->pool_questions[$j]->c_type;
                    $quest->c_pool = 0;
                    //$q_pool[$i]->pool_questions[$j]->c_pool;
                    $quest->c_qcat = 0;
                    if ($q_pool[$i]->pool_questions[$j]->c_qcat) {
                        for ($ij = 0; $ij < count($quest_cats); $ij++) {
                            if ($quest_cats[$ij]->c_id == $q_pool[$i]->pool_questions[$j]->c_qcat) {
                                $quest->c_qcat = $quest_cats[$ij]->new_qcat_id;
                                break;
                            }
                        }
                    }
                    $quest->ordering = $q_pool[$i]->pool_questions[$j]->ordering;
                    if ($q_pool[$i]->pool_questions[$j]->question_image && !in_array($q_pool[$i]->pool_questions[$j]->question_image, $quiz_images2)) {
                        // Changed 20.08.2007 by DEN - from:
                        //if ($q_pool[$i]->pool_questions[$j]->question_image) {
                        $quiz_images2[] = $q_pool[$i]->pool_questions[$j]->question_image;
                    }
                    $JLMS_DB->insertObject("#__lms_quiz_t_question", $quest, "c_id");
                    $q_pool[$i]->pool_questions[$j]->new_id = $JLMS_DB->insertid();
                    $j++;
                }
                //feedbacks processing
                $j = 0;
                while ($j < count($q_pool[$i]->question_feedbacks)) {
                    $q_fb = new stdClass();
                    $q_fb->choice_id = $q_pool[$i]->question_feedbacks[$j]->choice_id;
                    $q_fb->fb_text = $q_pool[$i]->question_feedbacks[$j]->fb_text;
                    $q_fb->quest_id = 0;
                    for ($k = 0; $k < count($q_pool[$i]->pool_questions); $k++) {
                        if ($q_pool[$i]->pool_questions[$k]->c_id == $q_pool[$i]->question_feedbacks[$j]->quest_id) {
                            $q_fb->quest_id = isset($q_pool[$i]->pool_questions[$k]->new_id) ? intval($q_pool[$i]->pool_questions[$k]->new_id) : 0;
                            break;
                        }
                    }
                    if ($q_fb->quest_id && $q_fb->fb_text && ($q_fb->choice_id == -1 || $q_fb->choice_id == 0)) {
                        $query = "INSERT INTO #__lms_quiz_t_question_fb (quest_id, choice_id, fb_text) VALUES ({$q_fb->quest_id}, {$q_fb->choice_id}, " . $JLMS_DB->Quote($q_fb->fb_text) . ")";
                        $JLMS_DB->SetQuery($query);
                        $JLMS_DB->query();
                    }
                    $j++;
                }
                //choices processing
                $j = 0;
                while ($j < count($q_pool[$i]->choice_data)) {
                    $q_choice = new stdClass();
                    $q_choice->c_choice = $q_pool[$i]->choice_data[$j]->choice_text;
                    $q_choice->c_right = $q_pool[$i]->choice_data[$j]->c_right;
                    $q_choice->ordering = $q_pool[$i]->choice_data[$j]->ordering;
                    $q_choice->c_question_id = 0;
                    for ($k = 0; $k < count($q_pool[$i]->pool_questions); $k++) {
                        if ($q_pool[$i]->pool_questions[$k]->c_id == $q_pool[$i]->choice_data[$j]->c_question_id) {
                            $q_choice->c_question_id = isset($q_pool[$i]->pool_questions[$k]->new_id) ? $q_pool[$i]->pool_questions[$k]->new_id : 0;
                            if ($q_pool[$i]->pool_questions[$k]->c_type == 12 || $q_pool[$i]->pool_questions[$k]->c_type == 13) {
                                $q_choice->c_choice = intval($q_choice->c_choice);
                                for ($kk = 0; $kk < count($quiz_images); $kk++) {
                                    if ($q_choice->c_choice == $quiz_images[$kk]->file_id) {
                                        $q_choice->c_choice = isset($quiz_images[$kk]->imgs_new_id) ? intval($quiz_images[$kk]->imgs_new_id) : 0;
                                        break;
                                    }
                                }
                            }
                            break;
                        }
                    }
                    if ($q_choice->c_question_id) {
                        $JLMS_DB->insertObject("#__lms_quiz_t_choice", $q_choice, "c_id");
                    }
                    $j++;
                }
                //matching processing
                $j = 0;
                while ($j < count($q_pool[$i]->match_data)) {
                    $q_match = new stdClass();
                    $q_match->c_left_text = $q_pool[$i]->match_data[$j]->match_text_left;
                    $q_match->c_right_text = $q_pool[$i]->match_data[$j]->match_text_right;
                    $q_match->ordering = $q_pool[$i]->match_data[$j]->ordering;
                    $q_match->c_question_id = 0;
                    for ($k = 0; $k < count($q_pool[$i]->pool_questions); $k++) {
                        if ($q_pool[$i]->pool_questions[$k]->c_id == $q_pool[$i]->match_data[$j]->c_question_id) {
                            $q_match->c_question_id = isset($q_pool[$i]->pool_questions[$k]->new_id) ? $q_pool[$i]->pool_questions[$k]->new_id : 0;
                            if ($q_pool[$i]->pool_questions[$k]->c_type == 11) {
                                $q_match->c_left_text = intval($q_match->c_left_text);
                                $q_match->c_right_text = intval($q_match->c_right_text);
                                $is_changed_match_images = 0;
                                for ($kk = 0; $kk < count($quiz_images); $kk++) {
                                    if ($q_match->c_left_text == $quiz_images[$kk]->file_id) {
                                        $q_match->c_left_text = isset($quiz_images[$kk]->imgs_new_id) ? intval($quiz_images[$kk]->imgs_new_id) : 0;
                                        $is_changed_match_images++;
                                        if ($is_changed_match_images == 2) {
                                            break;
                                        }
                                    }
                                    if ($q_match->c_right_text == $quiz_images[$kk]->file_id) {
                                        $q_match->c_right_text = isset($quiz_images[$kk]->imgs_new_id) ? intval($quiz_images[$kk]->imgs_new_id) : 0;
                                        $is_changed_match_images++;
                                        if ($is_changed_match_images == 2) {
                                            break;
                                        }
                                    }
                                }
                            }
                            break;
                        }
                    }
                    if ($q_match->c_question_id) {
                        $JLMS_DB->insertObject("#__lms_quiz_t_matching", $q_match, "c_id");
                    }
                    $j++;
                }
                //likert scale processing (27.03.2008 - DEN)
                $j = 0;
                while ($j < count($q_pool[$i]->scale_data)) {
                    $q_scale = new stdClass();
                    $q_scale->c_field = $q_pool[$i]->scale_data[$j]->scale_field;
                    $q_scale->c_type = $q_pool[$i]->scale_data[$j]->c_type;
                    $q_scale->ordering = $q_pool[$i]->scale_data[$j]->ordering;
                    $q_scale->c_question_id = 0;
                    for ($k = 0; $k < count($q_pool[$i]->pool_questions); $k++) {
                        if ($q_pool[$i]->pool_questions[$k]->c_id == $q_pool[$i]->scale_data[$j]->c_question_id) {
                            $q_scale->c_question_id = isset($q_pool[$i]->pool_questions[$k]->new_id) ? $q_pool[$i]->pool_questions[$k]->new_id : 0;
                            break;
                        }
                    }
                    if ($q_scale->c_question_id) {
                        $JLMS_DB->insertObject("#__lms_quiz_t_scale", $q_scale, "c_id");
                    }
                    $j++;
                }
                //hotspot processing
                $j = 0;
                while ($j < count($q_pool[$i]->hotspot_data)) {
                    $q_hotspot = new stdClass();
                    $q_hotspot->c_start_x = $q_pool[$i]->hotspot_data[$j]->hs_start_x;
                    $q_hotspot->c_start_y = $q_pool[$i]->hotspot_data[$j]->hs_start_y;
                    $q_hotspot->c_width = $q_pool[$i]->hotspot_data[$j]->hs_width;
                    $q_hotspot->c_height = $q_pool[$i]->hotspot_data[$j]->hs_height;
                    $q_hotspot->c_question_id = 0;
                    for ($k = 0; $k < count($q_pool[$i]->pool_questions); $k++) {
                        if ($q_pool[$i]->pool_questions[$k]->c_id == $q_pool[$i]->hotspot_data[$j]->c_question_id) {
                            $q_hotspot->c_question_id = isset($q_pool[$i]->pool_questions[$k]->new_id) ? $q_pool[$i]->pool_questions[$k]->new_id : 0;
                            break;
                        }
                    }
                    if ($q_hotspot->c_question_id) {
                        $JLMS_DB->insertObject("#__lms_quiz_t_hotspot", $q_hotspot, "c_id");
                    }
                    $j++;
                }
                //blank quests processing
                $j = 0;
                $blank_inserted = array();
                while ($j < count($q_pool[$i]->blank_data)) {
                    $q_blank = new stdClass();
                    $q_blank->c_question_id = 0;
                    $q_blank->c_default = $q_pool[$i]->blank_data[$j]->default_answer;
                    for ($k = 0; $k < count($q_pool[$i]->pool_questions); $k++) {
                        if ($q_pool[$i]->pool_questions[$k]->c_id == $q_pool[$i]->blank_data[$j]->c_question_id) {
                            $q_blank->c_question_id = isset($q_pool[$i]->pool_questions[$k]->new_id) ? $q_pool[$i]->pool_questions[$k]->new_id : 0;
                            break;
                        }
                    }
                    if ($q_blank->c_question_id) {
                        $proceed_insert = true;
                        foreach ($blank_inserted as $bains) {
                            if ($bains->quest_id == $q_blank->c_question_id) {
                                $proceed_insert = false;
                                $new_blank_id = $bains->blank_id;
                                break;
                            }
                        }
                        if ($proceed_insert) {
                            $JLMS_DB->insertObject("#__lms_quiz_t_blank", $q_blank, "c_id");
                            $new_blank_id = $JLMS_DB->insertid();
                            $blankins = new stdClass();
                            $blankins->quest_id = $q_blank->c_question_id;
                            $blankins->blank_id = $new_blank_id;
                            $blank_inserted[] = $blankins;
                        }
                        $q_blank_text = new stdClass();
                        $q_blank_text->c_blank_id = $new_blank_id;
                        $q_blank_text->c_text = $q_pool[$i]->blank_data[$j]->blank_text;
                        $q_blank_text->ordering = $q_pool[$i]->blank_data[$j]->ordering;
                        if ($q_blank_text->c_blank_id) {
                            $JLMS_DB->insertObject("#__lms_quiz_t_text", $q_blank_text, "c_id");
                        }
                    }
                    $j++;
                }
                $i++;
            }
            $element =& $root->getElementByPath('quizzes');
            $quizzes = JLMS_parse_XML_elements($element->children(), array('c_id', 'published'), array('quiz_title', 'quiz_description', 'quiz_category', 'quiz_full_score', 'quiz_time_limit', 'quiz_min_after', 'quiz_passing_score', 'quiz_right_message', 'quiz_wrong_message', 'quiz_pass_message', 'quiz_unpass_message', 'quiz_review', 'quiz_email', 'quiz_print', 'quiz_certif', 'quiz_skin', 'quiz_random', 'quiz_guest', 'quiz_slide', 'quiz_language', 'quiz_certificate', 'quiz_gradebook', 'quiz_params', 'is_time_related', 'show_period'), true, array('quiz_pool_assoc', 'quiz_questions', 'question_feedbacks', 'choice_data', 'match_data', 'scale_data', 'blank_data', 'hotspot_data'), array(array('qcat_id', 'items_number'), array('c_id', 'c_point', 'c_attempts', 'c_type', 'c_pool', 'c_qcat', 'ordering'), array('quest_id', 'choice_id'), array('c_question_id', 'c_right', 'ordering'), array('c_question_id', 'ordering'), array('c_question_id', 'c_type', 'ordering'), array('c_question_id', 'ordering'), array('c_question_id')), array(array(), array('question_text', 'question_image', 'question_params', 'question_explanation'), array('fb_text'), array('choice_text'), array('match_text_left', 'match_text_right'), array('scale_field'), array('blank_text', 'default_answer'), array('hs_start_x', 'hs_start_y', 'hs_width', 'hs_height')));
            $i = 0;
            while ($i < count($quizzes)) {
                $insert = new stdClass();
                $insert->course_id = $course_id;
                $insert->c_category_id = 0;
                $quiz_cat_name = $quizzes[$i]->quiz_category;
                $query = "SELECT c_id FROM #__lms_quiz_t_category WHERE c_category = '" . $quiz_cat_name . "' AND is_quiz_cat = 1";
                $JLMS_DB->SetQuery($query);
                $quiz_cat_id = $JLMS_DB->LoadResult();
                if ($quiz_cat_id) {
                    $insert->c_category_id = $quiz_cat_id;
                } elseif ($quiz_cat_name) {
                    $query = "INSERT INTO #__lms_quiz_t_category (course_id, c_category, c_instruction, is_quiz_cat) VALUES ({$course_id}, " . $JLMS_DB->Quote($quiz_cat_name) . ", '', 1)";
                    $JLMS_DB->SetQuery($query);
                    $JLMS_DB->query();
                    $insert->c_category_id = $JLMS_DB->insertid();
                }
                $insert->c_user_id = $my->id;
                $insert->c_full_score = $quizzes[$i]->quiz_full_score;
                $insert->c_title = $quizzes[$i]->quiz_title;
                $insert->c_description = $quizzes[$i]->quiz_description;
                $insert->c_time_limit = $quizzes[$i]->quiz_time_limit;
                $insert->c_min_after = $quizzes[$i]->quiz_min_after;
                $insert->c_passing_score = $quizzes[$i]->quiz_passing_score;
                $insert->c_created_time = date('Y-m-d');
                $insert->published = $quizzes[$i]->published;
                //!!!
                $insert->c_right_message = $quizzes[$i]->quiz_right_message;
                $insert->c_wrong_message = $quizzes[$i]->quiz_wrong_message;
                $insert->c_pass_message = $quizzes[$i]->quiz_pass_message;
                $insert->c_unpass_message = $quizzes[$i]->quiz_unpass_message;
                $insert->c_enable_review = $quizzes[$i]->quiz_review;
                $insert->c_email_to = $quizzes[$i]->quiz_email;
                $insert->c_enable_print = $quizzes[$i]->quiz_print;
                $insert->c_enable_sertif = $quizzes[$i]->quiz_certif;
                $insert->c_skin = $quizzes[$i]->quiz_skin;
                $insert->c_random = $quizzes[$i]->quiz_random;
                $insert->c_guest = $quizzes[$i]->quiz_guest;
                $insert->c_skin = $quizzes[$i]->quiz_skin;
                $insert->c_slide = $quizzes[$i]->quiz_slide;
                $insert->c_language = $quizzes[$i]->quiz_language;
                $insert->params = $quizzes[$i]->quiz_params;
                $insert->is_time_related = $quizzes[$i]->is_time_related;
                $insert->show_period = $quizzes[$i]->show_period;
                $insert->c_certificate = 0;
                if ($quizzes[$i]->quiz_certificate) {
                    for ($r = 0; $r < count($certificates); $r++) {
                        if ($certificates[$r]->id == $quizzes[$i]->quiz_certificate) {
                            $insert->c_certificate = $certificates[$r]->new_id;
                            break;
                        }
                    }
                }
                $insert->c_gradebook = $quizzes[$i]->quiz_gradebook;
                $JLMS_DB->insertObject("#__lms_quiz_t_quiz", $insert, "c_id");
                $new_quiz_id = $JLMS_DB->insertid();
                $quizzes[$i]->new_quiz_id = $new_quiz_id;
                if (!empty($quizzes[$i]->quiz_pool_assoc)) {
                    $j = 0;
                    while ($j < count($quizzes[$i]->quiz_pool_assoc)) {
                        $ins_qp = new stdClass();
                        $ins_qp->quiz_id = $new_quiz_id;
                        $ins_qp->qcat_id = 0;
                        //$quizzes[$i]->quiz_pool_assoc[$j]->qcat_id;
                        if ($quizzes[$i]->quiz_pool_assoc[$j]->qcat_id) {
                            for ($ij = 0; $ij < count($quest_cats); $ij++) {
                                if ($quest_cats[$ij]->c_id == $quizzes[$i]->quiz_pool_assoc[$j]->qcat_id) {
                                    $ins_qp->qcat_id = $quest_cats[$ij]->new_qcat_id;
                                    break;
                                }
                            }
                        }
                        $ins_qp->items_number = $quizzes[$i]->quiz_pool_assoc[$j]->items_number;
                        $query = "INSERT INTO #__lms_quiz_t_quiz_pool (quiz_id, qcat_id, items_number) VALUES ({$ins_qp->quiz_id}, {$ins_qp->qcat_id}, {$ins_qp->items_number})";
                        $JLMS_DB->SetQuery($query);
                        $JLMS_DB->query();
                        $j++;
                    }
                }
                //questions processing
                $j = 0;
                while ($j < count($quizzes[$i]->quiz_questions)) {
                    $quest = new stdClass();
                    $quest->course_id = $course_id;
                    $quest->c_quiz_id = $new_quiz_id;
                    $quest->c_point = $quizzes[$i]->quiz_questions[$j]->c_point;
                    $quest->c_attempts = $quizzes[$i]->quiz_questions[$j]->c_attempts;
                    $quest->c_question = $quizzes[$i]->quiz_questions[$j]->question_text;
                    $quest->c_image = $quizzes[$i]->quiz_questions[$j]->question_image;
                    $quest->c_type = $quizzes[$i]->quiz_questions[$j]->c_type;
                    $quest->params = $quizzes[$i]->quiz_questions[$j]->question_params;
                    $quest->c_explanation = $quizzes[$i]->quiz_questions[$j]->question_explanation;
                    // added 27.03.2008 (DEN)
                    $quest->c_pool = 0;
                    //$q_pool[$i]->pool_questions[$j]->c_pool;
                    $quest->c_qcat = 0;
                    if ($quizzes[$i]->quiz_questions[$j]->c_qcat) {
                        for ($ij = 0; $ij < count($quest_cats); $ij++) {
                            if ($quest_cats[$ij]->c_id == $quizzes[$i]->quiz_questions[$j]->c_qcat) {
                                $quest->c_qcat = $quest_cats[$ij]->new_qcat_id;
                                break;
                            }
                        }
                    }
                    if ($quizzes[$i]->quiz_questions[$j]->c_pool && $quest->c_type == 20) {
                        if (!empty($q_pool[0]->pool_questions)) {
                            for ($ij = 0; $ij < count($q_pool[0]->pool_questions); $ij++) {
                                if ($q_pool[0]->pool_questions[$ij]->c_id == $quizzes[$i]->quiz_questions[$j]->c_pool) {
                                    $quest->c_pool = $q_pool[0]->pool_questions[$ij]->new_id;
                                    break;
                                }
                            }
                        }
                    }
                    $quest->ordering = $quizzes[$i]->quiz_questions[$j]->ordering;
                    if ($quizzes[$i]->quiz_questions[$j]->question_image && !in_array($quizzes[$i]->quiz_questions[$j]->question_image, $quiz_images2)) {
                        // Changed 20.08.2007 by DEN - from:
                        //if ($q_pool[$i]->pool_questions[$j]->question_image) {
                        $quiz_images2[] = $quizzes[$i]->quiz_questions[$j]->question_image;
                    }
                    $JLMS_DB->insertObject("#__lms_quiz_t_question", $quest, "c_id");
                    $quizzes[$i]->quiz_questions[$j]->new_id = $JLMS_DB->insertid();
                    $j++;
                }
                //feedbacks processing
                $j = 0;
                while ($j < count($quizzes[$i]->question_feedbacks)) {
                    $q_fb = new stdClass();
                    $q_fb->choice_id = $quizzes[$i]->question_feedbacks[$j]->choice_id;
                    $q_fb->fb_text = $quizzes[$i]->question_feedbacks[$j]->fb_text;
                    $q_fb->quest_id = 0;
                    for ($k = 0; $k < count($quizzes[$i]->quiz_questions); $k++) {
                        if ($quizzes[$i]->quiz_questions[$k]->c_id == $quizzes[$i]->question_feedbacks[$j]->quest_id) {
                            $q_fb->quest_id = isset($quizzes[$i]->quiz_questions[$k]->new_id) ? intval($quizzes[$i]->quiz_questions[$k]->new_id) : 0;
                            break;
                        }
                    }
                    if ($q_fb->quest_id && $q_fb->fb_text && ($q_fb->choice_id == -1 || $q_fb->choice_id == 0)) {
                        $query = "INSERT INTO #__lms_quiz_t_question_fb (quest_id, choice_id, fb_text) VALUES ({$q_fb->quest_id}, {$q_fb->choice_id}, " . $JLMS_DB->Quote($q_fb->fb_text) . ")";
                        $JLMS_DB->SetQuery($query);
                        $JLMS_DB->query();
                    }
                    $j++;
                }
                //choices processing
                $j = 0;
                while ($j < count($quizzes[$i]->choice_data)) {
                    $q_choice = new stdClass();
                    $q_choice->c_choice = $quizzes[$i]->choice_data[$j]->choice_text;
                    $q_choice->c_right = $quizzes[$i]->choice_data[$j]->c_right;
                    $q_choice->ordering = $quizzes[$i]->choice_data[$j]->ordering;
                    $q_choice->c_question_id = 0;
                    for ($k = 0; $k < count($quizzes[$i]->quiz_questions); $k++) {
                        if ($quizzes[$i]->quiz_questions[$k]->c_id == $quizzes[$i]->choice_data[$j]->c_question_id) {
                            $q_choice->c_question_id = isset($quizzes[$i]->quiz_questions[$k]->new_id) ? $quizzes[$i]->quiz_questions[$k]->new_id : 0;
                            if ($quizzes[$i]->quiz_questions[$k]->c_type == 12 || $quizzes[$i]->quiz_questions[$k]->c_type == 13) {
                                $q_choice->c_choice = intval($q_choice->c_choice);
                                for ($kk = 0; $kk < count($quiz_images); $kk++) {
                                    if ($q_choice->c_choice == $quiz_images[$kk]->file_id) {
                                        $q_choice->c_choice = isset($quiz_images[$kk]->imgs_new_id) ? intval($quiz_images[$kk]->imgs_new_id) : 0;
                                        break;
                                    }
                                }
                            }
                            break;
                        }
                    }
                    if ($q_choice->c_question_id) {
                        $JLMS_DB->insertObject("#__lms_quiz_t_choice", $q_choice, "c_id");
                    }
                    $j++;
                }
                //matching processing
                $j = 0;
                while ($j < count($quizzes[$i]->match_data)) {
                    $q_match = new stdClass();
                    $q_match->c_left_text = $quizzes[$i]->match_data[$j]->match_text_left;
                    $q_match->c_right_text = $quizzes[$i]->match_data[$j]->match_text_right;
                    $q_match->ordering = $quizzes[$i]->match_data[$j]->ordering;
                    $q_match->c_question_id = 0;
                    for ($k = 0; $k < count($quizzes[$i]->quiz_questions); $k++) {
                        if ($quizzes[$i]->quiz_questions[$k]->c_id == $quizzes[$i]->match_data[$j]->c_question_id) {
                            $q_match->c_question_id = isset($quizzes[$i]->quiz_questions[$k]->new_id) ? $quizzes[$i]->quiz_questions[$k]->new_id : 0;
                            break;
                            if ($quizzes[$i]->quiz_questions[$k]->c_type == 11) {
                                $q_match->c_left_text = intval($q_match->c_left_text);
                                $q_match->c_right_text = intval($q_match->c_right_text);
                                $is_changed_match_images = 0;
                                for ($kk = 0; $kk < count($quiz_images); $kk++) {
                                    if ($q_match->c_left_text == $quiz_images[$kk]->file_id) {
                                        $q_match->c_left_text = isset($quiz_images[$kk]->imgs_new_id) ? intval($quiz_images[$kk]->imgs_new_id) : 0;
                                        $is_changed_match_images++;
                                        if ($is_changed_match_images == 2) {
                                            break;
                                        }
                                    }
                                    if ($q_match->c_right_text == $quiz_images[$kk]->file_id) {
                                        $q_match->c_right_text = isset($quiz_images[$kk]->imgs_new_id) ? intval($quiz_images[$kk]->imgs_new_id) : 0;
                                        $is_changed_match_images++;
                                        if ($is_changed_match_images == 2) {
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if ($q_match->c_question_id) {
                        $JLMS_DB->insertObject("#__lms_quiz_t_matching", $q_match, "c_id");
                    }
                    $j++;
                }
                //likert scale processing (27.03.2008 - DEN)
                $j = 0;
                while ($j < count($quizzes[$i]->scale_data)) {
                    $q_scale = new stdClass();
                    $q_scale->c_field = $quizzes[$i]->scale_data[$j]->scale_field;
                    $q_scale->c_type = $quizzes[$i]->scale_data[$j]->c_type;
                    $q_scale->ordering = $quizzes[$i]->scale_data[$j]->ordering;
                    $q_scale->c_question_id = 0;
                    for ($k = 0; $k < count($quizzes[$i]->quiz_questions); $k++) {
                        if ($quizzes[$i]->quiz_questions[$k]->c_id == $quizzes[$i]->scale_data[$j]->c_question_id) {
                            $q_scale->c_question_id = isset($quizzes[$i]->quiz_questions[$k]->new_id) ? $quizzes[$i]->quiz_questions[$k]->new_id : 0;
                            break;
                        }
                    }
                    if ($q_scale->c_question_id) {
                        $JLMS_DB->insertObject("#__lms_quiz_t_scale", $q_scale, "c_id");
                    }
                    $j++;
                }
                //hotspot processing
                $j = 0;
                while ($j < count($quizzes[$i]->hotspot_data)) {
                    $q_hotspot = new stdClass();
                    $q_hotspot->c_start_x = $quizzes[$i]->hotspot_data[$j]->hs_start_x;
                    $q_hotspot->c_start_y = $quizzes[$i]->hotspot_data[$j]->hs_start_y;
                    $q_hotspot->c_width = $quizzes[$i]->hotspot_data[$j]->hs_width;
                    $q_hotspot->c_height = $quizzes[$i]->hotspot_data[$j]->hs_height;
                    $q_hotspot->c_question_id = 0;
                    for ($k = 0; $k < count($quizzes[$i]->quiz_questions); $k++) {
                        if ($quizzes[$i]->quiz_questions[$k]->c_id == $quizzes[$i]->hotspot_data[$j]->c_question_id) {
                            $q_hotspot->c_question_id = isset($quizzes[$i]->quiz_questions[$k]->new_id) ? $quizzes[$i]->quiz_questions[$k]->new_id : 0;
                            break;
                        }
                    }
                    if ($q_hotspot->c_question_id) {
                        $JLMS_DB->insertObject("#__lms_quiz_t_hotspot", $q_hotspot, "c_id");
                    }
                    $j++;
                }
                //blank quests processing
                $j = 0;
                $blank_inserted = array();
                while ($j < count($quizzes[$i]->blank_data)) {
                    $q_blank = new stdClass();
                    $q_blank->c_question_id = 0;
                    $q_blank->c_default = $quizzes[$i]->blank_data[$j]->default_answer;
                    for ($k = 0; $k < count($quizzes[$i]->quiz_questions); $k++) {
                        if ($quizzes[$i]->quiz_questions[$k]->c_id == $quizzes[$i]->blank_data[$j]->c_question_id) {
                            $q_blank->c_question_id = isset($quizzes[$i]->quiz_questions[$k]->new_id) ? $quizzes[$i]->quiz_questions[$k]->new_id : 0;
                            break;
                        }
                    }
                    if ($q_blank->c_question_id) {
                        $proceed_insert = true;
                        foreach ($blank_inserted as $bains) {
                            if ($bains->quest_id == $q_blank->c_question_id) {
                                $proceed_insert = false;
                                $new_blank_id = $bains->blank_id;
                                break;
                            }
                        }
                        if ($proceed_insert) {
                            $JLMS_DB->insertObject("#__lms_quiz_t_blank", $q_blank, "c_id");
                            $new_blank_id = $JLMS_DB->insertid();
                            $blankins = new stdClass();
                            $blankins->quest_id = $q_blank->c_question_id;
                            $blankins->blank_id = $new_blank_id;
                            $blank_inserted[] = $blankins;
                        }
                        $q_blank_text = new stdClass();
                        $q_blank_text->c_blank_id = $new_blank_id;
                        $q_blank_text->c_text = $quizzes[$i]->blank_data[$j]->blank_text;
                        $q_blank_text->ordering = $quizzes[$i]->blank_data[$j]->ordering;
                        if ($q_blank_text->c_blank_id) {
                            $JLMS_DB->insertObject("#__lms_quiz_t_text", $q_blank_text, "c_id");
                        }
                    }
                    $j++;
                }
                $i++;
            }
            // ****************************************************************************************************
            // Copy quiz images
            if (count($quiz_images2)) {
                $fromDir = $extract_dir . "quiz_images/";
                $toDir = $JLMS_CONFIG->getCfg('absolute_path') . "/images/joomlaquiz/images/";
                $i = 0;
                while ($i < count($quiz_images2)) {
                    if (file_exists($fromDir . $quiz_images2[$i])) {
                        @rename($fromDir . $quiz_images2[$i], $toDir . $quiz_images2[$i]);
                    }
                    $i++;
                }
            }
            //} // end if ($lms_cfg_quiz_enabled)
        }
        $l_paths = array();
        $lpath_prerequisites = array();
        if (in_array(2, $imp_tools)) {
            // ****************************************************************************************************
            // Get LearningPaths DATA and insert it into the DB
            $element =& $root->getElementByPath('learn_paths');
            $l_paths = JLMS_parse_XML_elements($element->children(), array('id', 'item_id', 'ordering', 'published', 'is_time_related', 'show_period'), array('lp_name', 'lp_shortdescription', 'lp_description', 'lp_params'), true, array('prerequisites', 'steps', 'conds'), array(array('lpath_id', 'req_id', 'time_minutes'), array('id', 'item_id', 'lpath_id', 'step_type', 'parent_id', 'ordering'), array('lpath_id', 'step_id', 'ref_step', 'cond_type', 'cond_value')), array(array(), array('step_name', 'step_shortdescription', 'step_description'), array()));
            $i = 0;
            $process_steps_on_exit = array();
            while ($i < count($l_paths)) {
                $insert = new stdClass();
                $insert->course_id = $course_id;
                $insert->owner_id = $my->id;
                $insert->lpath_name = $l_paths[$i]->lp_name;
                if ($l_paths[$i]->item_id) {
                    $n = 0;
                    $scorm_found = false;
                    while ($n < count($scorms)) {
                        if ($scorms[$n]->id == $l_paths[$i]->item_id) {
                            $insert->item_id = $scorms[$n]->new_sco_id;
                            $scorm_found = true;
                            break;
                        }
                        $n++;
                    }
                    if (!$scorm_found) {
                        $i++;
                        continue;
                    }
                } else {
                    $insert->item_id = 0;
                }
                $insert->lpath_shortdescription = $l_paths[$i]->lp_shortdescription;
                $insert->lp_params = $l_paths[$i]->lp_params;
                $insert->lpath_description = $l_paths[$i]->lp_description;
                $insert->ordering = $l_paths[$i]->ordering;
                $insert->published = $l_paths[$i]->published;
                $insert->lp_type = 0;
                // for compatibility with SCORM import/export (if lp_type == 0, then new instance in 'lms_n_scorm' will be added automatically)
                $insert->is_time_related = $l_paths[$i]->is_time_related;
                $insert->show_period = $l_paths[$i]->show_period;
                $JLMS_DB->insertObject("#__lms_learn_paths", $insert, "id");
                $l_paths[$i]->new_step_id = $JLMS_DB->insertid();
                // 18 August 2007 - DEN - prerequisites import
                $j = 0;
                while ($j < count($l_paths[$i]->prerequisites)) {
                    $new_prereq = new stdClass();
                    $new_prereq->old_lpath_id = $l_paths[$i]->prerequisites[$j]->lpath_id;
                    $new_prereq->old_req_id = $l_paths[$i]->prerequisites[$j]->req_id;
                    $new_prereq->lpath_id = 0;
                    $new_prereq->req_id = 0;
                    $new_prereq->time_minutes = 0;
                    $lpath_prerequisites[] = $new_prereq;
                    $j++;
                }
                if ($l_paths[$i]->item_id) {
                } else {
                    $j = 0;
                    while ($j < count($l_paths[$i]->steps)) {
                        $step = new stdClass();
                        $step->course_id = $course_id;
                        $step->lpath_id = $l_paths[$i]->new_step_id;
                        $step->step_type = $l_paths[$i]->steps[$j]->step_type;
                        $do_process_on_exit = false;
                        $proc_exit_item = 0;
                        if ($step->step_type == 2) {
                            //document
                            $is_doc_found = false;
                            for ($k = 0; $k < count($documents); $k++) {
                                if ($documents[$k]->id == $l_paths[$i]->steps[$j]->item_id) {
                                    $step->item_id = $documents[$k]->new_doc_id;
                                    $is_doc_found = true;
                                    break;
                                }
                            }
                            if (!$is_doc_found) {
                                $step->step_type = 4;
                            }
                        } elseif ($step->step_type == 3) {
                            //link
                            $is_link_found = false;
                            for ($k = 0; $k < count($links); $k++) {
                                if ($links[$k]->id == $l_paths[$i]->steps[$j]->item_id) {
                                    $step->item_id = $links[$k]->new_link_id;
                                    $is_link_found = true;
                                    break;
                                }
                            }
                            if (!$is_link_found) {
                                $step->step_type = 4;
                            }
                        } elseif ($step->step_type == 5) {
                            //quiz
                            $is_quiz_found = false;
                            for ($k = 0; $k < count($quizzes); $k++) {
                                if ($quizzes[$k]->c_id == $l_paths[$i]->steps[$j]->item_id) {
                                    $step->item_id = $quizzes[$k]->new_quiz_id;
                                    $is_quiz_found = true;
                                    break;
                                }
                            }
                            if (!$is_quiz_found) {
                                $step->step_type = 4;
                            }
                        } elseif ($step->step_type == 6) {
                            //scorm
                            // Axtung - we should make all scorm steps - as 4 (content) and after processing all LPaths -> upgrade them to type 6 (scorm) and link to scorm LPath
                            // (because) at this step not all Lpaths are processed !!!
                            $step->step_type = 4;
                            $do_process_on_exit = true;
                            $proc_exit_item = $l_paths[$i]->steps[$j]->item_id;
                        } else {
                            //other
                            $step->item_id = $l_paths[$i]->steps[$j]->item_id;
                        }
                        $parent = $l_paths[$i]->steps[$j]->parent_id;
                        // search processed steps for parent_id
                        if ($parent) {
                            $a = 0;
                            while ($a < $j) {
                                if ($l_paths[$i]->steps[$a]->id == $parent) {
                                    $parent = $l_paths[$i]->steps[$a]->new_id;
                                    break;
                                }
                                $a++;
                            }
                        }
                        $step->parent_id = $parent;
                        $step->step_name = $l_paths[$i]->steps[$j]->step_name;
                        $step->step_shortdescription = $l_paths[$i]->steps[$j]->step_shortdescription;
                        $step->step_description = $l_paths[$i]->steps[$j]->step_description;
                        $step->ordering = $l_paths[$i]->steps[$j]->ordering;
                        $JLMS_DB->insertObject("#__lms_learn_path_steps", $step, "id");
                        $l_paths[$i]->steps[$j]->new_id = $JLMS_DB->insertid();
                        if ($do_process_on_exit) {
                            $pr_step = new stdClass();
                            $pr_step->step_id = $l_paths[$i]->steps[$j]->new_id;
                            $pr_step->item_id = $proc_exit_item;
                            $pr_step->new_item_id = 0;
                            $process_steps_on_exit[] = $pr_step;
                        }
                        $j++;
                    }
                    $j = 0;
                    while ($j < count($l_paths[$i]->conds)) {
                        $cond = new stdClass();
                        $cond->course_id = $course_id;
                        $cond->lpath_id = $l_paths[$i]->new_step_id;
                        $cond->cond_type = $l_paths[$i]->conds[$j]->cond_type;
                        $cond->cond_value = $l_paths[$i]->conds[$j]->cond_value;
                        $st1 = $l_paths[$i]->conds[$j]->step_id;
                        if ($st1) {
                            $a = 0;
                            while ($a < count($l_paths[$i]->steps)) {
                                if ($l_paths[$i]->steps[$a]->id == $st1) {
                                    $st1 = $l_paths[$i]->steps[$a]->new_id;
                                    break;
                                }
                                $a++;
                            }
                        }
                        $cond->step_id = $st1;
                        $st2 = $l_paths[$i]->conds[$j]->ref_step;
                        if ($st2) {
                            $a = 0;
                            while ($a < count($l_paths[$i]->steps)) {
                                if ($l_paths[$i]->steps[$a]->id == $st2) {
                                    $st2 = $l_paths[$i]->steps[$a]->new_id;
                                    break;
                                }
                                $a++;
                            }
                        }
                        $cond->ref_step = $st2;
                        $JLMS_DB->insertObject("#__lms_learn_path_conds", $cond, "id");
                        $j++;
                    }
                }
                $i++;
            }
            // 18 August 2007 - DEN - import scorm steps
            if (!empty($process_steps_on_exit)) {
                $a = 0;
                while ($a < count($process_steps_on_exit)) {
                    foreach ($l_paths as $lp) {
                        if ($lp->id == $process_steps_on_exit[$a]->item_id) {
                            $process_steps_on_exit[$a]->new_item_id = $lp->new_step_id;
                            // xm.. why 'step' ?? (bad name)
                        }
                    }
                    $a++;
                }
                foreach ($process_steps_on_exit as $lp_pse) {
                    if ($lp_pse->new_item_id && $lp_pse->step_id) {
                        $query = "UPDATE #__lms_learn_path_steps SET step_type = 6, item_id = " . intval($lp_pse->new_item_id) . " WHERE id = " . intval($lp_pse->step_id);
                        $JLMS_DB->SetQuery($query);
                        $JLMS_DB->query();
                    }
                }
            }
            // 18 August 2007 - DEN - prerequisites import
            if (!empty($lpath_prerequisites)) {
                $a = 0;
                while ($a < count($lpath_prerequisites)) {
                    foreach ($l_paths as $lp) {
                        if ($lp->id == $lpath_prerequisites[$a]->old_lpath_id) {
                            $lpath_prerequisites[$a]->lpath_id = $lp->new_step_id;
                            // xm.. why 'step' ?? (bad name)
                        }
                        if ($lp->id == $lpath_prerequisites[$a]->old_req_id) {
                            $lpath_prerequisites[$a]->req_id = $lp->new_step_id;
                            // xm.. why 'step' ?? (bad name)
                        }
                    }
                    $a++;
                }
                foreach ($lpath_prerequisites as $lp_pre) {
                    if ($lp_pre->lpath_id && $lp_pre->req_id && $lp_pre->lpath_id != $lp_pre->req_id) {
                        $query = "INSERT INTO #__lms_learn_path_prerequisites (lpath_id, req_id, time_minutes) VALUES (" . intval($lp_pre->lpath_id) . ", " . intval($lp_pre->req_id) . ", " . intval($lp_pre->time_minutes) . ")";
                        $JLMS_DB->SetQuery($query);
                        $JLMS_DB->query();
                    }
                }
            }
            if ($course_redirect_lp) {
                $lp_id = 0;
                foreach ($l_paths as $lp) {
                    if ($lp->id == $course_redirect_lp) {
                        $lp_id = $lp->new_step_id;
                        // xm.. why 'step' ?? (bad name)
                        break;
                    }
                }
                if ($lp_id) {
                    $params->set('learn_path', $lp_id);
                    $params_ar = $params->toArray();
                    if (is_array($params_ar)) {
                        foreach ($params_ar as $k => $v) {
                            $txt[] = "{$k}={$v}";
                        }
                        $new_params = implode("\n", $txt);
                        if ($new_params) {
                            $query = "UPDATE #__lms_courses SET " . $JLMS_DB->NameQuote('params') . " = " . $JLMS_DB->Quote($new_params) . " WHERE id = {$course_id}";
                            $JLMS_DB->SetQuery($query);
                            $JLMS_DB->query();
                        }
                    }
                }
            }
        }
        $course_topics = array();
        // ****************************************************************************************************
        // Get TOPICS Items DATA and insert it into the 'lms_topics', 'lms_topic_items' tables - 27.03.2008 (DEN)
        $element =& $root->getElementByPath('course_topics');
        $course_topics = JLMS_parse_XML_elements($element->children(), array('topic_id', 'ordering', 'published', 'publish_start', 'start_date', 'publish_end', 'end_date', 'is_time_related', 'show_period'), array('topic_name', 'topic_description'));
        $i = 0;
        while ($i < count($course_topics)) {
            $insert = new stdClass();
            $insert->course_id = $course_id;
            $insert->ordering = $course_topics[$i]->ordering;
            $insert->name = $course_topics[$i]->topic_name;
            $insert->description = $course_topics[$i]->topic_description;
            $insert->published = $course_topics[$i]->published;
            $insert->start_date = $course_topics[$i]->start_date;
            $insert->publish_start = $course_topics[$i]->publish_start;
            $insert->end_date = $course_topics[$i]->end_date;
            $insert->publish_end = $course_topics[$i]->publish_end;
            $insert->is_time_related = $course_topics[$i]->is_time_related;
            $insert->show_period = $course_topics[$i]->show_period;
            $JLMS_DB->insertObject("#__lms_topics", $insert, "id");
            $course_topics[$i]->new_topic_id = $JLMS_DB->insertid();
            $i++;
        }
        $element =& $root->getElementByPath('course_topic_items');
        $course_topic_items = JLMS_parse_XML_elements($element->children(), array('topic_id', 'item_id', 'item_type', 'ordering', 'is_shown'), array());
        $i = 0;
        while ($i < count($course_topic_items)) {
            $insert = new stdClass();
            $insert->course_id = $course_id;
            $insert->topic_id = 0;
            foreach ($course_topics as $ct) {
                if ($course_topic_items[$i]->topic_id == $ct->topic_id) {
                    $insert->topic_id = $ct->new_topic_id;
                }
            }
            if ($insert->topic_id) {
                $insert->ordering = $course_topic_items[$i]->ordering;
                $insert->item_type = $course_topic_items[$i]->item_type;
                $insert->show = $course_topic_items[$i]->is_shown ? 1 : 0;
                $insert->item_id = 0;
                $do_add_topic_item = false;
                if ($insert->item_type == 2) {
                    // documents
                    if (in_array(1, $imp_tools)) {
                        for ($k = 0; $k < count($documents); $k++) {
                            if ($documents[$k]->id == $course_topic_items[$i]->item_id) {
                                $insert->item_id = $documents[$k]->new_doc_id;
                                $do_add_topic_item = true;
                                break;
                            }
                        }
                    }
                } elseif ($insert->item_type == 3) {
                    // links
                    if (in_array(4, $imp_tools)) {
                        for ($k = 0; $k < count($links); $k++) {
                            if ($links[$k]->id == $course_topic_items[$i]->item_id) {
                                $insert->item_id = $links[$k]->new_link_id;
                                $do_add_topic_item = true;
                                break;
                            }
                        }
                    }
                } elseif ($insert->item_type == 5) {
                    // quizzes
                    if (in_array(5, $imp_tools)) {
                        for ($k = 0; $k < count($quizzes); $k++) {
                            if ($quizzes[$k]->c_id == $course_topic_items[$i]->item_id) {
                                $insert->item_id = $quizzes[$k]->new_quiz_id;
                                $do_add_topic_item = true;
                                break;
                            }
                        }
                    }
                } elseif ($insert->item_type == 7) {
                    // learning paths
                    if (in_array(2, $imp_tools)) {
                        for ($k = 0; $k < count($l_paths); $k++) {
                            if ($l_paths[$k]->id == $course_topic_items[$i]->item_id) {
                                $insert->item_id = $l_paths[$k]->new_step_id;
                                $do_add_topic_item = true;
                                break;
                            }
                        }
                    }
                }
                if ($do_add_topic_item && $insert->item_id) {
                    $JLMS_DB->insertObject("#__lms_topic_items", $insert, "id");
                    $course_topic_items[$i]->new_topic_item_id = $JLMS_DB->insertid();
                }
            }
            $i++;
        }
        if (in_array(8, $imp_tools)) {
            // ****************************************************************************************************
            // Get GradeBook Items DATA and insert it into the 'lms_gradebook_items' table
            $element =& $root->getElementByPath('gradebook_items');
            $gb_items = JLMS_parse_XML_elements($element->children(), array('gbi_option', 'ordering'), array('gbi_name', 'gbi_description', 'gb_category'));
            $i = 0;
            while ($i < count($gb_items)) {
                $insert = new stdClass();
                $query = "SELECT id FROM #__lms_gradebook_cats WHERE gb_category = '" . $gb_items[$i]->gb_category . "'";
                $JLMS_DB->SetQuery($query);
                $cat_id = $JLMS_DB->LoadResult();
                if (!$cat_id) {
                    $cat_id = 0;
                }
                $insert->course_id = $course_id;
                $insert->gbc_id = $cat_id;
                $insert->gbi_name = $gb_items[$i]->gbi_name;
                $insert->gbi_description = $gb_items[$i]->gbi_description;
                $insert->gbi_points = 0;
                $insert->gbi_option = $gb_items[$i]->gbi_option;
                $insert->ordering = $gb_items[$i]->ordering;
                $JLMS_DB->insertObject("#__lms_gradebook_items", $insert, "id");
                $gb_items[$i]->new_gbi_id = $JLMS_DB->insertid();
                $i++;
            }
            // ****************************************************************************************************
            // Get GradeBook Scale DATA and insert it into the 'lms_gradebook_scale' table
            $element =& $root->getElementByPath('gradebook_scale');
            $gb_scale = JLMS_parse_XML_elements($element->children(), array('min_val', 'max_val', 'ordering'), array('scale_name'));
            $i = 0;
            while ($i < count($gb_scale)) {
                $insert = new stdClass();
                $insert->course_id = $course_id;
                $insert->scale_name = $gb_scale[$i]->scale_name;
                $insert->min_val = $gb_scale[$i]->min_val;
                $insert->max_val = $gb_scale[$i]->max_val;
                $insert->ordering = $gb_scale[$i]->ordering;
                $JLMS_DB->insertObject("#__lms_gradebook_scale", $insert, "id");
                $gb_scale[$i]->new_gbs_id = $JLMS_DB->insertid();
                $i++;
            }
            // ****************************************************************************************************
            // Get GradeBook Scale DATA and insert it into the 'lms_gradebook_scale' table
            $element =& $root->getElementByPath('gradebook_lpaths');
            $gb_lpaths = JLMS_parse_XML_elements($element->children(), array('learn_path_id'), array());
            $i = 0;
            while ($i < count($gb_lpaths)) {
                $insert = new stdClass();
                $insert->course_id = $course_id;
                $insert->learn_path_id = 0;
                foreach ($l_paths as $lp) {
                    if ($gb_lpaths[$i]->learn_path_id == $lp->id) {
                        $insert->learn_path_id = $lp->new_step_id;
                    }
                }
                if ($insert->learn_path_id) {
                    $JLMS_DB->insertObject("#__lms_gradebook_lpaths", $insert, "id");
                }
                $i++;
            }
        }
    }
    // delete temporary files
    deldir($extract_dir);
    return $archive;
}