trinion_suo-1.0.x-dev/trinion_suo.module
trinion_suo.module
<?php
/**
* Implements hook_form_alter().
*/
function trinion_suo_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if ($form_id == 'user_login_form') {
$form['#submit'][] = 'trinion_suo_login_submit';
}
}
function trinion_suo_login_submit($form, \Drupal\Core\Form\FormStateInterface $form_state) {
$user = \Drupal\user\Entity\User::load(Drupal::currentUser()->id());
if ($user->get('field_ts_uchenik')->getString()) {
$form_state->setRedirect('view.suo_user_courses.page_1');
}
}
/**
* Implements hook_mail().
*/
function trinion_suo_mail($key, &$message, $params) {
switch ($key) {
case 'course_buy_notice':
$message['subject'] = 'Учетные данные для прохождения курса';
$message['body'][] = "Добрый день, {$params['name']},";
$message['body'][] = "Ваши учетные данные для прохождения курса:";
$message['body'][] = "Логин: {$params['mail']}";
$message['body'][] = "Пароль: {$params['pass']}";
$message['body'][] = "Ссылка на авторизацию:";
$message['body'][] = "https://trinion.org/user";
$message['body'][] = "Ссылка на курс:";
$message['body'][] = "https://trinion.org/bpmn-course";
$message['body'][] = "Благодарим за покупку!";
break;
}
}
function trinion_suo_node_access(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $account) {
if ($entity->bundle() == 'urok_kursa') {
if ($operation == 'view')
return \Drupal::service("trinion_suo.course")->checkLessonAccess($entity) ? \Drupal\Core\Access\AccessResult::allowed() : \Drupal\Core\Access\AccessResult::forbidden();
}
}
/**
* Implements hook_theme().
*/
function trinion_suo_theme() {
$theme = [
'complete_lesson_button' => [
'variables' => [
'nid' => [],
],
],
'course_plan_menu' => [
'variables' => [
'categories' => [],
],
],
'course_structure_admin' => [
'variables' => [
'categories' => NULL,
],
'template' => 'course_structure_admin',
],
];
return $theme;
}
/**
* Implements hook_entity_extra_field_info().
*/
function trinion_suo_entity_extra_field_info() {
$extra = [];
$extra['node']['urok_kursa'] = [
'display' => [
'complete_lesson_button' => [
'label' => 'Кнопка пройденности урока',
'weight' => '0'
]
]
];
$extra['node']['kurs_obucheniya'] = [
'display' => [
'kolichestvo_video' => [
'label' => 'Количество видео уроков в курсе',
'weight' => '0'
],
'prodolzhitelnost_video' => [
'label' => 'Продолжительность видео уроков в курсе',
'weight' => '0'
],
'kolichestvo_voprosov_testah' => [
'label' => 'Количество вопросов в тестах',
'weight' => '0'
],
'kolichestvo_domashnih_zadaniy' => [
'label' => 'Количество домашних заданий',
'weight' => '0'
],
'kolichestvo_statey' => [
'label' => 'Количество статей',
'weight' => '0'
],
'kolichestvo_skachivaniy' => [
'label' => 'Количество скачиваний',
'weight' => '0'
]
]
];
return $extra;
}
function trinion_suo_node_view(array &$build, \Drupal\node\NodeInterface $node, $display, $view_mode) {
if ($display->getComponent('complete_lesson_button')) {
$is_completed = Drupal::service('trinion_suo.course')->isLessonCompleted($node);
$next_nid = \Drupal::service('trinion_suo.course')->getNextLesson($node);
if ($next_nid && empty($is_completed))
$build['complete_lesson_button'] = [
'#theme' => 'complete_lesson_button',
'#nid' => $node->id(),
];
}
if ($display->getComponent('kolichestvo_video')) {
$cours_tid = $node->get('field_ts_kategoriya_kursa')->getValue()[0]['target_id'];
$categories = Drupal::service('trinion_suo.course')->getCategories($cours_tid);
$cnt = 0;
foreach ($categories as $cat) {
foreach ($cat['sub_categories'] as $sub_categoriy) {
foreach ($sub_categoriy['lessons'] as $lesson) {
if ($lesson->hasField('field_ts_vid_uroka') && $lesson->get('field_ts_vid_uroka')->getString() == 'Видео')
$cnt++;
}
}
}
$build['kolichestvo_video']['#markup'] = $cnt;
}
// if ($display->getComponent('prodolzhitelnost_video')) {
// $cours_tid = $node->get('field_ts_kategoriya_kursa')->getValue()[0]['target_id'];
// $categories = Drupal::service('trinion_suo.course')->getCategories($cours_tid);
// $length = 0;
// foreach ($categories as $cat) {
// $length += $cat['length'];
// }
// $build['prodolzhitelnost_video']['#markup'] = $length;
// }
if ($display->getComponent('kolichestvo_voprosov_testah')) {
$cours_tid = $node->get('field_ts_kategoriya_kursa')->getValue()[0]['target_id'];
$categories = Drupal::service('trinion_suo.course')->getCategories($cours_tid);
$cnt = 0;
foreach ($categories as $cat) {
foreach ($cat['sub_categories'] as $sub_categoriy) {
foreach ($sub_categoriy['lessons'] as $lesson) {
if ($lesson->bundle() == 'test') {
$cnt += count($lesson->get('field_ts_questions'));
}
}
}
}
$build['kolichestvo_voprosov_testah']['#markup'] = $cnt;
}
if ($display->getComponent('kolichestvo_domashnih_zadaniy')) {
$cours_tid = $node->get('field_ts_kategoriya_kursa')->getValue()[0]['target_id'];
$categories = Drupal::service('trinion_suo.course')->getCategories($cours_tid);
$cnt = 0;
foreach ($categories as $cat) {
foreach ($cat['sub_categories'] as $sub_categoriy) {
foreach ($sub_categoriy['lessons'] as $lesson) {
if ($lesson->hasField('field_ts_vid_uroka') && $lesson->get('field_ts_vid_uroka')->getString() == 'Практическое задание')
$cnt++;
}
}
}
$build['kolichestvo_domashnih_zadaniy']['#markup'] = $cnt;
}
}
function trinion_suo_views_query_alter(\Drupal\views\ViewExecutable $view, \Drupal\views\Plugin\views\query\QueryPluginBase $query) {
if ($view->id() == 'suo_user_courses') {
$user = \Drupal\user\Entity\User::load(Drupal::currentUser()->id());
$course_access_tid = [0];
foreach ($user->get('field_ts_course_access') as $item)
$course_access_tid[] = $item->getString();
$query->addWhere('0', 'taxonomy_term_field_data_node__field_ts_kategoriya_kursa.tid', $course_access_tid, 'IN');
}
}
/**
* Implements hook_views_data_alter().
*/
function trinion_suo_views_data_alter(array &$data) {
$data['node']['goto_last_lesson'] = [
'title' => t('Last lesson link'),
'field' => [
'title' => t('Last lesson link'),
'id' => 'goto_last_lesson',
'help' => t('Last lesson link in course'),
],
];
}
function trinion_suo_theme_suggestions_form_element_alter(array &$suggestions, array $variables) {
if (isset($variables['element']['#array_parents'][0]) && $variables['element']['#array_parents'][0] == 'question' && isset($variables['element']['#array_parents'][1]) && $variables['element']['#array_parents'][1] == 'quest')
$suggestions[] = 'form_element__test_question__' . $variables['element']['#type'];
}
function trinion_suo_theme_suggestions_fieldset_alter(array &$suggestions, array $variables) {
if (isset($variables['element']['#array_parents'][0]) && $variables['element']['#array_parents'][0] == 'question' && isset($variables['element']['#array_parents'][1]) && $variables['element']['#array_parents'][1] == 'quest')
$suggestions[] = 'fieldset__test_question__' . $variables['element']['#type'];
}
function trinion_suo_trinion_base_inline_show_fields($field_name, $entity) {
$build = [];
if ($field_name == 'weight') {
$build = [
'#type' => 'textfield',
'#value' => $entity->getWeight(),
];
}
elseif ($field_name == 'category') {
$category = $entity->get('parent')->entity;
$course = $category->get('parent')->entity;
$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('course_categories', $course->id(), 1, true);
$options = [];
foreach ($tree as $term) {
$options[$term->id()] = $term->label();
}
$build = [
'#type' => 'select',
'#options' => $options,
'#value' => $category->id(),
];
}
elseif ($field_name == 'lesson') {
$category = $entity->get('field_ts_kategoriya_kursa')->entity;
$parent_category = $category->get('parent')->entity;
$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('course_categories', $parent_category->id(), 1, true);
$options = [];
foreach ($tree as $term) {
$options[$term->id()] = $term->label();
}
$build = [
'#type' => 'select',
'#options' => $options,
'#value' => $category->id(),
];
}
elseif ($field_name == 'field_ts_lesson_number') {
$build = [
'#type' => 'textfield',
'#value' => $entity->get('field_ts_lesson_number')->getString(),
];
}
elseif ($field_name == 'field_ts_length') {
$build = [
'#type' => 'textfield',
'#value' => $entity->get('field_ts_length')->getString(),
];
}
return $build;
}
function trinion_suo_trinion_base_inline_change_fields($field_name, $entity, $new_value) {
$build = NULL;
if ($field_name == 'weight') {
$new_value = (int)$new_value;
$entity->set('weight', $new_value);
$entity->save();
$build = (string)$new_value;
}
elseif ($field_name == 'category') {
$new_value = (int)$new_value;
$entity->set('parent', $new_value);
$entity->save();
$term = \Drupal\taxonomy\Entity\Term::load($new_value);
$build = (string)$term->label();
}
elseif ($field_name == 'lesson') {
$new_value = (int)$new_value;
$entity->set('field_ts_kategoriya_kursa', $new_value);
$entity->save();
$term = \Drupal\taxonomy\Entity\Term::load($new_value);
$build = (string)$term->label();
}
elseif ($field_name == 'field_ts_lesson_number') {
$new_value = (int)$new_value;
$entity->set('field_ts_lesson_number', $new_value);
$entity->save();
$build = (string)$new_value;
}
elseif ($field_name == 'field_ts_length') {
$new_value = (int)$new_value;
$entity->set('field_ts_length', $new_value);
$entity->save();
$build = (string)$new_value;
}
return $build;
}
