<?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/>.

/**
 * Contains class mod_surveypro\local\ipe\layout_variable
 *
 * @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\local\ipe;

use core_external\external_api;

/**
 * Class to prepare an item variable for display and in-place editing
 *
 * @package   mod_surveypro
 * @copyright 2013 onwards kordan <stringapiccola@gmail.com>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class layout_variable extends \core\output\inplace_editable
{
    /**
     * Constructor.
     *
     * @param int $itemid
     * @param string $variablename
     */
    public function __construct($itemid, $variablename) {
        $variablename = format_string($variablename);
        $editeditvariable = get_string('editvariable', 'mod_surveypro');

        parent::__construct(
            'mod_surveypro',
            'layout_variable',
            $itemid,
            true,
            $variablename,
            $variablename,
            $editeditvariable,
            $editeditvariable,
        );
    }

    /**
     * Updates usertemplate name and returns instance of this object
     *
     * @param int $itemid
     * @param string $newvarname
     * @return static
     */
    public static function update($itemid, $newvarname) {
        global $DB;

        $itemrecord = $DB->get_record('surveypro_item', ['id' => $itemid], 'id, surveyproid, type, plugin', MUST_EXIST);
        $surveypro = $DB->get_record('surveypro', ['id' => $itemrecord->surveyproid], '*', MUST_EXIST);
        $cm = get_coursemodule_from_instance('surveypro', $surveypro->id, $surveypro->course, false, MUST_EXIST);
        $context = \context_module::instance($cm->id);
        external_api::validate_context($context);

        // Why was I required to move surveypro_get_itemclass from locallib.php to lib.php?
        $item = surveypro_get_itemclass($cm, $surveypro, $itemid, $itemrecord->type, $itemrecord->plugin);

        // Before saving to the the plugin table, validate the variable name.
        $record = new \stdClass();
        $record->itemid = $itemid;
        $record->surveyproid = $surveypro->id;
        $record->variable = $newvarname;
        $record->plugin = $itemrecord->plugin;

        $item->item_validate_variablename($record);
        $DB->set_field('surveypro_item', 'variable', $record->variable, ['id' => $itemid]);

        return new static($itemid, $newvarname);
    }
}
