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);
 }
 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);
 }
     } 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';
         } else {
             // already registered to another user
             $key = 'reg.registered.clickerId.duplicate.notowned';
         }
         $message = iclicker_service::msg($key, $cr->clicker_id);
     }
     $registrations = iclicker_service::get_registrations_by_user($owner_id, true);
     $output = iclicker_service::encode_registration_result($registrations, $reg_status, $message);
     if ($reg_status) {
         $status = 200;
         //OK;
     } else {
         $status = 400;
         //BAD_REQUEST;
     }
 } else {
     // UNKNOWN
     $valid = false;
     $output = "Unknown path ({$cntlr->path}) specified for method POST";
     $status = 404;
     //NOT_FOUND;
 }
 /**
  * Determines the content to display in a block
  *
  * Blocks use two properties of $this->content: "text" and "footer".
  * The text is displayed as-is as the block content, and the footer is displayed below the content in a smaller font size.
  *
  * List blocks use $this->content->footer in the exact same way,
  * but they ignore $this->content->text.
  * Moodle expects such blocks to set two other properties when the get_content() method is called:
  * $this->content->items and $this->content->icons.
  * $this->content->items should be a numerically indexed array containing elements that
  * represent the HTML for each item in the list that is going to be displayed.
  * Usually these items will be HTML anchor tags which provide links to some page.
  * $this->content->icons should also be a numerically indexed array, with exactly as many items
  * as $this->content->items has. Each of these items should be a fully qualified HTML <img> tag,
  * with "src", "height", "width" and "alt" attributes. Obviously, it makes sense to keep the images
  * small and of a uniform size.
  * In order to tell Moodle that we want to have a list block instead of the standard text block,
  * we need to make a small change to our block class declaration.
  * Instead of extending class block_base, our block will extend class block_list.
  *
  * You can hide the block by displaying nothing. That means that both
  * $this->content->text and $this->content->footer are each equal to the
  * empty string (''). Moodle performs this check by calling the block's
  * is_empty() method, and if the block is indeed empty then it is not
  * displayed at all.
  *
  * @return string the content to display in the block
  */
 function get_content()
 {
     // for iclicker we will just render links here and possibly an indicator to show if you have registered a clicker
     global $CFG, $USER, $COURSE;
     if ($this->content !== null) {
         return $this->content;
     }
     $this->content = new stdClass();
     $this->content->text = '';
     if (iclicker_service::get_current_user_id()) {
         $this->content->text = "<div class='iclicker_nav_items'>\n";
         $reg_link = '<a href="' . iclicker_service::block_url('registration.php') . '">' . iclicker_service::msg('reg.title') . '</a><br/>';
         $this->content->text .= "  " . $reg_link . "\n";
         // also show the list of currently registered clickers
         $clicker_list_html = '';
         if ($clickers = iclicker_service::get_registrations_by_user(null, true)) {
             $clicker_list_html .= "  <ul class='iclicker_clickerids'>" . PHP_EOL;
             foreach ($clickers as $clicker) {
                 $clicker_list_html .= "    <li class='iclicker_clickerid'>{$clicker->clicker_id}</li>" . PHP_EOL;
             }
             $clicker_list_html .= "  </ul>\n";
         }
         $this->content->text .= $clicker_list_html;
         // the other links
         if (iclicker_service::is_admin()) {
             $link = '<a href="' . iclicker_service::block_url('admin.php') . '">' . iclicker_service::msg('admin.title') . '</a><br/>' . PHP_EOL;
             $this->content->text .= "  " . $link . "\n";
             // remove inst link after testing complete
             //$link = '<b><i>remove inst link</i></b> <a href="'.iclicker_service::block_url('instructor.php').'">'.iclicker_service::msg('inst.title').'</a>';
             //$this->content->text .= "  ".$link."\n";
         } else {
             if (iclicker_service::is_instructor()) {
                 $link = '<a href="' . iclicker_service::block_url('instructor.php') . '">' . iclicker_service::msg('inst.title') . '</a><br/>' . PHP_EOL;
                 $sso_link = '';
                 if (iclicker_service::$block_iclicker_sso_enabled) {
                     $sso_link = '<a class="nav_link" href="' . iclicker_service::block_url('instructor_sso.php') . '">' . iclicker_service::msg('inst.sso.title') . '</a><br/>' . PHP_EOL;
                 }
                 $this->content->text .= ' ' . $link . $sso_link;
             }
         }
         // close out the html
         $this->content->text .= "</div>" . PHP_EOL;
     }
     // FOOTER
     //$this->content->footer = '<a href="'.$CFG->wwwroot.'/blocks/iclicker/page.php?blockid='.$this->instance->id.'&courseid='.$COURSE->id.'">'.get_string('addpage', 'block_iclicker').'</a>';
     $this->content->footer = '';
     // Sample list items
     //$this->content->items[] = '<a href="some_file.php">Menu Option 1</a>';
     //$this->content->icons[] = '<img src="images/icons/1.gif" class="icon" alt="" />';
     return $this->content;
 }