aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/coursewizardsteps/AdvancedBasicDataWizardStep.php
blob: c3432c9ebbb2f4ff057b246f2b06d027eefe9787 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
 * AdvancedBasicDataWizardStep.php
 * Course wizard step for getting the basic course data.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Thomas Hackl <thomas.hackl@uni-passau.de>
 * @author      Stefan Osterloh <s.osterloh@uni-oldenburg.de>
 * @copyright   2015 Stud.IP Core-Group
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 */

class AdvancedBasicDataWizardStep extends BasicDataWizardStep
{
    /**
     * Returns the Flexi template for entering the necessary values
     * for this step.
     *
     * @param Array $values Pre-set values
     * @param int $stepnumber which number has the current step in the wizard?
     * @param String $temp_id temporary ID for wizard workflow
     * @return String a Flexi template for getting needed data.
     */
    public function getStepTemplate($values, $stepnumber, $temp_id)
    {
        $values = $this->adjustValues($values);

       // We only need our own stored values here.
        if (!empty($values[__CLASS__]['studygroup'])) {
            return parent::getStepTemplate($values, $stepnumber, $temp_id);
        }

        // Load template from step template directory.
        $factory = new Flexi\Factory($GLOBALS['STUDIP_BASE_PATH'].'/app/views/course/wizard/steps');
        $template = $factory->open('advancedbasicdata/index');

        $template = $this->setupTemplateAttributes($template, $values, $stepnumber, $temp_id);
        if (!$template) {
            return '';
        }

        $values = $this->makeI18N($values[__CLASS__] ?? [], ['name', 'description', 'subtitle', 'kind']);

        $template->set_attribute('values', array_merge($template->values, $values));

        return $template->render();
    }

    /**
     * Validates if given values are sufficient for completing the current
     * course wizard step and switch to another one. If not, all errors are
     * collected and shown via PageLayout::postMessage.
     *
     * @param mixed $values Array of stored values
     * @return bool Everything ok?
     */
    public function validate($values)
    {
        $values = $this->adjustValues($values);
        return parent::validate($values);
    }

    /**
     * Stores the given values to the given course.
     *
     * @param Course $course the course to store values for
     * @param Array $values values to set
     * @return Course The course object with updated values.
     */
    public function storeValues($course, $values)
    {
        $values = $this->adjustValues($values);
        $course = parent::storeValues($course, $values);

        // There probably was an error upon storing
        if (!$course) {
            return false;
        }

        // Studygroup? -> nothing to do here
        if (!empty($values[__CLASS__]['studygroup'])) {
            return $course;
        }

        // Add advanced data
        $course->untertitel = new I18NString($values[__CLASS__]['subtitle'], $values[__CLASS__]['subtitle_i18n']);
        $course->art = new I18NString($values[__CLASS__]['kind'], $values[__CLASS__]['kind_i18n']);
        $course->ects = $values[__CLASS__]['ects'];
        $course->admission_turnout = $values[__CLASS__]['maxmembers'];
        if ($course->store() === false) {
            PageLayout::postError(sprintf(_('Es ist ein Fehler beim Speichern der erweiterten Einstellungen für %s aufgetreten. Kontrollieren Sie bitte:')
                    , htmlReady($course->name)),
                    [_('Untertitel der Veranstalung'),
                        _('Art der Veranstaltung'),
                        _('ECTS-Punkte der Veranstaltung'),
                        _('Max. Teilnehmendenzahl der Veranstaltung')]);
        }
        return $course;
    }

    /**
     * This method will adjust the given values from parent class
     * or use previously set values from this class.
     *
     * @param array $values Array of values
     * @return array of adjusted values
     */
    private function adjustValues($values)
    {
        $parent_class = get_parent_class($this);

        if (!isset($values[__CLASS__]) && isset($values[$parent_class])) {
            $values[__CLASS__] = $values[$parent_class];
        } else {
            $values[$parent_class] = $values[__CLASS__] ?? [];
        }

        return $values;
    }

    /**
     * Copy values for basic data wizard step from given course.
     * @param Course $course
     * @param Array $values
     */
    public function copy($course, $values)
    {
        $values = parent::copy($course, $values);
        $values = $this->adjustValues($values);

        $values[__CLASS__] = array_merge($values[__CLASS__], [
            'subtitle' => $course->untertitel,
            'subtitle_i18n' => is_object($course->untertitel) ? $course->untertitel->toArray() : $course->untertitel,
            'kind' => $course->art,
            'kind_i18n' => is_object($course->art) ? $course->art->toArray() : $course->art,
            'ects' => $course->ects,
            'maxmembers' => $course->admission_turnout,
        ]);

        return $values;
    }
}