PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/tool/assignmentupgrade/locallib.php

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