diff --git a/classes/defaults_form.php b/classes/defaults_form.php index fb1428e..2d41820 100644 --- a/classes/defaults_form.php +++ b/classes/defaults_form.php @@ -109,12 +109,53 @@ public function definition () { $mform->addHelpButton('urkund_storedocuments_'.$sm, 'storedocuments', 'plagiarism_urkund'); $mform->setType('urkund_storedocuments_'.$sm, PARAM_INT); + if ($sm == 'assign') { + // Check if maharaws is enabled. + $pm = \core_plugin_manager::instance(); + $submissionplugins = $pm->get_enabled_plugins('assignsubmission'); + if (!empty($submissionplugins) && key_exists('maharaws', $submissionplugins)) { + $mform->addElement('html', get_string('setmahara', 'plagiarism_urkund')); + + $mform->addElement('text', 'urkund_maharawsurl_' . $sm, + get_string('maharawsurl', 'plagiarism_urkund'), + array('maxlength' => 255, 'size' => 50)); + $mform->addHelpButton('urkund_maharawsurl_' . $sm, 'maharawsurl', 'plagiarism_urkund'); + $mform->setDefault('urkund_maharawsurl_' . $sm, ''); + $mform->setType('urkund_maharawsurl_' . $sm, PARAM_URL); + + $mform->addElement('text', 'urkund_maharawshtmllitekey_' . $sm, + get_string('maharawshtmllitekey', 'plagiarism_urkund'), + array('maxlength' => 255, 'size' => 50)); + $mform->addHelpButton('urkund_maharawshtmllitekey_' . $sm, 'maharawshtmllitekey', 'plagiarism_urkund'); + $mform->setDefault('urkund_maharawshtmllitekey_' . $sm, ''); + $mform->setType('urkund_maharawshtmllitekey_' . $sm, PARAM_ALPHANUM); + + $mform->addElement('text', 'urkund_maharawshtmllitesecret_' . $sm, + get_string('maharawshtmllitesecret', 'plagiarism_urkund'), + array('maxlength' => 255, 'size' => 50)); + $mform->addHelpButton('urkund_maharawshtmllitesecret_' . $sm, 'maharawshtmllitesecret', 'plagiarism_urkund'); + $mform->setDefault('urkund_maharawshtmllitesecret_' . $sm, ''); + $mform->setType('urkund_maharawshtmllitesecret_' . $sm, PARAM_ALPHANUM); + + $mform->addElement('text', 'urkund_maharawshtmllitetoken_' . $sm, + get_string('maharawshtmllitetoken', 'plagiarism_urkund'), + array('maxlength' => 255, 'size' => 50)); + $mform->addHelpButton('urkund_maharawshtmllitetoken_' . $sm, 'maharawshtmllitetoken', 'plagiarism_urkund'); + $mform->setDefault('urkund_maharawshtmllitetoken_' . $sm, ''); + $mform->setType('urkund_maharawshtmllitetoken_' . $sm, PARAM_ALPHANUM); + } + } + $items = array(); $aliases = array( 'use_urkund' => 'useurkund', 'urkund_allowallfile' => 'allowallsupportedfiles', 'urkund_selectfiletypes' => 'restrictfiles', 'urkund_restrictcontent' => 'restrictcontent', + 'urkund_maharawshtmllitekey' => 'maharawshtmllitekey', + 'urkund_maharawshtmllitesecret' => 'maharawshtmllitesecret', + 'urkund_maharawshtmllitetoken' => 'maharawshtmllitetoken', + 'urkund_maharawsurl' => 'maharawsurl', 'urkund_storedocuments' => 'storedocuments' ); foreach (plagiarism_plugin_urkund::config_options() as $setting) { diff --git a/classes/helper/mahara_portfolio.php b/classes/helper/mahara_portfolio.php new file mode 100644 index 0000000..58828e0 --- /dev/null +++ b/classes/helper/mahara_portfolio.php @@ -0,0 +1,273 @@ +. + +/** + * Manage connection to Mahara for portfolio 'HTML Lite' export. + * + * @package plagiarism_urkund + * @author David Balch + * @copyright 2021 onwards Catalyst IT Europe + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace plagiarism_urkund\helper; +defined('MOODLE_INTERNAL') || die(); + +/** + * Class mahara_portfolio + * + * @package plagiarism_urkund + * @copyright 2021 onwards Catalyst IT Europe + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mahara_portfolio { + + private $instanceid; + private $context; + private $userid; + private $relateduserid; + private $submission; + private $fileinfo; + private $host; + private $oauthurl; + private $oauthkey; + private $oauthsecret; + private $wstoken; + private $fileurl; + private $zipfile; + + /** + * Constructor. + * + * @param array[int] Data about this assignsubmission_maharaws submission: + * $instanceid instance id of the assignment. + * $submissionid id of the submission. + * $userid id of the user submitting. + * $relateduserid id of the user being submitted onbehalf of. + * @throws dml_exception + */ + public function __construct($data) { + global $CFG, $DB; + $this->instanceid = $data->instanceid; + $this->userid = $data->userid; + $this->relateduserid = $data->relateduserid; + + // Get submission context id from context instance id. + $this->context = \context_module::instance($this->instanceid); + + // Load submission details. + $this->submission = $this->get_mahara_submission($data->submissionid); + + // Set site/assignment config. + $plagiarismvalues = $DB->get_records_menu('plagiarism_urkund_config', + array('cm' => $this->instanceid), '', 'name, value'); + $this->host = $CFG->wwwroot; + $this->oauthurl = $plagiarismvalues['urkund_maharawsurl']; + $this->oauthkey = $plagiarismvalues['urkund_maharawshtmllitekey']; + $this->oauthsecret = $plagiarismvalues['urkund_maharawshtmllitesecret']; + $this->wstoken = $plagiarismvalues['urkund_maharawshtmllitetoken']; + + // Check if there's a zip file already downloaded by previous processing. + $fs = get_file_storage(); + $fileinfo = array( + 'contextid' => $this->context->id, + 'instanceid' => $this->instanceid, + 'component' => 'plagiarism_urkund', + 'filearea' => 'htmllite', + 'itemid' => $this->submission->id, + 'filepath' => '/maharahtmllite/', + 'filename' => 's' . $this->submission->id . '.zip' + ); + $this->fileinfo = $fileinfo; + + if ($file = $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'], + $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename'])) { + mtrace('urkund: mahara_portfolio: Using previously downloaded zip file.'); + $this->zipfile = $file; + } + } + + /** + * Get Mahara submission information from the database + * + * @param int $submissionid + * @return mixed + */ + private function get_mahara_submission($submissionid) { + global $DB; + + return $DB->get_record('assignsubmission_maharaws', array('submission' => $submissionid)); + } + + /** + * Run all the steps to download a portfolio and add it to the Ouriginal queue. + * + */ + public function download_to_queue() { + if (empty($this->zipfile)) { + $this->get_fileurl(); + $this->download_zip(); + } + $this->queue_files(); + } + + /** + * Make initial Mahara HTML Lite API request to generate files for the portfolio. + * + */ + private function get_fileurl() { + mtrace('urkund: mahara_portfolio: Calling Mahara HTML Lite API.'); + + // Get fileurl. + $params = array('viewid' => $this->submission->viewid, + 'iscollection' => $this->submission->iscollection, + 'submittedhost' => $this->host, + 'exporttype' => 'htmllite'); + $params = array('views' => array($params)); + $data = $this->webservice_call('mahara_submission_generate_view_for_plagiarism_test', $params, $method = "POST"); + $this->fileurl = $data[0]['fileurl']; + } + + /** + * Webservice call helper. + * + * @param string $function + * @param array $params + * @param string $method + * + * @return mixed + */ + private function webservice_call($function, $params, $method = "POST") { + global $CFG; + + $endpoint = $this->oauthurl . + (preg_match('/\/$/', $this->oauthurl) ? '' : '/') . + 'webservice/rest/server.php'; + $args = array( + 'oauth_consumer_key' => $this->oauthkey, + 'oauth_consumer_secret' => $this->oauthsecret, + 'oauth_callback' => 'about:blank', + 'api_root' => $endpoint, + ); + + // Reuse the maharaws oauth client. + require_once($CFG->dirroot . '/mod/assign/submissionplugin.php'); + require_once($CFG->dirroot . '/mod/assign/submission/maharaws/locallib.php'); + $client = new \mahara_oauth($args); + if (!empty($CFG->disablesslchecks)) { + $options = array('CURLOPT_SSL_VERIFYPEER' => 0, 'CURLOPT_SSL_VERIFYHOST' => 0); + $client->setup_oauth_http_options($options); + } + // Have to flatten nested parameters into JSON as OAuth can't handle it. + foreach ($params as $k => $v) { + if (is_array($v)) { + $params[$k] = json_encode($v); + } + } + $content = $client->request($method, $endpoint, + array_merge($params, array('wsfunction' => $function, 'alt' => 'json')), + null, + $this->oauthsecret); + $data = json_decode($content, true); + + if (isset($data['error']) && $data['error'] == true ) { + throw new \Exception($data['error_rendered']); + } + return $data; + } + + /** + * Download HTML Lite export .zip file from Mahara. + * + */ + private function download_zip() { + mtrace('urkund: mahara_portfolio: Downloading portfolio zip file.'); + // Would be nice to use the zip streaming added in M3.11 (MDL-68533), + // but for now we have to save to a temp file and then extract it. + $url = $this->fileurl . '&wstoken=' . $this->wstoken; + + $fs = get_file_storage(); + $this->zipfile = $fs->create_file_from_url($this->fileinfo, $url); + } + + /** + * Extract files from zip and add to the Ouriginal queue. + * + */ + private function queue_files() { + global $CFG; + mtrace('urkund: mahara_portfolio: Extracting files from zip file.'); + require_once($CFG->dirroot . '/plagiarism/urkund/lib.php'); + + $success = true; + $zipfile = $this->zipfile; + $fileinfo = $this->fileinfo; + $dirpath = $fileinfo['filepath'] . 's' . $this->submission->id . '/'; + if ($this->relateduserid) { + $theuserid = $this->relateduserid; + } else { + $theuserid = $this->userid; + } + + // Extract the files to storage and save them as Moodle files, + // to make them available to the Ouriginal queue in a scheduled task. + $packer = new \zip_packer(); + $zipcontents = $zipfile->extract_to_storage($packer, $this->context->id, $fileinfo['component'], + $fileinfo['filearea'], $this->submission->submission, $dirpath, $theuserid); + if (!$zipcontents) { + mtrace('urkund: mahara_portfolio: Extracting zip failed - aborting'); + return false; + } + $fs = get_file_storage(); + foreach (array_keys($zipcontents) as $entry) { + if ($pos = strpos($entry, '/')) { + // Split path components from the entry into the path param. + $thisdir = $dirpath . substr($entry, 0, $pos + 1); + $thisfile = substr($entry, $pos +1 ); + } else { + $thisdir = $dirpath; + $thisfile = $entry; + } + + // Load as a Moodle file object, suitable for adding to the queue. + $file = $fs->get_file($this->context->id, $fileinfo['component'], $fileinfo['filearea'], + $this->submission->submission, $thisdir, $thisfile); + if ($file->get_filename() != '.') { + mtrace('urkund: mahara_portfolio: Queue file: ' . $entry); + $result = urkund_queue_file($this->instanceid, $this->userid, $file, $this->relateduserid); + if (!$result) { + $success = false; + mtrace('urkund: mahara_portfolio: Queue file failed - aborting: ' . $entry); + } + } + } + + if ($success) { + global $DB; + // Remove this submission's maharawsdl entry from queue. + $DB->delete_records('plagiarism_urkund_files', array( + 'cm' => $this->instanceid, + 'userid' => $this->userid, + 'statuscode' => 'maharawsdl' + )); + // Remove saved zip file from storage. + mtrace('urkund: mahara_portfolio: Deleting downloaded portfolio zip file ' . $this->zipfile->get_filename()); + $this->zipfile->delete(); + } + + } + +} diff --git a/classes/task/fetch_mahara.php b/classes/task/fetch_mahara.php new file mode 100644 index 0000000..b525ba9 --- /dev/null +++ b/classes/task/fetch_mahara.php @@ -0,0 +1,55 @@ +. + +/** + * URKUND task - Fetch queued portfolios from Mahara. + * + * @package plagiarism_urkund + * @author David Balch + * @copyright 2021 onwards Catalyst IT Europe + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace plagiarism_urkund\task; + +defined('MOODLE_INTERNAL') || die(); + +/** + * fetch_mahara class, fetches queued portfolios from Mahara. + * + * @package plagiarism_urkund + * @author David Balch + * @copyright 2021 onwards Catalyst IT Europe + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class fetch_mahara extends \core\task\scheduled_task { + /** + * Returns the name of this task. + */ + public function get_name() { + // Shown in admin screens. + return get_string('fetchmahara', 'plagiarism_urkund'); + } + + /** + * Execute task. + */ + public function execute() { + global $CFG; + require_once($CFG->dirroot.'/plagiarism/urkund/lib.php'); + plagiarism_urkund_fetch_mahara(); + } +} diff --git a/db/tasks.php b/db/tasks.php index 278b93f..ba899e6 100644 --- a/db/tasks.php +++ b/db/tasks.php @@ -61,5 +61,14 @@ 'day' => '*', 'dayofweek' => '*', 'month' => '*' + ), + array( + 'classname' => 'plagiarism_urkund\task\fetch_mahara', + 'blocking' => 0, + 'minute' => '*/5', + 'hour' => '*', + 'day' => '*', + 'dayofweek' => '*', + 'month' => '*' ) ); diff --git a/lang/en/plagiarism_urkund.php b/lang/en/plagiarism_urkund.php index 45be143..a80df2a 100644 --- a/lang/en/plagiarism_urkund.php +++ b/lang/en/plagiarism_urkund.php @@ -48,6 +48,7 @@ $string['deleteselectedfiles'] = 'Delete selected'; $string['deleteallfiltered'] = 'Delete all files that match the current filter'; $string['explainerrors'] = 'This page lists any files that are currently in an error state.
When files are deleted on this page they will not be able to be resubmitted and errors will no longer display to teachers or students'; +$string['fetchmahara'] = 'Fetch Mahara portfolios for checking'; $string['file'] = 'File'; $string['filedeleted'] = 'File deleted from queue'; $string['filereset'] = 'A file has been reset for re-submission to Ouriginal'; @@ -66,6 +67,16 @@ $string['idisagree'] = 'Cancel'; $string['id'] = 'ID'; $string['identifier'] = 'Identifier'; +$string['setmahara'] = 'Set the Mahara URL, and the HTML Lite OAuth secret and key here to check portfolio submissions.'; +$string['maharawshtmlliteenabled'] = 'Check Mahara portfolios'; +$string['maharawshtmllitekey'] = 'Mahara HTML Lite OAuth key'; +$string['maharawshtmllitekey_help'] = 'The HTML Lite OAuth key from the partner Mahara sute.'; +$string['maharawshtmllitesecret'] = 'Mahara HTML Lite OAuth secret'; +$string['maharawshtmllitesecret_help'] = 'The HTML Lite OAuth secret from the partner Mahara sute.'; +$string['maharawshtmllitetoken'] = 'Mahara HTML Lite access token'; +$string['maharawshtmllitetoken_help'] = 'The HTML Lite access token from the partner Mahara sute.'; +$string['maharawsurl'] = 'Mahara site URL'; +$string['maharawsurl_help'] = 'The URL of the partner Mahara sute. (Must be the Mahara site URL defined in the Mahara submissions plugin.)'; $string['module'] = 'Module'; $string['modulenotfound'] = 'Could not find module for {$a->module} with id {$a->modid}.'; $string['name'] = 'Name'; diff --git a/lib.php b/lib.php index 21c2f77..993f58c 100644 --- a/lib.php +++ b/lib.php @@ -47,6 +47,9 @@ define('URKUND_STATUSCODE_INVALID_RESPONSE', '613'); // Invalid response received from URKUND. define('URKUND_STATUSCODE_PENDING', 'pending'); +// Codes relating to processing Mahara protfolios. +define('URKUND_STATUSCODE_MAHARAWS_DOWNLOAD', 'maharawsdl'); + // Url to external xml that states URKUNDS allowed file type list. define('URKUND_FILETYPE_URL', 'https://secure.urkund.com/ws/integration/accepted-formats.xml'); @@ -111,7 +114,9 @@ public static function get_settings() { public static function config_options($adminsettings = false) { $options = array('use_urkund', 'urkund_show_student_score', 'urkund_show_student_report', 'urkund_draft_submit', 'urkund_resubmit_on_close', 'urkund_receiver', 'urkund_studentemail', - 'urkund_allowallfile', 'urkund_selectfiletypes', 'urkund_restrictcontent', 'urkund_storedocuments'); + 'urkund_allowallfile', 'urkund_selectfiletypes', 'urkund_restrictcontent', 'urkund_storedocuments', + 'urkund_maharawsurl', 'urkund_maharawshtmllitekey', + 'urkund_maharawshtmllitesecret', 'urkund_maharawshtmllitetoken'); if ($adminsettings) { $options[] = 'urkund_advanceditems'; } @@ -632,6 +637,24 @@ public function event_handler($eventdata) { $result = true; if (isset($plagiarismvalues['urkund_draft_submit']) && $plagiarismvalues['urkund_draft_submit'] == PLAGIARISM_URKUND_DRAFTSUBMIT_FINAL) { + + // If there's a Mahara portfolio submission, add it to the queue. + $submissionid = $eventdata['objectid']; + $maharasubmission = $DB->get_record('assignsubmission_maharaws', array('submission' => $submissionid)); + if ($maharasubmission) { + $data = array( + 'instanceid' => $cmid, + 'submissionid' => $submissionid, + 'userid' => $userid, + 'relateduserid' => $relateduserid + ); + $data = json_encode($data); + + // Save Mahara file details to temp file, so the download can be in the async task queue. + $file = urkund_create_temp_file($cmid, $eventdata['courseid'], $userid, $data, false); + urkund_queue_file($cmid, $userid, $file, $relateduserid, URKUND_STATUSCODE_MAHARAWS_DOWNLOAD); + } + // Any files attached to previous events were not submitted. // These files are now finalized, and should be submitted for processing. require_once("$CFG->dirroot/mod/assign/locallib.php"); @@ -883,7 +906,7 @@ public function load_receiver() { * * @return string */ -function urkund_create_temp_file($cmid, $courseid, $userid, $filecontent) { +function urkund_create_temp_file($cmid, $courseid, $userid, $filecontent, $formattempcontent = true) { global $CFG; if (!check_dir_exists($CFG->tempdir."/urkund", true, true)) { mkdir($CFG->tempdir."/urkund", 0700); @@ -892,8 +915,12 @@ function urkund_create_temp_file($cmid, $courseid, $userid, $filecontent) { $filepath = $CFG->tempdir."/urkund/" . $filename; $fd = fopen($filepath, 'wb'); // Create if not exist, write binary. - // Write html and body tags as it seems that Urkund doesn't works well without them. - $content = plagiarism_urkund_format_temp_content($filecontent); + if ($formattempcontent) { + // Write html and body tags as it seems that Urkund doesn't works well without them. + $content = plagiarism_urkund_format_temp_content($filecontent); + } else { + $content = $filecontent; + } fwrite($fd, $content); fclose($fd); @@ -1255,6 +1282,37 @@ function urkund_get_form_elements($mform) { $supportedfiles, array('multiple' => true)); $mform->setType('urkund_selectfiletypes', PARAM_TAGLIST); + // Check if maharaws is enabled. + $pm = \core_plugin_manager::instance(); + $submissionplugins = $pm->get_enabled_plugins('assignsubmission'); + if (!empty($submissionplugins) && key_exists('maharaws', $submissionplugins)) { + $mform->addElement('html', get_string('setmahara', 'plagiarism_urkund')); + + $mform->addElement('text', 'urkund_maharawsurl', get_string('maharawsurl', 'plagiarism_urkund'), + array('maxlength' => 255, 'size' => 50)); + $mform->addHelpButton('urkund_maharawsurl', 'maharawsurl', 'plagiarism_urkund'); + $mform->setDefault('urkund_maharawsurl', ''); + $mform->setType('urkund_maharawsurl', PARAM_URL); + + $mform->addElement('text', 'urkund_maharawshtmllitekey', get_string('maharawshtmllitekey', 'plagiarism_urkund'), + array('maxlength' => 255, 'size' => 50)); + $mform->addHelpButton('urkund_maharawshtmllitekey', 'maharawshtmllitekey', 'plagiarism_urkund'); + $mform->setDefault('urkund_maharawshtmllitekey', ''); + $mform->setType('urkund_maharawshtmllitekey', PARAM_ALPHANUM); + + $mform->addElement('password', 'urkund_maharawshtmllitesecret', get_string('maharawshtmllitesecret', 'plagiarism_urkund'), + array('maxlength' => 255, 'size' => 50)); + $mform->addHelpButton('urkund_maharawshtmllitesecret', 'maharawshtmllitesecret', 'plagiarism_urkund'); + $mform->setDefault('urkund_maharawshtmllitesecret', ''); + $mform->setType('urkund_maharawshtmllitesecret', PARAM_ALPHANUM); + + $mform->addElement('password', 'urkund_maharawshtmllitetoken', get_string('maharawshtmllitetoken', 'plagiarism_urkund'), + array('maxlength' => 255, 'size' => 50)); + $mform->addHelpButton('urkund_maharawshtmllitetoken', 'maharawshtmllitetoken', 'plagiarism_urkund'); + $mform->setDefault('urkund_maharawshtmllitetoken', ''); + $mform->setType('urkund_maharawshtmllitetoken', PARAM_ALPHANUM); + } + $mform->addElement('select', 'urkund_storedocuments', get_string('storedocuments', 'plagiarism_urkund'), $ynoptions); $mform->addHelpButton('urkund_storedocuments', 'storedocuments', 'plagiarism_urkund'); $mform->setType('urkund_storedocuments', PARAM_INT); @@ -1331,13 +1389,21 @@ function urkund_get_plagiarism_file($cmid, $userid, $file, $relateduserid = null * @param int $userid - user id * @param varied $file string if path to temp file or full Moodle file object. * @param int $relateduserid - related user if if passed. (use when sending to URKUND. + * @param string $statusoverride - Override the file's initial queue status. Used for queuing a Mahara HTML Lite download. * @return boolean */ -function urkund_queue_file($cmid, $userid, $file, $relateduserid = null) { +function urkund_queue_file($cmid, $userid, $file, $relateduserid = null, $statusoverride = null) { global $DB; $plagiarismfile = urkund_get_plagiarism_file($cmid, $userid, $file, $relateduserid); $plagiarismvalues = $DB->get_records_menu('plagiarism_urkund_config', array('cm' => $cmid), '', 'name, value'); + // Check if $plagiarismfile needs to be downloaded from Mahara. + if (!empty($statusoverride)) { + $plagiarismfile->statuscode = $statusoverride; + $DB->update_record('plagiarism_urkund_files', $plagiarismfile); + return ''; + } + // Check if $plagiarismfile actually needs to be submitted. if ($plagiarismfile->statuscode <> 'pending') { return ''; @@ -2021,6 +2087,11 @@ function plagiarism_urkund_get_file_object($plagiarismfile) { foreach ($submissionplugins as $submissionplugin) { $component = $submissionplugin->get_subtype() . '_' . $submissionplugin->get_type(); $fileareas = $submissionplugin->get_file_areas(); + if ($component == 'assignsubmission_maharaws') { + // File area for scanning maharaws files is set in plagiarism_urkund\helper\mahara_portfolio. + $component = 'plagiarism_urkund'; + $fileareas = array('htmllite' => 'maharawsfiles'); + } foreach ($fileareas as $filearea => $name) { if (debugging()) { mtrace("URKUND fileid:" . $plagiarismfile->id . " Check component:" . $component . " Filearea:" . @@ -2173,6 +2244,46 @@ function plagiarism_urkund_send_files() { } } +/** + * Function called by scheduled task to get files from Mahara. + * + */ +function plagiarism_urkund_fetch_mahara() { + global $DB; + + $plagiarismsettings = plagiarism_plugin_urkund::get_settings(); + if (!empty($plagiarismsettings)) { + // Get all files awaiting Mahara download. + $sql = 'statuscode = ? AND attempt <= ?'; + $plagiarismfiles = $DB->get_recordset_select("plagiarism_urkund_files", $sql, + array(URKUND_STATUSCODE_MAHARAWS_DOWNLOAD, PLAGIARISM_URKUND_MAXATTEMPTS)); + foreach ($plagiarismfiles as $pf) { + // Check to make sure cm exists. - delete record if cm has been deleted. + $sql = "SELECT m.name + FROM {modules} m + JOIN {course_modules} cm ON cm.module = m.id + WHERE cm.id = ?"; + $modulename = $DB->get_field_sql($sql, array($pf->cm)); + if (empty($modulename)) { + // The coursemodule related to this file has been deleted, delete the urkund entry. + mtrace("URKUND fileid:$pf->id Course module id:".$pf->cm. " does not exist, deleting record"); + $DB->delete_records('plagiarism_urkund_files', array('id' => $pf->id)); + continue; + } + mtrace("URKUND fileid:".$pf->id. ' sending for processing'); + $file = plagiarism_urkund_get_file_object($pf); + // Get Mahara file details from temp file. + $data = json_decode(file_get_contents($file->filepath)); + + // Create helper with assignment submission details. + $portfolio = new \plagiarism_urkund\helper\mahara_portfolio($data); + + // Call Mahara to get the portfolio. + $portfolio->download_to_queue(); + } + } +} + /** * Check if assign group submission is being used in assign activity. * This is based on old-code, there might be a more efficient way to do this.