/** * Generic handler function for all events - queues files for sending. * @return boolean */ public function event_handler($eventdata) { global $DB, $CFG; $plagiarismsettings = $this->get_settings(); if (!$plagiarismsettings) { return true; } $cmid = $eventdata['contextinstanceid']; $plagiarismvalues = $DB->get_records_menu('plagiarism_urkund_config', array('cm' => $cmid), '', 'name, value'); if (empty($plagiarismvalues['use_urkund'])) { // Urkund not in use for this cm - return. return true; } // Check if the module associated with this event still exists. if (!$DB->record_exists('course_modules', array('id' => $cmid))) { return true; } // Check to see if restrictcontent is in use. $showcontent = true; $showfiles = true; if (!empty($plagiarismvalues[$cmid]['urkund_restrictcontent'])) { if ($plagiarismvalues['urkund_restrictcontent'] == PLAGIARISM_URKUND_RESTRICTCONTENTFILES) { $showcontent = false; } else { if ($plagiarismvalues['urkund_restrictcontent'] == PLAGIARISM_URKUND_RESTRICTCONTENTTEXT) { $showfiles = false; } } } if (!empty($plagiarismsettings['urkund_wordcount'])) { $wordcount = $plagiarismsettings['urkund_wordcount']; } else { // Set a sensible default if we can't find one. $wordcount = 50; } if ($eventdata['eventtype'] == 'assignsubmission_submitted' && empty($eventdata['other']['submission_editable'])) { // Assignment-specific functionality: // This is a 'finalize' event. No files from this event itself, // but need to check if files from previous events need to be submitted for processing. $result = true; if (isset($plagiarismvalues['urkund_draft_submit']) && $plagiarismvalues['urkund_draft_submit'] == PLAGIARISM_URKUND_DRAFTSUBMIT_FINAL) { // 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"; require_once "{$CFG->dirroot}/mod/assign/submission/file/locallib.php"; $modulecontext = context_module::instance($cmid); if ($showfiles) { // If we should be handling files. $fs = get_file_storage(); if ($files = $fs->get_area_files($modulecontext->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA, $eventdata['objectid'], "id", false)) { foreach ($files as $file) { urkund_queue_file($cmid, $eventdata['userid'], $file); } } } if ($showcontent) { // If we should be handling in-line text. $submission = $DB->get_record('assignsubmission_onlinetext', array('submission' => $eventdata['objectid'])); if (!empty($submission) && str_word_count($submission->onlinetext) > $wordcount) { $content = trim(format_text($submission->onlinetext, $submission->onlineformat, array('context' => $modulecontext))); $file = urkund_create_temp_file($cmid, $eventdata['courseid'], $eventdata['userid'], $content); urkund_queue_file($cmid, $eventdata['userid'], $file); } } } return $result; } if (isset($plagiarismvalues['urkund_draft_submit']) && $plagiarismvalues['urkund_draft_submit'] == PLAGIARISM_URKUND_DRAFTSUBMIT_FINAL) { // Assignment-specific functionality: // Files should only be sent for checking once "finalized". return true; } // Text is attached. $result = true; if (!empty($eventdata['other']['content']) && $showcontent && str_word_count($eventdata['other']['content']) > $wordcount) { $file = urkund_create_temp_file($cmid, $eventdata['courseid'], $eventdata['userid'], $eventdata['other']['content']); urkund_queue_file($cmid, $eventdata['userid'], $file); } // Normal situation: 1 or more assessable files attached to event, ready to be checked. if (!empty($eventdata['other']['pathnamehashes']) && $showfiles) { foreach ($eventdata['other']['pathnamehashes'] as $hash) { $fs = get_file_storage(); $efile = $fs->get_file_by_hash($hash); if (empty($efile)) { mtrace("nofilefound!"); continue; } else { if ($efile->get_filename() === '.') { // This 'file' is actually a directory - nothing to submit. continue; } } urkund_queue_file($cmid, $eventdata['userid'], $efile); } } return $result; }
/** * Generic handler function for all events - triggers sending of files. * @return boolean */ public function event_handler($eventdata) { global $DB, $CFG; $supportedevents = urkund_supported_events(); if (!in_array($eventdata->eventtype, $supportedevents)) { return true; // Don't need to handle this event. } $plagiarismsettings = $this->get_settings(); if (!$plagiarismsettings) { return true; } $cmid = !empty($eventdata->cm->id) ? $eventdata->cm->id : $eventdata->cmid; $plagiarismvalues = $DB->get_records_menu('plagiarism_urkund_config', array('cm' => $cmid), '', 'name, value'); if (empty($plagiarismvalues['use_urkund'])) { // Urkund not in use for this cm - return. return true; } // Check if the module associated with this event still exists. if (!$DB->record_exists('course_modules', array('id' => $eventdata->cmid))) { return true; } if ($eventdata->eventtype == 'files_done' || $eventdata->eventtype == 'content_done' || $eventdata->eventtype == 'assessable_submitted' && $eventdata->params['submission_editable'] == false) { // Assignment-specific functionality: // This is a 'finalize' event. No files from this event itself, // but need to check if files from previous events need to be submitted for processing. mtrace("finalise"); $result = true; if (isset($plagiarismvalues['urkund_draft_submit']) && $plagiarismvalues['urkund_draft_submit'] == PLAGIARISM_URKUND_DRAFTSUBMIT_FINAL) { // Any files attached to previous events were not submitted. // These files are now finalized, and should be submitted for processing. if ($eventdata->modulename == 'assignment') { // Hack to include filelib so that file_storage class is available. require_once "{$CFG->dirroot}/mod/assignment/lib.php"; // We need to get a list of files attached to this assignment and put them in an array, so that // we can submit each of them for processing. $assignmentbase = new assignment_base($cmid); $submission = $assignmentbase->get_submission($eventdata->userid); $modulecontext = context_module::instance($eventdata->cmid); $fs = get_file_storage(); if ($files = $fs->get_area_files($modulecontext->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) { foreach ($files as $file) { $sendresult = urkund_send_file($cmid, $eventdata->userid, $file, $plagiarismsettings); $result = $result && $sendresult; } } } else { if ($eventdata->modulename == 'assign') { require_once "{$CFG->dirroot}/mod/assign/locallib.php"; require_once "{$CFG->dirroot}/mod/assign/submission/file/locallib.php"; $modulecontext = context_module::instance($eventdata->cmid); $fs = get_file_storage(); if ($files = $fs->get_area_files($modulecontext->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA, $eventdata->itemid, "id", false)) { foreach ($files as $file) { $sendresult = urkund_send_file($cmid, $eventdata->userid, $file, $plagiarismsettings); $result = $result && $sendresult; } } $submission = $DB->get_record('assignsubmission_onlinetext', array('submission' => $eventdata->itemid)); if (!empty($submission)) { $eventdata->content = trim(format_text($submission->onlinetext, $submission->onlineformat, array('context' => $modulecontext))); $file = urkund_create_temp_file($cmid, $eventdata); $sendresult = urkund_send_file($cmid, $eventdata->userid, $file, $plagiarismsettings); $result = $result && $sendresult; unlink($file->filepath); // Delete temp file. } } } } return $result; } if (isset($plagiarismvalues['urkund_draft_submit']) && $plagiarismvalues['urkund_draft_submit'] == PLAGIARISM_URKUND_DRAFTSUBMIT_FINAL) { // Assignment-specific functionality: // Files should only be sent for checking once "finalized". return true; } // Text is attached. $result = true; if (!empty($eventdata->content)) { $file = urkund_create_temp_file($cmid, $eventdata); $sendresult = urkund_send_file($cmid, $eventdata->userid, $file, $plagiarismsettings); $result = $result && $sendresult; unlink($file->filepath); // Delete temp file. } // Normal situation: 1 or more assessable files attached to event, ready to be checked. if (!empty($eventdata->pathnamehashes)) { foreach ($eventdata->pathnamehashes as $hash) { $fs = get_file_storage(); $efile = $fs->get_file_by_hash($hash); if (empty($efile)) { mtrace("nofilefound!"); continue; } else { if ($efile->get_filename() === '.') { // This 'file' is actually a directory - nothing to submit. continue; } } // Check if assign group submission is being used. if ($eventdata->modulename == 'assign') { require_once "{$CFG->dirroot}/mod/assign/locallib.php"; $modulecontext = context_module::instance($eventdata->cmid); $assign = new assign($modulecontext, false, false); if (!empty($assign->get_instance()->teamsubmission)) { $mygroups = groups_get_user_groups($assign->get_course()->id, $eventdata->userid); if (count($mygroups) == 1) { $groupid = reset($mygroups)[0]; // Only users with single groups are supported - otherwise just use the normal userid on this record. // Get all users from this group. $userids = array(); $users = groups_get_members($groupid, 'u.id'); foreach ($users as $u) { $userids[] = $u->id; } // Find the earliest plagiarism record for this cm with any of these users. $sql = 'cm = ? AND userid IN (' . implode(',', $userids) . ')'; $previousfiles = $DB->get_records_select('plagiarism_urkund_files', $sql, array($eventdata->cmid), 'id'); $sanitycheckusers = 10; // Search through this number of users to find a valid previous submission. $i = 0; foreach ($previousfiles as $pf) { if ($pf->userid == $eventdata->userid) { break; // The submission comes from this user so break. } // Sanity Check to make sure the user isn't in multiple groups. $pfgroups = groups_get_user_groups($assign->get_course()->id, $pf->userid); if (count($pfgroups) == 1) { // This user made the first valid submission so use their id when sending the file. $eventdata->userid = $pf->userid; break; } if ($i >= $sanitycheckusers) { // don't cause a massive loop here and break at a sensible limit. break; } $i++; } } } } $sendresult = urkund_send_file($cmid, $eventdata->userid, $efile, $plagiarismsettings); $result = $result && $sendresult; } } return $result; }