<commit_before><?php
@_API_EXEC === 1 or die('Restricted access.');

require_once($_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/AnonymousField.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/Database.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/Endpoint.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/Field.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/Lesson.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/Role.php');

require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/exceptions/MissingArgumentException.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/exceptions/NotFoundException.php');

require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/endpoints/accountEndpoint.php');

require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/endpoints/lessonCompetenceEndpoint.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/endpoints/lessonFieldEndpoint.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/endpoints/lessonPDFEndpoint.php');

use Ramsey\Uuid\Uuid;

$lessonEndpoint = new OdyMaterialyAPI\Endpoint('lesson');
$lessonEndpoint->addSubEndpoint('competence', $lessonCompetenceEndpoint);
$lessonEndpoint->addSubEndpoint('field', $lessonFieldEndpoint);
$lessonEndpoint->addSubEndpoint('pdf', $lessonPDFEndpoint);

function populateField($db, $field)
{
	global $accountEndpoint;

	$competence_sql = <<<SQL
SELECT competences.id, competences.number
FROM competences
JOIN competences_for_lessons ON competences.id = competences_for_lessons.competence_id
WHERE competences_for_lessons.lesson_id = ?
ORDER BY competences.number;
SQL;
	$group_sql = <<<SQL
SELECT group_id FROM groups_for_lessons
WHERE lesson_id = ?;
SQL;

	$db->execute();
	$lesson_id = '';
	$lesson_name = '';
	$lesson_version = '';
	$db->bind_result($lesson_id, $lesson_name, $lesson_version);

	while($db->fetch())
	{
		$db2 = new OdymaterialyAPI\Database();

		$loginState = $accountEndpoint->call('GET', ['no-avatar' => 'true']);
		if($loginState['status'] == '200')
		{
			$groups = $loginState['response']['groups'];
			$groups[] = '00000000-0000-0000-0000-000000000000';
		}
		else
		{
			$groups = ['00000000-0000-0000-0000-000000000000'];
		}
		$db2->prepare($group_sql);
		$db2->bind_param('s', $lesson_id);
		$db2->execute();
		$group_id = '';
		$db2->bind_result($group_id);
		while($db2->fetch())
		{
			if(in_array(Uuid::fromBytes($group_id)->toString(), $groups))
			{
				// Create a new Lesson in the newly-created Field
				$field->lessons[] = new OdyMaterialyAPI\Lesson($lesson_id, $lesson_name, $lesson_version);

				// Find out the competences this Lesson belongs to
				$db3 = new OdymaterialyAPI\Database();
				$db3->prepare($competence_sql);
				$db3->bind_param('s', $lesson_id);
				$db3->execute();
				$competence_id = '';
				$competence_number = '';
				$db3->bind_result($competence_id, $competence_number);
				end($field->lessons)->lowestCompetence = 0;
				if($db3->fetch())
				{
					end($field->lessons)->lowestCompetence = $competence_number;
					end($field->lessons)->competences[] = $competence_id;
				}
				else
				{
					end($field->lessons)->lowestCompetence = 0;
				}
				while($db3->fetch())
				{
					end($field->lessons)->competences[] = $competence_id;
				}
				break;
			}
		}
	}
}

$listLessons = function($skautis, $data, $endpoint)
{
	$field_sql = <<<SQL
SELECT id, name
FROM fields;
SQL;
	$anonymous_sql = <<<SQL
SELECT lessons.id, lessons.name, lessons.version
FROM lessons
LEFT JOIN lessons_in_fields ON lessons.id = lessons_in_fields.lesson_id
WHERE lessons_in_fields.field_id IS NULL;
SQL;
	$lesson_sql = <<<SQL
SELECT lessons.id, lessons.name, lessons.version
FROM lessons
JOIN lessons_in_fields ON lessons.id = lessons_in_fields.lesson_id
WHERE lessons_in_fields.field_id = ?;
SQL;

	$fields = [new OdymaterialyAPI\AnonymousField()];

	$db = new OdymaterialyAPI\Database();
	$db->prepare($anonymous_sql);
	populateField($db, end($fields));

	// Select all the fields in the database<commit_msg>lesson group checking into function<commit_after><?php
@_API_EXEC === 1 or die('Restricted access.');

require_once($_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/AnonymousField.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/Database.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/Endpoint.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/Field.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/Lesson.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/Role.php');

require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/exceptions/MissingArgumentException.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/internal/exceptions/NotFoundException.php');

require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/endpoints/accountEndpoint.php');

require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/endpoints/lessonCompetenceEndpoint.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/endpoints/lessonFieldEndpoint.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/API/v0.9/endpoints/lessonPDFEndpoint.php');

use Ramsey\Uuid\Uuid;

$lessonEndpoint = new OdyMaterialyAPI\Endpoint('lesson');
$lessonEndpoint->addSubEndpoint('competence', $lessonCompetenceEndpoint);
$lessonEndpoint->addSubEndpoint('field', $lessonFieldEndpoint);
$lessonEndpoint->addSubEndpoint('pdf', $lessonPDFEndpoint);

function checkLessonGroup($lesson_id)
{
	global $accountEndpoint;

	$group_sql = <<<SQL
SELECT group_id FROM groups_for_lessons
WHERE lesson_id = ?;
SQL;

	$loginState = $accountEndpoint->call('GET', ['no-avatar' => 'true']);
	if($loginState['status'] == '200')
	{
		$groups = $loginState['response']['groups'];
		$groups[] = '00000000-0000-0000-0000-000000000000';
	}
	else
	{
		$groups = ['00000000-0000-0000-0000-000000000000'];
	}

	$db = new OdymaterialyAPI\Database();
	$db->prepare($group_sql);
	$db->bind_param('s', $lesson_id);
	$db->execute();
	$group_id = '';
	$db->bind_result($group_id);
	while($db->fetch())
	{
		if(in_array(Uuid::fromBytes($group_id)->toString(), $groups))
		{
			return true;
		}
	}
	return false;
}

function populateField($db, $field)
{
	$competence_sql = <<<SQL
SELECT competences.id, competences.number
FROM competences
JOIN competences_for_lessons ON competences.id = competences_for_lessons.competence_id
WHERE competences_for_lessons.lesson_id = ?
ORDER BY competences.number;
SQL;

	$db->execute();
	$lesson_id = '';
	$lesson_name = '';
	$lesson_version = '';
	$db->bind_result($lesson_id, $lesson_name, $lesson_version);

	while($db->fetch())
	{
		if(checkLessonGroup($lesson_id))
		{
			// Create a new Lesson in the newly-created Field
			$field->lessons[] = new OdyMaterialyAPI\Lesson($lesson_id, $lesson_name, $lesson_version);

			// Find out the competences this Lesson belongs to
			$db2 = new OdymaterialyAPI\Database();
			$db2->prepare($competence_sql);
			$db2->bind_param('s', $lesson_id);
			$db2->execute();
			$competence_id = '';
			$competence_number = '';
			$db2->bind_result($competence_id, $competence_number);
			end($field->lessons)->lowestCompetence = 0;
			if($db2->fetch())
			{
				end($field->lessons)->lowestCompetence = $competence_number;
				end($field->lessons)->competences[] = $competence_id;
			}
			else
			{
				end($field->lessons)->lowestCompetence = 0;
			}
			while($db2->fetch())
			{
				end($field->lessons)->competences[] = $competence_id;
			}
		}
	}
}

$listLessons = function($skautis, $data, $endpoint)
{
	$field_sql = <<<SQL
SELECT id, name
FROM fields;
SQL;
	$anonymous_sql = <<<SQL
SELECT lessons.id, lessons.name, lessons.version
FROM lessons
LEFT JOIN lessons_in_fields ON lessons.id = lessons_in_fields.lesson_id
WHERE lessons_in_fields.field_id IS NULL;
SQL;
	$lesson_sql = <<<SQL
SELECT lessons.id, lessons.name, lessons.version
FROM lessons
JOIN lessons_in_fields ON lessons.id = lessons_in_fields.lesson_id
WHERE lessons_in_fields.field_id = ?;
SQL;

	$fields = [new OdymaterialyAPI\AnonymousField()];

	$db = new OdymaterialyAPI\Database();
	$db->prepare($anonymous_sql);
	populateField($db, end($fields));

	// Select all the fields in the database
{'fraction_non_alphanumeric': 0.1718282912830324, 'fraction_numerical': 0.024871457610905177, 'mean_word_length': 5.56, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1993262', 'n_tokens_mistral': 3292, 'n_tokens_neox': 2972, 'n_words': 443}