Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/report/completion/user.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00026 require('../../config.php');
00027 require_once($CFG->dirroot.'/report/completion/lib.php');
00028 require_once($CFG->libdir.'/completionlib.php');
00029 
00030 $userid   = required_param('id', PARAM_INT);
00031 $courseid = required_param('course', PARAM_INT);
00032 
00033 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
00034 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
00035 
00036 $coursecontext   = context_course::instance($course->id);
00037 $personalcontext = context_user::instance($user->id);
00038 
00039 if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
00040         and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
00041     //TODO: do not require parents to be enrolled in courses - this is a hack!
00042     require_login();
00043     $PAGE->set_course($course);
00044 } else {
00045     require_login($course);
00046 }
00047 
00048 if (!report_completion_can_access_user_report($user, $course, true)) {
00049     // this should never happen
00050     error('Can not access user completion report');
00051 }
00052 
00053 add_to_log($course->id, 'course', 'report completion', "report/completion/user.php?id=$user->id&course=$course->id", $course->id);
00054 
00055 $stractivityreport = get_string('activityreport');
00056 
00057 $PAGE->set_pagelayout('admin');
00058 $PAGE->set_url('/report/completion/user.php', array('id'=>$user->id, 'course'=>$course->id));
00059 $PAGE->navigation->extend_for_user($user);
00060 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
00061 $PAGE->set_title("$course->shortname: $stractivityreport");
00062 $PAGE->set_heading($course->fullname);
00063 echo $OUTPUT->header();
00064 
00065 
00066 // Display course completion user report
00067 
00068 // Grab all courses the user is enrolled in and their completion status
00069 $sql = "
00070     SELECT DISTINCT
00071         c.id AS id
00072     FROM
00073         {course} c
00074     INNER JOIN
00075         {context} con
00076      ON con.instanceid = c.id
00077     INNER JOIN
00078         {role_assignments} ra
00079      ON ra.contextid = con.id
00080     INNER JOIN
00081         {enrol} e
00082      ON c.id = e.courseid
00083     INNER JOIN
00084         {user_enrolments} ue
00085      ON e.id = ue.enrolid AND ra.userid = ue.userid
00086     AND ra.userid = {$user->id}
00087 ";
00088 
00089 // Get roles that are tracked by course completion
00090 if ($roles = $CFG->gradebookroles) {
00091     $sql .= '
00092         AND ra.roleid IN ('.$roles.')
00093     ';
00094 }
00095 
00096 $sql .= '
00097     WHERE
00098         con.contextlevel = '.CONTEXT_COURSE.'
00099     AND c.enablecompletion = 1
00100 ';
00101 
00102 
00103 // If we are looking at a specific course
00104 if ($course->id != 1) {
00105     $sql .= '
00106         AND c.id = '.(int)$course->id.'
00107     ';
00108 }
00109 
00110 // Check if result is empty
00111 $rs = $DB->get_recordset_sql($sql);
00112 if (!$rs->valid()) {
00113 
00114     if ($course->id != 1) {
00115         $error = get_string('nocompletions', 'report_completion'); // TODO: missing string
00116     } else {
00117         $error = get_string('nocompletioncoursesenroled', 'report_completion'); // TODO: missing string
00118     }
00119 
00120     echo $OUTPUT->notification($error);
00121     $rs->close(); // not going to loop (but break), close rs
00122     echo $OUTPUT->footer();
00123     die();
00124 }
00125 
00126 // Categorize courses by their status
00127 $courses = array(
00128     'inprogress'    => array(),
00129     'complete'      => array(),
00130     'unstarted'     => array()
00131 );
00132 
00133 // Sort courses by the user's status in each
00134 foreach ($rs as $course_completion) {
00135     $c_info = new completion_info((object)$course_completion);
00136 
00137     // Is course complete?
00138     $coursecomplete = $c_info->is_course_complete($user->id);
00139 
00140     // Has this user completed any criteria?
00141     $criteriacomplete = $c_info->count_course_user_data($user->id);
00142 
00143     if ($coursecomplete) {
00144         $courses['complete'][] = $c_info;
00145     } else if ($criteriacomplete) {
00146         $courses['inprogress'][] = $c_info;
00147     } else {
00148         $courses['unstarted'][] = $c_info;
00149     }
00150 }
00151 $rs->close(); // after loop, close rs
00152 
00153 // Loop through course status groups
00154 foreach ($courses as $type => $infos) {
00155 
00156     // If there are courses with this status
00157     if (!empty($infos)) {
00158 
00159         echo '<h1 align="center">'.get_string($type, 'report_completion').'</h1>';
00160         echo '<table class="generalbox boxaligncenter">';
00161         echo '<tr class="ccheader">';
00162         echo '<th class="c0 header" scope="col">'.get_string('course').'</th>';
00163         echo '<th class="c1 header" scope="col">'.get_string('requiredcriteria', 'completion').'</th>';
00164         echo '<th class="c2 header" scope="col">'.get_string('status').'</th>';
00165         echo '<th class="c3 header" scope="col" width="15%">'.get_string('info').'</th>';
00166 
00167         if ($type === 'complete') {
00168             echo '<th class="c4 header" scope="col">'.get_string('completiondate', 'report_completion').'</th>';
00169         }
00170 
00171         echo '</tr>';
00172 
00173         // For each course
00174         foreach ($infos as $c_info) {
00175 
00176             // Get course info
00177             $c_course = $DB->get_record('course', array('id' => $c_info->course_id));
00178             $course_context = get_context_instance(CONTEXT_COURSE, $c_course->id, MUST_EXIST);
00179             $course_name = format_string($c_course->fullname, true, array('context' => $course_context));
00180 
00181             // Get completions
00182             $completions = $c_info->get_completions($user->id);
00183 
00184             // Save row data
00185             $rows = array();
00186 
00187             // For aggregating activity completion
00188             $activities = array();
00189             $activities_complete = 0;
00190 
00191             // For aggregating prerequisites
00192             $prerequisites = array();
00193             $prerequisites_complete = 0;
00194 
00195             // Loop through course criteria
00196             foreach ($completions as $completion) {
00197                 $criteria = $completion->get_criteria();
00198                 $complete = $completion->is_complete();
00199 
00200                 // Activities are a special case, so cache them and leave them till last
00201                 if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
00202                     $activities[$criteria->moduleinstance] = $complete;
00203 
00204                     if ($complete) {
00205                         $activities_complete++;
00206                     }
00207 
00208                     continue;
00209                 }
00210 
00211                 // Prerequisites are also a special case, so cache them and leave them till last
00212                 if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_COURSE) {
00213                     $prerequisites[$criteria->courseinstance] = $complete;
00214 
00215                     if ($complete) {
00216                         $prerequisites_complete++;
00217                     }
00218 
00219                     continue;
00220                 }
00221 
00222                 $row = array();
00223                 $row['title'] = $criteria->get_title();
00224                 $row['status'] = $completion->get_status();
00225                 $rows[] = $row;
00226             }
00227 
00228             // Aggregate activities
00229             if (!empty($activities)) {
00230 
00231                 $row = array();
00232                 $row['title'] = get_string('activitiescomplete', 'report_completion');
00233                 $row['status'] = $activities_complete.' of '.count($activities);
00234                 $rows[] = $row;
00235             }
00236 
00237             // Aggregate prerequisites
00238             if (!empty($prerequisites)) {
00239 
00240                 $row = array();
00241                 $row['title'] = get_string('prerequisitescompleted', 'completion');
00242                 $row['status'] = $prerequisites_complete.' of '.count($prerequisites);
00243                 array_splice($rows, 0, 0, array($row));
00244             }
00245 
00246             $first_row = true;
00247 
00248             // Print table
00249             foreach ($rows as $row) {
00250 
00251                 // Display course name on first row
00252                 if ($first_row) {
00253                     echo '<tr><td class="c0"><a href="'.$CFG->wwwroot.'/course/view.php?id='.$c_course->id.'">'.$course_name.'</a></td>';
00254                 } else {
00255                     echo '<tr><td class="c0"></td>';
00256                 }
00257 
00258                 echo '<td class="c1">';
00259                 echo $row['title'];
00260                 echo '</td><td class="c2">';
00261 
00262                 switch ($row['status']) {
00263                     case 'Yes':
00264                         echo get_string('complete');
00265                         break;
00266 
00267                     case 'No':
00268                         echo get_string('incomplete', 'report_completion');
00269                         break;
00270 
00271                     default:
00272                         echo $row['status'];
00273                 }
00274 
00275                 // Display link on first row
00276                 echo '</td><td class="c3">';
00277                 if ($first_row) {
00278                     echo '<a href="'.$CFG->wwwroot.'/blocks/completionstatus/details.php?course='.$c_course->id.'&user='.$user->id.'">'.get_string('detailedview', 'report_completion').'</a>';
00279                 }
00280                 echo '</td>';
00281 
00282                 // Display completion date for completed courses on first row
00283                 if ($type === 'complete' && $first_row) {
00284                     $params = array(
00285                         'userid'    => $user->id,
00286                         'course'  => $c_course->id
00287                     );
00288 
00289                     $ccompletion = new completion_completion($params);
00290                     echo '<td class="c4">'.userdate($ccompletion->timecompleted, '%e %B %G').'</td>';
00291                 }
00292 
00293                 $first_row = false;
00294                 echo '</tr>';
00295             }
00296         }
00297 
00298         echo '</table>';
00299     }
00300 }
00301 
00302 
00303 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations