/**
  *
  * @global type $CFG
  * @param type $linkarray
  * @return type
  */
 public function get_links($linkarray)
 {
     global $CFG, $DB, $OUTPUT, $PAGE, $USER;
     // Don't show links for certain file types as they won't have been submitted to Turnitin.
     if (!empty($linkarray["file"])) {
         $file = $linkarray["file"];
         $filearea = $file->get_filearea();
         $nonsubmittingareas = array("feedback_files", "introattachment");
         if (in_array($filearea, $nonsubmittingareas)) {
             return;
         }
     }
     // Set static variables.
     static $cm;
     static $forum;
     if (empty($cm)) {
         $cm = get_coursemodule_from_id('', $linkarray["cmid"]);
         if ($cm->modname == 'forum') {
             if (!($forum = $DB->get_record("forum", array("id" => $cm->instance)))) {
                 print_error('invalidforumid', 'forum');
             }
         }
     }
     static $plagiarismsettings;
     if (empty($plagiarismsettings)) {
         $plagiarismsettings = $this->get_settings($linkarray["cmid"]);
     }
     // Exit if Turnitin is not being used for this module.
     if (empty($plagiarismsettings['use_turnitin'])) {
         return;
     }
     static $config;
     if (empty($config)) {
         $config = turnitintooltwo_admin_config();
     }
     static $moduletiienabled;
     if (empty($moduletiienabled)) {
         $moduletiienabled = $this->get_config_settings('mod_' . $cm->modname);
     }
     // Exit if Turnitin is not being used for this activity type.
     if (empty($moduletiienabled)) {
         return;
     }
     static $moduledata;
     if (empty($moduledata)) {
         $moduledata = $DB->get_record($cm->modname, array('id' => $cm->instance));
     }
     static $context;
     if (empty($context)) {
         $context = context_course::instance($cm->course);
     }
     static $coursedata;
     if (empty($coursedata)) {
         $coursedata = $this->get_course_data($cm->id, $cm->course);
     }
     // Create module object
     $moduleclass = "turnitin_" . $cm->modname;
     $moduleobject = new $moduleclass();
     // Work out if logged in user is a tutor on this module.
     static $istutor;
     if (empty($istutor)) {
         $istutor = $moduleobject->is_tutor($context);
     }
     // Define the timestamp for updating Peermark Assignments.
     if (empty($_SESSION["updated_pm"][$cm->id]) && $config->enablepeermark) {
         $_SESSION["updated_pm"][$cm->id] = time() - 60 * 5;
     }
     $output = "";
     // If a text submission has been made, we can only display links for current attempts so don't show links previous attempts.
     // This will need to be reworked when linkarray contains submission id.
     static $contentdisplayed;
     if ($cm->modname == 'assign' && !empty($linkarray["content"]) && $contentdisplayed == true) {
         return $output;
     }
     if ((!empty($linkarray["file"]) || !empty($linkarray["content"])) && !empty($linkarray["cmid"])) {
         $this->load_page_components();
         $identifier = '';
         $itemid = 0;
         // Get File or Content information.
         $submittinguser = $linkarray['userid'];
         if (!empty($linkarray["file"])) {
             $identifier = $file->get_pathnamehash();
             $itemid = $file->get_itemid();
             $submissiontype = 'file';
         } else {
             if (!empty($linkarray["content"])) {
                 // Get turnitin text content details.
                 $submissiontype = $cm->modname == "forum" ? 'forum_post' : 'text_content';
                 $content = $moduleobject->set_content($linkarray, $cm);
                 $identifier = sha1($content);
             }
         }
         // Group submissions where all students have to submit sets userid to 0;
         if ($linkarray['userid'] == 0 && !$istutor) {
             $linkarray['userid'] = $USER->id;
         }
         // Get correct user id that submission is for rather than who submitted, this only affects file submissions
         // post Moodle 2.7 which is problematic as teachers can submit on behalf of students.
         $author = $linkarray['userid'];
         if ($itemid != 0) {
             $author = $moduleobject->get_author($itemid);
             $linkarray['userid'] = !empty($author) ? $author : $linkarray['userid'];
         }
         $output .= $OUTPUT->box_start('tii_links_container');
         // Show the EULA for a student if necessary.
         if ($linkarray["userid"] == $USER->id && empty($plagiarismfile->externalid)) {
             $eula = "";
             static $userid;
             if (empty($userid)) {
                 $userid = 0;
             }
             // Condition added to test for Moodle 2.7 as it calls this function twice.
             if ($CFG->branch >= 27 || $userid != $linkarray["userid"]) {
                 // Show EULA if necessary and we have a connection to Turnitin.
                 static $eulashown;
                 if (empty($eulashown)) {
                     $eulashown = false;
                 }
                 $user = new turnitintooltwo_user($USER->id, "Learner");
                 $success = $user->join_user_to_class($coursedata->turnitin_cid);
                 // $success is false if there is no Turnitin connection and null if user has previously been enrolled.
                 if ((is_null($success) || $success === true) && $eulashown == false) {
                     $eulaaccepted = $user->user_agreement_accepted == 0 ? $user->get_accepted_user_agreement() : $user->user_agreement_accepted;
                     $userid = $linkarray["userid"];
                     if ($eulaaccepted != 1) {
                         $eula_link = html_writer::link($CFG->wwwroot . '/plagiarism/turnitin/extras.php?cmid=' . $linkarray["cmid"] . '&cmd=useragreement&view_context=box_solid', $OUTPUT->pix_icon('tiiIcon', '', 'plagiarism_turnitin', array('class' => 'icon_size_large')) . '<br/>' . get_string('turnitinppulapost', 'plagiarism_turnitin'), array("class" => "pp_turnitin_eula_link"));
                         $eula = html_writer::tag('div', $eula_link, array('class' => 'pp_turnitin_ula js_required', 'data-userid' => $user->id));
                     }
                     // Show EULA launcher and form placeholder.
                     if (!empty($eula)) {
                         $output .= $eula;
                         $turnitincomms = new turnitin_comms();
                         $turnitincall = $turnitincomms->initialise_api();
                         $customdata = array("disable_form_change_checker" => true, "elements" => array(array('html', $OUTPUT->box('', '', 'useragreement_inputs'))));
                         $eulaform = new turnitintooltwo_form($turnitincall->getApiBaseUrl() . TiiLTI::EULAENDPOINT, $customdata, 'POST', $target = 'eulaWindow', array('id' => 'eula_launch'));
                         $output .= $OUTPUT->box($eulaform->display(), 'tii_useragreement_form', 'useragreement_form');
                         $eulashown = true;
                     }
                 }
             }
         }
         // Check whether submission is a group submission - only applicable to assignment module.
         // If it's a group submission then other users in the group should be able to see the originality score
         // They can not open the DV though.
         $submissionusers = array($linkarray["userid"]);
         if ($cm->modname == "assign") {
             if ($moduledata->teamsubmission) {
                 $assignment = new assign($context, $cm, null);
                 if ($group = $assignment->get_submission_group($linkarray["userid"])) {
                     $users = groups_get_members($group->id);
                     $submissionusers = array_keys($users);
                 }
             }
         }
         // Group originality score for Coursework module
         // Check whether submission is a group submission
         // If it's a group submission then other users in the group should be able to see the originality score
         // They can not open the DV though.
         if ($cm->modname == "coursework") {
             if ($moduledata->use_groups) {
                 $coursework = new \mod_coursework\models\coursework($moduledata->id);
                 $user = $DB->get_record('user', array('id' => $linkarray["userid"]));
                 $user = mod_coursework\models\user::find($user);
                 if ($group = $coursework->get_student_group($user)) {
                     $users = groups_get_members($group->id);
                     $submissionusers = array_keys($users);
                 }
             }
         }
         // Proceed to displaying links for submissions.
         if ($istutor || in_array($USER->id, $submissionusers)) {
             // Prevent text content links being displayed for previous attempts as we have no way of getting the data.
             if (!empty($linkarray["content"]) && $linkarray["userid"] == $USER->id) {
                 $contentdisplayed = true;
             }
             // Get turnitin file details
             $plagiarismfiles = $DB->get_records('plagiarism_turnitin_files', array('userid' => $linkarray["userid"], 'cm' => $linkarray["cmid"], 'identifier' => $identifier), 'lastmodified DESC', '*', 0, 1);
             $plagiarismfile = current($plagiarismfiles);
             // Populate gradeitem query
             $gradeitemqueryarray = array('iteminstance' => $cm->instance, 'itemmodule' => $cm->modname, 'courseid' => $cm->course, 'itemnumber' => 0);
             // Get grade item and work out whether grades have been released for viewing.
             $gradesreleased = true;
             if ($gradeitem = $DB->get_record('grade_items', $gradeitemqueryarray)) {
                 switch ($gradeitem->hidden) {
                     case 1:
                         $gradesreleased = false;
                         break;
                     default:
                         $gradesreleased = $gradeitem->hidden >= time() ? false : true;
                         break;
                 }
                 // Give Marking workflow higher priority than gradebook hidden date.
                 if ($cm->modname == 'assign' && !empty($moduledata->markingworkflow)) {
                     $gradesreleased = $DB->record_exists('assign_user_flags', array('userid' => $linkarray["userid"], 'assignment' => $cm->instance, 'workflowstate' => 'released'));
                 }
             }
             $currentgradequery = false;
             if ($gradeitem) {
                 $currentgradequery = $moduleobject->get_current_gradequery($linkarray["userid"], $cm->instance, $gradeitem->id);
             }
             // Display links to OR, GradeMark and show relevant errors.
             if ($plagiarismfile) {
                 if ($plagiarismfile->statuscode == 'success') {
                     if ($istutor || $linkarray["userid"] == $USER->id) {
                         $output .= html_writer::tag('div', $OUTPUT->pix_icon('tiiIcon', get_string('turnitinid', 'plagiarism_turnitin') . ': ' . $plagiarismfile->externalid, 'plagiarism_turnitin', array('class' => 'icon_size')) . get_string('turnitinid', 'plagiarism_turnitin') . ': ' . $plagiarismfile->externalid, array('class' => 'turnitin_status'));
                     }
                     // Show Originality Report score and link.
                     if (($istutor || in_array($USER->id, $submissionusers) && $plagiarismsettings["plagiarism_show_student_report"]) && ((is_null($plagiarismfile->orcapable) || $plagiarismfile->orcapable == 1) && !is_null($plagiarismfile->similarityscore))) {
                         // This class is applied so that only the user who submitted or a tutor can open the DV.
                         $useropenclass = $USER->id == $linkarray["userid"] || $istutor ? 'pp_origreport_open' : '';
                         $output .= $OUTPUT->box_start('row_score pp_origreport ' . $useropenclass . ' origreport_' . $plagiarismfile->externalid . '_' . $linkarray["cmid"], $CFG->wwwroot . '/plagiarism/turnitin/extras.php?cmid=' . $linkarray["cmid"]);
                         // Show score.
                         if ($plagiarismfile->statuscode == "pending") {
                             $output .= html_writer::tag('div', '&nbsp;', array('title' => get_string('pending', 'plagiarism_turnitin'), 'class' => 'tii_tooltip origreport_score score_colour score_colour_'));
                         } else {
                             // Put EN flag if translated matching is on and that is the score used.
                             $transmatch = $plagiarismfile->transmatch == 1 ? ' EN' : '';
                             if (is_null($plagiarismfile->similarityscore)) {
                                 $score = '&nbsp;';
                                 $titlescore = get_string('pending', 'plagiarism_turnitin');
                                 $class = 'score_colour_';
                             } else {
                                 $score = $plagiarismfile->similarityscore . '%';
                                 $titlescore = $plagiarismfile->similarityscore . '% ' . get_string('similarity', 'plagiarism_turnitin');
                                 $class = 'score_colour_' . round($plagiarismfile->similarityscore, -1);
                             }
                             $output .= html_writer::tag('div', $score . $transmatch, array('title' => $titlescore, 'class' => 'tii_tooltip origreport_score score_colour ' . $class));
                         }
                         // Put in div placeholder for DV launch form.
                         $output .= $OUTPUT->box('', 'launch_form origreport_form_' . $plagiarismfile->externalid);
                         // Add url for launching DV from Forum post.
                         if ($cm->modname == 'forum') {
                             $output .= $OUTPUT->box($CFG->wwwroot . '/plagiarism/turnitin/extras.php?cmid=' . $linkarray["cmid"], 'origreport_forum_launch origreport_forum_launch_' . $plagiarismfile->externalid);
                         }
                         $output .= $OUTPUT->box_end(true);
                     }
                     if ($plagiarismfile->orcapable == 0 && !is_null($plagiarismfile->orcapable)) {
                         // This class is applied so that only the user who submitted or a tutor can open the DV.
                         $useropenclass = $USER->id == $linkarray["userid"] || $istutor ? 'pp_origreport_open' : '';
                         $output .= $OUTPUT->box_start('row_score pp_origreport ' . $useropenclass, '');
                         $output .= html_writer::tag('div', 'x', array('title' => get_string('notorcapable', 'plagiarism_turnitin'), 'class' => 'tii_tooltip score_colour score_colour_ score_no_orcapable'));
                         $output .= $OUTPUT->box_end(true);
                     }
                     //Check if blind marking is on and revealidentities is not set yet.
                     $blindon = !empty($moduledata->blindmarking) && empty($moduledata->revealidentities);
                     // Can grade and feedback be released to this student yet?
                     $released = !$blindon && ($gradesreleased && (!empty($plagiarismfile->gm_feedback) || isset($currentgradequery->grade)));
                     // Show link to open grademark.
                     if ($config->usegrademark && ($istutor || $linkarray["userid"] == $USER->id && $released) && !empty($gradeitem)) {
                         // Output grademark icon.
                         $output .= $OUTPUT->box_start('grade_icon', '');
                         $output .= html_writer::tag('div', $OUTPUT->pix_icon('icon-edit', get_string('grademark', 'plagiarism_turnitin'), 'plagiarism_turnitin'), array('title' => get_string('grademark', 'plagiarism_turnitin'), 'class' => 'pp_grademark_open tii_tooltip grademark_' . $plagiarismfile->externalid . '_' . $linkarray["cmid"], 'id' => $CFG->wwwroot . '/plagiarism/turnitin/extras.php?cmid=' . $linkarray["cmid"]));
                         // Add url for launching DV from Forum post.
                         if ($cm->modname == 'forum') {
                             $output .= $OUTPUT->box($CFG->wwwroot . '/plagiarism/turnitin/extras.php?cmid=' . $linkarray["cmid"], 'grademark_forum_launch grademark_forum_launch_' . $plagiarismfile->externalid);
                         }
                         // Put in div placeholder for DV launch form.
                         $output .= $OUTPUT->box('', 'launch_form grademark_form_' . $plagiarismfile->externalid);
                         $output .= $OUTPUT->box_end(true);
                     }
                     // Indicate whether student has viewed the feedback.
                     if ($istutor) {
                         if (isset($plagiarismfile->externalid)) {
                             $studentread = !empty($plagiarismfile->student_read) ? $plagiarismfile->student_read : 0;
                             if ($studentread > 0) {
                                 $output .= $OUTPUT->pix_icon('icon-student-read', get_string('student_read', 'plagiarism_turnitin') . ' ' . userdate($studentread), 'plagiarism_turnitin', array("class" => "student_read_icon"));
                             } else {
                                 $output .= $OUTPUT->pix_icon('icon-dot', get_string('student_notread', 'plagiarism_turnitin'), 'plagiarism_turnitin', array("class" => "student_read_icon"));
                             }
                         } else {
                             $output .= "--";
                         }
                     }
                     // Show link to view rubric for student.
                     if (!$istutor && $config->usegrademark && !empty($plagiarismsettings["plagiarism_rubric"])) {
                         // Update assignment in case rubric is not stored in Turnitin yet.
                         $tiiassignment = $this->sync_tii_assignment($cm, $coursedata->turnitin_cid);
                         $rubricviewlink = html_writer::tag('div', html_writer::link($CFG->wwwroot . '/plagiarism/turnitin/ajax.php?cmid=' . $cm->id . '&action=rubricview&view_context=box', '&nbsp;', array('class' => 'tii_tooltip rubric_view_pp_launch', 'id' => 'rubric_view_launch', 'title' => get_string('launchrubricview', 'plagiarism_turnitin'))) . html_writer::tag('span', '', array('class' => 'launch_form', 'id' => 'rubric_view_form')), array('class' => 'row_rubric_view'));
                         $output .= $rubricviewlink;
                     }
                     if ($config->enablepeermark) {
                         // If this module is already on Turnitin then refresh and get Peermark Assignments.
                         if (!empty($plagiarismsettings['turnitin_assignid'])) {
                             if ($_SESSION["updated_pm"][$cm->id] <= time() - 60 * 2) {
                                 $this->refresh_peermark_assignments($cm, $plagiarismsettings['turnitin_assignid']);
                                 $turnitintooltwoassignment = new turnitintooltwo_assignment($cm->instance, '', 'PP');
                                 $_SESSION["peermark_assignments"][$cm->id] = $turnitintooltwoassignment->get_peermark_assignments($plagiarismsettings['turnitin_assignid']);
                                 $_SESSION["updated_pm"][$cm->id] = time();
                             }
                             // Determine if we have any active Peermark Assignments.
                             static $peermarksactive;
                             if (!isset($peermarksactive)) {
                                 $peermarksactive = false;
                                 foreach ($_SESSION["peermark_assignments"][$cm->id] as $peermarkassignment) {
                                     if (time() > $peermarkassignment->dtstart) {
                                         $peermarksactive = true;
                                         break;
                                     }
                                 }
                             }
                             // Show Peermark Reviews link.
                             if ($istutor && count($_SESSION["peermark_assignments"][$cm->id]) > 0 || !$istutor && $peermarksactive) {
                                 $peermarkreviewslink = $OUTPUT->box_start('row_peermark_reviews', '');
                                 $peermarkreviewslink .= html_writer::link($CFG->wwwroot . '/plagiarism/turnitin/ajax.php?cmid=' . $cm->id . '&action=peermarkreviews&view_context=box', '', array('title' => get_string('launchpeermarkreviews', 'plagiarism_turnitin'), 'class' => 'peermark_reviews_pp_launch tii_tooltip'));
                                 $peermarkreviewslink .= html_writer::tag('span', '', array('class' => 'launch_form', 'id' => 'peermark_reviews_form'));
                                 $peermarkreviewslink .= $OUTPUT->box_end(true);
                                 $output .= $peermarkreviewslink;
                             }
                         }
                     }
                 } else {
                     if ($plagiarismfile->statuscode == 'error') {
                         // Deal with legacy error issues.
                         $errorcode = isset($plagiarismfile->errorcode) ? $plagiarismfile->errorcode : 0;
                         if ($errorcode == 0 && $submissiontype == 'file') {
                             if ($file->get_filesize() > TURNITINTOOLTWO_MAX_FILE_UPLOAD_SIZE) {
                                 $errorcode = 2;
                             }
                         }
                         // Show error message if there is one.
                         if ($errorcode == 0) {
                             $langstring = $istutor ? 'ppsubmissionerrorseelogs' : 'ppsubmissionerrorstudent';
                             $errorstring = isset($plagiarismfile->errormsg) ? get_string($langstring, 'plagiarism_turnitin') : $plagiarismfile->errormsg;
                         } else {
                             $errorstring = get_string('errorcode' . $plagiarismfile->errorcode, 'plagiarism_turnitin', display_size(TURNITINTOOLTWO_MAX_FILE_UPLOAD_SIZE));
                         }
                         $erroricon = html_writer::tag('div', $OUTPUT->pix_icon('x-red', $errorstring, 'plagiarism_turnitin'), array('title' => $errorstring, 'class' => 'tii_tooltip tii_error_icon'));
                         // Attach error text or resubmit link after icon depending on whether user is a student/teacher.
                         // Don't attach resubmit link if the user has not accepted the EULA.
                         if (!$istutor) {
                             $output .= html_writer::tag('div', $erroricon . ' ' . $errorstring, array('class' => 'warning clear'));
                         } else {
                             if ($errorcode == 3) {
                                 $output .= html_writer::tag('div', $erroricon, array('class' => 'clear'));
                             } else {
                                 $output .= html_writer::tag('div', $erroricon . ' ' . get_string('resubmittoturnitin', 'plagiarism_turnitin'), array('class' => 'clear pp_resubmit_link', 'id' => 'pp_resubmit_' . $plagiarismfile->id));
                                 $output .= html_writer::tag('div', $OUTPUT->pix_icon('loading', $errorstring, 'plagiarism_turnitin') . ' ' . get_string('resubmitting', 'plagiarism_turnitin'), array('class' => 'pp_resubmitting hidden'));
                                 // Pending status for after resubmission.
                                 $statusstr = get_string('turnitinstatus', 'plagiarism_turnitin') . ': ' . get_string('pending', 'plagiarism_turnitin');
                                 $output .= html_writer::tag('div', $OUTPUT->pix_icon('tiiIcon', $statusstr, 'plagiarism_turnitin', array('class' => 'icon_size')) . $statusstr, array('class' => 'turnitin_status hidden'));
                                 // Show hidden data for potential forum post resubmissions
                                 if ($submissiontype == 'forum_post' && !empty($linkarray["content"])) {
                                     $output .= html_writer::tag('div', $linkarray["content"], array('class' => 'hidden', 'id' => 'content_' . $plagiarismfile->id));
                                 }
                                 if ($cm->modname == 'forum') {
                                     // Get forum data from the query string as we'll need this to recreate submission event.
                                     $querystrid = optional_param('id', 0, PARAM_INT);
                                     $discussionid = optional_param('d', 0, PARAM_INT);
                                     $reply = optional_param('reply', 0, PARAM_INT);
                                     $edit = optional_param('edit', 0, PARAM_INT);
                                     $delete = optional_param('delete', 0, PARAM_INT);
                                     $output .= html_writer::tag('div', $querystrid . '_' . $discussionid . '_' . $reply . '_' . $edit . '_' . $delete, array('class' => 'hidden', 'id' => 'forumdata_' . $plagiarismfile->id));
                                 }
                             }
                         }
                     } else {
                         if ($plagiarismfile->statuscode == 'deleted') {
                             $errorcode = isset($plagiarismfile->errorcode) ? $plagiarismfile->errorcode : 0;
                             if ($errorcode == 0) {
                                 $langstring = $istutor ? 'ppsubmissionerrorseelogs' : 'ppsubmissionerrorstudent';
                                 $errorstring = isset($plagiarismfile->errormsg) ? get_string($langstring, 'plagiarism_turnitin') : $plagiarismfile->errormsg;
                             } else {
                                 $errorstring = get_string('errorcode' . $plagiarismfile->errorcode, 'plagiarism_turnitin', display_size(TURNITINTOOLTWO_MAX_FILE_UPLOAD_SIZE));
                             }
                             $statusstr = get_string('turnitinstatus', 'plagiarism_turnitin') . ': ' . get_string('deleted', 'plagiarism_turnitin') . '<br />';
                             $statusstr .= get_string('because', 'plagiarism_turnitin') . '<br />"' . $errorstring . '"';
                             $output .= html_writer::tag('div', $OUTPUT->pix_icon('tiiIcon', $statusstr, 'plagiarism_turnitin', array('class' => 'icon_size')) . $statusstr, array('class' => 'turnitin_status'));
                         } else {
                             $statusstr = get_string('turnitinstatus', 'plagiarism_turnitin') . ': ' . get_string('pending', 'plagiarism_turnitin');
                             $output .= html_writer::tag('div', $OUTPUT->pix_icon('tiiIcon', $statusstr, 'plagiarism_turnitin', array('class' => 'icon_size')) . $statusstr, array('class' => 'turnitin_status'));
                         }
                     }
                 }
             } else {
                 // Add Error if the user has not accepted EULA for submissions made before instant submission was removed.
                 $eulaerror = "";
                 if ($linkarray["userid"] != $USER->id && $submittinguser == $author && $istutor) {
                     // There is a moodle plagiarism bug where get_links is called twice, the first loop is incorrect and is killing
                     // this functionality. Have to check that user exists here first else there will be a fatal error.
                     if ($mdl_user = $DB->get_record('user', array('id' => $linkarray["userid"]))) {
                         // We need to check for security that the user is actually on the course.
                         if ($moduleobject->user_enrolled_on_course($context, $linkarray["userid"])) {
                             $user = new turnitintooltwo_user($linkarray["userid"], "Learner");
                             if ($user->user_agreement_accepted != 1) {
                                 $erroricon = html_writer::tag('div', $OUTPUT->pix_icon('doc-x-grey', get_string('errorcode3', 'plagiarism_turnitin'), 'plagiarism_turnitin'), array('title' => get_string('errorcode3', 'plagiarism_turnitin'), 'class' => 'tii_tooltip tii_error_icon'));
                                 $eulaerror = html_writer::tag('div', $erroricon, array('class' => 'clear'));
                             }
                         }
                     }
                 }
                 // Show Turnitin Pending status or EULA error.
                 if (!empty($eulaerror)) {
                     $output .= $eulaerror;
                 } else {
                     $statusstr = get_string('turnitinstatus', 'plagiarism_turnitin') . ': ' . get_string('pending', 'plagiarism_turnitin');
                     $output .= html_writer::tag('div', $OUTPUT->pix_icon('tiiIcon', $statusstr, 'plagiarism_turnitin', array('class' => 'icon_size')) . $statusstr, array('class' => 'turnitin_status'));
                 }
             }
             $output .= html_writer::tag('div', '', array('class' => 'clear'));
         }
         $output .= $OUTPUT->box_end(true);
     }
     // This comment is here as it is useful for product support.
     $plagiarismsettings = $this->get_settings($cm->id);
     $turnitinassignid = empty($plagiarismsettings['turnitin_assignid']) ? '' : $plagiarismsettings['turnitin_assignid'];
     $output .= html_writer::tag('span', '<!-- Turnitin Plagiarism plugin Version: ' . get_config('plagiarism_turnitin', 'version') . ' Course ID: ' . $coursedata->turnitin_cid . ' TII assignment ID: ' . $turnitinassignid . ' -->');
     return $output;
 }
Esempio n. 2
0
 /**
  *
  * @global type $CFG
  * @param type $linkarray
  * @return type
  */
 public function get_links($linkarray)
 {
     global $CFG, $DB, $OUTPUT, $PAGE, $USER;
     // Don't submit certain file types to Turnitin.
     if (!empty($linkarray["file"])) {
         $file = $linkarray["file"];
         $filearea = $file->get_filearea();
         $nonsubmittingareas = array("feedback_files", "introattachment");
         if (in_array($filearea, $nonsubmittingareas)) {
             return;
         }
     }
     // Set static variables.
     static $cm;
     static $forum;
     if (empty($cm)) {
         $cm = get_coursemodule_from_id('', $linkarray["cmid"]);
         if ($cm->modname == 'forum') {
             if (!($forum = $DB->get_record("forum", array("id" => $cm->instance)))) {
                 print_error('invalidforumid', 'forum');
             }
         }
     }
     static $plagiarismsettings;
     if (empty($plagiarismsettings)) {
         $plagiarismsettings = $this->get_settings($linkarray["cmid"]);
         if ($cm->modname == 'assign') {
             $plagiarismsettings["plagiarism_draft_submit"] = isset($plagiarismsettings["plagiarism_draft_submit"]) ? $plagiarismsettings["plagiarism_draft_submit"] : 0;
         }
     }
     // Exit if Turnitin is not being used for this module.
     if (empty($plagiarismsettings['use_turnitin'])) {
         return;
     }
     static $config;
     if (empty($config)) {
         $config = turnitintooltwo_admin_config();
     }
     static $moduletiienabled;
     if (empty($moduletiienabled)) {
         $moduletiienabled = $this->get_config_settings('mod_' . $cm->modname);
     }
     // Exit if Turnitin is not being used for this activity type.
     if (empty($moduletiienabled)) {
         return;
     }
     static $moduledata;
     if (empty($moduledata)) {
         $moduledata = $DB->get_record($cm->modname, array('id' => $cm->instance));
     }
     static $context;
     if (empty($context)) {
         $context = context_course::instance($cm->course);
     }
     static $coursedata;
     if (empty($coursedata)) {
         // Create the course/class in Turnitin if it doesn't already exist.
         $coursedata = turnitintooltwo_assignment::get_course_data($cm->course, 'PP');
         if (empty($coursedata->turnitin_cid)) {
             // Course may existed in a previous incarnation of this plugin.
             // Get this and save it in courses table if so.
             if ($turnitincid = $this->get_previous_course_id($cm)) {
                 $coursedata->turnitin_cid = $turnitincid;
                 $coursedata = $this->migrate_previous_course($coursedata, $turnitincid);
             } else {
                 // Otherwise create new course in Turnitin.
                 $tiicoursedata = $this->create_tii_course($cm, $coursedata);
                 $coursedata->turnitin_cid = $tiicoursedata->turnitin_cid;
                 $coursedata->turnitin_ctl = $tiicoursedata->turnitin_ctl;
             }
         }
     }
     // Work out if logged in user is a tutor on this module.
     static $istutor;
     switch ($cm->modname) {
         case "forum":
         case "workshop":
             if (empty($istutor)) {
                 $istutor = has_capability('plagiarism/turnitin:viewfullreport', $context);
             }
             break;
         default:
             if (empty($istutor)) {
                 $istutor = has_capability('mod/' . $cm->modname . ':grade', $context);
             }
             break;
     }
     // Define the timestamp for updating Peermark Assignments.
     if (empty($_SESSION["updated_pm"][$cm->id]) && $config->enablepeermark) {
         $_SESSION["updated_pm"][$cm->id] = time() - 60 * 5;
     }
     $output = "";
     // If a text submission has been made, we can only display links for current attempts so don't show links previous attempts.
     // This will need to be reworked when linkarray contains submission id.
     static $contentdisplayed;
     if ($cm->modname == 'assign' && !empty($linkarray["content"]) && $contentdisplayed == true) {
         return $output;
     }
     if ((!empty($linkarray["file"]) || !empty($linkarray["content"])) && !empty($linkarray["cmid"])) {
         // Include Javascript.
         $jsurl = new moodle_url('/mod/turnitintooltwo/jquery/jquery-1.8.2.min.js');
         $PAGE->requires->js($jsurl);
         $jsurl = new moodle_url('/mod/turnitintooltwo/jquery/turnitintooltwo.js');
         $PAGE->requires->js($jsurl);
         $jsurl = new moodle_url('/mod/turnitintooltwo/jquery/plagiarism_plugin.js');
         $PAGE->requires->js($jsurl);
         $jsurl = new moodle_url('/mod/turnitintooltwo/jquery/jquery.colorbox.js');
         $PAGE->requires->js($jsurl);
         $jsurl = new moodle_url('/mod/turnitintooltwo/jquery/jquery.tooltipster.js');
         $PAGE->requires->js($jsurl);
         $PAGE->requires->string_for_js('closebutton', 'turnitintooltwo');
         // Initialise vars for working out whether we are submitting.
         $submitting = false;
         $submission_status = true;
         $identifier = '';
         // Get Assignment submission data and account for draft submissions which may not be submitted
         if ($cm->modname == 'assign') {
             if (!empty($linkarray["file"])) {
                 $submission = $DB->get_record('assign_submission', array('userid' => $linkarray["userid"], 'assignment' => $moduledata->id, 'id' => $file->get_itemid()));
             } else {
                 // This will need to be reworked when linkarray contains submission id.
                 $submissions = $DB->get_records('assign_submission', array('userid' => $linkarray["userid"], 'assignment' => $moduledata->id));
                 $submission = end($submissions);
             }
             if ($submission) {
                 $submission_status = $submission->status == "submitted" || $moduledata->submissiondrafts == 1 && $plagiarismsettings["plagiarism_draft_submit"] == 0 ? true : false;
             }
         }
         // Check whether a user's submission needs to be sent to Turnitin via Ajax.
         // Get File or Content information, for content only submit if it has been modified.
         $itemid = 0;
         if (!empty($linkarray["file"])) {
             $identifier = $file->get_pathnamehash();
             $itemid = $file->get_itemid();
             $submissiontype = 'file';
             $submitting = $submission_status ? true : false;
             // Get plagiarism file info to check if file was previously submitted and has been modified.
             $typefield = $CFG->dbtype == "oci" ? " to_char(submissiontype) " : " submissiontype ";
             $plagiarismfiles = $DB->get_records_select('plagiarism_turnitin_files', " userid = ? AND cm = ? AND identifier = ? AND (" . $typefield . " = ? OR " . $typefield . " IS NULL )", array($linkarray["userid"], $linkarray["cmid"], $identifier, $submissiontype, ''));
             $plagiarismfile = end($plagiarismfiles);
             if (!empty($plagiarismfile)) {
                 $submitting = $file->get_timemodified() > $plagiarismfile->lastmodified ? $submitting : false;
             }
         } else {
             if (!empty($linkarray["content"])) {
                 // Get turnitin text content details.
                 $submissiontype = $cm->modname == "forum" ? 'forum_post' : 'text_content';
                 switch ($cm->modname) {
                     case 'assign':
                         $moodletextsubmission = $DB->get_record('assignsubmission_onlinetext', array('submission' => $submission->id), 'onlinetext');
                         if (empty($moodletextsubmission)) {
                             $content = '';
                             $submission_status = false;
                         } else {
                             $content = $moodletextsubmission->onlinetext;
                         }
                         break;
                     case 'workshop':
                         $submission = $DB->get_record('workshop_submissions', array('authorid' => $linkarray["userid"], 'workshopid' => $moduledata->id));
                         $content = $linkarray["content"];
                         break;
                     case 'forum':
                         static $discussionid;
                         // Use query string id to check whether we are on forum home page.
                         $querystrid = optional_param('id', 0, PARAM_INT);
                         // Work out the discussion id from query string.
                         if (empty($discussionid)) {
                             $discussionid = optional_param('d', 0, PARAM_INT);
                         }
                         if (empty($discussionid)) {
                             $reply = optional_param('reply', 0, PARAM_INT);
                             $edit = optional_param('edit', 0, PARAM_INT);
                             $delete = optional_param('delete', 0, PARAM_INT);
                             $parent = '';
                             if ($reply != 0) {
                                 $parent = forum_get_post_full($reply);
                             } else {
                                 if ($edit != 0) {
                                     $parent = forum_get_post_full($edit);
                                 } else {
                                     if ($delete != 0) {
                                         $parent = forum_get_post_full($delete);
                                     }
                                 }
                             }
                             if (!empty($parent)) {
                                 $discussionid = $parent->discussion;
                             }
                         }
                         // Some forum types don't pass in certain values on main forum page.
                         if ((empty($discussionid) || $querystrid != 0) && ($forum->type == 'blog' || $forum->type == 'single')) {
                             if (!($discussion = $DB->get_record_sql('SELECT FD.id
                                                                 FROM {forum_posts} FP JOIN {forum_discussions} FD
                                                                 ON FP.discussion = FD.id
                                                                 WHERE FD.forum = ? AND FD.course = ?
                                                                 AND FP.userid = ? AND FP.message LIKE ? ', array($forum->id, $forum->course, $linkarray["userid"], $linkarray["content"])))) {
                                 print_error('notpartofdiscussion', 'forum');
                             }
                             $discussionid = $discussion->id;
                         }
                         $submission = $DB->get_record_select('forum_posts', " userid = ? AND message LIKE ? AND discussion = ? ", array($linkarray["userid"], $linkarray["content"], $discussionid));
                         $itemid = $submission->id;
                         $submission->timemodified = $submission->modified;
                         $content = $linkarray["content"];
                         break;
                 }
                 // Get plagiarism file info.
                 $identifier = sha1($content);
                 $typefield = $CFG->dbtype == "oci" ? " to_char(submissiontype) " : " submissiontype ";
                 switch ($cm->modname) {
                     case 'assign':
                     case 'workshop':
                         $plagiarismfile = $DB->get_record_select('plagiarism_turnitin_files', " userid = ? AND cm = ? AND " . $typefield . " = ? ", array($linkarray["userid"], $linkarray["cmid"], $submissiontype));
                         break;
                     case 'forum':
                         $plagiarismfile = $DB->get_record_select('plagiarism_turnitin_files', " userid = ? AND cm = ? AND identifier = ? AND " . $typefield . " = ? ", array($linkarray["userid"], $linkarray["cmid"], $identifier, $submissiontype));
                         break;
                 }
                 if (!empty($plagiarismfile)) {
                     $submitting = $submission->timemodified > $plagiarismfile->lastmodified && $plagiarismfile->identifier != $identifier ? true : false;
                 } else {
                     $submitting = true;
                 }
             }
         }
         // Group submissions where all students have to submit sets userid to 0;
         if ($linkarray['userid'] == 0 && !$istutor) {
             $linkarray['userid'] = $USER->id;
         }
         $output .= $OUTPUT->box_start('tii_links_container');
         // Show the EULA for a student if necessary.
         if ($linkarray["userid"] == $USER->id) {
             $eula = "";
             static $userid;
             if (empty($userid)) {
                 $userid = 0;
             }
             // Condition added to test for Moodle 2.7 as it calls this function twice.
             if ($CFG->branch >= 27 || $userid != $linkarray["userid"]) {
                 // Show EULA if necessary and we have a connection to Turnitin.
                 static $eulashown;
                 if (empty($eulashown)) {
                     $eulashown = false;
                 }
                 $user = new turnitintooltwo_user($USER->id, "Learner");
                 $success = $user->join_user_to_class($coursedata->turnitin_cid);
                 // $success is false if there is no Turnitin connection and null if user has previously been enrolled.
                 if ((is_null($success) || $success === true) && $eulashown == false) {
                     $eulaaccepted = $user->user_agreement_accepted == 0 ? $user->get_accepted_user_agreement() : $user->user_agreement_accepted;
                     $userid = $linkarray["userid"];
                     if ($eulaaccepted != 1) {
                         $eula_link = html_writer::link($CFG->wwwroot . '/plagiarism/turnitin/extras.php?cmid=' . $linkarray["cmid"] . '&cmd=useragreement&view_context=box_solid', get_string('turnitinppulapost', 'turnitintooltwo'), array("class" => "pp_turnitin_eula_link"));
                         $eula = html_writer::tag('div', $eula_link, array('class' => 'pp_turnitin_ula js_required', 'data-userid' => $user->id));
                         $submitting = false;
                     }
                     // Show EULA launcher and form placeholder.
                     if (!empty($eula)) {
                         $output .= $eula;
                         $turnitincomms = new turnitintooltwo_comms();
                         $turnitincall = $turnitincomms->initialise_api();
                         $customdata = array("disable_form_change_checker" => true, "elements" => array(array('html', $OUTPUT->box('', '', 'useragreement_inputs'))));
                         $eulaform = new turnitintooltwo_form($turnitincall->getApiBaseUrl() . TiiLTI::EULAENDPOINT, $customdata, 'POST', $target = 'eulaWindow', array('id' => 'eula_launch'));
                         $output .= $OUTPUT->box($eulaform->display(), 'tii_useragreement_form', 'useragreement_form');
                         $eulashown = true;
                     }
                 } else {
                     $submitting = false;
                 }
             }
             // If a user has just submitted then send to Turnitin via Ajax.
             if ($submitting && $submission_status) {
                 // Include Javascript for Submitting.
                 $jsurl = new moodle_url('/mod/turnitintooltwo/jquery/plagiarism_submission.js');
                 $PAGE->requires->js($jsurl);
                 // Forum posts have a lot of html stripped out so we have to get data to ajax differently
                 if ($submissiontype == 'forum_post') {
                     $output .= html_writer::start_tag('div', array('class' => 'plagiarism_submission'));
                     $output .= html_writer::tag('div', $identifier . '-' . $submissiontype, array('class' => 'plagiarism_submission_id'));
                     $output .= html_writer::tag('div', urlencode($linkarray["content"]), array('class' => 'plagiarism_submission_content'));
                     $output .= html_writer::tag('div', $linkarray["cmid"], array('class' => 'plagiarism_submission_cmid'));
                     $output .= html_writer::tag('div', $itemid, array('class' => 'plagiarism_submission_itemid'));
                     $output .= html_writer::end_tag('div');
                 } else {
                     $output .= html_writer::start_tag('div', array('class' => 'plagiarism_submission', 'id' => $identifier . '-' . $submissiontype));
                     $output .= html_writer::tag('div', $linkarray["cmid"], array('class' => 'plagiarism_submission_cmid'));
                     $output .= html_writer::tag('div', $itemid, array('class' => 'plagiarism_submission_itemid'));
                     $output .= html_writer::end_tag('div');
                 }
             }
         }
         // Check whether submission is a group submission - only applicable to assignment module.
         // If it's a group submission then other users in the group should be able to see the originality score
         // They can not open the DV though.
         $submissionusers = array($linkarray["userid"]);
         if ($cm->modname == "assign" && $CFG->branch > 23) {
             if ($moduledata->teamsubmission) {
                 $assignment = new assign($context, $cm, null);
                 if ($group = $assignment->get_submission_group($linkarray["userid"])) {
                     $users = groups_get_members($group->id);
                     $submissionusers = array_keys($users);
                 }
             }
         }
         // Display Links for files and contents.
         if ((!empty($linkarray["file"]) || !empty($linkarray["content"])) && ($istutor || $submission_status && in_array($USER->id, $submissionusers))) {
             // Prevent text content links being displayed for previous attempts as we have no way of getting the data.
             if (!empty($linkarray["content"]) && $linkarray["userid"] == $USER->id) {
                 $contentdisplayed = true;
             }
             // Get turnitin details - have to do this again as submission may have been made above.
             $plagiarismfiles = $DB->get_records('plagiarism_turnitin_files', array('userid' => $linkarray["userid"], 'cm' => $linkarray["cmid"], 'identifier' => $identifier), 'lastmodified DESC', '*', 0, 1);
             $plagiarismfile = current($plagiarismfiles);
             // Get user's grades.
             // Get Due date
             $duedate = 0;
             if (!empty($moduledata->duedate)) {
                 $duedate = $moduledata->duedate;
             } else {
                 if (!empty($moduledata->timedue)) {
                     $duedate = $moduledata->timedue;
                 }
             }
             // Get post date
             $postdate = 0;
             if ($cm->modname != "forum") {
                 // Populate gradeitem query
                 $queryarray = array('iteminstance' => $cm->instance, 'itemmodule' => $cm->modname, 'courseid' => $cm->course, 'itemnumber' => 0);
                 if ($gradeitem = $DB->get_record('grade_items', $queryarray)) {
                     switch ($gradeitem->hidden) {
                         case 1:
                             $postdate = strtotime('+1 month');
                             break;
                         case 0:
                             $postdate = time();
                             if ($CFG->branch >= 26 && $cm->modname == 'assign' && !empty($moduledata->markingworkflow)) {
                                 $gradesreleased = $DB->record_exists('assign_user_flags', array('userid' => $linkarray["userid"], 'assignment' => $cm->instance, 'workflowstate' => 'released'));
                                 $postdate = $gradesreleased ? time() : strtotime('+4 weeks', $duedate);
                             }
                             break;
                         default:
                             $postdate = $gradeitem->hidden;
                             break;
                     }
                 }
             }
             $currentgradequery = false;
             if ($istutor || $linkarray["userid"] == $USER->id) {
                 if ($cm->modname == 'forum') {
                     static $gradeitem;
                     if (empty($gradeitem)) {
                         $gradeitem = $DB->get_record('grade_items', array('iteminstance' => $cm->instance, 'itemmodule' => $cm->modname, 'courseid' => $cm->course));
                     }
                     if ($gradeitem) {
                         $currentgradequery = $DB->get_record('grade_grades', array('userid' => $linkarray["userid"], 'itemid' => $gradeitem->id));
                     }
                 } else {
                     if ($cm->modname == 'workshop') {
                         if ($gradeitem) {
                             $currentgradequery = $DB->get_record('grade_grades', array('userid' => $linkarray["userid"], 'itemid' => $gradeitem->id));
                         }
                         $postdate = $moduledata->assessmentend;
                     } else {
                         if ($cm->modname == 'assign') {
                             if ($gradeitem) {
                                 $currentgradesquery = $DB->get_records('assign_grades', array('userid' => $linkarray["userid"], 'assignment' => $cm->instance), 'id DESC');
                                 $currentgradequery = current($currentgradesquery);
                             }
                         }
                     }
                 }
             }
             if ($plagiarismfile && (!$submitting || !$submission_status)) {
                 if ($plagiarismfile->statuscode == 'success') {
                     if ($istutor || $linkarray["userid"] == $USER->id) {
                         $output .= html_writer::tag('div', $OUTPUT->pix_icon('icon-sml', get_string('turnitinid', 'turnitintooltwo') . ': ' . $plagiarismfile->externalid, 'mod_turnitintooltwo', array('class' => 'turnitin_paper_id')) . get_string('turnitinid', 'turnitintooltwo') . ': ' . $plagiarismfile->externalid);
                     }
                     // Show Originality Report score and link.
                     if (($istutor || in_array($USER->id, $submissionusers) && $plagiarismsettings["plagiarism_show_student_report"]) && ((is_null($plagiarismfile->orcapable) || $plagiarismfile->orcapable == 1) && !is_null($plagiarismfile->similarityscore))) {
                         // This class is applied so that only the user who submitted or a tutor can open the DV.
                         $useropenclass = $USER->id == $linkarray["userid"] || $istutor ? 'pp_origreport_open' : '';
                         $output .= $OUTPUT->box_start('row_score pp_origreport ' . $useropenclass . ' origreport_' . $plagiarismfile->externalid . '_' . $linkarray["cmid"], $CFG->wwwroot . '/plagiarism/turnitin/extras.php?cmid=' . $linkarray["cmid"]);
                         // Show score.
                         if ($plagiarismfile->statuscode == "pending") {
                             $output .= html_writer::tag('div', '&nbsp;', array('title' => get_string('pending', 'turnitintooltwo'), 'class' => 'tii_tooltip origreport_score score_colour score_colour_'));
                         } else {
                             // Put EN flag if translated matching is on and that is the score used.
                             $transmatch = $plagiarismfile->transmatch == 1 ? ' EN' : '';
                             if (is_null($plagiarismfile->similarityscore)) {
                                 $score = '&nbsp;';
                                 $titlescore = get_string('pending', 'turnitintooltwo');
                                 $class = 'score_colour_';
                             } else {
                                 $score = $plagiarismfile->similarityscore . '%';
                                 $titlescore = $plagiarismfile->similarityscore . '% ' . get_string('similarity', 'turnitintooltwo');
                                 $class = 'score_colour_' . round($plagiarismfile->similarityscore, -1);
                             }
                             $output .= html_writer::tag('div', $score . $transmatch, array('title' => $titlescore, 'class' => 'tii_tooltip origreport_score score_colour ' . $class));
                         }
                         // Put in div placeholder for DV launch form.
                         $output .= $OUTPUT->box('', 'launch_form origreport_form_' . $plagiarismfile->externalid);
                         // Add url for launching DV from Forum post.
                         if ($cm->modname == 'forum') {
                             $output .= $OUTPUT->box($CFG->wwwroot . '/plagiarism/turnitin/extras.php?cmid=' . $linkarray["cmid"], 'origreport_forum_launch origreport_forum_launch_' . $plagiarismfile->externalid);
                         }
                         $output .= $OUTPUT->box_end(true);
                     }
                     if ($plagiarismfile->orcapable == 0 && !is_null($plagiarismfile->orcapable)) {
                         // This class is applied so that only the user who submitted or a tutor can open the DV.
                         $useropenclass = $USER->id == $linkarray["userid"] || $istutor ? 'pp_origreport_open' : '';
                         $output .= $OUTPUT->box_start('row_score pp_origreport ' . $useropenclass, '');
                         $output .= html_writer::tag('div', 'x', array('title' => get_string('notorcapable', 'turnitintooltwo'), 'class' => 'tii_tooltip score_colour score_colour_ score_no_orcapable'));
                         $output .= $OUTPUT->box_end(true);
                     }
                     // Can grade and feedback be released to this student yet?
                     $released = $postdate <= time() && (!is_null($plagiarismfile->grade) || isset($currentgradequery->grade));
                     // Show link to open grademark.
                     if ($config->usegrademark && ($istutor || $linkarray["userid"] == $USER->id && $released) && !empty($gradeitem)) {
                         // Output grademark icon.
                         $output .= $OUTPUT->box_start('grade_icon', '');
                         $output .= html_writer::tag('div', $OUTPUT->pix_icon('icon-edit', get_string('grademark', 'turnitintooltwo'), 'mod_turnitintooltwo'), array('title' => get_string('grademark', 'turnitintooltwo'), 'class' => 'pp_grademark_open tii_tooltip grademark_' . $plagiarismfile->externalid . '_' . $linkarray["cmid"], 'id' => $CFG->wwwroot . '/plagiarism/turnitin/extras.php?cmid=' . $linkarray["cmid"]));
                         // Add url for launching DV from Forum post.
                         if ($cm->modname == 'forum') {
                             $output .= $OUTPUT->box($CFG->wwwroot . '/plagiarism/turnitin/extras.php?cmid=' . $linkarray["cmid"], 'grademark_forum_launch grademark_forum_launch_' . $plagiarismfile->externalid);
                         }
                         // Put in div placeholder for DV launch form.
                         $output .= $OUTPUT->box('', 'launch_form grademark_form_' . $plagiarismfile->externalid);
                         $output .= $OUTPUT->box_end(true);
                     }
                     if (!$istutor && $config->usegrademark && !empty($plagiarismsettings["plagiarism_rubric"])) {
                         // Update assignment in case rubric is not stored in Turnitin yet.
                         $this->sync_tii_assignment($cm, $coursedata->turnitin_cid);
                         $rubricviewlink = html_writer::tag('div', html_writer::link($CFG->wwwroot . '/plagiarism/turnitin/ajax.php?cmid=' . $cm->id . '&action=rubricview&view_context=box', '&nbsp;', array('class' => 'tii_tooltip rubric_view_pp_launch', 'id' => 'rubric_view_launch', 'title' => get_string('launchrubricview', 'turnitintooltwo'))) . html_writer::tag('span', '', array('class' => 'launch_form', 'id' => 'rubric_view_form')), array('class' => 'row_rubric_view'));
                         $output .= $rubricviewlink;
                     }
                     if ($config->enablepeermark) {
                         // If this module is already on Turnitin then refresh and get Peermark Assignments.
                         if (!empty($plagiarismsettings['turnitin_assignid'])) {
                             if ($_SESSION["updated_pm"][$cm->id] <= time() - 60 * 2) {
                                 $this->refresh_peermark_assignments($cm, $plagiarismsettings['turnitin_assignid']);
                                 $turnitintooltwoassignment = new turnitintooltwo_assignment($cm->instance, '', 'PP');
                                 $_SESSION["peermark_assignments"][$cm->id] = $turnitintooltwoassignment->get_peermark_assignments($plagiarismsettings['turnitin_assignid']);
                                 $_SESSION["updated_pm"][$cm->id] = time();
                             }
                             // Determine if we have any active Peermark Assignments.
                             static $peermarksactive;
                             if (!isset($peermarksactive)) {
                                 $peermarksactive = false;
                                 foreach ($_SESSION["peermark_assignments"][$cm->id] as $peermarkassignment) {
                                     if (time() > $peermarkassignment->dtstart) {
                                         $peermarksactive = true;
                                         break;
                                     }
                                 }
                             }
                             // Show Peermark Reviews link.
                             if ($istutor && count($_SESSION["peermark_assignments"][$cm->id]) > 0 || !$istutor && $peermarksactive) {
                                 $peermarkreviewslink = $OUTPUT->box_start('row_peermark_reviews', '');
                                 $peermarkreviewslink .= html_writer::link($CFG->wwwroot . '/plagiarism/turnitin/ajax.php?cmid=' . $cm->id . '&action=peermarkreviews&view_context=box', '', array('title' => get_string('launchpeermarkreviews', 'turnitintooltwo'), 'class' => 'peermark_reviews_pp_launch tii_tooltip'));
                                 $peermarkreviewslink .= html_writer::tag('span', '', array('class' => 'launch_form', 'id' => 'peermark_reviews_form'));
                                 $peermarkreviewslink .= $OUTPUT->box_end(true);
                                 $output .= $peermarkreviewslink;
                             }
                         }
                     }
                 } else {
                     if ($plagiarismfile->statuscode == 'error') {
                         // Deal with legacy error issues.
                         if (!isset($plagiarismfile->errorcode)) {
                             $errorcode = 0;
                             if ($submissiontype == 'file') {
                                 if ($file->get_filesize() > TURNITINTOOLTWO_MAX_FILE_UPLOAD_SIZE) {
                                     $errorcode = 2;
                                 }
                             }
                         } else {
                             $errorcode = $plagiarismfile->errorcode;
                         }
                         // Show error message if there is one.
                         if ($errorcode == 0) {
                             $langstring = $istutor ? 'ppsubmissionerrorseelogs' : 'ppsubmissionerrorstudent';
                             $errorstring = isset($plagiarismfile->errormsg) ? get_string($langstring, 'turnitintooltwo') : $plagiarismfile->errormsg;
                         } else {
                             $errorstring = get_string('errorcode' . $plagiarismfile->errorcode, 'turnitintooltwo', display_size(TURNITINTOOLTWO_MAX_FILE_UPLOAD_SIZE));
                         }
                         $erroricon = html_writer::tag('div', $OUTPUT->pix_icon('x-red', $errorstring, 'mod_turnitintooltwo'), array('title' => $errorstring, 'class' => 'tii_tooltip tii_error_icon'));
                         // If logged in as a student, attach error text after icon.
                         if (!$istutor) {
                             $output .= html_writer::tag('div', $erroricon . ' ' . $errorstring, array('class' => 'warning clear'));
                         } else {
                             $output .= html_writer::tag('div', $erroricon, array('class' => 'clear'));
                         }
                     }
                 }
             } else {
                 // Add Error to show that user has not accepted EULA.
                 if ($linkarray["userid"] != $USER->id && $istutor) {
                     // There is a moodle plagiarism bug where get_links is called twice, the first loop is incorrect and is killing
                     // this functionality. Have to check that user exists here first else there will be a fatal error.
                     if ($mdl_user = $DB->get_record('user', array('id' => $linkarray["userid"]))) {
                         // We also need to check for security that they are actually on the Course.
                         switch ($cm->modname) {
                             case 'assign':
                             case 'workshop':
                                 $capability = 'mod/' . $cm->modname . ':submit';
                                 break;
                             case 'forum':
                                 $capability = 'mod/' . $cm->modname . ':replypost';
                                 break;
                         }
                         if (has_capability($capability, $context, $linkarray["userid"])) {
                             $user = new turnitintooltwo_user($linkarray["userid"], "Learner");
                             if ($user->user_agreement_accepted != 1) {
                                 $erroricon = html_writer::tag('div', $OUTPUT->pix_icon('doc-x-grey', get_string('notacceptedeula', 'turnitintooltwo'), 'mod_turnitintooltwo'), array('title' => get_string('notacceptedeula', 'turnitintooltwo'), 'class' => 'tii_tooltip tii_error_icon'));
                                 $output .= html_writer::tag('div', $erroricon, array('class' => 'clear'));
                             }
                         }
                     }
                 }
             }
             // Show error warning for submission
             $output .= html_writer::tag('div', '', array('class' => 'turnitin_submit_error warning clear turnitin_submit_error_' . $identifier));
             // Show success of submission
             $output .= html_writer::tag('div', '', array('class' => 'turnitin_submit_success success clear turnitin_submit_success_' . $identifier));
             $output .= html_writer::tag('div', '', array('class' => 'clear'));
         }
         $output .= $OUTPUT->box_end(true);
     }
     return $output;
 }