Fix on moderators
Fix on moderator serialization and queries Change-Id: Iaf765be43a02101da6395bd655a4299907251e66
This commit is contained in:
parent
c34ef254b7
commit
f7a6f04891
@ -11,6 +11,8 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
use models\summit\Presentation;
|
||||
|
||||
/**
|
||||
* Class PresentationSerializer
|
||||
* @package ModelSerializers
|
||||
@ -54,10 +56,12 @@ class PresentationSerializer extends SummitEventSerializer
|
||||
{
|
||||
if(!count($relations)) $relations = $this->getAllowedRelations();
|
||||
|
||||
$values = parent::serialize($expand, $fields, $relations, $params);
|
||||
|
||||
$presentation = $this->object;
|
||||
|
||||
if(!$presentation instanceof Presentation) return [];
|
||||
|
||||
$values = parent::serialize($expand, $fields, $relations, $params);
|
||||
|
||||
if(in_array('speakers', $relations)) {
|
||||
$values['speakers'] = $presentation->getSpeakerIds();
|
||||
}
|
||||
@ -104,6 +108,9 @@ class PresentationSerializer extends SummitEventSerializer
|
||||
$speakers[] = SerializerRegistry::getInstance()->getSerializer($s)->serialize();
|
||||
}
|
||||
$values['speakers'] = $speakers;
|
||||
if(isset($values['moderator_speaker_id']) && intval($values['moderator_speaker_id']) > 0 ){
|
||||
$values['moderator'] = SerializerRegistry::getInstance()->getSerializer($presentation->getModerator())->serialize();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -13,6 +13,8 @@
|
||||
**/
|
||||
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use models\summit\Presentation;
|
||||
use models\summit\PresentationSpeaker;
|
||||
|
||||
/**
|
||||
* Class PresentationSpeakerSerializer
|
||||
@ -44,13 +46,18 @@ class PresentationSpeakerSerializer extends SilverStripeSerializer
|
||||
*/
|
||||
public function serialize($expand = null, array $fields = array(), array $relations = array(), array $params = array() )
|
||||
{
|
||||
if(!count($relations)) $relations = $this->getAllowedRelations();
|
||||
$values = parent::serialize($expand, $fields, $relations, $params);
|
||||
$speaker = $this->object;
|
||||
$summit_id = isset($params['summit_id'])? intval($params['summit_id']):null;
|
||||
$published = isset($params['published'])? intval($params['published']):true;
|
||||
$values['presentations'] = $speaker->getPresentationIds($summit_id, $published);
|
||||
$values['pic'] = Config::get("server.assets_base_url", 'https://www.openstack.org/') . 'profile_images/speakers/' . $speaker->getId();
|
||||
if(!count($relations)) $relations = $this->getAllowedRelations();
|
||||
|
||||
$speaker = $this->object;
|
||||
if(!$speaker instanceof PresentationSpeaker) return [];
|
||||
|
||||
$values = parent::serialize($expand, $fields, $relations, $params);
|
||||
|
||||
$summit_id = isset($params['summit_id'])? intval($params['summit_id']):null;
|
||||
$published = isset($params['published'])? intval($params['published']):true;
|
||||
$values['presentations'] = $speaker->getPresentationIds($summit_id, $published);
|
||||
$values['moderated_presentations'] = $speaker->getModeratedPresentationIds($summit_id, $published);
|
||||
$values['pic'] = Config::get("server.assets_base_url", 'https://www.openstack.org/') . 'profile_images/speakers/' . $speaker->getId();
|
||||
|
||||
if (in_array('member', $relations) && $speaker->hasMember())
|
||||
{
|
||||
@ -77,11 +84,17 @@ class PresentationSpeakerSerializer extends SilverStripeSerializer
|
||||
foreach (explode(',', $expand) as $relation) {
|
||||
switch (trim($relation)) {
|
||||
case 'presentations': {
|
||||
$presentations = array();
|
||||
foreach ($speaker->getPresentations() as $p) {
|
||||
$presentations = [];
|
||||
foreach ($speaker->getPresentations($summit_id, $published) as $p) {
|
||||
$presentations[] = SerializerRegistry::getInstance()->getSerializer($p)->serialize();
|
||||
}
|
||||
$values['presentations'] = $presentations;
|
||||
|
||||
$moderated_presentations = [];
|
||||
foreach ($speaker->getModeratedPresentation($summit_id, $published) as $p) {
|
||||
$moderated_presentations[] = SerializerRegistry::getInstance()->getSerializer($p)->serialize();
|
||||
}
|
||||
$values['moderated_presentations'] = $presentations;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -168,10 +168,16 @@ class PresentationSpeaker extends SilverstripeBaseModel
|
||||
*/
|
||||
private $presentations;
|
||||
|
||||
/**
|
||||
* @ORM\OneToMany(targetEntity="Presentation", mappedBy="moderator", cascade={"persist"})
|
||||
*/
|
||||
private $moderated_presentations;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->presentations = new ArrayCollection;
|
||||
$this->presentations = new ArrayCollection;
|
||||
$this->moderated_presentations = new ArrayCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -197,6 +203,22 @@ class PresentationSpeaker extends SilverstripeBaseModel
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null|int $summit_id
|
||||
* @param bool|true $published_ones
|
||||
* @return Presentation[]
|
||||
*/
|
||||
public function moderated_presentations($summit_id, $published_ones = true)
|
||||
{
|
||||
|
||||
return $this->moderated_presentations
|
||||
->filter(function($p) use($published_ones, $summit_id){
|
||||
$res = $published_ones? $p->isPublished(): true;
|
||||
$res &= is_null($summit_id)? true : $p->getSummit()->getId() == $summit_id;
|
||||
return $res;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $presentation_id
|
||||
* @return Presentation
|
||||
@ -205,6 +227,7 @@ class PresentationSpeaker extends SilverstripeBaseModel
|
||||
{
|
||||
return $this->presentations->get($presentation_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $summit_id
|
||||
* @param bool|true $published_ones
|
||||
@ -217,6 +240,43 @@ class PresentationSpeaker extends SilverstripeBaseModel
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $summit_id
|
||||
* @param bool|true $published_ones
|
||||
* @return array
|
||||
*/
|
||||
public function getPresentations($summit_id, $published_ones = true)
|
||||
{
|
||||
return $this->presentations($summit_id, $published_ones)->map(function($entity) {
|
||||
return $entity;
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param null $summit_id
|
||||
* @param bool|true $published_ones
|
||||
* @return array
|
||||
*/
|
||||
public function getModeratedPresentationIds($summit_id, $published_ones = true)
|
||||
{
|
||||
return $this->moderated_presentations($summit_id, $published_ones)->map(function($entity) {
|
||||
return $entity->getId();
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $summit_id
|
||||
* @param bool|true $published_ones
|
||||
* @return array
|
||||
*/
|
||||
public function getModeratedPresentations($summit_id, $published_ones = true)
|
||||
{
|
||||
return $this->moderated_presentations($summit_id, $published_ones)->map(function($entity) {
|
||||
return $entity;
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="models\main\File")
|
||||
|
@ -673,25 +673,13 @@ class Summit extends SilverstripeBaseModel
|
||||
*/
|
||||
private function buildModeratorsQuery()
|
||||
{
|
||||
$query = <<<SQL
|
||||
SELECT DISTINCT p0_.FirstName , p0_.LastName,
|
||||
p0_.Title , p0_.Bio , p0_.IRCHandle,
|
||||
p0_.TwitterName ,
|
||||
p0_.ID , p0_.Created, p0_.LastEdited,
|
||||
p0_.PhotoID,
|
||||
p0_.MemberID
|
||||
FROM PresentationSpeaker p0_
|
||||
|
||||
WHERE p0_.ID IN (
|
||||
SELECT Presentation.ModeratorID FROM Presentation INNER JOIN SummitEvent ON SummitEvent.ID = Presentation.ID
|
||||
WHERE SummitID = {$this->getId()} AND Published = 1
|
||||
)
|
||||
SQL;
|
||||
$rsm = new ResultSetMappingBuilder($this->getEM());
|
||||
$rsm->addRootEntityFromClassMetadata(\models\summit\PresentationSpeaker::class, 's', []);
|
||||
// build rsm here
|
||||
$native_query = $this->getEM()->createNativeQuery($query, $rsm);
|
||||
return $native_query;
|
||||
return $this->createQueryBuilder()
|
||||
->select('distinct ps')
|
||||
->from('models\summit\PresentationSpeaker','ps')
|
||||
->join('ps.moderated_presentations','p')
|
||||
->join('p.summit','s')
|
||||
->where("s.id = :summit_id and p.published = 1")
|
||||
->setParameter('summit_id', $this->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -712,7 +700,7 @@ SQL;
|
||||
*/
|
||||
public function getSpeakers(){
|
||||
// moderators
|
||||
$moderators = $this->buildModeratorsQuery()->getResult();
|
||||
$moderators = $this->buildModeratorsQuery()->getQuery()->getResult();
|
||||
// get moderators ids to exclude from speakers
|
||||
$moderators_ids = array();
|
||||
foreach($moderators as $m){
|
||||
|
@ -686,8 +686,9 @@ final class OAuth2SummitApiTest extends ProtectedApiTest
|
||||
public function testGetEvent(){
|
||||
$params = array
|
||||
(
|
||||
'id' => 6,
|
||||
'event_id' => 6838,
|
||||
'id' => 7,
|
||||
'event_id' => 15303,
|
||||
'expand' => 'speakers',
|
||||
);
|
||||
|
||||
$headers = array
|
||||
|
Loading…
x
Reference in New Issue
Block a user