Proyectos de Subversion Moodle

Rev

Rev 394 | Rev 396 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?php

class StaticsBlocks
{
    public $user;
    public $userID;
    public $currentUser;
    public $blockManager;
    public $blockExists;
    public $blockNames;
    public $regionName;

    public function __construct($regionName, $blockNames)
    {
        global $USER, $PAGE;
        require_login(null, false);

        if (isguestuser()) {
            throw new require_login_exception('Guests are not allowed here.');
        }

        $this->userID = optional_param('userid', $USER->id, PARAM_INT);
        $this->currentUser = $this->userID == $USER->id;
        $this->user = core_user::get_user($this->userID);
        $this->blockManager = $PAGE->blocks;
        $this->blockNames = $blockNames;
        $this->regionName = $regionName;

        if (!$this->user || !core_user::is_real_user($this->userID)) {
            throw new moodle_exception('invaliduser', 'error');
        }
    }

    public function addRegion()
    {
        if (!in_array($this->regionName, $this->blockManager->get_regions())) {
            $this->blockManager->add_region($this->regionName);
        }
    }

    public function validateIfExistBlocks()
    {
        // Obtener los bloques para la región especificada
        $blocks = $this->blockManager->get_blocks_for_region($this->regionName);

        // Verificamos que hay bloques disponibles y que el array de blockNames no esté vacío
        if (empty($this->blockNames) || empty($blocks)) {
            $this->blockExists = false; // No hay bloques que validar
            return $this->blockExists;
        }

        // Extraer los nombres de los bloques en un array
        $blockNamesInBlocks = array_map(fn($block) => $block->name(), $blocks);

        // Asumimos que todos los bloques existen inicialmente
        $this->blockExists = true;

        // Verificar si cada bloque en blockNames existe en blockNamesInBlocks
        foreach ($this->blockNames as $blockName) {
            if (!in_array($blockName, $blockNamesInBlocks, true)) {
                $this->blockExists = false; // Si falta algún bloque, lo marcamos
                break; // Salimos al encontrar el primer bloque faltante
            }
        }

        return $this->blockExists;
    }


    public function addBlocksIfNotExist()
    {
        if (!$this->blockExists) {
            foreach ($this->blockNames as $blockName) {
                $this->blockManager->add_block($blockName, $this->regionName, 1, true);
            }
        }
    }

    public function renderBlocks()
    {
        global $OUTPUT;

        $this->addRegion(); // Validar si la región existe y añadirla en caso de no existir

        $this->blockManager->load_blocks(true); // Cargar bloques en la región

        $this->validateIfExistBlocks(); // Valida si el listado de bloques pasados por parametros coincide con los bloques cargados
        // $this->addBlocksIfNotExist(); // Agrega los bloques en caso de no coincidir

        $blocks = $this->blockManager->get_blocks_for_region($this->regionName);
        $blockNamesInBlocks = array_map(fn($block) => $block->name(), $blocks);

        return $blockNamesInBlocks[0]; // Devolver los bloques de la región
    }
}