Your IP : 216.73.216.240


Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Sd/
Upload File :
Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Sd/SdQuiz.php

<?php
/*
* @package		Tech Fry Library
* @license		https://www.gnu.org/licenses/gpl-3.0.en.html
*/

namespace TechFry\Library\View\Sd;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;

// https://developers.google.com/search/docs/appearance/structured-data/education-qa
// https://developers.google.com/search/docs/appearance/structured-data/practice-problems
class SdQuiz
{   
    public static function get_output($data = array())
    {
        // $data: subject, level, topic/concept, question, answer

        $name = $data['topic'] ?? $data['concept'] ?? '';
        $subject = $data['subject'] ?? '';
        $level = $data['level'] ?? '';
        $question = $data['question'] ?? $data['description'] ?? $data['introtext'] ?? '';
        $answer = $data['answer'] ?? $data['fulltext'] ?? '';
        
        $json = array(
            "@context" => "https://schema.org",
            "@type" => "Quiz",
            "hasPart" => array(
                array(
                    "@context" => "https://schema.org/",
                    "@type" => "Question",
                    "eduQuestionType" => "Flashcard",
                    "text" => $question,
                    "acceptedAnswer" => array(
                        "@type" => "Answer",
                        "text" => $answer,
                    )
                ),
            )
        );

        if ($name)
        {
            $json["about"] = array(
                "@type" => "Thing",
                "name" => $name, // Underlying concept of the Quiz
            );
        }

        if ($subject)
        {
            $json["educationalAlignment"][] = array(
                "@type" => "AlignmentObject",
                "alignmentType" => "educationalSubject",
                "targetName" => $subject,
            );

        }

        if ($level)
        {  
            $json["educationalAlignment"][] = array(
                "@type" => "AlignmentObject",
                "alignmentType" => "educationalLevel",
                "targetName" => $level,
            );
        }

        $type = $data['question_type'] ?? $data['type'] ?? '';

        // Checkbox, Multiple choice
        if ($type)
        {
            $json["hasPart"]["eduQuestionType"] = $type;
            $json["hasPart"]["learningResourceType"] = "Practice problem";

            $answers = $data['answers'] ?? $data['options'] ?? '';
            $correct = (array) $correct;
            $i = 0;
            foreach ($answers as $answer)
            {
                if (\in_array($answer, $correct))
                {
                    $json["hasPart"]["acceptedAnswer"][] = array(
                        "@type" => "Answer",
                        "position" => $i,
                        "text" => $option,
                    );
                }
                else
                {
                    $json["hasPart"]["suggestedAnswer"][] = array(
                        "@type" => "Answer",
                        "position" => $i,
                        "text" => $option,
                    );
                }

                $i++;
            }
        }

        Sd::display_sd($json);
    }
}