예제 #1
0
 /**
  * Writes the current data to the database
  * @return	boolean	Query result
  */
 function write_to_db()
 {
     if ($this->debug > 0) {
         error_log('New LP - In learnpathItem::write_to_db()', 0);
     }
     $mode = $this->get_lesson_mode();
     $credit = $this->get_credit();
     $my_verified_status = $this->get_status(false);
     $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
     $sql_verified = 'SELECT status FROM ' . $item_view_table . ' WHERE lp_item_id="' . $this->db_id . '" AND lp_view_id="' . $this->view_id . '" AND view_count="' . $this->attempt_id . '" ;';
     $rs_verified = Database::query($sql_verified, __FILE__, __LINE__);
     $row_verified = Database::fetch_array($rs_verified);
     $my_case_completed = array('completed', 'passed', 'browsed', 'failed');
     //added by isaac flores
     if (in_array($sql_verified['status'], $my_case_completed)) {
         $save = false;
     } else {
         $save = true;
     }
     if ($save === false && $this->type == 'sco' || $this->type == 'sco' && ($credit == 'no-credit' or $mode == 'review' or $mode == 'browse')) {
         //this info shouldn't be saved as the credit or lesson mode info prevent it
         if ($this->debug > 1) {
             error_log('New LP - In learnpathItem::write_to_db() - credit(' . $credit . ') or lesson_mode(' . $mode . ') prevent recording!', 0);
         }
     } else {
         //check the row exists
         $inserted = false;
         // this a special case for multiple attempts and Dokeos exercises
         if ($this->type == 'quiz' && $this->get_prevent_reinit() == 0 && $this->get_status() == 'completed') {
             // we force the item to be restarted
             $this->restart();
             $sql = "INSERT INTO {$item_view_table} " . "(total_time, " . "start_time, " . "score, " . "status, " . "max_score, " . "lp_item_id, " . "lp_view_id, " . "view_count, " . "suspend_data, " . "lesson_location)" . "VALUES" . "(" . $this->get_total_time() . "," . "" . $this->current_start_time . "," . "" . $this->get_score() . "," . "'" . $this->get_status(false) . "'," . "'" . $this->get_max() . "'," . "" . $this->db_id . "," . "" . $this->view_id . "," . "" . $this->get_attempt_id() . "," . "'" . Database::escape_string($this->current_data) . "'," . "'" . $this->lesson_location . "')";
             if ($this->debug > 2) {
                 error_log('New LP - In learnpathItem::write_to_db() - Inserting into item_view forced: ' . $sql, 0);
             }
             $res = Database::query($sql, __FILE__, __LINE__);
             $this->db_item_view_id = Database::insert_id();
             $inserted = true;
         }
         $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
         $check = "SELECT * FROM {$item_view_table} " . "WHERE lp_item_id = " . $this->db_id . " " . "AND   lp_view_id = " . $this->view_id;
         if ($this->debug > 2) {
             error_log('New LP - In learnpathItem::write_to_db() - Querying item_view: ' . $check, 0);
         }
         $check_res = Database::query($check);
         //depending on what we want (really), we'll update or insert a new row
         //now save into DB
         $res = 0;
         if ($inserted == false && Database::num_rows($check_res) < 1) {
             /*$my_status = '';
             		if ($this->type!=TOOL_QUIZ) {
             				$my_status = $this->get_status(false);
             		}*/
             $sql = "INSERT INTO {$item_view_table} " . "(total_time, " . "start_time, " . "score, " . "status, " . "max_score, " . "lp_item_id, " . "lp_view_id, " . "view_count, " . "suspend_data, " . "lesson_location)" . "VALUES" . "(" . $this->get_total_time() . "," . "" . $this->current_start_time . "," . "" . $this->get_score() . "," . "'" . $this->get_status(false) . "'," . "'" . $this->get_max() . "'," . "" . $this->db_id . "," . "" . $this->view_id . "," . "" . $this->get_attempt_id() . "," . "'" . Database::escape_string($this->current_data) . "'," . "'" . $this->lesson_location . "')";
             if ($this->debug > 2) {
                 error_log('New LP - In learnpathItem::write_to_db() - Inserting into item_view: ' . $sql, 0);
             }
             $res = Database::query($sql, __FILE__, __LINE__);
             $this->db_item_view_id = Database::insert_id();
         } else {
             $sql = '';
             if ($this->type == 'hotpotatoes') {
                 //make an exception for HotPotatoes, don't update the score
                 //because it has been saved outside of this tool
                 $sql = "UPDATE {$item_view_table} " . "SET total_time = " . $this->get_total_time() . ", " . " start_time = " . $this->get_current_start_time() . ", " . " score = " . $this->get_score() . ", " . " status = '" . $this->get_status(false) . "'," . " max_score = '" . $this->get_max() . "'," . " suspend_data = '" . Database::escape_string($this->current_data) . "'," . " lesson_location = '" . $this->lesson_location . "' " . "WHERE lp_item_id = " . $this->db_id . " " . "AND lp_view_id = " . $this->view_id;
             } else {
                 //for all other content types...
                 if ($this->type == 'quiz') {
                     $my_status = ' ';
                     $total_time = ' ';
                     if (!empty($_REQUEST['exeId'])) {
                         $TBL_TRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
                         $safe_exe_id = Database::escape_string($_REQUEST['exeId']);
                         $sql = 'SELECT start_date,exe_date FROM ' . $TBL_TRACK_EXERCICES . ' WHERE exe_id = ' . (int) $safe_exe_id;
                         $res = Database::query($sql, __FILE__, __LINE__);
                         $row_dates = Database::fetch_array($res);
                         $time_start_date = convert_mysql_date($row_dates['start_date']);
                         $time_exe_date = convert_mysql_date($row_dates['exe_date']);
                         $mytime = (int) $time_exe_date - (int) $time_start_date;
                         $total_time = " total_time = " . $mytime . ", ";
                     }
                 } else {
                     $my_type_lp = learnpath::get_type_static($this->lp_id);
                     // this is a array containing values finished
                     $case_completed = array('completed', 'passed', 'browsed');
                     //is not multiple attempts
                     if ($this->get_prevent_reinit() == 1) {
                         // process of status verified into data base
                         $sql_verified = 'SELECT status FROM ' . $item_view_table . ' WHERE lp_item_id="' . $this->db_id . '" AND lp_view_id="' . $this->view_id . '" AND view_count="' . $this->attempt_id . '" ;';
                         $rs_verified = Database::query($sql_verified, __FILE__, __LINE__);
                         $row_verified = Database::fetch_array($rs_verified);
                         //get type lp: 1=lp dokeos and  2=scorm
                         // if not is completed or passed or browsed and learning path is scorm
                         if (!in_array($this->get_status(false), $case_completed) && $my_type_lp == 2) {
                             //&& $this->type!='dir'
                             $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                             $my_status = " status = '" . $this->get_status(false) . "' ,";
                         } else {
                             //verified into data base
                             if (!in_array($row_verified['status'], $case_completed) && $my_type_lp == 2) {
                                 //&& $this->type!='dir'
                                 $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                                 $my_status = " status = '" . $this->get_status(false) . "' ,";
                             } elseif (in_array($row_verified['status'], $case_completed) && $my_type_lp == 2 && $this->type != 'sco') {
                                 //&& $this->type!='dir'
                                 $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                                 $my_status = " status = '" . $this->get_status(false) . "' ,";
                             } else {
                                 //&& !in_array($row_verified['status'],$case_completed)
                                 //is lp dokeos or aicc
                                 if ($my_type_lp == 1 && $this->type != 'chapter') {
                                     $total_time = " total_time = total_time + " . $this->get_total_time() . ", ";
                                     $my_status = " status = '" . $this->get_status(false) . "' ,";
                                 } else {
                                     if ($my_type_lp == 3) {
                                         $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                                         $my_status = " status = '" . $this->get_status(false) . "' ,";
                                     }
                                 }
                             }
                         }
                     } else {
                         // is multiple attempts
                         if (in_array($this->get_status(false), $case_completed) && $my_type_lp == 2) {
                             //reset zero new attempt ?
                             $my_status = " status = '" . $this->get_status(false) . "' ,";
                         } elseif (!in_array($this->get_status(false), $case_completed) && $my_type_lp == 2) {
                             $total_time = " total_time = " . $this->get_total_time() . ", ";
                             $my_status = " status = '" . $this->get_status(false) . "' ,";
                         } else {
                             //is dokeos LP
                             $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                             $my_status = " status = '" . $this->get_status(false) . "' ,";
                         }
                         //code added by isaac flores
                         //this code line fix the problem of wrong status
                         if ($my_type_lp == 2) {
                             //verify current status in multiples attempts
                             $sql_status = 'SELECT status FROM ' . $item_view_table . ' WHERE lp_item_id="' . $this->db_id . '" AND lp_view_id="' . $this->view_id . '" AND view_count="' . $this->attempt_id . '" ';
                             $rs_status = Database::query($sql_status, __FILE__, __LINE__);
                             $current_status = Database::result($rs_status, 0, 'status');
                             if (in_array($current_status, $case_completed)) {
                                 $my_status = '';
                                 $total_time = '';
                             } else {
                                 $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                             }
                         }
                     }
                     /*if ($my_type_lp==1 && !in_array($row_verified['status'],$case_completed)) {
                     			$total_time =" total_time = total_time + ".$this->get_total_time().", ";
                     		}*/
                 }
                 // if $this->force_scorm_time is set to true, then we need to add the session time to the initial time
                 if ($this->force_scorm_time) {
                     $total_time = ' total_time = ' . ($this->time_before_session + $this->get_total_time()) . ' , ';
                     $this->force_scorm_time = false;
                 }
                 $my_view_count = 'view_count=' . ($this->get_attempt_id() + 1) . ',';
                 $sql = "UPDATE {$item_view_table} " . "SET " . $total_time . " start_time = " . $this->get_current_start_time() . ", " . " score = " . $this->get_score() . ", " . $my_status . $my_view_count . " max_score = '" . $this->get_max() . "'," . " suspend_data = '" . Database::escape_string($this->current_data) . "'," . " lesson_location = '" . $this->lesson_location . "' " . "WHERE lp_item_id = " . $this->db_id;
                 //." " .
                 //"AND lp_view_id = ".$this->view_id." "
                 $this->current_start_time = time();
             }
             if ($this->debug > 2) {
                 error_log('New LP - In learnpathItem::write_to_db() - Updating item_view: ' . $sql, 0);
             }
             $res = Database::query($sql, __FILE__, __LINE__);
         }
         //if(!$res)
         //{
         //	$this->error = 'Could not update item_view table...'.mysql_error();
         //}
         if (is_array($this->interactions) && count($this->interactions) > 0) {
             //save interactions
             $tbl = Database::get_course_table(TABLE_LP_ITEM_VIEW);
             $sql = "SELECT id FROM {$tbl} " . "WHERE lp_item_id = " . $this->db_id . " " . "AND   lp_view_id = " . $this->view_id . " " . "AND   view_count = " . $this->attempt_id;
             $res = Database::query($sql, __FILE__, __LINE__);
             if (Database::num_rows($res) > 0) {
                 $row = Database::fetch_array($res);
                 $lp_iv_id = $row[0];
                 if ($this->debug > 2) {
                     error_log('New LP - In learnpathItem::write_to_db() - Got item_view_id ' . $lp_iv_id . ', now checking interactions ', 0);
                 }
                 foreach ($this->interactions as $index => $interaction) {
                     $correct_resp = '';
                     if (is_array($interaction[4]) && !empty($interaction[4][0])) {
                         foreach ($interaction[4] as $resp) {
                             $correct_resp .= $resp . ',';
                         }
                         $correct_resp = substr($correct_resp, 0, strlen($correct_resp) - 1);
                     }
                     $iva_table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
                     $iva_sql = "SELECT id FROM {$iva_table} " . "WHERE lp_iv_id = {$lp_iv_id} " . "AND (order_id = {$index} " . "OR interaction_id = '" . Database::escape_string($interaction[0]) . "')";
                     $iva_res = Database::query($iva_sql, __FILE__, __LINE__);
                     //id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
                     if (Database::num_rows($iva_res) > 0) {
                         //update (or don't)
                         $iva_row = Database::fetch_array($iva_res);
                         $iva_id = $iva_row[0];
                         $ivau_sql = "UPDATE {$iva_table} " . "SET interaction_id = '" . Database::escape_string($interaction[0]) . "'," . "interaction_type = '" . Database::escape_string($interaction[1]) . "'," . "weighting = '" . Database::escape_string($interaction[3]) . "'," . "completion_time = '" . Database::escape_string($interaction[2]) . "'," . "correct_responses = '" . Database::escape_string($correct_resp) . "'," . "student_response = '" . Database::escape_string($interaction[5]) . "'," . "result = '" . Database::escape_string($interaction[6]) . "'," . "latency = '" . Database::escape_string($interaction[7]) . "'" . "WHERE id = {$iva_id}";
                         $ivau_res = Database::query($ivau_sql, __FILE__, __LINE__);
                     } else {
                         //insert new one
                         $ivai_sql = "INSERT INTO {$iva_table} " . "(order_id, lp_iv_id, interaction_id, interaction_type, " . "weighting, completion_time, correct_responses, " . "student_response, result, latency)" . "VALUES" . "(" . $index . "," . $lp_iv_id . ",'" . Database::escape_string($interaction[0]) . "','" . Database::escape_string($interaction[1]) . "'," . "'" . Database::escape_string($interaction[3]) . "','" . Database::escape_string($interaction[2]) . "','" . Database::escape_string($correct_resp) . "'," . "'" . Database::escape_string($interaction[5]) . "','" . Database::escape_string($interaction[6]) . "','" . Database::escape_string($interaction[7]) . "'" . ")";
                         $ivai_res = Database::query($ivai_sql, __FILE__, __LINE__);
                     }
                 }
             }
         }
     }
     if ($this->debug > 2) {
         error_log('New LP - End of learnpathItem::write_to_db()', 0);
     }
     return true;
 }
    /**
     * Writes the current data to the database
     * @return    boolean    Query result
     */
    public function write_to_db()
    {
        if (self::debug > 0) {
            error_log('learnpathItem::write_to_db()', 0);
        }
        // Check the session visibility.
        if (!api_is_allowed_to_session_edit()) {
            if (self::debug > 0) {
                error_log('return false api_is_allowed_to_session_edit');
            }
            return false;
        }
        $course_id = api_get_course_int_id();
        $mode = $this->get_lesson_mode();
        $credit = $this->get_credit();
        $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
        $sql_verified = 'SELECT status FROM ' . $item_view_table . '
		                 WHERE c_id = ' . $course_id . ' AND lp_item_id="' . $this->db_id . '" AND lp_view_id="' . $this->view_id . '" AND view_count="' . $this->get_attempt_id() . '" ;';
        $rs_verified = Database::query($sql_verified);
        $row_verified = Database::fetch_array($rs_verified);
        $my_case_completed = array('completed', 'passed', 'browsed', 'failed');
        $save = true;
        if (isset($row_verified) && isset($row_verified['status'])) {
            if (in_array($row_verified['status'], $my_case_completed)) {
                $save = false;
            }
        }
        if (($save === false && $this->type == 'sco' || $this->type == 'sco' && ($credit == 'no-credit' or $mode == 'review' or $mode == 'browse')) && ($this->seriousgame_mode != 1 && $this->type == 'sco')) {
            if (self::debug > 1) {
                error_log("This info shouldn't be saved as the credit or lesson mode info prevent it");
                error_log('learnpathItem::write_to_db() - credit(' . $credit . ') or lesson_mode(' . $mode . ') prevent recording!', 0);
            }
        } else {
            // Check the row exists.
            $inserted = false;
            // This a special case for multiple attempts and Chamilo exercises.
            if ($this->type == 'quiz' && $this->get_prevent_reinit() == 0 && $this->get_status() == 'completed') {
                // We force the item to be restarted.
                $this->restart();
                $sql = "INSERT INTO {$item_view_table} " . "(c_id, total_time, " . "start_time, " . "score, " . "status, " . "max_score, " . "lp_item_id, " . "lp_view_id, " . "view_count, " . "suspend_data, " . "lesson_location)" . "VALUES" . "({$course_id}, " . $this->get_total_time() . "," . "" . $this->current_start_time . "," . "" . $this->get_score() . "," . "'" . $this->get_status(false) . "'," . "'" . $this->get_max() . "'," . "" . $this->db_id . "," . "" . $this->view_id . "," . "" . $this->get_attempt_id() . "," . "'" . Database::escape_string($this->current_data) . "'," . "'" . $this->lesson_location . "')";
                if (self::debug > 2) {
                    error_log('learnpathItem::write_to_db() - Inserting into item_view forced: ' . $sql, 0);
                }
                $res = Database::query($sql);
                $this->db_item_view_id = Database::insert_id();
                $inserted = true;
            }
            $check_attempts = self::check_attempts($this->get_attempt_id());
            if (!$check_attempts) {
                return false;
            }
            $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
            $check = "SELECT * FROM {$item_view_table}\n                        WHERE\n                            c_id = {$course_id} AND\n                            lp_item_id = " . $this->db_id . " AND\n                            lp_view_id = " . $this->view_id . " AND\n                            view_count = " . $this->get_attempt_id();
            if (self::debug > 2) {
                error_log('learnpathItem::write_to_db() - Querying item_view: ' . $check, 0);
            }
            $check_res = Database::query($check);
            // Depending on what we want (really), we'll update or insert a new row
            // now save into DB.
            $res = 0;
            if (!$inserted && Database::num_rows($check_res) < 1) {
                $sql = "INSERT INTO {$item_view_table} " . "(c_id, total_time, " . "start_time, " . "score, " . "status, " . "max_score, " . "lp_item_id, " . "lp_view_id, " . "view_count, " . "suspend_data, " . "lesson_location)" . "VALUES" . "({$course_id}, " . $this->get_total_time() . "," . "" . $this->current_start_time . "," . "" . $this->get_score() . "," . "'" . $this->get_status(false) . "'," . "'" . $this->get_max() . "'," . "" . $this->db_id . "," . "" . $this->view_id . "," . "" . $this->get_attempt_id() . "," . "'" . Database::escape_string($this->current_data) . "'," . "'" . $this->lesson_location . "')";
                if (self::debug > 2) {
                    error_log('learnpathItem::write_to_db() - Inserting into item_view: ' . $sql, 0);
                }
                $res = Database::query($sql);
                $this->db_item_view_id = Database::insert_id();
            } else {
                $sql = '';
                if ($this->type == 'hotpotatoes') {
                    $sql = "UPDATE {$item_view_table} " . "SET total_time = " . $this->get_total_time() . ", " . " start_time = " . $this->get_current_start_time() . ", " . " score = " . $this->get_score() . ", " . " status = '" . $this->get_status(false) . "'," . " max_score = '" . $this->get_max() . "'," . " suspend_data = '" . Database::escape_string($this->current_data) . "'," . " lesson_location = '" . $this->lesson_location . "' " . "WHERE c_id = {$course_id} AND lp_item_id = " . $this->db_id . " " . "AND lp_view_id = " . $this->view_id . " " . "AND view_count = " . $this->get_attempt_id();
                } else {
                    // For all other content types...
                    if ($this->type == 'quiz') {
                        $my_status = ' ';
                        $total_time = ' ';
                        if (!empty($_REQUEST['exeId'])) {
                            $TBL_TRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
                            $safe_exe_id = Database::escape_string($_REQUEST['exeId']);
                            $sql = 'SELECT start_date,exe_date FROM ' . $TBL_TRACK_EXERCICES . ' WHERE exe_id = ' . (int) $safe_exe_id;
                            $res = Database::query($sql);
                            $row_dates = Database::fetch_array($res);
                            $time_start_date = api_convert_sql_date($row_dates['start_date']);
                            $time_exe_date = api_convert_sql_date($row_dates['exe_date']);
                            $mytime = (int) $time_exe_date - (int) $time_start_date;
                            $total_time = " total_time = " . $mytime . ", ";
                        }
                    } else {
                        $my_type_lp = learnpath::get_type_static($this->lp_id);
                        // This is a array containing values finished
                        $case_completed = array('completed', 'passed', 'browsed', 'failed');
                        //is not multiple attempts
                        if ($this->seriousgame_mode == 1 && $this->type == 'sco') {
                            $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                            $my_status = " status = '" . $this->get_status(false) . "' ,";
                        } elseif ($this->get_prevent_reinit() == 1) {
                            // Process of status verified into data base.
                            $sql_verified = 'SELECT status FROM ' . $item_view_table . '
                                             WHERE c_id = ' . $course_id . ' AND lp_item_id = "' . $this->db_id . '" AND lp_view_id="' . $this->view_id . '" AND view_count="' . $this->get_attempt_id() . '" ;';
                            $rs_verified = Database::query($sql_verified);
                            $row_verified = Database::fetch_array($rs_verified);
                            // Get type lp: 1=lp dokeos and  2=scorm.
                            // If not is completed or passed or browsed and learning path is scorm.
                            if (!in_array($this->get_status(false), $case_completed) && $my_type_lp == 2) {
                                //&& $this->type!='dir'
                                $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                                $my_status = " status = '" . $this->get_status(false) . "' ,";
                            } else {
                                // Verified into data base.
                                if (!in_array($row_verified['status'], $case_completed) && $my_type_lp == 2) {
                                    //&& $this->type!='dir'
                                    $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                                    $my_status = " status = '" . $this->get_status(false) . "' ,";
                                } elseif (in_array($row_verified['status'], $case_completed) && $my_type_lp == 2 && $this->type != 'sco') {
                                    //&& $this->type!='dir'
                                    $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                                    $my_status = " status = '" . $this->get_status(false) . "' ,";
                                } else {
                                    //&& !in_array($row_verified['status'], $case_completed)
                                    //is lp dokeos
                                    if ($my_type_lp == 1 && $this->type != 'chapter') {
                                        $total_time = " total_time = total_time + " . $this->get_total_time() . ", ";
                                        $my_status = " status = '" . $this->get_status(false) . "' ,";
                                    }
                                }
                            }
                        } else {
                            // Multiple attempts are allowed.
                            if (in_array($this->get_status(false), $case_completed) && $my_type_lp == 2) {
                                // Reset zero new attempt ?
                                $my_status = " status = '" . $this->get_status(false) . "' ,";
                            } elseif (!in_array($this->get_status(false), $case_completed) && $my_type_lp == 2) {
                                $total_time = " total_time = " . $this->get_total_time() . ", ";
                                $my_status = " status = '" . $this->get_status(false) . "' ,";
                            } else {
                                // It is dokeos LP.
                                $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                                $my_status = " status = '" . $this->get_status(false) . "' ,";
                            }
                            // Code added by Isaac Flores.
                            // This code line fixes the problem of wrong status.
                            if ($my_type_lp == 2) {
                                // Verify current status in multiples attempts.
                                $sql_status = 'SELECT status FROM ' . $item_view_table . '
                                               WHERE c_id = ' . $course_id . ' AND lp_item_id="' . $this->db_id . '" AND lp_view_id="' . $this->view_id . '" AND view_count="' . $this->get_attempt_id() . '" ';
                                $rs_status = Database::query($sql_status);
                                $current_status = Database::result($rs_status, 0, 'status');
                                if (in_array($current_status, $case_completed)) {
                                    $my_status = '';
                                    $total_time = '';
                                } else {
                                    $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                                }
                            }
                        }
                    }
                    if ($this->type == 'sco') {
                        //IF scorm scorm_update_time has already updated total_tim in db
                        $sql = "UPDATE {$item_view_table} " . " SET " . " score = " . $this->get_score() . ", " . $my_status . " max_score = '" . $this->get_max() . "'," . " suspend_data = '" . Database::escape_string($this->current_data) . "'," . " lesson_location = '" . $this->lesson_location . "' " . "WHERE c_id = {$course_id} AND lp_item_id = " . $this->db_id . " " . "AND lp_view_id = " . $this->view_id . " " . "AND view_count = " . $this->get_attempt_id();
                    } else {
                        $sql = "UPDATE {$item_view_table} " . "SET " . $total_time . " start_time = " . $this->get_current_start_time() . ", " . " score = " . $this->get_score() . ", " . $my_status . " max_score = '" . $this->get_max() . "'," . " suspend_data = '" . Database::escape_string($this->current_data) . "'," . " lesson_location = '" . $this->lesson_location . "' " . "WHERE c_id = {$course_id} AND lp_item_id = " . $this->db_id . " " . "AND lp_view_id = " . $this->view_id . " " . "AND view_count = " . $this->get_attempt_id();
                    }
                    $this->current_start_time = time();
                }
                if (self::debug > 2) {
                    error_log('learnpathItem::write_to_db() - Updating item_view: ' . $sql, 0);
                }
                $res = Database::query($sql);
            }
            if (is_array($this->interactions) && count($this->interactions) > 0) {
                // Save interactions.
                $tbl = Database::get_course_table(TABLE_LP_ITEM_VIEW);
                $sql = "SELECT id FROM {$tbl} " . "WHERE c_id = {$course_id} AND lp_item_id = " . $this->db_id . " " . "AND   lp_view_id = " . $this->view_id . " " . "AND   view_count = " . $this->get_attempt_id();
                $res = Database::query($sql);
                if (Database::num_rows($res) > 0) {
                    $row = Database::fetch_array($res);
                    $lp_iv_id = $row[0];
                    if (self::debug > 2) {
                        error_log('learnpathItem::write_to_db() - Got item_view_id ' . $lp_iv_id . ', now checking interactions ', 0);
                    }
                    foreach ($this->interactions as $index => $interaction) {
                        $correct_resp = '';
                        if (is_array($interaction[4]) && !empty($interaction[4][0])) {
                            foreach ($interaction[4] as $resp) {
                                $correct_resp .= $resp . ',';
                            }
                            $correct_resp = substr($correct_resp, 0, strlen($correct_resp) - 1);
                        }
                        $iva_table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
                        $iva_sql = "SELECT id FROM {$iva_table} " . "WHERE c_id = {$course_id} AND lp_iv_id = {$lp_iv_id} " . "AND (order_id = {$index} " . "OR interaction_id = '" . Database::escape_string($interaction[0]) . "')";
                        $iva_res = Database::query($iva_sql);
                        // id(0), type(1), time(2), weighting(3), correct_responses(4), student_response(5), result(6), latency(7)
                        if (Database::num_rows($iva_res) > 0) {
                            // Update (or don't).
                            $iva_row = Database::fetch_array($iva_res);
                            $iva_id = $iva_row[0];
                            $ivau_sql = "UPDATE {$iva_table} " . "SET interaction_id = '" . Database::escape_string($interaction[0]) . "'," . "interaction_type = '" . Database::escape_string($interaction[1]) . "'," . "weighting = '" . Database::escape_string($interaction[3]) . "'," . "completion_time = '" . Database::escape_string($interaction[2]) . "'," . "correct_responses = '" . Database::escape_string($correct_resp) . "'," . "student_response = '" . Database::escape_string($interaction[5]) . "'," . "result = '" . Database::escape_string($interaction[6]) . "'," . "latency = '" . Database::escape_string($interaction[7]) . "'" . "WHERE c_id = {$course_id} AND id = {$iva_id}";
                            Database::query($ivau_sql);
                        } else {
                            // Insert new one.
                            $ivai_sql = "INSERT INTO {$iva_table} (c_id, order_id, lp_iv_id, interaction_id, interaction_type, " . "weighting, completion_time, correct_responses, " . "student_response, result, latency)" . "VALUES" . "({$course_id}, " . $index . "," . $lp_iv_id . ",'" . Database::escape_string($interaction[0]) . "','" . Database::escape_string($interaction[1]) . "'," . "'" . Database::escape_string($interaction[3]) . "','" . Database::escape_string($interaction[2]) . "','" . Database::escape_string($correct_resp) . "'," . "'" . Database::escape_string($interaction[5]) . "','" . Database::escape_string($interaction[6]) . "','" . Database::escape_string($interaction[7]) . "'" . ")";
                            Database::query($ivai_sql);
                        }
                    }
                }
            }
        }
        if (self::debug > 2) {
            error_log('End of learnpathItem::write_to_db()', 0);
        }
        return true;
    }
 /**
  * Writes the current data to the database
  * @return    boolean    Query result
  */
 public function write_to_db()
 {
     if (self::debug > 0) {
         error_log('learnpathItem::write_to_db()', 0);
     }
     // Check the session visibility.
     if (!api_is_allowed_to_session_edit()) {
         if (self::debug > 0) {
             error_log('return false api_is_allowed_to_session_edit');
         }
         return false;
     }
     $course_id = api_get_course_int_id();
     $mode = $this->get_lesson_mode();
     $credit = $this->get_credit();
     $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
     $sql = 'SELECT status FROM ' . $item_view_table . '
             WHERE
                 c_id = ' . $course_id . ' AND
                 lp_item_id="' . $this->db_id . '" AND
                 lp_view_id="' . $this->view_id . '" AND
                 view_count="' . $this->get_attempt_id() . '" ';
     $rs_verified = Database::query($sql);
     $row_verified = Database::fetch_array($rs_verified);
     $my_case_completed = array('completed', 'passed', 'browsed', 'failed');
     $save = true;
     if (isset($row_verified) && isset($row_verified['status'])) {
         if (in_array($row_verified['status'], $my_case_completed)) {
             $save = false;
         }
     }
     if (($save === false && $this->type == 'sco' || $this->type == 'sco' && ($credit == 'no-credit' || $mode == 'review' || $mode == 'browse')) && ($this->seriousgame_mode != 1 && $this->type == 'sco')) {
         if (self::debug > 1) {
             error_log("This info shouldn't be saved as the credit or lesson mode info prevent it");
             error_log('learnpathItem::write_to_db() - credit(' . $credit . ') or' . ' lesson_mode(' . $mode . ') prevent recording!', 0);
         }
     } else {
         // Check the row exists.
         $inserted = false;
         // This a special case for multiple attempts and Chamilo exercises.
         if ($this->type == 'quiz' && $this->get_prevent_reinit() == 0 && $this->get_status() == 'completed') {
             // We force the item to be restarted.
             $this->restart();
             $params = array("c_id" => $course_id, "total_time" => $this->get_total_time(), "start_time" => $this->current_start_time, "score" => $this->get_score(), "status" => $this->get_status(false), "max_score" => $this->get_max(), "lp_item_id" => $this->db_id, "lp_view_id" => $this->view_id, "view_count" => $this->get_attempt_id(), "suspend_data" => $this->current_data, "lesson_location" => $this->lesson_location);
             if (self::debug > 2) {
                 error_log('learnpathItem::write_to_db() - Inserting into item_view forced: ' . print_r($params, 1), 0);
             }
             $this->db_item_view_id = Database::insert($item_view_table, $params);
             if ($this->db_item_view_id) {
                 $sql = "UPDATE {$item_view_table} SET id = iid\n                            WHERE iid = " . $this->db_item_view_id;
                 Database::query($sql);
                 $inserted = true;
             }
         }
         $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
         $sql = "SELECT * FROM {$item_view_table}\n                    WHERE\n                        c_id = {$course_id} AND\n                        lp_item_id = " . $this->db_id . " AND\n                        lp_view_id = " . $this->view_id . " AND\n                        view_count = " . intval($this->get_attempt_id());
         if (self::debug > 2) {
             error_log('learnpathItem::write_to_db() - Querying item_view: ' . $sql, 0);
         }
         $check_res = Database::query($sql);
         // Depending on what we want (really), we'll update or insert a new row
         // now save into DB.
         if (!$inserted && Database::num_rows($check_res) < 1) {
             $params = array("c_id" => $course_id, "total_time" => $this->get_total_time(), "start_time" => $this->current_start_time, "score" => $this->get_score(), "status" => $this->get_status(false), "max_score" => $this->get_max(), "lp_item_id" => $this->db_id, "lp_view_id" => $this->view_id, "view_count" => $this->get_attempt_id(), "suspend_data" => $this->current_data, "lesson_location" => $this->lesson_location);
             if (self::debug > 2) {
                 error_log('learnpathItem::write_to_db() - Inserting into item_view forced: ' . print_r($params, 1), 0);
             }
             $this->db_item_view_id = Database::insert($item_view_table, $params);
             if ($this->db_item_view_id) {
                 $sql = "UPDATE {$item_view_table} SET id = iid\n                            WHERE iid = " . $this->db_item_view_id;
                 Database::query($sql);
             }
         } else {
             if ($this->type == 'hotpotatoes') {
                 $params = array('total_time' => $this->get_total_time(), 'start_time' => $this->get_current_start_time(), 'score' => $this->get_score(), 'status' => $this->get_status(false), 'max_score' => $this->get_max(), 'suspend_data' => $this->current_data, 'lesson_location' => $this->lesson_location);
                 $where = array('c_id = ? AND lp_item_id = ? AND lp_view_id = ? AND view_count = ?' => array($course_id, $this->db_id, $this->view_id, $this->get_attempt_id()));
                 Database::update($item_view_table, $params, $where);
             } else {
                 // For all other content types...
                 $my_status = ' ';
                 $total_time = ' ';
                 if ($this->type == 'quiz') {
                     if (!empty($_REQUEST['exeId'])) {
                         $TBL_TRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
                         $safe_exe_id = intval($_REQUEST['exeId']);
                         $sql = "SELECT start_date,exe_date\n                                    FROM {$TBL_TRACK_EXERCICES}\n                                    WHERE exe_id = {$safe_exe_id}";
                         $res = Database::query($sql);
                         $row_dates = Database::fetch_array($res);
                         $time_start_date = convert_sql_date($row_dates['start_date']);
                         $time_exe_date = convert_sql_date($row_dates['exe_date']);
                         $mytime = (int) $time_exe_date - (int) $time_start_date;
                         $total_time = " total_time = " . $mytime . ", ";
                     }
                 } else {
                     $my_type_lp = learnpath::get_type_static($this->lp_id);
                     // This is a array containing values finished
                     $case_completed = array('completed', 'passed', 'browsed', 'failed');
                     // Is not multiple attempts
                     if ($this->seriousgame_mode == 1 && $this->type == 'sco') {
                         $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                         $my_status = " status = '" . $this->get_status(false) . "' ,";
                     } elseif ($this->get_prevent_reinit() == 1) {
                         // Process of status verified into data base.
                         $sql = 'SELECT status FROM ' . $item_view_table . '
                                 WHERE
                                     c_id = ' . $course_id . ' AND
                                     lp_item_id="' . $this->db_id . '" AND
                                     lp_view_id="' . $this->view_id . '" AND
                                     view_count="' . $this->get_attempt_id() . '"
                                 ';
                         $rs_verified = Database::query($sql);
                         $row_verified = Database::fetch_array($rs_verified);
                         // Get type lp: 1=lp dokeos and  2=scorm.
                         // If not is completed or passed or browsed and learning path is scorm.
                         if (!in_array($this->get_status(false), $case_completed) && $my_type_lp == 2) {
                             $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                             $my_status = " status = '" . $this->get_status(false) . "' ,";
                         } else {
                             // Verified into database.
                             if (!in_array($row_verified['status'], $case_completed) && $my_type_lp == 2) {
                                 $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                                 $my_status = " status = '" . $this->get_status(false) . "' ,";
                             } elseif (in_array($row_verified['status'], $case_completed) && $my_type_lp == 2 && $this->type != 'sco') {
                                 $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                                 $my_status = " status = '" . $this->get_status(false) . "' ,";
                             } else {
                                 if ($my_type_lp == 3 && $this->type == 'au' || $my_type_lp == 1 && $this->type != 'chapter') {
                                     // Is AICC or Chamilo LP
                                     $total_time = " total_time = total_time + " . $this->get_total_time() . ", ";
                                     $my_status = " status = '" . $this->get_status(false) . "' ,";
                                 }
                             }
                         }
                     } else {
                         // Multiple attempts are allowed.
                         if (in_array($this->get_status(false), $case_completed) && $my_type_lp == 2) {
                             // Reset zero new attempt ?
                             $my_status = " status = '" . $this->get_status(false) . "' ,";
                         } elseif (!in_array($this->get_status(false), $case_completed) && $my_type_lp == 2) {
                             $total_time = " total_time = " . $this->get_total_time() . ", ";
                             $my_status = " status = '" . $this->get_status(false) . "' ,";
                         } else {
                             // It is chamilo LP.
                             $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                             $my_status = " status = '" . $this->get_status(false) . "' ,";
                         }
                         // This code line fixes the problem of wrong status.
                         if ($my_type_lp == 2) {
                             // Verify current status in multiples attempts.
                             $sql = 'SELECT status FROM ' . $item_view_table . '
                                     WHERE
                                         c_id = ' . $course_id . ' AND
                                         lp_item_id="' . $this->db_id . '" AND
                                         lp_view_id="' . $this->view_id . '" AND
                                         view_count="' . $this->get_attempt_id() . '" ';
                             $rs_status = Database::query($sql);
                             $current_status = Database::result($rs_status, 0, 'status');
                             if (in_array($current_status, $case_completed)) {
                                 $my_status = '';
                                 $total_time = '';
                             } else {
                                 $total_time = " total_time = total_time +" . $this->get_total_time() . ", ";
                             }
                         }
                     }
                 }
                 if ($this->type == 'sco') {
                     //IF scorm scorm_update_time has already updated total_time in db
                     //" . //start_time = ".$this->get_current_start_time().", " . //scorm_init_time does it
                     ////" max_time_allowed = '".$this->get_max_time_allowed()."'," .
                     $sql = "UPDATE {$item_view_table} SET\n                                    score = " . $this->get_score() . ",\n                                    {$my_status}\n                                    max_score = '" . $this->get_max() . "',\n                                    suspend_data = '" . Database::escape_string($this->current_data) . "',\n                                    lesson_location = '" . $this->lesson_location . "'\n                                WHERE\n                                    c_id = {$course_id} AND\n                                    lp_item_id = " . $this->db_id . " AND\n                                    lp_view_id = " . $this->view_id . "  AND\n                                    view_count = " . $this->get_attempt_id();
                 } else {
                     //" max_time_allowed = '".$this->get_max_time_allowed()."'," .
                     $sql = "UPDATE {$item_view_table} SET\n                                    {$total_time}\n                                    start_time = " . $this->get_current_start_time() . ",\n                                    score = " . $this->get_score() . ",\n                                    {$my_status}\n                                    max_score = '" . $this->get_max() . "',\n                                    suspend_data = '" . Database::escape_string($this->current_data) . "',\n                                    lesson_location = '" . $this->lesson_location . "'\n                                WHERE\n                                    c_id = {$course_id} AND\n                                    lp_item_id = " . $this->db_id . " AND\n                                    lp_view_id = " . $this->view_id . " AND\n                                    view_count = " . $this->get_attempt_id();
                 }
                 $this->current_start_time = time();
             }
             if (self::debug > 2) {
                 error_log('learnpathItem::write_to_db() - Updating item_view: ' . $sql, 0);
             }
             Database::query($sql);
         }
         if (is_array($this->interactions) && count($this->interactions) > 0) {
             // Save interactions.
             $tbl = Database::get_course_table(TABLE_LP_ITEM_VIEW);
             $sql = "SELECT id FROM {$tbl}\n                        WHERE\n                            c_id = {$course_id} AND\n                            lp_item_id = " . $this->db_id . " AND\n                            lp_view_id = " . $this->view_id . " AND\n                            view_count = " . $this->get_attempt_id();
             $res = Database::query($sql);
             if (Database::num_rows($res) > 0) {
                 $row = Database::fetch_array($res);
                 $lp_iv_id = $row[0];
                 if (self::debug > 2) {
                     error_log('learnpathItem::write_to_db() - Got item_view_id ' . $lp_iv_id . ', now checking interactions ', 0);
                 }
                 foreach ($this->interactions as $index => $interaction) {
                     $correct_resp = '';
                     if (is_array($interaction[4]) && !empty($interaction[4][0])) {
                         foreach ($interaction[4] as $resp) {
                             $correct_resp .= $resp . ',';
                         }
                         $correct_resp = substr($correct_resp, 0, strlen($correct_resp) - 1);
                     }
                     $iva_table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
                     //also check for the interaction ID as it must be unique for this SCO view
                     $iva_sql = "SELECT id FROM {$iva_table}\n                                    WHERE\n                                        c_id = {$course_id} AND\n                                        lp_iv_id = {$lp_iv_id} AND\n                                        (\n                                            order_id = {$index} OR\n                                            interaction_id = '" . Database::escape_string($interaction[0]) . "'\n                                        )\n                                    ";
                     $iva_res = Database::query($iva_sql);
                     // id(0), type(1), time(2), weighting(3), correct_responses(4), student_response(5), result(6), latency(7)
                     if (Database::num_rows($iva_res) > 0) {
                         // Update (or don't).
                         $iva_row = Database::fetch_array($iva_res);
                         $iva_id = $iva_row[0];
                         // Insert new one.
                         $params = array('interaction_id' => $interaction[0], 'interaction_type' => $interaction[1], 'weighting' => $interaction[3], 'completion_time' => $interaction[2], 'correct_responses' => $correct_resp, 'student_response' => $interaction[5], 'result' => $interaction[6], 'latency' => $interaction[7]);
                         Database::update($iva_table, $params, array('c_id = ? AND id = ?' => array($course_id, $iva_id)));
                     } else {
                         // Insert new one.
                         $params = array('c_id' => $course_id, 'order_id' => $index, 'lp_iv_id' => $lp_iv_id, 'interaction_id' => $interaction[0], 'interaction_type' => $interaction[1], 'weighting' => $interaction[3], 'completion_time' => $interaction[2], 'correct_responses' => $correct_resp, 'student_response' => $interaction[5], 'result' => $interaction[6], 'latency' => $interaction[7]);
                         $insertId = Database::insert($iva_table, $params);
                         if ($insertId) {
                             $sql = "UPDATE {$iva_table} SET id = iid\n                                        WHERE iid = {$insertId}";
                             Database::query($sql);
                         }
                     }
                 }
             }
         }
     }
     if (self::debug > 2) {
         error_log('End of learnpathItem::write_to_db()', 0);
     }
     return true;
 }