/** * method returns object IDs that user interacted (explicit/implicit) with * usable for decision which method to use * @param $eventType type of the interaction: "implicit" or "explicit" * @return <type> array of objectIDs */ public static function getInteractedObjects($eventType) { if ($eventType == "implicit") { $table = Config::$implicitEventStorageTable; } else { if ($eventType == "explicit") { $table = Config::$explicitEventStorageTable; } else { return false; } } $query = "select distinct `objectID` from `" . $table . "`\n where `userID`=" . ComponentCore::getUserId() . " "; $database = ComponentDatabase::get_instance(); $qr = $database->executeQuery($query); $objectsList = $qr->getResponseList(); while ($record = $database->getNextRow($objectsList)) { $res[] = $record["objectID"]; } return $res; }
/** *returns $noOfObjects of the best rated objects for the group of $usersArray users * method aggregates object scores in eventValues for users specified in $usersArray of events from $implicitEventsList and $explicitEventsList * @param <type> $usersArray array of ("userID => weight(similarity) of user:element of (0,1] interval ) * @param <type> $noOfObjects number of similar objects, we search for * @param <type> $implicitEventsList array of calculated implicitEvents * @param <type> $explicitEventsList array of calculated explicitEvents * @return <type> array( objectID => score ) ) * @return <type> */ public function getBestObjectForUsers($usersArray, $noOfObjects, $objectList = "", $implicitEventsList = "", $explicitEventsList = "") { $implicitTable = Config::$implicitEventStorageTable; $explicitTable = Config::$explicitEventStorageTable; $this->users = $usersArray; $this->objectToScoreArray = array(); $this->userID = ComponentCore::getUserId(); $database = ComponentDatabase::get_instance(); $objectRestrictionQuery = "\n SELECT distinct (`objectID`)\n FROM `" . $implicitTable . "`\n WHERE `userID`=" . $this->userID . " \n and `eventType`= \"object_shown_in_list\"\n and `eventValue` >= 3\n "; $objectAllowedQuery = "\n SELECT distinct (`objectID`)\n FROM `" . $implicitTable . "`\n WHERE `userID`=" . $this->userID . " \n and `eventType`= \"pageview\"\n and `eventValue` >= 1\n "; $shownArray = array(); $qrs = $database->executeQuery($objectRestrictionQuery); $shownObjects = $qrs->getResponseList(); while ($recordSL = $database->getNextRow($shownObjects)) { $shownArray[] = $recordSL["objectID"]; } $allowedArray = array(); $qra = $database->executeQuery($objectAllowedQuery); $allowedObjects = $qra->getResponseList(); while ($recordAO = $database->getNextRow($allowedObjects)) { $allowedArray[] = $recordAO["objectID"]; } $restrictedObjects = array_diff($shownArray, $allowedArray); if (sizeof($restrictedObjects) > 0) { $objectQuery = "`objectID` not in (" . implode(", ", $restrictedObjects) . ")"; } else { $objectQuery = " 1 "; } //forming user array into the query if (is_array($this->users) and sizeof($this->users) != 0) { $userQuery = "`userID` in ("; $first = 1; foreach ($this->users as $user => $val) { if ($first) { $first = 0; $userQuery .= $user; } else { $userQuery .= ", " . $user; } } $userQuery .= ")"; } else { return false; } //implicit events if (is_array($implicitEventsList) and sizeof($implicitEventsList) != 0) { //forming $implicitEventsList into the query $eventQuery = "`eventType` in ("; $first = 1; foreach ($implicitEventsList as $eType) { if ($first) { $first = 0; $eventQuery .= "\"" . $eType . "\""; } else { $eventQuery .= ", \"" . $eType . "\""; } } $eventQuery .= ")"; $queryImplicit = "select distinct `userID`,`objectID`,`eventType`,`eventValue`\n from `" . $implicitTable . "`\n where " . $userQuery . " and " . $eventQuery . " and " . $objectQuery . "\n order by `objectID` "; echo "<!--QueryCollaborativeNegPref " . $queryImplicit . "-->"; $this->usersToObjectRate($queryImplicit); } if (is_array($explicitEventsList) and sizeof($explicitEventsList) != 0) { //forming $implicitEventsList into the query $eventQuery = "`eventType` in ("; $first = 1; foreach ($explicitEventsList as $eType) { if ($first) { $first = 0; $eventQuery .= "\"" . $eType . "\""; } else { $eventQuery .= ", \"" . $eType . "\""; } } $eventQuery .= ")"; $queryExplicit = "select distinct `userID`,`objectID`,`eventType`,`eventValue`\n from `" . $explicitTable . "`\n where " . $userQuery . " and " . $eventQuery . "\n order by `objectID` "; //echo $queryExplicit; $this->usersToObjectRate($queryExplicit); } if (!$useImplicit and !$useExplicit) { $errLog = ErrorLog::get_instance(); $errLog->logError("No implicit or explicit events specified, no prediction made", "Standard"); } //rating of object finished - filter, sort + return the best arsort($this->objectToScoreArray); if (is_array($objectList) and sizeof($objectList) != 0) { $i = 0; $j = 0; $all_objects = array_keys($this->objectToScoreArray); $res_objects = array(); while ($i < $noOfObjects and $j <= sizeof($all_objects)) { if (in_array($all_objects[$j], $objectList)) { $res_objects[$all_objects[$j]] = $this->objectToScoreArray[$all_objects[$j]]; $i++; } $j++; } echo "<!-- resObjectCollab"; print_r($res_objects); echo "-->"; return $res_objects; } else { return array_slice($this->objectToScoreArray, 0, $noOfObjects, TRUE); } }