Exemplo n.º 1
0
                             $a_input['comment'] = '';
                             $a_input['state'] = PluginFusioninventoryTaskjoblog::TASK_STARTED;
                             $pfTaskjoblog->add($a_input);
                         }
                     }
                 }
             }
         }
     }
     break;
 case 'setAnswer':
     // example
     // ?action=setAnswer&InformationSource=0x00000000&BIOSVersion=VirtualBox&SystemManufacturer=innotek%20GmbH&uuid=fepjhoug56743h&SystemProductName=VirtualBox&BIOSReleaseDate=12%2F01%2F2006
     $pfTaskjobstate = new PluginFusioninventoryTaskjobstate();
     $pfAgent = new PluginFusioninventoryAgent();
     $jobstate = current($pfTaskjobstate->find("`uniqid`='" . $_GET['uuid'] . "'\n            AND `state`!='" . PluginFusioninventoryTaskjobstate::FINISHED . "'", '', 1));
     if (isset($jobstate['plugin_fusioninventory_agents_id'])) {
         $pfAgent->getFromDB($jobstate['plugin_fusioninventory_agents_id']);
         $computers_id = $pfAgent->fields['computers_id'];
         $a_values = $_GET;
         unset($a_values['action']);
         unset($a_values['uuid']);
         switch ($jobstate['itemtype']) {
             case 'PluginFusioninventoryCollect_Registry':
                 // update registry content
                 $pfCRC = new PluginFusioninventoryCollect_Registry_Content();
                 $pfCRC->updateComputer($computers_id, $a_values, $jobstate['items_id']);
                 $pfTaskjobstate->changeStatus($jobstate['id'], PluginFusioninventoryTaskjobstate::AGENT_HAS_SENT_DATA);
                 if (isset($a_values['_cpt']) && $a_values['_cpt'] == 0) {
                     // it not find the path
                     $pfTaskjobstate->changeStatusFinish($jobstate['id'], $jobstate['items_id'], $jobstate['itemtype'], 1, 'Path not found');
 function prepareTaskjobs($methods = array())
 {
     global $DB;
     $agent = new PluginFusioninventoryAgent();
     $timeslot = new PluginFusioninventoryTimeslot();
     $now = new DateTime();
     //transform methods array into string for database query
     $methods = "'" . implode("','", $methods) . "'";
     $query = implode(" \n", array("SELECT", "     task.`id`, task.`name`,", "     job.`id`, job.`name`, job.`method`, ", "     job.`targets`, job.`actors`", "FROM `glpi_plugin_fusioninventory_taskjobs` job", "LEFT JOIN `glpi_plugin_fusioninventory_tasks` task", "  ON task.`id` = job.`plugin_fusioninventory_tasks_id`", "WHERE task.`is_active` = 1", "AND (", "        (   task.`datetime_start` IS NOT NULL AND task.`datetime_end` IS NULL", "              AND '" . $now->format("Y-m-d H:i:s") . "' >= task.`datetime_start` )", "     OR", "        (   task.`datetime_start` IS NOT NULL AND task.`datetime_end` IS NOT NULL", "              AND '" . $now->format("Y-m-d H:i:s") . "' ", "                    between task.`datetime_start` AND task.`datetime_end` )", "     OR", "        ( task.`datetime_start` IS NULL AND task.`datetime_end` IS NULL )", ")", "AND job.`method` IN (" . $methods . ")", "ORDER BY job.`id`"));
     $query_result = $DB->query($query);
     $results = array();
     if ($query_result) {
         $results = PluginFusioninventoryToolbox::fetchAssocByTable($query_result);
     }
     // Fetch a list of actors to be prepared. We may have the same actors for each job so this
     // part can speed up the process.
     //$actors = array();
     // Set basic elements of jobstates
     $run_base = array('state' => PluginFusioninventoryTaskjobstate::PREPARED);
     $log_base = array('date' => $now->format("Y-m-d H:i:s"), 'state' => PluginFusioninventoryTaskjoblog::TASK_PREPARED, 'comment' => '');
     $jobstate = new PluginFusioninventoryTaskjobstate();
     $joblog = new PluginFusioninventoryTaskjoblog();
     foreach ($results as $index => $result) {
         $actors = importArrayFromDB($result['job']['actors']);
         // Get agents linked to the actors
         $agent_ids = array();
         foreach ($this->getAgentsFromActors($actors) as $agent_id) {
             $agent_ids[$agent_id] = true;
         }
         //Continue with next job if there are no agents found from actors.
         //TODO: This may be good to report this kind of information. We just need to do a list of
         //agent's ids generated by actors like array('actors_type-id' => array( 'agent_0',...).
         //Then the following could be put in the targets foreach loop before looping through
         //agents.
         if (count($agent_ids) == 0) {
             continue;
         }
         $saved_agent_ids = $agent_ids;
         $targets = importArrayFromDB($result['job']['targets']);
         if ($result['job']['method'] == 'networkinventory') {
             $pfNetworkinventory = new PluginFusioninventoryNetworkinventory();
             foreach ($targets as $keyt => $target) {
                 $item_type = key($target);
                 $items_id = current($target);
                 if ($item_type == 'PluginFusioninventoryIPRange') {
                     unset($targets[$keyt]);
                     // In this case get devices of this iprange
                     $deviceList = $pfNetworkinventory->getDevicesOfIPRange($items_id);
                     $targets = array_merge($targets, $deviceList);
                 }
             }
         }
         $limit = 0;
         foreach ($targets as $target) {
             $agent_ids = $saved_agent_ids;
             $item_type = key($target);
             $item_id = current($target);
             $job_id = $result['job']['id'];
             $jobstates_running = $jobstate->find(implode(" \n", array("    `itemtype` = '" . $item_type . "'", "AND `items_id` = " . $item_id, "AND `plugin_fusioninventory_taskjobs_id` = " . $job_id, "AND `state` not in ('" . implode("','", array(PluginFusioninventoryTaskjobstate::FINISHED, PluginFusioninventoryTaskjobstate::IN_ERROR, PluginFusioninventoryTaskjobstate::CANCELLED)) . "')", "AND `plugin_fusioninventory_agents_id` IN (", "'" . implode("','", array_keys($agent_ids)) . "'", ")")));
             // Filter out agents that are already running the targets.
             foreach ($jobstates_running as $jobstate_running) {
                 $jobstate_agent_id = $jobstate_running['plugin_fusioninventory_agents_id'];
                 if (isset($agent_ids[$jobstate_agent_id])) {
                     $agent_ids[$jobstate_agent_id] = false;
                 }
             }
             // Cancel agents prepared but not in $agent_ids (like computer
             // not in dynamic group)
             $jobstates_tocancel = $jobstate->find(implode(" \n", array("    `itemtype` = '" . $item_type . "'", "AND `items_id` = " . $item_id, "AND `plugin_fusioninventory_taskjobs_id` = " . $job_id, "AND `state` not in ('" . implode("','", array(PluginFusioninventoryTaskjobstate::FINISHED, PluginFusioninventoryTaskjobstate::IN_ERROR, PluginFusioninventoryTaskjobstate::CANCELLED)) . "')", "AND `plugin_fusioninventory_agents_id` NOT IN (", "'" . implode("','", array_keys($agent_ids)) . "'", ")")));
             foreach ($jobstates_tocancel as $jobstate_tocancel) {
                 $jobstate->getFromDB($jobstate_tocancel['id']);
                 $jobstate->cancel(__('Device no longer defined in definition of job', 'fusioninventory'));
             }
             foreach ($agent_ids as $agent_id => $agent_not_running) {
                 if ($agent_not_running) {
                     $limit += 1;
                     if ($limit > 500) {
                         $limit = 0;
                         break;
                     }
                     $run = array_merge($run_base, array('itemtype' => $item_type, 'items_id' => $item_id, 'plugin_fusioninventory_taskjobs_id' => $job_id, 'plugin_fusioninventory_agents_id' => $agent_id, 'uniqid' => uniqid()));
                     $run_id = $jobstate->add($run);
                     if ($run_id !== false) {
                         $log = array_merge($log_base, array('plugin_fusioninventory_taskjobstates_id' => $run_id, ''));
                         $joblog->add($log);
                     }
                 }
             }
         }
     }
     return true;
 }
 /**
  * Display short logs
  *
  * @param $taskjobs_id integer id of taskjob
  * @param $veryshort boolean activation to have very very short display
  *
  * @return nothing
  */
 function displayShortLogs($taskjobs_id, $veryshort = 0)
 {
     global $DB, $CFG_GLPI;
     $pfTaskjobstate = new PluginFusioninventoryTaskjobstate();
     echo "<td colspan='2' valign='top'>";
     if ($veryshort == '0') {
         echo "<table width='100%'>";
         echo "<tr class='tab_bg_3'>";
     } else {
         echo "<table>";
         echo "<tr class='tab_bg_1'>";
     }
     $query = "SELECT * FROM `glpi_plugin_fusioninventory_taskjobstates`\n         WHERE `plugin_fusioninventory_taskjobs_id`='" . $taskjobs_id . "'\n         ORDER BY `uniqid` DESC\n         LIMIT 1";
     $result = $DB->query($query);
     $uniqid = 0;
     while ($data = $DB->fetch_array($result)) {
         $uniqid = $data['uniqid'];
     }
     $query = "SELECT `glpi_plugin_fusioninventory_taskjoblogs`.*\n            FROM `glpi_plugin_fusioninventory_taskjoblogs`\n         LEFT JOIN `glpi_plugin_fusioninventory_taskjobstates`\n            ON plugin_fusioninventory_taskjobstates_id = " . "`glpi_plugin_fusioninventory_taskjobstates`.`id`\n         WHERE `uniqid`='" . $uniqid . "'\n         ORDER BY `glpi_plugin_fusioninventory_taskjoblogs`.`id` DESC\n         LIMIT 1";
     $state = 0;
     $date = '';
     $comment = '';
     $taskstates_id = 0;
     $result = $DB->query($query);
     while ($data = $DB->fetch_array($result)) {
         $state = $data['state'];
         $date = $data['date'];
         $comment = $data['comment'];
         $taskstates_id = $data['plugin_fusioninventory_taskjobstates_id'];
     }
     if (strstr($comment, "Merged with")) {
         $state = '7';
     }
     $a_taskjobstates = count($pfTaskjobstate->find("`plugin_fusioninventory_taskjobs_id`='" . $taskjobs_id . "'\n               AND `state` != '3'\n               AND `uniqid`='" . $uniqid . "'"));
     if ($state == '1' or $state == '6' or $state == '7') {
         // not finish
         if ($veryshort == '0') {
             echo "<th>";
             echo "<img src='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/pics/task_running.png'/>";
             echo "</th>";
         }
         echo $this->getDivState($state, 'td');
         echo "<td align='center'>";
         echo " <a href='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/front/taskjoblog.php?sort=1&order=DESC&field[0]=6&" . "searchtype[0]=contains&contains[0]=" . $uniqid . "&" . "itemtype=PluginFusioninventoryTaskjoblog&start=0'>" . __('View logs of this execution', 'fusioninventory') . "</a>";
         echo "<form name='form' method='post' action='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/front/taskjob.form.php'>";
         echo "<input type='hidden' name='taskjobstates_id' value='" . $taskstates_id . "' />";
         echo "<input type='hidden' name='taskjobs_id' value='" . $taskjobs_id . "' />";
         echo '&nbsp;&nbsp;&nbsp;<input name="forceend" value="' . __('Force the end', 'fusioninventory') . '" class="submit" type="submit">';
         Html::closeForm();
         echo "</td>";
         if ($veryshort == '0') {
             echo "</tr>";
             echo "<tr class='tab_bg_3'>";
             echo "<th>";
             echo "<img src='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/pics/task_finished.png'/>";
             echo "</th>";
             echo "<td colspan='2' align='center'>";
             echo " <a href='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/front/taskjoblog.php?sort=1&order=DESC&" . "field[0]=3&searchtype[0]=equals&contains[0]=" . $taskjobs_id . "&" . "itemtype=PluginFusioninventoryTaskjoblog&start=0'>" . __('See all executions', 'fusioninventory') . "</a>";
             echo "</td>";
             echo "</tr>";
         }
     } else {
         // Finish
         if ($veryshort == '0') {
             echo "<th rowspan='2' height='64'>";
             echo "<img src='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/pics/task_finished.png'/>";
             echo "</th>";
         }
         echo $this->getDivState($state, 'td');
         if ($veryshort == '0') {
             echo "<td align='center'>";
         } else {
             echo "<td>";
         }
         if ($taskstates_id == '0') {
             echo __('Last run') . "&nbsp;:&nbsp;" . __('Never');
         } else {
             if ($veryshort == '0') {
                 if ($a_taskjobstates == '0') {
                     echo __('Last run') . " (" . Html::convDateTime($date) . ") : ";
                 }
                 echo "<a href='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/front/taskjoblog.php?field[0]=6&" . "searchtype[0]=contains&contains[0]=" . $uniqid . "&" . "itemtype=PluginFusioninventoryTaskjoblog&start=0'>" . __('View logs of this execution', 'fusioninventory') . "</a>";
             } else {
                 if ($a_taskjobstates == '0') {
                     echo __('Last run') . " :<br/> " . Html::convDateTime($date) . "";
                 }
             }
         }
         if ($a_taskjobstates != '0') {
             echo "<form name='form' method='post' action='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/front/taskjob.form.php'>";
             echo "<input type='hidden' name='taskjobstates_id' value='" . $taskstates_id . "' />";
             echo "<input type='hidden' name='taskjobs_id' value='" . $taskjobs_id . "' />";
             echo '&nbsp;&nbsp;&nbsp;<input name="forceend" value="' . __('Force the end', 'fusioninventory') . '" class="submit" type="submit">';
             Html::closeForm();
         }
         echo "</td>";
         echo "</tr>";
         if ($veryshort == '0') {
             echo "<tr class='tab_bg_3'>";
             echo "<td colspan='2' align='center'>";
             echo " <a href='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/front/taskjoblog.php?field[0]=3&" . "searchtype[0]=equals&contains[0]=" . $taskjobs_id . "&" . "itemtype=PluginFusioninventoryTaskjoblog&start=0'>" . __('See all executions', 'fusioninventory') . "</a>";
             echo "</td>";
             echo "</tr>";
         }
     }
     echo "</table>";
     echo "</td>";
 }
 function prepareRun($taskjob_id, $definitions_filter = NULL)
 {
     global $DB;
     $task = new PluginFusioninventoryTask();
     $job = new PluginFusioninventoryTaskjob();
     $joblog = new PluginFusioninventoryTaskjoblog();
     $jobstate = new PluginFusioninventoryTaskjobstate();
     $agent = new PluginFusioninventoryAgent();
     $agentmodule = new PluginFusioninventoryAgentmodule();
     $job->getFromDB($taskjob_id);
     $task->getFromDB($job->fields['plugin_fusioninventory_tasks_id']);
     $communication = $task->fields['communication'];
     $actions = importArrayFromDB($job->fields['action']);
     $definitions = importArrayFromDB($job->fields['definition']);
     $taskvalid = 0;
     $computers = array();
     foreach ($actions as $action) {
         $itemtype = key($action);
         $items_id = current($action);
         switch ($itemtype) {
             case 'Computer':
                 if ($this->definitionFiltered("Computer", $definitions_filter)) {
                     break;
                 }
                 $computers[] = $items_id;
                 break;
             case 'Group':
                 if ($this->definitionFiltered("Group", $definitions_filter)) {
                     break;
                 }
                 $computer_object = new Computer();
                 //find computers by user associated with this group
                 $group_users = new Group_User();
                 $group = new Group();
                 $group->getFromDB($items_id);
                 $members = array();
                 $computers_a_1 = array();
                 $computers_a_2 = array();
                 //array_keys($group_users->find("groups_id = '$items_id'"));
                 $members = $group_users->getGroupUsers($items_id);
                 foreach ($members as $member) {
                     $computers = $computer_object->find("users_id = '{$member['id']}' " . " AND `is_deleted`='0' AND `is_template`='0'");
                     foreach ($computers as $computer) {
                         $computers_a_1[] = $computer['id'];
                     }
                 }
                 //find computers directly associated with this group
                 $computers = $computer_object->find("groups_id = '{$items_id}' " . " AND `is_deleted`='0' AND `is_template`='0'");
                 foreach ($computers as $computer) {
                     $computers_a_2[] = $computer['id'];
                 }
                 //merge two previous array and deduplicate entries
                 $computers = array_unique(array_merge($computers_a_1, $computers_a_2));
                 break;
             case 'PluginFusioninventoryDeployGroup':
                 $group = new PluginFusioninventoryDeployGroup();
                 $group->getFromDB($items_id);
                 switch ($group->getField('type')) {
                     case 'STATIC':
                         if ($this->definitionFiltered("PluginFusioninventoryDeployGroupStatic", $definitions_filter)) {
                             break;
                         }
                         $query = "SELECT items_id\n                     FROM glpi_plugin_fusioninventory_deploygroups_staticdatas\n                     WHERE groups_id = '{$items_id}'\n                     AND itemtype = 'Computer'";
                         $res = $DB->query($query);
                         while ($row = $DB->fetch_assoc($res)) {
                             $computers[] = $row['items_id'];
                         }
                         break;
                     case 'DYNAMIC':
                         if ($this->definitionFiltered("PluginFusioninventoryDeployGroupDynamic", $definitions_filter)) {
                             break;
                         }
                         //$definitions_filter is NULL = update by crontask !
                         if ($definitions_filter != NULL) {
                             $where = " AND `can_update_group`='1'";
                         } else {
                             $where = "";
                         }
                         $query = "SELECT fields_array\n                     FROM glpi_plugin_fusioninventory_deploygroups_dynamicdatas\n                     WHERE groups_id = '{$items_id}' {$where}\n                     LIMIT 1";
                         $res = $DB->query($query);
                         $row = $DB->fetch_assoc($res);
                         //No dynamic groups have been found : break
                         if ($DB->numrows($res) == 0) {
                             break;
                         }
                         if (isset($_GET)) {
                             $get_tmp = $_GET;
                         }
                         if (isset($_SESSION["glpisearchcount"]['Computer'])) {
                             unset($_SESSION["glpisearchcount"]['Computer']);
                         }
                         if (isset($_SESSION["glpisearchcount2"]['Computer'])) {
                             unset($_SESSION["glpisearchcount2"]['Computer']);
                         }
                         $_GET = importArrayFromDB($row['fields_array']);
                         $_GET["glpisearchcount"] = count($_GET['field']);
                         if (isset($_GET['field2'])) {
                             $_GET["glpisearchcount2"] = count($_GET['field2']);
                         }
                         $pfSearch = new PluginFusioninventorySearch();
                         Search::manageParams('Computer');
                         $glpilist_limit = $_SESSION['glpilist_limit'];
                         $_SESSION['glpilist_limit'] = 999999999;
                         $result = $pfSearch->constructSQL('Computer', $_GET);
                         $_SESSION['glpilist_limit'] = $glpilist_limit;
                         while ($data = $DB->fetch_array($result)) {
                             $computers[] = $data['id'];
                         }
                         if (count($get_tmp) > 0) {
                             $_GET = $get_tmp;
                         }
                         break;
                 }
                 break;
         }
     }
     //Remove duplicatas from array
     //We are using isset for faster processing than array_unique because we might have many
     //entries in this list.
     $tmp_computers = array();
     foreach ($computers as $computer) {
         if (!isset($tmp_computers[$computer])) {
             $tmp_computers[$computer] = 1;
         }
     }
     $computers = array_keys($tmp_computers);
     $c_input = array();
     $c_input['plugin_fusioninventory_taskjobs_id'] = $job->fields['id'];
     $c_input['state'] = 0;
     $c_input['plugin_fusioninventory_agents_id'] = 0;
     $c_input['execution_id'] = $task->fields['execution_id'];
     $package = new PluginFusioninventoryDeployPackage();
     foreach ($computers as $computer_id) {
         //Unique Id match taskjobstatuses for an agent(computer)
         foreach ($definitions as $definition) {
             $uniqid = uniqid();
             $package->getFromDB($definition['PluginFusioninventoryDeployPackage']);
             $c_input['state'] = 0;
             $c_input['itemtype'] = 'PluginFusioninventoryDeployPackage';
             $c_input['items_id'] = $package->fields['id'];
             $c_input['date'] = date("Y-m-d H:i:s");
             $c_input['uniqid'] = $uniqid;
             //get agent for this computer
             $agents_id = $agent->getAgentWithComputerid($computer_id);
             if ($agents_id === FALSE) {
                 $jobstates_id = $jobstate->add($c_input);
                 $jobstate->changeStatusFinish($jobstates_id, 0, '', 1, "No agent found for [[Computer::" . $computer_id . "]]", 0, 0);
             } else {
                 if ($agentmodule->isAgentCanDo('DEPLOY', $agents_id)) {
                     $c_input['plugin_fusioninventory_agents_id'] = $agents_id;
                     $jobstates_running = $jobstate->find(implode(" ", array("    `itemtype` = 'PluginFusioninventoryDeployPackage'", "AND `items_id` = " . $package->fields['id'], "AND `state` <> " . PluginFusioninventoryTaskjobstate::FINISHED, "AND `plugin_fusioninventory_agents_id` = " . $agents_id)));
                     if (count($jobstates_running) == 0) {
                         # Push the agent, in the stack of agent to awake
                         if ($communication == "push") {
                             $_SESSION['glpi_plugin_fusioninventory']['agents'][$agents_id] = 1;
                         }
                         $jobstates_id = $jobstate->add($c_input);
                         //Add log of taskjob
                         $c_input['plugin_fusioninventory_taskjobstates_id'] = $jobstates_id;
                         $c_input['state'] = PluginFusioninventoryTaskjoblog::TASK_PREPARED;
                         $taskvalid++;
                         $joblog->add($c_input);
                         unset($c_input['state']);
                         unset($c_input['plugin_fusioninventory_agents_id']);
                     }
                 }
             }
         }
     }
     if ($taskvalid > 0) {
         $job->fields['status'] = 1;
         $job->update($job->fields);
     } else {
         $job->reinitializeTaskjobs($job->fields['plugin_fusioninventory_tasks_id']);
     }
 }
 function pre_deleteItem()
 {
     global $CFG_GLPI;
     //if task active, delete denied
     if ($this->getField('is_active') == 1) {
         Session::addMessageAfterRedirect(__('This task is active. delete denied', 'fusioninventory'));
         Html::redirect($CFG_GLPI["root_doc"] . "/plugins/fusinvdeploy/front/task.form.php?id=" . $this->getField('id'));
         return FALSE;
     }
     $task_id = $this->getField('id');
     $job = new PluginFusioninventoryTaskjob();
     $status = new PluginFusioninventoryTaskjobstate();
     $log = new PluginFusioninventoryTaskjoblog();
     // clean all sub-tables
     $a_taskjobs = $job->find("`plugin_fusioninventory_tasks_id`='{$task_id}'");
     foreach ($a_taskjobs as $a_taskjob) {
         $a_taskjobstatuss = $status->find("`plugin_fusioninventory_taskjobs_id`='" . $a_taskjob['id'] . "'");
         foreach ($a_taskjobstatuss as $a_taskjobstatus) {
             $a_taskjoblogs = $log->find("`plugin_fusioninventory_taskjobstates_id`='" . $a_taskjobstatus['id'] . "'");
             foreach ($a_taskjoblogs as $a_taskjoblog) {
                 $log->delete($a_taskjoblog, 1);
             }
             $status->delete($a_taskjobstatus, 1);
         }
         $job->delete($a_taskjob, 1);
     }
     return TRUE;
 }
 /**
  * @test
  */
 public function deleteTask()
 {
     global $DB;
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $pfTaskjobState = new PluginFusioninventoryTaskjobstate();
     $pfTaskjoblog = new PluginFusioninventoryTaskjoblog();
     //delete task
     $return = $pfTask->delete(array('id' => self::$tasks_id));
     $this->assertEquals(true, $return);
     //check deletion of job
     $jobs_found = $pfTaskjob->find("id = " . self::$taskjobs_id);
     $this->assertEquals(array(), $jobs_found);
     //check deletion of state
     $states_found = $pfTaskjobState->find("id = " . self::$taskjobstates_id);
     $this->assertEquals(array(), $states_found);
     //check deletion of log
     $logs_found = $pfTaskjoblog->find("id = " . self::$taskjobstates_id);
     $this->assertEquals(array(), $logs_found);
 }
 /**
  * Force end task
  */
 function forceEnd()
 {
     $pfTaskjobstate = new PluginFusioninventoryTaskjobstate();
     $a_taskjobstates = $pfTaskjobstate->find("`plugin_fusioninventory_taskjobs_id`='" . $this->fields["id"] . "'");
     //TODO: in order to avoid too many atomic operations on DB, convert the
     //following into a massive prepared operation (ie. ids in one massive action)
     foreach ($a_taskjobstates as $a_taskjobstate) {
         $pfTaskjobstate->getFromDB($a_taskjobstate['id']);
         if ($a_taskjobstate['state'] != PluginFusioninventoryTaskjobstate::FINISHED) {
             $pfTaskjobstate->changeStatusFinish($a_taskjobstate['id'], 0, '', 1, "Action cancelled by user", 0, 0);
         }
     }
     $this->reinitializeTaskjobs($this->fields['plugin_fusioninventory_tasks_id']);
 }
 function display($options = array())
 {
     global $DB, $CFG_GLPI;
     $pfAgent = new PluginFusioninventoryAgent();
     $pfTaskjobstate = new PluginFusioninventoryTaskjobstate();
     $pfTaskjoblog = new PluginFusioninventoryTaskjoblog();
     $pfStateInventory = new PluginFusioninventoryStateInventory();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $start = 0;
     if (isset($_REQUEST["start"])) {
         $start = $_REQUEST["start"];
     }
     // Total Number of events
     $querycount = "SELECT count(*) AS cpt FROM `glpi_plugin_fusioninventory_taskjobstates`\n         LEFT JOIN `glpi_plugin_fusioninventory_taskjobs`\n            ON `plugin_fusioninventory_taskjobs_id` = `glpi_plugin_fusioninventory_taskjobs`.`id`\n         WHERE `method` = 'networkdiscovery'\n         GROUP BY `uniqid`\n         ORDER BY `uniqid` DESC ";
     $resultcount = $DB->query($querycount);
     $number = $DB->numrows($resultcount);
     // Display the pager
     Html::printPager($start, $number, $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/front/stateinventory.php", '');
     echo "<table class='tab_cadre_fixe'>";
     echo "<tr class='tab_bg_1'>";
     echo "<th>" . __('Unique id', 'fusioninventory') . "</th>";
     echo "<th>" . __('Task job', 'fusioninventory') . "</th>";
     echo "<th>" . __('Agent', 'fusioninventory') . "</th>";
     echo "<th>" . __('Status') . "</th>";
     echo "<th>" . __('Starting date', 'fusioninventory') . "</th>";
     echo "<th>" . __('Ending date', 'fusioninventory') . "</th>";
     echo "<th>" . __('Total duration') . "</th>";
     echo "<th>" . __('Threads number', 'fusioninventory') . "</th>";
     echo "<th>" . __('Total discovery devices', 'fusioninventory') . "</th>";
     echo "<th>" . __('Devices not imported', 'fusioninventory') . "</th>";
     echo "<th>" . __('Devices linked', 'fusioninventory') . "</th>";
     echo "<th>" . __('Devices imported', 'fusioninventory') . "</th>";
     echo "</tr>";
     $sql = "SELECT `glpi_plugin_fusioninventory_taskjobstates`.*\n            FROM `glpi_plugin_fusioninventory_taskjobstates`\n         LEFT JOIN `glpi_plugin_fusioninventory_taskjobs`\n            ON `plugin_fusioninventory_taskjobs_id` = `glpi_plugin_fusioninventory_taskjobs`.`id`\n         WHERE `method` = 'networkdiscovery'\n         GROUP BY `uniqid`\n         ORDER BY `uniqid` DESC\n         LIMIT " . intval($start) . ", " . intval($_SESSION['glpilist_limit']);
     $result = $DB->query($sql);
     while ($data = $DB->fetch_array($result)) {
         echo "<tr class='tab_bg_1'>";
         echo "<td>" . $data['uniqid'] . "</td>";
         $pfTaskjob->getFromDB($data['plugin_fusioninventory_taskjobs_id']);
         echo "<td>" . $pfTaskjob->getLink() . "</td>";
         $pfAgent->getFromDB($data['plugin_fusioninventory_agents_id']);
         echo "<td>" . $pfAgent->getLink(1) . "</td>";
         $nb_found = 0;
         $nb_threads = 0;
         $start_date = "";
         $end_date = "";
         $notimporteddevices = 0;
         $updateddevices = 0;
         $createddevices = 0;
         $a_taskjobstates = $pfTaskjobstate->find("`uniqid`='" . $data['uniqid'] . "'");
         foreach ($a_taskjobstates as $datastate) {
             $a_taskjoblog = $pfTaskjoblog->find("`plugin_fusioninventory_taskjobstates_id`='" . $datastate['id'] . "'");
             foreach ($a_taskjoblog as $taskjoblog) {
                 if (strstr($taskjoblog['comment'], " ==devicesfound==")) {
                     $nb_found += str_replace(" ==devicesfound==", "", $taskjoblog['comment']);
                 } else {
                     if (strstr($taskjoblog['comment'], "==importdenied==")) {
                         $notimporteddevices++;
                     } else {
                         if (strstr($taskjoblog['comment'], "==updatetheitem==")) {
                             $updateddevices++;
                         } else {
                             if (strstr($taskjoblog['comment'], "==addtheitem==")) {
                                 $createddevices++;
                             } else {
                                 if ($taskjoblog['state'] == "1") {
                                     $nb_threads = str_replace(" threads", "", $taskjoblog['comment']);
                                     $start_date = $taskjoblog['date'];
                                 }
                             }
                         }
                     }
                 }
                 if ($taskjoblog['state'] == "2" or $taskjoblog['state'] == "3" or $taskjoblog['state'] == "4" or $taskjoblog['state'] == "5") {
                     if (!strstr($taskjoblog['comment'], 'Merged with ')) {
                         $end_date = $taskjoblog['date'];
                     }
                 }
             }
         }
         // State
         echo "<td>";
         switch ($data['state']) {
             case 0:
                 echo __('Prepared', 'fusioninventory');
                 break;
             case 1:
             case 2:
                 echo __('Started', 'fusioninventory');
                 break;
             case 3:
                 echo __('Finished tasks', 'fusioninventory');
                 break;
         }
         echo "</td>";
         echo "<td>" . Html::convDateTime($start_date) . "</td>";
         echo "<td>" . Html::convDateTime($end_date) . "</td>";
         if ($end_date == '') {
             $end_date = date("Y-m-d H:i:s");
         }
         if ($start_date == '') {
             echo "<td>-</td>";
         } else {
             $interval = '';
             if (phpversion() >= 5.3) {
                 $date1 = new DateTime($start_date);
                 $date2 = new DateTime($end_date);
                 $interval = $date1->diff($date2);
                 $display_date = '';
                 if ($interval->h > 0) {
                     $display_date .= $interval->h . "h ";
                 } else {
                     if ($interval->i > 0) {
                         $display_date .= $interval->i . "min ";
                     }
                 }
                 echo "<td>" . $display_date . $interval->s . "s</td>";
             } else {
                 $interval = $pfStateInventory->date_diff($start_date, $end_date);
             }
         }
         echo "<td>" . $nb_threads . "</td>";
         echo "<td>" . $nb_found . "</td>";
         echo "<td>" . $notimporteddevices . "</td>";
         echo "<td>" . $updateddevices . "</td>";
         echo "<td>" . $createddevices . "</td>";
         echo "</tr>";
     }
     echo "</table>";
 }