PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/report/progress/index.php

https://bitbucket.org/kudutest/moodlegit
PHP | 428 lines | 298 code | 72 blank | 58 comment | 64 complexity | d0890c76308a87dece7ad757428ad3bc MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Activity progress reports
  18. *
  19. * @package report
  20. * @subpackage progress
  21. * @copyright 2008 Sam Marshall
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. require('../../config.php');
  25. require_once($CFG->libdir . '/completionlib.php');
  26. define('COMPLETION_REPORT_PAGE', 25);
  27. // Get course
  28. $id = required_param('course',PARAM_INT);
  29. $course = $DB->get_record('course',array('id'=>$id));
  30. if (!$course) {
  31. print_error('invalidcourseid');
  32. }
  33. $context = context_course::instance($course->id);
  34. // Sort (default lastname, optionally firstname)
  35. $sort = optional_param('sort','',PARAM_ALPHA);
  36. $firstnamesort = $sort == 'firstname';
  37. // CSV format
  38. $format = optional_param('format','',PARAM_ALPHA);
  39. $excel = $format == 'excelcsv';
  40. $csv = $format == 'csv' || $excel;
  41. // Paging
  42. $start = optional_param('start', 0, PARAM_INT);
  43. $sifirst = optional_param('sifirst', 'all', PARAM_ALPHA);
  44. $silast = optional_param('silast', 'all', PARAM_ALPHA);
  45. $start = optional_param('start', 0, PARAM_INT);
  46. // Whether to show extra user identity information
  47. $extrafields = get_extra_user_fields($context);
  48. $leftcols = 1 + count($extrafields);
  49. function csv_quote($value) {
  50. global $excel;
  51. if ($excel) {
  52. return textlib::convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE');
  53. } else {
  54. return '"'.str_replace('"',"'",$value).'"';
  55. }
  56. }
  57. $url = new moodle_url('/report/progress/index.php', array('course'=>$id));
  58. if ($sort !== '') {
  59. $url->param('sort', $sort);
  60. }
  61. if ($format !== '') {
  62. $url->param('format', $format);
  63. }
  64. if ($start !== 0) {
  65. $url->param('start', $start);
  66. }
  67. $PAGE->set_url($url);
  68. $PAGE->set_pagelayout('report');
  69. require_login($course);
  70. // Check basic permission
  71. require_capability('report/progress:view',$context);
  72. // Get group mode
  73. $group = groups_get_course_group($course,true); // Supposed to verify group
  74. if ($group===0 && $course->groupmode==SEPARATEGROUPS) {
  75. require_capability('moodle/site:accessallgroups',$context);
  76. }
  77. // Get data on activities and progress of all users, and give error if we've
  78. // nothing to display (no users or no activities)
  79. $reportsurl = $CFG->wwwroot.'/course/report.php?id='.$course->id;
  80. $completion = new completion_info($course);
  81. $activities = $completion->get_activities();
  82. // Generate where clause
  83. $where = array();
  84. $where_params = array();
  85. if ($sifirst !== 'all') {
  86. $where[] = $DB->sql_like('u.firstname', ':sifirst', false);
  87. $where_params['sifirst'] = $sifirst.'%';
  88. }
  89. if ($silast !== 'all') {
  90. $where[] = $DB->sql_like('u.lastname', ':silast', false);
  91. $where_params['silast'] = $silast.'%';
  92. }
  93. // Get user match count
  94. $total = $completion->get_num_tracked_users(implode(' AND ', $where), $where_params, $group);
  95. // Total user count
  96. $grandtotal = $completion->get_num_tracked_users('', array(), $group);
  97. // Get user data
  98. $progress = array();
  99. if ($total) {
  100. $progress = $completion->get_progress_all(
  101. implode(' AND ', $where),
  102. $where_params,
  103. $group,
  104. $firstnamesort ? 'u.firstname ASC' : 'u.lastname ASC',
  105. $csv ? 0 : COMPLETION_REPORT_PAGE,
  106. $csv ? 0 : $start,
  107. $context
  108. );
  109. }
  110. if ($csv && $grandtotal && count($activities)>0) { // Only show CSV if there are some users/actvs
  111. $shortname = format_string($course->shortname, true, array('context' => $context));
  112. header('Content-Disposition: attachment; filename=progress.'.
  113. preg_replace('/[^a-z0-9-]/','_',textlib::strtolower(strip_tags($shortname))).'.csv');
  114. // Unicode byte-order mark for Excel
  115. if ($excel) {
  116. header('Content-Type: text/csv; charset=UTF-16LE');
  117. print chr(0xFF).chr(0xFE);
  118. $sep="\t".chr(0);
  119. $line="\n".chr(0);
  120. } else {
  121. header('Content-Type: text/csv; charset=UTF-8');
  122. $sep=",";
  123. $line="\n";
  124. }
  125. } else {
  126. // Use SVG to draw sideways text if supported
  127. $svgcleverness = can_use_rotated_text();
  128. // Navigation and header
  129. $strreports = get_string("reports");
  130. $strcompletion = get_string('activitycompletion', 'completion');
  131. $PAGE->set_title($strcompletion);
  132. $PAGE->set_heading($course->fullname);
  133. echo $OUTPUT->header();
  134. if ($svgcleverness) {
  135. $PAGE->requires->js('/report/progress/textrotate.js');
  136. $PAGE->requires->js_function_call('textrotate_init', null, true);
  137. }
  138. // Handle groups (if enabled)
  139. groups_print_course_menu($course,$CFG->wwwroot.'/report/progress/?course='.$course->id);
  140. }
  141. if (count($activities)==0) {
  142. echo $OUTPUT->container(get_string('err_noactivities', 'completion'), 'errorbox errorboxcontent');
  143. echo $OUTPUT->footer();
  144. exit;
  145. }
  146. // If no users in this course what-so-ever
  147. if (!$grandtotal) {
  148. echo $OUTPUT->container(get_string('err_nousers', 'completion'), 'errorbox errorboxcontent');
  149. echo $OUTPUT->footer();
  150. exit;
  151. }
  152. // Build link for paging
  153. $link = $CFG->wwwroot.'/report/progress/?course='.$course->id;
  154. if (strlen($sort)) {
  155. $link .= '&amp;sort='.$sort;
  156. }
  157. $link .= '&amp;start=';
  158. // Build the the page by Initial bar
  159. $initials = array('first', 'last');
  160. $alphabet = explode(',', get_string('alphabet', 'langconfig'));
  161. $pagingbar = '';
  162. foreach ($initials as $initial) {
  163. $var = 'si'.$initial;
  164. $othervar = $initial == 'first' ? 'silast' : 'sifirst';
  165. $othervar = $$othervar != 'all' ? "&amp;{$othervar}={$$othervar}" : '';
  166. $pagingbar .= ' <div class="initialbar '.$initial.'initial">';
  167. $pagingbar .= get_string($initial.'name').':&nbsp;';
  168. if ($$var == 'all') {
  169. $pagingbar .= '<strong>'.get_string('all').'</strong> ';
  170. }
  171. else {
  172. $pagingbar .= "<a href=\"{$link}{$othervar}\">".get_string('all').'</a> ';
  173. }
  174. foreach ($alphabet as $letter) {
  175. if ($$var === $letter) {
  176. $pagingbar .= '<strong>'.$letter.'</strong> ';
  177. }
  178. else {
  179. $pagingbar .= "<a href=\"$link&amp;$var={$letter}{$othervar}\">$letter</a> ";
  180. }
  181. }
  182. $pagingbar .= '</div>';
  183. }
  184. // Do we need a paging bar?
  185. if ($total > COMPLETION_REPORT_PAGE) {
  186. // Paging bar
  187. $pagingbar .= '<div class="paging">';
  188. $pagingbar .= get_string('page').': ';
  189. $sistrings = array();
  190. if ($sifirst != 'all') {
  191. $sistrings[] = "sifirst={$sifirst}";
  192. }
  193. if ($silast != 'all') {
  194. $sistrings[] = "silast={$silast}";
  195. }
  196. $sistring = !empty($sistrings) ? '&amp;'.implode('&amp;', $sistrings) : '';
  197. // Display previous link
  198. if ($start > 0) {
  199. $pstart = max($start - COMPLETION_REPORT_PAGE, 0);
  200. $pagingbar .= "(<a class=\"previous\" href=\"{$link}{$pstart}{$sistring}\">".get_string('previous').'</a>)&nbsp;';
  201. }
  202. // Create page links
  203. $curstart = 0;
  204. $curpage = 0;
  205. while ($curstart < $total) {
  206. $curpage++;
  207. if ($curstart == $start) {
  208. $pagingbar .= '&nbsp;'.$curpage.'&nbsp;';
  209. } else {
  210. $pagingbar .= "&nbsp;<a href=\"{$link}{$curstart}{$sistring}\">$curpage</a>&nbsp;";
  211. }
  212. $curstart += COMPLETION_REPORT_PAGE;
  213. }
  214. // Display next link
  215. $nstart = $start + COMPLETION_REPORT_PAGE;
  216. if ($nstart < $total) {
  217. $pagingbar .= "&nbsp;(<a class=\"next\" href=\"{$link}{$nstart}{$sistring}\">".get_string('next').'</a>)';
  218. }
  219. $pagingbar .= '</div>';
  220. }
  221. // Okay, let's draw the table of progress info,
  222. // Start of table
  223. if (!$csv) {
  224. print '<br class="clearer"/>'; // ugh
  225. print $pagingbar;
  226. if (!$total) {
  227. echo $OUTPUT->heading(get_string('nothingtodisplay'));
  228. echo $OUTPUT->footer();
  229. exit;
  230. }
  231. print '<div id="completion-progress-wrapper" class="no-overflow">';
  232. print '<table id="completion-progress" class="generaltable flexible boxaligncenter" style="text-align:left"><tr style="vertical-align:top">';
  233. // User heading / sort option
  234. print '<th scope="col" class="completion-sortchoice">';
  235. $sistring = "&amp;silast={$silast}&amp;sifirst={$sifirst}";
  236. if ($firstnamesort) {
  237. print
  238. get_string('firstname')." / <a href=\"./?course={$course->id}{$sistring}\">".
  239. get_string('lastname').'</a>';
  240. } else {
  241. print "<a href=\"./?course={$course->id}&amp;sort=firstname{$sistring}\">".
  242. get_string('firstname').'</a> / '.
  243. get_string('lastname');
  244. }
  245. print '</th>';
  246. // Print user identity columns
  247. foreach ($extrafields as $field) {
  248. echo '<th scope="col" class="completion-identifyfield">' .
  249. get_user_field_name($field) . '</th>';
  250. }
  251. } else {
  252. foreach ($extrafields as $field) {
  253. echo $sep . csv_quote(get_user_field_name($field));
  254. }
  255. }
  256. // Activities
  257. foreach($activities as $activity) {
  258. $activity->datepassed = $activity->completionexpected && $activity->completionexpected <= time();
  259. $activity->datepassedclass=$activity->datepassed ? 'completion-expired' : '';
  260. if ($activity->completionexpected) {
  261. $datetext=userdate($activity->completionexpected,get_string('strftimedate','langconfig'));
  262. } else {
  263. $datetext='';
  264. }
  265. // Some names (labels) come URL-encoded and can be very long, so shorten them
  266. $activity->name = shorten_text($activity->name);
  267. if ($csv) {
  268. print $sep.csv_quote(strip_tags($activity->name)).$sep.csv_quote($datetext);
  269. } else {
  270. $formattedactivityname = format_string($activity->name, true, array('context' => $context));
  271. print '<th scope="col" class="'.$activity->datepassedclass.'">'.
  272. '<a href="'.$CFG->wwwroot.'/mod/'.$activity->modname.
  273. '/view.php?id='.$activity->id.'" title="' . $formattedactivityname . '">'.
  274. '<img src="'.$OUTPUT->pix_url('icon', $activity->modname).'" alt="'.
  275. get_string('modulename',$activity->modname).'" /> <span class="completion-activityname">'.
  276. $formattedactivityname.'</span></a>';
  277. if ($activity->completionexpected) {
  278. print '<div class="completion-expected"><span>'.$datetext.'</span></div>';
  279. }
  280. print '</th>';
  281. }
  282. }
  283. if ($csv) {
  284. print $line;
  285. } else {
  286. print '</tr>';
  287. }
  288. // Row for each user
  289. foreach($progress as $user) {
  290. // User name
  291. if ($csv) {
  292. print csv_quote(fullname($user));
  293. foreach ($extrafields as $field) {
  294. echo $sep . csv_quote($user->{$field});
  295. }
  296. } else {
  297. print '<tr><th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
  298. $user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></th>';
  299. foreach ($extrafields as $field) {
  300. echo '<td>' . s($user->{$field}) . '</td>';
  301. }
  302. }
  303. // Progress for each activity
  304. foreach($activities as $activity) {
  305. // Get progress information and state
  306. if (array_key_exists($activity->id,$user->progress)) {
  307. $thisprogress=$user->progress[$activity->id];
  308. $state=$thisprogress->completionstate;
  309. $date=userdate($thisprogress->timemodified);
  310. } else {
  311. $state=COMPLETION_INCOMPLETE;
  312. $date='';
  313. }
  314. // Work out how it corresponds to an icon
  315. switch($state) {
  316. case COMPLETION_INCOMPLETE : $completiontype='n'; break;
  317. case COMPLETION_COMPLETE : $completiontype='y'; break;
  318. case COMPLETION_COMPLETE_PASS : $completiontype='pass'; break;
  319. case COMPLETION_COMPLETE_FAIL : $completiontype='fail'; break;
  320. }
  321. $completionicon='completion-'.
  322. ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
  323. '-'.$completiontype;
  324. $modcontext = context_module::instance($activity->id);
  325. $describe = get_string('completion-' . $completiontype, 'completion');
  326. $a=new StdClass;
  327. $a->state=$describe;
  328. $a->date=$date;
  329. $a->user=fullname($user);
  330. $a->activity = format_string($activity->name, true, array('context' => $modcontext));
  331. $fulldescribe=get_string('progress-title','completion',$a);
  332. if ($csv) {
  333. print $sep.csv_quote($describe).$sep.csv_quote($date);
  334. } else {
  335. print '<td class="completion-progresscell '.$activity->datepassedclass.'">'.
  336. '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
  337. '" alt="'.$describe.'" title="'.$fulldescribe.'" /></td>';
  338. }
  339. }
  340. if ($csv) {
  341. print $line;
  342. } else {
  343. print '</tr>';
  344. }
  345. }
  346. if ($csv) {
  347. exit;
  348. }
  349. print '</table>';
  350. print '</div>';
  351. print $pagingbar;
  352. print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
  353. '&amp;format=csv">'.get_string('csvdownload','completion').'</a></li>
  354. <li><a href="index.php?course='.$course->id.'&amp;format=excelcsv">'.
  355. get_string('excelcsvdownload','completion').'</a></li></ul>';
  356. echo $OUTPUT->footer();