1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5/**
6 * User settings for workspace folders
7 *
8 * @author killing@leifos.de
9 */
10class ilWorkspaceFolderUserSettings
11{
12    /**
13     * @var ilTree
14     */
15    protected $tree;
16
17    /**
18     * @var int
19     */
20    protected $user_id;
21
22    /**
23     * @var ilWorkspaceFolderUserSettingsRepository
24     */
25    protected $repo;
26
27    /**
28     * Constructor
29     */
30    public function __construct(int $user_id, ilWorkspaceFolderUserSettingsRepository $repo, ilWorkspaceTree $tree = null)
31    {
32        $this->repo = $repo;
33        $this->user_id = $user_id;
34        $this->tree = ($tree != null)
35            ? $tree
36            : new ilWorkspaceTree($user_id);
37    }
38
39    /**
40     * Get Sortation of workspace folder
41     * @param int $wfld_id folder object id
42     * @return int
43     */
44    public function getSortation(int $wfld_id) : int
45    {
46        $sort = $this->repo->getSortation($wfld_id);
47        if ($sort > 0) {
48            return $sort;
49        }
50        if (ilObject::_lookupType($wfld_id) == "wfld") {
51            return ilWorkspaceFolderSorting::SORT_DERIVED;
52        }
53        return ilWorkspaceFolderSorting::SORT_ALPHABETICAL_ASC;
54    }
55
56    /**
57     * Update sortation for workspace folder
58     * @param int $wfld_id folder object id
59     * @param int $sortation
60     */
61    public function updateSortation(int $wfld_id, int $sortation)
62    {
63        $this->repo->updateSortation($wfld_id, $sortation);
64    }
65
66    /**
67     * Get effective sortation for a workspace folder (next upper
68     * context that has sortation > 0)
69     * @param int $wfld_wsp_id
70     * @return int|mixed
71     */
72    public function getEffectiveSortation(int $wfld_wsp_id)
73    {
74        $tree = $this->tree;
75
76        // get path
77        $path = $tree->getPathId($wfld_wsp_id);
78        // get object ids of path
79        $obj_ids = array_map(function ($wsp_id) use ($tree) {
80            return $tree->lookupObjectId($wsp_id);
81        }, $path);
82
83        // get sortations for object ids
84        $sortations = $this->repo->getSortationMultiple($obj_ids);
85
86        // search bottom to top first one with sortation > 0
87        foreach (array_reverse($obj_ids) as $id) {
88            if ($sortations[$id] > 0) {
89                return $sortations[$id];
90            }
91        }
92        return ilWorkspaceFolderSorting::SORT_ALPHABETICAL_ASC;
93    }
94}
95