public static function repopulate_table()
 {
     $sql = "DELETE FROM `TestTemplate`";
     mysql_query($sql);
     $sql = "SELECT * FROM `TestSection`";
     $z = mysql_query($sql);
     while ($r = mysql_fetch_array($z)) {
         set_time_limit(0);
         if ($r['TestSectionType_id'] == DS_TestSectionType::LOAD_HTML_TEMPLATE) {
             $ts = TestSection::from_mysql_id($r['id']);
             $vals = $ts->get_values();
             $template = Template::from_mysql_id($vals[0]);
             if ($template != null) {
                 $html = Template::output_html($template->HTML, $vals, $template->get_outputs(), $template->get_inserts());
                 $test_template = new TestTemplate();
                 $test_template->Test_id = $r['Test_id'];
                 $test_template->TestSection_id = $r['id'];
                 $test_template->Template_id = $vals[0];
                 $test_template->HTML = $html;
                 $test_template->effect_show = $template->effect_show;
                 $test_template->effect_hide = $template->effect_hide;
                 $test_template->effect_show_options = $template->effect_show_options;
                 $test_template->effect_hide_options = $template->effect_hide_options;
                 $test_template->mysql_save();
             }
         }
     }
 }
 public function get_RCode()
 {
     $code = "";
     $next = $this->get_next_TestSection();
     $next_counter = $next != null ? $next->counter : 0;
     $end_of_loop = $this->is_end_of_loop();
     $loop = null;
     $loop_vals = null;
     if ($end_of_loop) {
         $loop = $this->get_section_loop();
         $loop_vals = $loop->get_values();
         $next_counter = $loop->counter;
     }
     $vals = $this->get_values();
     switch ($this->TestSectionType_id) {
         case DS_TestSectionType::START:
             $test = Test::from_mysql_id($this->Test_id);
             if ($test == null) {
                 return sprintf("stop('Invalid test id: %s in section #%s')", $this->Test_id, $this->counter);
             }
             $code = sprintf("\n                            print('Starting test with id: %s')\n                            CONCERTO_TEST_ID <<- %s\n                            return(%d)\n                            ", $this->Test_id, $this->Test_id, $next_counter);
             break;
         case DS_TestSectionType::END:
             $code = sprintf("\n                    update.session.status(%d)    \n                    update.session.counter(%d)\n                    return(%d)\n                    ", TestSession::TEST_SESSION_STATUS_COMPLETED, $next_counter, $next_counter);
             break;
         case DS_TestSectionType::CUSTOM:
             $cs = CustomSection::from_mysql_id($vals[0]);
             if ($cs == null) {
                 return sprintf("stop('Invalid custom section #%s')", $this->counter);
             }
             $parameters = $cs->get_parameter_CustomSectionVariables();
             $returns = $cs->get_return_CustomSectionVariables();
             $code = "";
             foreach ($parameters as $param) {
                 $val = $param->name;
                 for ($j = 0; $j < $vals[1] * 2; $j = $j + 2) {
                     if ($vals[3 + $j] == $param->name && isset($vals[3 + $j + 1]) && $vals[3 + $j + 1] != "") {
                         $val = $vals[3 + $j + 1];
                         break;
                     }
                 }
                 $code .= sprintf("\n                            %s <- %s\n                            ", $param->name, $val);
             }
             $code .= $cs->code;
             foreach ($returns as $ret) {
                 $val = $ret->name;
                 for ($j = 0; $j < $vals[2] * 2; $j = $j + 2) {
                     if ($vals[3 + $vals[1] * 2 + $j] == $ret->name && isset($vals[3 + $vals[1] * 2 + $j]) && $vals[3 + $vals[1] * 2 + $j] != "") {
                         $val = $vals[3 + $vals[1] * 2 + $j + 1];
                         break;
                     }
                 }
                 $code .= sprintf("\n                            %s <<- %s\n                            ", $val, $ret->name);
             }
             $code .= sprintf("\n                        return(%d)\n                        ", $this->end == 0 ? $next_counter : -2);
             break;
         case DS_TestSectionType::R_CODE:
             $code = sprintf("\n                        %s\n                        return(%d)\n                        ", $vals[0], $this->end == 0 ? $next_counter : -2);
             break;
         case DS_TestSectionType::LOWER_LEVEL_R_CODE:
             $code = sprintf("\n                        %s\n                        ", $vals[0]);
             break;
         case DS_TestSectionType::QTI_INITIALIZATION:
             $ai = QTIAssessmentItem::from_mysql_id($vals[0]);
             if ($ai == null) {
                 return sprintf("stop('Invalid QTI assessment item id: %s in section #%s')", $vals[0], $this->counter);
             }
             $map = QTIAssessmentItem::get_mapped_variables($this->id);
             $result = $ai->validate($map, null, $this->id);
             if (json_decode($result)->result != 0) {
                 return sprintf("stop('Validation failed on QTI assessment item id: %s in section #%s')", $vals[0], $this->counter);
             }
             $qtir = $ai->get_QTI_ini_R_code($map, $this->id);
             $code = sprintf("\n                        %s\n                        return(%d)\n                        ", $qtir, $this->end == 0 ? $next_counter : -2);
             break;
         case DS_TestSectionType::QTI_RESPONSE_PROCESSING:
             $ts = TestSection::from_property(array("counter" => $vals[0], "Test_id" => $this->Test_id), false);
             if ($ts == null) {
                 return sprintf("stop('Invalid test section id: %s in section #%s')", $vals[0], $this->counter);
             }
             $tsvals = $ts->get_values();
             $ai = QTIAssessmentItem::from_mysql_id($tsvals[0]);
             if ($ai == null) {
                 return sprintf("stop('Invalid QTI assessment item id: %s in section #%s')", $tsvals[0], $this->counter);
             }
             $map = QTIAssessmentItem::get_mapped_variables($ts->id);
             $result = $ai->validate($map, null, $ts->id);
             if (json_decode($result)->result != 0) {
                 return sprintf("stop('Validation failed on QTI assessment item id: %s in section #%s')", $tsvals[0], $this->counter);
             }
             $qtir = $ai->get_response_processing_R_code();
             $code = sprintf("\n                        %s\n                        return(%d)\n                        ", $qtir, $this->end == 0 ? $next_counter : -2);
             break;
         case DS_TestSectionType::LOAD_HTML_TEMPLATE:
             $template_id = $vals[0];
             $template = Template::from_mysql_id($template_id);
             if ($template == null) {
                 return sprintf("stop('Invalid template id: %s in section #%s')", $template_id, $this->counter);
             }
             $code = sprintf("\n                        update.session.template_id(%d)\n                        if(!exists('TIME_LIMIT')) TIME_LIMIT <<- 0\n                        update.session.time_limit(TIME_LIMIT)\n                        update.session.status(%d)\n                        update.session.counter(%d)\n                        update.session.template_testsection_id(%d)\n                        update.session.HTML(%d,%d,%d)\n                        update.session.effects('%s','%s','%s','%s')\n                        return(%d)\n                        ", $template_id, TestSession::TEST_SESSION_STATUS_TEMPLATE, $next_counter, $this->id, $this->Test_id, $this->id, $template_id, addcslashes($template->effect_show, "'"), addcslashes($template->effect_hide, "'"), addcslashes($template->effect_show_options, "'"), addcslashes($template->effect_hide_options, "'"), $this->end == 0 ? -1 : -2);
             break;
         case DS_TestSectionType::GO_TO:
             $code = "";
             foreach ($this->get_parent_loops_counters() as $loop) {
                 $target = TestSection::from_property(array("Test_id" => $this->Test_id, "counter" => $vals[0]), false);
                 if ($target != null) {
                     $target_loops = $target->get_parent_loops_counters();
                     if (!in_array($loop, $target_loops)) {
                         $code .= sprintf("\n                                    %s <<- FALSE\n                                    ", TestSection::build_for_initialization_variable($this->Test_id, $loop));
                     }
                 }
             }
             $code .= sprintf("\n                        return(%d)\n                        ", $vals[0] == 0 ? $next_counter : $vals[0]);
             break;
         case DS_TestSectionType::IF_STATEMENT:
             $contents = TestSection::from_property(array("Test_id" => $this->Test_id, "parent_counter" => $this->counter));
             $is_empty = count($contents) == 0;
             $code = "";
             $next_not_child = $this->get_next_not_child_TestSection();
             $next_not_child_counter = $next_not_child->counter;
             if ($end_of_loop) {
                 $next_not_child_counter = $next_counter;
             } else {
                 $is_end_of_loop = $this->is_end_of_loop(false);
                 if ($is_end_of_loop) {
                     $parent_loop = $this->get_section_loop();
                     if ($parent_loop != null) {
                         $next_not_child_counter = $parent_loop->counter;
                     }
                 }
             }
             if ($is_empty) {
                 $code .= sprintf("\n                            return(%d)\n                            ", $next_not_child_counter);
                 break;
             }
             $additional_conds = "";
             $i = 3;
             while (isset($vals[$i])) {
                 $additional_conds .= sprintf("%s %s %s %s", $vals[$i], $vals[$i + 1], $vals[$i + 2], $vals[$i + 3]);
                 $i += 4;
             }
             $code .= sprintf("\n                if(%s %s %s %s) {\n                    return(%d)\n                    }\n                    else {\n                    return(%d)\n                    }\n                    ", $vals[0], $vals[1], $vals[2], $additional_conds, $next->counter, $next_not_child_counter);
             break;
         case DS_TestSectionType::TEST:
             $test = Test::from_mysql_id($vals[0]);
             if ($test == null) {
                 return sprintf("stop('Invalid test id: %s in section #%s')", $vals[0], $this->counter);
             } else {
                 $code = sprintf("\n                        CONCERTO_TEST_ID <<- %s\n                        ", $test->id);
                 $parameters = $test->get_parameter_TestVariables();
                 $params_code = "";
                 $returns = $test->get_return_TestVariables();
                 $returns_code = "";
                 foreach ($parameters as $param) {
                     $val = $param->name;
                     for ($j = 0; $j < $vals[1] * 2; $j = $j + 2) {
                         if ($vals[3 + $j] == $param->name && isset($vals[3 + $j + 1]) && $vals[3 + $j + 1] != "") {
                             $val = $vals[3 + $j + 1];
                             break;
                         }
                     }
                     $params_code .= sprintf("\n                            %s <- %s\n                            ", $param->name, $val);
                 }
                 foreach ($returns as $ret) {
                     $val = $ret->name;
                     for ($j = 0; $j < $vals[2] * 2; $j = $j + 2) {
                         if ($vals[3 + $vals[1] * 2 + $j] == $ret->name && isset($vals[3 + $vals[1] * 2 + $j]) && $vals[3 + $vals[1] * 2 + $j] != "") {
                             $val = $vals[3 + $vals[1] * 2 + $j + 1];
                             break;
                         }
                     }
                     $returns_code .= sprintf("\n                            %s <<- %s\n                            ", $val, $ret->name);
                 }
                 $code .= sprintf("\n                            %s\n                            ", $params_code);
                 $sections = TestSection::from_property(array("Test_id" => $test->id));
                 foreach ($sections as $section) {
                     if ($section->TestSectionType_id != DS_TestSectionType::END) {
                         $code .= $section->get_RFunction();
                     } else {
                         $end_code = sprintf("\n                                    CONCERTO_TEST_ID <<- %s\n                                    %s\n                                    return(%d)\n                                    ", $this->Test_id, $returns_code, $next_counter);
                         $code .= sprintf("\n                                    %s <<- function(){\n                                    print('Start of section with index: %s')\n                                    %s\n                                    }\n                                    ", 'CONCERTO_Test' . $vals[0] . 'Section2', 2, $end_code);
                     }
                 }
                 $code .= "\n                            return(1)\n                            ";
             }
             break;
         case DS_TestSectionType::LOOP:
             $contents = TestSection::from_property(array("Test_id" => $this->Test_id, "parent_counter" => $this->counter));
             $is_empty = count($contents) == 0;
             $next_not_child = $this->get_next_not_child_TestSection();
             $next_not_child_counter = $next_not_child->counter;
             if ($end_of_loop) {
                 $next_not_child_counter = $next_counter;
             } else {
                 $is_end_of_loop = $this->is_end_of_loop(false);
                 if ($is_end_of_loop) {
                     $parent_loop = $this->get_section_loop();
                     if ($parent_loop != null) {
                         $next_not_child_counter = $parent_loop->counter;
                     }
                 }
             }
             $code = "";
             if ($is_empty) {
                 $code .= sprintf("\n                            return(%d)\n                            ", $next_not_child_counter);
                 break;
             }
             if ($vals[0] == 1) {
                 $code = sprintf("\n                if(%s %s %s) {\n                    return(%d)\n                    }\n                    else {\n                    return(%d)\n                    }\n                    ", $vals[1], $vals[2], $vals[3], $next->counter, $next_not_child_counter);
             } else {
                 $code = sprintf("\n                if(exists('%s') && %s) {\n                    %s <<- %s + as.numeric(%s)\n                }\n                else {\n                    %s <<- TRUE\n                    %s <<- as.numeric(%s)\n                }\n                if(%s %s %s) {\n                    return(%d)\n                    }\n                    else {\n                    %s <<- FALSE\n                    return(%d)\n                    }\n                    ", $this->get_for_initialization_variable(), $this->get_for_initialization_variable(), $vals[1], $vals[1], $vals[5], $this->get_for_initialization_variable(), $vals[1], $vals[4], $vals[1], $vals[2], $vals[3], $next->counter, $this->get_for_initialization_variable(), $next_not_child_counter);
             }
             break;
         case DS_TestSectionType::FOR_LOOP:
             $contents = TestSection::from_property(array("Test_id" => $this->Test_id, "parent_counter" => $this->counter));
             $is_empty = count($contents) == 0;
             $next_not_child = $this->get_next_not_child_TestSection();
             $next_not_child_counter = $next_not_child->counter;
             if ($end_of_loop) {
                 $next_not_child_counter = $next_counter;
             } else {
                 $is_end_of_loop = $this->is_end_of_loop(false);
                 if ($is_end_of_loop) {
                     $parent_loop = $this->get_section_loop();
                     if ($parent_loop != null) {
                         $next_not_child_counter = $parent_loop->counter;
                     }
                 }
             }
             $code = "";
             if ($is_empty) {
                 $code .= sprintf("\n                            return(%d)\n                            ", $next_not_child_counter);
                 break;
             }
             $code = sprintf("\n                if(exists('%s') && %s) {\n                    %s <<- %s + 1\n                }\n                else {\n                    %s <<- TRUE\n                    %s <<- 1\n                }\n\n                CONCERTO_TEMP_FOR_INDEX <- 1\n                \n                for(CONCERTO_TEMP_FOR_VALUE in %s){\n                    if(CONCERTO_TEMP_FOR_INDEX==%s){\n                        %s <<- CONCERTO_TEMP_FOR_VALUE\n                        return(%d)\n                        break\n                    }\n                    CONCERTO_TEMP_FOR_INDEX <- CONCERTO_TEMP_FOR_INDEX+1\n                }\n                \n                %s <<- FALSE\n                return(%d)\n                    ", $this->get_for_initialization_variable(), $this->get_for_initialization_variable(), $this->get_for_index_variable(), $this->get_for_index_variable(), $this->get_for_initialization_variable(), $this->get_for_index_variable(), $vals[1], $this->get_for_index_variable(), $vals[0], $next->counter, $this->get_for_initialization_variable(), $next_not_child_counter);
             break;
         case DS_TestSectionType::WHILE_LOOP:
             $contents = TestSection::from_property(array("Test_id" => $this->Test_id, "parent_counter" => $this->counter));
             $is_empty = count($contents) == 0;
             $next_not_child = $this->get_next_not_child_TestSection();
             $next_not_child_counter = $next_not_child->counter;
             if ($end_of_loop) {
                 $next_not_child_counter = $next_counter;
             } else {
                 $is_end_of_loop = $this->is_end_of_loop(false);
                 if ($is_end_of_loop) {
                     $parent_loop = $this->get_section_loop();
                     if ($parent_loop != null) {
                         $next_not_child_counter = $parent_loop->counter;
                     }
                 }
             }
             $code = "";
             if ($is_empty) {
                 $code .= sprintf("\n                            return(%d)\n                            ", $next_not_child_counter);
                 break;
             }
             $additional_conds = "";
             $i = 3;
             while (isset($vals[$i])) {
                 $additional_conds .= sprintf("%s %s %s %s", $vals[$i], $vals[$i + 1], $vals[$i + 2], $vals[$i + 3]);
                 $i += 4;
             }
             $code = sprintf("\n                if(%s %s %s %s) {\n                    return(%d)\n                    }\n                    else {\n                    return(%d)\n                    }\n                    ", $vals[0], $vals[1], $vals[2], $additional_conds, $next->counter, $next_not_child_counter);
             break;
         case DS_TestSectionType::TABLE_MOD:
             $type = $vals[0];
             $set_count = $vals[2];
             $where_count = $vals[1];
             $table = Table::from_mysql_id($vals[3]);
             if ($table == null) {
                 return sprintf("stop('Invalid table id: %s in section #%s')", $vals[3], $this->counter);
             }
             $set = "";
             for ($i = 0; $i < $vals[2]; $i++) {
                 $column = TableColumn::from_property(array("Table_id" => $vals[3], "index" => $vals[4 + $i * 2]), false);
                 if ($column == null) {
                     return sprintf("stop('Invalid table column index: %s of table id: %s in section #%s')", $vals[4 + $i * 2], $vals[3], $this->counter);
                 }
                 if ($i > 0) {
                     $set .= ",";
                 }
                 $set .= sprintf("`%s`='\",dbEscapeStrings(CONCERTO_DB_CONNECTION,toString(%s)),\"'", $column->name, $vals[4 + $i * 2 + 1]);
             }
             $where = "";
             for ($i = 0; $i < $vals[1]; $i++) {
                 $j = 4 + $vals[2] * 2 + $i * 4;
                 $column = TableColumn::from_property(array("Table_id" => $vals[3], "index" => $vals[$j + 1]), false);
                 if ($column == null) {
                     return sprintf("stop('Invalid table column index: %s of table id: %s in section #%s')", $vals[$j + 1], $vals[3], $this->counter);
                 }
                 if ($i > 0) {
                     $where .= sprintf("%s", $vals[$j]);
                 }
                 $where .= sprintf("`%s` %s '\",dbEscapeStrings(CONCERTO_DB_CONNECTION,toString(%s)),\"'", $column->name, $vals[$j + 2], $vals[$j + 3]);
             }
             $sql = "";
             if ($type == 0) {
                 $sql .= sprintf("INSERT INTO `%s` SET %s", $table->get_table_name(), $set);
             }
             if ($type == 1) {
                 $sql .= sprintf("UPDATE `%s` SET %s WHERE %s", $table->get_table_name(), $set, $where);
             }
             if ($type == 2) {
                 $sql .= sprintf("DELETE FROM `%s` WHERE %s", $table->get_table_name(), $where);
             }
             $code = sprintf('
                     CONCERTO_SQL <- paste("%s",sep="")
                     CONCERTO_SQL_RESULT <- dbSendQuery(CONCERTO_DB_CONNECTION,CONCERTO_SQL)
                     return(%d)
                     ', $sql, $this->end == 0 ? $next_counter : -2);
             break;
         case DS_TestSectionType::SET_VARIABLE:
             $type = $vals[2];
             $columns_count = $vals[0];
             $conds_count = $vals[1];
             if ($type == 0) {
                 $table = Table::from_mysql_id($vals[5]);
                 if ($table == null) {
                     return sprintf("stop('Invalid table id: %s in section #%s')", $vals[5], $this->counter);
                 }
                 $column = TableColumn::from_property(array("Table_id" => $table->id, "index" => $vals[6]), false);
                 if ($column == null) {
                     return sprintf("stop('Invalid table column index: %s of table id: %s in section #%s')", $vals[6], $table->id, $this->counter);
                 }
                 $sql = sprintf("SELECT `%s`", $column->name);
                 for ($i = 1; $i <= $columns_count; $i++) {
                     $column = TableColumn::from_property(array("Table_id" => $table->id, "index" => $vals[6 + $i]), false);
                     if ($column == null) {
                         return sprintf("stop('Invalid table column index: %s of table id: %s in section #%s')", $vals[6 + $i], $table->id, $this->counter);
                     }
                     $sql .= sprintf(",`%s`", $column->name);
                 }
                 $sql .= sprintf(" FROM `%s` ", $table->get_table_name());
                 if ($conds_count > 0) {
                     $sql .= sprintf("WHERE ");
                     $j = 7 + $columns_count;
                     for ($i = 1; $i <= $conds_count; $i++) {
                         if ($i > 1) {
                             $link = $vals[$j];
                             $j++;
                         } else {
                             $j++;
                         }
                         $cond_col = TableColumn::from_property(array("Table_id" => $table->id, "index" => $vals[$j]), false);
                         if ($cond_col == null) {
                             return sprintf("stop('Invalid table column index: %s of table id: %s in section #%s')", $vals[$j], $table->id, $this->counter);
                         }
                         $j++;
                         $operator = $vals[$j];
                         $j++;
                         $exp = $vals[$j];
                         $j++;
                         if ($i > 1) {
                             $sql .= sprintf("%s `%s` %s '\",dbEscapeStrings(CONCERTO_DB_CONNECTION,toString(%s)),\"' ", $link, $cond_col->name, $operator, $exp);
                         } else {
                             $sql .= sprintf("`%s` %s '\",dbEscapeStrings(CONCERTO_DB_CONNECTION,toString(%s)),\"' ", $cond_col->name, $operator, $exp);
                         }
                     }
                 }
                 $code = sprintf('
                     CONCERTO_SQL <- paste("%s",sep="")
                     CONCERTO_SQL_RESULT <- dbSendQuery(CONCERTO_DB_CONNECTION,CONCERTO_SQL)
                     %s <<- fetch(CONCERTO_SQL_RESULT,n=-1)
                     return(%d)
                     ', $sql, $vals[4], $this->end == 0 ? $next_counter : -2);
                 break;
             }
             if ($type == 1) {
                 $code = sprintf('
                     %s <<- {
                     %s
                     }
                     return(%d)
                     ', $vals[4], $vals[3], $this->end == 0 ? $next_counter : -2);
                 break;
             }
     }
     return TestSection::replace_invalid_code($code);
 }
 public function RCall($code, $include_ini_code = false, $close = false, $debug_syntax = false)
 {
     $command = "";
     if (!$debug_syntax) {
         if ($include_ini_code) {
             $command = $this->get_ini_RCode();
         } else {
             $command .= $this->get_next_ini_RCode();
         }
     }
     $command .= $code;
     if (!$debug_syntax) {
         $command .= $this->get_post_RCode();
     }
     $output = array();
     $return = -999;
     $command_obj = json_encode(array("session_id" => $this->id, "code" => $command, "close" => 0));
     if (TestServer::$debug) {
         TestServer::log_debug("TestSession->RCall --- checking for server");
     }
     if (!TestServer::is_running()) {
         TestServer::start_process();
     }
     if (TestServer::$debug) {
         TestServer::log_debug("TestSession->RCall --- server found, trying to send");
     }
     $response = TestServer::send($command_obj);
     $result = json_decode(trim($response));
     if (TestServer::$debug) {
         TestServer::log_debug("TestSession->RCall --- sent and recieved response");
     }
     $output = explode("\n", $result->output);
     $return = $result->return;
     $thisSession = null;
     $status = TestSession::TEST_SESSION_STATUS_ERROR;
     $removed = false;
     $release = 0;
     $html = "";
     $head = "";
     $Template_id = 0;
     $debug = 0;
     $hash = "";
     $time_limit = 0;
     $Test_id = 0;
     $finished = 0;
     $loader_HTML = "";
     $loader_head = "";
     $loader_effect_show = "none";
     $loader_effect_hide = "none";
     $loader_effect_show_options = "";
     $loader_effect_hide_options = "";
     $effect_show = "none";
     $effect_hide = "none";
     $effect_show_options = "";
     $effect_hide_options = "";
     if (!$debug_syntax) {
         $thisSession = TestSession::from_mysql_id($this->id);
         if ($thisSession != null) {
             $status = $thisSession->status;
             $release = $thisSession->release;
             $html = $thisSession->HTML;
             $Template_id = $thisSession->Template_id;
             $debug = $thisSession->debug;
             $hash = $thisSession->hash;
             $time_limit = $thisSession->time_limit;
             $Test_id = $thisSession->Test_id;
             $loader_HTML = $thisSession->loader_HTML;
             $loader_head = $thisSession->loader_head;
             $loader_effect_hide = $thisSession->loader_effect_hide;
             $loader_effect_hide_options = $thisSession->loader_effect_hide_options;
             $loader_effect_show = $thisSession->loader_effect_show;
             $loader_effect_show_options = $thisSession->loader_effect_show_options;
             $effect_hide = $thisSession->effect_hide;
             $effect_hide_options = $thisSession->effect_hide_options;
             $effect_show = $thisSession->effect_show;
             $effect_show_options = $thisSession->effect_show_options;
             if ($return != 0) {
                 $status = TestSession::TEST_SESSION_STATUS_ERROR;
             }
             if ($status == TestSession::TEST_SESSION_STATUS_WORKING && $release == 1 || $close) {
                 $status = TestSession::TEST_SESSION_STATUS_COMPLETED;
             }
             $thisSession->status = $status;
             $thisSession->mysql_save();
             switch ($status) {
                 case TestSession::TEST_SESSION_STATUS_COMPLETED:
                     if ($debug) {
                         TestSession::unregister($thisSession->id);
                         $removed = true;
                     } else {
                         $thisSession->serialize();
                     }
                     break;
                 case TestSession::TEST_SESSION_STATUS_ERROR:
                 case TestSession::TEST_SESSION_STATUS_TAMPERED:
                     if ($debug) {
                         TestSession::unregister($thisSession->id);
                         $removed = true;
                     } else {
                         $thisSession->close();
                     }
                     break;
                 case TestSession::TEST_SESSION_STATUS_TEMPLATE:
                     if ($debug) {
                         $html = Template::strip_html($html);
                         if ($release) {
                             TestSession::unregister($thisSession->id);
                         }
                     } else {
                         $head = Template::from_mysql_id($Template_id)->head;
                         if ($release) {
                             $thisSession->serialize();
                         }
                     }
                     break;
             }
         } else {
             $removed = true;
         }
     }
     $test = Test::from_mysql_id($this->Test_id);
     $debug_data = false;
     $logged_user = User::get_logged_user();
     if ($logged_user != null) {
         $debug_data = $logged_user->is_object_readable($test);
     }
     if ($release == 1 || $status == TestSession::TEST_SESSION_STATUS_COMPLETED || $status == TestSession::TEST_SESSION_STATUS_ERROR || $status == TestSession::TEST_SESSION_STATUS_TAMPERED) {
         $finished = 1;
     }
     if (!$debug_syntax) {
         $response = array("data" => array("HEAD" => $head, "HASH" => $hash, "TIME_LIMIT" => $time_limit, "HTML" => $html, "TEST_ID" => $Test_id, "TEST_SESSION_ID" => $this->id, "STATUS" => $status, "TEMPLATE_ID" => $Template_id, "FINISHED" => $finished, "LOADER_HTML" => $loader_HTML, "LOADER_HEAD" => $loader_head, "LOADER_EFFECT_SHOW" => $loader_effect_show, "LOADER_EFFECT_SHOW_OPTIONS" => $loader_effect_show_options, "LOADER_EFFECT_HIDE" => $loader_effect_hide, "LOADER_EFFECT_HIDE_OPTIONS" => $loader_effect_hide_options, "EFFECT_SHOW" => $effect_show, "EFFECT_HIDE" => $effect_hide, "EFFECT_SHOW_OPTIONS" => $effect_show_options, "EFFECT_HIDE_OPTIONS" => $effect_hide_options));
     }
     if ($debug_data) {
         for ($i = 0; $i < count($output); $i++) {
             if (strpos($output[$i], "CONCERTO_DB_PASSWORD <-") !== false) {
                 $output[$i] = "[hidden]";
             }
             $output[$i] = htmlspecialchars($output[$i], ENT_QUOTES);
         }
         $command_lines = explode("\n", $command);
         for ($i = 0; $i < count($command_lines); $i++) {
             if (strpos($command_lines[$i], "CONCERTO_DB_PASSWORD <-") !== false) {
                 $command_lines[$i] = "[hidden]";
             }
         }
         $command = implode("\n", $command_lines);
         $command = htmlspecialchars($command, ENT_QUOTES);
         if (!is_array($response)) {
             $response = array();
         }
         $response["debug"] = array("code" => $command, "return" => $return, "output" => $output);
     }
     if (Ini::$timer_tamper_prevention && !$debug_syntax && !$removed) {
         $sql = sprintf("UPDATE `%s` SET `time_tamper_prevention`=%d WHERE `id`=%d", TestSession::get_mysql_table(), time(), $this->id);
         mysql_query($sql);
     }
     return $response;
 }
Example #4
0
 public static function calculate_all_xml_hashes()
 {
     //CustomSection - calculate xml_hash
     $sql = "SELECT `id` FROM `CustomSection`";
     $z = mysql_query($sql);
     while ($r = mysql_fetch_array($z)) {
         set_time_limit(0);
         $obj = CustomSection::from_mysql_id($r[0]);
         $obj->xml_hash = $obj->calculate_xml_hash();
         $obj->mysql_save();
     }
     //Table - calculate xml_hash
     $sql = "SELECT `id` FROM `Table`";
     $z = mysql_query($sql);
     while ($r = mysql_fetch_array($z)) {
         set_time_limit(0);
         $obj = Table::from_mysql_id($r[0]);
         $obj->xml_hash = $obj->calculate_xml_hash();
         $obj->mysql_save();
     }
     //Template - calculate xml_hash
     $sql = "SELECT `id` FROM `Template`";
     $z = mysql_query($sql);
     while ($r = mysql_fetch_array($z)) {
         set_time_limit(0);
         $obj = Template::from_mysql_id($r[0]);
         $obj->xml_hash = $obj->calculate_xml_hash();
         $obj->mysql_save();
     }
     //Test - calculate xml_hash
     $sql = "SELECT `id` FROM `Test`";
     $z = mysql_query($sql);
     while ($r = mysql_fetch_array($z)) {
         set_time_limit(0);
         $obj = Test::from_mysql_id($r[0]);
         $obj->xml_hash = $obj->calculate_xml_hash();
         $obj->mysql_save();
     }
 }
Example #5
0
?>
        <td>
            <div class="divFormControl">
                <select id="selectLoaderTemplate" class="fullWidth ui-widget-content ui-corner-all fullWidth" onchange="Test.uiRefreshLoader($(this).val())">
                    <option value="0" <?php 
echo $loader_id == 0 ? "selected" : "";
?>
>&lt;<?php 
echo Language::string(538);
?>
&gt;</option>
                    <?php 
$sql = $logged_user->mysql_list_rights_filter("Template", "`name` ASC");
$z = mysql_query($sql);
while ($r = mysql_fetch_array($z)) {
    $t = Template::from_mysql_id($r[0]);
    ?>
                        <option value="<?php 
    echo $t->id;
    ?>
" <?php 
    echo $loader_id == $t->id ? "selected" : "";
    ?>
><?php 
    echo $t->name;
    ?>
 ( <?php 
    echo $t->get_system_data();
    ?>
 )</option>
                    <?php 
Example #6
0
 public function export($xml = null, $sub_test = false, $main_test = null)
 {
     if ($xml == null) {
         $xml = new DOMDocument('1.0', 'UTF-8');
         $export = $xml->createElement("export");
         $export->setAttribute("version", Ini::$version);
         $xml->appendChild($export);
         $xpath = new DOMXPath($xml);
     } else {
         $xpath = new DOMXPath($xml);
         $export = $xpath->query("/export");
         $export = $export->item(0);
     }
     //append subobjects of test
     $tests_ids = array();
     array_push($tests_ids, $this->id);
     $templates_ids = array();
     $custom_sections_ids = array();
     $tables_ids = array();
     $qtiai_ids = array();
     $loader = $this->get_loader_Template();
     if ($loader != null) {
         if (!in_array($loader->id, $templates_ids)) {
             $template = $loader;
             if ($template != null) {
                 $present_templates = $xpath->query("/export/Template");
                 $exists = false;
                 foreach ($present_templates as $obj) {
                     if ($template->xml_hash == $obj->getAttribute("xml_hash")) {
                         $exists = true;
                         break;
                     }
                 }
                 if (!$exists) {
                     $element = $template->to_XML();
                     $obj = $xml->importNode($element, true);
                     $export->appendChild($obj);
                     array_push($templates_ids, $loader->id);
                 }
             }
         }
     }
     $sql = sprintf("SELECT \n            `TestSection`.`id`,`TestSectionValue`.`value`,`TestSection`.`TestSectionType_id` \n            FROM `TestSection` \n            LEFT JOIN `TestSectionValue` ON `TestSectionValue`.`TestSection_id`=`TestSection`.`id`\n            WHERE \n            (`TestSection`.`TestSectionType_id`=13 AND `TestSectionValue`.`index`=0 OR\n            `TestSection`.`TestSectionType_id`=2 AND `TestSectionValue`.`index`=0 OR\n            `TestSection`.`TestSectionType_id`=9 AND `TestSectionValue`.`index`=0 OR\n            `TestSection`.`TestSectionType_id`=11 AND `TestSectionValue`.`index`=0 OR\n            `TestSection`.`TestSectionType_id`=8 AND `TestSectionValue`.`index`=3 OR\n            `TestSection`.`TestSectionType_id`=5 AND `TestSectionValue`.`index`=5) AND `TestSection`.`Test_id`=%d ORDER BY `TestSection`.`TestSectionType_id` ASC", $this->id);
     $z = mysql_query($sql);
     while ($r = mysql_fetch_array($z)) {
         switch ($r[2]) {
             //templates
             case 2:
                 if (!in_array($r[1], $templates_ids)) {
                     $template = Template::from_mysql_id($r[1]);
                     if ($template != null) {
                         $present_templates = $xpath->query("/export/Template");
                         $exists = false;
                         foreach ($present_templates as $obj) {
                             if ($template->xml_hash == $obj->getAttribute("xml_hash")) {
                                 $exists = true;
                                 break;
                             }
                         }
                         if ($exists) {
                             break;
                         }
                         $element = $template->to_XML();
                         $obj = $xml->importNode($element, true);
                         $export->appendChild($obj);
                         array_push($templates_ids, $r[1]);
                     }
                 }
                 break;
                 //QTIAssessmentItem
             //QTIAssessmentItem
             case 13:
                 if (!in_array($r[1], $qtiai_ids)) {
                     $qtiai = QTIAssessmentItem::from_mysql_id($r[1]);
                     if ($qtiai != null) {
                         $present_qtiai = $xpath->query("/export/QTIAssessmentItem");
                         $exists = false;
                         foreach ($present_qtiai as $obj) {
                             if ($qtiai->xml_hash == $obj->getAttribute("xml_hash")) {
                                 $exists = true;
                                 break;
                             }
                         }
                         if ($exists) {
                             break;
                         }
                         $element = $qtiai->to_XML();
                         $obj = $xml->importNode($element, true);
                         $export->appendChild($obj);
                         array_push($qtiai_ids, $r[1]);
                     }
                 }
                 break;
                 //custom sections
             //custom sections
             case 9:
                 if (!in_array($r[1], $custom_sections_ids)) {
                     $custom_section = CustomSection::from_mysql_id($r[1]);
                     if ($custom_section != null) {
                         $present_custom_sections = $xpath->query("/export/CustomSection");
                         $exists = false;
                         foreach ($present_custom_sections as $obj) {
                             if ($custom_section->xml_hash == $obj->getAttribute("xml_hash")) {
                                 $exists = true;
                                 break;
                             }
                         }
                         if ($exists) {
                             break;
                         }
                         $element = $custom_section->to_XML();
                         $obj = $xml->importNode($element, true);
                         $export->appendChild($obj);
                         array_push($custom_sections_ids, $r[1]);
                     }
                 }
                 break;
                 //tables
             //tables
             case 8:
                 if (!in_array($r[1], $tables_ids)) {
                     $table = Table::from_mysql_id($r[1]);
                     if ($table != null) {
                         $present_tables = $xpath->query("/export/Table");
                         $exists = false;
                         foreach ($present_tables as $obj) {
                             if ($table->xml_hash == $obj->getAttribute("xml_hash")) {
                                 $exists = true;
                                 break;
                             }
                         }
                         if ($exists) {
                             break;
                         }
                         $element = $table->to_XML();
                         $obj = $xml->importNode($element, true);
                         $export->appendChild($obj);
                         array_push($tables_ids, $r[1]);
                     }
                 }
                 break;
             case 5:
                 $sql2 = sprintf("SELECT * FROM `TestSectionValue` WHERE `TestSection_id`=%d AND `index`=2 AND `value`=0", $r[0]);
                 $z2 = mysql_query($sql2);
                 while ($r2 = mysql_fetch_array($z2)) {
                     if (!in_array($r[1], $tables_ids)) {
                         $table = Table::from_mysql_id($r[1]);
                         if ($table != null) {
                             $present_tables = $xpath->query("/export/Table");
                             $exists = false;
                             foreach ($present_tables as $obj) {
                                 if ($table->xml_hash == $obj->getAttribute("xml_hash")) {
                                     $exists = true;
                                     break;
                                 }
                             }
                             if ($exists) {
                                 break;
                             }
                             $element = $table->to_XML();
                             $obj = $xml->importNode($element, true);
                             $export->appendChild($obj);
                             array_push($tables_ids, $r[1]);
                         }
                     }
                 }
                 break;
                 //tests
             //tests
             case 11:
                 if (!in_array($r[1], $tests_ids)) {
                     $test = Test::from_mysql_id($r[1]);
                     if ($test != null) {
                         if ($main_test != null && $main_test->id == $test->id) {
                             break;
                         }
                         $present_tests = $xpath->query("/export/Test");
                         $exists = false;
                         foreach ($present_tests as $obj) {
                             if ($test->xml_hash == $obj->getAttribute("xml_hash")) {
                                 $exists = true;
                                 break;
                             }
                         }
                         if ($exists) {
                             break;
                         }
                         $xml = $test->export($xml, true, $main_test != null ? $main_test : $this);
                         $element = $test->to_XML();
                         $obj = $xml->importNode($element, true);
                         $export->appendChild($obj);
                         array_push($tests_ids, $r[1]);
                     }
                 }
                 break;
         }
     }
     if (!$sub_test) {
         $element = $this->to_XML();
         $obj = $xml->importNode($element, true);
         $export->appendChild($obj);
     }
     return $sub_test ? $xml : $xml->saveXML();
 }
}
$logged_user = User::get_logged_user();
if ($logged_user == null) {
    echo "<script>location.reload();</script>";
    die(Language::string(278));
}
if (isset($oid)) {
    if (!$logged_user->is_module_writeable($class_name)) {
        die(Language::string(81));
    }
    if (!$logged_user->is_object_editable($obj)) {
        die(Language::string(81));
    }
} else {
    $oid = $_POST['oid'];
    $obj = Template::from_mysql_id($oid);
    $class_name = $_POST['class_name'];
    if (!$logged_user->is_module_writeable($class_name)) {
        die(Language::string(81));
    }
    if (!$logged_user->is_object_editable($obj)) {
        die(Language::string(81));
    }
}
$effect_show = $obj->effect_show;
if (array_key_exists("effect_show", $_POST)) {
    $effect_show = $_POST['effect_show'];
}
if ($effect_show == "") {
    $effect_show = "none";
}
Example #8
0
 public static function update_db($simulate = false, $only_recalculate_hash = false, $only_repopulate_TestTemplate = false, $only_validate_column_names = false, $only_create_db = false)
 {
     require '../Ini.php';
     $ini = new Ini();
     if ($only_create_db) {
         return self::create_db_structure();
     }
     if ($only_recalculate_hash) {
         OModule::calculate_all_xml_hashes();
         return json_encode(array("result" => 0));
     }
     if ($only_repopulate_TestTemplate) {
         TestTemplate::repopulate_table();
         return json_encode(array("result" => 0));
     }
     if ($only_validate_column_names) {
         $result = TableColumn::validate_columns_name();
         return json_encode(array("result" => $result ? 0 : 1));
     }
     $versions_to_update = array();
     $previous_version = Setting::get_setting("version");
     if ($previous_version == null) {
         $previous_version = Ini::$version;
     }
     $recalculate_hash = false;
     $repopulate_TestTemplate = false;
     $validate_column_names = false;
     if (Ini::does_patch_apply("3.3.0", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.3.0");
         } else {
             ///COMPATIBILITY FIX FOR V3.0.0 START
             $sql = "SHOW COLUMNS FROM `User` WHERE `Field`='last_activity'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) > 0) {
                 $sql = "ALTER TABLE `User` CHANGE `last_activity` `last_login` timestamp NOT NULL default '0000-00-00 00:00:00';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //DS_TableColumnType - numeric split to integer and float
             $id_4_found = false;
             $sql = "SELECT * FROM `DS_TableColumnType`";
             $z = mysql_query($sql);
             while ($r = mysql_fetch_array($z)) {
                 switch ($r['id']) {
                     case 2:
                         if ($r['name'] != "integer") {
                             $sql2 = "UPDATE `DS_TableColumnType` SET `name`='integer', value='integer' WHERE `id`=2";
                             if (!mysql_query($sql2)) {
                                 return json_encode(array("result" => 1, "param" => $sql2));
                             }
                         }
                         break;
                     case 3:
                         if ($r['name'] != "float") {
                             $sql2 = "UPDATE `DS_TableColumnType` SET `name`='float', value='float' WHERE `id`=3";
                             if (!mysql_query($sql2)) {
                                 return json_encode(array("result" => 1, "param" => $sql2));
                             }
                         }
                         break;
                     case 4:
                         $id_4_found = true;
                         if ($r['name'] != "HTML") {
                             $sql2 = "UPDATE `DS_TableColumnType` SET `name`='HTML', value='HTML' WHERE `id`=4";
                             if (!mysql_query($sql2)) {
                                 return json_encode(array("result" => 1, "param" => $sql2));
                             }
                         }
                         break;
                 }
             }
             if (!$id_4_found) {
                 $sql2 = "INSERT INTO `DS_TableColumnType` SET `id`=4, `name`='HTML', value='HTML', `position`=4";
                 if (!mysql_query($sql2)) {
                     return json_encode(array("result" => 1, "param" => $sql2));
                 }
             }
             //TableColumn - change numeric and float MySQL type
             $sql = sprintf("SELECT * FROM `TableColumn`");
             $z = mysql_query($sql);
             while ($r = mysql_fetch_array($z)) {
                 $table = Table::from_mysql_id($r['Table_id']);
                 if ($table == null) {
                     continue;
                 }
                 if (!$table->has_table()) {
                     $table->mysql_delete();
                     continue;
                 }
                 $table_name = $table->get_table_name();
                 $type = "TEXT NOT NULL";
                 switch ($r['TableColumnType_id']) {
                     case 2:
                         $type = "BIGINT NOT NULL";
                         break;
                     case 3:
                         $type = "DOUBLE NOT NULL";
                         break;
                 }
                 $old_name = $r['name'];
                 $new_name = Table::format_column_name($old_name);
                 if ($r['TableColumnType_id'] == 3) {
                     $sql2 = sprintf("UPDATE `TableColumn` SET `TableColumnType_id`='%d' WHERE `id`='%d'", 4, $r['id']);
                     if (!mysql_query($sql2)) {
                         return json_encode(array("result" => 1, "param" => $sql2));
                     }
                 }
                 if ($old_name != $new_name) {
                     $sql2 = sprintf("ALTER TABLE `%s` CHANGE `%s` `%s` %s;", $table_name, $old_name, $new_name, $type);
                     $i = 1;
                     while (!mysql_query($sql2)) {
                         $new_name = "col" . $i;
                         $sql2 = sprintf("ALTER TABLE `%s` CHANGE `%s` `%s` %s;", $table_name, $old_name, $new_name, $type);
                         $i++;
                     }
                     $sql2 = sprintf("UPDATE `TableColumn` SET `name`='%s' WHERE `id`='%d'", $new_name, $r['id']);
                     if (!mysql_query($sql2)) {
                         return json_encode(array("result" => 1, "param" => $sql2));
                     }
                 }
             }
             Setting::set_setting("version", "3.3.0");
             return json_encode(array("result" => 0, "param" => "3.3.0"));
         }
     }
     if (Ini::does_patch_apply("3.4.0", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.4.0");
         } else {
             //Test - add session_count field
             $sql = "SHOW COLUMNS FROM `Test` WHERE `Field`='session_count'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `Test` ADD `session_count` bigint(20) NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestSectionValue - indexes changes
             $sql = sprintf("SELECT `TestSection`.`id`, `TestSection`.`TestSectionType_id` FROM `TestSection` WHERE `TestSectionType_id` IN (%d,%d,%d)", DS_TestSectionType::LOAD_HTML_TEMPLATE, DS_TestSectionType::SET_VARIABLE, DS_TestSectionType::CUSTOM);
             $z = mysql_query($sql);
             while ($r = mysql_fetch_array($z)) {
                 set_time_limit(0);
                 switch ($r[1]) {
                     case DS_TestSectionType::LOAD_HTML_TEMPLATE:
                         $params_count = 0;
                         $returns_count = 0;
                         $sql2 = sprintf("SELECT `index`,`value` FROM `%s` WHERE `TestSection_id`=%d AND (`index`=1 OR `index`=2) ", TestSectionValue::get_mysql_table(), $r[0]);
                         $z2 = mysql_query($sql2);
                         while ($r2 = mysql_fetch_array($z2)) {
                             if ($r2['index'] == 1) {
                                 $params_count = $r2['value'];
                             }
                             if ($r2['index'] == 2) {
                                 $returns_count = $r2['value'];
                             }
                         }
                         $delete_index = 3 + $params_count + 1;
                         for ($i = 0; $i < $returns_count; $i++) {
                             $sql2 = sprintf("DELETE FROM `%s` WHERE `TestSection_id`=%d AND `index` IN (%d,%d)", TestSectionValue::get_mysql_table(), $r[0], $delete_index, $delete_index + 1);
                             if (!mysql_query($sql2)) {
                                 return json_encode(array("result" => 1, "param" => $sql2));
                             }
                             $sql2 = sprintf("UPDATE `%s` SET `index`=`index`-2 WHERE `TestSection_id`=%d AND `index`>%d", TestSectionValue::get_mysql_table(), $r[0], $delete_index);
                             if (!mysql_query($sql2)) {
                                 return json_encode(array("result" => 1, "param" => $sql2));
                             }
                             $delete_index++;
                         }
                         break;
                     case DS_TestSectionType::SET_VARIABLE:
                         $sql2 = sprintf("DELETE FROM `%s` WHERE `TestSection_id`=%d AND `index` IN (4,5)", TestSectionValue::get_mysql_table(), $r[0]);
                         if (!mysql_query($sql2)) {
                             return json_encode(array("result" => 1, "param" => $sql2));
                         }
                         $sql2 = sprintf("UPDATE `%s` SET `index`=`index`-2 WHERE `TestSection_id`=%d AND `index`>%d", TestSectionValue::get_mysql_table(), $r[0], 5);
                         if (!mysql_query($sql2)) {
                             return json_encode(array("result" => 1, "param" => $sql2));
                         }
                         break;
                     case DS_TestSectionType::CUSTOM:
                         $params_count = 0;
                         $returns_count = 0;
                         $csid = 0;
                         $sql2 = sprintf("SELECT `value` FROM `%s` WHERE `TestSection_id`=%d AND `index`=0 ", TestSectionValue::get_mysql_table(), $r[0]);
                         $z2 = mysql_query($sql2);
                         $r2 = mysql_fetch_array($z2);
                         $csid = $r2['value'];
                         $sql2 = sprintf("SELECT * FROM `%s` WHERE `CustomSection_id`=%d AND `type`=0", CustomSectionVariable::get_mysql_table(), $csid);
                         $params_count = mysql_num_rows(mysql_query($sql2));
                         $sql2 = sprintf("SELECT * FROM `%s` WHERE `CustomSection_id`=%d AND `type`=1", CustomSectionVariable::get_mysql_table(), $csid);
                         $returns_count = mysql_num_rows(mysql_query($sql2));
                         $delete_index = 1 + $params_count + 1;
                         for ($i = 0; $i < $returns_count; $i++) {
                             $sql2 = sprintf("DELETE FROM `%s` WHERE `TestSection_id`=%d AND `index` IN (%d,%d)", TestSectionValue::get_mysql_table(), $r[0], $delete_index, $delete_index + 1);
                             if (!mysql_query($sql2)) {
                                 return json_encode(array("result" => 1, "param" => $sql2));
                             }
                             $sql2 = sprintf("UPDATE `%s` SET `index`=`index`-2 WHERE `TestSection_id`=%d AND `index`>%d", TestSectionValue::get_mysql_table(), $r[0], $delete_index);
                             if (!mysql_query($sql2)) {
                                 return json_encode(array("result" => 1, "param" => $sql2));
                             }
                             $delete_index++;
                         }
                         break;
                 }
             }
             //TestSession - added new fields
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='status'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `status` tinyint(4) NOT NULL default '0';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='time_limit'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `time_limit` int(11) NOT NULL default '0';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='HTML'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `HTML` text NOT NULL default '';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='Template_id'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `Template_id` bigint(20) NOT NULL default '0';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='time_tamper_prevention'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE  `TestSession` ADD  `time_tamper_prevention` INT NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='hash'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `hash` text NOT NULL default '';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='r_type'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE  `TestSession` ADD  `r_type` TINYINT( 1 ) NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             Setting::set_setting("version", "3.4.0");
             return json_encode(array("result" => 0, "param" => "3.4.0"));
         }
     }
     if (Ini::does_patch_apply("3.4.1", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.4.1");
         } else {
             //TestSession - added Template_TestSection_id field
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='Template_TestSection_id'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `Template_TestSection_id` bigint(20) NOT NULL default '0';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             Setting::set_setting("version", "3.4.1");
             return json_encode(array("result" => 0, "param" => "3.4.1"));
         }
     }
     if (Ini::does_patch_apply("3.4.3", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.4.3");
         } else {
             //TestSection - added end field
             $sql = "SHOW COLUMNS FROM `TestSection` WHERE `Field`='end'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSection` ADD `end` tinyint(1) NOT NULL default '0';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestSession - added new fields
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='debug'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `debug` tinyint(1) NOT NULL default '0';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='release'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `release` tinyint(1) NOT NULL default '0';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='serialized'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `serialized` tinyint(1) NOT NULL default '0';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             Setting::set_setting("version", "3.4.3");
             return json_encode(array("result" => 0, "param" => "3.4.3"));
         }
     }
     if (Ini::does_patch_apply("3.5.0", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.5.0");
         } else {
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='serialized'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `serialized` tinyint(1) NOT NULL default '0';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //Table - added description field
             $sql = "SHOW COLUMNS FROM `Table` WHERE `Field`='description'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `Table` ADD `description` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TableColumn - fix Timestamp fields names
             $sql = "SHOW COLUMNS FROM `TableColumn` WHERE `Field`='created' OR `Field`='udpated'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 2) {
                 $sql = "ALTER TABLE `TableColumn` CHANGE `created` `updated_temp` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
                 $sql = "ALTER TABLE `TableColumn` CHANGE `udpated` `created` TIMESTAMP NOT NULL DEFAULT  '0000-00-00 00:00:00';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
                 $sql = "ALTER TABLE `TableColumn` CHANGE `updated_temp` `updated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //Template - added description field
             $sql = "SHOW COLUMNS FROM `Template` WHERE `Field`='description'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `Template` ADD `description` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //Test - added description field
             $sql = "SHOW COLUMNS FROM `Test` WHERE `Field`='description'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `Test` ADD `description` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestSection - fix Timestamp fields
             $sql = "SHOW COLUMNS FROM `TestSection` WHERE `Field`='created' OR `Field`='updated'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 2) {
                 $sql = "ALTER TABLE `TestSection` CHANGE `created` `updated_temp` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
                 $sql = "ALTER TABLE `TestSection` CHANGE `updated` `created` TIMESTAMP NOT NULL DEFAULT  '0000-00-00 00:00:00';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
                 $sql = "ALTER TABLE `TestSection` CHANGE `updated_temp` `updated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestSectionValue - fix Timestamp fields
             $sql = "SHOW COLUMNS FROM `TestSectionValue` WHERE `Field`='created' OR `Field`='updated'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 2) {
                 $sql = "ALTER TABLE `TestSectionValue` CHANGE `created` `updated_temp` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
                 $sql = "ALTER TABLE `TestSectionValue` CHANGE `updated` `created` TIMESTAMP NOT NULL DEFAULT  '0000-00-00 00:00:00';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
                 $sql = "ALTER TABLE `TestSectionValue` CHANGE `updated_temp` `updated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             Setting::set_setting("version", "3.5.0");
             return json_encode(array("result" => 0, "param" => "3.5.0"));
         }
     }
     if (Ini::does_patch_apply("3.5.2", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.5.2");
         } else {
             //TestSection - fixed name of r_type field
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='r_typ'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) > 0) {
                 $sql = "ALTER TABLE `TestSession` CHANGE `r_typ` `r_type` tinyint(1) NOT NULL default '0';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             Setting::set_setting("version", "3.5.2");
             return json_encode(array("result" => 0, "param" => "3.5.2"));
         }
     }
     if (Ini::does_patch_apply("3.6.0", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.6.0");
         } else {
             //DS_TestSectionType - added loop and test inclusion section
             $sql = "INSERT INTO `DS_TestSectionType` SET `id`=10, `name`='loop', `value`='10', `position`=10;";
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => 1, "msg" => $sql));
             }
             $sql = "INSERT INTO `DS_TestSectionType` SET `id`=11, `name`='test inclusion', `value`='11', `position`=11;";
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => 1, "param" => $sql));
             }
             //Template - added head field
             $sql = "SHOW COLUMNS FROM `Template` WHERE `Field`='head'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `Template` ADD `head` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             Setting::set_setting("version", "3.6.0");
             return json_encode(array("result" => 0, "param" => "3.6.0"));
         }
     }
     if (Ini::does_patch_apply("3.6.2", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.6.2");
         } else {
             //CustomSection - added xml_hash
             $sql = "SHOW COLUMNS FROM `CustomSection` WHERE `Field`='xml_hash'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `CustomSection` ADD `xml_hash` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //Table - added xml_hash
             $sql = "SHOW COLUMNS FROM `Table` WHERE `Field`='xml_hash'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `Table` ADD `xml_hash` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //Template - added xml_hash
             $sql = "SHOW COLUMNS FROM `Template` WHERE `Field`='xml_hash'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `Template` ADD `xml_hash` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //Test - added xml_hash
             $sql = "SHOW COLUMNS FROM `Test` WHERE `Field`='xml_hash'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `Test` ADD `xml_hash` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             Setting::set_setting("version", "3.6.2");
             return json_encode(array("result" => 0, "param" => "3.6.2"));
         }
     }
     if (Ini::does_patch_apply("3.6.7", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.6.7");
         } else {
             //User - added new fields
             $sql = "SHOW COLUMNS FROM `User` WHERE `Field`='UserInstitutionType_id'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `User` ADD `UserInstitutionType_id` int(11) NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             $sql = "SHOW COLUMNS FROM `User` WHERE `Field`='institution_name'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `User` ADD `institution_name` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //UserType - changed Sharing_id of standard user type to public
             $sql = "UPDATE `UserType` SET `Sharing_id`=3 WHERE `id`=4;";
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => 1, "param" => $sql));
             }
             Setting::set_setting("version", "3.6.7");
             return json_encode(array("result" => 0, "param" => "3.6.7"));
         }
     }
     if (Ini::does_patch_apply("3.6.8", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.6.8");
         } else {
             //TestSectionValue - indexes changes
             $sections = TestSection::from_property(array("TestSectionType_id" => DS_TestSectionType::LOAD_HTML_TEMPLATE));
             foreach ($sections as $section) {
                 $sql = sprintf("DELETE FROM `%s` WHERE `TestSection_id`=%d AND `index`>=3", TestSectionValue::get_mysql_table(), $section->id);
                 $vals = $section->get_values();
                 $template = Template::from_mysql_id($vals[0]);
                 if ($template == null) {
                     continue;
                 }
                 $inserts = $template->get_inserts();
                 $returns = $template->get_outputs();
                 $j = 3;
                 $i = 3;
                 foreach ($inserts as $ins) {
                     $tsv = new TestSectionValue();
                     $tsv->TestSection_id = $section->id;
                     $tsv->index = $j;
                     $tsv->value = $ins;
                     $tsv->mysql_save();
                     $tsv = new TestSectionValue();
                     $tsv->TestSection_id = $section->id;
                     $tsv->index = $j + 1;
                     $tsv->value = isset($vals[$i]) ? $vals[$i] : $ins;
                     $tsv->mysql_save();
                     $j = $j + 2;
                     $i++;
                 }
                 foreach ($returns as $ret) {
                     $tsv = new TestSectionValue();
                     $tsv->TestSection_id = $section->id;
                     $tsv->index = $j;
                     $tsv->value = $ret['name'];
                     $tsv->mysql_save();
                     $tsv = new TestSectionValue();
                     $tsv->TestSection_id = $section->id;
                     $tsv->index = $j + 1;
                     $tsv->value = isset($vals[$i]) ? $vals[$i] : $ret['name'];
                     $tsv->mysql_save();
                     $j = $j + 2;
                     $i++;
                 }
             }
             Setting::set_setting("version", "3.6.8");
             return json_encode(array("result" => 0, "param" => "3.6.8"));
         }
         $validate_column_names = true;
     }
     if (Ini::does_patch_apply("3.6.10", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.6.10");
         } else {
             //User - change of password field
             $sql = "SHOW COLUMNS FROM `User` WHERE `Field`='md5_password'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) > 0) {
                 $sql = "ALTER TABLE `User` CHANGE `md5_password` `password` text NOT NULL default '';";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             Setting::set_setting("version", "3.6.10");
             return json_encode(array("result" => 0, "param" => "3.6.10"));
         }
     }
     if (Ini::does_patch_apply("3.6.11", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.6.11");
         } else {
             //DS_TestSectionType - add new section type
             $sql = "\r\n                INSERT INTO `DS_TestSectionType` (`id`, `name`, `value`, `position`) VALUES\r\n                (12, 'lower level R code', '12', 12);\r\n                ";
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => 1, "param" => $sql));
             }
             Setting::set_setting("version", "3.6.11");
             return json_encode(array("result" => 0, "param" => "3.6.11"));
         }
     }
     if (Ini::does_patch_apply("3.7.2", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.7.2");
         } else {
             //DS_Module - add new module type
             $sql = "\r\n                INSERT INTO `DS_Module` (`id`, `name`, `value`, `position`) VALUES\r\n                (8, 'QTI assessment item', 'QTIAssessmentItem', 8);\r\n                ";
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => 1, "param" => $sql));
             }
             //UserTypeRight - add default user type rights for new module
             $sql = "\r\n                INSERT INTO `UserTypeRight` (`id`, `updated`, `created`, `Module_id`, `UserType_id`, `read`, `write`, `ownership`) VALUES\r\n                (21, '2012-01-18 18:59:34', '2012-01-18 18:59:34', 8, 1, 5, 5, 1),\r\n                (22, '2012-02-03 18:29:48', '2012-02-03 18:29:48', 8, 4, 3, 3, 0);\r\n                ";
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => 1, "param" => $sql));
             }
             Setting::set_setting("version", "3.7.2");
             return json_encode(array("result" => 0, "param" => "3.7.2"));
         }
     }
     if (Ini::does_patch_apply("3.7.3", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.7.3");
         } else {
             //DS_TestSectionType - add new section types
             $sql = "\r\n                INSERT INTO `DS_TestSectionType` (`id`, `name`, `value`, `position`) VALUES\r\n                (13, 'QTI item initialization', '13', 13),\r\n                (14, 'QTI item response processing', '14', 14);\r\n                ";
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => 1, "param" => $sql));
             }
             Setting::set_setting("version", "3.7.3");
             return json_encode(array("result" => 0, "param" => "3.7.3"));
         }
     }
     if (Ini::does_patch_apply("3.7.4", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.7.4");
         } else {
             //Test - add open field
             $sql = "SHOW COLUMNS FROM `Test` WHERE `Field`='open'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `Test` ADD `open` tinyint(1) NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             Setting::set_setting("version", "3.7.4");
             return json_encode(array("result" => 0, "param" => "3.7.4"));
         }
     }
     if (Ini::does_patch_apply("3.7.5", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.7.5");
         } else {
             //TestSectionValue - custom section values
             $ts = TestSection::from_property(array("TestSectionType_id" => DS_TestSectionType::CUSTOM));
             foreach ($ts as $custom) {
                 $value = TestSectionValue::from_property(array("TestSection_id" => $custom->id, "index" => 0), false);
                 $custom->delete_values();
                 $value->id = 0;
                 $value->mysql_save();
                 $value->id = 0;
                 $value->index = 1;
                 $value->value = 0;
                 $value->mysql_save();
                 $value->id = 0;
                 $value->index = 2;
                 $value->value = 0;
                 $value->mysql_save();
             }
             Setting::set_setting("version", "3.7.5");
             return json_encode(array("result" => 0, "param" => "3.7.5"));
         }
     }
     if (Ini::does_patch_apply("3.7.6", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.7.6");
         } else {
             //TestSectionValue - test section values
             $ts = TestSection::from_property(array("TestSectionType_id" => DS_TestSectionType::TEST));
             foreach ($ts as $custom) {
                 $value = TestSectionValue::from_property(array("TestSection_id" => $custom->id, "index" => 0), false);
                 $custom->delete_values();
                 $value->id = 0;
                 $value->mysql_save();
                 $value->id = 0;
                 $value->index = 1;
                 $value->value = 0;
                 $value->mysql_save();
                 $value->id = 0;
                 $value->index = 2;
                 $value->value = 0;
                 $value->mysql_save();
             }
             Setting::set_setting("version", "3.7.6");
             return json_encode(array("result" => 0, "param" => "3.7.6"));
         }
     }
     if (Ini::does_patch_apply("3.8.6", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.8.6");
         } else {
             //Test - add loader_Template_id field
             $sql = "SHOW COLUMNS FROM `Test` WHERE `Field`='loader_Template_id'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `Test` ADD `loader_Template_id` bigint(20) NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //Template - add effect_show field
             $sql = "SHOW COLUMNS FROM `Template` WHERE `Field`='effect_show'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `Template` ADD `effect_show` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //Template - add effect_hide field
             $sql = "SHOW COLUMNS FROM `Template` WHERE `Field`='effect_hide'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `Template` ADD `effect_hide` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //Template - add effect_show_options field
             $sql = "SHOW COLUMNS FROM `Template` WHERE `Field`='effect_show_options'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `Template` ADD `effect_show_options` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //Template - add effect_hide_options field
             $sql = "SHOW COLUMNS FROM `Template` WHERE `Field`='effect_hide_options'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `Template` ADD `effect_hide_options` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             ///
             //TestTemplate - add effect_show field
             $sql = "SHOW COLUMNS FROM `TestTemplate` WHERE `Field`='effect_show'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestTemplate` ADD `effect_show` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestTemplate - add effect_hide field
             $sql = "SHOW COLUMNS FROM `TestTemplate` WHERE `Field`='effect_hide'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestTemplate` ADD `effect_hide` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestTemplate - add effect_show_options field
             $sql = "SHOW COLUMNS FROM `TestTemplate` WHERE `Field`='effect_show_options'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestTemplate` ADD `effect_show_options` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestTemplate - add effect_hide_options field
             $sql = "SHOW COLUMNS FROM `TestTemplate` WHERE `Field`='effect_hide_options'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestTemplate` ADD `effect_hide_options` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             ///
             //TestSession - add effect_show field
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='effect_show'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `effect_show` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestSession - add effect_hide field
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='effect_hide'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `effect_hide` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestSession - add effect_show_options field
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='effect_show_options'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `effect_show_options` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestSession - add effect_hide_options field
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='effect_hide_options'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `effect_hide_options` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             ///
             //TestSession - add loader_effect_show field
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='loader_effect_show'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `loader_effect_show` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestSession - add loader_effect_hide field
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='loader_effect_hide'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `loader_effect_hide` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestSession - add loader_effect_show_options field
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='loader_effect_show_options'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `loader_effect_show_options` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestSession - add loader_effect_hide_options field
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='loader_effect_hide_options'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `loader_effect_hide_options` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestSession - add loader_Template_id field
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='loader_Template_id'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `loader_Template_id` bigint(20) NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestSession - add loader_HTML field
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='loader_HTML'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `loader_HTML` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TestSession - add loader_head field
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='loader_head'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TestSession` ADD `loader_head` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             Setting::set_setting("version", "3.8.6");
             return json_encode(array("result" => 0, "param" => "3.8.6"));
         }
         $repopulate_TestTemplate = true;
     }
     if (Ini::does_patch_apply("3.8.7", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.8.7");
         } else {
             //TableColumn - add type field
             $sql = "SHOW COLUMNS FROM `TableColumn` WHERE `Field`='type'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TableColumn` ADD `type` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TableColumn - add length field
             $sql = "SHOW COLUMNS FROM `TableColumn` WHERE `Field`='length'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TableColumn` ADD `length` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TableColumn - add default_value field
             $sql = "SHOW COLUMNS FROM `TableColumn` WHERE `Field`='default_value'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TableColumn` ADD `default_value` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TableColumn - add attributes field
             $sql = "SHOW COLUMNS FROM `TableColumn` WHERE `Field`='attributes'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TableColumn` ADD `attributes` text NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TableColumn - add null field
             $sql = "SHOW COLUMNS FROM `TableColumn` WHERE `Field`='null'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TableColumn` ADD `null` tinyint(1) NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TableColumn - add auto_increment field
             $sql = "SHOW COLUMNS FROM `TableColumn` WHERE `Field`='auto_increment'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) == 0) {
                 $sql = "ALTER TABLE `TableColumn` ADD `auto_increment` tinyint(1) NOT NULL;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             //TableColumn - fill type column text
             $sql = "UPDATE `TableColumn` SET `type`='text' WHERE `TableColumnType_id`=1 OR `TableColumnType_id`=4";
             $z = mysql_query($sql);
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => 1, "param" => $sql));
             }
             //TableColumn - fill type column integer
             $sql = "UPDATE `TableColumn` SET `type`='bigint' WHERE `TableColumnType_id`=2";
             $z = mysql_query($sql);
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => 1, "param" => $sql));
             }
             //TableColumn - fill type column double
             $sql = "UPDATE `TableColumn` SET `type`='double' WHERE `TableColumnType_id`=3";
             $z = mysql_query($sql);
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => 1, "param" => $sql));
             }
             //TableColumn - remove DS_TableColumnType_id
             $sql = "SHOW COLUMNS FROM `TableColumn` WHERE `Field`='TableColumnType_id'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) > 0) {
                 $sql = "ALTER TABLE `TableColumn` DROP `TableColumnType_id`;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             Setting::set_setting("version", "3.8.7");
             return json_encode(array("result" => 0, "param" => "3.8.7"));
         }
     }
     if (Ini::does_patch_apply("3.8.8", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.8.8");
         } else {
             Setting::set_setting("version", "3.8.8");
             return json_encode(array("result" => 0, "param" => "3.8.8"));
         }
         $recalculate_hash = true;
     }
     if (Ini::does_patch_apply("3.9.0", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.9.0");
         } else {
             ///TestSession - removing r_type field
             $sql = "SHOW COLUMNS FROM `TestSession` WHERE `Field`='r_type'";
             $z = mysql_query($sql);
             if (mysql_num_rows($z) > 0) {
                 $sql = "ALTER TABLE `TestSession` DROP `r_type`;";
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => 1, "param" => $sql));
                 }
             }
             Setting::set_setting("version", "3.9.0");
             return json_encode(array("result" => 0, "param" => "3.9.0"));
         }
     }
     if (Ini::does_patch_apply("3.9.2", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "3.9.2");
         } else {
             //DS_TestSectionType - adding two new loop sections
             $sql = "INSERT INTO `DS_TestSectionType` (`id`, `name`, `value`, `position`) VALUES \r\n                    (15, 'FOR loop', '15', 15),\r\n                    (16, 'WHILE loop', '16', 16)";
             $z = mysql_query($sql);
             Setting::set_setting("version", "3.9.2");
             return json_encode(array("result" => 0, "param" => "3.9.2"));
         }
     }
     if ($simulate) {
         return json_encode(array("versions" => $versions_to_update, "validate_column_names" => $validate_column_names, "repopulate_TestTemplate" => $repopulate_TestTemplate, "recalculate_hash" => $recalculate_hash, "create_db" => self::create_db_structure(true)));
     }
     return json_encode(array("result" => 2));
 }
Example #9
0
 public static function calculate_all_xml_hashes()
 {
     //Table - calculate xml_hash
     $sql = sprintf("SELECT `id` FROM `Table`");
     $z = mysql_query($sql);
     while ($r = mysql_fetch_array($z)) {
         set_time_limit(0);
         $obj = Table::from_mysql_id($r[0]);
         $obj->xml_hash = $obj->calculate_xml_hash();
         $obj->mysql_save();
     }
     //Template - calculate xml_hash
     $sql = sprintf("SELECT `id` FROM `Template`");
     $z = mysql_query($sql);
     while ($r = mysql_fetch_array($z)) {
         set_time_limit(0);
         $obj = Template::from_mysql_id($r[0]);
         $obj->xml_hash = $obj->calculate_xml_hash();
         $obj->mysql_save();
     }
     //QTIAssessmentItem - calculate xml_hash
     $sql = sprintf("SELECT `id` FROM `QTIAssessmentItem`");
     $z = mysql_query($sql);
     while ($r = mysql_fetch_array($z)) {
         set_time_limit(0);
         $obj = QTIAssessmentItem::from_mysql_id($r[0]);
         $obj->xml_hash = $obj->calculate_xml_hash();
         $obj->mysql_save();
     }
     //Test - calculate xml_hash
     $sql = sprintf("SELECT `id` FROM `Test`");
     $z = mysql_query($sql);
     while ($r = mysql_fetch_array($z)) {
         set_time_limit(0);
         $obj = Test::from_mysql_id($r[0]);
         $obj->xml_hash = $obj->calculate_xml_hash();
         $obj->mysql_save();
     }
 }