Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 4db061680e
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 170 lines (135 sloc) 6.381 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
<?php

///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////

// Edit course completion settings

require_once('../config.php');
require_once('lib.php');
require_once($CFG->libdir.'/completionlib.php');
require_once($CFG->libdir.'/completion/completion_criteria_self.php');
require_once($CFG->libdir.'/completion/completion_criteria_date.php');
require_once($CFG->libdir.'/completion/completion_criteria_unenrol.php');
require_once($CFG->libdir.'/completion/completion_criteria_activity.php');
require_once($CFG->libdir.'/completion/completion_criteria_duration.php');
require_once($CFG->libdir.'/completion/completion_criteria_grade.php');
require_once($CFG->libdir.'/completion/completion_criteria_role.php');
require_once($CFG->libdir.'/completion/completion_criteria_course.php');
require_once $CFG->libdir.'/gradelib.php';
require_once('completion_form.php');

$id = required_param('id', PARAM_INT); // course id

/// basic access control checks
if ($id) { // editing course

    if($id == SITEID){
        // don't allow editing of 'site course' using this from
        print_error('cannoteditsiteform');
    }

    if (!$course = $DB->get_record('course', array('id'=>$id))) {
        print_error('invalidcourseid');
    }
    require_login($course);
    require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id));

} else {
    require_login();
    print_error('needcourseid');
}

/// Set up the page
$streditcompletionsettings = get_string("editcoursecompletionsettings", 'completion');
$PAGE->set_course($course);
$PAGE->set_url('/course/completion.php', array('id' => $course->id));
//$PAGE->navbar->add($streditcompletionsettings);
$PAGE->set_title($course->shortname);
$PAGE->set_heading($course->fullname);
$PAGE->set_pagelayout('standard');

/// first create the form
$form = new course_completion_form('completion.php?id='.$id, compact('course'));

// now override defaults if course already exists
if ($form->is_cancelled()){
    redirect($CFG->wwwroot.'/course/view.php?id='.$course->id);

} else if ($data = $form->get_data()) {

    $completion = new completion_info($course);

/// process criteria unlocking if requested
    if (!empty($data->settingsunlock)) {

        $completion->delete_course_completion_data();

        // Return to form (now unlocked)
        redirect($CFG->wwwroot."/course/completion.php?id=$course->id");
    }

/// process data if submitted
    // Delete old criteria
    $completion->clear_criteria();

    // Loop through each criteria type and run update_config
    global $COMPLETION_CRITERIA_TYPES;
    foreach ($COMPLETION_CRITERIA_TYPES as $type) {
        $class = 'completion_criteria_'.$type;
        $criterion = new $class();
        $criterion->update_config($data);
    }

    // Handle aggregation methods
    // Overall aggregation
    $aggdata = array(
        'course' => $data->id,
        'criteriatype' => null
    );
    $aggregation = new completion_aggregation($aggdata);
    $aggregation->setMethod($data->overall_aggregation);
    $aggregation->save();

    // Activity aggregation
    if (empty($data->activity_aggregation)) {
        $data->activity_aggregation = 0;
    }

    $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ACTIVITY;
    $aggregation = new completion_aggregation($aggdata);
    $aggregation->setMethod($data->activity_aggregation);
    $aggregation->save();

    // Course aggregation
    if (empty($data->course_aggregation)) {
        $data->course_aggregation = 0;
    }

    $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_COURSE;
    $aggregation = new completion_aggregation($aggdata);
    $aggregation->setMethod($data->course_aggregation);
    $aggregation->save();

    // Role aggregation
    if (empty($data->role_aggregation)) {
        $data->role_aggregation = 0;
    }

    $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ROLE;
    $aggregation = new completion_aggregation($aggdata);
    $aggregation->setMethod($data->role_aggregation);
    $aggregation->save();

    // Update course total passing grade
    if (!empty($data->criteria_grade)) {
        if ($grade_item = grade_category::fetch_course_category($course->id)->grade_item) {
            $grade_item->gradepass = $data->criteria_grade_value;
            if (method_exists($grade_item, 'update')) {
                $grade_item->update('course/completion.php');
            }
        }
    }

    add_to_log($course->id, 'course', 'completion updated', 'completion.php?id='.$course->id);

    $url = new moodle_url('/course/view.php', array('id' => $course->id));
    redirect($url);
}


/// Print the form


echo $OUTPUT->header();
echo $OUTPUT->heading($streditcompletionsettings);

$form->display();

echo $OUTPUT->footer();
Something went wrong with that request. Please try again.