MDL-32895: Improvements to assignment upgrade tool for large numbers of assignments.
[moodle.git] / admin / tool / assignmentupgrade / locallib.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 * Assignment upgrade tool library functions
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 * Get the URL of a script within this plugin.
29 * @param string $script the script name, without .php. E.g. 'index'
30 * @param array $params URL parameters (optional)
31 * @return moodle_url
32 */
33function tool_assignmentupgrade_url($script, $params = array()) {
34 return new moodle_url('/admin/tool/assignmentupgrade/' . $script . '.php', $params);
35}
36
37/**
38 * Class to encapsulate the continue / cancel for batch operations
39 *
40 * @package tool_assignmentupgrade
41 * @copyright 2012 NetSpot
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44class tool_assignmentupgrade_batchoperationconfirm implements renderable {
45 /** @var string $continuemessage The message to show above the continue cancel buttons */
46 public $continuemessage = '';
47 /** @var string $continueurl The url to load if the user clicks continue */
48 public $continueurl;
49
50 /**
51 * Constructor for this class
52 * @param stdClass $data - The data from the previous batch form
53 */
54 function __construct($data) {
55 if (isset($data->upgradeselected)) {
56 $this->continuemessage = get_string('upgradeselectedcount', 'tool_assignmentupgrade', count(explode(',', $data->selectedassignments)));
57 $this->continueurl = new moodle_url('/admin/tool/assignmentupgrade/batchupgrade.php', array('upgradeselected'=>'1', 'confirm'=>'1', 'sesskey'=>sesskey(), 'selected'=>$data->selectedassignments));
58 } else if (isset($data->upgradeall)) {
59 if (!tool_assignmentupgrade_any_upgradable_assignments()) {
60 $this->continuemessage = get_string('noassignmentstoupgrade', 'tool_assignmentupgrade');
61 $this->continueurl = '';
62 } else {
63 $this->continuemessage = get_string('upgradeallconfirm', 'tool_assignmentupgrade');
64 $this->continueurl = new moodle_url('/admin/tool/assignmentupgrade/batchupgrade.php', array('upgradeall'=>'1', 'confirm'=>'1', 'sesskey'=>sesskey()));
65 }
66 }
67 }
68}
69
70
71/**
72 * Class to encapsulate one of the functionalities that this plugin offers.
73 *
74 * @package tool_assignmentupgrade
75 * @copyright 2012 NetSpot
76 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
77 */
78class tool_assignmentupgrade_action {
79 /** @var string the name of this action. */
80 public $name;
81 /** @var moodle_url the URL to launch this action. */
82 public $url;
83 /** @var string a description of this aciton. */
84 public $description;
85
86 /**
87 * Constructor to set the fields.
88 *
89 * In order to create a new tool_assignmentupgrade_action instance you must use the tool_assignmentupgrade_action::make
90 * method.
91 *
92 * @param string $name the name of this action.
93 * @param moodle_url $url the URL to launch this action.
94 * @param string $description a description of this aciton.
95 */
96 protected function __construct($name, moodle_url $url, $description) {
97 $this->name = $name;
98 $this->url = $url;
99 $this->description = $description;
100 }
101
102 /**
103 * Make an action with standard values.
104 * @param string $shortname internal name of the action. Used to get strings and build a URL.
105 * @param array $params any URL params required.
106 * @return tool_assignmentupgrade_action
107 */
108 public static function make($shortname, $params = array()) {
109 return new self(
110 get_string($shortname, 'tool_assignmentupgrade'),
111 tool_assignmentupgrade_url($shortname, $params),
112 get_string($shortname . '_desc', 'tool_assignmentupgrade'));
113 }
114}
115
116/**
117 * Determine if there are any assignments that can be upgraded
118 * @return boolean - Are there any assignments that can be upgraded
119 */
120function tool_assignmentupgrade_any_upgradable_assignments() {
121 global $DB, $CFG;
122 require_once($CFG->dirroot . '/mod/assign/locallib.php');
123 // first find all the unique assignment types
124 $types = $DB->get_records_sql('SELECT plugin AS assignmenttype, value AS version FROM {config_plugins} WHERE name = ? AND plugin LIKE ?', array('version', 'assignment_%'));
125
126 $upgradabletypes = array();
127
128 foreach ($types as $assignment) {
129 $shorttype = substr($assignment->assignmenttype, strlen('assignment_'));
130 if (assign::can_upgrade_assignment($shorttype, $assignment->version)) {
131 $upgradabletypes[] = $shorttype;
132 }
133 }
5d81b754 134 list($sql, $params) = $DB->get_in_or_equal($upgradabletypes);
bbd0e548 135
5d81b754 136 $count = $DB->count_records_sql('SELECT COUNT(id) from {assignment} where assignmenttype ' . $sql, $params);
bbd0e548 137
5d81b754 138 return $count > 0;
bbd0e548
DW
139}
140
141/**
142 * Load a list of all the assignmentids that can be upgraded
143 * @return array of assignment ids
144 */
145function tool_assignmentupgrade_load_all_upgradable_assignmentids() {
146 global $DB, $CFG;
147 require_once($CFG->dirroot . '/mod/assign/locallib.php');
148 // first find all the unique assignment types
149 $types = $DB->get_records_sql('SELECT plugin AS assignmenttype, value AS version FROM {config_plugins} WHERE name = ? AND plugin LIKE ?', array('version', 'assignment_%'));
150
151 $upgradabletypes = array();
152
153 foreach ($types as $assignment) {
154 $shorttype = substr($assignment->assignmenttype, strlen('assignment_'));
155 if (assign::can_upgrade_assignment($shorttype, $assignment->version)) {
156 $upgradabletypes[] = $shorttype;
157 }
158 }
bbd0e548 159
5d81b754
DW
160 list($sql, $params) = $DB->get_in_or_equal($upgradabletypes);
161
162 $records = $DB->get_records_sql('SELECT id from {assignment} where assignmenttype ' . $sql, $params);
bbd0e548
DW
163 $ids = array();
164 foreach ($records as $record) {
165 $ids[] = $record->id;
166 }
167
168 return $ids;
169}
170
171
172/**
5b121144
DW
173 * Upgrade a single assignment. This is used by both upgrade single and upgrade batch
174 *
175 * @param int $assignmentid - The assignment id to upgrade
176 * @return array(string, boolean, string) -
177 * The array contains
178 * - the assignment summary (returned by tool_assignmentupgrade_get_assignment)
179 * - success
180 * - the upgrade log
bbd0e548 181 */
5b121144 182function tool_assignmentupgrade_upgrade_assignment($assignmentid) {
bbd0e548 183 global $CFG;
bbd0e548 184 require_once($CFG->dirroot . '/mod/assign/upgradelib.php');
bbd0e548
DW
185
186 $assignment_upgrader = new assign_upgrade_manager();
5b121144
DW
187 $info = tool_assignmentupgrade_get_assignment($assignmentid);
188 if ($info) {
189 $log = '';
190 $success = $assignment_upgrader->upgrade_assignment($assignmentid, $log);
191 } else {
192 $success = false;
193 $log = get_string('assignmentnotfound', 'tool_assignmentupgrade', $assignmentid);
194 $info = new stdClass();
195 $info->name = get_string('unknown', 'tool_assignmentupgrade');
196 $info->shortname = get_string('unknown', 'tool_assignmentupgrade');
bbd0e548 197 }
bbd0e548 198
5b121144 199 return array($info, $success, $log);
bbd0e548
DW
200}
201
202/**
203 * Get the information about a assignment to be upgraded.
204 * @param int $assignmentid the assignment id.
205 * @return stdClass the information about that assignment.
206 */
207function tool_assignmentupgrade_get_assignment($assignmentid) {
208 global $DB;
209 return $DB->get_record_sql("
210 SELECT a.id, a.name, c.shortname, c.id AS courseid
211 FROM {assignment} a
212 JOIN {course} c ON c.id = a.course
213 WHERE a.id = ?", array($assignmentid));
214}
215