diff --git a/grade/import/direct/classes/import_form.php b/grade/import/direct/classes/import_form.php new file mode 100644 index 00000000000..bab48caae0b --- /dev/null +++ b/grade/import/direct/classes/import_form.php @@ -0,0 +1,71 @@ +. + +require_once($CFG->libdir.'/formslib.php'); + +if (!defined('MOODLE_INTERNAL')) { + die('Direct access to this script is forbidden.'); // It must be included from a Moodle page. +} + +/** + * Form for copying and pasting from a spreadsheet. + * + * @package gradeimport_direct + * @copyright 2014 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class gradeimport_direct_import_form extends moodleform { + + /** + * Definition method. + */ + public function definition() { + global $COURSE; + + $mform = $this->_form; + + if (isset($this->_customdata)) { // Hardcoding plugin names here is hacky. + $features = $this->_customdata; + } else { + $features = array(); + } + + // Course id needs to be passed for auth purposes. + $mform->addElement('hidden', 'id', optional_param('id', 0, PARAM_INT)); + $mform->setType('id', PARAM_INT); + + $mform->addElement('header', 'general', get_string('pluginname', 'gradeimport_direct')); + // Data upload from copy/paste. + $mform->addElement('textarea', 'userdata', 'Data', array('rows' => 10, 'class' => 'gradeimport_data_area')); + $mform->addRule('userdata', null, 'required'); + $mform->setType('userdata', PARAM_RAW); + + $encodings = core_text::get_encodings(); + $mform->addElement('select', 'encoding', get_string('encoding', 'grades'), $encodings); + + if (!empty($features['verbosescales'])) { + $options = array(1 => get_string('yes'), 0 => get_string('no')); + $mform->addElement('select', 'verbosescales', get_string('verbosescales', 'grades'), $options); + } + + $options = array('10' => 10, '20' => 20, '100' => 100, '1000' => 1000, '100000' => 100000); + $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'grades'), $options); + $mform->setType('previewrows', PARAM_INT); + $mform->addElement('hidden', 'groupid', groups_get_course_group($COURSE)); + $mform->setType('groupid', PARAM_INT); + $this->add_action_buttons(false, get_string('uploadgrades', 'grades')); + } +} diff --git a/grade/import/direct/classes/mapping_form.php b/grade/import/direct/classes/mapping_form.php new file mode 100644 index 00000000000..5dc7c56110c --- /dev/null +++ b/grade/import/direct/classes/mapping_form.php @@ -0,0 +1,111 @@ +. + +require_once($CFG->libdir.'/formslib.php'); +require_once($CFG->libdir.'/gradelib.php'); + +if (!defined('MOODLE_INTERNAL')) { + die('Direct access to this script is forbidden.'); // It must be included from a Moodle page. +} + +/** + * Form for mapping columns to the fields in the table. + * + * @package gradeimport_direct + * @copyright 2014 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class gradeimport_direct_mapping_form extends moodleform { + + /** + * Definition method. + */ + public function definition() { + global $CFG, $COURSE; + $mform = $this->_form; + + // This is an array of headers. + $header = $this->_customdata['header']; + // Course id. + + $mform->addElement('header', 'general', get_string('identifier', 'grades')); + $mapfromoptions = array(); + + if ($header) { + foreach ($header as $i => $h) { + $mapfromoptions[$i] = s($h); + } + } + $mform->addElement('select', 'mapfrom', get_string('mapfrom', 'grades'), $mapfromoptions); + + $maptooptions = array( + 'userid' => get_string('userid', 'grades'), + 'username' => get_string('username'), + 'useridnumber' => get_string('idnumber'), + 'useremail' => get_string('email'), + '0' => get_string('ignore', 'grades') + ); + $mform->addElement('select', 'mapto', get_string('mapto', 'grades'), $maptooptions); + + $mform->addElement('header', 'general', get_string('mappings', 'grades')); + + // Add a feedback option. + $feedbacks = array(); + if ($gradeitems = $this->_customdata['gradeitems']) { + foreach ($gradeitems as $itemid => $itemname) { + $feedbacks['feedback_'.$itemid] = get_string('feedbackforgradeitems', 'grades', $itemname); + } + } + + if ($header) { + $i = 0; + foreach ($header as $h) { + $h = trim($h); + // This is what each header maps to. + $headermapsto = array( + get_string('others', 'grades') => array( + '0' => get_string('ignore', 'grades'), + 'new' => get_string('newitem', 'grades') + ), + get_string('gradeitems', 'grades') => $gradeitems, + get_string('feedbacks', 'grades') => $feedbacks + ); + $mform->addElement('selectgroups', 'mapping_'.$i, s($h), $headermapsto); + $i++; + } + } + // Course id needs to be passed for auth purposes. + $mform->addElement('hidden', 'map', 1); + $mform->setType('map', PARAM_INT); + $mform->setConstant('map', 1); + $mform->addElement('hidden', 'id', $this->_customdata['id']); + $mform->setType('id', PARAM_INT); + $mform->setConstant('id', $this->_customdata['id']); + $mform->addElement('hidden', 'iid', $this->_customdata['iid']); + $mform->setType('iid', PARAM_INT); + $mform->setConstant('iid', $this->_customdata['iid']); + $mform->addElement('hidden', 'importcode', $this->_customdata['importcode']); + $mform->setType('importcode', PARAM_FILE); + $mform->setConstant('importcode', $this->_customdata['importcode']); + $mform->addElement('hidden', 'verbosescales', 1); + $mform->setType('verbosescales', PARAM_INT); + $mform->setConstant('verbosescales', $this->_customdata['importcode']); + $mform->addElement('hidden', 'groupid', groups_get_course_group($COURSE)); + $mform->setType('groupid', PARAM_INT); + $mform->setConstant('groupid', groups_get_course_group($COURSE)); + $this->add_action_buttons(false, get_string('uploadgrades', 'grades')); + } +} diff --git a/grade/import/direct/db/access.php b/grade/import/direct/db/access.php new file mode 100644 index 00000000000..c45f006bb20 --- /dev/null +++ b/grade/import/direct/db/access.php @@ -0,0 +1,37 @@ +. + +/** + * Capabilities gradeimport plugin. + * + * @package gradeimport_direct + * @copyright 2014 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$capabilities = array( + + 'gradeimport/direct:view' => array( + 'captype' => 'write', + 'contextlevel' => CONTEXT_COURSE, + 'archetypes' => array( + 'editingteacher' => CAP_ALLOW, + 'manager' => CAP_ALLOW + ) + ) +); \ No newline at end of file diff --git a/grade/import/direct/index.php b/grade/import/direct/index.php new file mode 100644 index 00000000000..1a9fc67c142 --- /dev/null +++ b/grade/import/direct/index.php @@ -0,0 +1,124 @@ +. + +require_once(__DIR__ . "../../../../config.php"); +require_once($CFG->libdir.'/gradelib.php'); +require_once($CFG->dirroot.'/grade/lib.php'); +require_once($CFG->dirroot.'/grade/import/lib.php'); +require_once($CFG->libdir . '/csvlib.class.php'); + +$id = required_param('id', PARAM_INT); // Course id. +$verbosescales = optional_param('verbosescales', 1, PARAM_BOOL); +$iid = optional_param('iid', null, PARAM_INT); +$importcode = optional_param('importcode', '', PARAM_FILE); + +$url = new moodle_url('/grade/import/direct/index.php', array('id' => $id)); + +if ($verbosescales !== 1) { + $url->param('verbosescales', $verbosescales); +} + +$PAGE->set_url($url); + +if (!$course = $DB->get_record('course', array('id' => $id))) { + print_error('nocourseid'); +} + +require_login($course); +$context = context_course::instance($id); +require_capability('moodle/grade:import', $context); +require_capability('gradeimport/direct:view', $context); + +$separatemode = (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and + !has_capability('moodle/site:accessallgroups', $context)); +$currentgroup = groups_get_course_group($course); + +print_grade_page_head($course->id, 'import', 'direct', get_string('pluginname', 'gradeimport_direct'), false, false, true, + 'userdata', 'gradeimport_direct'); + +$renderer = $PAGE->get_renderer('gradeimport_csv'); + +// Get the grade items to be matched with the import mapping columns. +$gradeitems = gradeimport_csv_load_data::fetch_grade_items($course->id); + +// If the csv file hasn't been imported yet then look for a form submission or +// show the initial submission form. +if (!$iid) { + + // Set up the import form. + $mform = new gradeimport_direct_import_form(null, array('includeseparator' => true, 'verbosescales' => true, 'acceptedtypes' => + array('.csv', '.txt'))); + + // If the import form has been submitted. + if ($formdata = $mform->get_data()) { + $text = $formdata->userdata; + $csvimport = new gradeimport_csv_load_data(); + $csvimport->load_csv_content($text, $formdata->encoding, 'tab', $formdata->previewrows); + $csvimporterror = $csvimport->get_error(); + if (!empty($csvimporterror)) { + echo $renderer->errors($csvimport->get_error()); + echo $OUTPUT->footer(); + die(); + } + $iid = $csvimport->get_iid(); + echo $renderer->import_preview_page($csvimport->get_headers(), $csvimport->get_previewdata()); + } else { + // Display the standard upload file form. + echo $renderer->standard_upload_file_form($course, $mform); + echo $OUTPUT->footer(); + die(); + } +} + +// Data has already been submitted so we can use the $iid to retrieve it. +$csvimport = new csv_import_reader($iid, 'grade'); +$header = $csvimport->get_columns(); +// Get a new import code for updating to the grade book. +if (empty($importcode)) { + $importcode = get_new_importcode(); +} + +$mappingformdata = array( + 'gradeitems' => $gradeitems, + 'header' => $header, + 'iid' => $iid, + 'id' => $id, + 'importcode' => $importcode, + 'verbosescales' => $verbosescales +); +// We create a form to handle mapping data from the file to the database. +$mform2 = new gradeimport_direct_mapping_form(null, $mappingformdata); + +// Here, if we have data, we process the fields and enter the information into the database. +if ($formdata = $mform2->get_data()) { + $gradeimport = new gradeimport_csv_load_data(); + $status = $gradeimport->prepare_import_grade_data($header, $formdata, $csvimport, $course->id, $separatemode, $currentgroup, + $verbosescales); + + // At this stage if things are all ok, we commit the changes from temp table. + if ($status) { + grade_import_commit($course->id, $importcode); + } else { + $errors = $gradeimport->get_gradebookerrors(); + $errors[] = get_string('importfailed', 'grades'); + echo $renderer->errors($errors); + } + echo $OUTPUT->footer(); +} else { + // If data hasn't been submitted then display the data mapping form. + $mform2->display(); + echo $OUTPUT->footer(); +} \ No newline at end of file diff --git a/grade/import/direct/lang/en/gradeimport_direct.php b/grade/import/direct/lang/en/gradeimport_direct.php new file mode 100644 index 00000000000..1218dc029ec --- /dev/null +++ b/grade/import/direct/lang/en/gradeimport_direct.php @@ -0,0 +1,29 @@ +. + +/** + * Strings for component 'gradeimport_direct', language 'en', branch 'MOODLE_28_STABLE' + * + * @package gradeimport_direct + * @copyright 2014 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +$string['direct:view'] = 'Import grades from CSV'; +$string['pluginname'] = 'Paste from spreadsheet'; +$string['userdata'] = 'Help copying data into this form.'; +$string['userdata_help'] = 'Grades may be copied and pasted from a spreadsheet into the gradebook. The spreadsheet should have a column containing user identity data - either username or ID number or email address. Each column for import should have a column header.'; +$string['userdata_link'] = 'grade/import/direct/index'; diff --git a/grade/import/direct/styles.css b/grade/import/direct/styles.css new file mode 100644 index 00000000000..a5b5a5550da --- /dev/null +++ b/grade/import/direct/styles.css @@ -0,0 +1,5 @@ +.gradeimport_data_area { + margin: 0px 0px 10px; + width: 475px; + height: 209px; +} \ No newline at end of file diff --git a/grade/import/direct/version.php b/grade/import/direct/version.php new file mode 100644 index 00000000000..023e95e01c2 --- /dev/null +++ b/grade/import/direct/version.php @@ -0,0 +1,30 @@ +. + +/** + * Version details + * + * @package gradeimport_direct + * @copyright 2014 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$plugin->version = 2014080400; // The current plugin version (Date: YYYYMMDDXX) +$plugin->requires = 2014050800; // Requires this Moodle version +$plugin->component = 'gradeimport_direct'; // Full name of the plugin (used for diagnostics). +$plugin->dependencies = array('gradeimport_csv' => 2014093000); // Grade import csv is required for this plugin. \ No newline at end of file diff --git a/lib/classes/plugin_manager.php b/lib/classes/plugin_manager.php index b385127ad59..ba915405a27 100644 --- a/lib/classes/plugin_manager.php +++ b/lib/classes/plugin_manager.php @@ -1034,7 +1034,7 @@ class core_plugin_manager { ), 'gradeimport' => array( - 'csv', 'xml' + 'csv', 'direct', 'xml' ), 'gradereport' => array(