gradebook_flatview.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php // $Id: $
  2. /* For licensing terms, see /license.txt */
  3. $language_file = 'gradebook';
  4. //$cidReset= true;
  5. require_once '../inc/global.inc.php';
  6. require_once 'lib/be.inc.php';
  7. require_once 'lib/gradebook_functions.inc.php';
  8. require_once 'lib/fe/dataform.class.php';
  9. require_once 'lib/fe/userform.class.php';
  10. require_once 'lib/flatview_data_generator.class.php';
  11. require_once 'lib/fe/flatviewtable.class.php';
  12. require_once 'lib/fe/displaygradebook.php';
  13. require_once 'lib/fe/exportgradebook.php';
  14. require_once 'lib/scoredisplay.class.php';
  15. //require_once api_get_path(LIBRARY_PATH).'ezpdf/class.ezpdf.php'; // This is the old library for pdf-export (non UTF-8 compatible).
  16. define('_MPDF_PATH', api_get_path(LIBRARY_PATH).'mpdf/');
  17. require_once _MPDF_PATH.'mpdf.php';
  18. if (!class_exists('HTML_Table')) { require_once api_get_path(LIBRARY_PATH).'pear/HTML/Table.php'; }
  19. api_block_anonymous_users();
  20. block_students();
  21. if (isset ($_POST['submit']) && isset ($_POST['keyword'])) {
  22. header('Location: '.api_get_self().'?selectcat='.Security::remove_XSS($_GET['selectcat']).'&search='.Security::remove_XSS($_POST['keyword']));
  23. exit;
  24. }
  25. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
  26. $interbreadcrumb[] = array (
  27. 'url' => $_SESSION['gradebook_dest'],
  28. 'name' => get_lang('Gradebook')
  29. );
  30. $showeval = isset($_POST['showeval']) ? '1' : '0';
  31. $showlink = isset($_POST['showlink']) ? '1' : '0';
  32. if (($showlink == '0') && ($showeval == '0')) {
  33. $showlink = '1';
  34. $showeval = '1';
  35. }
  36. $cat = Category::load($_REQUEST['selectcat']);
  37. if (isset($_GET['userid'])) {
  38. $userid = Security::remove_XSS($_GET['userid']);
  39. } else {
  40. $userid = '';
  41. }
  42. if ($showeval) {
  43. $alleval = $cat[0]->get_evaluations($userid, true);
  44. } else {
  45. $alleval = null;
  46. }
  47. if ($showlink) {
  48. $alllinks = $cat[0]->get_links($userid, true);
  49. } else {
  50. $alllinks = null;
  51. }
  52. if (isset($export_flatview_form) && (!$file_type == 'pdf')) {
  53. Display :: display_normal_message($export_flatview_form->toHtml(), false);
  54. }
  55. if (isset($_GET['selectcat'])) {
  56. $category_id = Security::remove_XSS($_GET['selectcat']);
  57. } else {
  58. $category_id = '';
  59. }
  60. $simple_search_form = new UserForm(UserForm :: TYPE_SIMPLE_SEARCH, null, 'simple_search_form', null, api_get_self() . '?selectcat=' . $category_id);
  61. $values = $simple_search_form->exportValues();
  62. $keyword = '';
  63. if (isset($_GET['search']) && !empty($_GET['search'])) {
  64. $keyword = Security::remove_XSS($_GET['search']);
  65. }
  66. if ($simple_search_form->validate() && (empty($keyword))) {
  67. $keyword = $values['keyword'];
  68. }
  69. if (!empty($keyword)) {
  70. $users = find_students($keyword);
  71. } else {
  72. if (isset($alleval) && isset($alllinks)) {
  73. $users = get_all_users($alleval, $alllinks);
  74. } else {
  75. $users = null;
  76. }
  77. }
  78. if (isset ($_GET['exportpdf'])) {
  79. $interbreadcrumb[] = array (
  80. 'url' => api_get_self().'?selectcat=' . Security::remove_XSS($_GET['selectcat']),
  81. 'name' => get_lang('FlatView')
  82. );
  83. $export_pdf_form = new DataForm(DataForm :: TYPE_EXPORT_PDF, 'export_pdf_form', null, api_get_self().'?exportpdf=&offset='.$_GET['offset'].'&selectcat='.$_GET['selectcat'], '_blank');
  84. if ($export_pdf_form->validate()) {
  85. // Beginning of PDF report creation
  86. $printable_data = get_printable_data($users, $alleval, $alllinks);
  87. $export = $export_pdf_form->exportValues();
  88. // Reading report's CSS
  89. //$css_file = api_get_path(TO_SYS, WEB_CSS_PATH).api_get_setting('stylesheets').'/print.css';
  90. $css_file = api_get_path(SYS_CODE_PATH).'gradebook/print.css';
  91. $css = file_exists($css_file) ? @file_get_contents($css_file) : '';
  92. // HTML report creation first
  93. $time = time();
  94. $cat_name = trim($cat[0]->get_name());
  95. $course_code = trim($cat[0]->get_course_code());
  96. $report_name = $course_code;
  97. if (!empty($cat_name) && $report_name != $cat_name) {
  98. $report_name .= ' - '.$cat_name;
  99. }
  100. $organization = api_get_setting('Institution');
  101. $creator = api_get_person_name($GLOBALS['_user']['firstName'], $GLOBALS['_user']['lastName']);
  102. $html = '';
  103. if (!empty($organization)) {
  104. $html .= '<h2 align="center">'.$organization.'</h2>';
  105. }
  106. $html .= '<h1 align="center">'.get_lang('FlatView').'</h1>';
  107. $html .= '<p><strong>'.$report_name.'</strong></p>';
  108. $html .= '<p><strong>'.api_format_date(DATE_TIME_FORMAT_LONG, $time).'</strong></p>';
  109. $html .= '<p><strong>'.get_lang('By').': '.$creator.'</strong></p>';
  110. $columns = count($printable_data[0]);
  111. $has_data = is_array($printable_data[1]) && count($printable_data[1]) > 0;
  112. if (api_is_western_name_order()) {
  113. // Choosing the right person name order according to the current language.
  114. list($printable_data[0][0], $printable_data[0][1]) = array($printable_data[0][1], $printable_data[0][0]);
  115. if ($has_data) {
  116. foreach ($printable_data[1] as &$printable_data_row) {
  117. list($printable_data_row[0], $printable_data_row[1]) = array($printable_data_row[1], $printable_data_row[0]);
  118. }
  119. }
  120. }
  121. $table = new HTML_Table(array('class' => 'data_table'));
  122. $row = 0;
  123. $column = 0;
  124. foreach ($printable_data[0] as $printable_data_cell) {
  125. $table->setHeaderContents($row, $column, $printable_data_cell);
  126. $column++;
  127. }
  128. $row++;
  129. if ($has_data) {
  130. foreach ($printable_data[1] as &$printable_data_row) {
  131. $column = 0;
  132. foreach ($printable_data_row as &$printable_data_cell) {
  133. $table->setCellContents($row, $column, $printable_data_cell);
  134. $table->updateCellAttributes($row, $column, 'align="center"');
  135. $column++;
  136. }
  137. $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
  138. $row++;
  139. }
  140. } else {
  141. $column = 0;
  142. $table->setCellContents($row, $column, get_lang('NoResults'));
  143. $table->updateCellAttributes($row, $column, 'colspan="'.$columns.'" align="center" class="row_odd"');
  144. }
  145. $html .= $table->toHtml();
  146. $html .= '<pagefooter name="myFooter1" content-left="My Book Title" content-center="myFooter1" content-right="{PAGENO}" footer-style="font-family:sans-serif; font-size:8pt; font-weight:bold; color:#008800;" footer-style-left="" line="on" />';
  147. // Memory release
  148. unset($printable_data);
  149. unset($table);
  150. // Conversion of the created HTML report to a PDF report
  151. $html = api_utf8_encode($html);
  152. $creator_pdf = api_utf8_encode($creator);
  153. $title_pdf = api_utf8_encode($report_name);
  154. $subject_pdf = api_utf8_encode(get_lang('FlatView'));
  155. $keywods_pdf = api_utf8_encode($course_code);
  156. $pdf = new mPDF('UTF-8', 'A4', '', '', 32, 25, 27, 25, 16, 13, $export['orientation']);
  157. $pdf->directionality = api_get_text_direction();
  158. $pdf->useOnlyCoreFonts = true;
  159. $pdf->SetFooter('{PAGENO}');
  160. $pdf->SetAuthor($creator_pdf);
  161. $pdf->SetTitle($title_pdf);
  162. $pdf->SetSubject($subject_pdf);
  163. $pdf->SetKeywords($keywods_pdf);
  164. if (!empty($css)) {
  165. $pdf->WriteHTML($css, 1);
  166. $pdf->WriteHTML($html, 2);
  167. } else {
  168. $pdf->WriteHTML($html);
  169. }
  170. // Sending the created PDF report to the client
  171. $file_name = date('YmdHi_', $time);
  172. if (!empty($course_code)) {
  173. $file_name .= $course_code.'_';
  174. }
  175. $file_name .= get_lang('FlatView').'.pdf';
  176. $file_name = replace_dangerous_char($file_name);
  177. $pdf->Output($file_name, 'D');
  178. /*
  179. // This is the old pdf-exporting routine that uses ezpdf library.
  180. //
  181. $format = $export['orientation']; //format is 'portrait' or 'landscape'
  182. $pdf =& new Cezpdf('a4',$format); //format is 'portrait' or 'landscape'
  183. $clear_printable_data=array();
  184. $clear_send_printable_data=array();
  185. //var_dump(count($printable_data[1]));
  186. for ($i=0;$i<count($printable_data[1]);$i++) {
  187. for ($k=0;$k<count($printable_data[1][$i]);$k++) {
  188. $clear_printable_data[]=strip_tags($printable_data[1][$i][$k]);
  189. }
  190. $clear_send_printable_data[]=$clear_printable_data;
  191. $clear_printable_data=array();
  192. }
  193. export_pdf($pdf,$clear_send_printable_data,$printable_data[0],$format);
  194. */
  195. exit;
  196. } else {
  197. Display :: display_header(get_lang('ExportPDF'));
  198. }
  199. }
  200. if (isset ($_GET['print'])) {
  201. $printable_data = get_printable_data ($users,$alleval, $alllinks);
  202. echo print_table($printable_data[1],$printable_data[0], get_lang('FlatView'), $cat[0]->get_name());
  203. exit;
  204. }
  205. if (!empty($_POST['export_report']) && $_POST['export_report'] == 'export_report') {
  206. if (api_is_platform_admin() || api_is_course_admin() || api_is_course_coach()) {
  207. $user_id = null;
  208. if (empty($_SESSION['export_user_fields'])) {
  209. $_SESSION['export_user_fields'] = false;
  210. }
  211. if (!api_is_allowed_to_edit(false, false) and !api_is_course_tutor()) {
  212. $user_id = api_get_user_id();
  213. }
  214. require_once 'gradebook_result.class.php';
  215. $printable_data = get_printable_data($users, $alleval, $alllinks);
  216. switch($_POST['export_format']) {
  217. case 'xls':
  218. $export = new GradeBookResult();
  219. $export->exportCompleteReportXLS($printable_data);
  220. exit;
  221. break;
  222. case 'csv':
  223. default:
  224. $export = new GradeBookResult();
  225. $export->exportCompleteReportCSV($printable_data);
  226. exit;
  227. break;
  228. }
  229. } else {
  230. api_not_allowed(true);
  231. }
  232. }
  233. $addparams = array ('selectcat' => $cat[0]->get_id());
  234. if (isset($_GET['search'])) {
  235. $addparams['search'] = $keyword;
  236. }
  237. $offset = isset($_GET['offset']) ? $_GET['offset'] : '0';
  238. $flatviewtable = new FlatViewTable($cat[0], $users, $alleval, $alllinks, true, $offset, $addparams);
  239. if (isset($_GET['exportpdf'])) {
  240. echo '<div class="normal-message">';
  241. $export_pdf_form->display();
  242. echo '</div>';
  243. } else {
  244. Display :: display_header(get_lang('FlatView'));
  245. }
  246. if (isset($_GET['isStudentView']) && $_GET['isStudentView'] == 'false') {
  247. DisplayGradebook :: display_header_reduce_flatview($cat[0], $showeval, $showlink, $simple_search_form);
  248. $flatviewtable->display();
  249. } elseif (isset($_GET['selectcat']) && ($_SESSION['studentview'] == 'teacherview')) {
  250. DisplayGradebook :: display_header_reduce_flatview($cat[0], $showeval, $showlink, $simple_search_form);
  251. /*echo '<div id="contentLoading" class="contentLoading">';
  252. echo Display::display_icon('loader.gif');
  253. echo '</div>';*/
  254. // main graph
  255. //@todo load images with jquery
  256. echo '<div id="contentArea" style="text-align:center;" >';
  257. $image_file = $flatviewtable->display_graph();
  258. $my_info_path_img = array();
  259. $my_info_path_img = explode('/', $image_file);
  260. if (strlen($my_info_path_img[5]) == 32) {
  261. echo '<img src="'.$image_file.'">';
  262. }
  263. $flatviewtable->display();
  264. $flatviewtable->display_graph_by_resource();
  265. echo '</div>';
  266. }
  267. Display :: display_footer();
  268. function get_printable_data($users,$alleval, $alllinks) {
  269. $datagen = new FlatViewDataGenerator ($users, $alleval, $alllinks);
  270. $offset = isset($_GET['offset']) ? $_GET['offset'] : '0';
  271. $count = (($offset + 10) > $datagen->get_total_items_count()) ? ($datagen->get_total_items_count() - $offset) : 10;
  272. $header_names = $datagen->get_header_names($offset, $count);
  273. $data_array = $datagen->get_data(FlatViewDataGenerator :: FVDG_SORT_LASTNAME, 0, null, $offset, $count, true);
  274. $newarray = array();
  275. foreach ($data_array as $data) {
  276. $newarray[] = array_slice($data, 1);
  277. }
  278. return array ($header_names, $newarray);
  279. }