Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Output the user submission actionbar for this activity.
19
 *
20
 * @package   mod_assign
21
 * @copyright 2021 Adrian Greeve <adrian@moodle.com>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace mod_assign\output;
26
 
27
use templatable;
28
use renderable;
29
use moodle_url;
30
use help_icon;
31
use single_button;
32
use stdClass;
33
 
34
/**
35
 * Output the user submission actionbar for this activity.
36
 *
37
 * @package   mod_assign
38
 * @copyright 2021 Adrian Greeve <adrian@moodle.com>
39
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class user_submission_actionmenu implements templatable, renderable {
42
 
43
    /** @var int The course module ID. */
44
    protected $cmid;
45
    /** @var bool Whether to show the submit button. */
46
    protected $showsubmit;
47
    /** @var bool Whether to show the edit button. */
48
    protected $showedit;
49
    /** @var stdClass A submission for this activity. */
50
    protected $submission;
51
    /** @var stdClass A team submission for this activity. */
52
    protected $teamsubmission;
53
    /** @var int The time limit for the submission. 0 = no time limit. */
54
    protected $timelimit;
55
 
56
    /**
57
     * Constructor for this object.
58
     *
59
     * @param int $cmid The course module ID.
60
     * @param bool $showsubmit Whether to show the submit button.
61
     * @param bool $showedit Whether to show the edit button.
62
     * @param stdClass|null $submission A submission for this activity.
63
     * @param stdClass|null $teamsubmission A team submission for this activity.
64
     * @param int $timelimit The time limit for completing this activity.
65
     */
66
    public function __construct(int $cmid, bool $showsubmit, bool $showedit, stdClass $submission = null,
67
            stdClass $teamsubmission = null, int $timelimit = 0) {
68
 
69
        $this->cmid = $cmid;
70
        $this->showsubmit = $showsubmit;
71
        $this->showedit = $showedit;
72
        $this->submission = $submission;
73
        $this->teamsubmission = $teamsubmission;
74
        $this->timelimit = $timelimit;
75
    }
76
 
77
    /**
78
     * Get the submission status.
79
     *
80
     * @return string The status of the submission.
81
     */
82
    protected function get_current_status(): string {
83
        if (!is_null($this->teamsubmission) && isset($this->teamsubmission->status)) {
84
            return $this->teamsubmission->status;
85
        } else if (!empty((array)$this->submission)) {
86
            return $this->submission->status;
87
        } else {
88
            return ASSIGN_SUBMISSION_STATUS_NEW;
89
        }
90
    }
91
 
92
    /**
93
     * Export the submission buttons for the page.
94
     *
95
     * @param  \renderer_base $output renderer base output.
96
     * @return array The data to be rendered.
97
     */
98
    public function export_for_template(\renderer_base $output): array {
99
        $data = ['edit' => false, 'submit' => false, 'remove' => false, 'previoussubmission' => false];
100
        if ($this->showedit) {
101
            $url = new moodle_url('/mod/assign/view.php', ['id' => $this->cmid, 'action' => 'editsubmission']);
102
            $button = new single_button($url, get_string('editsubmission', 'mod_assign'), 'get');
103
            $data['edit'] = [
104
                'button' => $button->export_for_template($output),
105
            ];
106
            $status = $this->get_current_status();
107
            if ($status !== ASSIGN_SUBMISSION_STATUS_NEW && $status !== ASSIGN_SUBMISSION_STATUS_REOPENED) {
108
                $url = new moodle_url('/mod/assign/view.php', ['id' => $this->cmid, 'action' => 'removesubmissionconfirm']);
109
                $button = new single_button($url, get_string('removesubmission', 'mod_assign'), 'get');
110
                $data['remove'] = ['button' => $button->export_for_template($output)];
111
            }
112
            if ($status === ASSIGN_SUBMISSION_STATUS_REOPENED) {
113
                $params = ['id' => $this->cmid, 'action' => 'editprevioussubmission', 'sesskey' => sesskey()];
114
                $url = new moodle_url('/mod/assign/view.php', $params);
115
                $button = new single_button($url, get_string('addnewattemptfromprevious', 'mod_assign'), 'get');
116
                $help = new help_icon('addnewattemptfromprevious', 'mod_assign');
117
                $data['previoussubmission'] = [
118
                    'button' => $button->export_for_template($output),
119
                    'help' => $help->export_for_template($output)
120
                ];
121
                $url = new moodle_url('/mod/assign/view.php', ['id' => $this->cmid, 'action' => 'editsubmission']);
122
                $newattemptbutton = new single_button(
123
                    $url,
124
                    get_string('addnewattempt', 'mod_assign'),
125
                    'get',
126
                    single_button::BUTTON_PRIMARY
127
                );
128
                $newattempthelp = new help_icon('addnewattempt', 'mod_assign');
129
                $data['edit']['button'] = $newattemptbutton->export_for_template($output);
130
                $data['edit']['help'] = $newattempthelp->export_for_template($output);
131
            }
132
            if ($status === ASSIGN_SUBMISSION_STATUS_NEW) {
133
 
134
                $timelimitenabled = get_config('assign', 'enabletimelimit');
135
                if ($timelimitenabled && $this->timelimit && empty($this->submission->timestarted)) {
136
                    $confirmation = new \confirm_action(
137
                        get_string('confirmstart', 'assign', format_time($this->timelimit)),
138
                        null,
139
                        get_string('beginassignment', 'assign')
140
                    );
141
                    $urlparams = array('id' => $this->cmid, 'action' => 'editsubmission');
142
                    $beginbutton = new \action_link(
143
                        new moodle_url('/mod/assign/view.php', $urlparams),
144
                            get_string('beginassignment', 'assign'),
145
                            $confirmation,
146
                            ['class' => 'btn btn-primary']
147
                    );
148
                    $data['edit']['button'] = $beginbutton->export_for_template($output);
149
                    $data['edit']['begin'] = true;
150
                    $data['edit']['help'] = '';
151
                } else {
152
                    $newattemptbutton = new single_button(
153
                        $url,
154
                        get_string('addsubmission', 'mod_assign'),
155
                        'get',
156
                        single_button::BUTTON_PRIMARY
157
                    );
158
                    $data['edit']['button'] = $newattemptbutton->export_for_template($output);
159
                    $data['edit']['help'] = '';
160
                }
161
            }
162
        }
163
        if ($this->showsubmit) {
164
            $url = new moodle_url('/mod/assign/view.php', ['id' => $this->cmid, 'action' => 'submit']);
165
            $button = new single_button($url, get_string('submitassignment', 'mod_assign'), 'get', single_button::BUTTON_PRIMARY);
166
            $help = new help_icon('submitassignment', 'mod_assign');
167
            $data['submit'] = [
168
                'button' => $button->export_for_template($output),
169
                'help' => $help->export_for_template($output)
170
            ];
171
        }
172
        return $data;
173
    }
174
}