MDL-32895: Improvements to assignment upgrade tool for large numbers of assignments.
[moodle.git] / admin / tool / assignmentupgrade / renderer.php
CommitLineData
bbd0e548
DW
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 * Defines the renderer for the assignment upgrade helper plugin.
19 *
20 * @package tool_assignmentupgrade
21 * @copyright 2012 NetSpot
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Renderer for the assignment upgrade helper plugin.
29 *
30 * @package tool_assignmentupgrade
31 * @copyright 2012 NetSpot
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class tool_assignmentupgrade_renderer extends plugin_renderer_base {
35
36 /**
37 * Render the index page.
38 * @param string $detected information about what sort of site was detected.
39 * @param array $actions list of actions to show on this page.
40 * @return string html to output.
41 */
42 public function index_page($detected, array $actions) {
43 $output = '';
44 $output .= $this->header();
45 $output .= $this->heading(get_string('pluginname', 'tool_assignmentupgrade'));
46 $output .= $this->box($detected);
47 $output .= html_writer::start_tag('ul');
48 foreach ($actions as $action) {
49 $output .= html_writer::tag('li',
50 html_writer::link($action->url, $action->name) . ' - ' .
51 $action->description);
52 }
53 $output .= html_writer::end_tag('ul');
54 $output .= $this->footer();
55 return $output;
56 }
57
58 /**
59 * Render a page that is just a simple message.
60 * @param string $message the message to display.
61 * @return string html to output.
62 */
63 public function simple_message_page($message) {
64 $output = '';
65 $output .= $this->header();
66 $output .= $this->heading($message);
67 $output .= $this->back_to_index();
68 $output .= $this->footer();
69 return $output;
70 }
71
72 /**
73 * Render the confirm batch operation page
74 * @param stdClass $data Submitted form data with list of assignments to upgrade
75 * @return string html to output.
76 */
77 public function confirm_batch_operation_page(stdClass $data) {
78 $output = '';
79 $output .= $this->header();
80
81 $output .= $this->heading(get_string('confirmbatchupgrade', 'tool_assignmentupgrade'));
82 $output .= $this->output->spacer(array(), true);
83
84 $output .= $this->container_start('tool_assignmentupgrade_confirmbatch');
85
86 $output .= $this->render(new tool_assignmentupgrade_batchoperationconfirm($data));
87 $output .= $this->container_end();
88
89 $output .= $this->back_to_index();
90 $output .= $this->footer();
91 return $output;
92 }
93
94 /**
95 * Render the confirm batch continue / cancel links
96 * @param tool_assignmentupgrade_batchoperationconfirm $confirm Wrapper class to determine the continue message and url
97 * @return string html to output.
98 */
99 public function render_tool_assignmentupgrade_batchoperationconfirm(tool_assignmentupgrade_batchoperationconfirm $confirm) {
100 $output = '';
101
102 if ($confirm->continueurl) {
103 $output .= $this->output->confirm($confirm->continuemessage, $confirm->continueurl, tool_assignmentupgrade_url('listnotupgraded'));
104 } else {
105 $output .= $this->output->box($confirm->continuemessage);
106 $output .= $this->output->continue_button(tool_assignmentupgrade_url('listnotupgraded'));
107 }
108 return $output;
109 }
110
111 /**
112 * Render the list of assignments that still need to be upgraded page.
113 * @param tool_assignmentupgrade_assignments_table $assignments of data about assignments.
114 * @param tool_assignmentupgrade_batchoperations_form $batchform Submitted form with list of assignments to upgrade
5b121144 115 * @param tool_assignmentupgrade_pagination_form $paginationform Form which contains the preferences for paginating the table
bbd0e548
DW
116 * @return string html to output.
117 */
5b121144 118 public function assignment_list_page(tool_assignmentupgrade_assignments_table $assignments, tool_assignmentupgrade_batchoperations_form $batchform, tool_assignmentupgrade_pagination_form $paginationform) {
bbd0e548
DW
119 $output = '';
120 $output .= $this->header();
121 $this->page->requires->js_init_call('M.tool_assignmentupgrade.init_upgrade_table', array());
122
123
124 $output .= $this->heading(get_string('notupgradedtitle', 'tool_assignmentupgrade'));
125 $output .= $this->box(get_string('notupgradedintro', 'tool_assignmentupgrade'));
126 $output .= $this->output->spacer(array(), true);
127
128 $output .= $this->container_start('tool_assignmentupgrade_upgradetable');
129
5b121144
DW
130 $output .= $this->container_start('tool_assignmentupgrade_paginationform');
131 $output .= $this->moodleform($paginationform);
132 $output .= $this->container_end();
133
bbd0e548
DW
134 $output .= $this->flexible_table($assignments, $assignments->get_rows_per_page(), true);
135 $output .= $this->container_end();
136
137 if ($assignments->anyupgradableassignments) {
138 $output .= $this->container_start('tool_assignmentupgrade_batchform');
139 $output .= $this->moodleform($batchform);
140 $output .= $this->container_end();
141 }
142
143 $output .= $this->back_to_index();
144 $output .= $this->footer();
145 return $output;
146 }
147
bbd0e548
DW
148 /**
149 * Render the result of an assignment conversion
150 * @param stdClass $assignmentsummary data about the assignment to upgrade.
151 * @param bool $success Set to true if the outcome of the conversion was a success
152 * @param string $log The log from the conversion
153 * @return string html to output.
154 */
155 public function convert_assignment_result($assignmentsummary, $success, $log) {
156 $output = '';
bbd0e548 157
5b121144
DW
158 $output .= $this->container_start('tool_assignmentupgrade_result');
159 $output .= $this->container(get_string('upgradeassignmentsummary', 'tool_assignmentupgrade', $assignmentsummary));
bbd0e548 160 if (!$success) {
5b121144 161 $output .= $this->container(get_string('conversionfailed', 'tool_assignmentupgrade', $log));
bbd0e548 162 } else {
5b121144
DW
163 $output .= $this->container(get_string('upgradeassignmentsuccess', 'tool_assignmentupgrade'));
164
165 $output .= $this->container(html_writer::link(new moodle_url('/course/view.php', array('id'=>$assignmentsummary->courseid)) ,get_string('viewcourse', 'tool_assignmentupgrade')));
bbd0e548 166 }
5b121144 167 $output .= $this->container_end();
bbd0e548 168
bbd0e548
DW
169 return $output;
170 }
171
172 /**
173 * Render the are-you-sure page to confirm a manual upgrade.
174 * @param stdClass $assignmentsummary data about the assignment to upgrade.
175 * @return string html to output.
176 */
177 public function convert_assignment_are_you_sure($assignmentsummary) {
178 $output = '';
179 $output .= $this->header();
180 $output .= $this->heading(get_string('areyousure', 'tool_assignmentupgrade'));
181
182 $params = array('id' => $assignmentsummary->id, 'confirmed' => 1, 'sesskey' => sesskey());
183 $output .= $this->confirm(get_string('areyousuremessage', 'tool_assignmentupgrade', $assignmentsummary),
184 new single_button(tool_assignmentupgrade_url('upgradesingle', $params), get_string('yes')),
185 tool_assignmentupgrade_url('listnotupgraded'));
186
187 $output .= $this->footer();
188 return $output;
189 }
190
191 /**
192 * Helper method dealing with the fact we can not just fetch the output of flexible_table
193 *
194 * @param flexible_table $table
195 * @param int $rowsperpage
196 * @param bool $displaylinks Show links in the table
197 * @return string HTML
198 */
199 protected function flexible_table(flexible_table $table, $rowsperpage, $displaylinks) {
200
201 $o = '';
202 ob_start();
203 $table->out($rowsperpage, $displaylinks);
204 $o = ob_get_contents();
205 ob_end_clean();
206
207 return $o;
208 }
209
210 /**
211 * Helper method dealing with the fact we can not just fetch the output of moodleforms
212 *
213 * @param moodleform $mform
214 * @return string HTML
215 */
216 protected function moodleform(moodleform $mform) {
217
218 $o = '';
219 ob_start();
220 $mform->display();
221 $o = ob_get_contents();
222 ob_end_clean();
223
224 return $o;
225 }
226
227
228 /**
229 * Render a link in a div, such as the 'Back to plugin main page' link.
230 * @param string|moodle_url $url the link URL.
231 * @param string $text the link text.
232 * @return string html to output.
233 */
234 public function end_of_page_link($url, $text) {
235 return html_writer::tag('div', html_writer::link($url, $text), array('class' => 'mdl-align'));
236 }
237
238 /**
239 * Output a link back to the plugin index page.
240 * @return string html to output.
241 */
242 public function back_to_index() {
243 return $this->end_of_page_link(tool_assignmentupgrade_url('index'), get_string('backtoindex', 'tool_assignmentupgrade'));
244 }
245}