gradebook_flatview.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script
  5. * @package chamilo.gradebook
  6. */
  7. /**
  8. * Init
  9. */
  10. $language_file = 'gradebook';
  11. require_once '../inc/global.inc.php';
  12. require_once 'lib/be.inc.php';
  13. require_once 'lib/fe/dataform.class.php';
  14. require_once 'lib/fe/userform.class.php';
  15. require_once 'lib/flatview_data_generator.class.php';
  16. require_once 'lib/fe/flatviewtable.class.php';
  17. require_once 'lib/fe/displaygradebook.php';
  18. require_once 'lib/fe/exportgradebook.php';
  19. require_once 'lib/scoredisplay.class.php';
  20. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  21. require_once api_get_path(LIBRARY_PATH).'pdf.lib.php';
  22. api_block_anonymous_users();
  23. block_students();
  24. if (isset ($_POST['submit']) && isset ($_POST['keyword'])) {
  25. header('Location: '.api_get_self().'?selectcat='.Security::remove_XSS($_GET['selectcat']).'&search='.Security::remove_XSS($_POST['keyword']));
  26. exit;
  27. }
  28. $interbreadcrumb[] = array ('url' => $_SESSION['gradebook_dest'].'?selectcat=1', 'name' => get_lang('ToolGradebook'));
  29. $showeval = isset($_POST['showeval']) ? '1' : '0';
  30. $showlink = isset($_POST['showlink']) ? '1' : '0';
  31. if (($showlink == '0') && ($showeval == '0')) {
  32. $showlink = '1';
  33. $showeval = '1';
  34. }
  35. $cat = Category::load($_REQUEST['selectcat']);
  36. if (isset($_GET['userid'])) {
  37. $userid = Security::remove_XSS($_GET['userid']);
  38. } else {
  39. $userid = '';
  40. }
  41. if ($showeval) {
  42. $alleval = $cat[0]->get_evaluations($userid, true);
  43. } else {
  44. $alleval = null;
  45. }
  46. if ($showlink) {
  47. $alllinks = $cat[0]->get_links($userid, true);
  48. } else {
  49. $alllinks = null;
  50. }
  51. if (isset($export_flatview_form) && (!$file_type == 'pdf')) {
  52. Display :: display_normal_message($export_flatview_form->toHtml(), false);
  53. }
  54. if (isset($_GET['selectcat'])) {
  55. $category_id = Security::remove_XSS($_GET['selectcat']);
  56. } else {
  57. $category_id = '';
  58. }
  59. $simple_search_form = new UserForm(UserForm :: TYPE_SIMPLE_SEARCH, null, 'simple_search_form', null, api_get_self() . '?selectcat=' . $category_id);
  60. $values = $simple_search_form->exportValues();
  61. $keyword = '';
  62. if (isset($_GET['search']) && !empty($_GET['search'])) {
  63. $keyword = Security::remove_XSS($_GET['search']);
  64. }
  65. if ($simple_search_form->validate() && (empty($keyword))) {
  66. $keyword = $values['keyword'];
  67. }
  68. if (!empty($keyword)) {
  69. $users = find_students($keyword);
  70. } else {
  71. if (isset($alleval) && isset($alllinks)) {
  72. $users = get_all_users($alleval, $alllinks);
  73. } else {
  74. $users = null;
  75. }
  76. }
  77. $offset = isset($_GET['offset']) ? $_GET['offset'] : '0';
  78. $flatviewtable = new FlatViewTable($cat[0], $users, $alleval, $alllinks, true, $offset, $addparams);
  79. $parameters=array('selectcat'=>intval($_GET['selectcat']));
  80. $flatviewtable->set_additional_parameters($parameters);
  81. if (isset($_GET['exportpdf'])) {
  82. $interbreadcrumb[] = array (
  83. 'url' => api_get_self().'?selectcat=' . Security::remove_XSS($_GET['selectcat']),
  84. 'name' => get_lang('FlatView')
  85. );
  86. $export_pdf_form = new DataForm(DataForm::TYPE_EXPORT_PDF, 'export_pdf_form', null, api_get_self().'?exportpdf=&offset='.intval($_GET['offset']).'&selectcat='.intval($_GET['selectcat']), '_blank', '');
  87. if ($export_pdf_form->validate()) {
  88. // Beginning of PDF report creation
  89. $printable_data = get_printable_data($cat[0], $users, $alleval, $alllinks);
  90. $export = $export_pdf_form->exportValues();
  91. // Reading report's CSS
  92. //$css_file = api_get_path(TO_SYS, WEB_CSS_PATH).api_get_setting('stylesheets').'/print.css';
  93. $css_file = api_get_path(SYS_CODE_PATH).'gradebook/print.css';
  94. $css = file_exists($css_file) ? @file_get_contents($css_file) : '';
  95. // HTML report creation first
  96. $time = time();
  97. $cat_name = trim($cat[0]->get_name());
  98. $course_code = trim($cat[0]->get_course_code());
  99. $organization = api_get_setting('Institution');
  100. $displayscore = ScoreDisplay :: instance();
  101. $customdisplays = $displayscore->get_custom_score_display_settings();
  102. if (is_array($customdisplays) && count(($customdisplays))) {
  103. $total = array();
  104. foreach($customdisplays as $custom) {
  105. $total[$custom['display']] = 0;
  106. }
  107. $user_results = $flatviewtable->datagen->get_data_to_graph2();
  108. foreach($user_results as $user_result) {
  109. $total[$user_result[count($user_result)-1][1]]++;
  110. }
  111. }
  112. $html = '';
  113. $img = api_get_path(SYS_CODE_PATH).'css/'.api_get_visual_theme().'/images/header-logo.png';
  114. if (file_exists($img)) {
  115. $img = api_get_path(WEB_CODE_PATH).'css/'.api_get_visual_theme().'/images/header-logo.png';
  116. $html .= "<img src='$img'>";
  117. } else {
  118. if (!empty($organization)) {
  119. $html .= '<h2 align="left">'.$organization.'</h2>';
  120. }
  121. }
  122. $html .= '<h2 align="center">'.get_lang('FlatView').'</h2>';
  123. $html .= '<table align="center" width="100%"><tr><td valign="top">';
  124. $html .= '<table align="left" width="33%">';
  125. $session_name = api_get_session_name(api_get_session_id());
  126. $teacher_list = CourseManager::get_teacher_list_from_course_code_to_string($course_code);
  127. if (!empty($session_name)) {
  128. $html .= Display::tag('tr', Display::tag('td', get_lang('Session')).Display::tag('td', Display::tag('strong', $session_name)));
  129. }
  130. $html .= Display::tag('tr', Display::tag('td', get_lang('Course')).Display::tag('td', Display::tag('strong', $course_code)));
  131. $html .= Display::tag('tr', Display::tag('td', get_lang('Date')).Display::tag('td', Display::tag('strong', api_convert_and_format_date(date('Y-m-d', time()), DATE_TIME_FORMAT_LONG))));
  132. $html .= Display::tag('tr', Display::tag('td', get_lang('Teacher')).Display::tag('td', Display::tag('strong', $teacher_list)));
  133. $html .= '</table></td>';
  134. $html .= '<td valign="top"><table align="left" width="33%">';
  135. if (!empty($total)) {
  136. foreach($total as $label => $count) {
  137. $total_custom_score = round($count/count($user_results), 2) *100;
  138. $html .= Display::tag('tr', Display::tag('td', $label).': '.Display::tag('td', Display::tag('strong', $total_custom_score.' %')));
  139. }
  140. }
  141. $html .= '</table></td>';
  142. $html .= '<td valign="top"><table align="left" width="33%">';
  143. $headers = $printable_data[0];
  144. unset($headers[0]);
  145. unset($headers[1]);
  146. unset($headers[count($headers)+1]);
  147. foreach ($headers as $head) {
  148. //$html .= Display::tag('tr', Display::tag('td', 'P1').Display::tag('td', Display::tag('strong', $head)));
  149. $html .= Display::tag('tr', Display::tag('td', Display::tag('strong', $head)));
  150. }
  151. $html .= '</table></td></table><br />';
  152. $columns = count($printable_data[0]);
  153. $has_data = is_array($printable_data[1]) && count($printable_data[1]) > 0;
  154. if (api_is_western_name_order()) {
  155. // Choosing the right person name order according to the current language.
  156. list($printable_data[0][0], $printable_data[0][1]) = array($printable_data[0][1], $printable_data[0][0]);
  157. if ($has_data) {
  158. foreach ($printable_data[1] as &$printable_data_row) {
  159. list($printable_data_row[0], $printable_data_row[1]) = array($printable_data_row[1], $printable_data_row[0]);
  160. }
  161. }
  162. }
  163. $table = new HTML_Table(array('class' => 'data_table'));
  164. $row = 0;
  165. $column = 0;
  166. foreach ($printable_data[0] as $printable_data_cell) {
  167. $table->setHeaderContents($row, $column, $printable_data_cell);
  168. $column++;
  169. }
  170. $row++;
  171. if ($has_data) {
  172. foreach ($printable_data[1] as &$printable_data_row) {
  173. $column = 0;
  174. foreach ($printable_data_row as &$printable_data_cell) {
  175. $table->setCellContents($row, $column, $printable_data_cell);
  176. $table->updateCellAttributes($row, $column, 'align="center"');
  177. $column++;
  178. }
  179. $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
  180. $row++;
  181. }
  182. } else {
  183. $column = 0;
  184. $table->setCellContents($row, $column, get_lang('NoResults'));
  185. $table->updateCellAttributes($row, $column, 'colspan="'.$columns.'" align="center" class="row_odd"');
  186. }
  187. $html .= $table->toHtml();
  188. //echo $html;exit;
  189. // Memory release
  190. unset($printable_data);
  191. unset($table);
  192. // Conversion of the created HTML report to a PDF report
  193. $html = api_utf8_encode($html);
  194. //@todo this is really a must?
  195. $creator_pdf = api_utf8_encode($creator);
  196. $title_pdf = api_utf8_encode($report_name);
  197. $subject_pdf = api_utf8_encode(get_lang('FlatView'));
  198. $keywods_pdf = api_utf8_encode($course_code);
  199. $page_format = $export['orientation'] == 'landscape' ? 'A4-L' : 'A4';
  200. $pdf = new PDF($page_format, $export['orientation']);
  201. // Sending the created PDF report to the client
  202. $file_name = date('YmdHi_', $time);
  203. if (!empty($course_code)) {
  204. $file_name .= $course_code.'_';
  205. }
  206. $file_name .= get_lang('FlatView').'.pdf';
  207. $pdf->content_to_pdf($html, $css, $file_name, api_get_course_id());
  208. exit;
  209. } else {
  210. Display :: display_header(get_lang('ExportPDF'));
  211. }
  212. }
  213. if (isset ($_GET['print'])) {
  214. $printable_data = get_printable_data ($cat[0], $users,$alleval, $alllinks);
  215. echo print_table($printable_data[1],$printable_data[0], get_lang('FlatView'), $cat[0]->get_name());
  216. exit;
  217. }
  218. if (!empty($_GET['export_report']) && $_GET['export_report'] == 'export_report') {
  219. if (api_is_platform_admin() || api_is_course_admin() || api_is_course_coach()) {
  220. $user_id = null;
  221. if (empty($_SESSION['export_user_fields'])) {
  222. $_SESSION['export_user_fields'] = false;
  223. }
  224. if (!api_is_allowed_to_edit(false, false) and !api_is_course_tutor()) {
  225. $user_id = api_get_user_id();
  226. }
  227. require_once 'gradebook_result.class.php';
  228. $printable_data = get_printable_data($cat[0], $users, $alleval, $alllinks);
  229. switch($_GET['export_format']) {
  230. case 'xls':
  231. $export = new GradeBookResult();
  232. $export->exportCompleteReportXLS($printable_data);
  233. break;
  234. case 'doc':
  235. $export = new GradeBookResult();
  236. $export->exportCompleteReportDOC($printable_data);
  237. exit;
  238. break;
  239. case 'csv':
  240. default:
  241. $export = new GradeBookResult();
  242. $export->exportCompleteReportCSV($printable_data);
  243. exit;
  244. break;
  245. }
  246. } else {
  247. api_not_allowed(true);
  248. }
  249. }
  250. $addparams = array ('selectcat' => $cat[0]->get_id());
  251. if (isset($_GET['search'])) {
  252. $addparams['search'] = $keyword;
  253. }
  254. $this_section = SECTION_COURSES;
  255. if (isset($_GET['exportpdf'])) {
  256. echo '<div class="normal-message">';
  257. $export_pdf_form->display();
  258. echo '</div>';
  259. } else {
  260. Display :: display_header(get_lang('FlatView'));
  261. }
  262. if (isset($_GET['isStudentView']) && $_GET['isStudentView'] == 'false') {
  263. DisplayGradebook :: display_header_reduce_flatview($cat[0], $showeval, $showlink, $simple_search_form);
  264. $flatviewtable->display();
  265. } elseif (isset($_GET['selectcat']) && ($_SESSION['studentview'] == 'teacherview')) {
  266. DisplayGradebook :: display_header_reduce_flatview($cat[0], $showeval, $showlink, $simple_search_form);
  267. // main graph
  268. $flatviewtable->display();
  269. // @todo this needs a fix
  270. //$image_file = $flatviewtable->display_graph();
  271. //@todo load images with jquery
  272. echo '<div id="contentArea" style="text-align: center;" >';
  273. if (!empty($image_file)) {
  274. echo '<img src="'.$image_file.'">';
  275. }
  276. $flatviewtable->display_graph_by_resource();
  277. echo '</div>';
  278. }
  279. Display :: display_footer();