7 – Views contextual filter with php validation


How to use php code to fetch 2 UIDs from a View and compare them to validate the View

I created a View in Drupal 7 that lists all entities of the entity type of “Session” (ECK) by USER A (a teacher).
USER B (a student) is the logged-in user and looks at that list view of USER A.
Both users have connected “User Info” entities (ECK) that are used to show particular profile info.
The eck_user_info_student (ECK) of USER B has a property called “my_active_course” which can only contain an integer and a single value, in our case an ID.
The eck_course entity type (ECK) has a property called “teacher” which can only contain an integer and a single value, in our case a UID.

First contextual filter:
Now I need to check IF the teacher of USER B’s active course (UID) is the author (UID) of the Sessions that USER B is looking at. For convenience, I have added the author UID of the Sessions to the URL. So that we can use that as the first contextual filter. If no UID is in the URL, the view will display an Access Denied page.

I have the following machine names in the database:

field_student_profile_ref the User account field (single value, integer, uid)

eck_session the Session (ECK Session entity type)

eck_course the Course (ECK Course entity type)

eck_user_info_student the Student User Info (ECK Student entity type)

eck_user_info_student:my_active_course the Active Course (single value, integer, id)

eck_user_info_student:my_active_course:teacher the Teacher UID on the Active Course (single value, integer, uid)

IF the author of the Sessions (UID) is equal to the teacher of USER B’s active course (UID),
=> display all Sessions.
ELSE the author of the Sessions (UID) is NOT equal to the teacher of USER B’s active course (UID),
=> display an “Access Denied” page.

Therefore I currently believe that I need to write a comparison statement in php code in the section “specific validation criteria” where I have the global user i.e. the logged-in user and create a variable where I store the UID from the Active Course of User B and I think that this last part is the actual problem: how to load that?
The code below is just provided as a starting point

<? php
global $user;
$show = false;
$teacher = $argument;
// provided in the URL
$my_teacher = $user->('field_student_profile_ref')('my_active_course')('uid');
// the UID of the Teacher in the field "my_active_course" on the eck_user_info_student
if($user->uid == $my_teacher->uid) {
  $show = true;
}
else {
  $show = false;
}
if ($show) {
return true;
}
else {
return false;
}