|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 00030 require_once($CFG->libdir.'/completionlib.php'); 00031 00036 class block_completionstatus extends block_base { 00037 00038 public function init() { 00039 $this->title = get_string('completionstatus', 'block_completionstatus'); 00040 } 00041 00042 public function get_content() { 00043 global $USER, $CFG, $DB, $COURSE; 00044 00045 // If content is cached 00046 if ($this->content !== NULL) { 00047 return $this->content; 00048 } 00049 00050 // Create empty content 00051 $this->content = new stdClass; 00052 00053 // Can edit settings? 00054 $can_edit = has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $this->page->course->id)); 00055 00056 // Get course completion data 00057 $info = new completion_info($this->page->course); 00058 00059 // Don't display if completion isn't enabled! 00060 if (!completion_info::is_enabled_for_site()) { 00061 if ($can_edit) { 00062 $this->content->text = get_string('completionnotenabledforsite', 'completion'); 00063 } 00064 return $this->content; 00065 00066 } else if (!$info->is_enabled()) { 00067 if ($can_edit) { 00068 $this->content->text = get_string('completionnotenabledforcourse', 'completion'); 00069 } 00070 return $this->content; 00071 } 00072 00073 // Load criteria to display 00074 $completions = $info->get_completions($USER->id); 00075 00076 // Check if this course has any criteria 00077 if (empty($completions)) { 00078 if ($can_edit) { 00079 $this->content->text = get_string('nocriteriaset', 'completion'); 00080 } 00081 return $this->content; 00082 } 00083 00084 // Check this user is enroled 00085 if (!$info->is_tracked_user($USER->id)) { 00086 // If not enrolled, but are can view the report: 00087 if (has_capability('report/completion:view', get_context_instance(CONTEXT_COURSE, $COURSE->id))) { 00088 $this->content->text = '<a href="'.$CFG->wwwroot.'/report/completion/index.php?course='.$COURSE->id. 00089 '">'.get_string('viewcoursereport', 'completion').'</a>'; 00090 return $this->content; 00091 } 00092 00093 // Otherwise, show error 00094 $this->content->text = get_string('notenroled', 'completion'); 00095 return $this->content; 00096 } 00097 00098 // Generate markup for criteria statuses 00099 $shtml = ''; 00100 00101 // For aggregating activity completion 00102 $activities = array(); 00103 $activities_complete = 0; 00104 00105 // For aggregating course prerequisites 00106 $prerequisites = array(); 00107 $prerequisites_complete = 0; 00108 00109 // Flag to set if current completion data is inconsistent with 00110 // what is stored in the database 00111 $pending_update = false; 00112 00113 // Loop through course criteria 00114 foreach ($completions as $completion) { 00115 00116 $criteria = $completion->get_criteria(); 00117 $complete = $completion->is_complete(); 00118 00119 if (!$pending_update && $criteria->is_pending($completion)) { 00120 $pending_update = true; 00121 } 00122 00123 // Activities are a special case, so cache them and leave them till last 00124 if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) { 00125 $activities[$criteria->moduleinstance] = $complete; 00126 00127 if ($complete) { 00128 $activities_complete++; 00129 } 00130 00131 continue; 00132 } 00133 00134 // Prerequisites are also a special case, so cache them and leave them till last 00135 if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_COURSE) { 00136 $prerequisites[$criteria->courseinstance] = $complete; 00137 00138 if ($complete) { 00139 $prerequisites_complete++; 00140 } 00141 00142 continue; 00143 } 00144 00145 $shtml .= '<tr><td>'; 00146 $shtml .= $criteria->get_title(); 00147 $shtml .= '</td><td style="text-align: right">'; 00148 $shtml .= $completion->get_status(); 00149 $shtml .= '</td></tr>'; 00150 } 00151 00152 // Aggregate activities 00153 if (!empty($activities)) { 00154 00155 $shtml .= '<tr><td>'; 00156 $shtml .= get_string('activitiescompleted', 'completion'); 00157 $shtml .= '</td><td style="text-align: right">'; 00158 $a = new stdClass(); 00159 $a->first = $activities_complete; 00160 $a->second = count($activities); 00161 $shtml .= get_string('firstofsecond', 'block_completionstatus', $a); 00162 $shtml .= '</td></tr>'; 00163 } 00164 00165 // Aggregate prerequisites 00166 if (!empty($prerequisites)) { 00167 00168 $phtml = '<tr><td>'; 00169 $phtml .= get_string('prerequisitescompleted', 'completion'); 00170 $phtml .= '</td><td style="text-align: right">'; 00171 $a = new stdClass(); 00172 $a->first = $prerequisites_complete; 00173 $a->second = count($prerequisites); 00174 $shtml .= get_string('firstofsecond', 'block_completionstatus', $a); 00175 $phtml .= '</td></tr>'; 00176 00177 $shtml = $phtml . $shtml; 00178 } 00179 00180 // Display completion status 00181 $this->content->text = '<table width="100%" style="font-size: 90%;"><tbody>'; 00182 $this->content->text .= '<tr><td colspan="2"><b>'.get_string('status').':</b> '; 00183 00184 // Is course complete? 00185 $coursecomplete = $info->is_course_complete($USER->id); 00186 00187 // Load course completion 00188 $params = array( 00189 'userid' => $USER->id, 00190 'course' => $COURSE->id 00191 ); 00192 $ccompletion = new completion_completion($params); 00193 00194 // Has this user completed any criteria? 00195 $criteriacomplete = $info->count_course_user_data($USER->id); 00196 00197 if ($pending_update) { 00198 $this->content->text .= '<i>'.get_string('pending', 'completion').'</i>'; 00199 } else if ($coursecomplete) { 00200 $this->content->text .= get_string('complete'); 00201 } else if (!$criteriacomplete && !$ccompletion->timestarted) { 00202 $this->content->text .= '<i>'.get_string('notyetstarted', 'completion').'</i>'; 00203 } else { 00204 $this->content->text .= '<i>'.get_string('inprogress','completion').'</i>'; 00205 } 00206 00207 $this->content->text .= '</td></tr>'; 00208 $this->content->text .= '<tr><td colspan="2">'; 00209 00210 // Get overall aggregation method 00211 $overall = $info->get_aggregation_method(); 00212 00213 if ($overall == COMPLETION_AGGREGATION_ALL) { 00214 $this->content->text .= get_string('criteriarequiredall', 'completion'); 00215 } else { 00216 $this->content->text .= get_string('criteriarequiredany', 'completion'); 00217 } 00218 00219 $this->content->text .= ':</td></tr>'; 00220 $this->content->text .= '<tr><td><b>'.get_string('requiredcriteria', 'completion').'</b></td><td style="text-align: right"><b>'.get_string('status').'</b></td></tr>'; 00221 $this->content->text .= $shtml.'</tbody></table>'; 00222 00223 // Display link to detailed view 00224 $this->content->footer = '<br><a href="'.$CFG->wwwroot.'/blocks/completionstatus/details.php?course='.$COURSE->id.'">'.get_string('moredetails', 'completion').'</a>'; 00225 00226 return $this->content; 00227 } 00228 }