<commit_before><?php
namespace common\models;

use common\components\helpers\CArrayHelper;

/**
 * @author <NAME> <<EMAIL>>
 */
trait UserQueryTrait
{
    /**
     * Counts total user projects.
     * @param  boolean $mustBeOwner Flag to filter and return only super user owned items (for regular users this flag is ignored)
     * @return integer
     */
    public function countProjects($mustBeOwner = false)
    {
        $query = Project::find()->select(Project::tableName() . '.id');

        if ($this->type != self::TYPE_SUPER || $mustBeOwner) {
            $query->joinWith('userRels', false)
                ->andWhere([UserProjectRel::tableName() . '.userId' => $this->id]);
        }

        return (int) $query->count();
    }

    /**
     * Returns filtered list with user projects.
     * @param  string $search
     * @param  integer $limit       Number of returned results.
     * @param  integer $offset      Results page offset
     * @param  boolean $mustBeOwner Flag to filter and return only super user owned items (for regular users this flag is ignored)
     * @return Project[]
     */
    public function searchProjects($search, $limit = -1, $offset = 0, $mustBeOwner = false)
    {
        $searchParts = explode(' ', $search); // some sort of fuzzy searching

        $query = Project::find()
            ->distinct()
            ->with(['featuredScreen', 'screens']);

        if ($this->type != self::TYPE_SUPER || $mustBeOwner) {
            $query->joinWith('userRels', false)
                ->andWhere([UserProjectRel::tableName() . '.userId' => $this->id]);
        }

        return $query->andWhere(['or like', Project::tableName() . '.title', $searchParts])
            ->orderBy([Project::tableName() . '.createdAt' => SORT_ASC])
            ->limit($limit)
            ->offset($offset)
            ->all();
    }

    /**
     * Returns list with user projects.
     * @param  integer $limit       Number of returned results.
     * @param  integer $offset      Results page offset
     * @param  boolean $mustBeOwner Flag to filter and return only super user owned items (for regular users this flag is ignored)
     * @return Project[]
     */
    public function findProjects($limit = -1, $offset = 0, $mustBeOwner = false)
    {
        $query = Project::find()
            ->distinct()
            ->with(['featuredScreen', 'screens']);

        if ($this->type != self::TYPE_SUPER || $mustBeOwner) {
            $query->joinWith('userRels', false)
                ->andWhere([UserProjectRel::tableName() . '.userId' => $this->id]);
        }

        return $query->orderBy([Project::tableName() . '.createdAt' => SORT_DESC])
            ->limit($limit)
            ->offset($offset)
            ->all();<commit_msg>show latest projects first on search<commit_after><?php
namespace common\models;

use common\components\helpers\CArrayHelper;

/**
 * @author <NAME> <<EMAIL>>
 */
trait UserQueryTrait
{
    /**
     * Counts total user projects.
     * @param  boolean $mustBeOwner Flag to filter and return only super user owned items (for regular users this flag is ignored)
     * @return integer
     */
    public function countProjects($mustBeOwner = false)
    {
        $query = Project::find()->select(Project::tableName() . '.id');

        if ($this->type != self::TYPE_SUPER || $mustBeOwner) {
            $query->joinWith('userRels', false)
                ->andWhere([UserProjectRel::tableName() . '.userId' => $this->id]);
        }

        return (int) $query->count();
    }

    /**
     * Returns filtered list with user projects.
     * @param  string $search
     * @param  integer $limit       Number of returned results.
     * @param  integer $offset      Results page offset
     * @param  boolean $mustBeOwner Flag to filter and return only super user owned items (for regular users this flag is ignored)
     * @return Project[]
     */
    public function searchProjects($search, $limit = -1, $offset = 0, $mustBeOwner = false)
    {
        $searchParts = explode(' ', $search); // some sort of fuzzy searching

        $query = Project::find()
            ->distinct()
            ->with(['featuredScreen', 'screens']);

        if ($this->type != self::TYPE_SUPER || $mustBeOwner) {
            $query->joinWith('userRels', false)
                ->andWhere([UserProjectRel::tableName() . '.userId' => $this->id]);
        }

        return $query->andWhere(['or like', Project::tableName() . '.title', $searchParts])
            ->orderBy([Project::tableName() . '.createdAt' => SORT_DESC])
            ->limit($limit)
            ->offset($offset)
            ->all();
    }

    /**
     * Returns list with user projects.
     * @param  integer $limit       Number of returned results.
     * @param  integer $offset      Results page offset
     * @param  boolean $mustBeOwner Flag to filter and return only super user owned items (for regular users this flag is ignored)
     * @return Project[]
     */
    public function findProjects($limit = -1, $offset = 0, $mustBeOwner = false)
    {
        $query = Project::find()
            ->distinct()
            ->with(['featuredScreen', 'screens']);

        if ($this->type != self::TYPE_SUPER || $mustBeOwner) {
            $query->joinWith('userRels', false)
                ->andWhere([UserProjectRel::tableName() . '.userId' => $this->id]);
        }

        return $query->orderBy([Project::tableName() . '.createdAt' => SORT_DESC])
            ->limit($limit)
            ->offset($offset)
            ->all();
{'fraction_non_alphanumeric': 0.14836154123154482, 'fraction_numerical': 0.0014404033129276198, 'mean_word_length': 2.3343337334933976, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 73, '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': '2992134', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1521, 'n_words': 426}