<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

namespace local_kid\output;

use local_kid\local\stage_repository;

defined('MOODLE_INTERNAL') || die();

/**
 * Landing screen for the "Stages" main-menu entry: pick which active stage
 * to fill in, then (via stage_grid_page's summary mode) which student.
 * Deliberately excludes the referentiel CRUD actions (add/edit/toggle) that
 * belong only to Administration > Stages (stages_admin_page).
 */
class stages_picker_page implements \renderable, \templatable {

    protected int $promotionid;

    public function __construct(int $promotionid) {
        $this->promotionid = $promotionid;
    }

    public function export_for_template(\renderer_base $output): array {
        $items = [];
        foreach (stage_repository::get_for_promotion($this->promotionid) as $stage) {
            if (!$stage->active) {
                continue;
            }
            $periode = trim(
                ($stage->datedebut ? userdate($stage->datedebut, get_string('strftimedatefullshort')) : '?')
                . ' – ' .
                ($stage->datefin ? userdate($stage->datefin, get_string('strftimedatefullshort')) : '?')
            );
            $items[] = [
                'titre' => format_string($stage->libelle),
                'meta' => ($stage->datedebut || $stage->datefin) ? $periode : '',
                'openurl' => (new \moodle_url('/local/kid/pages/stages.php', ['stageid' => $stage->id]))->out(false),
            ];
        }

        return [
            'introlabel' => get_string('picker_intro_stages', 'local_kid'),
            'hasitems' => !empty($items),
            'items' => $items,
            'message' => get_string('picker_empty_stages', 'local_kid'),
        ];
    }
}
