function test_user_keys()
 {
     global $DB;
     $user_id = iclicker_service::require_user();
     $this->assertNotNull($user_id);
     // saving key for user should work
     $user_key = iclicker_service::makeUserKey($user_id, true);
     $this->assertNotNull($user_key);
     $keysCount = $DB->count_records(iclicker_service::USER_KEY_TABLENAME, array('user_id' => $user_id));
     $this->assertEqual(1, $keysCount);
     // getting key for user (from make)
     $user_key1 = iclicker_service::makeUserKey($user_id, false);
     $this->assertNotNull($user_key1);
     $this->assertEqual($user_key, $user_key1);
     // getting key for user (from get)
     $user_key1 = iclicker_service::getUserKey($user_id);
     $this->assertNotNull($user_key1);
     $this->assertEqual($user_key, $user_key1);
     // update the key should work
     $user_key2 = iclicker_service::makeUserKey($user_id, true);
     $this->assertNotNull($user_key2);
     $this->assertNotEqual($user_key, $user_key2);
     // trying to save another key for this user should fail
     $dupeUserKey = new stdClass();
     $dupeUserKey->user_id = $user_id;
     $dupeUserKey->user_key = 'x12xx3x3x43x44';
     try {
         $DB->insert_record(iclicker_service::USER_KEY_TABLENAME, $dupeUserKey);
         $this->fail("Should have thrown an exception");
     } catch (dml_exception $e) {
         $this->assertNotNull($e->getMessage());
     }
     // delete the key should work
     $DB->delete_records(iclicker_service::USER_KEY_TABLENAME, array('user_id' => $user_id));
     $user_key3 = iclicker_service::getUserKey($user_id);
     $this->assertNull($user_key3);
     // adding key2 should work since key1 is gone
     $user_key4 = iclicker_service::makeUserKey($user_id, true);
     $this->assertNotNull($user_key4);
     // cleanup
     $DB->delete_records(iclicker_service::USER_KEY_TABLENAME, array('user_id' => $user_id));
 }
 public function processInstructorSSO()
 {
     $this->results['instPath'] = iclicker_service::block_url('instructor.php');
     // admin/instructor check
     if (!iclicker_service::is_admin() && !iclicker_service::is_instructor()) {
         throw new ClickerSecurityException("Current user is not an instructor and cannot access the instructor view");
     }
     $this->results['sso_enabled'] = iclicker_service::$block_iclicker_sso_enabled;
     $current_user_id = iclicker_service::get_current_user_id();
     if (iclicker_service::$block_iclicker_sso_enabled) {
         $current_user_key = null;
         if ('POST' == $this->method) {
             if (optional_param('generateKey', false, PARAM_ALPHANUM) != null) {
                 // handle generating a new key
                 $current_user_key = iclicker_service::makeUserKey($current_user_id, true);
                 $this->addMessage(self::KEY_INFO, 'inst.sso.generated.new.key', null);
             }
         }
         if ($current_user_key == null) {
             $current_user_key = iclicker_service::makeUserKey($current_user_id, false);
         }
         $this->results['sso_user_key'] = $current_user_key;
     }
     //$current_user_key = iclicker_service::makeUserKey($current_user_id);
     //$this->results['sso_user_key'] = $current_user_key;
 }