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

/**
 * @package mod_circuitkit
 * @copyright  29/07/2025 Damien Renou (http://openelearning.org)
 * @copyright  29/07/2025 QUIQUEREZ Laurent (https://www.univ-lyon1.fr/)
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die;

/**
 * Clean circuit URL to extract only the ctz parameter.
 *
 * @param string $url The original circuit URL
 * @return string The cleaned URL with only ctz parameter
 */
function circuitkit_clean_circuit_url($url) {
    if (empty($url)) {
        return '';
    }
    
    // If URL already starts with ?ctz=, it's already cleaned - return as is
    if (strpos($url, '?ctz=') === 0) {
        return $url;
    }
    
    // Only clean URLs that contain 'http' (full URLs)
    if (strpos($url, 'http') === false) {
        return $url;
    }
    
    $parsed_url = parse_url($url);
    
    if (isset($parsed_url['query'])) {
        parse_str($parsed_url['query'], $query_params);
        
        if (isset($query_params['ctz']) && !empty($query_params['ctz'])) {
            return '?ctz=' . $query_params['ctz'];
        }
    }
    
    // If no ctz parameter found, return empty string
    return '';
}

/**
 * Recompose circuit URL with platform domain and circuit data.
 *
 * @param string $content The circuit content (usually ?ctz=...)
 * @return string The complete circuit URL
 */
function circuitkit_build_circuit_url($content) {
    global $CFG;
    
    if (empty($content)) {
        return $CFG->wwwroot . '/mod/circuitkit/engine/war/circuitjs.html?startCircuit=lrc.txt';
    }
    
    // If content starts with ?, it's already a query parameter
    if (strpos($content, '?') === 0) {
        return $CFG->wwwroot . '/mod/circuitkit/engine/war/circuitjs.html' . $content;
    }
    
    // If it's a full URL, return as is (for backward compatibility)
    if (filter_var($content, FILTER_VALIDATE_URL)) {
        return $content;
    }
    
    // Default case
    return $CFG->wwwroot . '/mod/circuitkit/engine/war/circuitjs.html?startCircuit=lrc.txt';
}

/**
 * List of features supported in circuitkit module
 * @param string $feature FEATURE_xx constant for requested feature
 * @return mixed True if module supports feature, false if not, null if doesn't know or string for the module purpose.
 */
function circuitkit_supports($feature) {
    switch($feature) {
        case FEATURE_MOD_ARCHETYPE:           return MOD_ARCHETYPE_RESOURCE;
        case FEATURE_GROUPS:                  return false;
        case FEATURE_GROUPINGS:               return false;
        case FEATURE_MOD_INTRO:               return true;
        case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
        case FEATURE_GRADE_HAS_GRADE:         return false;
        case FEATURE_GRADE_OUTCOMES:          return false;
        case FEATURE_BACKUP_MOODLE2:          return true;
        case FEATURE_SHOW_DESCRIPTION:        return true;
        case FEATURE_MOD_PURPOSE:             return MOD_PURPOSE_CONTENT;

        default: return null;
    }
}

/**
 * This function is used by the reset_course_userdata function in moodlelib.
 * @param $data the data submitted from the reset course.
 * @return array status array
 */
function circuitkit_reset_userdata($data) {

    // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
    // See MDL-9367.

    return array();
}

/**
 * List the actions that correspond to a view of this module.
 * This is used by the participation report.
 *
 * Note: This is not used by new logging system. Event with
 *       crud = 'r' and edulevel = LEVEL_PARTICIPATING will
 *       be considered as view action.
 *
 * @return array
 */
function circuitkit_get_view_actions() {
    return array('view','view all');
}

/**
 * List the actions that correspond to a post of this module.
 * This is used by the participation report.
 *
 * Note: This is not used by new logging system. Event with
 *       crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
 *       will be considered as post action.
 *
 * @return array
 */
function circuitkit_get_post_actions() {
    return array('update', 'add');
}

/**
 * Add circuitkit instance.
 * @param stdClass $data
 * @param mod_circuitkit_mod_form $mform
 * @return int new circuitkit instance id
 */
function circuitkit_add_instance($data, $mform = null) {
    global $CFG, $DB;
    require_once("$CFG->libdir/resourcelib.php");

    $cmid = $data->coursemodule;

    // Clean the circuit URL to extract only the ctz parameter
    if (!empty($data->content)) {
        $data->content = circuitkit_clean_circuit_url($data->content);
    }

    $data->timemodified = time();
    $displayoptions = array();
    if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
        $displayoptions['popupwidth']  = $data->popupwidth;
        $displayoptions['popupheight'] = $data->popupheight;
    }
    $displayoptions['printintro']   = $data->printintro;
    $displayoptions['printlastmodified'] = $data->printlastmodified;
    $data->displayoptions = serialize($displayoptions);

    if ($mform) {
        // Content is now a simple text field, not an editor
        if (isset($data->circuitkit) && is_array($data->circuitkit)) {
            // Old format (editor)
            $data->content       = $data->circuitkit['text'];
            $data->contentformat = $data->circuitkit['format'];
        } else {
            // New format (simple text field)
            $data->contentformat = FORMAT_PLAIN;
        }
    }

    $data->id = $DB->insert_record('circuitkit', $data);

    // we need to use context now, so we need to make sure all needed info is already in db
    $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
    $context = context_module::instance($cmid);

    if ($mform and !empty($data->circuitkit['itemid'])) {
        $draftitemid = $data->circuitkit['itemid'];
        $data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_circuitkit', 'content', 0, circuitkit_get_editor_options($context), $data->content);
        $DB->update_record('circuitkit', $data);
    }

    $completiontimeexpected = !empty($data->completionexpected) ? $data->completionexpected : null;
    \core_completion\api::update_completion_date_event($cmid, 'circuitkit', $data->id, $completiontimeexpected);

    return $data->id;
}

/**
 * Update circuitkit instance.
 * @param object $data
 * @param object $mform
 * @return bool true
 */
function circuitkit_update_instance($data, $mform) {
    global $CFG, $DB;
    require_once("$CFG->libdir/resourcelib.php");

    $cmid = $data->coursemodule;
    $draftitemid = null;
    
    // Clean the circuit URL to extract only the ctz parameter
    if (!empty($data->content)) {
        $data->content = circuitkit_clean_circuit_url($data->content);
    }
    
    // Handle both old and new format
    if (isset($data->circuitkit) && is_array($data->circuitkit)) {
        // Old format (editor)
        $draftitemid = $data->circuitkit['itemid'];
        $data->content       = $data->circuitkit['text'];
        $data->contentformat = $data->circuitkit['format'];
    } else {
        // New format (simple text field)
        $data->contentformat = FORMAT_PLAIN;
    }

    $data->timemodified = time();
    $data->id           = $data->instance;
    $data->revision++;

    $displayoptions = array();
    if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
        $displayoptions['popupwidth']  = $data->popupwidth;
        $displayoptions['popupheight'] = $data->popupheight;
    }
    $displayoptions['printintro']   = $data->printintro;
    $displayoptions['printlastmodified'] = $data->printlastmodified;
    $data->displayoptions = serialize($displayoptions);

    $DB->update_record('circuitkit', $data);

    $context = context_module::instance($cmid);
    if ($draftitemid) {
        $data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_circuitkit', 'content', 0, circuitkit_get_editor_options($context), $data->content);
        $DB->update_record('circuitkit', $data);
    }

    $completiontimeexpected = !empty($data->completionexpected) ? $data->completionexpected : null;
    \core_completion\api::update_completion_date_event($cmid, 'circuitkit', $data->id, $completiontimeexpected);

    return true;
}

/**
 * Delete circuitkit instance.
 * @param int $id
 * @return bool true
 */
function circuitkit_delete_instance($id) {
    global $DB;

    if (!$circuitkit = $DB->get_record('circuitkit', array('id'=>$id))) {
        return false;
    }

    $cm = get_coursemodule_from_instance('circuitkit', $id);
    \core_completion\api::update_completion_date_event($cm->id, 'circuitkit', $id, null);

    // note: all context files are deleted automatically

    $DB->delete_records('circuitkit', array('id'=>$circuitkit->id));

    return true;
}

/**
 * Given a course_module object, this function returns any
 * "extra" information that may be needed when printing
 * this activity in a course listing.
 *
 * See {@link course_modinfo::get_array_of_activities()}
 *
 * @param stdClass $coursemodule
 * @return cached_cm_info Info to customise main circuitkit display
 */
function circuitkit_get_coursemodule_info($coursemodule) {
    global $CFG, $DB;
    require_once("$CFG->libdir/resourcelib.php");

    if (!$circuitkit = $DB->get_record('circuitkit', array('id'=>$coursemodule->instance),
            'id, name, display, displayoptions, intro, introformat')) {
        return NULL;
    }

    $info = new cached_cm_info();
    $info->name = $circuitkit->name;

    if ($coursemodule->showdescription) {
        // Convert intro to html. Do not filter cached version, filters run at display time.
        $info->content = format_module_intro('circuitkit', $circuitkit, $coursemodule->id, false);
    }

    if ($circuitkit->display != RESOURCELIB_DISPLAY_POPUP) {
        return $info;
    }

    $fullurl = "$CFG->wwwroot/mod/circuitkit/view.php?id=$coursemodule->id&amp;inpopup=1";
    $options = empty($circuitkit->displayoptions) ? [] : (array) unserialize_array($circuitkit->displayoptions);
    $width  = empty($options['popupwidth'])  ? 620 : $options['popupwidth'];
    $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
    $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
    $info->onclick = "window.open('$fullurl', '', '$wh'); return false;";

    return $info;
}


/**
 * Lists all browsable file areas
 *
 * @package  mod_circuitkit
 * @category files
 * @param stdClass $course course object
 * @param stdClass $cm course module object
 * @param stdClass $context context object
 * @return array
 */
function circuitkit_get_file_areas($course, $cm, $context) {
    $areas = array();
    $areas['content'] = get_string('content', 'circuitkit');
    return $areas;
}

/**
 * File browsing support for circuitkit module content area.
 *
 * @package  mod_circuitkit
 * @category files
 * @param stdClass $browser file browser instance
 * @param stdClass $areas file areas
 * @param stdClass $course course object
 * @param stdClass $cm course module object
 * @param stdClass $context context object
 * @param string $filearea file area
 * @param int $itemid item ID
 * @param string $filepath file path
 * @param string $filename file name
 * @return file_info instance or null if not found
 */
function circuitkit_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
    global $CFG;

    if (!has_capability('moodle/course:managefiles', $context)) {
        // students can not peak here!
        return null;
    }

    $fs = get_file_storage();

    if ($filearea === 'content') {
        $filepath = is_null($filepath) ? '/' : $filepath;
        $filename = is_null($filename) ? '.' : $filename;

        $urlbase = $CFG->wwwroot.'/pluginfile.php';
        if (!$storedfile = $fs->get_file($context->id, 'mod_circuitkit', 'content', 0, $filepath, $filename)) {
            if ($filepath === '/' and $filename === '.') {
                $storedfile = new virtual_root_file($context->id, 'mod_circuitkit', 'content', 0);
            } else {
                // not found
                return null;
            }
        }
        require_once("$CFG->dirroot/mod/circuitkit/locallib.php");
        return new circuitkit_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
    }

    // note: circuitkit_intro handled in file_browser automatically

    return null;
}

/**
 * Serves the circuitkit files.
 *
 * @package  mod_circuitkit
 * @category files
 * @param stdClass $course course object
 * @param stdClass $cm course module object
 * @param stdClass $context context object
 * @param string $filearea file area
 * @param array $args extra arguments
 * @param bool $forcedownload whether or not force download
 * @param array $options additional options affecting the file serving
 * @return bool false if file not found, does not return if found - just send the file
 */
function circuitkit_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
    global $CFG, $DB;
    require_once("$CFG->libdir/resourcelib.php");

    if ($context->contextlevel != CONTEXT_MODULE) {
        return false;
    }

    require_course_login($course, true, $cm);
    if (!has_capability('mod/circuitkit:view', $context)) {
        return false;
    }

    if ($filearea !== 'content') {
        // intro is handled automatically in pluginfile.php
        return false;
    }

    // $arg could be revision number or index.html
    $arg = array_shift($args);
    if ($arg == 'index.html' || $arg == 'index.htm') {
        // serve circuitkit content
        $filename = $arg;

        if (!$circuitkit = $DB->get_record('circuitkit', array('id'=>$cm->instance), '*', MUST_EXIST)) {
            return false;
        }

        // We need to rewrite the pluginfile URLs so the media filters can work.
        $content = file_rewrite_pluginfile_urls($circuitkit->content, 'webservice/pluginfile.php', $context->id, 'mod_circuitkit', 'content',
                                                $circuitkit->revision);
        $formatoptions = new stdClass;
        $formatoptions->noclean = true;
        $formatoptions->overflowdiv = true;
        $formatoptions->context = $context;
        $content = format_text($content, $circuitkit->contentformat, $formatoptions);

        // Remove @@PLUGINFILE@@/.
        $options = array('reverse' => true);
        $content = file_rewrite_pluginfile_urls($content, 'webservice/pluginfile.php', $context->id, 'mod_circuitkit', 'content',
                                                $circuitkit->revision, $options);
        $content = str_replace('@@PLUGINFILE@@/', '', $content);

        send_file($content, $filename, 0, 0, true, true);
    } else {
        $fs = get_file_storage();
        $relativepath = implode('/', $args);
        $fullpath = "/$context->id/mod_circuitkit/$filearea/0/$relativepath";
        if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
            $circuitkit = $DB->get_record('circuitkit', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST);
            if ($circuitkit->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
                return false;
            }
            if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_circuitkit', 'content', 0)) {
                return false;
            }
            //file migrate - update flag
            $circuitkit->legacyfileslast = time();
            $DB->update_record('circuitkit', $circuitkit);
        }

        // finally send the file
        send_stored_file($file, null, 0, $forcedownload, $options);
    }
}

/**
 * Return a list of circuitkit types
 * @param string $circuitkittype current circuitkit type
 * @param stdClass $parentcontext Block's parent context
 * @param stdClass $currentcontext Current context of block
 */
function circuitkit_circuitkit_type_list($circuitkittype, $parentcontext, $currentcontext) {
    $module_circuitkittype = array('mod-circuitkit-*'=>get_string('circuitkit-mod-circuitkit-x', 'circuitkit'));
    return $module_circuitkittype;
}

/**
 * Export circuitkit resource contents
 *
 * @return array of file content
 */
function circuitkit_export_contents($cm, $baseurl) {
    global $CFG, $DB;
    $contents = array();
    $context = context_module::instance($cm->id);

    $circuitkit = $DB->get_record('circuitkit', array('id'=>$cm->instance), '*', MUST_EXIST);

    // circuitkit contents
    $fs = get_file_storage();
    $files = $fs->get_area_files($context->id, 'mod_circuitkit', 'content', 0, 'sortorder DESC, id ASC', false);
    foreach ($files as $fileinfo) {
        $file = array();
        $file['type']         = 'file';
        $file['filename']     = $fileinfo->get_filename();
        $file['filepath']     = $fileinfo->get_filepath();
        $file['filesize']     = $fileinfo->get_filesize();
        $file['fileurl']      = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_circuitkit/content/'.$circuitkit->revision.$fileinfo->get_filepath().$fileinfo->get_filename(), true);
        $file['timecreated']  = $fileinfo->get_timecreated();
        $file['timemodified'] = $fileinfo->get_timemodified();
        $file['sortorder']    = $fileinfo->get_sortorder();
        $file['userid']       = $fileinfo->get_userid();
        $file['author']       = $fileinfo->get_author();
        $file['license']      = $fileinfo->get_license();
        $file['mimetype']     = $fileinfo->get_mimetype();
        $file['isexternalfile'] = $fileinfo->is_external_file();
        if ($file['isexternalfile']) {
            $file['repositorytype'] = $fileinfo->get_repository_type();
        }
        $contents[] = $file;
    }

    // circuitkit html conent
    $filename = 'index.html';
    $circuitkitfile = array();
    $circuitkitfile['type']         = 'file';
    $circuitkitfile['filename']     = $filename;
    $circuitkitfile['filepath']     = '/';
    $circuitkitfile['filesize']     = 0;
    $circuitkitfile['fileurl']      = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_circuitkit/content/' . $filename, true);
    $circuitkitfile['timecreated']  = null;
    $circuitkitfile['timemodified'] = $circuitkit->timemodified;
    // make this file as main file
    $circuitkitfile['sortorder']    = 1;
    $circuitkitfile['userid']       = null;
    $circuitkitfile['author']       = null;
    $circuitkitfile['license']      = null;
    $contents[] = $circuitkitfile;

    return $contents;
}

/**
 * Register the ability to handle drag and drop file uploads
 * @return array containing details of the files / types the mod can handle
 */
function circuitkit_dndupload_register() {
    return array('types' => array(
                     array('identifier' => 'text/html', 'message' => get_string('createcircuitkit', 'circuitkit')),
                     array('identifier' => 'text', 'message' => get_string('createcircuitkit', 'circuitkit'))
                 ));
}

/**
 * Handle a file that has been uploaded
 * @param object $uploadinfo details of the file / content that has been uploaded
 * @return int instance id of the newly created mod
 */
function circuitkit_dndupload_handle($uploadinfo) {
    // Gather the required info.
    $data = new stdClass();
    $data->course = $uploadinfo->course->id;
    $data->name = $uploadinfo->displayname;
    $data->intro = '<p>'.$uploadinfo->displayname.'</p>';
    $data->introformat = FORMAT_HTML;
    if ($uploadinfo->type == 'text/html') {
        $data->contentformat = FORMAT_HTML;
        $data->content = clean_param($uploadinfo->content, PARAM_CLEANHTML);
    } else {
        $data->contentformat = FORMAT_PLAIN;
        $data->content = clean_param($uploadinfo->content, PARAM_TEXT);
    }
    $data->coursemodule = $uploadinfo->coursemodule;

    // Set the display options to the site defaults.
    $config = get_config('circuitkit');
    $data->display = $config->display;
    $data->popupheight = $config->popupheight;
    $data->popupwidth = $config->popupwidth;
    $data->printintro = $config->printintro;
    $data->printlastmodified = $config->printlastmodified;

    return circuitkit_add_instance($data, null);
}

/**
 * Mark the activity completed (if required) and trigger the course_module_viewed event.
 *
 * @param  stdClass $circuitkit       circuitkit object
 * @param  stdClass $course     course object
 * @param  stdClass $cm         course module object
 * @param  stdClass $context    context object
 * @since Moodle 3.0
 */
function circuitkit_view($circuitkit, $course, $cm, $context) {

    // Trigger course_module_viewed event.
    $params = array(
        'context' => $context,
        'objectid' => $circuitkit->id
    );

    $event = \mod_circuitkit\event\course_module_viewed::create($params);
    $event->add_record_snapshot('course_modules', $cm);
    $event->add_record_snapshot('course', $course);
    $event->add_record_snapshot('circuitkit', $circuitkit);
    $event->trigger();

    // Completion.
    $completion = new completion_info($course);
    $completion->set_module_viewed($cm);
}

/**
 * Check if the module has any update that affects the current user since a given time.
 *
 * @param  cm_info $cm course module data
 * @param  int $from the time to check updates from
 * @param  array $filter  if we need to check only specific updates
 * @return stdClass an object with the different type of areas indicating if they were updated or not
 * @since Moodle 3.2
 */
function circuitkit_check_updates_since(cm_info $cm, $from, $filter = array()) {
    $updates = course_check_module_updates_since($cm, $from, array('content'), $filter);
    return $updates;
}

/**
 * This function receives a calendar event and returns the action associated with it, or null if there is none.
 *
 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
 * is not displayed on the block.
 *
 * @param calendar_event $event
 * @param \core_calendar\action_factory $factory
 * @return \core_calendar\local\event\entities\action_interface|null
 */
function mod_circuitkit_core_calendar_provide_event_action(calendar_event $event,
                                                      \core_calendar\action_factory $factory, $userid = 0) {
    global $USER;

    if (empty($userid)) {
        $userid = $USER->id;
    }

    $cm = get_fast_modinfo($event->courseid, $userid)->instances['circuitkit'][$event->instance];

    $completion = new \completion_info($cm->get_course());

    $completiondata = $completion->get_data($cm, false, $userid);

    if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
        return null;
    }

    return $factory->create_instance(
        get_string('view'),
        new \moodle_url('/mod/circuitkit/view.php', ['id' => $cm->id]),
        1,
        true
    );
}

/**
 * Given an array with a file path, it returns the itemid and the filepath for the defined filearea.
 *
 * @param  string $filearea The filearea.
 * @param  array  $args The path (the part after the filearea and before the filename).
 * @return array The itemid and the filepath inside the $args path, for the defined filearea.
 */
function mod_circuitkit_get_path_from_pluginfile(string $filearea, array $args) : array {
    // circuitkit never has an itemid (the number represents the revision but it's not stored in database).
    array_shift($args);

    // Get the filepath.
    if (empty($args)) {
        $filepath = '/';
    } else {
        $filepath = '/' . implode('/', $args) . '/';
    }

    return [
        'itemid' => 0,
        'filepath' => $filepath,
    ];
}
