MDL-32774: Require sesskey for all actions in the assignment upgrade tool
[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
73
74 $where = '1 = 1';
75 $where .= ' GROUP BY a.id, a.name, a.assignmenttype, c.shortname, c.id ';
76
77 $this->set_sql($fields, $from, $where, array());
78 $this->set_count_sql('SELECT COUNT(*) FROM ' . $from, array());
79
80 $columns = array();
81 $headers = array();
82
83 $columns[] = 'select';
84 $headers[] = get_string('select', 'tool_assignmentupgrade') . '<div class="selectall"><input type="checkbox" name="selectall" title="' . get_string('selectall') . '"/></div>';
85 $columns[] = 'upgradable';
86 $headers[] = get_string('upgradable', 'tool_assignmentupgrade');
87 $columns[] = 'id';
88 $headers[] = get_string('assignmentid', 'tool_assignmentupgrade');
89 $columns[] = 'courseshortname';
90 $headers[] = get_string('course');
91 $columns[] = 'name';
92 $headers[] = get_string('name');
93 $columns[] = 'type';
94 $headers[] = get_string('assignmenttype', 'tool_assignmentupgrade');
95 $columns[] = 'submissioncount';
96 $headers[] = get_string('submissions', 'tool_assignmentupgrade');
97
98 // set the columns
99 $this->define_columns($columns);
100 $this->define_headers($headers);
101 $this->no_sorting('upgradable');
102 $this->no_sorting('select');
103 }
104
105 /**
106 * Return the number of rows to display on a single page
107 *
108 * @return int The number of rows per page
109 */
110 function get_rows_per_page() {
111 return $this->perpage;
112 }
113
114 /**
115 * Format a link to the assignment instance
116 *
117 * @param stdClass $row
118 * @return string
119 */
120 function col_name(stdClass $row) {
121 return html_writer::link(new moodle_url('/mod/assignment/view.php',
122 array('a' => $row->id)), $row->name);
123 }
124
125
126 /**
127 * Format a link to the upgrade single tool
128 *
129 * @param stdClass $row (contains cached result from previous upgradable check)
130 * @return string
131 */
132 function col_upgradable(stdClass $row) {
133 if ($row->upgradable) {
134 return html_writer::link(new moodle_url('/admin/tool/assignmentupgrade/upgradesingleconfirm.php',
ee225061 135 array('id' => $row->id, 'sesskey' => sesskey())), get_string('supported', 'tool_assignmentupgrade'));
bbd0e548
DW
136 } else {
137 return get_string('notsupported', 'tool_assignmentupgrade');
138 }
139 }
140
141 /**
142 * Insert a checkbox for selecting the current row for batch operations
143 *
144 * @param stdClass $row
145 * @return string
146 */
147 function col_select(stdClass $row) {
148 global $CFG;
149 $version = get_config('assignment_' . $row->type, 'version');
150 require_once($CFG->dirroot . '/mod/assign/locallib.php');
151 if (assign::can_upgrade_assignment($row->type, $version)) {
152 $row->upgradable = true;
153 return '<input type="checkbox" name="selectedassignment" value="' . $row->id . '"/>';
154 }
155 $row->upgradable = false;
156 return '';
157 }
158
159 /**
160 * Override the table show_hide_link to not show for select column
161 *
162 * @param string $column the column name, index into various names.
163 * @param int $index numerical index of the column.
164 * @return string HTML fragment.
165 */
166 protected function show_hide_link($column, $index) {
167 if ($index > 0) {
168 return parent::show_hide_link($column, $index);
169 }
170 return '';
171 }
172}