Esempio n. 1
0
require_login($course, false, $cm);
if (isguestuser() or !confirm_sesskey()) {
    print_error('error');
}
// Set up completion object and check it is enabled.
$completion = new completion_info($course);
if (!$completion->is_enabled()) {
    throw new moodle_exception('completionnotenabled', 'completion');
}
// NOTE: All users are allowed to toggle their completion state, including
// users for whom completion information is not directly tracked. (I.e. even
// if you are a teacher, or admin who is not enrolled, you can still toggle
// your own completion state. You just don't appear on the reports.)
// Check completion state is manual
if ($cm->completion != COMPLETION_TRACKING_MANUAL) {
    error_or_ajax('cannotmanualctrack', $fromajax);
}
$completion->update_state($cm, $targetstate);
// And redirect back to course
if ($fromajax) {
    print 'OK';
} else {
    // In case of use in other areas of code we allow a 'backto' parameter,
    // otherwise go back to course page
    if ($backto = optional_param('backto', null, PARAM_URL)) {
        redirect($backto);
    } else {
        redirect(course_get_url($course, $cm->sectionnum));
    }
}
// utility functions
        print get_string($message, 'error');
        exit;
    } else {
        print_error($message);
    }
}
// Get course-modules entry
if (!($cm = $DB->get_record('course_modules', array('id' => $cmid)))) {
    error_or_ajax('invalidactivityid');
}
if (!($course = $DB->get_record('course', array('id' => $cm->course)))) {
    error_or_ajax('invalidcourseid');
}
// Check user is logged in
require_login($course);
// Check completion state is manual
if ($cm->completion != COMPLETION_TRACKING_MANUAL) {
    error_or_ajax('cannotmanualctrack');
}
// Now change state
$completion = new completion_info($course);
$completion->update_state($cm, $targetstate);
// And redirect back to course
if ($fromajax) {
    print 'OK';
} else {
    // In case of use in other areas of code we allow a 'backto' parameter,
    // otherwise go back to course page
    $backto = optional_param('backto', 'view.php?id=' . $course->id, PARAM_URL);
    redirect($backto);
}