Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-9150: modify schoolyear settings and TPR ontologyword details name #160

Merged
merged 5 commits into from
Nov 16, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
UHF-9150: Add comprehensive schoolyear setting in addition to high sc…
…hool year.
  • Loading branch information
Jussiles committed Nov 7, 2023
commit 01cad11c7cfeb0b0ac59db075afbc48251b5d2c5
19 changes: 19 additions & 0 deletions modules/helfi_school_addons/helfi_school_addons.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @file
* Contains installation functions for Helfi school addons.
*/

declare(strict_types = 1);

use Drupal\helfi_school_addons\SchoolUtility;

/**
*
*/
function helfi_school_addons_update_9001() {
$highSchoolYear = \Drupal::state()->get('helfi_school_addons.school_year') ? (int) \Drupal::state()->get('helfi_school_addons.school_year') : '2023-2024';
SchoolUtility::setCurrentHighSchoolYear(SchoolUtility::composeSchoolYear($highSchoolYear));
SchoolUtility::setCurrentComprehensiveSchoolYear('2023-2024');
}
56 changes: 22 additions & 34 deletions modules/helfi_school_addons/src/Form/SchoolSettingsForm.php
Original file line number Diff line number Diff line change
@@ -24,19 +24,33 @@ public function getFormId() {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$currentSchoolYear = SchoolUtility::getCurrentSchoolYear();
$currentHighSchoolYear = SchoolUtility::getCurrentHighSchoolYear();
$currentComprehensiveSchoolYear = SchoolUtility::getCurrentComprehensiveSchoolYear();

$form['current_school_year_info'] = [
'#markup' => '<p>' . t('Current school year:') . ' ' . ($currentSchoolYear ? $currentSchoolYear : '-') . '</p>',
'#markup' => '<p>' . t('Current high school year:') . ' ' . ($currentHighSchoolYear ? $currentHighSchoolYear : '-') . '</p>',
];

$form['school_year_first'] = [
$form['high_school_year_first'] = [
'#type' => 'number',
'#title' => $this->t('Starting year for school year'),
'#title' => $this->t('Starting year for high school year'),
'#min' => 2020,
'#max' => 9999,
'#default_value' => ($currentSchoolYear ? $this->splitStartYear($currentSchoolYear) : ''),
'#description' => t('Select the starting year for a school year period. For example, selecting "2022" would set the school year to "2022-2023".'),
'#default_value' => ($currentHighSchoolYear ? SchoolUtility::splitStartYear($currentHighSchoolYear) : ''),
'#description' => t('Select the starting year for a high school year period. For example, selecting "2022" would set the school year to "2022-2023".'),
];

$form['current_comprehensive_school_year_info'] = [
'#markup' => '<p>' . t('Current comprehensive school year:') . ' ' . ($currentComprehensiveSchoolYear ? $currentComprehensiveSchoolYear : '-') . '</p>',
];

$form['comprehensive_school_year_first'] = [
'#type' => 'number',
'#title' => $this->t('Starting year for comprehensive school year'),
'#min' => 2020,
'#max' => 9999,
'#default_value' => ($currentComprehensiveSchoolYear ? SchoolUtility::splitStartYear($currentComprehensiveSchoolYear) : ''),
'#description' => t('Select the starting year for a comprehensive school year period. For example, selecting "2022" would set the school year to "2022-2023".'),
];

$form['submit'] = [
@@ -52,33 +66,7 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
SchoolUtility::setCurrentSchoolYear($this->composeSchoolYear((int) $form_state->getValue('school_year_first')));
}

/**
* Gets the start year from a school year period.
*
* @param string $schoolYear
* The school year, e.g. '2022-2023'.
*
* @return string
* The year.
*/
private function splitStartYear(string $schoolYear): string {
return strtok($schoolYear, '-');
}

/**
* Gets the school year from a starting year.
*
* @param int $firstYear
* The year.
*
* @return string
* The school year, e.g. '2022-2023'.
*/
private function composeSchoolYear(int $firstYear): string {
return $firstYear . '-' . strval($firstYear + 1);
SchoolUtility::setCurrentHighSchoolYear(SchoolUtility::composeSchoolYear((int) $form_state->getValue('high_school_year_first')));
SchoolUtility::setCurrentComprehensiveSchoolYear(SchoolUtility::composeSchoolYear((int) $form_state->getValue('comprehensive_school_year_first')));
}

}
Original file line number Diff line number Diff line change
@@ -86,7 +86,7 @@ protected function queryByWordId(int $wordId) {
$diJoin = Views::pluginManager('join')->createInstance('standard', $diConfiguration);
$this->query->addRelationship('di', $diJoin, 'owd_fd');

$schoolYear = SchoolUtility::getCurrentSchoolYear();
$schoolYear = SchoolUtility::getCurrentHighSchoolYear();
if ($schoolYear) {
$this->query->addWhere('AND', 'di.detail_items_schoolyear', $schoolYear);
}
@@ -106,7 +106,7 @@ protected function generateOptions(): array {
}

$langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
$schoolYear = SchoolUtility::getCurrentSchoolYear();
$schoolYear = SchoolUtility::getCurrentHighSchoolYear();
if ($schoolYear === NULL) {
return [];
}
Original file line number Diff line number Diff line change
@@ -80,7 +80,7 @@ public function query() {
$diJoin = Views::pluginManager('join')->createInstance('standard', $diConfiguration);
$this->query->addRelationship('di_spt', $diJoin, 'owd_fd_spt');

$schoolYear = SchoolUtility::getCurrentSchoolYear();
$schoolYear = SchoolUtility::getCurrentHighSchoolYear();
if ($schoolYear) {
$this->query->addWhere('AND', 'di_spt.detail_items_schoolyear', $schoolYear);
}
61 changes: 54 additions & 7 deletions modules/helfi_school_addons/src/SchoolUtility.php
Original file line number Diff line number Diff line change
@@ -12,26 +12,73 @@ class SchoolUtility {
/**
* School year key used with State API.
*/
private const SCHOOL_YEAR_KEY = 'helfi_school_addons.school_year';
private const HIGH_SCHOOL_YEAR_KEY = 'helfi_school_addons.high_school_year';
private const COMPREHENSIVE_SCHOOL_YEAR_KEY = 'helfi_school_addons.comprehensive_school_year';

/**
* Helper function to get the current school year.
* Helper function to get the current high school year.
*
* @return string|null
* The current school year, e.g. "2022-2023".
*/
public static function getCurrentSchoolYear(): ?string {
return \Drupal::state()->get(self::SCHOOL_YEAR_KEY);
public static function getCurrentHighSchoolYear(): ?string {
return \Drupal::state()->get(self::HIGH_SCHOOL_YEAR_KEY);
}

/**
* Helper function to set the current school year.
* Helper function to get the current comprehensive school year.
*
* @return string|null
* The current school year, e.g. "2022-2023".
*/
public static function getCurrentComprehensiveSchoolYear(): ?string {
return \Drupal::state()->get(self::COMPREHENSIVE_SCHOOL_YEAR_KEY);
}

/**
* Helper function to set the current high school year.
*
* @param string $school_year
* The current school year, e.g. "2022-2023".
*/
public static function setCurrentHighSchoolYear(string $school_year) {
\Drupal::state()->set(self::HIGH_SCHOOL_YEAR_KEY, $school_year);
}

/**
* Helper function to set the comprehensive current school year.
*
* @param string $school_year
* The current school year, e.g. "2022-2023".
*/
public static function setCurrentSchoolYear(string $school_year) {
\Drupal::state()->set(self::SCHOOL_YEAR_KEY, $school_year);
public static function setCurrentComprehensiveSchoolYear(string $school_year) {
\Drupal::state()->set(self::COMPREHENSIVE_SCHOOL_YEAR_KEY, $school_year);
}

/**
* Gets the school year from a starting year.
*
* @param int $firstYear
* The year.
*
* @return string
* The school year, e.g. '2022-2023'.
*/
public static function composeSchoolYear(int $firstYear): string {
return $firstYear . '-' . strval($firstYear + 1);
}

/**
* Gets the start year from a school year period.
*
* @param string $schoolYear
* The school year, e.g. '2022-2023'.
*
* @return string
* The year.
*/
public static function splitStartYear(string $schoolYear): string {
return strtok($schoolYear, '-');
}

}
Loading