first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,46 @@
<?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/>.
/**
* Privacy Subsystem implementation for gradeimport_xml.
*
* @package gradeimport_xml
* @copyright 2018 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace gradeimport_xml\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for gradeimport_xml implementing null_provider.
*
* @copyright 2018 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
+47
View File
@@ -0,0 +1,47 @@
<?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/>.
/**
* Capabilities gradeimport plugin.
*
* @package gradeimport_xml
* @copyright 2007 Martin Dougiamas
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$capabilities = array(
'gradeimport/xml:view' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),
'gradeimport/xml:publish' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'manager' => CAP_ALLOW
)
)
);
+38
View File
@@ -0,0 +1,38 @@
<?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/>.
define('NO_MOODLE_COOKIES', true); // session not used here
require_once '../../../config.php';
$id = required_param('id', PARAM_INT); // course id
if (!$course = $DB->get_record('course', array('id'=>$id))) {
throw new \moodle_exception('invalidcourseid');
}
require_user_key_login('grade/import', $id); // we want different keys for each course
if (empty($CFG->gradepublishing)) {
throw new \moodle_exception('gradepubdisable');
}
$context = context_course::instance($id);
require_capability('gradeimport/xml:publish', $context);
// use the same page parameters as import.php and append &key=sdhakjsahdksahdkjsahksadjksahdkjsadhksa
require 'import.php';
+113
View File
@@ -0,0 +1,113 @@
<?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/>.
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once $CFG->libdir.'/formslib.php';
class grade_import_form extends moodleform {
function definition() {
global $COURSE, $USER, $CFG, $DB;
$mform =& $this->_form;
if (isset($this->_customdata)) {
$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('importfile', 'grades'));
$mform->addElement('advcheckbox', 'feedback', get_string('importfeedback', 'grades'));
$mform->setDefault('feedback', 0);
// Restrict the possible upload file types.
if (!empty($features['acceptedtypes'])) {
$acceptedtypes = $features['acceptedtypes'];
} else {
$acceptedtypes = '*';
}
// File upload.
$mform->addElement('filepicker', 'userfile', get_string('file'), null, array('accepted_types' => $acceptedtypes));
$mform->disabledIf('userfile', 'url', 'noteq', '');
$mform->addElement('text', 'url', get_string('fileurl', 'gradeimport_xml'), 'size="80"');
$mform->setType('url', PARAM_URL);
$mform->disabledIf('url', 'userfile', 'noteq', '');
$mform->addHelpButton('url', 'fileurl', 'gradeimport_xml');
if (!empty($CFG->gradepublishing)) {
$mform->addElement('header', 'publishing', get_string('publishing', 'grades'));
$options = array(get_string('nopublish', 'grades'), get_string('createnewkey', 'userkey'));
$keys = $DB->get_records_select('user_private_key',
"script='grade/import' AND instance=? AND userid=?",
array($COURSE->id, $USER->id));
if ($keys) {
foreach ($keys as $key) {
$options[$key->value] = $key->value; // TODO: add more details - ip restriction, valid until ??
}
}
$mform->addElement('select', 'key', get_string('userkey', 'userkey'), $options);
$mform->addHelpButton('key', 'userkey', 'userkey');
$mform->addElement('static', 'keymanagerlink', get_string('keymanager', 'userkey'),
'<a href="'.$CFG->wwwroot.'/grade/import/keymanager.php?id='.$COURSE->id.'">'.get_string('keymanager', 'userkey').'</a>');
$mform->addElement('text', 'iprestriction', get_string('keyiprestriction', 'userkey'), array('size'=>80));
$mform->addHelpButton('iprestriction', 'keyiprestriction', 'userkey');
$mform->setDefault('iprestriction', getremoteaddr()); // own IP - just in case somebody does not know what user key is
$mform->addElement('date_time_selector', 'validuntil', get_string('keyvaliduntil', 'userkey'), array('optional'=>true));
$mform->addHelpButton('validuntil', 'keyvaliduntil', 'userkey');
$mform->setDefault('validuntil', time()+3600*24*7); // only 1 week default duration - just in case somebody does not know what user key is
$mform->disabledIf('iprestriction', 'key', 'noteq', 1);
$mform->disabledIf('validuntil', 'key', 'noteq', 1);
$mform->disabledIf('iprestriction', 'url', 'eq', '');
$mform->disabledIf('validuntil', 'url', 'eq', '');
$mform->disabledIf('key', 'url', 'eq', '');
}
$this->add_sticky_action_buttons(false, get_string('uploadgrades', 'grades'));
}
function validation($data, $files) {
$err = parent::validation($data, $files);
if (empty($data['url']) and empty($data['userfile'])) {
if (array_key_exists('url', $data)) {
$err['url'] = get_string('required');
}
if (array_key_exists('userfile', $data)) {
$err['userfile'] = get_string('required');
}
} else if (array_key_exists('url', $data) and $data['url'] != clean_param($data['url'], PARAM_URL)) {
$err['url'] = get_string('error');
}
return $err;
}
}
+81
View File
@@ -0,0 +1,81 @@
<?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/>.
require_once '../../../config.php';
require_once 'lib.php';
require_once $CFG->libdir.'/filelib.php';
$gradesurl = required_param('url', PARAM_URL); // only real urls here
$id = required_param('id', PARAM_INT); // course id
$feedback = optional_param('feedback', 0, PARAM_BOOL);
$url = new moodle_url('/grade/import/xml/import.php', array('id' => $id,'url' => $gradesurl));
if ($feedback !== 0) {
$url->param('feedback', $feedback);
}
$PAGE->set_url($url);
if (!$course = $DB->get_record('course', array('id'=>$id))) {
throw new \moodle_exception('invalidcourseid');
}
require_login($course);
$context = context_course::instance($id);
require_capability('moodle/grade:import', $context);
require_capability('gradeimport/xml:view', $context);
// Large files are likely to take their time and memory. Let PHP know
// that we'll take longer, and that the process should be recycled soon
// to free up memory.
core_php_time_limit::raise();
raise_memory_limit(MEMORY_EXTRA);
$text = download_file_content($gradesurl);
if ($text === false) {
throw new \moodle_exception('cannotreadfile', 'error',
$CFG->wwwroot . '/grade/import/xml/index.php?id=' . $id, $gradesurl);
}
$error = '';
$importcode = import_xml_grades($text, $course, $error);
if ($importcode !== false) {
/// commit the code if we are up this far
if (defined('USER_KEY_LOGIN')) {
if (grade_import_commit($id, $importcode, $feedback, false)) {
echo 'ok';
die;
} else {
throw new \moodle_exception('cannotimportgrade'); // TODO: localize.
}
} else {
print_grade_page_head($course->id, 'import', 'xml', get_string('importxml', 'grades'));
grade_import_commit($id, $importcode, $feedback, true);
echo $OUTPUT->footer();
die;
}
} else {
throw new \moodle_exception('errorduringimport', 'gradeimport_xml',
$CFG->wwwroot . '/grade/import/xml/index.php?id=' . $id, $error);
}
+98
View File
@@ -0,0 +1,98 @@
<?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/>.
require_once('../../../config.php');
require_once($CFG->libdir . '/gradelib.php');
require_once($CFG->dirroot . '/grade/lib.php');
require_once($CFG->dirroot . '/grade/import/xml/lib.php');
require_once($CFG->dirroot . '/grade/import/xml/grade_import_form.php');
$id = required_param('id', PARAM_INT); // course id
$PAGE->set_url(new moodle_url('/grade/import/xml/index.php', array('id'=>$id)));
$PAGE->set_pagelayout('admin');
if (!$course = $DB->get_record('course', array('id'=>$id))) {
throw new \moodle_exception('invalidcourseid');
}
require_login($course);
$context = context_course::instance($id);
require_capability('moodle/grade:import', $context);
require_capability('gradeimport/xml:view', $context);
// print header
$strgrades = get_string('grades', 'grades');
$actionstr = get_string('pluginname', 'gradeimport_xml');
if (!empty($CFG->gradepublishing)) {
$CFG->gradepublishing = has_capability('gradeimport/xml:publish', $context);
}
$mform = new grade_import_form(null, array('acceptedtypes' => array('.xml')));
if ($data = $mform->get_data()) {
// Large files are likely to take their time and memory. Let PHP know
// that we'll take longer, and that the process should be recycled soon
// to free up memory.
core_php_time_limit::raise();
raise_memory_limit(MEMORY_EXTRA);
if ($text = $mform->get_file_content('userfile')) {
print_grade_page_head($COURSE->id, 'import', 'xml',
get_string('importxml', 'grades'), false, false, true, 'importxml', 'gradeimport_xml');
$error = '';
$importcode = import_xml_grades($text, $course, $error);
if ($importcode) {
grade_import_commit($id, $importcode, $data->feedback, true);
echo $OUTPUT->footer();
die;
} else {
echo $OUTPUT->notification($error);
echo $OUTPUT->continue_button($CFG->wwwroot.'/grade/index.php?id='.$course->id);
echo $OUTPUT->footer();
die;
}
} else if (empty($data->key)) {
redirect('import.php?id='.$id.'&amp;feedback='.(int)($data->feedback).'&url='.urlencode($data->url));
} else {
if ($data->key == 1) {
$data->key = create_user_key('grade/import', $USER->id, $course->id, $data->iprestriction, $data->validuntil);
}
print_grade_page_head($COURSE->id, 'import', 'xml',
get_string('importxml', 'grades'), false, false, true, 'importxml', 'gradeimport_xml');
echo '<div class="gradeexportlink">';
$link = $CFG->wwwroot.'/grade/import/xml/fetch.php?id='.$id.'&amp;feedback='.(int)($data->feedback).'&amp;url='.urlencode($data->url).'&amp;key='.$data->key;
echo get_string('import', 'grades').': <a href="'.$link.'">'.$link.'</a>';
echo '</div>';
echo $OUTPUT->footer();
die;
}
}
$actionbar = new \core_grades\output\import_action_bar($context, null, 'xml');
print_grade_page_head($COURSE->id, 'import', 'xml', get_string('importxml', 'grades'),
false, false, true, 'importxml', 'gradeimport_xml', null, $actionbar);
$mform->display();
echo $OUTPUT->footer();
@@ -0,0 +1,42 @@
<?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 component 'gradeimport_xml', language 'en', branch 'MOODLE_20_STABLE'
*
* @package gradeimport_xml
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['errbadxmlformat'] = 'Error - bad XML format';
$string['errduplicategradeidnumber'] = 'Error - there are two grade items with idnumber \'{$a}\' in this course. This should be impossible.';
$string['errduplicateidnumber'] = 'Error - duplicate idnumber';
$string['errincorrectgradeidnumber'] = 'Error - idnumber \'{$a}\' from the import file does not match any grade item.';
$string['errincorrectidnumber'] = 'Error - incorrect idnumber';
$string['errincorrectuseridnumber'] = 'Error - idnumber \'{$a}\' from the import file does not match any user.';
$string['error'] = 'Errors occur';
$string['errorduringimport'] = 'An error occurred when trying to import: {$a}';
$string['fileurl'] = 'Remote file URL';
$string['fileurl_help'] = 'The remote file URL field is for fetching data from a remote server, such as a student information system.';
$string['pluginname'] = 'XML file';
$string['privacy:metadata'] = 'The import grades from XML plugin does not store any personal data.';
$string['xml:publish'] = 'Publish import grades from XML';
$string['xml:view'] = 'Import grades from XML';
$string['importxml'] = 'XML import';
$string['importxml_help'] = 'Grades can be imported via an XML file containing user ID numbers and activity ID numbers. To obtain the correct format, first export some grades to XML file then view the file.';
$string['importxml_link'] = 'grade/import/xml/index';
+123
View File
@@ -0,0 +1,123 @@
<?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/>.
require_once $CFG->libdir.'/gradelib.php';
require_once($CFG->libdir.'/xmlize.php');
require_once $CFG->dirroot.'/grade/lib.php';
require_once $CFG->dirroot.'/grade/import/lib.php';
function import_xml_grades($text, $course, &$error) {
global $USER, $DB;
$importcode = get_new_importcode();
$status = true;
$content = xmlize($text);
if (!empty($content['results']['#']['result'])) {
$results = $content['results']['#']['result'];
foreach ($results as $i => $result) {
$gradeidnumber = $result['#']['assignment'][0]['#'];
if (!$grade_items = grade_item::fetch_all(array('idnumber'=>$gradeidnumber, 'courseid'=>$course->id))) {
// gradeitem does not exist
// no data in temp table so far, abort
$status = false;
$error = get_string('errincorrectgradeidnumber', 'gradeimport_xml', $gradeidnumber);
break;
} else if (count($grade_items) != 1) {
$status = false;
$error = get_string('errduplicategradeidnumber', 'gradeimport_xml', $gradeidnumber);
break;
} else {
$grade_item = reset($grade_items);
}
// grade item locked, abort
if ($grade_item->is_locked()) {
$status = false;
$error = get_string('gradeitemlocked', 'grades');
break;
}
// check if user exist and convert idnumber to user id
$useridnumber = $result['#']['student'][0]['#'];
if (!$user = $DB->get_record('user', array('idnumber' =>$useridnumber))) {
// no user found, abort
$status = false;
$error = get_string('errincorrectuseridnumber', 'gradeimport_xml', $useridnumber);
break;
}
// check if grade_grade is locked and if so, abort
if ($grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$user->id))) {
$grade_grade->grade_item =& $grade_item;
if ($grade_grade->is_locked()) {
// individual grade locked, abort
$status = false;
$error = get_string('gradelocked', 'grades');
break;
}
}
$newgrade = new stdClass();
$newgrade->itemid = $grade_item->id;
$newgrade->userid = $user->id;
$newgrade->importcode = $importcode;
$newgrade->importer = $USER->id;
// check grade value exists and is a numeric grade
if (isset($result['#']['score'][0]['#']) && $result['#']['score'][0]['#'] !== '-') {
if (is_numeric($result['#']['score'][0]['#'])) {
$newgrade->finalgrade = $result['#']['score'][0]['#'];
} else {
$status = false;
$error = get_string('badgrade', 'grades');
break;
}
} else {
$newgrade->finalgrade = NULL;
}
// check grade feedback exists
if (isset($result['#']['feedback'][0]['#'])) {
$newgrade->feedback = $result['#']['feedback'][0]['#'];
} else {
$newgrade->feedback = NULL;
}
// insert this grade into a temp table
$DB->insert_record('grade_import_values', $newgrade);
}
} else {
// no results section found in xml,
// assuming bad format, abort import
$status = false;
$error = get_string('errbadxmlformat', 'gradeimport_xml');
}
if ($status) {
return $importcode;
} else {
import_cleanup($importcode);
return false;
}
}
+30
View File
@@ -0,0 +1,30 @@
<?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/>.
/**
* Version details
*
* @package gradeimport
* @subpackage xml
* @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024042200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2024041600; // Requires this Moodle version.
$plugin->component = 'gradeimport_xml'; // Full name of the plugin (used for diagnostics)