Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/course/togglecompletion.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00026 require_once('../config.php');
00027 require_once($CFG->libdir.'/completionlib.php');
00028 
00029 // Parameters
00030 $cmid = optional_param('id', 0, PARAM_INT);
00031 $courseid = optional_param('course', 0, PARAM_INT);
00032 $confirm = optional_param('confirm', 0, PARAM_BOOL);
00033 
00034 if (!$cmid && !$courseid) {
00035     print_error('invalidarguments');
00036 }
00037 
00038 // Process self completion
00039 if ($courseid) {
00040     $PAGE->set_url(new moodle_url('/course/togglecompletion.php', array('course'=>$courseid)));
00041 
00042     // Check user is logged in
00043     $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
00044     $context = get_context_instance(CONTEXT_COURSE, $course->id);
00045     require_login($course);
00046 
00047     $completion = new completion_info($course);
00048 
00049     // Check if we are marking a user complete via the completion report
00050     $user = optional_param('user', 0, PARAM_INT);
00051     $rolec = optional_param('rolec', 0, PARAM_INT);
00052 
00053     if ($user && $rolec) {
00054         require_sesskey();
00055 
00056         completion_criteria::factory((object) array('id'=>$rolec, 'criteriatype'=>COMPLETION_CRITERIA_TYPE_ROLE)); //TODO: this is dumb, because it does not fetch the data?!?!
00057         $criteria = completion_criteria_role::fetch(array('id'=>$rolec));
00058 
00059         if ($criteria and user_has_role_assignment($USER->id, $criteria->role, $context->id)) {
00060             $criteria_completions = $completion->get_completions($user, COMPLETION_CRITERIA_TYPE_ROLE);
00061 
00062             foreach ($criteria_completions as $criteria_completion) {
00063                 if ($criteria_completion->criteriaid == $rolec) {
00064                     $criteria->complete($criteria_completion);
00065                     break;
00066                 }
00067             }
00068         }
00069 
00070         // Return to previous page
00071         if (!empty($_SERVER['HTTP_REFERER'])) {
00072             redirect($_SERVER['HTTP_REFERER']);
00073         } else {
00074             redirect('view.php?id='.$course->id);
00075         }
00076 
00077     } else {
00078 
00079         // Confirm with user
00080         if ($confirm and confirm_sesskey()) {
00081             $completion = $completion->get_completion($USER->id, COMPLETION_CRITERIA_TYPE_SELF);
00082 
00083             if (!$completion) {
00084                 print_error('noselfcompletioncriteria', 'completion');
00085             }
00086 
00087             // Check if the user has already marked themselves as complete
00088             if ($completion->is_complete()) {
00089                 print_error('useralreadymarkedcomplete', 'completion');
00090             }
00091 
00092             $completion->mark_complete();
00093 
00094             redirect($CFG->wwwroot.'/course/view.php?id='.$courseid);
00095             return;
00096         }
00097 
00098         $strconfirm = get_string('confirmselfcompletion', 'completion');
00099         $PAGE->set_title($strconfirm);
00100         $PAGE->set_heading($course->fullname);
00101         $PAGE->navbar->add($strconfirm);
00102         echo $OUTPUT->header();
00103         $buttoncontinue = new single_button(new moodle_url('/course/togglecompletion.php', array('course'=>$courseid, 'confirm'=>1, 'sesskey'=>sesskey())), get_string('yes'), 'post');
00104         $buttoncancel   = new single_button(new moodle_url('/course/view.php', array('id'=>$courseid)), get_string('no'), 'get');
00105         echo $OUTPUT->confirm($strconfirm, $buttoncontinue, $buttoncancel);
00106         echo $OUTPUT->footer();
00107         exit;
00108     }
00109 }
00110 
00111 
00112 $targetstate = required_param('completionstate', PARAM_INT);
00113 $fromajax    = optional_param('fromajax', 0, PARAM_INT);
00114 
00115 $PAGE->set_url('/course/togglecompletion.php', array('id'=>$cmid, 'completionstate'=>$targetstate));
00116 
00117 switch($targetstate) {
00118     case COMPLETION_COMPLETE:
00119     case COMPLETION_INCOMPLETE:
00120         break;
00121     default:
00122         print_error('unsupportedstate');
00123 }
00124 
00125 // Get course-modules entry
00126 $cm = get_coursemodule_from_id(null, $cmid, null, false, MUST_EXIST);
00127 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
00128 
00129 // Check user is logged in
00130 require_login($course, false, $cm);
00131 
00132 if (isguestuser() or !confirm_sesskey()) {
00133     print_error('error');
00134 }
00135 
00136 // Now change state
00137 $completion = new completion_info($course);
00138 if (!$completion->is_enabled()) {
00139     die;
00140 }
00141 
00142 // Check completion state is manual
00143 if($cm->completion != COMPLETION_TRACKING_MANUAL) {
00144     error_or_ajax('cannotmanualctrack', $fromajax);
00145 }
00146 
00147 $completion->update_state($cm, $targetstate);
00148 
00149 // And redirect back to course
00150 if ($fromajax) {
00151     print 'OK';
00152 } else {
00153     // In case of use in other areas of code we allow a 'backto' parameter,
00154     // otherwise go back to course page
00155     $backto = optional_param('backto', 'view.php?id='.$course->id, PARAM_URL);
00156     redirect($backto);
00157 }
00158 
00159 // utility functions
00160 
00161 function error_or_ajax($message, $fromajax) {
00162     if ($fromajax) {
00163         print get_string($message, 'error');
00164         exit;
00165     } else {
00166         print_error($message);
00167     }
00168 }
00169 
 All Data Structures Namespaces Files Functions Variables Enumerations