Exemplo n.º 1
0
 public function importgroupsAction()
 {
     $fieldID = $this->_request->getParam('f');
     if ($fieldID) {
         $user = Zend_Auth::getInstance()->getStorage()->read();
         $groups_DB = new Application_Model_DbTable_Group();
         $students_DB = new Application_Model_DbTable_StudentsInField();
         $groups = $groups_DB->getAll($user->ganID, $fieldID);
         foreach ($groups as $g) {
             $new_group = array('name' => $g['name'], 'color' => $g['color'], 'ganID' => $user->ganID, 'fieldID' => $_SESSION['Default']['field']);
             try {
                 $group_id = $groups_DB->insert($new_group);
             } catch (Exception $ex) {
                 die(json_encode(array('status' => 'danger', 'msg' => $ex->getMessage())));
             }
             $students = $students_DB->getAll($g['groupID']);
             foreach ($students as $s) {
                 $new_student = array('studentID' => $s['studentID'], 'fieldID' => $_SESSION['Default']['field'], 'groupID' => $group_id);
                 try {
                     $student_id = $students_DB->insert($new_student);
                 } catch (Exception $ex) {
                     die(json_encode(array('status' => 'danger', 'msg' => $ex->getMessage())));
                 }
             }
         }
         $this->_redirect("/managegroups");
     } else {
         $fields_DB = new Application_Model_DbTable_Field();
         $fields = $fields_DB->getAll();
         $this->view->fields = $fields;
         $this->view->fieldID = $_SESSION['Default']['field'];
     }
 }
 public function recommend()
 {
     $group_id = $this->_request->getParam('g');
     $plans_DB = new Application_Model_DbTable_Planing();
     $last_plan = $plans_DB->getLastPlan($group_id, $_SESSION['Default']['field']);
     if ($last_plan) {
         $target_id = $last_plan['goal_id'];
         $game_id = $last_plan['game_id'];
         $related_plan_id = $last_plan['planID'];
         $targets_DB = new Application_Model_DbTable_Target();
         $target_level = $targets_DB->getGoalLevel($target_id);
         settype($target_level, "integer");
         $records_DB = new Application_Model_DbTable_Records();
         $records = $records_DB->getAll($group_id, $game_id);
         $count_1 = 0;
         $count_2 = 0;
         $count_3 = 0;
         $count_4 = 0;
         foreach ($records as $r) {
             $grade = $r['grade_value'];
             settype($grade, "integer");
             if ($grade == 1) {
                 $count_1++;
             } else {
                 if ($grade == 2) {
                     $count_2++;
                 } else {
                     if ($grade == 3) {
                         $count_3++;
                     } else {
                         if ($grade == 4) {
                             $count_4++;
                         }
                     }
                 }
             }
         }
         $num_of_students = count($records);
         $continue_childrenless = false;
         $students_DB = new Application_Model_DbTable_StudentsInField();
         $real_num_of_students = count($students_DB->getAll($group_id));
         if ($num_of_students < $real_num_of_students) {
             $continue_childrenless = true;
         }
         $num_of_levels = count($targets_DB->getLevels());
         //current target
         $recommended_target = $target_id;
         $recommendation = $this->lang->_('RECOMMEND_CURRENT_LEVEL');
         if ($num_of_students > 2 && $count_1 >= $num_of_students / 2 || $num_of_students == 1 && $count_1 == 1 || $num_of_students == 2 && ($count_1 == 2 || $count_1 == 1 && $count_2 == 1)) {
             //return to a game from prev goal
             if ($target_level > 1) {
                 $target_level = $target_level - 1;
                 $recommended_target = $targets_DB->getAllByLevel($target_level, $_SESSION['Default']['field']);
                 $recommendation = $this->lang->_('RECOMMEND_PREV_LEVEL');
             }
         } else {
             if ($num_of_students > 2 && $count_4 >= $num_of_students - 1 || $num_of_students == 1 && $count_4 == 1 || $num_of_students == 2 && ($count_4 == 2 || $count_4 == 1 && $count_3 == 1)) {
                 $unlearned_targets = $targets_DB->getUnlearnedInLevel($target_level, $group_id, $_SESSION['Default']['field']);
                 //next target in current level
                 if (count($unlearned_targets) > 0) {
                     $recommended_target = $unlearned_targets[0]['goalID'];
                     $recommendation = $this->lang->_('RECOMMEND_NEXT_LEVEL');
                 } else {
                     if ($target_level < $num_of_levels) {
                         $target_level = $target_level + 1;
                         $recommended_target = $targets_DB->getAllByLevel($target_level, $_SESSION['Default']['field']);
                         $recommendation = $this->lang->_('RECOMMEND_NEXT_LEVEL');
                     }
                 }
             }
         }
         $games_DB = new Application_Model_DbTable_Game();
         settype($recommended_target, "integer");
         $unplayed_games = $games_DB->getGamesNotPlayed($group_id, $recommended_target);
         //check if all games were played
         if (count($unplayed_games) == 0) {
             $recommended_game = $games_DB->getRandomGame($recommended_target);
         } else {
             $recommended_game = $unplayed_games[0];
         }
         $recommended_target_name = $targets_DB->getGoalName($recommended_target);
         $this->view->recommended_target = $recommended_target_name;
         $this->view->recommendation = $recommendation;
         $this->view->recommended_game = $recommended_game['name'];
         //insert to DB as last plan
         $plan_DB = new Application_Model_DbTable_Planing();
         $new_plan = array('groupID' => $group_id, 'gameID' => $recommended_game['gameID'], 'date' => date('Y-m-d H:i:s'), 'recommendation' => $recommendation, 'relatedPlanID' => $related_plan_id, 'fieldID' => $_SESSION['Default']['field']);
         try {
             $plan_id = $plan_DB->insert($new_plan);
         } catch (Exception $ex) {
             die(json_encode(array('status' => 'danger', 'msg' => $this->lang->_('FAILED_DOC'))));
         }
         //return json to ajax
         $this->getHelper('Layout')->disableLayout();
         $this->getHelper('ViewRenderer')->setNoRender();
         echo json_encode(array('recommendation' => $recommendation, 'recommended_game' => $recommended_game['name'], 'recommended_target' => $recommended_target_name, 'continue_childrenless' => $continue_childrenless));
     }
 }