Beispiel #1
0
 /**
  * Records the event of the current tool having been used
  * This is used for the favorites list of the My Tools module
  *
  * @param      string  $app Name of app called
  * @param      integer $uid User ID
  * @return     void
  */
 private function _recordUsage($app, $uid)
 {
     include_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'version.php';
     $tool = new \Components\Tools\Tables\Version($this->database);
     $tool->loadFromName($app);
     $created = Date::toSql();
     // Get a list of all their recent tools
     include_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'recent.php';
     $rt = new \Components\Tools\Tables\Recent($this->database);
     $rows = $rt->getRecords($uid);
     $thisapp = 0;
     for ($i = 0, $n = count($rows); $i < $n; $i++) {
         if ($app == trim($rows[$i]->tool)) {
             $thisapp = $rows[$i]->id;
         }
     }
     // Get the oldest entry. We may need this later.
     $oldest = end($rows);
     // Check if any recent tools are the same as the one just launched
     if ($thisapp) {
         // There was one, so just update its creation time
         $rt->id = $thisapp;
         $rt->uid = $uid;
         $rt->tool = $app;
         $rt->created = $created;
     } else {
         // Check if we've reached 5 recent tools or not
         if (count($rows) < 5) {
             // Still under 5, so insert a new record
             $rt->uid = $uid;
             $rt->tool = $app;
             $rt->created = $created;
         } else {
             // We reached the limit, so update the oldest entry effectively replacing it
             $rt->id = $oldest->id;
             $rt->uid = $uid;
             $rt->tool = $app;
             $rt->created = $created;
         }
     }
     if (!$rt->store()) {
         App::abort(500, $rt->getError());
         return;
     }
 }