[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/course/ -> togglecompletion.php (source)

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1