/** * generic handler function for all events - triggers sending of files. * @return boolean */ public function event_handler($eventdata) { global $DB, $CFG; $supported_events = compilatio_supported_events(); if (!in_array($eventdata->eventtype, $supported_events)) { 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_compilatio_config', array('cm' => $cmid), '', 'name, value'); if (empty($plagiarismvalues['use_compilatio'])) { // Compilatio 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') { // 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['compilatio_draft_submit']) && $plagiarismvalues['compilatio_draft_submit'] == PLAGIARISM_COMPILATIO_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') { require_once "{$CFG->dirroot}/mod/assignment/lib.php"; // Hack to include filelib so that file_storage class is available // 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 = module_context::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 = compilatio_send_file($cmid, $eventdata->userid, $file, $plagiarismsettings); $result = $result && $sendresult; } } } else { if ($eventdata->modulename == 'assign') { require_once "{$CFG->dirroot}/mod/assign/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 = compilatio_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 = compilatio_create_temp_file($cmid, $eventdata); $sendresult = compilatio_send_file($cmid, $eventdata->userid, $file, $plagiarismsettings); $result = $result && $sendresult; unlink($file->filepath); //Delete temp file. } } } } return $result; } if (isset($plagiarismvalues['compilatio_draft_submit']) && $plagiarismvalues['compilatio_draft_submit'] == PLAGIARISM_COMPILATIO_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 = compilatio_create_temp_file($cmid, $eventdata); $sendresult = compilatio_send_file($cmid, $eventdata->userid, $file, $plagiarismsettings); $result = $result && $sendresult; unlink($file->filepath); } // 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; } } $sendresult = compilatio_send_file($cmid, $eventdata->userid, $efile, $plagiarismsettings); $result = $result && $sendresult; } } 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; }
public function event_handler($eventdata) { global $DB, $CFG; $plagiarismsettings = $this->get_settings(); $cmid = !empty($eventdata->cm->id) ? $eventdata->cm->id : $eventdata->cmid; $plagiarismvalues = $DB->get_records_menu('plagiarism_turnitin_config', array('cm' => $cmid), '', 'name,value'); if (!$plagiarismsettings || empty($plagiarismvalues['use_turnitin'])) { //nothing to do here... move along! return true; } if ($eventdata->eventtype == "mod_created") { return turnitin_update_assignment($plagiarismsettings, $plagiarismvalues, $eventdata, 'create'); } else { if ($eventdata->eventtype == "mod_updated") { return turnitin_update_assignment($plagiarismsettings, $plagiarismvalues, $eventdata, 'update'); } else { if ($eventdata->eventtype == "mod_deleted") { return turnitin_update_assignment($plagiarismsettings, $plagiarismvalues, $eventdata, 'delete'); } else { if ($eventdata->eventtype == "file_uploaded") { // check if the module associated with this event still exists $cm = $DB->get_record('course_modules', array('id' => $eventdata->cmid)); $modulename = $DB->get_field('modules', 'name', array('id' => $cm->module)); if (!$cm) { return true; } // If the assignment has only just been set up, we don't want to try to submit to it, or // we'll get a 1001 error $assignmentstarttime = $DB->get_field('plagiarism_turnitin_config', 'value', array('cm' => $cm->id, 'name' => 'turnitin_dtstart')); if ($assignmentstarttime > time()) { // May not be set up properly - we need to allow for wonky server clocks. mtrace("Warning: assignment start time is too early " . date('Y-m-d H:i:s', $assignmentstarttime) . " cmid:" . $eventdata->cmid . " will delay sending files until next cron"); return false; } if (!empty($eventdata->file) && empty($eventdata->files)) { //single assignment type passes a single file $eventdata->files[] = $eventdata->file; } if (empty($eventdata->files)) { // There are no files attached to this 'fileuploaded' event. // This is a 'finalize' event - assignment-focused functionality mtrace("finalise"); if (isset($plagiarismvalues['plagiarism_draft_submit']) && $plagiarismvalues['plagiarism_draft_submit'] == PLAGIARISM_TII_DRAFTSUBMIT_FINAL) { // Drafts haven't previously been sent // get assignment details, list of draft files and submit to TII. require_once "{$CFG->dirroot}/mod/{$modulename}/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 = get_context_instance(CONTEXT_MODULE, $eventdata->cmid); $fs = get_file_storage(); $result = true; if ($files = $fs->get_area_files($modulecontext->id, 'mod_' . $modulename, 'submission', $submission->id, "timemodified", false)) { foreach ($files as $file) { $fileresult = false; //TODO: need to check if this file has already been sent! - possible that the file was sent before draft submit was set. $pid = plagiarism_update_record($cmid, $eventdata->userid, $file->get_pathnamehash()); if (!empty($pid)) { $fileresult = turnitin_send_file($pid, $plagiarismsettings, $file); } $result = $fileresult && $result; } } return $result; } } // Assignment-module focused functionality: if (isset($plagiarismvalues['plagiarism_draft_submit']) && $plagiarismvalues['plagiarism_draft_submit'] == PLAGIARISM_TII_DRAFTSUBMIT_FINAL) { // Files shouldn't be submitted to TII until 'finalize' file upload event. return true; } // Normal scenario - this is an upload event with one or more attached files // Attached file(s) are to be immediately submitted to TII $result = true; foreach ($eventdata->files as $efile) { $fileresult = false; if ($efile->get_filename() === '.') { // This is a directory - nothing to do. continue; } //hacky way to check file still exists $fs = get_file_storage(); $fileid = $fs->get_file_by_hash($efile->get_pathnamehash()); if (empty($fileid)) { mtrace("nofilefound!"); continue; } //check if this particular file has already been submitted. $pid = plagiarism_update_record($cmid, $eventdata->userid, $efile->get_pathnamehash()); if (!empty($pid)) { $fileresult = turnitin_send_file($pid, $plagiarismsettings, $efile); } else { $fileresult = true; //file already been sent. } $result = $result && $fileresult; } return $result; } else { if ($eventdata->eventtype == "quizattempt") { //get list of essay questions and the users answer in this quiz $sql = "SELECT s.* FROM {question} q, {quiz_question_instances} i, {question_states} s, {question_sessions} qs\n WHERE i.quiz = ? AND i.quiz=q.id AND q.qtype='essay'\n AND s.question = q.id AND qs.questionid= q.id AND qs.newest = s.id AND qs.attemptid = s.attempt AND s.attempt = ?"; $essayquestions = $DB->get_records_sql($sql, array($eventdata->quiz, $eventdata->attempt)); //check dir exists if (!file_exists($CFG->dataroot . "/temp/turnitin")) { if (!file_exists($CFG->dataroot . "/temp")) { mkdir($CFG->dataroot . "/temp", 0700); } mkdir($CFG->dataroot . "/temp/turnitin", 0700); } foreach ($essayquestions as $qid) { //get actual response //create file to send $pid = plagiarism_update_record($cmid, $eventdata->userid, $qid->id); if (!empty($pid)) { $file = new stdclass(); $file->type = "tempturnitin"; $file->filename = $pid . ".txt"; $file->timestamp = $qid->timestamp; $file->filepath = $CFG->dataroot . "/temp/turnitin/" . $pid . ".txt"; $fd = fopen($file->filepath, 'wb'); //create if not exist, write binary fwrite($fd, $qid->answer); fclose($fd); $result = turnitin_send_file($pid, $plagiarismsettings, $file); unlink($file->filepath); //delete temp file. } } return true; } else { return true; //Don't need to handle this event } } } } } }
function crot_event_file_uploaded($eventdata) { global $DB, $CFG; $result = true; //mainly used by assignment finalize - used if you want to handle "submit for marking" events //a file has been uploaded/finalised - submit this to the plagiarism prevention service. // $plagiarismvalues = $DB->get_records_menu('plagiarism_crot_config', array('cm'=>$eventdata->cmid),'','name,value'); $cmid = !empty($eventdata->cm->id) ? $eventdata->cm->id : $eventdata->cmid; $plagiarismvalues = $DB->get_records_menu('plagiarism_crot_config', array('cm' => $cmid), '', 'name,value'); if (empty($plagiarismvalues['crot_use'])) { return $result; } else { //sw 21/02 $cm = $DB->get_record('course_modules', array('id' => $eventdata->cmid)); if (empty($cm)) { $cm = $DB->get_record('course_modules', array('id' => $cmid)); } //sw if (empty($cm)) { return $result; } $modulename = $DB->get_field('modules', 'name', array('id' => $cm->module)); //sw uncommented require_once "{$CFG->dirroot}/mod/{$modulename}/lib.php"; //sw $status_value = array('queue', 'in_processing', 'end_processing'); $modulecontext = get_context_instance(CONTEXT_MODULE, $eventdata->cmid); // put files that were submitted for marking into queue for check up //sw if ($eventdata->modulename == 'assignment') { require_once "{$CFG->dirroot}/mod/assignment/lib.php"; $assignmentbase = new assignment_base($cmid); $submission = $assignmentbase->get_submission($eventdata->userid); $fs = get_file_storage(); if ($files = $fs->get_area_files($modulecontext->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) { foreach ($files as $file) { if ($file->get_filename() === '.') { // This 'file' is actually a directory - nothing to submit. continue; } $newelement = new stdclass(); $newelement->file_id = $file->get_id(); $newelement->path = $file->get_contenthash(); $newelement->status = $status_value[0]; $newelement->time = time(); $newelement->cm = $eventdata->cmid; $newelement->courseid = $eventdata->courseid; $result = $DB->insert_record('plagiarism_crot_files', $newelement); echo "\nfile " . $file->get_filename() . " was queued up for plagiarism detection service\n"; } } } else { if ($eventdata->modulename == 'assign') { //sw foreach ($eventdata->pathnamehashes as $hash) { $fs = get_file_storage(); $file = $fs->get_file_by_hash($hash); if (empty($file) || $file->get_filename() === '.') { // This 'file' is actually a directory - nothing to submit. continue; } $newelement = new stdclass(); $newelement->file_id = $file->get_id(); $newelement->path = $file->get_contenthash(); $newelement->status = $status_value[0]; $newelement->time = time(); $newelement->cm = $eventdata->cmid; $newelement->courseid = $eventdata->courseid; $result = $DB->insert_record('plagiarism_crot_files', $newelement); echo "\nfile " . $file->get_filename() . " was queued up for plagiarism detection service\n"; } //ws } //end assign } ///ws return $result; } }