<commit_before><?php

namespace Chamilo\Core\Repository\ContentObject\LearningPath\Service;

use Chamilo\Core\Repository\ContentObject\LearningPath\Display\Manager;
use Chamilo\Core\Repository\ContentObject\LearningPath\Domain\Action;
use Chamilo\Core\Repository\ContentObject\LearningPath\Domain\LearningPathTreeNode;
use Chamilo\Libraries\File\Redirect;
use Chamilo\Libraries\Platform\Translation;

/**
 * Generates the actions for a given LearningPathTreeNode
 *
 * @author <NAME> - <NAME>
 */
class NodeActionGenerator
{
    /**
     * @var Translation
     */
    protected $translator;

    /**
     * @var array
     */
    protected $baseParameters;

    /**
     * NodeActionGenerator constructor.
     *
     * @param Translation $translator
     * @param array $baseParameters
     */
    public function __construct(Translation $translator, array $baseParameters = array())
    {
        $this->translator = $translator;
        $this->baseParameters = $baseParameters;
    }

    /**
     * Generates the acions for a given LearningPathTreeNode
     *
     * @param LearningPathTreeNode $learningPathTreeNode
     *
     * @return array|Action[]
     */
    public function generateNodeActions(
        LearningPathTreeNode $learningPathTreeNode, $canEditLearningPathTreeNode = false
    ): array
    {
        $actions = array();

        if($canEditLearningPathTreeNode)
        {
            $actions[] = $this->getUpdateNodeAction($learningPathTreeNode);
            $actions[] = $this->getDeleteNodeAction($learningPathTreeNode);
            $actions[] = $this->getMoveNodeAction($learningPathTreeNode);
        }

        return $actions;
    }

    /**
     * Returns the action to update a given LearningPathTreeNode
     *
     * @param LearningPathTreeNode $learningPathTreeNode
     *
     * @return Action
     */
    protected function getUpdateNodeAction(LearningPathTreeNode $learningPathTreeNode)
    {
        $title = $this->translator->getTranslation('UpdaterComponent', null, Manager::context());
        $url = $this->getUrl(
            array(
                Manager::PARAM_ACTION => Manager::ACTION_UPDATE_COMPLEX_CONTENT_OBJECT_ITEM,
                Manager::PARAM_CHILD_ID => $learningPathTreeNode->getId()
            )
        );

        return new Action('edit', $title, $url, 'fa fa-pencil');
    }

    /**
     * Returns the action to delete a given LearningPathTreeNode
     *
     * @param LearningPathTreeNode $learningPathTreeNode
     *
     * @return Action
     */
    protected function getDeleteNodeAction(LearningPathTreeNode $learningPathTreeNode)
    {
        $title = $this->translator->getTranslation('DeleterComponent', null, Manager::context());
        $url = $this->getUrl(
            array(
                Manager::PARAM_ACTION => Manager::ACTION_DELETE_COMPLEX_CONTENT_OBJECT_ITEM,
                Manager::PARAM_CHILD_ID => $learningPathTreeNode->getId()
            )
        );

        return new Action('delete', $title, $url, 'glyphicon glyphicon-remove');
    }

    /**
     * Returns the action to move a given LearningPathTreeNode
     *
     * @param LearningPathTreeNode $learningPathTreeNode
     *
     * @return Action
     */
    protected function getMoveNodeAction(LearningPathTreeNode $learningPathTreeNode)
    {
        $title = $this->translator->getTranslation('Move', null, Manager::context());
        $url = $this->getUrl(
            array(
                Manager::PARAM_ACTION => Manager::ACTION_MOVE,
                Manager::PARAM_CHILD_ID => $learningPathTreeNode->getId()
            )
        );

        return new Action('move', $title, $url, 'glyphicon glyphicon-random');
    }

    /**
     * Generates a URL for the given parameters and filters, includes the base parameters given in this service
     *
     * @param array $parameters
     * @param array $filter
     * @param bool $encode_entities
     *
     * @return string
     */
    public function getUrl($parameters = array(), $filter = array(), $encode_entities = false)
    {
        $parameters = (count($parameters) ? array_merge($this->baseParameters, $parameters) : $this->baseParameters);

        $redirect = new Redirect($parameters, $filter, $encode_entities);

        return $redirect->getUrl();
    }
}<commit_msg>[Learning Path] The refactoring ocntinues: adding more context menu actions<commit_after><?php

namespace Chamilo\Core\Repository\ContentObject\LearningPath\Service;

use Chamilo\Core\Repository\ContentObject\LearningPath\Display\Manager;
use Chamilo\Core\Repository\ContentObject\LearningPath\Domain\Action;
use Chamilo\Core\Repository\ContentObject\LearningPath\Domain\LearningPathTreeNode;
use Chamilo\Libraries\File\Redirect;
use Chamilo\Libraries\Platform\Translation;

/**
 * Generates the actions for a given LearningPathTreeNode
 *
 * @author <NAME> - <NAME>
 */
class NodeActionGenerator
{
    /**
     * @var Translation
     */
    protected $translator;

    /**
     * @var array
     */
    protected $baseParameters;

    /**
     * NodeActionGenerator constructor.
     *
     * @param Translation $translator
     * @param array $baseParameters
     */
    public function __construct(Translation $translator, array $baseParameters = array())
    {
        $this->translator = $translator;
        $this->baseParameters = $baseParameters;
    }

    /**
     * Generates the acions for a given LearningPathTreeNode
     *
     * @param LearningPathTreeNode $learningPathTreeNode
     *
     * @return array|Action[]
     */
    public function generateNodeActions(
        LearningPathTreeNode $learningPathTreeNode, $canEditLearningPathTreeNode = false
    ): array
    {
        $actions = array();

        if($canEditLearningPathTreeNode)
        {
            $actions[] = $this->getUpdateNodeAction($learningPathTreeNode);
            $actions[] = $this->getBlockOrUnblockNodeAction($learningPathTreeNode);
            $actions[] = $this->getNodeReportingAction($learningPathTreeNode);

            if(!$learningPathTreeNode->isRootNode())
            {
                $actions[] = $this->getDeleteNodeAction($learningPathTreeNode);
                $actions[] = $this->getMoveNodeAction($learningPathTreeNode);
            }
        }

        $actions[] = $this->getMyProgressNodeAction($learningPathTreeNode);
        $actions[] = $this->getNodeActivityAction($learningPathTreeNode);

        if($learningPathTreeNode->hasChildNodes())
        {
            $actions[] = $this->getManageNodesAction($learningPathTreeNode);
        }

        return $actions;
    }

    /**
     * Returns the action to update a given LearningPathTreeNode
     *
     * @param LearningPathTreeNode $learningPathTreeNode
     *
     * @return Action
     */
    protected function getUpdateNodeAction(LearningPathTreeNode $learningPathTreeNode)
    {
        $title = $this->translator->getTranslation('UpdaterComponent', null, Manager::context());
        $url = $this->getUrl(
            array(
                Manager::PARAM_ACTION => Manager::ACTION_UPDATE_COMPLEX_CONTENT_OBJECT_ITEM,
                Manager::PARAM_CHILD_ID => $learningPathTreeNode->getId()
            )
        );

        return new Action('edit', $title, $url, 'fa fa-pencil');
    }

    /**
     * Returns the action to delete a given LearningPathTreeNode
     *
     * @param LearningPathTreeNode $learningPathTreeNode
     *
     * @return Action
     */
    protected function getDeleteNodeAction(LearningPathTreeNode $learningPathTreeNode)
    {
        $title = $this->translator->getTranslation('DeleterComponent', null, Manager::context());
        $url = $this->getUrl(
            array(
                Manager::PARAM_ACTION => Manager::ACTION_DELETE_COMPLEX_CONTENT_OBJECT_ITEM,
                Manager::PARAM_CHILD_ID => $learningPathTreeNode->getId()
            )
        );

        return new Action('delete', $title, $url, 'glyphicon glyphicon-remove');
    }

    /**
     * Returns the action to move a given LearningPathTreeNode
     *
     * @param LearningPathTreeNode $learningPathTreeNode
     *
     * @return Action
     */
    protected function getMoveNodeAction(LearningPathTreeNode $learningPathTreeNode)
    {
        $title = $this->translator->getTranslation('Move', null, Manager::context());
        $url = $this->getUrl(
            array(
                Manager::PARAM_ACTION => Manager::ACTION_MOVE,
                Manager::PARAM_CHILD_ID => $learningPathTreeNode->getId()
            )
        );

        return new Action('move', $title, $url, 'glyphicon glyphicon-random');
    }

    /**
     * Returns the action to view the activity of a given LearningPathTreeNode
     *
     * @param LearningPathTreeNode $learningPathTreeNode
     *
     * @return Action
     */
    protected function getNodeActivityAction(LearningPathTreeNode $learningPathTreeNode)
    {
        $title = $this->translator->getTranslation('ActivityComponent', null, Manager::context());
        $url = $this->getUrl(
            array(
                Manager::PARAM_ACTION => Manager::ACTION_ACTIVITY,
                Manager::PARAM_CHILD_ID => $learningPathTreeNode->getId()
            )
        );

        return new Action('activity', $title, $url, 'fa fa-mouse-pointer');
    }

    /**
     * Returns the action to view the activity of a given LearningPathTreeNode
     *
     * @param LearningPathTreeNode $learningPathTreeNode
     *
     * @return Action
     */
    protected function getManageNodesAction(LearningPathTreeNode $learningPathTreeNode)
    {
        $title = $this->translator->getTranslation('ManagerComponent', null, Manager::context());
        $url = $this->getUrl(
            array(
                Manager::PARAM_ACTION => Manager::ACTION_MANAGE,
                Manager::PARAM_CHILD_ID => $learningPathTreeNode->getId()
            )
        );

        return new Action('manage', $title, $url, 'fa fa-bars');
    }

    /**
     * Returns the action to block or unblock a given LearningPathTreeNode
     *
     * @param LearningPathTreeNode $learningPathTreeNode
     *
     * @return Action
     */
    protected function getBlockOrUnblockNodeAction(LearningPathTreeNode $learningPathTreeNode)
    {
        $translationVariable = ($learningPathTreeNode->getLearningPathChild() &&
            $learningPathTreeNode->getLearningPathChild()->isBlocked()) ?
            'MarkAsOptional' : 'MarkAsRequired';

        $icon = ($learningPathTreeNode->getLearningPathChild() &&
            $learningPathTreeNode->getLearningPathChild()->isBlocked()) ?
            'unlock' : 'ban';

        $title = $this->translator->getTranslation($translationVariable, null, Manager::context());
        $url = $this->getUrl(
            array(
                Manager::PARAM_ACTION => Manager::ACTION_TOGGLE_BLOCKED_STATUS,
                Manager::PARAM_CHILD_ID => $learningPathTreeNode->getId()
            )
        );

        return new Action('block', $title, $url, 'fa fa-' . $icon);
    }

    /**
     * Returns the action to view the progress for a given LearningPathTreeNode
     *
     * @param LearningPathTreeNode $learningPathTreeNode
     *
     * @return Action
     */
    protected function getMyProgressNodeAction(LearningPathTreeNode $learningPathTreeNode)
    {
        $title = $this->translator->getTranslation('MyProgress', null, Manager::context());
        $url = $this->getUrl(
            array(
                Manager::PARAM_ACTION => Manager::ACTION_REPORTING,
                Manager::PARAM_CHILD_ID => $learningPathTreeNode->getId()
            )
        );

        return new Action('progress', $title, $url, 'fa fa-pie-chart');
    }

    /**
     * Returns the action to view the reporting for a given LearningPathTreeNode
     *
     * @param LearningPathTreeNode $learningPathTreeNode
     *
     * @return Action
     */
    protected function getNodeReportingAction(LearningPathTreeNode $learningPathTreeNode)
    {
        $title = $this->translator->getTranslation('Reporting', null, Manager::context());
        $url = $this->getUrl(
            array(
                Manager::PARAM_ACTION => Manager::ACTION_REPORTING,
                Manager::PARAM_CHILD_ID => $learningPathTreeNode->getId()
            )
        );

        return new Action('reporting', $title, $url, 'fa fa-bar-chart');
    }

    /**
     * Generates a URL for the given parameters and filters, includes the base parameters given in this service
     *
     * @param array $parameters
     * @param array $filter
     * @param bool $encode_entities
     *
     * @return string
     */
    public function getUrl($parameters = array(), $filter = array(), $encode_entities = false)
    {
        $parameters = (count($parameters) ? array_merge($this->baseParameters, $parameters) : $this->baseParameters);

        $redirect = new Redirect($parameters, $filter, $encode_entities);

        return $redirect->getUrl();
    }
}
{'fraction_non_alphanumeric': 0.1129019819541914, 'fraction_numerical': 0.0, 'mean_word_length': 2.684090909090909, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 102, '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': '5531835', 'n_tokens_mistral': 3556, 'n_tokens_neox': 3439, 'n_words': 693}