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

/**
 * Core container for calendar events.
 *
 * The purpose of this class is simply to wire together the various
 * implementations of calendar event components to produce a solution
 * to the problems Moodle core wants to solve.
 *
 * @package    core_calendar
 * @copyright  2017 <NAME> <<EMAIL>>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace core_calendar\local\event;

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

use core_calendar\action_factory;
use core_calendar\local\event\factories\action_event_factory;
use core_calendar\local\event\factories\event_factory;
use core_calendar\local\event\factories\event_vault_factory;
use core_calendar\local\event\mappers\event_mapper;
use core_calendar\local\interfaces\action_event_interface;
use core_calendar\local\interfaces\event_interface;

/**
 * Core container.
 *
 * @copyright 2017 <NAME> <<EMAIL>>
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class core_container {
    /**
     * @var event_factory $eventfactory Event factory.
     */
    protected static $eventfactory;

    /**
     * @var action_event_factory $actioneventfactory Action event factory.
     */
    protected static $actioneventfactory;

    /**
     * @var \core_calendar\local\interfaces\event_mapper_interface $eventmapper Event mapper.
     */
    protected static $eventmapper;

    /**
     * @var action_factory $actionfactory Action factory.
     */
    protected static $actionfactory;

    /**
     * @var \core_calendar\local\event\data_access\event_vault $eventvault Event vault.
     */
    protected static $eventvault;

    /**
     * @var array A list of callbacks to use.
     */
    protected static $callbacks = array();

    /**
     * @var stdClass[] An array of cached courses to use with the event factory.
     */
    protected static $coursecache = array();

    /**
     * @var stdClass[] An array of cached modules to use with the event factory.
     */
    protected static $modulecache = array();

    /**
     * Initialises the dependency graph if it hasn't yet been.
     */
    private static function init() {
        if (empty(self::$eventfactory)) {
            $getcallback = function($which) {
                return self::$callbacks[PHPUNIT_TEST ? 'testing' : 'production'][$which];
            };

            self::initcallbacks();
            self::$actionfactory = new action_factory();
            self::$actioneventfactory = new action_event_factory();
            self::$eventmapper = new event_mapper(
                new event_factory(
                    function(event_interface $event) {
                        return $event;
                    },
                    function() {
                        return true;
                    },
                    function() {
                        return false;
                    },
                    self::$coursecache,
                    self::$modulecache
                )
            );

            self::$eventfactory = new event_factory(
                $getcallback('action'),
                $getcallback('visibility'),
                function ($dbrow) {
                    $instances = get_fast_modinfo($dbrow->courseid)->instances;

                    if (!isset($instances[$dbrow->modulename]) || !isset($instances[$dbrow->modulename][$dbrow->instance])) {
                        return true;
                    }

                    $cm = $instances[$dbrow->modulename][$dbrow->instance];

                    return !(bool)$cm->uservisible;
                },
                self::$coursecache,
                self::$modulecache
            );
        }

        if (empty(self::$eventvault)) {
            $vaultfactory = new event_vault_factory();
            self::$eventvault = $vaultfactory->create_instance(self::$eventfactory);
        }
    }

    /**
     * Gets the event factory.
     *
     * @return event_factory
     */
    public static function get_event_factory() {
        self::init();
        return self::$eventfactory;
    }
<commit_msg>MDL-57878 core_calendar: extended bailout callback<commit_after><?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/>.

/**
 * Core container for calendar events.
 *
 * The purpose of this class is simply to wire together the various
 * implementations of calendar event components to produce a solution
 * to the problems Moodle core wants to solve.
 *
 * @package    core_calendar
 * @copyright  2017 <NAME> <<EMAIL>>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace core_calendar\local\event;

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

use core_calendar\action_factory;
use core_calendar\local\event\factories\action_event_factory;
use core_calendar\local\event\factories\event_factory;
use core_calendar\local\event\factories\event_vault_factory;
use core_calendar\local\event\mappers\event_mapper;
use core_calendar\local\interfaces\action_event_interface;
use core_calendar\local\interfaces\event_interface;

/**
 * Core container.
 *
 * @copyright 2017 <NAME> <<EMAIL>>
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class core_container {
    /**
     * @var event_factory $eventfactory Event factory.
     */
    protected static $eventfactory;

    /**
     * @var action_event_factory $actioneventfactory Action event factory.
     */
    protected static $actioneventfactory;

    /**
     * @var \core_calendar\local\interfaces\event_mapper_interface $eventmapper Event mapper.
     */
    protected static $eventmapper;

    /**
     * @var action_factory $actionfactory Action factory.
     */
    protected static $actionfactory;

    /**
     * @var \core_calendar\local\event\data_access\event_vault $eventvault Event vault.
     */
    protected static $eventvault;

    /**
     * @var array A list of callbacks to use.
     */
    protected static $callbacks = array();

    /**
     * @var stdClass[] An array of cached courses to use with the event factory.
     */
    protected static $coursecache = array();

    /**
     * @var stdClass[] An array of cached modules to use with the event factory.
     */
    protected static $modulecache = array();

    /**
     * Initialises the dependency graph if it hasn't yet been.
     */
    private static function init() {
        if (empty(self::$eventfactory)) {
            $getcallback = function($which) {
                return self::$callbacks[PHPUNIT_TEST ? 'testing' : 'production'][$which];
            };

            self::initcallbacks();
            self::$actionfactory = new action_factory();
            self::$actioneventfactory = new action_event_factory();
            self::$eventmapper = new event_mapper(
                new event_factory(
                    function(event_interface $event) {
                        return $event;
                    },
                    function() {
                        return true;
                    },
                    function() {
                        return false;
                    },
                    self::$coursecache,
                    self::$modulecache
                )
            );

            self::$eventfactory = new event_factory(
                $getcallback('action'),
                $getcallback('visibility'),
                function ($dbrow) {
                    $instances = get_fast_modinfo($dbrow->courseid)->instances;

                    if (!isset($instances[$dbrow->modulename]) || !isset($instances[$dbrow->modulename][$dbrow->instance])) {
                        return true;
                    }

                    $cm = $instances[$dbrow->modulename][$dbrow->instance];

                    if (!$cm->uservisible) {
                        return true;
                    }

                    // Ok, now check if we are looking at a completion event.
                    if ($dbrow->eventtype === \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED) {
                        // Need to have completion enabled before displaying these events.
                        $course = new \stdClass();
                        $course->id = $dbrow->courseid;
                        $completion = new \completion_info($course);

                        return (bool) !$completion->is_enabled($cm);
                    }

                    return false;
                },
                self::$coursecache,
                self::$modulecache
            );
        }

        if (empty(self::$eventvault)) {
            $vaultfactory = new event_vault_factory();
            self::$eventvault = $vaultfactory->create_instance(self::$eventfactory);
        }
    }

    /**
     * Gets the event factory.
     *
     * @return event_factory
     */
    public static function get_event_factory() {
        self::init();
        return self::$eventfactory;
    }

{'fraction_non_alphanumeric': 0.10563792430745221, 'fraction_numerical': 0.0026336324619586422, 'mean_word_length': 2.054215072981829, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5757917', 'n_tokens_mistral': 2751, 'n_tokens_neox': 2639, 'n_words': 754}