gradebook_flatview.php 11 KB

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