Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * Toggles the manual completion flag for a particular activity or course completion
  20   * and the current user.
  21   *
  22   * If by student params: course=2
  23   * If by manager params: course=2&user=4&rolec=3&sesskey=ghfgsdf
  24   *
  25   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   * @package course
  27   */
  28  
  29  require_once('../config.php');
  30  require_once($CFG->libdir.'/completionlib.php');
  31  
  32  // Parameters
  33  $cmid = optional_param('id', 0, PARAM_INT);
  34  $courseid = optional_param('course', 0, PARAM_INT);
  35  $confirm = optional_param('confirm', 0, PARAM_BOOL);
  36  
  37  // Check if we are marking a user complete via the completion report.
  38  $user = optional_param('user', 0, PARAM_INT);
  39  $rolec = optional_param('rolec', 0, PARAM_INT);
  40  
  41  if (!$cmid && !$courseid) {
  42      print_error('invalidarguments');
  43  }
  44  
  45  // Process self completion
  46  if ($courseid) {
  47      $PAGE->set_url(new moodle_url('/course/togglecompletion.php', array('course'=>$courseid)));
  48  
  49      // Check user is logged in
  50      $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  51      $context = context_course::instance($course->id);
  52      require_login($course);
  53  
  54      $completion = new completion_info($course);
  55      $trackeduser = ($user ? $user : $USER->id);
  56  
  57      if (!$completion->is_enabled()) {
  58          throw new moodle_exception('completionnotenabled', 'completion');
  59      } else if (!$completion->is_tracked_user($trackeduser)) {
  60          throw new moodle_exception('nottracked', 'completion');
  61      }
  62  
  63      if ($user && $rolec) {
  64          require_sesskey();
  65  
  66          completion_criteria::factory(array('id'=>$rolec, 'criteriatype'=>COMPLETION_CRITERIA_TYPE_ROLE)); //TODO: this is dumb, because it does not fetch the data?!?!
  67          $criteria = completion_criteria_role::fetch(array('id'=>$rolec));
  68  
  69          if ($criteria and user_has_role_assignment($USER->id, $criteria->role, $context->id)) {
  70              $criteria_completions = $completion->get_completions($user, COMPLETION_CRITERIA_TYPE_ROLE);
  71  
  72              foreach ($criteria_completions as $criteria_completion) {
  73                  if ($criteria_completion->criteriaid == $rolec) {
  74                      $criteria->complete($criteria_completion);
  75                      break;
  76                  }
  77              }
  78          }
  79  
  80          // Return to previous page
  81          $referer = get_local_referer(false);
  82          if (!empty($referer)) {
  83              redirect($referer);
  84          } else {
  85              redirect('view.php?id='.$course->id);
  86          }
  87  
  88      } else {
  89  
  90          // Confirm with user
  91          if ($confirm and confirm_sesskey()) {
  92              $completion = $completion->get_completion($USER->id, COMPLETION_CRITERIA_TYPE_SELF);
  93  
  94              if (!$completion) {
  95                  print_error('noselfcompletioncriteria', 'completion');
  96              }
  97  
  98              // Check if the user has already marked themselves as complete
  99              if ($completion->is_complete()) {
 100                  print_error('useralreadymarkedcomplete', 'completion');
 101              }
 102  
 103              $completion->mark_complete();
 104  
 105              redirect($CFG->wwwroot.'/course/view.php?id='.$courseid);
 106              return;
 107          }
 108  
 109          $strconfirm = get_string('confirmselfcompletion', 'completion');
 110          $PAGE->set_title($strconfirm);
 111          $PAGE->set_heading($course->fullname);
 112          $PAGE->navbar->add($strconfirm);
 113          echo $OUTPUT->header();
 114          $buttoncontinue = new single_button(new moodle_url('/course/togglecompletion.php', array('course'=>$courseid, 'confirm'=>1, 'sesskey'=>sesskey())), get_string('yes'), 'post');
 115          $buttoncancel   = new single_button(new moodle_url('/course/view.php', array('id'=>$courseid)), get_string('no'), 'get');
 116          echo $OUTPUT->confirm($strconfirm, $buttoncontinue, $buttoncancel);
 117          echo $OUTPUT->footer();
 118          exit;
 119      }
 120  }
 121  
 122  
 123  $targetstate = required_param('completionstate', PARAM_INT);
 124  $fromajax    = optional_param('fromajax', 0, PARAM_INT);
 125  
 126  $PAGE->set_url('/course/togglecompletion.php', array('id'=>$cmid, 'completionstate'=>$targetstate));
 127  
 128  switch($targetstate) {
 129      case COMPLETION_COMPLETE:
 130      case COMPLETION_INCOMPLETE:
 131          break;
 132      default:
 133          print_error('unsupportedstate');
 134  }
 135  
 136  // Get course-modules entry
 137  $cm = get_coursemodule_from_id(null, $cmid, null, true, MUST_EXIST);
 138  $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
 139  
 140  // Check user is logged in
 141  require_login($course, false, $cm);
 142  require_capability('moodle/course:togglecompletion', context_module::instance($cmid));
 143  
 144  if (isguestuser() or !confirm_sesskey()) {
 145      print_error('error');
 146  }
 147  
 148  // Set up completion object and check it is enabled.
 149  $completion = new completion_info($course);
 150  if (!$completion->is_enabled()) {
 151      throw new moodle_exception('completionnotenabled', 'completion');
 152  }
 153  
 154  // NOTE: All users are allowed to toggle their completion state, including
 155  // users for whom completion information is not directly tracked. (I.e. even
 156  // if you are a teacher, or admin who is not enrolled, you can still toggle
 157  // your own completion state. You just don't appear on the reports.)
 158  
 159  // Check completion state is manual
 160  if($cm->completion != COMPLETION_TRACKING_MANUAL) {
 161      error_or_ajax('cannotmanualctrack', $fromajax);
 162  }
 163  
 164  $completion->update_state($cm, $targetstate);
 165  
 166  // And redirect back to course
 167  if ($fromajax) {
 168      print 'OK';
 169  } else {
 170      // In case of use in other areas of code we allow a 'backto' parameter,
 171      // otherwise go back to course page
 172  
 173      if ($backto = optional_param('backto', null, PARAM_URL)) {
 174          redirect($backto);
 175      } else {
 176          redirect(course_get_url($course, $cm->sectionnum));
 177      }
 178  }
 179  
 180  // utility functions
 181  
 182  function error_or_ajax($message, $fromajax) {
 183      if ($fromajax) {
 184          print get_string($message, 'error');
 185          exit;
 186      } else {
 187          print_error($message);
 188      }
 189  }
 190