<?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\access_helper;
use local_kid\local\capp_repository;

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

/**
 * Landing screen for the "Conseil d'apprentissage" main-menu entry: pick
 * which active CAPP session to fill in, then (via capp_grid_page's summary
 * mode) which student. Deliberately excludes the referentiel CRUD actions
 * (add/edit/toggle, rattachement) that belong only to Administration > CAPP
 * (capp_admin_page).
 */
class capp_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 {
        $context = \context_system::instance();
        $canexportrapport = access_helper::has_capability_or_teacher('local/kid:evaluate_capp', $context);

        $items = [];
        foreach (capp_repository::get_active_sessions_for_promotion($this->promotionid) as $session) {
            $item = [
                'titre' => format_string($session->libelle),
                'meta' => format_string($session->code),
                'openurl' => (new \moodle_url('/local/kid/pages/capp.php', ['sessionid' => $session->id]))->out(false),
            ];
            if ($canexportrapport) {
                $item['exporturl'] = (new \moodle_url('/local/kid/pages/capp_rapport.php',
                    ['sessionid' => $session->id, 'sesskey' => sesskey()]))->out(false);
            }
            $items[] = $item;
        }

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