예제 #1
0
 public function setUp()
 {
     $this->resetAfterTest();
     // Set this user as the admin.
     $this->setAdminUser();
     $this->task = new dummy_sync_members_task();
     $generator = $this->getDataGenerator();
     $course = $generator->create_course();
     $tooldata = ['courseid' => $course->id, 'membersyncmode' => helper::MEMBER_SYNC_ENROL_AND_UNENROL, 'membersync' => 1];
     $tool = $generator->create_lti_tool((object) $tooldata);
     $this->tool = helper::get_lti_tool($tool->id);
     $dataconnector = $this->task->get_dataconnector();
     $this->consumer = new ToolConsumer('Consumer1Key', $dataconnector);
     $this->consumer->name = 'Consumer1';
     $this->consumer->secret = 'Consumer1Secret';
     $this->consumer->save();
     $toolprovider = new tool_provider($this->tool->id);
     $toolprovider->consumer = $this->consumer;
     $toolprovider->map_tool_to_consumer();
     $imageurl = $this->getExternalTestFileUrl('test.jpg');
     $count = 10;
     $this->members = [];
     for ($i = 1; $i <= $count; $i++) {
         $user = new User();
         $user->firstname = 'Firstname' . $i;
         $user->lastname = 'Lastname' . $i;
         $user->ltiUserId = 'user' . $i;
         // Set user image values for some users.
         if ($i % 3 == 0) {
             $user->image = $imageurl;
         }
         $this->members[] = $user;
     }
     $this->context = Context::fromConsumer($this->consumer, 'testlticontextid');
     $this->context->save();
     $this->resourcelink = ResourceLink::fromContext($this->context, 'testresourcelinkid');
     $this->resourcelink->save();
 }
예제 #2
0
 /**
  * Check if a share arrangement is in place.
  *
  * @return boolean True if no error is reported
  */
 private function checkForShare()
 {
     $ok = true;
     $doSaveResourceLink = true;
     $id = $this->resourceLink->primaryResourceLinkId;
     $shareRequest = isset($_POST['custom_share_key']) && !empty($_POST['custom_share_key']);
     if ($shareRequest) {
         if (!$this->allowSharing) {
             $ok = false;
             $this->reason = 'Your sharing request has been refused because sharing is not being permitted.';
         } else {
             // Check if this is a new share key
             $shareKey = new ResourceLinkShareKey($this->resourceLink, $_POST['custom_share_key']);
             if (!is_null($shareKey->primaryConsumerKey) && !is_null($shareKey->primaryResourceLinkId)) {
                 // Update resource link with sharing primary resource link details
                 $key = $shareKey->primaryConsumerKey;
                 $id = $shareKey->primaryResourceLinkId;
                 $ok = $key !== $this->consumer->getKey() || $id != $this->resourceLink->getId();
                 if ($ok) {
                     $this->resourceLink->primaryConsumerKey = $key;
                     $this->resourceLink->primaryResourceLinkId = $id;
                     $this->resourceLink->shareApproved = $shareKey->autoApprove;
                     $ok = $this->resourceLink->save();
                     if ($ok) {
                         $doSaveResourceLink = false;
                         $this->user->getResourceLink()->primaryConsumerKey = $key;
                         $this->user->getResourceLink()->primaryResourceLinkId = $id;
                         $this->user->getResourceLink()->shareApproved = $shareKey->autoApprove;
                         $this->user->getResourceLink()->updated = time();
                         // Remove share key
                         $shareKey->delete();
                     } else {
                         $this->reason = 'An error occurred initialising your share arrangement.';
                     }
                 } else {
                     $this->reason = 'It is not possible to share your resource link with yourself.';
                 }
             }
             if ($ok) {
                 $ok = !is_null($key);
                 if (!$ok) {
                     $this->reason = 'You have requested to share a resource link but none is available.';
                 } else {
                     $ok = !is_null($this->user->getResourceLink()->shareApproved) && $this->user->getResourceLink()->shareApproved;
                     if (!$ok) {
                         $this->reason = 'Your share request is waiting to be approved.';
                     }
                 }
             }
         }
     } else {
         // Check no share is in place
         $ok = is_null($id);
         if (!$ok) {
             $this->reason = 'You have not requested to share a resource link but an arrangement is currently in place.';
         }
     }
     // Look up primary resource link
     if ($ok && !is_null($id)) {
         $consumer = new ToolConsumer($key, $this->dataConnector);
         $ok = !is_null($consumer->created);
         if ($ok) {
             $resourceLink = ResourceLink::fromConsumer($consumer, $id);
             $ok = !is_null($resourceLink->created);
         }
         if ($ok) {
             if ($doSaveResourceLink) {
                 $this->resourceLink->save();
             }
             $this->resourceLink = $resourceLink;
         } else {
             $this->reason = 'Unable to load resource link being shared.';
         }
     }
     return $ok;
 }