<?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.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * The view_cover class
 *
 * @package   mod_surveypro
 * @copyright 2013 onwards kordan <stringapiccola@gmail.com>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace mod_surveypro;

use mod_surveypro\utility_layout;

/**
 * The class managing the page "cover" of the module
 *
 * @package   mod_surveypro
 * @copyright 2013 onwards kordan <stringapiccola@gmail.com>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class view_cover
{
    /**
     * @var \stdClass Course module object
     */
    protected $cm;

    /**
     * @var \stdClass Context object
     */
    protected $context;

    /**
     * @var \stdClass Surveypro object
     */
    protected $surveypro;

    /**
     * Class constructor.
     *
     * @param object $cm
     * @param object $context
     * @param object $surveypro
     */
    public function __construct($cm, $context, $surveypro) {
        $this->cm = $cm;
        $this->context = $context;
        $this->surveypro = $surveypro;
    }

    /**
     * Display the overview page.
     *
     * @return void
     */
    public function display_cover() {
        global $OUTPUT, $COURSE, $USER;

        $utilitylayoutman = new utility_layout($this->cm, $this->surveypro);

        $labelsep = get_string('labelsep', 'langconfig'); // Separator usually is ': '..

        $cansubmit = has_capability('mod/surveypro:submit', $this->context);
        $canmanageitems = has_capability('mod/surveypro:manageitems', $this->context);
        $canignoremaxentries = has_capability('mod/surveypro:ignoremaxentries', $this->context);
        $canaccessreserveditems = has_capability('mod/surveypro:accessreserveditems', $this->context);

        $itemcount = $utilitylayoutman->has_items(0, SURVEYPRO_TYPEFIELD, $canmanageitems, $canaccessreserveditems, true);

        $messages = [];
        $timenow = time();

        // User submitted responses.
        $countclosed = $utilitylayoutman->has_submissions(true, SURVEYPRO_STATUSCLOSED, $USER->id);
        $inprogress = $utilitylayoutman->has_submissions(true, SURVEYPRO_STATUSINPROGRESS, $USER->id);
        $next = $countclosed + $inprogress + 1;

        // Begin of: is the button to add one more response going to be displayed?
        $addnew = $utilitylayoutman->is_newresponse_allowed($next);
        // End of: is the button to add one more response going to be displayed?

        // Number of elements.
        // If you can not manage items, you do not want to know their number.
        if ($itemcount && $canmanageitems) {
            $a = $itemcount;
            $message = get_string('count_allitems', 'mod_surveypro', $a);
            if ($canmanageitems) {
                // If I $canmanageitems in $itemcount items counted were: visible + hidden.
                $message .= ' ';
                $visibleonly = $utilitylayoutman->has_items(0, SURVEYPRO_TYPEFIELD, false, $canaccessreserveditems, true);
                $a = $itemcount - $visibleonly;
                $message .= get_string('count_hiddenitems', 'mod_surveypro', $a);
            }
            $messages[] = $message;
        }

        // Number of pages.
        $pagecount = $utilitylayoutman->assign_pages();
        if ($pagecount > 1) {
            $messages[] = get_string('count_pages', 'mod_surveypro', $pagecount);
        }

        if ($cansubmit) {
            if (empty($this->surveypro->maxentries) || $canignoremaxentries) {
                $maxentries = get_string('unlimited', 'mod_surveypro');
            } else {
                $maxentries = $this->surveypro->maxentries;
            }
            $messages[] = get_string('maxentries', 'mod_surveypro') . $labelsep . $maxentries;

            // Your 'closed' responses.
            $a = new \stdClass();
            $a->status = get_string('statusclosed', 'mod_surveypro');
            $a->responsescount = $countclosed;
            $messages[] = get_string('yoursubmissions', 'mod_surveypro', $a);

            $pasuseresumesurvey = ($this->surveypro->pauseresume == SURVEYPRO_PAUSERESUMENOEMAIL);
            $pasuseresumesurvey = $pasuseresumesurvey || ($this->surveypro->pauseresume == SURVEYPRO_PAUSERESUMEEMAIL);
            if ($pasuseresumesurvey) {
                // Your 'in progress' responses.
                $a = new \stdClass();
                $a->status = get_string('statusinprogress', 'mod_surveypro');
                $a->responsescount = $inprogress;
                $messages[] = get_string('yoursubmissions', 'mod_surveypro', $a);
            }
        }

        $this->display_messages($messages, get_string('attemptinfo', 'mod_surveypro'));
        $messages = [];
        // End of: general info.

        if ($addnew) {
            $paramurl = [];
            $paramurl['s'] = $this->cm->instance;
            $paramurl['mode'] = SURVEYPRO_NEWRESPONSEMODE;
            $paramurl['area'] = 'surveypro';
            $paramurl['section'] = 'responsesubmit';
            $paramurl['begin'] = 1;
            $url = new \moodle_url('/mod/surveypro/view.php', $paramurl);
            $message = get_string('addnewsubmission', 'mod_surveypro');
            echo $OUTPUT->box($OUTPUT->single_button($url, $message, 'get', ['type' => 'primary']), 'clearfix mdl-align');
        } else {
            if (!$cansubmit) {
                $message = get_string('canneversubmit', 'mod_surveypro');
                echo $OUTPUT->notification($message, 'notifyproblem');
            } else if (($this->surveypro->timeopen) && ($this->surveypro->timeopen >= $timenow)) {
                $message = get_string('cannotsubmittooearly', 'mod_surveypro', userdate($this->surveypro->timeopen));
                echo $OUTPUT->notification($message, 'notifyproblem');
            } else if (($this->surveypro->timeclose) && ($this->surveypro->timeclose <= $timenow)) {
                $message = get_string('cannotsubmittoolate', 'mod_surveypro', userdate($this->surveypro->timeclose));
                echo $OUTPUT->notification($message, 'notifyproblem');
            } else if (($this->surveypro->maxentries > 0) && ($next >= $this->surveypro->maxentries)) {
                $message = get_string('nomoresubmissionsallowed', 'mod_surveypro', $this->surveypro->maxentries);
                if ($inprogress) {
                    $a = new \stdClass();
                    $a->inprogress = get_string('statusinprogress', 'mod_surveypro');
                    $a->surveyproresponses = get_string('surveypro_responses', 'mod_surveypro');
                    $message .= get_string('onlyfinalizationallowed', 'mod_surveypro', $a);
                } else {
                    $message .= '.';
                }
                echo $OUTPUT->notification($message, 'notifyproblem');
            }
        }
        // End of: the button to add one more surveypro.

        if (!$itemcount) {
            return;
        }
    }

    /**
     * Recursive function to populate the $messages array for reports nested as much times as wanted
     *
     * Uncomment lines of get_haschildrenreports method
     * in surveyproreport_colles\report class of report/colles/classes/report.php file
     * to see this function in action.
     *
     * @param string $childrenreports
     * @param string $pluginname
     * @param array $messages
     * @param string $categoryname
     * @return void
     */
    public function add_report_link($childrenreports, $pluginname, &$messages, $categoryname) {
        foreach ($childrenreports as $reportkey => $childparams) {
            $subreport = get_string($reportkey, 'surveyprotemplate_' . $this->surveypro->template);
            if (is_array(reset($childparams))) { // If the first element of $childparams is an array.
                $categoryname .= ' > ' . $subreport;
                $this->add_report_link($childparams, $pluginname, $messages, $categoryname);
            } else {
                $childparams = ['s' => $this->cm->instance] + $childparams;
                $url = new \moodle_url('/mod/surveypro/report/' . $pluginname . '/view.php', $childparams);
                $a = new \stdClass();
                $a->href = $url->out();
                $a->reportname = $categoryname . ' > ' . $subreport;
                $messages[] = get_string('runreport', 'mod_surveypro', $a);
            }
        }
    }

    /**
     * Display the generic message of the overview page.
     *
     * @param string $messages
     * @param string $strlegend
     * @return void
     */
    public function display_messages($messages, $strlegend) {
        global $OUTPUT;

        if (count($messages)) {
            echo \html_writer::start_tag('fieldset', ['class' => 'generalbox']);
            echo \html_writer::start_tag('legend', ['class' => 'coverinfolegend']);
            echo $strlegend;
            echo \html_writer::end_tag('legend');
            foreach ($messages as $message) {
                echo $OUTPUT->container($message, 'mdl-left');
            }
            echo \html_writer::end_tag('fieldset');
        }
    }
}
