public function mysql_save_from_post($post)
 {
     $post['oid'] = parent::mysql_save_from_post($post);
     $obj = self::from_mysql_id($post['oid']);
     if (array_key_exists('ids', $post) && array_key_exists('values', $post) && array_key_exists('rws', $post)) {
         for ($i = 0; $i < count($post['ids']); $i++) {
             $id = $post['ids'][$i];
             $val = $post['values'][$i];
             $rw = $post['rws'][$i];
             if ($id != "" && $val != "" && $rw != "") {
                 $right = UserTypeRight::from_property(array("UserType_id" => $obj->id, "Module_id" => $id), false);
                 if ($right == null) {
                     $right = new UserTypeRight();
                     $right->UserType_id = $obj->id;
                     $right->Module_id = $id;
                 }
                 if ($rw == "r") {
                     $right->read = $val;
                 }
                 if ($rw == "w") {
                     $right->write = $val;
                 }
                 if ($rw == "o") {
                     $right->ownership = $val;
                 }
                 $right->mysql_save();
             }
         }
     }
     return $post['oid'];
 }
 public function mysql_save_from_post($post)
 {
     $lid = parent::mysql_save_from_post($post);
     if ($this->id != 0) {
         $this->delete_object_links(CustomSectionVariable::get_mysql_table());
         $i = 0;
     }
     if (array_key_exists("parameters", $post)) {
         foreach ($post["parameters"] as $param) {
             $p = json_decode($param);
             $var = new CustomSectionVariable();
             $var->description = $p->description;
             $var->name = $p->name;
             $var->index = $i;
             $var->type = 0;
             $var->CustomSection_id = $lid;
             $var->mysql_save();
             $i++;
         }
     }
     if (array_key_exists("returns", $post)) {
         foreach ($post["returns"] as $ret) {
             $r = json_decode($ret);
             $var = new CustomSectionVariable();
             $var->description = $r->description;
             $var->name = $r->name;
             $var->index = $i;
             $var->type = 1;
             $var->CustomSection_id = $lid;
             $var->mysql_save();
             $i++;
         }
     }
     $obj = static::from_mysql_id($lid);
     if ($obj != null) {
         $xml_hash = $obj->calculate_xml_hash();
         $obj->xml_hash = $xml_hash;
         $obj->mysql_save();
     }
     return $lid;
 }
 public function mysql_save_from_post($post)
 {
     $lid = parent::mysql_save_from_post($post);
     $obj = static::from_mysql_id($lid);
     if ($obj != null) {
         $xml_hash = $obj->calculate_xml_hash();
         $obj->xml_hash = $xml_hash;
         $obj->mysql_save();
     }
     return $lid;
 }
Exemple #4
0
 public static function get_list_columns()
 {
     $cols = parent::get_list_columns();
     array_push($cols, array("name" => Language::string(488), "property" => "format_open", "searchable" => true, "sortable" => true, "type" => "string", "groupable" => true, "width" => 120, "show" => true));
     array_push($cols, array("name" => Language::string(335), "property" => "session_count", "searchable" => true, "sortable" => true, "type" => "number", "groupable" => false, "width" => 120, "show" => true));
     return $cols;
 }
 public function __construct($params = array())
 {
     $this->name = Language::string(79);
     parent::__construct($params);
 }
 public function mysql_save_from_post($post)
 {
     $lid = parent::mysql_save_from_post($post);
     $obj = static::from_mysql_id($lid);
     if ($obj != null) {
         $xml_hash = $obj->calculate_xml_hash();
         $obj->xml_hash = $xml_hash;
         $validation = json_decode($obj->validate());
         if ($validation->result == 0) {
             $obj->ini_r_code = $obj->get_QTI_ini_R_code();
             $obj->response_proc_r_code = $obj->get_response_processing_R_code();
         }
         $obj->mysql_save();
     }
     return $lid;
 }
 public function mysql_delete()
 {
     $this->delete_object_links(TestTemplate::get_mysql_table());
     parent::mysql_delete();
 }
Exemple #8
0
 public function mysql_save_from_post($post)
 {
     $is_new = $this->id == 0;
     if ($is_new) {
         if (!Table::create_new_mysql_table($post['name'])) {
             return json_encode(array("result" => -6, "message" => mysql_error()));
         }
     } else {
         if ($this->name != $post['name']) {
             if (!$this->rename_mysql_table($post['name'])) {
                 return json_encode(array("result" => -6, "message" => mysql_error()));
             }
         }
     }
     $lid = parent::mysql_save_from_post($post);
     $obj = Table::from_mysql_id($lid);
     if (array_key_exists("deleteData", $post)) {
         if ($post["deleteData"] == "*") {
             $sql = sprintf("DELETE FROM `%s`", mysql_real_escape_string($obj->name));
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => -6, "message" => mysql_error()));
             }
         } else {
             $rows = json_decode($post["deleteData"]);
             foreach ($rows as $row) {
                 $sql = sprintf("DELETE FROM `%s` WHERE id='%s'", mysql_real_escape_string($obj->name), mysql_real_escape_string($row));
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => -6, "message" => mysql_error()));
                 }
             }
         }
     }
     if (array_key_exists("deleteIndexes", $post)) {
         $indexes = json_decode($post["deleteIndexes"]);
         foreach ($indexes as $index) {
             $sql = sprintf("DROP INDEX `%s` ON `%s`", mysql_real_escape_string($index), mysql_real_escape_string($obj->name));
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => -6, "message" => mysql_error()));
             }
         }
     }
     if (array_key_exists("deleteColumns", $post)) {
         $columns = json_decode($post["deleteColumns"]);
         foreach ($columns as $column) {
             $sql = sprintf("ALTER TABLE `%s` DROP COLUMN `%s`", mysql_real_escape_string($obj->name), mysql_real_escape_string($column));
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => -6, "message" => mysql_error()));
             }
         }
     }
     if (array_key_exists("updateColumns", $post)) {
         $columns = json_decode($post["updateColumns"]);
         foreach ($columns as $column) {
             $col = TableColumn::from_ui($column);
             if ($column->id != "") {
                 $sql = sprintf("ALTER TABLE `%s` CHANGE COLUMN `%s` `%s` %s", mysql_real_escape_string($obj->name), mysql_real_escape_string($column->id), mysql_real_escape_string($column->name), $col->get_definition());
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => -6, "message" => mysql_error()));
                 }
             } else {
                 $sql = sprintf("ALTER TABLE `%s` ADD COLUMN `%s` %s", mysql_real_escape_string($obj->name), mysql_real_escape_string($column->name), $col->get_definition());
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => -6, "message" => mysql_error()));
                 }
             }
         }
     }
     if (array_key_exists("updateIndexes", $post)) {
         $indexes = json_decode($post["updateIndexes"]);
         foreach ($indexes as $index) {
             $ind = TableIndex::from_ui($index);
             if ($index->id != "") {
                 $sql = sprintf("ALTER TABLE `%s` DROP INDEX `%s`", mysql_real_escape_string($obj->name), mysql_real_escape_string($index->id));
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => -6, "message" => mysql_error()));
                 }
             }
             $sql = sprintf("ALTER TABLE `%s` ADD %s", mysql_real_escape_string($obj->name), $ind->get_definition());
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => -6, "message" => mysql_error()));
             }
         }
     }
     $columns = $this->get_columns();
     if (array_key_exists("updateData", $post)) {
         $rows = json_decode($post["updateData"], true);
         foreach ($rows as $row) {
             $set = "";
             foreach ($row as $k => $v) {
                 if ($k == "id") {
                     continue;
                 }
                 if ($set != "") {
                     $set .= ",";
                 }
                 if ($v == "") {
                     $nullable = true;
                     foreach ($columns as $col) {
                         if ($col->name == $k) {
                             $nullable = $col->null;
                         }
                     }
                     if (!$nullable) {
                         $set .= "`" . mysql_real_escape_string($k) . "`=''";
                     } else {
                         $set .= "`" . mysql_real_escape_string($k) . "`=NULL";
                     }
                 } else {
                     $set .= "`" . mysql_real_escape_string($k) . "`='" . mysql_real_escape_string($v) . "'";
                 }
             }
             if ($row["id"] != null) {
                 $sql = sprintf("UPDATE `%s` SET %s WHERE `id`=%s", mysql_real_escape_string($obj->name), $set, mysql_real_escape_string($row["id"]));
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => -6, "message" => mysql_error()));
                 }
             } else {
                 $sql = sprintf("INSERT INTO `%s` SET %s", mysql_real_escape_string($obj->name), $set);
                 if (!mysql_query($sql)) {
                     return json_encode(array("result" => -6, "message" => mysql_error() . " " . $sql));
                 }
             }
         }
     }
     //hash
     if ($obj != null) {
         $xml_hash = $obj->calculate_xml_hash();
         $obj->xml_hash = $xml_hash;
         $obj->mysql_save();
     }
     return $lid;
 }
Exemple #9
0
 public static function get_list()
 {
     $list = parent::get_list();
     $logged_user = User::get_logged_user();
     if ($logged_user != null) {
         if ($logged_user->superuser == 1) {
             return $list;
         } else {
             return array($logged_user);
         }
     }
     return array();
 }
Exemple #10
0
 public static function update_db($simulate = false, $only_recalculate_hash = false, $only_create_db = false)
 {
     require '../Ini.php';
     $ini = new Ini();
     if ($only_create_db) {
         return Ini::create_db_structure();
     }
     if ($only_recalculate_hash) {
         foreach (User::get_all_db() as $db) {
             mysql_select_db($db);
             OModule::calculate_all_xml_hashes();
         }
         return json_encode(array("result" => 0));
     }
     $versions_to_update = array();
     $previous_version = Setting::get_setting("version");
     if ($previous_version == null) {
         $previous_version = Ini::$version;
     }
     $recalculate_hash = false;
     if (Setup::does_patch_apply("4.0.0.beta2", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "4.0.0.beta2");
         } else {
             //RDoc.HTML should be larger
             $sql = "ALTER TABLE `" . Ini::$db_master_name . "`.`RDoc` CHANGE `HTML` `HTML` mediumtext NOT NULL;";
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => 1, "param" => $sql));
             }
             //names should be unique
             $tables = array("Test", "Template", "Table", "QTIAssessmentItem");
             foreach (User::get_all_db() as $db) {
                 foreach ($tables as $table) {
                     $sql = sprintf("SELECT `name` , COUNT(  `name` ) AS `c`\r\n                            FROM  `%s`.`%s` \r\n                            GROUP BY `name` \r\n                            HAVING `c`>1", $db, $table);
                     $z = mysql_query($sql);
                     if (!$z) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                     while ($r = mysql_fetch_array($z)) {
                         $sql = sprintf("UPDATE `%s`.`%s` SET `name`=CONCAT(`name`,'_',`id`) WHERE `name`='%s'", $db, $table, mysql_real_escape_string($r[0]));
                         if (!mysql_query($sql)) {
                             return json_encode(array("result" => 1, "param" => $sql));
                         }
                     }
                     //change object name column to unique
                     $sql = sprintf("ALTER TABLE `%s`.`%s` CHANGE  `name`  `name` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL", $db, $table);
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                     $sql = sprintf("ALTER TABLE  `%s`.`%s` ADD UNIQUE ( `name` )", $db, $table);
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 //add TestSession new columns
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='effect_show'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `effect_show` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='effect_hide'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `effect_hide` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='effect_show_options'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `effect_show_options` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='effect_hide_options'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `effect_hide_options` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_HTML'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_HTML` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_head'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_head` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_effect_show'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_effect_show` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_effect_hide'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_effect_hide` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_effect_show_options'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_effect_show_options` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_effect_hide_options'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_effect_hide_options` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_Template_id'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_Template_id` bigint(20) NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_UserWorkspace_id'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_UserWorkspace_id` bigint(20) NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_Template_id'", $db, Test::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) > 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` DROP COLUMN `loader_Template_id`", $db, Test::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
             }
             Setting::set_setting("version", "4.0.0.beta2");
             return json_encode(array("result" => 0, "param" => "4.0.0.beta2"));
         }
     }
     if (Setup::does_patch_apply("4.0.0.beta4", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "4.0.0.beta4");
         } else {
             foreach (User::get_all_db() as $db) {
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='open'", $db, Test::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) > 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` CHANGE `open` `type` TINYINT(1) NOT NULL", $db, Test::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
             }
             Setting::set_setting("version", "4.0.0.beta4");
             return json_encode(array("result" => 0, "param" => "4.0.0.beta4"));
         }
     }
     if (Setup::does_patch_apply("4.0.0.beta9", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "4.0.0.beta9");
         } else {
             foreach (User::get_all_db() as $db) {
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='resume_declined'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `resume_declined` tinyint(1) NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
             }
             Setting::set_setting("version", "4.0.0.beta9");
             return json_encode(array("result" => 0, "param" => "4.0.0.beta9"));
         }
     }
     if ($simulate) {
         return json_encode(array("versions" => $versions_to_update, "recalculate_hash" => $recalculate_hash, "create_db" => Ini::create_db_structure(true)));
     }
     return json_encode(array("result" => 2));
 }
Exemple #11
0
 public static function get_list_columns()
 {
     $cols = parent::get_list_columns();
     array_push($cols, array("name" => Language::string(173), "property" => "login", "searchable" => true, "sortable" => true, "type" => "string", "groupable" => false, "show" => true));
     array_push($cols, array("name" => Language::string(174), "property" => "email", "searchable" => true, "sortable" => true, "type" => "string", "groupable" => true, "show" => true));
     array_push($cols, array("name" => Language::string(175), "property" => "get_last_login", "searchable" => true, "sortable" => true, "type" => "string", "groupable" => false, "show" => true));
     array_push($cols, array("name" => Language::string(176), "property" => "get_UserGroup_name", "searchable" => true, "sortable" => true, "type" => "string", "groupable" => true, "show" => true));
     array_push($cols, array("name" => Language::string(177), "property" => "get_UserType_name", "searchable" => true, "sortable" => true, "type" => "string", "groupable" => true, "show" => true));
     array_push($cols, array("name" => Language::string(335), "property" => "get_session_count", "searchable" => true, "sortable" => true, "type" => "number", "groupable" => false, "width" => 120, "show" => true));
     for ($i = 0; $i < count($cols); $i++) {
         if ($cols[$i]["property"] == "name") {
             array_splice($cols, $i, 1);
             $i--;
         }
         if ($cols[$i]["property"] == "get_owner_full_name") {
             array_splice($cols, $i, 1);
             $i--;
         }
         if ($cols[$i]["property"] == "get_sharing_name") {
             array_splice($cols, $i, 1);
             $i--;
         }
     }
     return $cols;
 }
Exemple #12
0
 public function mysql_save_from_post($post)
 {
     $simulation = false;
     if (array_key_exists("save_simulation", $post) && $post['save_simulation'] == 1) {
         $simulation = true;
     }
     $lid = parent::mysql_save_from_post($post);
     if (!$simulation) {
         $this->mysql_delete_TableColumn();
     }
     if (array_key_exists("cols", $post)) {
         if ($simulation) {
             $table_name = "`" . self::get_table_prefix() . "_temp_" . $lid . "`";
         } else {
             $table_name = "`" . self::get_table_prefix() . "_" . $lid . "`";
         }
         //table
         if (!$simulation) {
             $sql = "DROP TABLE IF EXISTS " . $table_name . ";";
             mysql_query($sql);
         }
         $sql = "CREATE " . ($simulation ? "TEMPORARY" : "") . " TABLE  " . $table_name . " (";
         $i = 0;
         $timestamp = false;
         foreach ($post['cols'] as $col_json) {
             $col = json_decode($col_json);
             if ($i > 0) {
                 $sql .= ",";
             }
             if (!$timestamp && $col->type == "timestamp") {
                 $timestamp = true;
                 $col->defaultValue = "CURRENT_TIMESTAMP";
                 $col->attributes = "on update current_timestamp";
                 $post['cols'][$i] = json_encode($col);
             }
             $sql .= "`" . $col->name . "` " . TableColumn::get_column_definition($col->type, $col->lengthValues, $col->attributes, $col->nullable, 0, $col->defaultValue);
             $i++;
         }
         $sql .= ") ENGINE = INNODB DEFAULT CHARSET=utf8;";
         if (!mysql_query($sql)) {
             $message = mysql_error();
             return json_encode(array("result" => -6, "message" => $message));
         }
         //indexes
         if (array_key_exists("indexes", $post)) {
             foreach ($post['indexes'] as $index_json) {
                 $index = json_decode($index_json);
                 $columns = explode(",", $index->columns);
                 $cols = "";
                 foreach ($columns as $column) {
                     if ($cols != "") {
                         $cols .= ",";
                     }
                     $cols .= "`" . $column . "`";
                 }
                 $sql = sprintf("ALTER TABLE %s ADD %s(%s)", $table_name, $index->type, $cols);
                 if (!mysql_query($sql)) {
                     $message = mysql_error();
                     return json_encode(array("result" => -6, "message" => $message));
                 }
             }
         }
         //auto increment
         $auto_increment = false;
         foreach ($post['cols'] as $col_json) {
             $col = json_decode($col_json);
             if ($col->auto_increment == 1) {
                 $auto_increment = true;
                 self::$auto_increment_row_comparer_field = $col->name;
                 $sql = sprintf("ALTER TABLE %s CHANGE `%s` `%s` %s", $table_name, $col->name, $col->name, TableColumn::get_column_definition($col->type, $col->lengthValues, $col->attributes, $col->nullable, $col->auto_increment, $col->defaultValue));
                 if (!mysql_query($sql)) {
                     $message = mysql_error();
                     return json_encode(array("result" => -6, "message" => $message));
                 }
             }
         }
         //TableColumn
         $sql = "START TRANSACTION";
         mysql_query($sql);
         $sql = sprintf("INSERT INTO `%s` (`index`,`name`,`Table_id`,`type`, `length`,`attributes`, `null`, `auto_increment`, `default_value`) VALUES ", TableColumn::get_mysql_table());
         $i = 0;
         foreach ($post['cols'] as $col_json) {
             $col = json_decode($col_json);
             if ($i > 0) {
                 $sql .= ",";
             }
             $sql .= "(";
             $sql .= $i + 1 . ",'" . mysql_real_escape_string($col->name) . "'," . $lid . ",'" . mysql_real_escape_string($col->type) . "', '" . mysql_real_escape_string($col->lengthValues) . "', '" . mysql_real_escape_string($col->attributes) . "', " . mysql_real_escape_string($col->nullable) . "," . mysql_real_escape_string($col->auto_increment) . ",'" . mysql_real_escape_string($col->defaultValue) . "'";
             $sql .= ")";
             $i++;
         }
         if (!mysql_query($sql)) {
             $message = mysql_error();
             $sql = "ROLLBACK";
             mysql_query($sql);
             return json_encode(array("result" => -6, "message" => $message));
         }
         //TableIndex
         if (array_key_exists("indexes", $post)) {
             foreach ($post['indexes'] as $index_json) {
                 $index = json_decode($index_json);
                 $columns = explode(",", $index->columns);
                 $ti = new TableIndex();
                 $ti->Table_id = $lid;
                 $ti->type = $index->type;
                 $ti_lid = $ti->mysql_save();
                 $i = 0;
                 foreach ($columns as $col) {
                     $tc = TableColumn::from_property(array("Table_id" => $lid, "name" => $col), false);
                     $tic = new TableIndexColumn();
                     $tic->index = $i;
                     $tic->TableIndex_id = $ti_lid;
                     if ($tc != null) {
                         $tic->TableColumn_id = $tc->id;
                     }
                     $tic->mysql_save();
                     $i++;
                 }
             }
         }
         //data
         if (array_key_exists("rows", $post) && $post['rows'] != null && is_array($post['rows'])) {
             if ($auto_increment) {
                 usort($post['rows'], "self::auto_increment_row_comparer");
             }
             $sql = "INSERT INTO " . $table_name . " (";
             $i = 0;
             foreach ($post['cols'] as $col_json) {
                 if ($i > 0) {
                     $sql .= ",";
                 }
                 $col = json_decode($col_json);
                 $sql .= "`" . $col->name . "`";
                 $i++;
             }
             $sql .= ") VALUES ";
             for ($a = 0; $a < count($post['rows']); $a++) {
                 $row = json_decode($post['rows'][$a]);
                 if ($a > 0) {
                     $sql .= ",";
                 }
                 $sql .= "(";
                 $i = 0;
                 foreach ($post['cols'] as $col_json) {
                     $col = json_decode($col_json);
                     $col_name = $col->name;
                     if ($row->{$col_name} == "" && $col->nullable == 1) {
                         $row->{$col_name} = null;
                     }
                     if ($i > 0) {
                         $sql .= ",";
                     }
                     if ($row->{$col_name} !== null) {
                         $sql .= "'" . mysql_real_escape_string($row->{$col_name}) . "'";
                     } else {
                         $sql .= "NULL";
                     }
                     $i++;
                 }
                 $sql .= ")";
             }
             if (!mysql_query($sql)) {
                 $message = mysql_error();
                 $sql = "ROLLBACK";
                 mysql_query($sql);
                 return json_encode(array("result" => -6, "message" => $message));
             }
         }
     }
     if (!$simulation) {
         $sql = "COMMIT";
         mysql_query($sql);
     } else {
         $sql = "ROLLBACK";
         mysql_query($sql);
     }
     //hash
     $obj = static::from_mysql_id($lid);
     if ($obj != null) {
         $xml_hash = $obj->calculate_xml_hash();
         $obj->xml_hash = $xml_hash;
         $obj->mysql_save();
     }
     return $lid;
 }
Exemple #13
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));
 }
Exemple #14
0
 public function mysql_save_from_post($post)
 {
     if ($post['modify_password'] == 1) {
         $post['md5_password'] = md5($post['password']);
     }
     $post['oid'] = parent::mysql_save_from_post($post);
     if ($this->id == 0 && $post['oid'] != 0) {
         $obj = self::from_mysql_id($post['oid']);
         $obj->Owner_id = $post['oid'];
         $obj->mysql_save();
         if ($post['send_credentials'] == 1) {
             mail($post['email'], "Concerto Platform login credentials.", nl2br($post['welcome']) . "<br/><br/>Your account has been created and you can now log in at: <b>" . Ini::$external_path . "admin</b><br/>Login: <b>" . $post['login'] . "</b><br/>Password: <b>" . $post['password'] . "</b>", "Content-type: text/html; charset=utf-8\r\nFrom: accounts@concerto-platform.org");
         }
     }
     return $post['oid'];
 }