Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 403]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Edit course completion settings
  20   *
  21   * @package     core_completion
  22   * @category    completion
  23   * @copyright   2009 Catalyst IT Ltd
  24   * @author      Aaron Barnes <aaronb@catalyst.net.nz>
  25   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  require_once(__DIR__.'/../config.php');
  29  require_once($CFG->dirroot.'/course/lib.php');
  30  require_once($CFG->libdir.'/completionlib.php');
  31  require_once($CFG->dirroot.'/completion/criteria/completion_criteria_self.php');
  32  require_once($CFG->dirroot.'/completion/criteria/completion_criteria_date.php');
  33  require_once($CFG->dirroot.'/completion/criteria/completion_criteria_unenrol.php');
  34  require_once($CFG->dirroot.'/completion/criteria/completion_criteria_activity.php');
  35  require_once($CFG->dirroot.'/completion/criteria/completion_criteria_duration.php');
  36  require_once($CFG->dirroot.'/completion/criteria/completion_criteria_grade.php');
  37  require_once($CFG->dirroot.'/completion/criteria/completion_criteria_role.php');
  38  require_once($CFG->dirroot.'/completion/criteria/completion_criteria_course.php');
  39  require_once $CFG->libdir.'/gradelib.php';
  40  require_once($CFG->dirroot.'/course/completion_form.php');
  41  
  42  $id = required_param('id', PARAM_INT);       // course id
  43  
  44  // Perform some basic access control checks.
  45  if ($id) {
  46  
  47      if($id == SITEID){
  48          // Don't allow editing of 'site course' using this form.
  49          throw new \moodle_exception('cannoteditsiteform');
  50      }
  51  
  52      if (!$course = $DB->get_record('course', array('id'=>$id))) {
  53          throw new \moodle_exception('invalidcourseid');
  54      }
  55      require_login($course);
  56      $context = context_course::instance($course->id);
  57      if (!has_capability('moodle/course:update', $context)) {
  58          // User is not allowed to modify course completion.
  59          // Check if they can see default completion or edit bulk completion and redirect there.
  60          if ($options = core_completion\manager::get_available_completion_options($course->id)) {
  61              // Redirect to the first available completion page.
  62              redirect(array_key_first($options));
  63          } else {
  64              require_capability('moodle/course:update', $context);
  65          }
  66      }
  67  
  68  } else {
  69      require_login();
  70      throw new \moodle_exception('needcourseid');
  71  }
  72  
  73  // Set up the page.
  74  $PAGE->set_course($course);
  75  $PAGE->set_url('/course/completion.php', array('id' => $course->id));
  76  $PAGE->set_title($course->shortname);
  77  $PAGE->set_heading($course->fullname);
  78  $PAGE->set_pagelayout('admin');
  79  
  80  // Create the settings form instance.
  81  $form = new course_completion_form('completion.php?id='.$id, array('course' => $course));
  82  
  83  if ($form->is_cancelled()){
  84      redirect($CFG->wwwroot.'/course/view.php?id='.$course->id);
  85  
  86  } else if ($data = $form->get_data()) {
  87      $completion = new completion_info($course);
  88  
  89      // Process criteria unlocking if requested.
  90      if (!empty($data->settingsunlock)) {
  91          $completion->delete_course_completion_data();
  92  
  93          // Return to form (now unlocked).
  94          redirect($PAGE->url);
  95      }
  96  
  97      // Delete old criteria.
  98      $completion->clear_criteria();
  99  
 100      // Loop through each criteria type and run its update_config() method.
 101      global $COMPLETION_CRITERIA_TYPES;
 102      foreach ($COMPLETION_CRITERIA_TYPES as $type) {
 103          $class = 'completion_criteria_'.$type;
 104          $criterion = new $class();
 105          $criterion->update_config($data);
 106      }
 107  
 108      // Handle overall aggregation.
 109      $aggdata = array(
 110          'course'        => $data->id,
 111          'criteriatype'  => null
 112      );
 113      $aggregation = new completion_aggregation($aggdata);
 114      $aggregation->setMethod($data->overall_aggregation);
 115      $aggregation->save();
 116  
 117      // Handle activity aggregation.
 118      if (empty($data->activity_aggregation)) {
 119          $data->activity_aggregation = 0;
 120      }
 121  
 122      $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ACTIVITY;
 123      $aggregation = new completion_aggregation($aggdata);
 124      $aggregation->setMethod($data->activity_aggregation);
 125      $aggregation->save();
 126  
 127      // Handle course aggregation.
 128      if (empty($data->course_aggregation)) {
 129          $data->course_aggregation = 0;
 130      }
 131  
 132      $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_COURSE;
 133      $aggregation = new completion_aggregation($aggdata);
 134      $aggregation->setMethod($data->course_aggregation);
 135      $aggregation->save();
 136  
 137      // Handle role aggregation.
 138      if (empty($data->role_aggregation)) {
 139          $data->role_aggregation = 0;
 140      }
 141  
 142      $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ROLE;
 143      $aggregation = new completion_aggregation($aggdata);
 144      $aggregation->setMethod($data->role_aggregation);
 145      $aggregation->save();
 146  
 147      // Trigger an event for course module completion changed.
 148      $event = \core\event\course_completion_updated::create(
 149              array(
 150                  'courseid' => $course->id,
 151                  'context' => context_course::instance($course->id)
 152                  )
 153              );
 154      $event->trigger();
 155  
 156      // Redirect to the course main page.
 157      $url = new moodle_url('/course/view.php', array('id' => $course->id));
 158      redirect($url);
 159  }
 160  
 161  $renderer = $PAGE->get_renderer('core_course', 'bulk_activity_completion');
 162  
 163  // Print the form.
 164  echo $OUTPUT->header();
 165  
 166  $actionbar = new \core_course\output\completion_action_bar($course->id, $PAGE->url);
 167  echo $renderer->render_course_completion_action_bar($actionbar);
 168  
 169  echo $OUTPUT->heading(get_string('editcoursecompletionsettings', 'core_completion'));
 170  
 171  $form->display();
 172  
 173  echo $OUTPUT->footer();