Example #1
0
/**
 * Function called by course/reset.php when resetting moodle course on Moodle 1.9+
 * Used to duplicate or reset a courses Turnitin activities
 *
 * @global object
 * @param var $courseid The course ID for the course to reset
 * @param string $action The action to use OLDCLASS or NEWCLASS
 * @param boolean $legacy True for 1.8 Moodle False for 1.9+ Moodle
 * @return array The status array to pass to turnitintool_reset_userdata
 */
function turnitintool_duplicate_recycle($courseid, $action, $legacy = false)
{
    set_time_limit(0);
    global $CFG, $USER;
    if (!($turnitintools = turnitintool_get_records('turnitintool', 'course', $courseid))) {
        turnitintool_print_error('assigngeterror', 'turnitintool', NULL, NULL, __FILE__, __LINE__);
        exit;
    }
    if (!($course = turnitintool_get_record('course', 'id', $courseid))) {
        turnitintool_print_error('coursegeterror', 'turnitintool', NULL, NULL, __FILE__, __LINE__);
        exit;
    }
    $partsarray = array();
    $loaderbar = NULL;
    $owner = turnitintool_get_owner($courseid);
    $uid = turnitintool_getUID($owner);
    if (is_null($uid)) {
        // In the unlikely event we have no Turnitin owner for this class to reset/recycle then we need
        // to make a new owner based on the logged in user. This scenario may occur if all classes have been reset
        // or all of the Turnitin data has been imported from backups without user data.
        $tii = new turnitintool_commclass(turnitintool_getUID($USER), $USER->firstname, $USER->lastname, $USER->email, 2, $loaderbar);
        $userid = turnitintool_usersetup($USER, '', $tii, $loaderbar);
        $uid = $userid->turnitin_uid;
        $owner = $USER;
    } else {
        $tii = new turnitintool_commclass(turnitintool_getUID($owner), $owner->firstname, $owner->lastname, $owner->email, 2, $loaderbar);
    }
    $tii->startSession();
    foreach ($turnitintools as $turnitintool) {
        if (!($parts = turnitintool_get_records_select('turnitintool_parts', 'turnitintoolid=' . $turnitintool->id . ' AND deleted=0'))) {
            turnitintool_print_error('partgeterror', 'turnitintool', NULL, NULL, __FILE__, __LINE__);
        }
        // Now build up the part array
        foreach ($parts as $part) {
            $partsarray[$courseid][$turnitintool->id][$part->id]['cid'] = turnitintool_getCID($courseid);
            $partsarray[$courseid][$turnitintool->id][$part->id]['ctl'] = turnitintool_getCTL($courseid);
            $partsarray[$courseid][$turnitintool->id][$part->id]['assign'] = $turnitintool->name . ' - ' . $part->partname . ' (Moodle: ' . $part->tiiassignid . ')';
            $partsarray[$courseid][$turnitintool->id][$part->id]['assignid'] = $part->tiiassignid;
            $partsarray[$courseid][$turnitintool->id][$part->id]['max_points'] = $part->maxmarks;
            $partsarray[$courseid][$turnitintool->id][$part->id]['turnitintool'] = $turnitintool;
        }
    }
    // ----------------------------------
    // IF action EQUALS 'OLDCLASS'
    // ----------------------------------
    // Class on TII: Dont Create New
    // Class in DB: Leave Alone
    // Parts in DB: Replace IDs with New
    // Old Parts on TII: Delete
    // ----------------------------------
    // ELSE IF action EQUALS 'NEWCLASS'
    // ----------------------------------
    // Class on TII: Create New
    // Class in DB: Replace IDs with New
    // Parts in DB: Replace IDs with New
    // Old Parts on TII: Leave Alone
    // ----------------------------------
    if ($action == "OLDCLASS") {
        $newclassid = turnitintool_getCID($courseid);
        $newclasstitle = turnitintool_getCTL($courseid);
    } else {
        $oldclassid = turnitintool_getCID($courseid);
        $oldclasstitle = turnitintool_getCTL($courseid);
        // Delete old TII Class Link Data
        if (!($delete = turnitintool_delete_records('turnitintool_courses', 'courseid', $courseid))) {
            turnitintool_print_error('coursedeleteerror', 'turnitintool', NULL, NULL, __FILE__, __LINE__);
            exit;
        }
        // Create a new class to use with new parts
        $turnitincourse = turnitintool_classsetup($course, $owner, '', $tii, $loaderbar);
        $newclassid = $turnitincourse->turnitin_cid;
        $newclasstitle = $turnitincourse->turnitin_ctl;
    }
    // Do the loop to create the new parts and swap over the stored TII Part IDs
    foreach ($partsarray as $classid => $v1) {
        foreach ($v1 as $assignid => $v2) {
            foreach ($v2 as $partid => $data) {
                // Get the current assignment part settings from Turnitin
                $post = new stdClass();
                $post->cid = $data['cid'];
                $post->ctl = $data['ctl'];
                $post->assign = $data['assign'];
                $post->assignid = $data['assignid'];
                $tii->queryAssignment($post, '');
                $assignObj = $tii->getAssignmentObject();
                if ($tii->getRerror()) {
                    $erroroutput[] = $tii->getAPIunavailable() ? get_string('apiunavailable', 'turnitintool') : $tii->getRmessage();
                }
                $post->cid = '';
                $post->uid = '';
                $post->assignid = '';
                $post->ctl = $newclasstitle;
                $uniquestring = strtoupper(uniqid());
                $post->name = current(explode(' (Moodle: ', $post->assign)) . ' (Moodle: ' . $uniquestring . ')';
                $post->dtstart = time();
                $post->dtdue = strtotime('+7 days');
                $post->dtpost = strtotime('+7 days');
                $post->s_view_report = $assignObj->s_view_report;
                $post->anon = $assignObj->anon;
                $post->report_gen_speed = $assignObj->report_gen_speed;
                $post->late_accept_flag = $assignObj->late_accept_flag;
                $post->submit_papers_to = $assignObj->submit_papers_to;
                $post->s_paper_check = $assignObj->s_paper_check;
                $post->internet_check = $assignObj->internet_check;
                $post->journal_check = $assignObj->journal_check;
                $post->max_points = $assignObj->maxpoints;
                // Synch the rest of the reset data from the turnitintool table
                $post->exclude_biblio = $data['turnitintool']->excludebiblio;
                $post->exclude_quoted = $data['turnitintool']->excludequoted;
                $post->exclude_value = $data['turnitintool']->excludevalue;
                $post->exclude_type = $data['turnitintool']->excludetype;
                $post->erater = $data['turnitintool']->erater;
                $post->erater_handbook = $data['turnitintool']->erater_handbook;
                $post->erater_dictionary = $data['turnitintool']->erater_dictionary;
                $post->erater_spelling = $data['turnitintool']->erater_spelling;
                $post->erater_grammar = $data['turnitintool']->erater_grammar;
                $post->erater_usage = $data['turnitintool']->erater_usage;
                $post->erater_mechanics = $data['turnitintool']->erater_mechanics;
                $post->erater_style = $data['turnitintool']->erater_style;
                // Create the Part on TII without IDs
                $tii->createAssignment($post, 'INSERT', '');
                if ($tii->getRerror()) {
                    $erroroutput[] = $tii->getAPIunavailable() ? get_string('apiunavailable', 'turnitintool') : $tii->getRmessage();
                }
                $newassignid = $tii->getAssignid();
                $post->currentassign = current(explode(' (Moodle: ', $post->assign)) . ' (Moodle: ' . $uniquestring . ')';
                $post->name = current(explode(' (Moodle: ', $post->name)) . ' (Moodle: ' . $newassignid . ')';
                $post->assignid = $newassignid;
                $post->dtstart = $assignObj->dtstart;
                $post->dtdue = $assignObj->dtdue;
                $post->dtpost = $assignObj->dtpost;
                $post->cid = $newclassid;
                // Create the Part on TII with IDs
                $tii->createAssignment($post, 'UPDATE', '');
                if ($tii->getRerror()) {
                    $erroroutput[] = $tii->getAPIunavailable() ? get_string('apiunavailable', 'turnitintool') : $tii->getRmessage();
                }
                $dbpart = new stdClass();
                $dbpart->id = $partid;
                $dbpart->tiiassignid = $newassignid;
                if (!($update = turnitintool_update_record('turnitintool_parts', $dbpart))) {
                    turnitintool_print_error('partupdateerror', 'turnitintool', NULL, NULL, __FILE__, __LINE__);
                    exit;
                }
                if (!($delete = turnitintool_delete_records('turnitintool_submissions', 'submission_part', $partid))) {
                    turnitintool_print_error('submissiondeleteerror', 'turnitintool', NULL, NULL, __FILE__, __LINE__);
                    exit;
                }
                unset($post);
            }
        }
    }
    // Do the loop again for deletes this ensures we dont have a partly executed create delete run breaking things
    if ($action == "OLDCLASS") {
        // Only do this if we are recycling the same TII class
        foreach ($partsarray as $classid => $v1) {
            foreach ($v1 as $assignid => $v2) {
                foreach ($v2 as $partid => $data) {
                    //$null=NULL;
                    //$tii = new turnitintool_commclass($data['uid'],$data['ufn'],$data['uln'],$data['uem'],2,$null);
                    //$tii->startSession();
                    $post->cid = $data['cid'];
                    $post->ctl = $data['ctl'];
                    $post->name = $data['assign'];
                    $post->assignid = $data['assignid'];
                    $tii->deleteAssignment($post, '');
                    if ($tii->getRerror()) {
                        $erroroutput[] = $tii->getAPIunavailable() ? get_string('apiunavailable', 'turnitintool') : $tii->getRmessage();
                    }
                    //$tii->endSession();
                }
            }
        }
    } else {
        if (isset($erroroutput)) {
            // If there was a comms error roll back the class ID data changes
            $classupdatedata = new stdClass();
            $classupdatedata->id = $courseid;
            $classupdatedata->turnitin_cid = $oldclassid;
            $classupdatedata->turnitin_ctl = $oldclasstitle;
            if (!($classupdate = turnitintool_update_record('turnitintool_courses', $classupdatedata))) {
                turnitintool_print_error('classupdateerror', 'turnitintool', NULL, NULL, __FILE__, __LINE__);
                exit;
            }
        }
    }
    $tii->endSession();
    if (isset($erroroutput) and $CFG->turnitin_enablediagnostic == '1') {
        $errors = '-----------<br />[' . join(']<br />-----------<br />[', $erroroutput) . ']';
    } else {
        $errors = '';
    }
    $status = NULL;
    if (!$legacy) {
        if (isset($erroroutput) and $action == "NEWCLASS") {
            $status[] = array('component' => get_string('modulenameplural', 'turnitintool'), 'item' => get_string('copyassigndata', 'turnitintool') . $errors, 'error' => true);
        } else {
            if (!isset($erroroutput) and $action == "NEWCLASS") {
                $status[] = array('component' => get_string('modulenameplural', 'turnitintool'), 'item' => get_string('copyassigndata', 'turnitintool'), 'error' => false);
            } else {
                if (isset($erroroutput) and $action == "OLDCLASS") {
                    $status[] = array('component' => get_string('modulenameplural', 'turnitintool'), 'item' => get_string('replaceassigndata', 'turnitintool') . $errors, 'error' => true);
                } else {
                    if (!isset($erroroutput) and $action == "OLDCLASS") {
                        $status[] = array('component' => get_string('modulenameplural', 'turnitintool'), 'item' => get_string('replaceassigndata', 'turnitintool'), 'error' => false);
                    }
                }
            }
        }
    } else {
        if (isset($erroroutput) and $action == "NEWCLASS") {
            $status['error'] = true;
        } else {
            if (!isset($erroroutput) and $action == "NEWCLASS") {
                $status['error'] = false;
            } else {
                if (isset($erroroutput) and $action == "OLDCLASS") {
                    $status['error'] = true;
                } else {
                    if (!isset($erroroutput) and $action == "OLDCLASS") {
                        $status['error'] = false;
                    }
                }
            }
        }
    }
    return $status;
}
                if ($tuser = turnitintool_get_record('turnitintool_users', 'id', $userlink) and $muser = turnitintool_get_record('user', 'id', $tuser->userid)) {
                    // Get the email address if the user has been deleted
                    if (empty($muser->email) or strpos($muser->email, '@') === false) {
                        $split = explode('.', $muser->username);
                        array_pop($split);
                        $muser->email = join('.', $split);
                    }
                    $tii = new turnitintool_commclass(null, $muser->firstname, $muser->lastname, $muser->email, $tuser->turnitin_utp, $loaderbar);
                    $tii->createUser($post, get_string('userprocess', 'turnitintool'));
                    $user = new stdClass();
                    $user->id = $userlink;
                    $user->turnitin_uid = $tii->getRerror() ? 0 : $tii->getUserID();
                    turnitintool_update_record('turnitintool_users', $user);
                    unset($tii);
                } else {
                    turnitintool_delete_records('turnitintool_users', 'id', $userlink);
                }
            }
            unset($loader);
            turnitintool_redirect($CFG->wwwroot . '/mod/turnitintool/extras.php?do=unlinkusers');
        }
        turnitintool_header(NULL, NULL, $_SERVER["REQUEST_URI"], get_string("modulenameplural", "turnitintool"), $SITE->fullname);
        echo '<div id="turnitintool_style">';
        turnitintool_box_start('generalbox boxwidthwide boxaligncenter', 'general');
        echo '<b>' . get_string('unlinkrelinkusers', 'turnitintool') . '</b><br /><br />';
        echo '<form method="POST" id="turnitin_unlink" action="' . $CFG->wwwroot . '/mod/turnitintool/extras.php?do=unlinkusers"><div style="height: 400px;overflow: auto;">
';
        if ($userrows = turnitintool_get_records('turnitintool_users')) {
            foreach ($userrows as $userdata) {
                if (!($user = turnitintool_get_record('user', 'id', $userdata->userid))) {
                    $user->id = $user->userid;