ContentTypeMeeting.php in Opigno Moxtra 3.x
File
src/Plugin/OpignoGroupManagerContentType/ContentTypeMeeting.php
View source
<?php
namespace Drupal\opigno_moxtra\Plugin\OpignoGroupManagerContentType;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\Url;
use Drupal\opigno_group_manager\ContentTypeBase;
use Drupal\opigno_group_manager\OpignoGroupContent;
use Drupal\opigno_moxtra\Entity\Meeting;
use Symfony\Component\HttpFoundation\Request;
class ContentTypeMeeting extends ContentTypeBase {
public function shouldShowNext() {
return FALSE;
}
public function getUserScore($user_id, $entity_id) {
$results = \Drupal::entityTypeManager()
->getStorage('opigno_moxtra_meeting_result')
->loadByProperties([
'user_id' => $user_id,
'meeting' => $entity_id,
]);
$best_score = 0;
foreach ($results as $result) {
$score = $result
->getScore();
if ($score > $best_score) {
$best_score = $score;
}
}
return $best_score / 100;
}
public function getViewContentUrl($entity_id) {
return Url::fromRoute('entity.opigno_moxtra_meeting.canonical', [
'opigno_moxtra_meeting' => $entity_id,
]);
}
public function getStartContentUrl($content_id, $group_id = NULL) {
return Url::fromRoute('opigno_moxtra.meeting', [
'opigno_moxtra_meeting' => $content_id,
]);
}
public function getContent($meeting) {
if (is_numeric($meeting)) {
$meeting = Meeting::load($meeting);
}
if ($meeting === NULL || $meeting === FALSE) {
return FALSE;
}
return new OpignoGroupContent($this
->getPluginId(), $this
->getEntityType(), $meeting
->id(), $meeting
->label(), $this
->getDefaultModuleImageUrl(), t('Default image'));
}
public function getDefaultModuleImageUrl() {
$request = \Drupal::request();
$path = \Drupal::service('module_handler')
->getModule('opigno_module')
->getPath();
return $request
->getBasePath() . '/' . $path . '/img/img_module.svg';
}
public function getContentFromRequest(Request $request) {
$meeting = $request
->get('opigno_moxtra_meeting');
if ($meeting === NULL || $meeting === FALSE) {
return FALSE;
}
return $this
->getContent($meeting);
}
public function getFormObject($entity_id = NULL) {
if (empty($entity_id)) {
$form = \Drupal::entityTypeManager()
->getFormObject($this
->getEntityType(), 'add');
$entity = Meeting::create();
}
else {
$form = \Drupal::entityTypeManager()
->getFormObject($this
->getEntityType(), 'edit');
$entity = Meeting::load($entity_id);
}
$form
->setEntity($entity);
return $form;
}
public function getAllContents() {
try {
$meetings = \Drupal::entityTypeManager()
->getStorage('opigno_moxtra_meeting')
->loadMultiple();
} catch (InvalidPluginDefinitionException $e) {
return FALSE;
}
$contents = [];
foreach ($meetings as $meeting) {
$contents[] = $this
->getContent($meeting);
}
return $contents;
}
public function getAvailableContents() {
return $this
->getAllContents();
}
}