Пример #1
0
 /**
  * Given a $json goal, creates a new instance of NelioABGoal and adds it to $exp.
  *
  * @param NelioABExperiment $exp  The experiment to which the goal has to be added.
  * @param object            $json An AppEngine JSON goal.
  *
  * @return void
  *
  * @since 1.4.0
  */
 public static function load_goal_from_json($exp, $json)
 {
     if (isset($json->kind)) {
         switch ($json->kind) {
             case NelioABGoal::ALTERNATIVE_EXPERIMENT_GOAL_STR:
                 require_once NELIOAB_MODELS_DIR . '/goals/alternative-experiment-goal.php';
                 $goal = NelioABAltExpGoal::decode_from_appengine($exp, $json);
                 $exp->add_goal($goal);
                 break;
         }
     }
 }
 protected function do_build()
 {
     // Check settings
     require_once NELIOAB_ADMIN_DIR . '/error-controller.php';
     $error = NelioABErrorController::build_error_page_on_invalid_settings();
     if ($error) {
         return;
     }
     // We recover the experiment (if any)
     // ----------------------------------------------
     global $nelioab_admin_controller;
     $experiment = NULL;
     $other_names = array();
     if (!empty($nelioab_admin_controller->data)) {
         $experiment = $nelioab_admin_controller->data;
     } else {
         $experiment = new NelioABThemeAlternativeExperiment(-time());
         $experiment->clear();
     }
     // ...and we also recover other experiment names (if any)
     if (isset($_POST['other_names'])) {
         $other_names = json_decode(urldecode($_POST['other_names']));
     } else {
         foreach (NelioABExperimentsManager::get_experiments() as $aux) {
             if ($aux->get_id() != $experiment->get_id()) {
                 array_push($other_names, $aux->get_name());
             }
         }
     }
     // Checking whether there is more than one theme
     // available
     // ---------------------------------------------------
     $themes = wp_get_themes();
     usort($themes, array($this, 'sort_themes_alphabetically'));
     if (count($themes) < 2) {
         require_once NELIOAB_ADMIN_DIR . '/views/errors/message-page.php';
         $view = new NelioABMessagePage(__('There is only one theme available', 'nelioab'), __('Please, install one or more themes to create an experiment of this type.', 'nelioab'));
         return $view;
     }
     // If everything is OK, we keep going!
     // ---------------------------------------------------
     $current_theme = wp_get_theme();
     $current_theme_id = $current_theme['Stylesheet'];
     $current_theme_name = $current_theme->offsetGet('Title');
     // We select the alternatives
     $experiment->add_selected_theme($current_theme_id, $current_theme_name);
     if (isset($_POST['nelioab_selected_themes'])) {
         $selected_themes = json_decode(urldecode($_POST['nelioab_selected_themes']));
         if (is_array($selected_themes)) {
             foreach ($selected_themes as $theme) {
                 if (isset($theme->isSelected) && $theme->isSelected) {
                     $experiment->add_selected_theme($theme->value, $theme->name);
                 }
             }
         }
     } else {
         $ori = $experiment->get_original();
         if ($ori) {
             $experiment->add_selected_theme($ori->get_value(), $ori->get_name());
         }
         foreach ($experiment->get_alternatives() as $alt) {
             $experiment->add_selected_theme($alt->get_value(), $alt->get_name());
         }
     }
     if (isset($_POST['nelioab_appspot_ids'])) {
         $experiment->set_appspot_ids(json_decode(urldecode($_POST['nelioab_appspot_ids'])));
     } else {
         $experiment->set_appspot_ids($experiment->get_appspot_ids());
     }
     // Creating the view
     $view = $this->create_view();
     // Experiment information
     $view->set_basic_info($experiment->get_id(), $experiment->get_name(), $experiment->get_description(), $experiment->get_finalization_mode(), $experiment->get_finalization_value());
     // Experiment alternatives
     $view->set_selected_themes($experiment->get_selected_themes());
     $view->set_appspot_ids($experiment->get_appspot_ids());
     $view->set_current_theme($current_theme_id, $current_theme_name, $current_theme->get_screenshot(), $current_theme->offsetGet('Author'));
     foreach ($themes as $theme) {
         $id = $theme['Stylesheet'];
         if ($id == $current_theme_id) {
             continue;
         }
         $view->add_theme($id, $theme->offsetGet('Title'), $theme->get_screenshot(), $theme->offsetGet('Author'), $experiment->is_theme_selected($id));
     }
     // Goals
     $goals = $experiment->get_goals();
     foreach ($goals as $goal) {
         $view->add_goal($goal->json4js());
     }
     if (count($goals) == 0) {
         $new_goal = new NelioABAltExpGoal($experiment);
         $new_goal->set_name(__('Default', 'nelioab'));
         $view->add_goal($new_goal->json4js());
     }
     return $view;
 }
 protected function do_build()
 {
     $title = __('Edit Experiment', 'nelioab');
     // Check settings
     require_once NELIOAB_ADMIN_DIR . '/error-controller.php';
     $error = NelioABErrorController::build_error_page_on_invalid_settings();
     if ($error) {
         return;
     }
     // We recover the experiment (if any)
     // ----------------------------------------------
     global $nelioab_admin_controller;
     $experiment = NULL;
     $other_names = array();
     if (!empty($nelioab_admin_controller->data)) {
         $experiment = $nelioab_admin_controller->data;
     } else {
         $experiment = new NelioABWidgetAlternativeExperiment(-time());
         $experiment->clear();
     }
     // ...and we also recover other experiment names (if any)
     if (isset($_POST['other_names'])) {
         $other_names = json_decode(urldecode($_POST['other_names']));
     } else {
         foreach (NelioABExperimentsManager::get_experiments() as $aux) {
             if ($aux->get_id() != $experiment->get_id()) {
                 array_push($other_names, $aux->get_name());
             }
         }
     }
     // If everything is OK, we keep going!
     // ---------------------------------------------------
     // Creating the view
     $view = $this->create_view();
     // Experiment information
     $view->set_basic_info($experiment->get_id(), $experiment->get_name(), $experiment->get_description(), $experiment->get_finalization_mode(), $experiment->get_finalization_value());
     // Experiment alternatives
     $view->set_alternatives($experiment->get_json4js_alternatives());
     // Goals
     $goals = $experiment->get_goals();
     foreach ($goals as $goal) {
         $view->add_goal($goal->json4js());
     }
     if (count($goals) == 0) {
         $new_goal = new NelioABAltExpGoal($experiment);
         $new_goal->set_name(__('Default', 'nelioab'));
         $view->add_goal($new_goal->json4js());
     }
     return $view;
 }
 /**
  * PHPDOC
  *
  * @param object            $json_goal PHPDOC
  * @param NelioABExperiment $exp       PHPDOC
  *
  * @return NelioABAltExpGoal PHPDOC
  *
  * @since PHPDOC
  */
 public static function build_goal_using_json4js($json_goal, $exp)
 {
     $goal = new NelioABAltExpGoal($exp);
     // If the goal was new, but it was also deleted, do nothing...
     if (isset($json_goal->wasDeleted) && $json_goal->wasDeleted) {
         if ($json_goal->id < 0) {
             return false;
         } else {
             $goal->set_to_be_deleted(true);
         }
     }
     $goal->set_id($json_goal->id);
     $goal->set_name($json_goal->name);
     if (isset($json_goal->benefit) && !empty($json_goal->benefit)) {
         $goal->set_benefit($json_goal->benefit);
     } else {
         $goal->set_benefit(NelioABSettings::get_def_conv_value());
     }
     foreach ($json_goal->actions as $json_action) {
         $action = NelioABAction::build_action_using_json4js($json_action);
         if ($action) {
             $goal->add_action($action);
         }
     }
     return $goal;
 }
 protected function compose_basic_alt_exp_using_post_data($exp)
 {
     $exp->set_name(stripslashes($_POST['exp_name']));
     $exp->set_description(stripslashes($_POST['exp_descr']));
     if (isset($_POST['exp_finalization_mode'])) {
         $exp->set_finalization_mode($_POST['exp_finalization_mode']);
     }
     if (isset($_POST['exp_finalization_value'])) {
         $exp->set_finalization_value($_POST['exp_finalization_value']);
     }
     if (isset($_POST['nelioab_alternatives'])) {
         $alts = stripslashes($_POST['nelioab_alternatives']);
         $alts = json_decode(urldecode($alts));
         $exp->load_json4js_alternatives($alts);
     }
     if (isset($_POST['nelioab_goals'])) {
         $goals = json_decode(urldecode($_POST['nelioab_goals']));
         if (count($goals) > 0) {
             foreach ($goals as $json_goal) {
                 $goal = NelioABAltExpGoal::build_goal_using_json4js($json_goal, $exp);
                 if ($goal) {
                     $exp->add_goal($goal);
                 }
             }
             $goals = $exp->get_goals();
             $main_goal = $goals[0];
             $main_goal->set_as_main_goal(true);
         }
     }
     return $exp;
 }