1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5/**
6 * Glossary actor class
7 *
8 * @author Alex Killing <alex.killing@gmx.de>
9 */
10class ilGlossaryAct
11{
12    /**
13     * @var ilObjGlossary
14     */
15    protected $glossary;
16
17    /**
18     * @var ilObjUser acting user
19     */
20    protected $user;
21
22    /**
23     * @var ilAccessHandler
24     */
25    protected $access;
26
27    /**
28     * ilGlossaryAct constructor.
29     * @param ilObjGlossary $a_glossary
30     * @param ilObjUser $a_user
31     */
32    protected function __construct(ilObjGlossary $a_glossary, ilObjUser $a_user)
33    {
34        global $DIC;
35
36        $this->access = $DIC->access();
37        $this->glossary = $a_glossary;
38        $this->user = $a_user;
39    }
40
41    /**
42     * Get instance
43     * @param ilObjGlossary $a_glossary
44     * @param ilObjUser $a_user
45     * @return ilGlossaryAct
46     */
47    public static function getInstance(ilObjGlossary $a_glossary, ilObjUser $a_user)
48    {
49        return new self($a_glossary, $a_user);
50    }
51
52    /**
53     * Copy term
54     *
55     * @param ilObjGlossary $a_source_glossary
56     * @param int $a_term_id term id
57     */
58    public function copyTerm(ilObjGlossary $a_source_glossary, $a_term_id)
59    {
60        if (!$this->access->checkAccessOfUser($this->user->getId(), "write", "", $this->glossary->getRefId())) {
61            return;
62        }
63
64        if (!$this->access->checkAccessOfUser($this->user->getId(), "read", "", $a_source_glossary->getRefId())) {
65            return;
66        }
67
68        if (ilGlossaryTerm::_lookGlossaryID($a_term_id) != $a_source_glossary->getId()) {
69            return;
70        }
71
72        ilGlossaryTerm::_copyTerm($a_term_id, $this->glossary->getId());
73    }
74
75
76    /**
77     * Reference a term of another glossary in current glossary
78     *
79     * @param ilObjGlossary $a_source_glossary
80     * @param int[] $a_term_ids
81     */
82    public function referenceTerms(ilObjGlossary $a_source_glossary, $a_term_ids)
83    {
84        if (!$this->access->checkAccessOfUser($this->user->getId(), "write", "", $this->glossary->getRefId())) {
85            return;
86        }
87
88        if (!$this->access->checkAccessOfUser($this->user->getId(), "read", "", $a_source_glossary->getRefId())) {
89            return;
90        }
91
92        $refs = new ilGlossaryTermReferences($this->glossary->getId());
93        foreach ($a_term_ids as $term_id) {
94            if (ilGlossaryTerm::_lookGlossaryID($term_id) != $a_source_glossary->getId()) {
95                continue;
96            }
97
98            if ($this->glossary->getId() == $a_source_glossary->getId()) {
99                continue;
100            }
101            $refs->addTerm($term_id);
102        }
103        $refs->update();
104    }
105}
106