Exemple #1
0
 /**
  * Returns an array of cluster ids that are associated to the supplied class through tracks and
  * the current user has access to enrol users into
  *
  * @param   int        $clsid  The class whose association ids we care about
  * @return  int array          The array of accessible cluster ids
  */
 public static function get_allowed_clusters($clsid)
 {
     global $USER;
     $context = pm_context_set::for_user_with_capability('cluster', 'local/elisprogram:assign_userset_user_class_instructor', $USER->id);
     $allowed_clusters = array();
     // TODO: Ugly, this needs to be overhauled
     $cpage = new pmclasspage();
     if ($cpage->_has_capability('local/elisprogram:assign_userset_user_class_instructor', $clsid)) {
         require_once elispm::lib('data/clusterassignment.class.php');
         $cmuserid = pm_get_crlmuserid($USER->id);
         $userclusters = clusterassignment::find(new field_filter('userid', $cmuserid));
         foreach ($userclusters as $usercluster) {
             $allowed_clusters[] = $usercluster->clusterid;
         }
     }
     //we first need to go through tracks to get to clusters
     $track_listing = new trackassignment(array('classid' => $clsid));
     $tracks = $track_listing->get_assigned_tracks();
     //iterate over the track ides, which are the keys of the array
     if (!empty($tracks)) {
         foreach (array_keys($tracks) as $track) {
             //get the clusters and check the context against them
             $clusters = clustertrack::get_clusters($track);
             $allowed_track_clusters = $context->get_allowed_instances($clusters, 'cluster', 'clusterid');
             //append all clusters that are allowed by the available clusters contexts
             foreach ($allowed_track_clusters as $allowed_track_cluster) {
                 $allowed_clusters[] = $allowed_track_cluster;
             }
         }
     }
     return $allowed_clusters;
 }
 /**
  * Test get_assigned_tracks function
  */
 public function test_get_assigned_tracks()
 {
     // Fixture.
     $dataset = $this->createCsvDataset(array(trackassignment::TABLE => elispm::file('tests/fixtures/trackassignment.csv')));
     $this->loadDataSet($dataset);
     // Test.
     $trackassignment = new trackassignment();
     $trackassignment->classid = 100;
     $assignedtracks = $trackassignment->get_assigned_tracks();
     // Verify we got a non-empty array.
     $this->assertNotEmpty($assignedtracks);
     $this->assertInternalType('array', $assignedtracks);
     // Verify contents.
     $count = 0;
     foreach ($assignedtracks as $trackid => $trackassignmentid) {
         $this->assertEquals(1, $trackid);
         $this->assertEquals(1, $trackassignmentid);
         $count++;
     }
     $this->assertEquals(1, $count);
 }
Exemple #3
0
 /**
  * Determines whether the current user is allowed to enrol users into the provided class
  *
  * @param   int      $classid  The id of the class we are checking permissions on
  *
  * @return  boolean            Whether the user is allowed to enrol users into the class
  *
  */
 static function can_enrol_into_class($classid)
 {
     global $USER;
     //check the standard capability
     // TODO: Ugly, this needs to be overhauled
     $cpage = new pmclasspage();
     if ($cpage->_has_capability('local/elisprogram:class_enrol', $classid) || $cpage->_has_capability('local/elisprogram:class_enrol_userset_user', $classid)) {
         return true;
     }
     //get the context for the "indirect" capability
     $context = pm_context_set::for_user_with_capability('cluster', 'local/elisprogram:class_enrol_userset_user', $USER->id);
     //we first need to go through tracks to get to clusters
     $track_listing = new trackassignment(array('classid' => $classid));
     $tracks = $track_listing->get_assigned_tracks();
     //iterate over the track ides, which are the keys of the array
     if (!empty($tracks)) {
         foreach (array_keys($tracks) as $track) {
             //get the clusters and check the context against them
             $clusters = clustertrack::get_clusters($track);
             if (!empty($clusters)) {
                 foreach ($clusters as $cluster) {
                     if ($context->context_allowed($cluster->clusterid, 'cluster')) {
                         return true;
                     }
                 }
             }
         }
     }
     return false;
 }