1: <?php
  2: 
  3: global $bright_user_registration_cache;
  4: 
  5: class Bright {
  6: 
  7:   
  8:   public static function add_learner_to_invitation($learner_id,$invitation_name,$nodelay=false) {
  9:     $api_root = get_option('bright_api_url');
 10:     $query_data=array();
 11: 
 12:     $query_data['format'] = 'json';
 13:     $query_data['realm_guid'] = get_option('bright_realm_guid');
 14:     $query_data['realm_secret_key'] = get_option('bright_secret_key');
 15:     $query_data['name'] = $invitation_name;
 16:     $query_data['learners'] = json_encode(array($learner_id));
 17:     if ($nodelay)
 18:       $query_data['nodelay'] = 'true';
 19: 
 20:     $url = $api_root.'/invitation/add_learners?' . http_build_query($query_data);
 21: 
 22:     $json = bright_curl($url);
 23:     $json_data = json_decode($json);
 24:     return $json_data;
 25:   }
 26:   
 27:   public static function create_invitation($query_data) {
 28:     $api_root = get_option('bright_api_url');
 29:     $query_data['format'] = 'json';
 30:     $query_data['realm_guid'] = get_option('bright_realm_guid');
 31:     $query_data['realm_secret_key'] = get_option('bright_secret_key');
 32:     $query_data['course_guids'] = json_encode($query_data['course_guids']);
 33:     $query_data['license_data'] = json_encode($query_data['license_data']);
 34:     
 35:     $url = $api_root.'/invitation';
 36:     
 37:     $json = bright_curl($url, 'POST', $query_data);
 38:     $json_data = json_decode($json);
 39:     return $json_data;
 40:   }
 41:   
 42:   public static function _get_api_key($email,$course_provider_id=null) {
 43:     $api_root = get_option('bright_api_url');
 44:     $bright_realm_guid = get_option('bright_realm_guid');
 45:     $bright_secret_key = get_option('bright_secret_key');
 46:     $query_data = array(
 47:                         'format' => 'json',
 48:                         'realm_guid' => $bright_realm_guid,
 49:                         'realm_secret_key' => $bright_secret_key,
 50:                         'user_email' => $email);
 51:     
 52:     if (!empty($course_provider_id))
 53:       $query_data['course_provider_id'] = $course_provider_id;
 54:     
 55:     if (!empty($bright_realm_guid) && !empty($bright_secret_key)) {
 56:       $query_data['realm_guid'] = $bright_realm_guid;
 57:       $query_data['realm_secret_key'] = $bright_secret_key;
 58:     }
 59:     
 60:     $auth_url = $api_root.'/api_key';
 61:     
 62:     $json = bright_curl($auth_url, 'POST', $query_data);
 63:     $json_data = json_decode($json);
 64:     return $json_data->access_token;
 65:   }
 66:   
 67:   public static function register_user_to_course($api_key,$user_email,$course,$first_name=null,$last_name=null) {
 68:     $api_call = 'registration';
 69:     if (strstr($api_root,"v1")) {
 70:       $api_call = 'scorm_cloud_registration';
 71:     }
 72:     $api_root = get_option('bright_api_url');
 73:     
 74:     
 75:     
 76:     
 77:     
 78:     
 79:     
 80:     
 81:     
 82:     
 83:     $query_params = array(
 84:                           'api_key' => $api_key,
 85:                           'dont_duplicate' => 1,
 86:                           'format' => 'json',
 87:                           'course_guid' => $course
 88:                           );
 89:     
 90:     if (!empty($first_name)) 
 91:       $query_params['fname'] = $first_name;
 92:     if (!empty($last_name)) 
 93:       $query_params['lname'] = $last_name;
 94:     
 95:     if (!empty($user_email)) 
 96:       $query_params['learner_id'] = $user_email;
 97:     
 98:     $auth_url = $api_root . '/' . $api_call . '/gcreate?' . http_build_query($query_params);
 99:     return bright_curl($auth_url);
100:   }
101:   
102:   
103:   104: 105: 106: 107: 
108:   
109:   public static function fetch_bright_realm_key($course_provider_id=false) {
110:     if (! $course_provider_id) 
111:       $course_provider_id =get_user_option('bright_course_provider_id',
112:                                            bright_get_user()->ID);
113:     
114:     $api_root = get_option('bright_api_url');
115:     $bright_realm_guid = get_option('bright_realm_guid');
116:     $bright_secret_key = get_option('bright_secret_key');
117:     
118:     if (empty($bright_secret_key) || empty($bright_realm_guid))
119:       throw new Exception("realm guid and secret key not set in your Bright settings");
120:     
121:     $query_data = array(
122:                         'format' => 'json',
123:                         'realm_guid' => $bright_realm_guid,
124:                         'realm_secret_key' => $bright_secret_key);
125:     
126:     if ($course_provider_id) 
127:       $query_data['course_provider_id'] = $course_provider_id;
128:     
129:     $auth_url = $api_root.'/api_key';
130:     $json = bright_curl($auth_url, 'POST', $query_data);
131:     $json_data = json_decode($json);
132:     return $json_data->access_token;
133:   }
134:   
135:   public static function json_encode_unicode($data) {
136:     if (defined('JSON_UNESCAPED_UNICODE')) {
137:       return json_encode($data, JSON_UNESCAPED_UNICODE);
138:     }
139:     return preg_replace_callback('/(?<!\\\\)\\\\u([0-9a-f]{4})/i',
140:                                  function ($m) {
141:                                    $d = pack("H*", $m[1]);
142:                                    $r = mb_convert_encoding($d, "UTF8", "UTF-16BE");
143:                                    return $r!=="?" && $r!=="" ? $r : $m[0];
144:                                  }, json_encode($data)
145:                                  );
146:   }
147:   
148:   public static  function json_cb(&$item, $key) { 
149:     if (is_string($item)) $item = mb_encode_numericentity($item, array (0x80, 0xffff, 0, 0xffff), 'UTF-8'); 
150:   }
151:   
152:   public static  function my_json_encode($arr){
153:     
154:     array_walk_recursive($arr, 'Bright::json_cb');
155:     return mb_decode_numericentity(json_encode($arr), array (0x80, 0xffff, 0, 0xffff), 'UTF-8');
156:     
157:   }
158:   
159:   public static function realm_user_gcustom($learner_id,$data) {
160:     $api_root = get_option('bright_api_url');
161:     $bright_realm_guid = get_option('bright_realm_guid');
162:     $bright_secret_key = get_option('bright_secret_key');
163:     
164:     $url = get_site_url();
165:     $find = array( 'http://', 'https://' );
166:     $replace = '';
167:     $without_protocol = str_replace( $find, $replace, $url );
168:     
169:     $query_data = array(
170:                         'format' => 'json',
171:                         'realm_guid' => $bright_realm_guid,
172:                         'email' => $learner_id,
173:                         'key' => 'hostdata',
174:                         'php_json_encoded' => "1",
175:                         $without_protocol => Bright::my_json_encode($data),
176:                         'realm_secret_key' => $bright_secret_key);
177:     
178:     $url = $api_root.'/realm_user/gcustom' . '?'. http_build_query($query_data);
179:     $json = file_get_contents($url);
180:     $json_data = json_decode($json);
181:     return $json_data;
182:   }
183:   
184:   public static function is_user_registered_to_course($course_id,$use_cache=true) {
185:     global $bright_user_registration_cache;
186:     
187:     if (empty($bright_user_registration_cache)) {
188:       $bright_user_registration_cache = array();
189:     }
190:     
191:     if ($bright_user_registration_cache[$course_id] && $use_cache) {
192:       return $bright_user_registration_cache[$course_id];
193:     }
194:     
195:     $user_is_registered = false;
196:     if ( is_user_logged_in() ) {
197:       $bright_key=bright_get_authentication_code_for_user(bright_get_user());
198:       $registration = bright_get_registration_data($bright_key,$course_id);
199:       if ($registration) {
200:         $json_data = json_decode($registration);
201:         if ($json_data[0] && ! $json_data[0]->deleted) {
202:           $user_is_registered = true;
203:         }
204:       }
205:     } 
206:     $bright_user_registration_cache[$course_id] = $user_is_registered;
207:     return $user_is_registered;
208:   }
209: }
210: