Пример #1
0
 public function update_status($course, $cm)
 {
     global $DB, $USER, $OUTPUT;
     $outputhtml = '';
     $userprofilefieldname = 'turnitinteachercoursecache';
     if (!($plagiarismsettings = $this->get_settings())) {
         return;
     }
     // If Turinitin has already been told about this user's rights in this course,
     // the courseid will exist in a comma separated listed in a hidden profile field.
     // Thus stored so that we don't repeatedly advise turnitin, and site admins can clear the cache if so desired.
     $newrecord = null;
     if (!isset($USER->profile)) {
         // User has had a partial login - possibly over web services.
         // Check for profile details directly in DB:
         $sql = 'SELECT d.id, d.data ' . 'FROM {user_info_field} f ' . ' INNER JOIN {user_info_data} d ON d.fieldid = f.id ' . 'WHERE f.shortname = ? ' . ' AND d.userid = ? ';
         $userprofiledetail = $DB->get_record_sql($sql, array($userprofilefieldname, $USER->id));
         if (!empty($userprofiledetail)) {
             $existingcourses = explode(',', $userprofiledetail->data);
             $newrecord = false;
         } else {
             $existingcourses = array();
             $newrecord = true;
         }
     } else {
         if (!empty($USER->profile[$userprofilefieldname])) {
             $existingcourses = explode(',', $USER->profile[$userprofilefieldname]);
             $newrecord = false;
         } else {
             $existingcourses = array();
             $sql = 'SELECT d.id ' . ' FROM {user_info_field} f ' . ' INNER JOIN {user_info_data} d ON d.fieldid = f.id ' . ' WHERE f.shortname = ? ' . ' AND d.userid = ? ';
             if (!$DB->record_exists_sql($sql, array($userprofilefieldname, $USER->id))) {
                 $newrecord = true;
             } else {
                 $newrecord = false;
             }
         }
     }
     if (!in_array($course->id, $existingcourses)) {
         // Turnitin doesn't (yet) know that this user is a teacher in this course.  Tell them:
         $tii = array();
         $tii['utp'] = TURNITIN_INSTRUCTOR;
         $tii = turnitin_get_tii_user($tii, $USER);
         $tii['cid'] = get_config('plagiarism_turnitin_course', $course->id);
         //course ID
         $tii['ctl'] = strlen($course->shortname) > 45 ? substr($course->shortname, 0, 45) : $course->shortname;
         $tii['ctl'] = strlen($tii['ctl']) > 5 ? $tii['ctl'] : $tii['ctl'] . "_____";
         $tii['fcmd'] = TURNITIN_RETURN_XML;
         $tii['fid'] = TURNITIN_CREATE_CLASS;
         $tiixml = plagiarism_get_xml(turnitin_get_url($tii, $plagiarismsettings));
         if ($tiixml->rcode[0] != TURNITIN_RESP_CLASS_CREATED) {
             return $OUTPUT->notification(get_string('errorassigninguser', 'plagiarism_turnitin'));
         }
         $existingcourses[] = $course->id;
         $newcoursecache = implode(',', $existingcourses);
         // Now update our record of what teacherships TII knows about:
         $userprofilefieldid = $DB->get_field('user_info_field', 'id', array('shortname' => $userprofilefieldname));
         if ($newrecord) {
             // New field - will need to insert a new record.
             $userdata = new stdclass();
             $userdata->userid = $USER->id;
             $userdata->fieldid = $userprofilefieldid;
             $userdata->data = $newcoursecache;
             $DB->insert_record('user_info_data', $userdata);
         } else {
             $DB->set_field('user_info_data', 'data', $newcoursecache, array('userid' => $USER->id, 'fieldid' => $userprofilefieldid));
         }
         $USER->profile[$userprofilefieldname] = $newcoursecache;
     }
     $tii = array();
     //print link to teacher login
     $tii['fcmd'] = TURNITIN_LOGIN;
     //when set to 2 this returns XML
     $tii['utp'] = TURNITIN_INSTRUCTOR;
     $tii['fid'] = TURNITIN_CREATE_USER;
     //set commands - Administrator login/statistics.
     $tii = turnitin_get_tii_user($tii, $USER);
     $outputhtml .= '<div style="text-align:right"><a href="' . turnitin_get_url($tii, $plagiarismsettings) . '" target="_blank">' . get_string("teacherlogin", "plagiarism_turnitin") . '</a></div>';
     //currently only used for grademark - check if enabled and return if not.
     //TODO: This call degrades page performance - need to run less frequently.
     if (empty($plagiarismsettings['turnitin_enablegrademark'])) {
         return $outputhtml;
     }
     if (!($moduletype = $DB->get_field('modules', 'name', array('id' => $cm->module)))) {
         debugging("invalid moduleid! - moduleid:" . $cm->module);
     }
     if (!($module = $DB->get_record($moduletype, array('id' => $cm->instance)))) {
         debugging("invalid instanceid! - instance:" . $cm->instance . " Module:" . $moduletype);
     }
     //set globals.
     $tii['utp'] = TURNITIN_INSTRUCTOR;
     $tii = turnitin_get_tii_user($tii, $USER);
     $tii['cid'] = get_config('plagiarism_turnitin_course', $course->id);
     //course ID
     $tii['ctl'] = strlen($course->shortname) > 45 ? substr($course->shortname, 0, 45) : $course->shortname;
     $tii['ctl'] = strlen($tii['ctl']) > 5 ? $tii['ctl'] : $tii['ctl'] . "_____";
     $turnitin_assignid = $DB->get_field('plagiarism_turnitin_config', 'value', array('cm' => $cm->id, 'name' => 'turnitin_assignid'));
     if (!empty($turnitin_assignid)) {
         $tii['assignid'] = $turnitin_assignid;
     }
     $tii['assign'] = turnitin_get_assign_name($module->name, $cm->id);
     //assignment name stored in TII
     $tii['fcmd'] = TURNITIN_RETURN_XML;
     $tii['fid'] = TURNITIN_LIST_SUBMISSIONS;
     $tiixml = plagiarism_get_xml(turnitin_get_url($tii, $plagiarismsettings));
     if (!empty($tiixml->object)) {
         //get full list of turnitin_files for this cm
         $grademarkstatus = array();
         foreach ($tiixml->object as $tiiobject) {
             $grademarkstatus[(int) $tiiobject->objectID[0]] = (int) $tiiobject->gradeMarkStatus[0];
         }
         if (!empty($grademarkstatus)) {
             $plagiarsim_files = $DB->get_records('plagiarism_turnitin_files', array('cm' => $cm->id));
             foreach ($plagiarsim_files as $file) {
                 if (isset($grademarkstatus[$file->externalid]) && $file->externalstatus != $grademarkstatus[$file->externalid]) {
                     $file->externalstatus = $grademarkstatus[$file->externalid];
                     $DB->update_record('plagiarism_turnitin_files', $file);
                 }
             }
         }
     }
     return $outputhtml;
 }
                    }
                }
                if (!empty($newf->identifier)) {
                    $DB->insert_record('plagiarism_turnitin_files', $newf);
                }
            }
        }
    }
    $dbman->rename_table($table, 'tii_files_legacy');
}
//finished check for old tables..
if ($plagiarismsettings) {
    //Now show link to ADMIN tii interface - NOTE: this logs in the ADMIN user, should be hidden from normal teachers.
    $tii['utp'] = TURNITIN_ADMIN;
    $tii = turnitin_get_tii_user($tii, $USER);
    $tii['fcmd'] = TURNITIN_LOGIN;
    //when set to 2 this returns XML
    $tii['fid'] = TURNITIN_ADMIN_STATS;
    //set commands - Administrator login/statistics.
    echo '<div align="center">';
    echo '<a href="' . turnitin_get_url($tii, $plagiarismsettings) . '" target="_blank">' . get_string("adminlogin", "plagiarism_turnitin") . '</a><br/>';
    $tii['utp'] = TURNITIN_INSTRUCTOR;
    $tii['fid'] = TURNITIN_CREATE_USER;
    //set commands - Administrator login/statistics.
    echo '<a href="' . turnitin_get_url($tii, $plagiarismsettings) . '" target="_blank">' . get_string("teacherlogin", "plagiarism_turnitin") . '</a>';
    echo '</div>';
}
echo $OUTPUT->box_start('generalbox boxaligncenter', 'intro');
$mform->display();
echo $OUTPUT->box_end();
echo $OUTPUT->footer();