function test_registrations()
 {
     $reg = null;
     $user_id = iclicker_service::require_user();
     // try get registration
     $reg = iclicker_service::get_registration_by_clicker_id($this->clicker_id);
     $this->assertFalse($reg);
     // create registration
     $reg = iclicker_service::create_clicker_registration($this->clicker_id, $user_id);
     $this->assertTrue($reg);
     $this->assertEqual($this->clicker_id, $reg->clicker_id);
     $reg_id = $reg->id;
     $this->assertTrue($reg_id);
     $this->assertFalse($reg->from_national);
     // get registration
     $reg1 = iclicker_service::get_registration_by_clicker_id($this->clicker_id, $user_id);
     $this->assertTrue($reg1);
     $this->assertEqual($this->clicker_id, $reg1->clicker_id);
     $this->assertEqual($reg_id, $reg1->id);
     $reg2 = iclicker_service::get_registration_by_id($reg_id);
     $this->assertTrue($reg2);
     $this->assertEqual($this->clicker_id, $reg2->clicker_id);
     $this->assertEqual($reg_id, $reg2->id);
     // save registration
     $reg->from_national = 1;
     $save_id = iclicker_service::save_registration($reg);
     $this->assertTrue($save_id);
     $this->assertEqual($reg_id, $save_id);
     // check it changed
     $reg3 = iclicker_service::get_registration_by_id($reg_id);
     $this->assertTrue($reg3);
     $this->assertEqual($reg_id, $reg3->id);
     $this->assertTrue($reg3->from_national);
     // too fast $this->assertNotEqual($reg->timemodified, $reg3->timemodified);
     // make registration inactive
     $this->assertTrue($reg->activated);
     $reg4 = iclicker_service::set_registration_active($reg_id, false);
     $this->assertTrue($reg4);
     $this->assertFalse($reg4->activated);
     // check it changed
     $reg5 = iclicker_service::get_registration_by_id($reg_id);
     $this->assertTrue($reg5);
     $this->assertEqual($reg_id, $reg5->id);
     $this->assertEqual($reg4->id, $reg5->id);
     $this->assertFalse($reg5->activated);
     // get all registration
     $results = iclicker_service::get_registrations_by_user($user_id);
     $this->assertTrue($results);
     $this->assertEqual(1, count($results));
     $results = iclicker_service::get_registrations_by_user($user_id, true);
     $this->assertNotNull($results);
     $this->assertFalse($results);
     $this->assertEqual(0, count($results));
     $results = iclicker_service::get_all_registrations();
     $this->assertTrue($results);
     $this->assertTrue(count($results) >= 1);
     // remove registration
     $result = iclicker_service::remove_registration($reg_id);
     $this->assertTrue($result);
     // try get registration
     $reg = iclicker_service::get_registration_by_id($reg_id);
     $this->assertFalse($reg);
 }
     // special return, non-XML
     $cntlr->setStatus(204);
     //No content
     $cntlr->sendResponse();
     return;
     // SHORT CIRCUIT
 } else {
     if ("register" == $pathSeg0) {
         iclicker_get_and_check_current_user("upload registrations data");
         $xml = iclicker_get_xml_data($cntlr);
         $cr = iclicker_service::decode_registration($xml);
         $owner_id = $cr->owner_id;
         $message = '';
         $reg_status = false;
         try {
             iclicker_service::create_clicker_registration($cr->clicker_id, $owner_id);
             // valid registration
             $message = iclicker_service::msg('reg.registered.below.success', $cr->clicker_id);
             $reg_status = true;
         } catch (ClickerIdInvalidException $e) {
             // invalid clicker id
             $message = iclicker_service::msg('reg.registered.clickerId.invalid', $cr->clicker_id);
         } catch (InvalidArgumentException $e) {
             // invalid user id
             $message = "Student not found in the CMS";
         } catch (ClickerRegisteredException $e) {
             // already registered
             $key = '';
             if ($e->owner_id == $e->registered_owner_id) {
                 // already registered to this user
                 $key = 'reg.registered.below.duplicate';
 public function processRegistration()
 {
     // process calls to the registration view
     if (!iclicker_service::get_current_user_id()) {
         throw new ClickerSecurityException('Current user is not logged in and cannot access the registration view');
     }
     $this->results['new_reg'] = false;
     $this->results['clicker_id_val'] = "";
     if ('POST' == $this->method) {
         if (optional_param('register', null, PARAM_ALPHANUM) != null) {
             // we are registering a clicker
             $clicker_id = optional_param('clickerId', null, PARAM_ALPHANUMEXT);
             if ($clicker_id == null) {
                 $this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.empty');
             } else {
                 $this->results['clicker_id_val'] = $clicker_id;
                 // save a new clicker registration
                 try {
                     iclicker_service::create_clicker_registration($clicker_id);
                     $this->addMessage(self::KEY_INFO, 'reg.registered.success', $clicker_id);
                     $this->addMessage(self::KEY_BELOW, 'reg.registered.below.success');
                     $this->results['new_reg'] = true;
                 } catch (ClickerRegisteredException $e) {
                     $this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.duplicate', $clicker_id);
                     if (iclicker_service::$allow_remote_sharing) {
                         $this->addMessage(self::KEY_BELOW, 'reg.registered.below.duplicate', $clicker_id);
                     } else {
                         $this->addMessage(self::KEY_BELOW, 'reg.registered.below.duplicate.noshare', $clicker_id);
                     }
                 } catch (ClickerWebservicesException $e) {
                     $this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.failure', $clicker_id, $e);
                 } catch (ClickerIdInvalidException $e) {
                     if (ClickerIdInvalidException::F_EMPTY == $e->type) {
                         $this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.empty');
                     } else {
                         if (ClickerIdInvalidException::F_LENGTH == $e->type) {
                             $this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.wrong.length');
                         } else {
                             if (ClickerIdInvalidException::GO_NO_USER == $e->type) {
                                 $this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.failure', $clicker_id, $e);
                             } else {
                                 if (ClickerIdInvalidException::GO_LASTNAME == $e->type) {
                                     $this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.go.wrong.lastname', $e);
                                 } else {
                                     if (ClickerIdInvalidException::GO_NO_MATCH == $e->type) {
                                         $this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.go.invalid', $clicker_id);
                                     } else {
                                         $this->addMessage(self::KEY_ERROR, 'reg.registered.clickerId.invalid', $clicker_id);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         } else {
             if (optional_param('activate', null, PARAM_ALPHANUM) != null) {
                 // First arrived at this page
                 $activate = optional_param('activate', 'false', PARAM_ALPHANUM);
                 $activate = $activate == 'true' ? true : false;
                 $reg_id = optional_param('registrationId', null, PARAM_INT);
                 if ($reg_id == null) {
                     $this->addMessage(self::KEY_ERROR, 'reg.activate.registrationId.empty', null);
                 } else {
                     // save a new clicker registration
                     $cr = iclicker_service::set_registration_active($reg_id, $activate);
                     if ($cr) {
                         $this->addMessage(self::KEY_INFO, 'reg.activate.success.' . ($cr->activated ? 'true' : 'false'), $cr->clicker_id);
                     }
                 }
             } else {
                 if (optional_param('remove', null, PARAM_ALPHANUM) != null) {
                     $reg_id = optional_param('registrationId', null, PARAM_ALPHANUMEXT);
                     if ($reg_id == null) {
                         $this->addMessage(self::KEY_ERROR, 'reg.activate.registrationId.empty', null);
                     } else {
                         // remove a new clicker registration by deactivating it
                         $cr = iclicker_service::set_registration_active($reg_id, false);
                         if ($cr) {
                             $this->addMessage(self::KEY_INFO, 'reg.remove.success', $cr->clicker_id);
                         }
                     }
                 } else {
                     // invalid POST
                     echo 'WARN: Invalid POST: does not contain register or activate, nothing to do';
                 }
             }
         }
     }
     $this->results['regs'] = iclicker_service::get_registrations_by_user(null, true);
     $this->results['is_instructor'] = iclicker_service::is_instructor();
     $this->results['sso_enabled'] = iclicker_service::$block_iclicker_sso_enabled;
     // added to allow special messages below the forms
     $this->results['below_messages'] = $this->getMessages(self::KEY_BELOW);
 }
 function test_registrations()
 {
     global $DB;
     $this->resetAfterTest(true);
     // reset all changes automatically after this test
     $this->setUser($this->instructorid);
     $reg = null;
     $user_id = iclicker_service::require_user();
     // try get registration
     $reg = iclicker_service::get_registration_by_clicker_id($this->clicker_id);
     $this->assertFalse($reg);
     // create registration
     $reg = iclicker_service::create_clicker_registration($this->clicker_id, $user_id);
     $this->assertTrue(!empty($reg));
     $this->assertEquals($this->clicker_id, $reg->clicker_id);
     $reg_id = $reg->id;
     $this->assertTrue(!empty($reg_id));
     $this->assertEquals(0, $reg->from_national);
     // get registration
     $reg1 = iclicker_service::get_registration_by_clicker_id($this->clicker_id, $user_id);
     $this->assertTrue(!empty($reg1));
     $this->assertEquals($this->clicker_id, $reg1->clicker_id);
     $this->assertEquals($reg_id, $reg1->id);
     $reg2 = iclicker_service::get_registration_by_id($reg_id);
     $this->assertTrue(!empty($reg2));
     $this->assertEquals($this->clicker_id, $reg2->clicker_id);
     $this->assertEquals($reg_id, $reg2->id);
     // save registration
     $reg->from_national = 1;
     $save_id = iclicker_service::save_registration($reg);
     $this->assertTrue(!empty($save_id));
     $this->assertEquals($reg_id, $save_id);
     // check it changed
     $reg3 = iclicker_service::get_registration_by_id($reg_id);
     $this->assertTrue(!empty($reg3));
     $this->assertEquals($reg_id, $reg3->id);
     $this->assertEquals(1, $reg3->from_national);
     // too fast $this->assertNotEquals($reg->timemodified, $reg3->timemodified);
     // make registration inactive
     $this->assertEquals(1, $reg->activated);
     $reg4 = iclicker_service::set_registration_active($reg_id, false);
     $this->assertTrue(!empty($reg4));
     $this->assertEquals(0, $reg4->activated);
     // check it changed
     $reg5 = iclicker_service::get_registration_by_id($reg_id);
     $this->assertTrue(!empty($reg5));
     $this->assertEquals($reg_id, $reg5->id);
     $this->assertEquals($reg4->id, $reg5->id);
     $this->assertEquals(0, $reg5->activated);
     // get all registration
     $results = iclicker_service::get_registrations_by_user($user_id);
     $this->assertTrue(!empty($results));
     $this->assertEquals(1, count($results));
     $results = iclicker_service::get_registrations_by_user($user_id, true);
     $this->assertNotNull($results);
     $this->assertFalse(!empty($results));
     $this->assertEquals(0, count($results));
     $this->setAdminUser();
     // MUST be admin
     $results = iclicker_service::get_all_registrations();
     $this->assertTrue(!empty($results));
     $this->assertTrue(count($results) >= 1);
     // remove registration
     $result = iclicker_service::remove_registration($reg_id);
     $this->assertTrue(!empty($results));
     // try get registration
     $reg = iclicker_service::get_registration_by_id($reg_id);
     $this->assertFalse($reg);
 }