Commit bbd0e548 authored by Damyon Wiese's avatar Damyon Wiese Committed by Dan Poltawski
Browse files

MDL-31270 mod_assign: introducing the assignment module

* Includes an assignment upgrade tool to convert from the old mod_assignment.
* Hides mod_assignment on new installs
parent b21a1d40
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Script to show all the assignments that have not been upgraded after the main upgrade.
*
* @package tool_assignmentupgrade
* @copyright 2012 NetSpot
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/../../../config.php');
require_once(dirname(__FILE__) . '/locallib.php');
require_once(dirname(__FILE__) . '/upgradableassignmentstable.php');
require_once(dirname(__FILE__) . '/upgradableassignmentsbatchform.php');
require_once($CFG->libdir . '/adminlib.php');
// admin_externalpage_setup calls require_login and checks moodle/site:config
admin_externalpage_setup('assignmentupgrade', '', array(), tool_assignmentupgrade_url('batchupgrade'));
$PAGE->navbar->add(get_string('batchupgrade', 'tool_assignmentupgrade'));
$renderer = $PAGE->get_renderer('tool_assignmentupgrade');
$confirm = required_param('confirm', PARAM_BOOL);
if (!$confirm) {
print_error('invalidrequest');
die();
}
$result = tool_assignmentupgrade_upgrade_multiple_assignments(optional_param('upgradeall', 0, PARAM_BOOL),
explode(',', optional_param('selected', '', PARAM_TEXT)));
echo $renderer->convert_multiple_assignments_result($result);
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This tool can upgrade old assignment activities to the new assignment activity type
*
* The upgrade can be done on any old assignment instance providing it is using one of the core
* assignment subtypes (online text, single upload, etc).
* The new assignment module was introduced in Moodle 2.3 and although it completely reproduces
* the features of the existing assignment type it wasn't designed to replace it entirely as there
* are many custom assignment types people use and it wouldn't be practical to try to convert them.
* Instead the existing assignment type will be left in core and people will be encouraged to
* use the new assignment type.
*
* This screen is the main entry-point to the plugin, it gives the admin a list
* of options available to them.
*
* @package tool_assignmentupgrade
* @copyright 2012 NetSpot
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/../../../config.php');
require_once(dirname(__FILE__) . '/locallib.php');
require_once($CFG->libdir . '/adminlib.php');
// admin_externalpage_setup calls require_login and checks moodle/site:config
admin_externalpage_setup('assignmentupgrade');
$renderer = $PAGE->get_renderer('tool_assignmentupgrade');
$actions = array();
$header = get_string('pluginname', 'tool_assignmentupgrade');
$actions[] = tool_assignmentupgrade_action::make('listnotupgraded');
echo $renderer->index_page($header, $actions);
\ No newline at end of file
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Strings for the assignment upgrade tool
*
* @package tool_assignmentupgrade
* @copyright 2012 NetSpot
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['areyousure'] = 'Are you sure?';
$string['areyousuremessage'] = 'Are you sure you want to upgrade the assignment "{$a->name}"?';
$string['assignmentid'] = 'Assignment ID';
$string['assignmentnotfound'] = 'Assignment could not be found (id={$a})';
$string['assignmenttype'] = 'Assignment type';
$string['backtoindex'] = 'Back to index';
$string['batchoperations'] = 'Batch operations';
$string['batchupgrade'] = 'Upgrade multiple assignments';
$string['confirmbatchupgrade'] = 'Confirm batch upgrade assignments';
$string['conversioncomplete'] = 'Assignment converted';
$string['conversionfailed'] = 'The assignment conversion was not successful. The log from the upgrade was: <br />{$a}';
$string['listnotupgraded'] = 'List assignments that have not been upgraded';
$string['listnotupgraded_desc'] = 'You can upgrade individual assignments from here';
$string['noassignmentstoupgrade'] = 'There are no assignments that require upgrading';
$string['notsupported'] = '';
$string['notupgradedintro'] = 'This page lists the assignments created with an older version of Moodle that have not been upgraded to the new assignment module in Moodle 2.3. Not all assignments can be upgraded - if they were created with a custom assignment subtype, then that subtype will need to be upgraded to the new assignment plugin format in order to complete the upgrade.';
$string['notupgradedtitle'] = 'Assignments not upgraded';
$string['pluginname'] = 'Assignment upgrade helper';
$string['select'] = 'Select';
$string['submissions'] = 'Submissions';
$string['supported'] = 'Upgrade';
$string['unknown'] = 'Unknown';
$string['upgradeassignmentsummary'] = 'Upgrade assignment: {$a->name} (Course: {$a->shortname})';
$string['upgradeassignmentsuccess'] = 'Result: Upgrade successful';
$string['upgradeassignmentfailed'] = 'Result: Upgrade failed. The log from the upgrade was: <br/><div class="tool_assignmentupgrade_upgradelog">{$a->log}</div>';
$string['upgradable'] = 'Upgradable';
$string['upgradeselected'] = 'Upgrade selected assignments';
$string['upgradeselectedcount'] = 'Upgrade {$a} selected assignments?';
$string['upgradeall'] = 'Upgrade all assignments';
$string['upgradeallconfirm'] = 'Upgrade all assignments?';
$string['upgradesingle'] = 'Upgrade single assignment';
$string['viewcourse'] = 'View the course with the converted assignment';
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Lib functions (cron) to automatically complete the assignment module upgrade if it was not done all at once during the main upgrade.
*
* @package tool_assignmentupgrade
* @copyright 2012 NetSpot
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* Standard cron function
*/
function tool_assignmentupgrade_cron() {
$settings = get_config('tool_assignmentupgrade');
if (empty($settings->cronenabled)) {
return;
}
mtrace('assignmentupgrade: tool_assignmentupgrade_cron() started at '. date('H:i:s'));
try {
tool_assignmentupgrade_process($settings);
} catch (Exception $e) {
mtrace('assignmentupgrade: tool_assignmentupgrade_cron() failed with an exception:');
mtrace($e->getMessage());
}
mtrace('assignmentupgrade: tool_assignmentupgrade_cron() finished at ' . date('H:i:s'));
}
/**
* This function does the cron process within the time range according to settings.
* This is not implemented yet
* @param stdClass $settings - not used
*/
function tool_assignmentupgrade_process($settings) {
global $CFG;
require_once(dirname(__FILE__) . '/locallib.php');
mtrace('assignmentupgrade: processing ...');
mtrace('assignmentupgrade: Done.');
return;
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Script to show all the assignments that have not been upgraded after the main upgrade.
*
* @package tool_assignmentupgrade
* @copyright 2012 NetSpot
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/../../../config.php');
require_once(dirname(__FILE__) . '/locallib.php');
require_once(dirname(__FILE__) . '/upgradableassignmentstable.php');
require_once(dirname(__FILE__) . '/upgradableassignmentsbatchform.php');
require_once($CFG->libdir . '/adminlib.php');
// admin_externalpage_setup calls require_login and checks moodle/site:config
admin_externalpage_setup('assignmentupgrade', '', array(), tool_assignmentupgrade_url('listnotupgraded'));
$PAGE->navbar->add(get_string('listnotupgraded', 'tool_assignmentupgrade'));
$renderer = $PAGE->get_renderer('tool_assignmentupgrade');
$perpage = get_user_preferences('tool_assignmentupgrade_perpage', 5);
$assignments = new tool_assignmentupgrade_assignments_table($perpage);
$batchform = new tool_assignmentupgrade_batchoperations_form();
$data = $batchform->get_data();
if ($data && $data->selectedassignments != '' || $data && isset($data->upgradeall)) {
echo $renderer->confirm_batch_operation_page($data);
} else {
echo $renderer->assignment_list_page($assignments, $batchform);
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Assignment upgrade tool library functions
*
* @package tool_assignmentupgrade
* @copyright 2012 NetSpot
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Get the URL of a script within this plugin.
* @param string $script the script name, without .php. E.g. 'index'
* @param array $params URL parameters (optional)
* @return moodle_url
*/
function tool_assignmentupgrade_url($script, $params = array()) {
return new moodle_url('/admin/tool/assignmentupgrade/' . $script . '.php', $params);
}
/**
* Class to encapsulate the continue / cancel for batch operations
*
* @package tool_assignmentupgrade
* @copyright 2012 NetSpot
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_assignmentupgrade_batchoperationconfirm implements renderable {
/** @var string $continuemessage The message to show above the continue cancel buttons */
public $continuemessage = '';
/** @var string $continueurl The url to load if the user clicks continue */
public $continueurl;
/**
* Constructor for this class
* @param stdClass $data - The data from the previous batch form
*/
function __construct($data) {
if (isset($data->upgradeselected)) {
$this->continuemessage = get_string('upgradeselectedcount', 'tool_assignmentupgrade', count(explode(',', $data->selectedassignments)));
$this->continueurl = new moodle_url('/admin/tool/assignmentupgrade/batchupgrade.php', array('upgradeselected'=>'1', 'confirm'=>'1', 'sesskey'=>sesskey(), 'selected'=>$data->selectedassignments));
} else if (isset($data->upgradeall)) {
if (!tool_assignmentupgrade_any_upgradable_assignments()) {
$this->continuemessage = get_string('noassignmentstoupgrade', 'tool_assignmentupgrade');
$this->continueurl = '';
} else {
$this->continuemessage = get_string('upgradeallconfirm', 'tool_assignmentupgrade');
$this->continueurl = new moodle_url('/admin/tool/assignmentupgrade/batchupgrade.php', array('upgradeall'=>'1', 'confirm'=>'1', 'sesskey'=>sesskey()));
}
}
}
}
/**
* Class to encapsulate one of the functionalities that this plugin offers.
*
* @package tool_assignmentupgrade
* @copyright 2012 NetSpot
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_assignmentupgrade_action {
/** @var string the name of this action. */
public $name;
/** @var moodle_url the URL to launch this action. */
public $url;
/** @var string a description of this aciton. */
public $description;
/**
* Constructor to set the fields.
*
* In order to create a new tool_assignmentupgrade_action instance you must use the tool_assignmentupgrade_action::make
* method.
*
* @param string $name the name of this action.
* @param moodle_url $url the URL to launch this action.
* @param string $description a description of this aciton.
*/
protected function __construct($name, moodle_url $url, $description) {
$this->name = $name;
$this->url = $url;
$this->description = $description;
}
/**
* Make an action with standard values.
* @param string $shortname internal name of the action. Used to get strings and build a URL.
* @param array $params any URL params required.
* @return tool_assignmentupgrade_action
*/
public static function make($shortname, $params = array()) {
return new self(
get_string($shortname, 'tool_assignmentupgrade'),
tool_assignmentupgrade_url($shortname, $params),
get_string($shortname . '_desc', 'tool_assignmentupgrade'));
}
}
/**
* Determine if there are any assignments that can be upgraded
* @return boolean - Are there any assignments that can be upgraded
*/
function tool_assignmentupgrade_any_upgradable_assignments() {
global $DB, $CFG;
require_once($CFG->dirroot . '/mod/assign/locallib.php');
// first find all the unique assignment types
$types = $DB->get_records_sql('SELECT plugin AS assignmenttype, value AS version FROM {config_plugins} WHERE name = ? AND plugin LIKE ?', array('version', 'assignment_%'));
$upgradabletypes = array();
foreach ($types as $assignment) {
$shorttype = substr($assignment->assignmenttype, strlen('assignment_'));
if (assign::can_upgrade_assignment($shorttype, $assignment->version)) {
$upgradabletypes[] = $shorttype;
}
}
$paramlist = '?';
foreach ($upgradabletypes as $index => $upgradabletype) {
if ($index > 0) {
$paramlist .= ', ?';
}
}
$record = $DB->get_record_sql('SELECT COUNT(id) as count from {assignment} where assignmenttype in (' . $paramlist . ')', $upgradabletypes);
return $record->count > 0;
}
/**
* Load a list of all the assignmentids that can be upgraded
* @return array of assignment ids
*/
function tool_assignmentupgrade_load_all_upgradable_assignmentids() {
global $DB, $CFG;
require_once($CFG->dirroot . '/mod/assign/locallib.php');
// first find all the unique assignment types
$types = $DB->get_records_sql('SELECT plugin AS assignmenttype, value AS version FROM {config_plugins} WHERE name = ? AND plugin LIKE ?', array('version', 'assignment_%'));
$upgradabletypes = array();
foreach ($types as $assignment) {
$shorttype = substr($assignment->assignmenttype, strlen('assignment_'));
if (assign::can_upgrade_assignment($shorttype, $assignment->version)) {
$upgradabletypes[] = $shorttype;
}
}
$paramlist = '?';
foreach ($upgradabletypes as $index => $upgradabletype) {
if ($index > 0) {
$paramlist .= ', ?';
}
}
$records = $DB->get_records_sql('SELECT id from {assignment} where assignmenttype in (' . $paramlist . ')', $upgradabletypes);
$ids = array();
foreach ($records as $record) {
$ids[] = $record->id;
}
return $ids;
}
/**
* Convert a list of assignments from the old format to the new one.
* @param bool $upgradeall - Upgrade all possible assignments
* @param array $assignmentids An array of assignment ids to upgrade
* @return array of $entry['assignmentsummary' => (result from tool_assignmentupgrade_get_assignment)
* $entry['success'] => boolean
* $entry['log'] => string - upgrade log
*/
function tool_assignmentupgrade_upgrade_multiple_assignments($upgradeall, $assignmentids) {
global $CFG;
require_once($CFG->dirroot . '/mod/assign/locallib.php');
require_once($CFG->dirroot . '/mod/assign/upgradelib.php');
$upgrades = array();
if ($upgradeall) {
$assignmentids = tool_assignmentupgrade_load_all_upgradable_assignmentids();
}
$assignment_upgrader = new assign_upgrade_manager();
foreach ($assignmentids as $assignmentid) {
$info = tool_assignmentupgrade_get_assignment($assignmentid);
if ($info) {
$log = '';
$success = $assignment_upgrader->upgrade_assignment($assignmentid, $log);
} else {
$success = false;
$log = get_string('assignmentnotfound', 'tool_assignmentupgrade', $assignmentid);
$info = new stdClass();
$info->name = get_string('unknown', 'tool_assignmentupgrade');
$info->shortname = get_string('unknown', 'tool_assignmentupgrade');
}
$upgrades[] = array('assignmentsummary'=>$info, 'success'=>$success, 'log'=>$log);
}
return $upgrades;
}
/**
* Convert a single assignment from the old format to the new one.
* @param stdClass $assignmentinfo An object containing information about this class
* @param string $log This gets appended to with the details of the conversion process
* @return boolean This is the overall result (true/false)
*/
function tool_assignmentupgrade_upgrade_assignment($assignmentinfo, &$log) {
global $CFG;
require_once($CFG->dirroot . '/mod/assign/locallib.php');
require_once($CFG->dirroot . '/mod/assign/upgradelib.php');
$assignment_upgrader = new assign_upgrade_manager();
return $assignment_upgrader->upgrade_assignment($assignmentinfo->id, $log);
}
/**
* Get the information about a assignment to be upgraded.
* @param int $assignmentid the assignment id.
* @return stdClass the information about that assignment.
*/
function tool_assignmentupgrade_get_assignment($assignmentid) {
global $DB;
return $DB->get_record_sql("
SELECT a.id, a.name, c.shortname, c.id AS courseid
FROM {assignment} a
JOIN {course} c ON c.id = a.course
WHERE a.id = ?", array($assignmentid));
}
M.tool_assignmentupgrade = {
init_upgrade_table: function(Y) {
Y.use('node', function(Y) {
checkboxes = Y.all('td.c0 input');
checkboxes.each(function(node) {
node.on('change', function(e) {
rowelement = e.currentTarget.get('parentNode').get('parentNode');
if (e.currentTarget.get('checked')) {
rowelement.setAttribute('class', 'selectedrow');
} else {
rowelement.setAttribute('class', 'unselectedrow');
}
});
rowelement = node.get('parentNode').get('parentNode');
if (node.get('checked')) {
rowelement.setAttribute('class', 'selectedrow');
} else {
rowelement.setAttribute('class', 'unselectedrow');
}
});
});
var selectall = Y.one('th.c0 input');
selectall.on('change', function(e) {
if (e.currentTarget.get('checked')) {
checkboxes = Y.all('td.c0 input');
checkboxes.each(function(node) {
rowelement = node.get('parentNode').get('parentNode');
node.set('checked', true);
rowelement.setAttribute('class', 'selectedrow');
});
} else {
checkboxes = Y.all('td.c0 input');
checkboxes.each(function(node) {
rowelement = node.get('parentNode').get('parentNode');
node.set('checked', false);
rowelement.setAttribute('class', 'unselectedrow');
});
}
});
var batchform = Y.one('.tool_assignmentupgrade_batchform form');
batchform.on('submit', function(e) {
checkboxes = Y.all('td.c0 input');
var selectedassignments = [];
checkboxes.each(function(node) {
if (node.get('checked')) {
selectedassignments[selectedassignments.length] = node.get('value');
}
});
operation = Y.one('#id_operation');
assignmentsinput = Y.one('input.selectedassignments');
assignmentsinput.set('value', selectedassignments.join(','));
if (selectedassignments.length == 0) {
alert(M.str.assign.noassignmentsselected);
e.preventDefault();
}
});
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines the renderer for the assignment upgrade helper plugin.
*
* @package tool_assignmentupgrade
* @copyright 2012 NetSpot
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/