MDL-32895: Improvements to assignment upgrade tool for large numbers of assignments.
[moodle.git] / admin / tool / assignmentupgrade / upgradableassignmentstable.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 * This file contains the definition for the grading table which subclassses easy_table
19 *
20 * @package tool_assignmentupgrade
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27require_once($CFG->libdir.'/tablelib.php');
28require_once($CFG->libdir.'/gradelib.php');
29require_once($CFG->dirroot.'/mod/assign/locallib.php');
30
31/**
32 * Extends table_sql to provide a table of assignment submissions
33 *
34 * @package tool_assignmentupgrade
35 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class tool_assignmentupgrade_assignments_table extends table_sql implements renderable {
39 /** @var int $perpage */
40 private $perpage = 10;
41 /** @var int $rownum (global index of current row in table) */
42 private $rownum = -1;
43 /** @var renderer_base for getting output */
44 private $output = null;
45 /** @var boolean anyupgradableassignments - True if there is one or more assignments that can upgraded */
46 public $anyupgradableassignments = false;
47
48 /**
49 * This table loads a list of the old assignment instances and tests them to see if they can be upgraded
50 *
51 * @param int $perpage How many per page
52 * @param int $rowoffset The starting row for pagination
53 */
54 function __construct($perpage, $rowoffset=0) {
55 global $PAGE;
56 parent::__construct('tool_assignmentupgrade_assignments');
57 $this->perpage = $perpage;
58 $this->output = $PAGE->get_renderer('tool_assignmentupgrade');
59
60 $this->define_baseurl(new moodle_url('/admin/tool/assignmentupgrade/listnotupgraded.php'));
61
62 $this->anyupgradableassignments = tool_assignmentupgrade_any_upgradable_assignments();
63
64 // do some business - then set the sql
65 if ($rowoffset) {
66 $this->rownum = $rowoffset - 1;
67 }
68
69 $fields = 'a.id as id, a.name as name, a.assignmenttype as type, c.shortname as courseshortname, c.id as courseid, COUNT(s.id) as submissioncount';
70 $from = '{assignment} a JOIN {course} c ON a.course = c.id ' .
71 ' LEFT JOIN {assignment_submissions} s ON a.id = s.assignment';
72
bbd0e548
DW
73 $where = '1 = 1';
74 $where .= ' GROUP BY a.id, a.name, a.assignmenttype, c.shortname, c.id ';
75
76 $this->set_sql($fields, $from, $where, array());
5b121144 77 $this->set_count_sql('SELECT COUNT(*) FROM {assignment} a JOIN {course} c ON a.course = c.id', array());
bbd0e548
DW
78
79 $columns = array();
80 $headers = array();
81
82 $columns[] = 'select';
83 $headers[] = get_string('select', 'tool_assignmentupgrade') . '<div class="selectall"><input type="checkbox" name="selectall" title="' . get_string('selectall') . '"/></div>';
84 $columns[] = 'upgradable';
85 $headers[] = get_string('upgradable', 'tool_assignmentupgrade');
86 $columns[] = 'id';
87 $headers[] = get_string('assignmentid', 'tool_assignmentupgrade');
88 $columns[] = 'courseshortname';
89 $headers[] = get_string('course');
90 $columns[] = 'name';
91 $headers[] = get_string('name');
92 $columns[] = 'type';
93 $headers[] = get_string('assignmenttype', 'tool_assignmentupgrade');
94 $columns[] = 'submissioncount';
95 $headers[] = get_string('submissions', 'tool_assignmentupgrade');
96
97 // set the columns
98 $this->define_columns($columns);
99 $this->define_headers($headers);
100 $this->no_sorting('upgradable');
101 $this->no_sorting('select');
102 }
103
104 /**
105 * Return the number of rows to display on a single page
106 *
107 * @return int The number of rows per page
108 */
109 function get_rows_per_page() {
110 return $this->perpage;
111 }
112
113 /**
114 * Format a link to the assignment instance
115 *
116 * @param stdClass $row
117 * @return string
118 */
119 function col_name(stdClass $row) {
120 return html_writer::link(new moodle_url('/mod/assignment/view.php',
121 array('a' => $row->id)), $row->name);
122 }
123
124
125 /**
126 * Format a link to the upgrade single tool
127 *
128 * @param stdClass $row (contains cached result from previous upgradable check)
129 * @return string
130 */
131 function col_upgradable(stdClass $row) {
132 if ($row->upgradable) {
133 return html_writer::link(new moodle_url('/admin/tool/assignmentupgrade/upgradesingleconfirm.php',
ee225061 134 array('id' => $row->id, 'sesskey' => sesskey())), get_string('supported', 'tool_assignmentupgrade'));
bbd0e548
DW
135 } else {
136 return get_string('notsupported', 'tool_assignmentupgrade');
137 }
138 }
139
140 /**
141 * Insert a checkbox for selecting the current row for batch operations
142 *
143 * @param stdClass $row
144 * @return string
145 */
146 function col_select(stdClass $row) {
147 global $CFG;
148 $version = get_config('assignment_' . $row->type, 'version');
149 require_once($CFG->dirroot . '/mod/assign/locallib.php');
150 if (assign::can_upgrade_assignment($row->type, $version)) {
151 $row->upgradable = true;
152 return '<input type="checkbox" name="selectedassignment" value="' . $row->id . '"/>';
153 }
154 $row->upgradable = false;
155 return '';
156 }
157
158 /**
159 * Override the table show_hide_link to not show for select column
160 *
161 * @param string $column the column name, index into various names.
162 * @param int $index numerical index of the column.
163 * @return string HTML fragment.
164 */
165 protected function show_hide_link($column, $index) {
166 if ($index > 0) {
167 return parent::show_hide_link($column, $index);
168 }
169 return '';
170 }
171}