Skip to content
Snippets Groups Projects
Course.php 41.1 KiB
Newer Older
     * Permission levels may be supplied to limit the course list.
     *
     * @param string $user_id The ID of the user whose courses shall be retrieved.
     *
     * @param string[] $perms The permission levels of the user that shall be
     *     regarded when retrieving courses.
     *
     * @param bool $with_deputies Whether to include courses where the user is
     *     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 = User::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) {
        }

        //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);
    }

    /**
     * Get user information for all users in this course
     *
     */
    public function getMembersData(?string $status = ''): array
    {
        $result = [];

        if (!$status) {
            foreach ($this->members->orderBy('position, nachname') as $member) {
                $result[$member->user_id] = $member->getExportData();
            }
            foreach ($this->admission_applicants->findBy('status', 'accepted')->orderBy('position') as $member) {
                $result[$member->user_id] = $member->getExportData();
            }
        } elseif ($status === 'awaiting') {
            foreach ($this->admission_applicants->findBy('status', $status)->orderBy('position') as $member) {
                $result[$member->user_id] = $member->getExportData();
            }
        } elseif ($status === 'claiming') {
            $cs = CourseSet::getSetForCourse($this->id);
            if (is_object($cs) && !$cs->hasAlgorithmRun()) {
                $claiming_users = User::findFullMany(array_keys(AdmissionPriority::getPrioritiesByCourse($cs->getId(), $this->id)), 'ORDER BY nachname');
                foreach ($claiming_users as $claiming_user) {
                        $studycourse = [];
                        $claiming_user->studycourses->map(function($sc) use (&$studycourse) {
                            $studycourse[]= $sc->studycourse->name .  ',' . $sc->degree->name . ',' . $sc->semester;
                        });
                        $export_data = [
                            'status' => $status,
                            'salutation' => $claiming_user->salutation,
                            'Titel' => $claiming_user->title_front,
                            'Vorname' => $claiming_user->vorname,
                            'Nachname' => $claiming_user->nachname,
                            'Titel2' => $claiming_user->title_rear,
                            'username' => $claiming_user->username,
                            'privadr' => $claiming_user->privadr,
                            'privatnr' => $claiming_user->privatnr,
                            'Email' => $claiming_user->email,
                            'Anmeldedatum' => '',
                            'Matrikelnummer' => $claiming_user->matriculation_number,
                            'studiengaenge' => implode(';', $studycourse),
                            'position' => 0,
                        ];
                    $result[$claiming_user->user_id] = $export_data;
                }
            }
        }

        return $result;
    }