Proyectos de Subversion Moodle

Rev

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

<?php
require_once(__DIR__ . '/cesa.php');

class StaticsBlocks extends Cesa
{

    public function __construct($title)
    {
        global $USER, $PAGE, $SITE;
        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->title = get_string($title);
        $this->blockManager = $PAGE->blocks;

        // Definimos varias regiones
        $this->regions = ['side-pre'];
        $this->blockNames = ['cesa_course_rating', 'comments'];
        $this->blockExists = true;

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

        // Asignar y validar los bloques en todas las regiones
        foreach ($this->regions as $region) {
            $this->regionName = $region;
            $this->addRegion(); // Añadir la región si no existe
            $this->validateIfExistBlocks(); // Validar si los bloques existen
            $this->addBlocksIfNotExist(); // Añadir bloques si no existen
        }
    }

    public function renderBlocks()
    {
        global $OUTPUT;

        $blocksView = '';
        // Renderizar bloques para cada región
        foreach ($this->regions as $region) {
            $this->regionName = $region;
            $this->blockManager->load_blocks(true); // Cargar bloques en la región
            $blocksView .= $OUTPUT->blocks_for_region($this->regionName); // Renderizar bloques
        }

        return $blocksView; // Devolver bloques renderizados
    }

    public function addRegion()
    {
        // Añadir la región si no existe en blockManager
        if (!in_array($this->regionName, $this->blockManager->get_regions())) {
            $this->blockManager->add_region($this->regionName);
        }
    }

    public function validateIfExistBlocks()
    {
        $this->blockExists = true;
        $this->blocks = $this->blockManager->get_blocks_for_region($this->regionName);

        foreach ($this->blockNames as $blockName) {
            $blockFound = false;
            foreach ($this->blocks as $block) {
                $blockclass = get_class($block);
                if ($blockclass == 'block_' . $blockName) {
                    $blockFound = true;
                    break;
                }
            }
            if (!$blockFound) {
                $this->blockExists = false;
                break;
            }
        }
        return $this->blockExists;
    }

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

/* // Instanciamos y renderizamos la página con los bloques estáticos
$statics_blocks = new StaticsBlocks();
echo $statics_blocks->renderBlocks(); */