/** * Execute the console command. * * @return mixed */ public function handle() { $date = Carbon::createFromDate($this->argument('year'), $this->argument('month'), $this->argument('day'), 'GMT'); $term = new Term(); $term->starts_at = $date; $term->ends_at = $date->addMonths(3); $term->save(); $this->info('Term created successfully.'); }
/** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * * @return void */ protected function schedule(Schedule $schedule) { $schedule->call(function () { $term = new Term(); $date = Carbon::now()->addDays(15); $term->starts_at = $date; $term->ends_at = $date->addMonths(3); $term->save(); })->daily()->when(function () { return term()->ends_at->diffInDays() == 15 && nextTerm() === null; }); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @param $id * * @return \Illuminate\Http\Response */ public function store(Request $request, $id) { $this->validate($request, ['name' => 'required']); $alias = new TermAlias(['name' => $request->name]); $term = Term::findOrFail($id)->aliases()->save($alias); return redirect()->action('TermAliasesController@create', [$id])->with('success', $alias); }
/** * Store a newly created resource in storage. * * @return Response */ public function store(MecanexUserRequest $request) { // $thematic_area=$request->interest; // dd($thematic_area); $mecanexuser = MecanexUser::create($request->all()); $username = $request->username; $user = User::where('username', $username)->get()->first(); // create records in table users_terms-scores once a mecanex user has been created $terms = Term::all(); $total_terms = $terms->count(); foreach ($terms as $term) { $mecanexuser->term()->sync([$term->id => ['user_score' => 0]], false); $mecanexuser->profilescore()->sync([$term->id => ['profile_score' => 0]], false); } //create record in table mecanex_user_term_home_term_neighbor once a mecanex user has been created for ($i = 1; $i <= $total_terms; $i++) { for ($j = $i + 1; $j <= $total_terms; $j++) { $mec_matrix = new MecanexUserTermHomeTermNeighbour(); $mec_matrix->mecanex_user_id = $mecanexuser->id; $mec_matrix->term_home_id = $i; $mec_matrix->term_neighbor_id = $j; $mec_matrix->link_score = 0.1; $mec_matrix->save(); } } //the following is only needed for linking an existing authorized user (users_table) with a new mecanex user provided they have the same username if (empty($user)) { $response = array('message' => 'Store successful'); } else { $mecanexuser->user_id = $user->id; $mecanexuser->save(); $response = array('message' => 'Store successful'); } return response($response, 201)->header('Content-Type', 'application/json'); }
/** * Show all votes in JSON form. * TODO: There should be a better way to do this. * * @return \Illuminate\Http\Response */ public function getVotes($terms = []) { Term::past()->get()->each(function ($term) use(&$terms) { $terms[] = ['id' => $term, 'start' => $term->starts_at->toDateString(), 'end' => $term->ends_at->toDateString(), 'candidates' => $term->candidates()->join('users', 'users.id', '=', 'candidates.user_id')->select(['candidates.id AS candidate_id', 'users.id AS user_id', 'users.facebook_id', 'users.name'])->get(), 'votes' => $term->votes()->join('users', 'users.id', '=', 'votes.user_id')->select(['votes.candidate_id', 'users.uuid'])->get()]; }); return response()->json(['terms' => $terms]); }
/** * Display a listing of the resource. * * @return Response */ public function scores() { //indexing of video terms set_time_limit(0); DB::table('videos_terms_scores')->delete(); //$counts=DB::select('select count(video_id) from videos'); //take number of videos //dd($counts); $terms = Term::all(); //take all Profile terms $videos = Video::all(); foreach ($videos as $video) { $id = $video->id; foreach ($terms as $term) { $results = DB::select(DB::raw('select MATCH (genre, topic, geographical_coverage, thesaurus_terms, title) AGAINST (? WITH QUERY EXPANSION) as rev from videos where id = (?)'), [$term->term, $id]); echo 'video_id=' . $id . ' term = ' . $term->term . ' score = ' . $results[0]->rev . '<br>'; DB::table('videos_terms_scores')->insert(['video_id' => $id, 'term_id' => $term->id, 'video_score' => $results[0]->rev]); echo 'record inserted!' . '<br>'; } } ///////////////////normalization/////////////////////////////////////////////////// //this takes for ever....raw db instead // foreach ($videos as $video) { // $id = $video->id; // $max_score = DB::table('videos_terms_scores')->where('video_id',$id)->max('video_score'); // foreach ($terms as $term) { // $temp_video = $video->term->find($term); // $video_term_score = $temp_video->pivot->video_score; //get score of video // $new_score=$video_term_score/$max_score; // $video->term()->sync([$term->id=> ['video_score' => $new_score]], false); // // } // } $query = DB::select(DB::raw('UPDATE videos_terms_scores as t join (select video_id,MAX(video_score) as maximum FROM videos_terms_scores GROUP BY video_id)as max_scores on t.video_id=max_scores.video_id SET t.video_score=t.video_score/max_scores.maximum')); //return view ('video.parser'); }
/** * Execute the command. * * @return void */ public function handle() { $term = \App\Term::firstOrCreate(['q' => $this->q]); $term->increment('searches_count'); $this->data['term_id'] = $term->id; \App\Search::firstOrCreate($this->data)->increment('count'); }
/** * Display the specified term. * * @Get("/{id}") * @Transaction( * @Response(200, body={"id": 1, "name": "Fall", "year": "2015"}), * @Response(404, body={"error": "not found"}) * ) * @param int $id * @return Response */ public function show($id) { try { $term = Term::findOrFail($id); return response()->json($term); } catch (ModelNotFoundException $e) { return new JsonResponse(['error' => 'not found'], Response::HTTP_NOT_FOUND); } }
public function post_terms() { $input = Input::all(); if ($input['submit'] == 'save') { if ($input['id'] > 0) { $terms = Term::find($input['id']); $terms->update($input); } else { $terms = new Term(); $terms->create($input); } } else { if ($input['id'] > 0) { Term::destroy($input['id']); } } return redirect('terms')->with('message', 'Terms have been updated successfully'); }
/** * Store a newly created resource in storage. * * @Response(201) * @param Request $request * @return Response */ public function store(Request $request) { $this->validate($request, ['member_id' => 'required|exists:members,id', 'reason' => 'required', 'term_id' => 'required|exists:terms,id']); $member = Member::findOrFail($request->input('member_id')); $term = Term::findOrFail($request->input('term_id')); $membership = new Membership(); $membership->reason = $request->input('reason'); $membership->term_id = $request->input('term_id'); $member->memberships()->save($membership); return new JsonResponse($membership, Response::HTTP_CREATED); }
/** * Run the database seeds. * * @return void */ public function run() { // \App\Term::create(['name' => 'villa', 'slug' => 'villa', 'route' => 'villa', 'type' => 'property_category', 'order' => 0, 'parent_id' => 0]); \App\Term::create(['name' => 'villa rental', 'slug' => 'villa-rental', 'route' => 'villa_rental', 'type' => 'property_category', 'order' => 0, 'parent_id' => 0]); \App\Term::create(['name' => 'land', 'slug' => 'land', 'route' => 'land', 'type' => 'property_category', 'order' => 1, 'parent_id' => 0]); \App\Term::create(['name' => 'beachfront property', 'slug' => 'beachfront-property', 'route' => 'beachfront_property', 'type' => 'property_tag', 'order' => 0, 'parent_id' => 0]); \App\Term::create(['name' => 'home and retirement', 'slug' => 'home-and-retirement', 'route' => 'home_and_retirement', 'type' => 'property_tag', 'order' => 0, 'parent_id' => 0]); \App\Term::create(['name' => 'uncategorized', 'slug' => 'uncategorized', 'route' => 'uncategorized', 'type' => 'post_category', 'order' => 0, 'parent_id' => 0]); \App\Term::create(['name' => '< $500.000', 'slug' => 'less-than-500k', 'route' => 'less_than_500k', 'type' => 'property_tag', 'order' => 0, 'parent_id' => 0]); \App\Term::create(['name' => '> $500.000', 'slug' => 'more-than-500k', 'route' => 'less_than_500k', 'type' => 'property_tag', 'order' => 0, 'parent_id' => 0]); }
public function remove_duplicates_on_score() { $terms = Term::all(); //take all Profile terms $videos = Video::all(); foreach ($videos as $video) { $id = $video->id; foreach ($terms as $term) { $results = DB::select(DB::raw('select * from videos_terms_scores where video_id=? and term_id=?'), [$id, $term->id]); if (count($results) > 1) { DB::table('videos_terms_scores')->where('video_id', $id)->where('term_id', $term->id)->delete(); DB::table('videos_terms_scores')->insert(['video_id' => $results[0]->video_id, 'term_id' => $results[0]->term_id, 'video_score' => $results[0]->video_score]); } } } return "removed duplicates"; }
/** * Display a listing of the resource. * * @return Response */ public function checkprofile() { $mecanex_user = Auth::user()->mecanex_user; $terms = Term::all(); $interests = Interest::all(); $listterms = []; $listscores = []; foreach ($interests as $interest) { array_push($listterms, $interest->interest); } foreach ($terms as $term) { $temp_user = $mecanex_user->profilescore->find($term->id); $user_term_score = $temp_user->pivot->profile_score; array_push($listscores, $user_term_score); } $userprofile = array_combine($listterms, $listscores); return view('user.checkprofile', compact('userprofile')); }
/** * Store a newly created resource in storage. * * @return Response */ public function store(CreateProfileRequest $request) { //validation first $request->all(); $username = Auth::user()->username; $email = Auth::user()->email; $name = Input::get('name'); $surname = Input::get('surname'); $gender_id = Input::get('gender_id'); $age_id = Input::get('age_id'); $country_id = Input::get('country_id'); $occupation_id = Input::get('occupation_id'); $education_id = Input::get('education_id'); //$facebook=Input::get('facebook'); //$twitter=Input::get('twitter'); $mecanex_user = new MecanexUser(['username' => $username, 'name' => $name, 'surname' => $surname, 'gender_id' => $gender_id, 'age_id' => $age_id, 'education_id' => $education_id, 'occupation_id' => $occupation_id, 'country_id' => $country_id, 'email' => $email]); $user = User::find(Auth::user()->id); $mecanex_user = $user->profile()->save($mecanex_user); //$mecanex_user->save(); // create records in table users_terms-scores once a mecanex user has been created $terms = Term::all(); $total_terms = $terms->count(); foreach ($terms as $term) { $mecanex_user->term()->sync([$term->id => ['user_score' => 0]], false); $mecanex_user->profilescore()->sync([$term->id => ['profile_score' => 0]], false); } //create record in table mecanex_user_term_home_term_neighbor once a mecanex user has been created for ($i = 1; $i <= $total_terms; $i++) { for ($j = $i + 1; $j <= $total_terms; $j++) { $mec_matrix = new MecanexUserTermHomeTermNeighbour(); $mec_matrix->mecanex_user_id = $mecanex_user->id; $mec_matrix->term_home_id = $i; $mec_matrix->term_neighbor_id = $j; $mec_matrix->link_score = 0.05; $mec_matrix->save(); } } return view('user.profile')->with('message', 'You can now state your interests'); // }
public function edit_announcement($id) { $fields = Field::orderBy('name')->lists('name', 'id'); $terms = Term::lists('name', 'id'); $municipalities = Municipality::lists('name', 'id'); $regions = Region::lists('name', 'id'); $type = array(0 => 'ვატარებ', 1 => 'ვეძებ'); $quarter = Quarter::all()->lists('name', 'id'); $months = Month::all()->lists('name', 'id'); $select = DB::select(' SELECT trainings.id, field_training.field_id, term_training.term_id, municipality_training.municipality_id, month_training.month_id FROM trainings JOIN field_training ON trainings.id = field_training.training_id JOIN term_training ON trainings.id = term_training.training_id JOIN municipality_training ON trainings.id = municipality_training.training_id JOIN month_training ON trainings.id = month_training.training_id where trainings.id = ' . $id . ' group by trainings.id '); $select = $select[0]; $training_filtered = new \stdClass(); $training_instance = Training::find($select->id); $training_field = Field::find($select->field_id); $training_municipality = Municipality::find($select->municipality_id); $training_months = DB::select('SELECT months.name FROM month_training JOIN months ON months.id = month_training.month_id WHERE training_id = ' . $training_instance->id); $training_terms = DB::select('SELECT terms.name FROM term_training JOIN terms ON terms.id = term_training.term_id WHERE training_id = ' . $training_instance->id); foreach ($training_months as $month) { $training_months_array[] = $month->name; } foreach ($training_terms as $term) { $training_terms_array[] = $term->name; } $training_filtered->id = $training_instance->id; $training_filtered->header = $training_instance->name; $training_filtered->description = $training_instance->description; $training_filtered->file = $training_instance->file; $training_filtered->link = $training_instance->link; $training_filtered->field['name'] = $training_field->name; $training_filtered->field['id'] = $training_field->id; $training_filtered->months = $training_months_array; $training_filtered->terms = $training_terms_array; $training_filtered->municipality['name'] = $training_municipality->name; $training_filtered->municipality['id'] = $training_municipality->id; $training_filtered->region = $training_municipality->region_id; $training_filtered->isAdmin = Auth::check(); return view('edit.edit_announcement', ['fields' => $fields, 'terms' => $terms, 'regions' => $regions, 'municipalities' => $municipalities, 'quarter' => $quarter, 'month' => $months, 'type' => $type, 'training' => $training_filtered]); }
/** * Display a listing of the resource. * * @return Response */ public function login(Request $request, SammyK\LaravelFacebookSdk\LaravelFacebookSdk $fb) { //test if token for FB login is enough $token = $request->token; // return $token; // does not work since $token is string but should be token // if (! $token->isLongLived()) { // // OAuth 2.0 client handler // $oauth_client = $fb->getOAuth2Client(); // // // Extend the access token. // try { // $token = $oauth_client->getLongLivedAccessToken($token); // } catch (Facebook\Exceptions\FacebookSDKException $e) { // dd($e->getMessage()); // } // } //this is for not include $token in the get calls $fb->setDefaultAccessToken($token); // Get basic info on the user from Facebook. try { $response = $fb->get('/me?fields=id,email'); } catch (Facebook\Exceptions\FacebookSDKException $e) { dd($e->getMessage()); } try { $profileresponse = $fb->get('/me?fields=id,name,gender'); } catch (Facebook\Exceptions\FacebookSDKException $e) { dd($e->getMessage()); } // Convert the response to a `Facebook/GraphNodes/GraphUser` collection $facebook_user = $response->getGraphUser(); $mecanex_user = $profileresponse->getGraphUser(); $existing_mecanex_user = MecanexUser::where('email', '=', $facebook_user["email"])->get()->first(); $existing_user = User::where('email', '=', $facebook_user["email"])->get()->first(); // Create the user if it does not exist or update the existing entry. // This will only work if you've added the SyncableGraphNodeTrait to your User model. if ($existing_user == null) { $facebook_user["username"] = "******" . $facebook_user["id"]; $user = User::createOrUpdateGraphNode($facebook_user); } else { $facebook_user["username"] = $existing_user->username; $user = $existing_user; $user->facebook_user_id = $facebook_user["id"]; $user->save(); } //store profile data in mecanex_users table $id = $user->id; $facebook_id = $user->facebook_user_id; if ($existing_mecanex_user == null) { $username = "******" . $user->facebook_user_id; } else { $username = $existing_mecanex_user->username; } $email = $user->email; $fullname = $mecanex_user->getName(); $fullname = explode(" ", $fullname); $name = $fullname[0]; $surname = $fullname[1]; $gender = $mecanex_user->getGender(); if ($gender == 'female') { $gender_id = 2; } else { $gender_id = 1; } $fbuser = MecanexUser::firstOrNew(array('email' => $email)); $fbuser->username = $username; $fbuser->user_id = $id; $fbuser->facebook_user_id = $facebook_id; $fbuser->gender_id = $gender_id; $fbuser->name = $name; $fbuser->surname = $surname; $fbuser->email = $email; $fbuser->save(); // Log the user into Laravel Auth::login($user); // create records in table users_terms-scores once a mecanex user has been created if ($existing_mecanex_user == null) { $terms = Term::all(); $total_terms = count($terms); foreach ($terms as $term) { $fbuser->term()->sync([$term->id => ['user_score' => 0]], false); $fbuser->profilescore()->sync([$term->id => ['profile_score' => 0]], false); } for ($i = 1; $i <= $total_terms; $i++) { for ($j = $i + 1; $j <= $total_terms; $j++) { $mec_matrix = new MecanexUserTermHomeTermNeighbour(); $mec_matrix->mecanex_user_id = $fbuser->id; $mec_matrix->term_home_id = $i; $mec_matrix->term_neighbor_id = $j; $mec_matrix->link_score = 0.05; $mec_matrix->save(); } } } $response = ['username' => $username, 'message' => 'User was successfully logged in']; $statusCode = 201; return response($response, $statusCode)->header('Content-Type', 'application/json'); }
public function drop_term($id, Request $request) { if (!Auth::user()) { return redirect('/'); } try { Term::where('id', '=', $id)->delete(); } catch (\exception $e) { $rules = ['delete' => 'required']; $messages = ['delete.required' => 'წაშლა შეუძლებელია']; $this->validate($request, $rules, $messages); } return redirect('/admin'); }
public function explicitrf_function(SignalsRequest $request) { $username = $request->username; $device = $request->device_id; $video_id = $request->video_id; $action_type = $request->action; $explicit_rf = $request->value; //check if $request->score==-1 or 0 or 1 $record_exists = UserAction::where('username', $username)->where('video_id', $video_id)->where('action', $action_type)->first(); if (empty($record_exists)) { $user_action = new UserAction(['username' => $username, 'device_id' => $device, 'video_id' => $video_id, 'explicit_rf' => $explicit_rf, 'action' => $action_type]); $user_action->save(); $get_importance = Action::where('id', $request->action)->first(); $importance = $get_importance->importance; $user_action->update(array('weight' => $explicit_rf, 'importance' => $importance)); } else { $record_exists->update(array('explicit_rf' => $explicit_rf, 'weight' => $explicit_rf)); } //store in the dcgs table - only used for the online experiments $mecanex_user = MecanexUser::where('username', $username)->first(); $dcg_record = Dcg::where('mecanex_user_id', $mecanex_user->id)->where('video_id', $video_id); $dcg_record->update(array('explicit_rf' => $explicit_rf)); //////////////calculate ku/////////////////////// // number of all enrichments $video = Video::where('video_id', $video_id)->first(); $num_all_enrichments = DB::select(DB::raw('SELECT COUNT(*) as num FROM enrichments_videos_time WHERE video_id=?'), [$video->id]); $num_all_enrichments = $num_all_enrichments[0]->num; // create list with clicked enrichments $get_action_id = Action::where('action', 'click enrichment')->first(); $action_id = $get_action_id->id; $enrichment_click_ids = UserAction::where('username', $username)->where('video_id', $video_id)->where('action', $action_id)->get(array('content_id')); $num_clicked_enrichments = count($enrichment_click_ids); // replace all database entries with one with the appropriate weight (so that the calculation can be easily done). // The information for what enrichments where clicked is now in the $enrichment_click_ids variable if ($num_clicked_enrichments != 0) { DB::table('user_actions')->where('username', $username)->where('video_id', $video_id)->where('action', $action_id)->delete(); $user_action = new UserAction(['username' => $username, 'device_id' => $device, 'video_id' => $video_id, 'action' => $action_id, 'content_id' => '1']); $user_action->save(); $get_importance = Action::where('id', $action_id)->first(); $importance = $get_importance->importance; $user_action->update(array('weight' => $num_clicked_enrichments / $num_all_enrichments, 'importance' => $importance)); } // create list with shared enrichments $get_action_id = Action::where('action', 'share')->first(); $action_id = $get_action_id->id; $enrichment_share_ids = UserAction::where('username', $username)->where('video_id', $video_id)->where('action', $action_id)->get(array('content_id')); $num_shared_enrichments = count($enrichment_share_ids); // replace all database entries with one with the appropriate weight (so that the calculation can be easily done). // The information for what enrichments where clicked is now in the $enrichment_shared_ids variable if ($num_shared_enrichments != 0) { DB::table('user_actions')->where('username', $username)->where('video_id', $video_id)->where('action', $action_id)->delete(); $user_action = new UserAction(['username' => $username, 'device_id' => $device, 'video_id' => $video_id, 'action' => $action_id, 'content_id' => '1']); $user_action->save(); $get_importance = Action::where('id', $action_id)->first(); $importance = $get_importance->importance; $user_action->update(array('weight' => $num_shared_enrichments / $num_all_enrichments, 'importance' => $importance)); } $k_nominator = UserAction::where('username', $username)->where('video_id', $video_id)->groupBy('username')->get(['username', DB::raw('SUM(importance*weight) as total_score')])->first(); $query = "SELECT SUM(t1.importance ) AS total FROM (SELECT DISTINCT action, importance FROM user_actions WHERE username=:username AND video_id=:videoid) AS t1 "; $k_denominator = DB::select(DB::raw($query), array('username' => $username, 'videoid' => $video_id)); //returns array $k = $k_nominator->total_score / $k_denominator[0]->total; //to [0] gia na prospelasei to 1o stoixeio tou array pou einai to object /////////////////////////////update weights and update profile////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////Retrieve Terms///////////////////////////// //////////////retrieve terms for given video////////////// $video = Video::where('video_id', $request->video_id)->first(); $threshold = 0; //need to appropriate set $results = $video->term()->where('video_score', '>', $threshold)->get(array('term_id')); $video_term_list = []; foreach ($results as $result) { //retrieve the terms that will be updated array_push($video_term_list, $result->term_id); } sort($video_term_list); ///////////retrieve terms for the clicked enrichments////////////// // $get_actionid = Action::where('action', 'click enrichment')->first(); // $action_id = $get_actionid->id; // $enrichment_ids = UserAction::where('username', $username)->where('video_id', $video_id)->where('action', $action_id)->get(array('content_id')); $enrichment_term_list = []; // foreach ($enrichment_ids as $enrichment_id) // { // // //call the api and get some terms // //or emulate enrichment_terms // //$results=Enrichment::where('id',$enrichment_id)->get(array('term_id')) // foreach($results as $result) // { // array_push($enrichment_term_list,$result->term_id); // } // // } //$enrichment_terms=array_unique($enrichment_term_list); $enrichment_terms = [3, 4, 7]; //retrieve terms for the clicked ads $ads_terms = [4, 8, 10]; ///Final term_list - no need since I will update in three steps // $term_list=array_merge($video_term_list,$enrichment_terms,$ads_terms); // $terms=array_unique($term_list); ////////////////update weights of profile////////////////////////// $term_scores = []; //save all scores to get the max $user = MecanexUser::where('username', $username)->get()->first(); $video = Video::where('video_id', $video_id)->get()->first(); $link_user = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $user->id)->get()->first(); //return $video; //return $link_user; //update based on video_terms //return $video_term_list; foreach ($video_term_list as $video_term_id) { $temp_user = $user->term->find($video_term_id); $user_term_score = $temp_user->pivot->user_score; //get score of user $temp_video = $video->term->find($video_term_id); $video_term_score = $temp_video->pivot->video_score; //get score of video $final_score = $k * (0.8 * $video_term_score); foreach ($enrichment_click_ids as $enrichment_click_id) { $id = $enrichment_click_id->content_id; $enrichment = Enrichment::where('enrichment_id', $id)->get()->first(); $temp_enrichment = $enrichment->term->find($video_term_id); $enrichment_term_score = $temp_enrichment->pivot->enrichment_score; $final_score = $final_score + $k * (0.1 / $num_clicked_enrichments * $enrichment_term_score); } foreach ($enrichment_share_ids as $enrichment_share_id) { $id = $enrichment_share_id->content_id; $enrichment = Enrichment::where('enrichment_id', $id)->get()->first(); $temp_enrichment = $enrichment->term->find($video_term_id); $enrichment_term_score = $temp_enrichment->pivot->enrichment_score; $final_score = $final_score + $k * (0.1 / $num_shared_enrichments * $enrichment_term_score); } //update score $new_score = $user_term_score + $final_score; //ok $new_score = max($new_score, 0); // don't let negative values array_push($term_scores, $new_score); // //store score $user->term()->sync([$video_term_id => ['user_score' => $new_score]], false); } //return $term_scores; // update matrix $number_video_terms = count($video_term_list); $link_term_scores = []; //save all scores to get the max for ($i = 0; $i <= $number_video_terms - 1; $i++) { for ($j = $i + 1; $j < $number_video_terms; $j++) { $temp_user_matrix = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $user->id)->where('term_home_id', $video_term_list[$i])->where('term_neighbor_id', $video_term_list[$j])->get()->first(); //return $temp_user_matrix; $temp_video = $video->term->find($video_term_list[$i]); $video_term_score1 = $temp_video->pivot->video_score; $temp_video = $video->term->find($video_term_list[$j]); $video_term_score2 = $temp_video->pivot->video_score; $final_score = $k * (0.8 * $video_term_score1 * $video_term_score2); foreach ($enrichment_click_ids as $enrichment_click_id) { $id = $enrichment_click_id->content_id; $enrichment = Enrichment::where('enrichment_id', $id)->get()->first(); $temp_enrichment = $enrichment->term->find($video_term_list[$i]); $enrichment_term_score1 = $temp_enrichment->pivot->enrichment_score; $temp_enrichment = $enrichment->term->find($video_term_list[$j]); $enrichment_term_score2 = $temp_enrichment->pivot->enrichment_score; $final_score = $final_score + $k * (0.1 / $num_clicked_enrichments * $enrichment_term_score1 * $enrichment_term_score2); } foreach ($enrichment_share_ids as $enrichment_share_id) { $id = $enrichment_share_id->content_id; $enrichment = Enrichment::where('enrichment_id', $id)->get()->first(); $temp_enrichment = $enrichment->term->find($video_term_list[$i]); $enrichment_term_score1 = $temp_enrichment->pivot->enrichment_score; $temp_enrichment = $enrichment->term->find($video_term_list[$j]); $enrichment_term_score2 = $temp_enrichment->pivot->enrichment_score; $final_score = $final_score + $k * (0.1 / $num_shared_enrichments * $enrichment_term_score1 * $enrichment_term_score2); } $new_score = $temp_user_matrix->link_score + $final_score; $new_score = max($new_score, 0); // don't let negative values array_push($link_term_scores, $new_score); $temp_user_matrix->link_score = $new_score; $temp_user_matrix->save(); } } //same should be done for enrichments and ads //find max value and divide term values $max_term_value = max(max($term_scores), 1); if ($link_term_scores != []) { $max_link_term_value = max(max($link_term_scores), 1); } foreach ($video_term_list as $video_term_id) { $temp_user = $user->term->find($video_term_id); $user_term_score = $temp_user->pivot->user_score; //get score of user $temp_video = $video->term->find($video_term_id); $video_term_score = $temp_video->pivot->video_score; //get score of video //update score $new_score = $user_term_score / $max_term_value; //ok //store score $user->term()->sync([$video_term_id => ['user_score' => $new_score]], false); } for ($i = 0; $i <= $number_video_terms - 1; $i++) { for ($j = $i + 1; $j < $number_video_terms; $j++) { $temp_user_matrix = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $user->id)->where('term_home_id', $video_term_list[$i])->where('term_neighbor_id', $video_term_list[$j])->get()->first(); $old_score = $temp_user_matrix->link_score; $new_score = $old_score / $max_link_term_value; $temp_user_matrix->link_score = $new_score; $temp_user_matrix->save(); } } //calculate profile $terms = Term::all()->count(); for ($j = 1; $j <= $terms; $j++) { $profile_score = 0; for ($i = 1; $i <= $terms; $i++) { $temp_user = $user->term->find($i); $user_term_score = $temp_user->pivot->user_score; //get score of user if ($i == $j) { $link_score = 0; } elseif ($i > $j) { $temp_user_matrix = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $user->id)->where('term_home_id', $j)->where('term_neighbor_id', $i)->get()->first(); $link_score = $temp_user_matrix->link_score; } else { $temp_user_matrix = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $user->id)->where('term_home_id', $i)->where('term_neighbor_id', $j)->get()->first(); $link_score = $temp_user_matrix->link_score; } $profile_score = $profile_score + $user_term_score * $link_score; } $user->profilescore()->sync([$j => ['profile_score' => $profile_score]], false); } DB::table('user_actions')->where('username', $username)->where('video_id', $video_id)->delete(); $response = ["message" => 'RF Saved']; $statusCode = 200; return response($response, $statusCode)->header('Content-Type', 'application/json'); }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update(InterestRequest $request, $username) { $interests = $request->except('_method'); $user = MecanexUser::where('username', $username)->get()->first(); if (empty($user)) { $response = ["error" => "User doesn`t exist"]; $statusCode = 404; } elseif (count($user->interest) == 0) { $response = ["message" => "User has not set any interests, To create interests, try store"]; $statusCode = 404; } else { //check if what sends is correct or else it crushes //updates interests and user scores $key_values = array(); foreach ($interests as $key => $value) { $interest = Interest::where('short_name', $key)->get(array('id'))->first(); $key_values[$interest->id] = $value; $user->interest()->sync([$interest->id => ['interest_score' => $value]], false); $term = Term::where('term', $key)->firstOrFail(); $value = $value / 5; $user->term()->sync([$term->id => ['user_score' => $value]], false); } //there must be a more clever way to do this (foreach maybe) //created 2 arrays one with the term ids ($term_ids) and the other with the scores ($term_scores) $terms = Term::get(array('id')); $all_terms = []; foreach ($terms as $term) { array_push($all_terms, $term->id); } $all_terms_length = count($all_terms); ksort($key_values); $term_ids = array_keys($key_values); $term_ids_length = count($term_ids); //update adjacency matrix for ($i = 0; $i <= $term_ids_length - 1; $i++) { for ($j = 0; $j <= $all_terms_length - 1; $j++) { if ($term_ids[$i] == $all_terms[$j]) { continue; } elseif ($term_ids[$i] > $all_terms[$j]) { $temp_user_matrix = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $user->id)->where('term_home_id', $all_terms[$j])->where('term_neighbor_id', $term_ids[$i])->get()->first(); $temp_user = $user->term->find($all_terms[$j]); $user_term_score_a = $temp_user->pivot->user_score; $temp_user = $user->term->find($term_ids[$i]); $user_term_score_b = $temp_user->pivot->user_score; $new_score = $user_term_score_b * $user_term_score_a; // } } else { $temp_user_matrix = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $user->id)->where('term_home_id', $term_ids[$i])->where('term_neighbor_id', $all_terms[$j])->get()->first(); $temp_user = $user->term->find($all_terms[$j]); $user_term_score_a = $temp_user->pivot->user_score; $temp_user = $user->term->find($term_ids[$i]); $user_term_score_b = $temp_user->pivot->user_score; $new_score = $user_term_score_b * $user_term_score_a; } $temp_user_matrix->link_score = $new_score; $temp_user_matrix->save(); } } //update profile for ($j = 1; $j <= $all_terms_length; $j++) { $profile_score = 0; for ($i = 1; $i <= $all_terms_length; $i++) { $temp_user = $user->term->find($i); $user_term_score = $temp_user->pivot->user_score; //get score of user if ($i == $j) { $link_score = 0; } elseif ($i > $j) { $temp_user_matrix = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $user->id)->where('term_home_id', $j)->where('term_neighbor_id', $i)->get()->first(); $link_score = $temp_user_matrix->link_score; } else { $temp_user_matrix = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $user->id)->where('term_home_id', $i)->where('term_neighbor_id', $j)->get()->first(); $link_score = $temp_user_matrix->link_score; } $profile_score = $profile_score + $user_term_score * $link_score; } $user->profilescore()->sync([$j => ['profile_score' => $profile_score]], false); } $response = ["message" => "User Interests were saved"]; $statusCode = 201; } return response($response, $statusCode)->header('Content-Type', 'application/json'); }
/** * Display a listing of the resource. * * @return Response */ public function login(Request $request, SammyK\LaravelFacebookSdk\LaravelFacebookSdk $fb) { //test if token for FB login is enough $token = $request->token; // return $token; // does not work since $token is string but should be token // if (! $token->isLongLived()) { // // OAuth 2.0 client handler // $oauth_client = $fb->getOAuth2Client(); // // // Extend the access token. // try { // $token = $oauth_client->getLongLivedAccessToken($token); // } catch (Facebook\Exceptions\FacebookSDKException $e) { // dd($e->getMessage()); // } // } //this is for not include $token in the get calls $fb->setDefaultAccessToken($token); // Get basic info on the user from Facebook. try { $response = $fb->get('/me?fields=id,email'); } catch (Facebook\Exceptions\FacebookSDKException $e) { dd($e->getMessage()); } try { $profileresponse = $fb->get('/me?fields=id,name,gender'); } catch (Facebook\Exceptions\FacebookSDKException $e) { dd($e->getMessage()); } // Convert the response to a `Facebook/GraphNodes/GraphUser` collection $facebook_user = $response->getGraphUser(); $mecanex_user = $profileresponse->getGraphUser(); // Create the user if it does not exist or update the existing entry. // This will only work if you've added the SyncableGraphNodeTrait to your User model. $user = User::createOrUpdateGraphNode($facebook_user); //store profile data in mecanex_users table $id = $user->id; $facebook_id = $user->facebook_user_id; $fullname = $mecanex_user->getName(); $fullname = explode(" ", $fullname); $name = $fullname[0]; $surname = $fullname[1]; $gender = $mecanex_user->getGender(); if ($gender == 'female') { $gender_id = 2; } else { $gender_id = 1; } $fbuser = MecanexUser::firstOrNew(array('facebook_user_id' => $facebook_id)); $fbuser->user_id = $id; $fbuser->facebook_user_id = $facebook_id; $fbuser->gender_id = $gender_id; $fbuser->name = $name; $fbuser->surname = $surname; $fbuser->save(); // Log the user into Laravel Auth::login($user); // create records in table users_terms-scores once a mecanex user has been created $terms = Term::all(); foreach ($terms as $term) { $fbuser->term()->sync([$term->id => ['user_score' => 0]], false); } $response = 'User was saved'; $statusCode = 201; return response($response, $statusCode)->header('Content-Type', 'application/json'); }
/** * Display a listing of the resource. * * @return Response */ public function target(Request $request) { try { $mecanexusers = MecanexUser::all(); // filter by demographics if ($request->gender_id != 0) { $mecanexusers = $mecanexusers->where("gender_id", (int) $request->gender_id); } if ($request->age_id != 0) { $mecanexusers = $mecanexusers->where("age_id", (int) $request->age_id); } if ($request->education_id != 0) { $mecanexusers = $mecanexusers->where("education_id", (int) $request->education_id); } if ($request->occupation_id != 0) { $mecanexusers = $mecanexusers->where("occupation_id", (int) $request->occupation_id); } if ($request->country_id != 0) { $mecanexusers = $mecanexusers->where("country_id", (int) $request->country_id); } // filter by preference on terms // keep mecanex users whose term profile score is more than 0.7 when normalized by the max term value of the user $statusCode = 200; if ($request->terms != []) { $all_terms = Term::all(); foreach ($all_terms as $term) { if (strpos($request->terms, $term->term) !== false) { $mecanexusers = $mecanexusers->filter(function ($user) use($term) { $myquery = DB::select(DB::raw(' SELECT MAX(profile_score) as mymax FROM users_terms_profilescores WHERE mecanex_user_id=' . $user->id . '')); if ($myquery[0]->mymax == 0) { // don't take into account users that have not provided any information return false; } return $user->profilescore[$term->id - 1]['pivot']['profile_score'] / $myquery[0]->mymax > 0.7; // minus one because terms start from 0 where id starts from 1 }); // $response[] = $mecanexusers[1]->profilescore[$term->id-1]['pivot']['profile_score']; // $myquery = DB::select(DB::raw(' SELECT MAX(profile_score) as mymax // FROM users_terms_profilescores // WHERE mecanex_user_id='. $mecanexusers[2]->id .'')); } } } $array_of_users = array(); // If no mecanexusers exist from the filters selected, we return a random list of videos if ($mecanexusers->isEmpty()) { $response = []; $response['message'][] = "No information for the target group"; $statusCode = 200; return $response; } foreach ($mecanexusers as $mecanexuser) { $array = array(); $terms = $mecanexuser->profilescore; foreach ($terms as $term) { $temp = (double) $term['pivot']['profile_score']; array_push($array, $temp); } // normalize with maximum because that way the clustering seems more accurate $maximum = max($array); if ($maximum != 0) { for ($i = 0; $i < count($array); $i++) { $array[$i] = $array[$i] / $maximum; } } array_push($array_of_users, $array); } /** return the array of users so that we can see whether the profiles have normalized * values so that they can be clustered properly */ $space = new Space(14); $num_of_clusters = 5; foreach ($array_of_users as $point) { $space->addPoint($point); } $clusters = $space->solve($num_of_clusters); $all_users = []; // foreach ($clusters as $i => $cluster) // printf("Cluster %d [%d,%d]: %d points\n", $i, $cluster[0], $cluster[1], count($cluster)); foreach ($clusters as $i => $cluster) { $terms = []; for ($j = 0; $j < 14; $j++) { array_push($terms, $cluster[$j]); } $all_users[] = ['cluster_id' => $i, 'cluster_terms' => $terms, 'num_of_users' => count($cluster)]; } $statusCode = 200; $response = $all_users; return $response; } catch (Exception $e) { $statusCode = 400; } finally { return Response::json($response, $statusCode); } }
public function scoreEnrichments() { set_time_limit(0); DB::table('enrichments_terms_scores')->delete(); $terms = Term::all(); //take all Profile terms $enrichments = Enrichment::all(); foreach ($enrichments as $enrichment) { $id = $enrichment->id; foreach ($terms as $term) { $results = DB::select(DB::raw('select MATCH (class, longName, description) AGAINST (? WITH QUERY EXPANSION) as rev from enrichments where id = (?)'), [$term->term, $id]); DB::table('enrichments_terms_scores')->insert(['enrichment_id' => $id, 'term_id' => $term->id, 'enrichment_score' => $results[0]->rev]); } } $query = DB::select(DB::raw('UPDATE enrichments_terms_scores as t join (select enrichment_id,MAX(enrichment_score) as maximum FROM enrichments_terms_scores GROUP BY enrichment_id)as max_scores on t.enrichment_id=max_scores.enrichment_id SET t.enrichment_score=t.enrichment_score/max_scores.maximum')); $terms = Term::all(); //take all Profile terms $enrichments = Enrichment::all(); foreach ($enrichments as $enrichment) { $id = $enrichment->id; foreach ($terms as $term) { $results = DB::select(DB::raw('select COUNT(*) as rev from enrichments where id=' . $id . ' and class LIKE "%' . $term->term . '%"')); DB::table('enrichments_terms_scores')->where('enrichment_id', $id)->where('term_id', $term->id)->update(['enrichment_score' => DB::raw('enrichment_score*0.5+' . $results[0]->rev * 0.5 . '')]); } } $response = ['message' => 'Successful scoring of enrichments']; $statusCode = 200; return Response::json($response, $statusCode); }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update(Request $request) { // //get the input $arts = $request->arts; $disasters = $request->disasters; $education = $request->education; $environment = $request->environment; $health = $request->health; $lifestyle = $request->lifestyle; $media = $request->media; $holidays = $request->holidays; $politics = $request->politics; $religion = $request->religion; $society = $request->society; $transportation = $request->transportation; $wars = $request->wars; $work = $request->work; //delete all records of that user in pivot table $mecanex_user = Auth::user()->mecanex_user; $mecanex_user->interest()->detach(); //store input in pivot table $mecanex_user->interest()->sync([1 => ['interest_score' => $arts], 2 => ['interest_score' => $disasters], 3 => ['interest_score' => $education], 4 => ['interest_score' => $environment], 5 => ['interest_score' => $health], 6 => ['interest_score' => $lifestyle], 7 => ['interest_score' => $media], 8 => ['interest_score' => $holidays], 9 => ['interest_score' => $politics], 10 => ['interest_score' => $religion], 11 => ['interest_score' => $society], 12 => ['interest_score' => $transportation], 13 => ['interest_score' => $wars], 14 => ['interest_score' => $work]]); //$interests=Interest::all(array('short_name')); $user_interests = $mecanex_user->interest; $term_ids = []; foreach ($user_interests as $user_interest) { $short_name = $user_interest->short_name; array_push($term_ids, $user_interest->id); $term = Term::where('term', $short_name)->firstOrFail(); $mecanex_user->term()->sync([$term->id => ['user_score' => $user_interest->pivot->interest_score / 5]], false); //normalization } $terms = Term::all()->count(); for ($i = 0; $i <= $terms - 1; $i++) { for ($j = $i + 1; $j <= $terms - 1; $j++) { $temp_user_matrix = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $mecanex_user->id)->where('term_home_id', $term_ids[$i])->where('term_neighbor_id', $term_ids[$j])->get()->first(); $temp_user = $mecanex_user->term->find($term_ids[$i]); $user_term_score_a = $temp_user->pivot->user_score; $temp_user = $mecanex_user->term->find($term_ids[$j]); $user_term_score_b = $temp_user->pivot->user_score; $new_score = $user_term_score_a * $user_term_score_b; $temp_user_matrix->link_score = $new_score; $temp_user_matrix->save(); } } for ($j = 1; $j <= $terms; $j++) { $profile_score = 0; for ($i = 1; $i <= $terms; $i++) { $temp_user = $mecanex_user->term->find($i); $user_term_score = $temp_user->pivot->user_score; //get score of user if ($i == $j) { $link_score = 0; } elseif ($i > $j) { $temp_user_matrix = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $mecanex_user->id)->where('term_home_id', $j)->where('term_neighbor_id', $i)->get()->first(); $link_score = $temp_user_matrix->link_score; } else { $temp_user_matrix = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $mecanex_user->id)->where('term_home_id', $i)->where('term_neighbor_id', $j)->get()->first(); $link_score = $temp_user_matrix->link_score; } $profile_score = $profile_score + $user_term_score * $link_score; } $mecanex_user->profilescore()->sync([$j => ['profile_score' => $profile_score]], false); } return Redirect::route('home')->with('message', 'Your interests were updated'); }
public function post_categories(Request $request) { $categories = \App\Term::where('type', 'post_category')->get(); if ($request->action == 'create') { return view('admin.pages.post.category-create', compact('categories')); } if ($request->action == 'edit' && isset($request->id)) { $term = \App\Term::find($request->id); return view('admin.pages.post.category-edit', compact('term', 'categories')); } return view('admin.pages.post.category-listing'); }
public function get_send_customer_quote($qr_id) { $message = Session::get('message'); $error = Session::get('error'); $quote_request = QuoteRequest::Find($qr_id); $qris = count($quote_request->get_quote) > 0 ? $quote_request->get_quote->qris : []; $terms = Term::all(); return view('quotes.send_customer_quote', compact('quote_request', 'qris', 'message', 'error', 'terms')); }
public function run() { DB::table('terms')->delete(); Term::create(['id' => '1', 'term' => 'arts']); Term::create(['id' => '2', 'term' => 'disasters']); Term::create(['id' => '3', 'term' => 'education']); Term::create(['id' => '4', 'term' => 'environment']); Term::create(['id' => '5', 'term' => 'health']); Term::create(['id' => '6', 'term' => 'lifestyle']); Term::create(['id' => '7', 'term' => 'media']); Term::create(['id' => '8', 'term' => 'holidays']); Term::create(['id' => '9', 'term' => 'politics']); Term::create(['id' => '10', 'term' => 'religion']); Term::create(['id' => '11', 'term' => 'society']); Term::create(['id' => '12', 'term' => 'transportation']); Term::create(['id' => '13', 'term' => 'wars']); Term::create(['id' => '14', 'term' => 'work']); }
<option value="{{ $city->city_name }}">{{ $city->city_name }}</option> @endforeach </select> </div> </div> <div class="clearfix"></div> <div class="form-group"> <div class="col-sm-6"> <label class="control-label" for="owner_name">Category</label> <select name="category" class="form-control" placeholder=""> <?php $categories = \App\Term::where('type', 'property_category')->get(); ?> @foreach($categories as $category) <option value="{{ $category->id }}">{{ $category->name }}</option> @endforeach </select> </div> </div> <div class="form-group map"> <div class="col-sm-12"> <label class="control-label" for="sell_note">Comment</label> <textarea name="sell_note" class="form-control" rows="5" placeholder="" style="margin-bottom: 30px;"></textarea> </div> </div>
<m-input select class="m-input-wrapper w50-6"> <select name="status"> <option value="1" {{ $property->status == 1 ? 'selected' : '' }}>available</option> <option value="0" {{ $property->status == 0 ? 'selected' : '' }}>unavailable</option> <option value="-1" {{ $property->status == -1 ? 'selected' : '' }}>hidden</option> </select> <label for="title">status</label> </m-input> </div> <div class="m-input-group fwidth flexbox flexbox-wrap"> <?php $exist_tags = $property->tags->lists('id')->toArray(); ?> <?php $tags = \App\Term::where('type', 'property_tag')->get(); ?> @foreach($tags as $tag) <m-checkbox data-label="{{ $tag->name }}" w25-9> <input type="checkbox" value="{{ $tag->id }}" name="tag[]" {{ in_array($tag->id, $exist_tags) ? 'checked' : '' }}> <lever></lever> </m-checkbox> @endforeach </div> <div class="m-input-group textarea fwidth flexbox flexbox-wrap"> <h3 class="input-group-title">Property Description</h3> <div class="input-wrapper fwidth"> <textarea name="content" id="property-input-description" rows="10" style="padding-top: 0">{{ $property->localeEN()->content or '' }}</textarea> </div> </div>
private function recommend_enr($list, $user_id) { $terms = Term::all(); $user = MecanexUser::where('id', $user_id)->get()->first(); $user_terms_scores = $user->profilescore; foreach ($user_terms_scores as $user_term_score) { $temp_profile_scores[] = $user_term_score->pivot->profile_score; } foreach ($list as $enrichment) { $arithmitis = 0; $sumA = 0; $sumB = 0; $temp_enrichment_scores = []; $enrichment_terms_scores = DB::select(DB::raw('SELECT * FROM enrichments_terms_scores WHERE enrichment_id=? ORDER BY term_id'), [$enrichment["enrichment_id"]]); foreach ($enrichment_terms_scores as $enrichment_term_score) { $temp_enrichment_scores[] = $enrichment_term_score->enrichment_score; } for ($i = 0; $i < count($terms); $i++) { $arithmitis = $arithmitis + $temp_enrichment_scores[$i] * $temp_profile_scores[$i]; $sumA = $sumA + pow($temp_enrichment_scores[$i], 2); $sumB = $sumB + pow($temp_profile_scores[$i], 2); } $enrichment["score"] = $arithmitis / (sqrt($sumA) + sqrt($sumB)); $enrichmentScores[$enrichment["enrichment_id"]] = $arithmitis / (sqrt($sumA) + sqrt($sumB)); } arsort($enrichmentScores); return $enrichmentScores; // // first key of enrichmentScores list - the most recommended enrichment // $recommended_enrichment = key($enrichmentScores); // // return Enrichment::find($recommended_enrichment)->enrichment_id; }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { // $term = Term::find($id); if (in_array($term->id, [1, 2, 3, 4, 5, 6])) { return response()->json(array('status' => 500, 'monolog' => array('title' => 'errors', 'message' => 'you are not allowed to delete this item'))); } $term->delete(); return response()->json(array('status' => 200, 'monolog' => array('title' => 'delete success', 'message' => 'object has been deleted'), 'id' => $id)); }