<commit_before><?php

namespace app\plugins\memberPetitions;

use app\models\db\Consultation;
use app\models\db\IMotion;
use app\models\db\Motion;
use app\models\db\MotionSupporter;
use app\models\db\Site;
use app\models\db\User;
use app\models\events\MotionEvent;
use app\models\supportTypes\ISupportType;
use app\plugins\memberPetitions\notifications\DiscussionSubmitted;
use app\plugins\memberPetitions\notifications\PetitionSubmitted;
use app\components\Tools as DateTools;

class Tools
{
    /**
     * @param Site $site
     * @param User $user
     * @return Consultation[]
     */
    public static function getUserConsultations($site, $user)
    {
        if (!$user) {
            return [];
        }

        $organizations = $user->getMyOrganizationIds();
        $consultations = [];
        foreach ($site->consultations as $consultation) {
            /** @var ConsultationSettings $settings */
            $settings = $consultation->getSettings();

            if ($settings->maintenanceMode && !$user->hasPrivilege($consultation, User::PRIVILEGE_ANY)) {
                continue;
            }

            if (in_array($settings->organizationId, $organizations)) {
                $consultations[] = $consultation;
            }
        }
        return $consultations;
    }

    /**
     * @param Consultation $consultation
     * @return \app\models\db\ConsultationMotionType|null
     */
    public static function getDiscussionType(Consultation $consultation)
    {
        foreach ($consultation->motionTypes as $motionType) {
            if ($motionType->supportType !== ISupportType::COLLECTING_SUPPORTERS) {
                return $motionType;
            }
        }
        return null;
    }

    /**
     * @param Consultation $consultation
     * @return \app\models\db\ConsultationMotionType|null
     */
    public static function getPetitionType(Consultation $consultation)
    {
        foreach ($consultation->motionTypes as $motionType) {
            if ($motionType->supportType === ISupportType::COLLECTING_SUPPORTERS) {
                return $motionType;
            }
        }
        return null;
    }

    /**
     * @param Consultation $consultation
     * @return bool
     */
    public static function isConsultationFullyConfigured(Consultation $consultation)
    {
        return (static::getPetitionType($consultation) !== null && static::getDiscussionType($consultation) !== null);
    }

    /**
     * @param Consultation[] $consultations
     * @return Motion[]
     */
    public static function getAllMotions($consultations)
    {
        $all = [];
        foreach ($consultations as $consultation) {
            if (!Tools::isConsultationFullyConfigured($consultation)) {
                continue;
            }
            $all = array_merge($all, static::getPetitionType($consultation)->getVisibleMotions(false));
            $all = array_merge($all, static::getMotionsCollecting($consultation));
            $all = array_merge($all, static::getDiscussionType($consultation)->getVisibleMotions(false));
            $all = array_merge($all, static::getMotionsAnswered($consultation));
        }
        return $all;
    }

    /**
     * @param Consultation $consultation
     * @return Motion[]
     */
    public static function getMotionsInDiscussion(Consultation $consultation)
    {
        $motions = Tools::getDiscussionType($consultation)->getVisibleMotions(false);
        return array_filter($motions, function (Motion $motion) {
            return ($motion->status == IMotion::STATUS_SUBMITTED_SCREENED);
        });
    }

    /**
     * @param Consultation[] $consultations
     * @return Motion[]
     */
    public static function getAllMotionsInDiscussion($consultations)
    {
        $all = [];
        foreach ($consultations as $consultation) {
            if (!Tools::isConsultationFullyConfigured($consultation)) {
                continue;
            }
            $all = array_merge($all, static::getMotionsInDiscussion($consultation));
        }
        return $all;
    }

    /**
     * @param Consultation $consultation
     * @return Motion[]
     */
    public static function getMotionsAnswered(Consultation $consultation)
    {
        $motions = Tools::getPetitionType($consultation)->getVisibleMotions(false);
        return array_filter($motions, function (Motion $motion) {
            return ($motion->status == IMotion::STATUS_PROCESSED);
        });
    }

    /**
     * @param Consultation[] $consultations
     * @return Motion[]
     */
    public static function getAllMotionsAnswered($consultations)
    {
        $all = [];
        foreach ($consultations as $consultation) {
            if (!Tools::isConsultationFullyConfigured($consultation)) {
                continue;
            }
            $all = array_merge($all, static::getMotionsAnswered($consultation));
        }
        return $all;
    }

    /**
     * @param Consultation $consultation
     * @return Motion[]
     */
    public static function getMotionsUnanswered(Consultation $consultation)
    {<commit_msg>Bugfix memberPetitions<commit_after><?php

namespace app\plugins\memberPetitions;

use app\models\db\Consultation;
use app\models\db\IMotion;
use app\models\db\Motion;
use app\models\db\MotionSupporter;
use app\models\db\Site;
use app\models\db\User;
use app\models\events\MotionEvent;
use app\models\supportTypes\ISupportType;
use app\plugins\memberPetitions\notifications\DiscussionSubmitted;
use app\plugins\memberPetitions\notifications\PetitionSubmitted;
use app\components\Tools as DateTools;

class Tools
{
    /**
     * @param Site $site
     * @param User $user
     * @return Consultation[]
     */
    public static function getUserConsultations($site, $user)
    {
        if (!$user) {
            return [];
        }

        $organizations = $user->getMyOrganizationIds();
        $consultations = [];
        foreach ($site->consultations as $consultation) {
            /** @var ConsultationSettings $settings */
            $settings = $consultation->getSettings();

            if ($settings->maintenanceMode && !$user->hasPrivilege($consultation, User::PRIVILEGE_ANY)) {
                continue;
            }

            if (in_array($settings->organizationId, $organizations)) {
                $consultations[] = $consultation;
            }
        }
        return $consultations;
    }

    /**
     * @param Consultation $consultation
     * @return \app\models\db\ConsultationMotionType|null
     */
    public static function getDiscussionType(Consultation $consultation)
    {
        foreach ($consultation->motionTypes as $motionType) {
            if ($motionType->supportType !== ISupportType::COLLECTING_SUPPORTERS) {
                return $motionType;
            }
        }
        return null;
    }

    /**
     * @param Consultation $consultation
     * @return \app\models\db\ConsultationMotionType|null
     */
    public static function getPetitionType(Consultation $consultation)
    {
        foreach ($consultation->motionTypes as $motionType) {
            if ($motionType->supportType === ISupportType::COLLECTING_SUPPORTERS) {
                return $motionType;
            }
        }
        return null;
    }

    /**
     * @param Consultation $consultation
     * @return bool
     */
    public static function isConsultationFullyConfigured(Consultation $consultation)
    {
        return (static::getPetitionType($consultation) !== null && static::getDiscussionType($consultation) !== null);
    }

    /**
     * @param Consultation[] $consultations
     * @return Motion[]
     */
    public static function getAllMotions($consultations)
    {
        $all = [];
        foreach ($consultations as $consultation) {
            if (!Tools::isConsultationFullyConfigured($consultation)) {
                continue;
            }
            $all = array_merge($all, static::getPetitionType($consultation)->getVisibleMotions(false));
            $all = array_merge($all, static::getMotionsCollecting($consultation));
            $all = array_merge($all, static::getDiscussionType($consultation)->getVisibleMotions(false));
            $all = array_merge($all, static::getMotionsAnswered($consultation));
        }
        return $all;
    }

    /**
     * @param Consultation $consultation
     * @return Motion[]
     */
    public static function getMotionsInDiscussion(Consultation $consultation)
    {
        $motions = Tools::getDiscussionType($consultation)->getVisibleMotions(false);
        return array_filter($motions, function (Motion $motion) {
            return ($motion->status == IMotion::STATUS_SUBMITTED_SCREENED);
        });
    }

    /**
     * @param Consultation[] $consultations
     * @return Motion[]
     */
    public static function getAllMotionsInDiscussion($consultations)
    {
        $all = [];
        foreach ($consultations as $consultation) {
            if (!Tools::isConsultationFullyConfigured($consultation)) {
                continue;
            }
            $all = array_merge($all, static::getMotionsInDiscussion($consultation));
        }
        return $all;
    }

    /**
     * @param Consultation $consultation
     * @return Motion[]
     */
    public static function getMotionsAnswered(Consultation $consultation)
    {
        $motions = Tools::getPetitionType($consultation)->getVisibleMotions(true); // true: wg. processed
        return array_filter($motions, function (Motion $motion) {
            return ($motion->status == IMotion::STATUS_PROCESSED);
        });
    }

    /**
     * @param Consultation[] $consultations
     * @return Motion[]
     */
    public static function getAllMotionsAnswered($consultations)
    {
        $all = [];
        foreach ($consultations as $consultation) {
            if (!Tools::isConsultationFullyConfigured($consultation)) {
                continue;
            }
            $all = array_merge($all, static::getMotionsAnswered($consultation));
        }
        return $all;
    }

    /**
     * @param Consultation $consultation
     * @return Motion[]
     */
    public static function getMotionsUnanswered(Consultation $consultation)
    {
{'fraction_non_alphanumeric': 0.11886810927249584, 'fraction_numerical': 0.0, 'mean_word_length': 2.5135878912968694, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2956004', 'n_tokens_mistral': 2989, 'n_tokens_neox': 2859, 'n_words': 506}