Skip to content
Snippets Groups Projects
Course.class.php 38.3 KiB
Newer Older
     *     a deputy (true) or not (false). Defaults to true.
     *
     * @return Course[] A list of courses.
     */
    public static function findByUser($user_id, $perms = [], $with_deputies = true)
    {
        if (!$user_id) {
            return [];
        }

        $db = DBManager::get();
        $sql = "SELECT `seminar_id`
                FROM `seminar_user`
                WHERE `user_id` = :user_id";
        $sql_params = ['user_id' => $user_id];
        if (is_array($perms) && count($perms)) {
            $sql .= ' AND `status` IN (:perms)';
            $sql_params['perms'] = $perms;
        }
        $seminar_ids = $db->fetchFirst($sql, $sql_params);
        if (Config::get()->DEPUTIES_ENABLE && $with_deputies) {
            $sql = 'SELECT range_id FROM `deputies` WHERE `deputies`.`user_id` = :user_id';
            $seminar_ids = array_merge($seminar_ids, $db->fetchFirst($sql, $sql_params));
        }

        $name_sort = Config::get()->IMPORTANT_SEMNUMBER ? 'VeranstaltungsNummer, Name' : 'Name';

        return Course::findBySQL(
            "LEFT JOIN semester_courses ON (semester_courses.course_id = seminare.Seminar_id)
             WHERE Seminar_id IN (?)
             ORDER BY semester_courses.semester_id IS NULL DESC, start_time DESC, {$name_sort}",
            [$seminar_ids]
        );
    }

    /**
     * Returns whether this course is a studygroup
     * @return bool
     */
    public function isStudygroup()
    {
        return in_array($this->status, studygroup_sem_types());
    }

    /**
     *
     */
    public function setDefaultTools()
    {
        $this->tools = [];
        foreach (array_values($this->getSemClass()->getActivatedModuleObjects()) as $module) {
            PluginManager::getInstance()->setPluginActivated($module->getPluginId(), $this->id, true);
            $this->tools[] = ToolActivation::find([$this->id, $module->getPluginId()]);
        }
    }

    /**
     * @param $name string name of tool / plugin
     * @return bool
     */
    public function isToolActive($name)
    {
        $plugin = PluginEngine::getPlugin($name);
        return $plugin && $this->tools->findOneby('plugin_id', $plugin->getPluginId());
    }

    /**
     * returns all activated plugins/modules for this course
     * @return StudipModule[]
     */
    public function getActivatedTools()
    {
        return array_filter($this->tools->getStudipModule());
    }
Moritz Strohm's avatar
Moritz Strohm committed

    /**
     * @see Range::__toString()
     */
    public function __toString() : string
    {
        return $this->getFullName();
    }

    /**
     * @inheritDoc
     */
    public static function getCalendarOwner(string $owner_id): ?\Studip\Calendar\Owner
    {
        return self::find($owner_id);
    }

    /**
     * @inheritDoc
     */
    public function isCalendarReadable(?string $user_id = null): bool
    {
        if ($user_id === null) {
            $user_id = self::findCurrent()->id;
        }

        //Calendar read permissions are granted for all participants
        //that have at least user permissions.
        return $GLOBALS['perm']->have_studip_perm('user', $this->id, $user_id);
    }

    /**
     * @inheritDoc
     */
    public function isCalendarWritable(string $user_id = null): bool
    {
        if ($user_id === null) {
            $user_id = self::findCurrent()->id;
        }

        //Calendar write permissions are granted for all participants
        //that have autor permissions or higher.
        return $GLOBALS['perm']->have_studip_perm('autor', $this->id, $user_id);
    }