gradebook_functions.inc.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script
  5. * @package chamilo.gradebook
  6. */
  7. /**
  8. * These are functions used in gradebook
  9. *
  10. * @author Stijn Konings <konings.stijn@skynet.be>, Hogeschool Ghent
  11. * @author Julio Montoya <gugli100@gmail.com> adding security functions
  12. * @version april 2007
  13. */
  14. require_once api_get_path(SYS_CODE_PATH) . 'gradebook/lib/be.inc.php';
  15. require_once 'gradebook_functions_users.inc.php';
  16. /**
  17. * Adds a resource to the unique gradebook of a given course
  18. * @param int
  19. * @param string Course code
  20. * @param int Resource type (use constants defined in linkfactory.class.php)
  21. * @param int Resource ID in the corresponding tool
  22. * @param string Resource name to show in the gradebook
  23. * @param int Resource weight to set in the gradebook
  24. * @param int Resource max
  25. * @param string Resource description
  26. * @param int Visibility (0 hidden, 1 shown)
  27. * @param int Session ID (optional or 0 if not defined)
  28. * @param int
  29. * @return boolean True on success, false on failure
  30. */
  31. function add_resource_to_course_gradebook(
  32. $category_id,
  33. $course_code,
  34. $resource_type,
  35. $resource_id,
  36. $resource_name = '',
  37. $weight = 0,
  38. $max = 0,
  39. $resource_description = '',
  40. $visible = 0,
  41. $session_id = 0,
  42. $link_id = null
  43. ) {
  44. $link = LinkFactory :: create($resource_type);
  45. $link->set_user_id(api_get_user_id());
  46. $link->set_course_code($course_code);
  47. if (empty($category_id)) {
  48. return false;
  49. }
  50. $link->set_category_id($category_id);
  51. if ($link->needs_name_and_description()) {
  52. $link->set_name($resource_name);
  53. } else {
  54. $link->set_ref_id($resource_id);
  55. }
  56. $link->set_weight($weight);
  57. if ($link->needs_max()) {
  58. $link->set_max($max);
  59. }
  60. if ($link->needs_name_and_description()) {
  61. $link->set_description($resource_description);
  62. }
  63. $link->set_visible(empty($visible) ? 0 : 1);
  64. if (!empty($session_id)) {
  65. $link->set_session_id($session_id);
  66. }
  67. $link->add();
  68. return true;
  69. }
  70. /**
  71. * Update a resource weight
  72. * @param int Link/Resource ID
  73. * @param string
  74. * @param float
  75. * @return bool false on error, true on success
  76. */
  77. function update_resource_from_course_gradebook($link_id, $course_code, $weight)
  78. {
  79. $course_code = Database::escape_string($course_code);
  80. if (!empty($link_id)) {
  81. $link_id = intval($link_id);
  82. $sql = 'UPDATE ' . Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK) . '
  83. SET weight = ' . "'" . Database::escape_string((float) $weight) . "'" . '
  84. WHERE course_code = "' . $course_code . '" AND id = ' . $link_id;
  85. Database::query($sql);
  86. }
  87. return true;
  88. }
  89. /**
  90. * Remove a resource from the unique gradebook of a given course
  91. * @param int Link/Resource ID
  92. * @return bool false on error, true on success
  93. */
  94. function remove_resource_from_course_gradebook($link_id)
  95. {
  96. if (empty($link_id)) {
  97. return false;
  98. }
  99. // TODO find the corresponding category (the first one for this course, ordered by ID)
  100. $l = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  101. $sql = "DELETE FROM $l WHERE id = ".(int)$link_id;
  102. Database::query($sql);
  103. return true;
  104. }
  105. function block_students()
  106. {
  107. if (!api_is_allowed_to_edit()) {
  108. api_not_allowed();
  109. }
  110. }
  111. /**
  112. * Returns the course name from a given code
  113. * @param string $code
  114. */
  115. function get_course_name_from_code($code)
  116. {
  117. $tbl_main_categories = Database :: get_main_table(TABLE_MAIN_COURSE);
  118. $sql = 'SELECT title, code FROM ' . $tbl_main_categories . 'WHERE code = "' . Database::escape_string($code) . '"';
  119. $result = Database::query($sql);
  120. if ($col = Database::fetch_array($result)) {
  121. return $col['title'];
  122. }
  123. }
  124. /**
  125. * Builds an img tag for a gradebook item
  126. * @param string $type value returned by a gradebookitem's get_icon_name()
  127. */
  128. function build_type_icon_tag($kind, $attributes = array()) {
  129. return Display::return_icon(get_icon_file_name($kind), ' ', $attributes, ICON_SIZE_SMALL);
  130. }
  131. /**
  132. * Returns the icon filename for a gradebook item
  133. * @param string $type value returned by a gradebookitem's get_icon_name()
  134. */
  135. function get_icon_file_name($type) {
  136. switch ($type) {
  137. case 'cat':
  138. $icon = 'gradebook.png';
  139. break;
  140. case 'evalempty':
  141. $icon = 'empty_evaluation.png';
  142. break;
  143. case 'evalnotempty':
  144. $icon = 'no_empty_evaluation.png';
  145. break;
  146. case 'exercise':
  147. case LINK_EXERCISE:
  148. $icon = 'quiz.gif';
  149. break;
  150. case 'learnpath':
  151. case LINK_LEARNPATH:
  152. $icon = 'learnpath.png';
  153. break;
  154. case 'studentpublication':
  155. case LINK_STUDENTPUBLICATION:
  156. $icon = 'works.gif';
  157. break;
  158. case 'link':
  159. $icon = 'link.gif';
  160. break;
  161. case 'forum':
  162. case LINK_FORUM_THREAD:
  163. $icon = 'forum.gif';
  164. break;
  165. case 'attendance':
  166. case LINK_ATTENDANCE:
  167. $icon = 'attendance.gif';
  168. break;
  169. case 'survey':
  170. case LINK_SURVEY:
  171. $icon = 'survey.gif';
  172. break;
  173. case 'dropbox':
  174. case LINK_DROPBOX:
  175. $icon = 'dropbox.gif';
  176. break;
  177. default:
  178. $icon = 'link.gif';
  179. break;
  180. }
  181. return $icon;
  182. }
  183. /**
  184. * Builds the course or platform admin icons to edit a category
  185. * @param object $cat category object
  186. * @param int $selectcat id of selected category
  187. */
  188. function build_edit_icons_cat($cat, $selectcat)
  189. {
  190. $show_message = $cat->show_message_resource_delete($cat->get_course_code());
  191. $grade_model_id = $selectcat->get_grade_model_id();
  192. $selectcat = $selectcat->get_id();
  193. $modify_icons = null;
  194. if ($show_message === false) {
  195. $visibility_icon = ($cat->is_visible() == 0) ? 'invisible' : 'visible';
  196. $visibility_command = ($cat->is_visible() == 0) ? 'set_visible' : 'set_invisible';
  197. $modify_icons .= '<a class="view_children" data-cat-id="' . $cat->get_id() . '" href="javascript:void(0);">' . Display::return_icon('view_more_stats.gif', get_lang('Show'), '', ICON_SIZE_SMALL) . '</a>';
  198. if (api_is_allowed_to_edit(null, true)) {
  199. //Locking button
  200. if (api_get_setting('gradebook_locking_enabled') == 'true') {
  201. if ($cat->is_locked()) {
  202. if (api_is_platform_admin()) {
  203. $modify_icons .= '&nbsp;<a onclick="javascript:if (!confirm(\'' . addslashes(get_lang('ConfirmToUnlockElement')) . '\')) return false;" href="' . api_get_self() . '?' . api_get_cidreq() . '&category_id=' . $cat->get_id() . '&action=unlock">' .
  204. Display::return_icon('lock.png', get_lang('UnLockEvaluation'), '', ICON_SIZE_SMALL) . '</a>';
  205. } else {
  206. $modify_icons .= '&nbsp;<a href="#">' . Display::return_icon('lock_na.png', get_lang('GradebookLockedAlert'), '', ICON_SIZE_SMALL) . '</a>';
  207. }
  208. $modify_icons .= '&nbsp;<a href="gradebook_flatview.php?export_pdf=category&selectcat=' . $cat->get_id() . '" >' . Display::return_icon('pdf.png', get_lang('ExportToPDF'), '', ICON_SIZE_SMALL) . '</a>';
  209. } else {
  210. $modify_icons .= '&nbsp;<a onclick="javascript:if (!confirm(\'' . addslashes(get_lang('ConfirmToLockElement')) . '\')) return false;" href="' . api_get_self() . '?' . api_get_cidreq() . '&category_id=' . $cat->get_id() . '&action=lock">' .
  211. Display::return_icon('unlock.png', get_lang('LockEvaluation'), '', ICON_SIZE_SMALL) . '</a>';
  212. $modify_icons .= '&nbsp;<a href="#" >' . Display::return_icon('pdf_na.png', get_lang('ExportToPDF'), '', ICON_SIZE_SMALL) . '</a>';
  213. //$modify_icons .= '&nbsp;<a href="gradebook_flatview.php?export_pdf=category&selectcat=' . $cat->get_id() . '" >'.Display::return_icon('pdf.png', get_lang('ExportToPDF'),'',ICON_SIZE_SMALL).'</a>';
  214. }
  215. }
  216. if (empty($grade_model_id) || $grade_model_id == -1) {
  217. if ($cat->is_locked() && !api_is_platform_admin()) {
  218. $modify_icons .= Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
  219. } else {
  220. $modify_icons .= '<a href="gradebook_edit_cat.php?editcat=' . $cat->get_id() . '&amp;cidReq=' . $cat->get_course_code() . '">' . Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL) . '</a>';
  221. }
  222. }
  223. $modify_icons .= '<a href="gradebook_edit_all.php?&selectcat=' . $cat->get_id() . '">' . Display::return_icon('percentage.png', get_lang('EditAllWeights'), '', ICON_SIZE_SMALL) . '</a>';
  224. $modify_icons .= '<a href="gradebook_flatview.php?selectcat=' . $cat->get_id() . '">' . Display::return_icon('stats.png', get_lang('FlatView'), '', ICON_SIZE_SMALL) . '</a>';
  225. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?visiblecat=' . $cat->get_id() . '&amp;' . $visibility_command . '=&amp;selectcat=' . $selectcat . ' ">' . Display::return_icon($visibility_icon . '.png', get_lang('Visible'), '', ICON_SIZE_SMALL) . '</a>';
  226. //no move ability for root categories
  227. if ($cat->is_movable()) {
  228. /* $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?movecat=' . $cat->get_id() . '&amp;selectcat=' . $selectcat . ' &amp;cidReq='.$cat->get_course_code().'">
  229. <img src="../img/icons/22/move.png" border="0" title="' . get_lang('Move') . '" alt="" /></a>'; */
  230. } else {
  231. //$modify_icons .= '&nbsp;<img src="../img/deplacer_fichier_na.gif" border="0" title="' . get_lang('Move') . '" alt="" />';
  232. }
  233. if ($cat->is_locked() && !api_is_platform_admin()) {
  234. $modify_icons .= Display::return_icon('delete_na.png', get_lang('DeleteAll'), '', ICON_SIZE_SMALL);
  235. } else {
  236. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?deletecat=' . $cat->get_id() . '&amp;selectcat=' . $selectcat . '&amp;cidReq=' . $cat->get_course_code() . '" onclick="return confirmation();">' . Display::return_icon('delete.png', get_lang('DeleteAll'), '', ICON_SIZE_SMALL) . '</a>';
  237. }
  238. }
  239. return $modify_icons;
  240. }
  241. }
  242. /**
  243. * Builds the course or platform admin icons to edit an evaluation
  244. * @param object $eval evaluation object
  245. * @param int $selectcat id of selected category
  246. */
  247. function build_edit_icons_eval($eval, $selectcat) {
  248. $status = CourseManager::get_user_in_course_status(api_get_user_id(), api_get_course_id());
  249. $is_locked = $eval->is_locked();
  250. $eval->get_course_code();
  251. $cat = new Category();
  252. $message_eval = $cat->show_message_resource_delete($eval->get_course_code());
  253. if ($message_eval === false && api_is_allowed_to_edit(null, true)) {
  254. $visibility_icon = ($eval->is_visible() == 0) ? 'invisible' : 'visible';
  255. $visibility_command = ($eval->is_visible() == 0) ? 'set_visible' : 'set_invisible';
  256. if ($is_locked && !api_is_platform_admin()) {
  257. $modify_icons = Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
  258. } else {
  259. $modify_icons = '<a href="gradebook_edit_eval.php?editeval=' . $eval->get_id() . ' &amp;cidReq=' . $eval->get_course_code() . '">' . Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL) . '</a>';
  260. }
  261. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?visibleeval=' . $eval->get_id() . '&amp;' . $visibility_command . '=&amp;selectcat=' . $selectcat . ' ">' . Display::return_icon($visibility_icon . '.png', get_lang('Visible'), '', ICON_SIZE_SMALL) . '</a>';
  262. if (api_is_allowed_to_edit(null, true)) {
  263. $modify_icons .= '&nbsp;<a href="gradebook_showlog_eval.php?visiblelog=' . $eval->get_id() . '&amp;selectcat=' . $selectcat . ' &amp;cidReq=' . $eval->get_course_code() . '">' . Display::return_icon('history.png', get_lang('GradebookQualifyLog'), '', ICON_SIZE_SMALL) . '</a>';
  264. }
  265. /*
  266. if ($locked_status == 0){
  267. $modify_icons .= "&nbsp;<a href=\"javascript:if (confirm('".addslashes(get_lang('AreYouSureToLockedTheEvaluation'))."')) { location.href='".api_get_self().'?lockedeval=' . $eval->get_id() . '&amp;selectcat=' . $selectcat . ' &amp;cidReq='.$eval->get_course_code()."'; }\">".Display::return_icon('unlock.png',get_lang('LockEvaluation'), array(), ICON_SIZE_SMALL)."</a>";
  268. } else {
  269. if (api_is_platform_admin()){
  270. $modify_icons .= "&nbsp;<a href=\"javascript:if (confirm('".addslashes(get_lang('AreYouSureToUnLockedTheEvaluation'))."')) { location.href='".api_get_self().'?lockedeval=' . $eval->get_id() . '&amp;typelocked=&amp;selectcat=' . $selectcat . ' &amp;cidReq='.$eval->get_course_code()."';\">".Display::return_icon('lock.png',get_lang('UnLockEvaluation'), array(), ICON_SIZE_SMALL)."</a>";
  271. } else {
  272. $modify_icons .= '&nbsp;<img src="../img/locked_na.png" border="0" title="' . get_lang('TheEvaluationIsLocked') . '" alt="" />';
  273. }
  274. } */
  275. if ($is_locked && !api_is_platform_admin()) {
  276. $modify_icons .= '&nbsp;' . Display::return_icon('delete_na.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
  277. } else {
  278. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?deleteeval=' . $eval->get_id() . '&selectcat=' . $selectcat . ' &amp;cidReq=' . $eval->get_course_code() . '" onclick="return confirmation();">' . Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL) . '</a>';
  279. }
  280. return $modify_icons;
  281. }
  282. }
  283. /**
  284. * Builds the course or platform admin icons to edit a link
  285. * @param object $linkobject
  286. * @param int $selectcat id of selected category
  287. */
  288. function build_edit_icons_link($link, $selectcat) {
  289. $cat = new Category();
  290. $message_link = $cat->show_message_resource_delete($link->get_course_code());
  291. $is_locked = $link->is_locked();
  292. $modify_icons = null;
  293. if (!api_is_allowed_to_edit(null, true)) {
  294. return null;
  295. }
  296. if ($message_link === false) {
  297. $visibility_icon = ($link->is_visible() == 0) ? 'invisible' : 'visible';
  298. $visibility_command = ($link->is_visible() == 0) ? 'set_visible' : 'set_invisible';
  299. if ($is_locked && !api_is_platform_admin()) {
  300. $modify_icons = Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
  301. } else {
  302. $modify_icons = '<a href="gradebook_edit_link.php?editlink=' . $link->get_id() . '&amp;cidReq=' . $link->get_course_code() . '">' .
  303. Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL) . '</a>';
  304. }
  305. //$modify_icons .= '&nbsp;<a href="' . api_get_self() . '?movelink=' . $link->get_id() . '&selectcat=' . $selectcat . '"><img src="../img/deplacer_fichier.gif" border="0" title="' . get_lang('Move') . '" alt="" /></a>';
  306. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?visiblelink=' . $link->get_id() . '&amp;' . $visibility_command . '=&amp;selectcat=' . $selectcat . ' ">' . Display::return_icon($visibility_icon . '.png', get_lang('Visible'), '', ICON_SIZE_SMALL) . '</a>';
  307. $modify_icons .= '&nbsp;<a href="gradebook_showlog_link.php?visiblelink=' . $link->get_id() . '&amp;selectcat=' . $selectcat . '&amp;cidReq=' . $link->get_course_code() . '">' . Display::return_icon('history.png', get_lang('GradebookQualifyLog'), '', ICON_SIZE_SMALL) . '</a>';
  308. //If a work is added in a gradebook you can only delete the link in the work tool
  309. if ($is_locked && !api_is_platform_admin()) {
  310. $modify_icons .= '&nbsp;' . Display::return_icon('delete_na.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
  311. } else {
  312. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?deletelink=' . $link->get_id() . '&selectcat=' . $selectcat . ' &amp;cidReq=' . $link->get_course_code() . '" onclick="return confirmation();">' . Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL) . '</a>';
  313. }
  314. return $modify_icons;
  315. }
  316. }
  317. /**
  318. * Checks if a resource is in the unique gradebook of a given course
  319. * @param string Course code
  320. * @param int Resource type (use constants defined in linkfactory.class.php)
  321. * @param int Resource ID in the corresponding tool
  322. * @param int Session ID (optional - 0 if not defined)
  323. * @return int false on error or link ID
  324. */
  325. function is_resource_in_course_gradebook($course_code, $resource_type, $resource_id, $session_id = 0)
  326. {
  327. $l = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  328. $course_code = Database::escape_string($course_code);
  329. $sql = "SELECT * FROM $l l
  330. WHERE course_code = '$course_code' AND type = ".(int)$resource_type . " AND ref_id = " . (int)$resource_id;
  331. $res = Database::query($sql);
  332. if (Database::num_rows($res) < 1) {
  333. return false;
  334. }
  335. $row = Database::fetch_array($res, 'ASSOC');
  336. return $row;
  337. }
  338. /**
  339. * Remove a resource from the unique gradebook of a given course
  340. * @param int Link/Resource ID
  341. * @return bool false on error, true on success
  342. */
  343. function get_resource_from_course_gradebook($link_id)
  344. {
  345. if (empty($link_id)) {
  346. return false;
  347. }
  348. // TODO find the corresponding category (the first one for this course, ordered by ID)
  349. $l = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  350. $sql = "SELECT * FROM $l WHERE id = " . (int) $link_id;
  351. $res = Database::query($sql);
  352. $row = array();
  353. if (Database::num_rows($res) > 0) {
  354. $row = Database::fetch_array($res, 'ASSOC');
  355. }
  356. return $row;
  357. }
  358. /**
  359. * Return the database name
  360. * @param int
  361. * @return String
  362. */
  363. function get_database_name_by_link_id($id_link) {
  364. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  365. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  366. $sql = 'SELECT db_name FROM ' . $course_table . ' c INNER JOIN ' . $tbl_grade_links . ' l
  367. ON c.code=l.course_code WHERE l.id=' . intval($id_link) . ' OR l.category_id=' . intval($id_link);
  368. $res = Database::query($sql);
  369. $my_db_name = Database::fetch_array($res, 'ASSOC');
  370. return $my_db_name['db_name'];
  371. }
  372. /**
  373. * Return the course id
  374. * @param int
  375. * @return String
  376. */
  377. function get_course_id_by_link_id($id_link) {
  378. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  379. $tbl_grade_links = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  380. $sql = 'SELECT c.id FROM ' . $course_table . ' c INNER JOIN ' . $tbl_grade_links . ' l
  381. ON c.code = l.course_code WHERE l.id=' . intval($id_link) . ' OR l.category_id=' . intval($id_link);
  382. $res = Database::query($sql);
  383. $array = Database::fetch_array($res, 'ASSOC');
  384. return $array['id'];
  385. }
  386. function get_table_type_course($type) {
  387. global $table_evaluated;
  388. return Database::get_course_table($table_evaluated[$type][0]);
  389. }
  390. function get_printable_data($cat, $users, $alleval, $alllinks, $params) {
  391. $datagen = new FlatViewDataGenerator($users, $alleval, $alllinks, $params);
  392. $offset = isset($_GET['offset']) ? $_GET['offset'] : '0';
  393. $offset = intval($offset);
  394. // step 2: generate rows: students
  395. $datagen->category = $cat;
  396. $count = (($offset + 10) > $datagen->get_total_items_count()) ? ($datagen->get_total_items_count() - $offset) : LIMIT;
  397. $header_names = $datagen->get_header_names($offset, $count, true);
  398. $data_array = $datagen->get_data(FlatViewDataGenerator :: FVDG_SORT_LASTNAME, 0, null, $offset, $count, true, true);
  399. $newarray = array();
  400. foreach ($data_array as $data) {
  401. $newarray[] = array_slice($data, 1);
  402. }
  403. $return = array($header_names, $newarray);
  404. return $return;
  405. }
  406. /**
  407. * XML-parser: handle character data
  408. */
  409. function character_data($parser, $data) {
  410. global $current_value;
  411. $current_value = $data;
  412. }
  413. /**
  414. * XML-parser: handle end of element
  415. */
  416. function element_end($parser, $data) {
  417. global $user;
  418. global $users;
  419. global $current_value;
  420. switch ($data) {
  421. case 'Result' :
  422. $users[] = $user;
  423. break;
  424. default :
  425. $user[$data] = $current_value;
  426. break;
  427. }
  428. }
  429. /**
  430. * XML-parser: handle start of element
  431. */
  432. function element_start($parser, $data) {
  433. global $user;
  434. global $current_tag;
  435. switch ($data) {
  436. case 'Result' :
  437. $user = array();
  438. break;
  439. default :
  440. $current_tag = $data;
  441. }
  442. }
  443. function overwritescore($resid, $importscore, $eval_max) {
  444. $result = Result :: load($resid);
  445. if ($importscore > $eval_max) {
  446. header('Location: gradebook_view_result.php?selecteval=' . Security::remove_XSS($_GET['selecteval']) . '&overwritemax=');
  447. exit;
  448. }
  449. $result[0]->set_score($importscore);
  450. $result[0]->save();
  451. unset($result);
  452. }
  453. /**
  454. * Read the XML-file
  455. * @param string $file Path to the XML-file
  456. * @return array All userinformation read from the file
  457. */
  458. function parse_xml_data($file) {
  459. global $current_tag;
  460. global $current_value;
  461. global $user;
  462. global $users;
  463. $users = array();
  464. $parser = xml_parser_create();
  465. xml_set_element_handler($parser, 'element_start', 'element_end');
  466. xml_set_character_data_handler($parser, "character_data");
  467. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
  468. xml_parse($parser, file_get_contents($file));
  469. xml_parser_free($parser);
  470. return $users;
  471. }
  472. /**
  473. * register user info about certificate
  474. * @param int The category id
  475. * @param int The user id
  476. * @param float The score obtained for certified
  477. * @param Datetime The date when you obtained the certificate
  478. * @return void()
  479. */
  480. function register_user_info_about_certificate($cat_id, $user_id, $score_certificate, $date_certificate) {
  481. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  482. $sql_exist = 'SELECT COUNT(*) as count FROM ' . $table_certificate . ' gc
  483. WHERE gc.cat_id="' . intval($cat_id) . '" AND user_id="' . intval($user_id) . '" ';
  484. $rs_exist = Database::query($sql_exist);
  485. $row = Database::fetch_array($rs_exist);
  486. if ($row['count'] == 0) {
  487. $sql = 'INSERT INTO ' . $table_certificate . ' (cat_id,user_id,score_certificate,created_at)
  488. VALUES("' . intval($cat_id) . '","' . intval($user_id) . '","' . Database::escape_string($score_certificate) . '","' . Database::escape_string($date_certificate) . '")';
  489. $rs = Database::query($sql);
  490. }
  491. }
  492. /**
  493. * Get date of user certificate
  494. * @param int The category id
  495. * @param int The user id
  496. * @return Datetime The date when you obtained the certificate
  497. */
  498. function get_certificate_by_user_id($cat_id, $user_id) {
  499. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  500. $sql_get_date = 'SELECT * FROM ' . $table_certificate . ' WHERE cat_id="' . intval($cat_id) . '" AND user_id="' . intval($user_id) . '"';
  501. $rs_get_date = Database::query($sql_get_date);
  502. $row = Database::fetch_array($rs_get_date, 'ASSOC');
  503. return $row;
  504. }
  505. /**
  506. * Get list of users certificates
  507. * @param int The category id
  508. * @return array
  509. */
  510. function get_list_users_certificates($cat_id = null) {
  511. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  512. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  513. $sql = 'SELECT DISTINCT u.user_id, u.lastname, u.firstname, u.username
  514. FROM ' . $table_user . ' u INNER JOIN ' . $table_certificate . ' gc ON u.user_id=gc.user_id ';
  515. if (!is_null($cat_id) && $cat_id > 0) {
  516. $sql.=' WHERE cat_id=' . Database::escape_string($cat_id);
  517. }
  518. $sql.=' ORDER BY u.firstname';
  519. $rs = Database::query($sql);
  520. $list_users = array();
  521. while ($row = Database::fetch_array($rs)) {
  522. $list_users[] = $row;
  523. }
  524. return $list_users;
  525. }
  526. /**
  527. * Gets the certificate list by user id
  528. * @param int The user id
  529. * @param int The category id
  530. * @return array
  531. */
  532. function get_list_gradebook_certificates_by_user_id($user_id, $cat_id = null) {
  533. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  534. $sql = 'SELECT gc.score_certificate, gc.created_at, gc.path_certificate, gc.cat_id, gc.user_id, gc.id FROM ' . $table_certificate . ' gc
  535. WHERE gc.user_id="' . Database::escape_string($user_id) . '" ';
  536. if (!is_null($cat_id) && $cat_id > 0) {
  537. $sql.=' AND cat_id=' . Database::escape_string($cat_id);
  538. }
  539. $rs = Database::query($sql);
  540. $list_certificate = array();
  541. while ($row = Database::fetch_array($rs)) {
  542. $list_certificate[] = $row;
  543. }
  544. return $list_certificate;
  545. }
  546. function get_user_certificate_content($user_id, $course_code, $is_preview = false, $hide_print_button = false) {
  547. //generate document HTML
  548. $content_html = DocumentManager::replace_user_info_into_html($user_id, $course_code, $is_preview);
  549. $new_content = explode('</head>', $content_html['content']);
  550. $new_content_html = $new_content[1];
  551. $path_image = api_get_path(WEB_COURSE_PATH) . api_get_course_path($course_code) . '/document/images/gallery';
  552. $new_content_html = str_replace('../images/gallery', $path_image, $new_content_html);
  553. $path_image_in_default_course = api_get_path(WEB_CODE_PATH) . 'default_course_document';
  554. $new_content_html = str_replace('/main/default_course_document', $path_image_in_default_course, $new_content_html);
  555. $new_content_html = str_replace('/main/img/', api_get_path(WEB_IMG_PATH), $new_content_html);
  556. //add print header
  557. if ($hide_print_button == false) {
  558. $print = '<style media="print" type="text/css">#print_div {visibility:hidden;}</style>';
  559. $print .= '<a href="javascript:window.print();" style="float:right; padding:4px;" id="print_div"><img src="' . api_get_path(WEB_CODE_PATH) . 'img/printmgr.gif" alt="' . get_lang('Print') . '" /> ' . get_lang('Print') . '</a>';
  560. }
  561. //add header
  562. $new_content_html = $new_content[0] . $print . '</head>' . $new_content_html;
  563. return array('content' => $new_content_html, 'variables' => $content_html['variables']);
  564. }
  565. function create_default_course_gradebook($course_code = null, $gradebook_model_id = 0) {
  566. if (api_is_allowed_to_edit(true, true)) {
  567. if (!isset($course_code) || empty($course_code)) {
  568. $course_code = api_get_course_id();
  569. }
  570. $session_id = api_get_session_id();
  571. $t = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  572. $sql = "SELECT * FROM $t WHERE course_code = '" . Database::escape_string($course_code) . "' ";
  573. if (!empty($session_id)) {
  574. $sql .= " AND session_id = " . (int) $session_id;
  575. } else {
  576. $sql .= " AND (session_id IS NULL OR session_id = 0) ";
  577. }
  578. $sql .= " ORDER BY id";
  579. $res = Database::query($sql);
  580. if (Database::num_rows($res) < 1) {
  581. //there is no unique category for this course+session combination,
  582. $cat = new Category();
  583. if (!empty($session_id)) {
  584. $my_session_id = api_get_session_id();
  585. $s_name = api_get_session_name($my_session_id);
  586. $cat->set_name($course_code . ' - ' . get_lang('Session') . ' ' . $s_name);
  587. $cat->set_session_id($session_id);
  588. } else {
  589. $cat->set_name($course_code);
  590. }
  591. $cat->set_course_code($course_code);
  592. $cat->set_description(null);
  593. $cat->set_user_id(api_get_user_id());
  594. $cat->set_parent_id(0);
  595. $default_weight_setting = api_get_setting('gradebook_default_weight');
  596. $default_weight = isset($default_weight_setting) && !empty($default_weight_setting) ? $default_weight_setting : 100;
  597. $cat->set_weight($default_weight);
  598. $cat->set_grade_model_id($gradebook_model_id);
  599. $cat->set_certificate_min_score(75);
  600. $cat->set_visible(0);
  601. $cat->add();
  602. $category_id = $cat->get_id();
  603. unset($cat);
  604. } else {
  605. $row = Database::fetch_array($res);
  606. $category_id = $row['id'];
  607. }
  608. }
  609. return $category_id;
  610. }
  611. function load_gradebook_select_in_tool($form) {
  612. $course_code = api_get_course_id();
  613. $session_id = api_get_session_id();
  614. create_default_course_gradebook();
  615. // Cat list
  616. $all_categories = Category :: load(null, null, $course_code, null, null, $session_id, false);
  617. $select_gradebook = $form->addElement('select', 'category_id', get_lang('SelectGradebook'));
  618. if (!empty($all_categories)) {
  619. foreach ($all_categories as $my_cat) {
  620. if ($my_cat->get_course_code() == api_get_course_id()) {
  621. $grade_model_id = $my_cat->get_grade_model_id();
  622. if (empty($grade_model_id)) {
  623. if ($my_cat->get_parent_id() == 0) {
  624. //$default_weight = $my_cat->get_weight();
  625. $select_gradebook->addoption(get_lang('Default'), $my_cat->get_id());
  626. $cats_added[] = $my_cat->get_id();
  627. } else {
  628. $select_gradebook->addoption($my_cat->get_name(), $my_cat->get_id());
  629. $cats_added[] = $my_cat->get_id();
  630. }
  631. } else {
  632. $select_gradebook->addoption(get_lang('Select'), 0);
  633. }
  634. }
  635. }
  636. }
  637. }
  638. /**
  639. * PDF report creation
  640. */
  641. function export_pdf_flatview($cat, $users, $alleval, $alllinks, $params = array()) {
  642. global $flatviewtable;
  643. //Getting data
  644. $printable_data = get_printable_data($cat[0], $users, $alleval, $alllinks, $params);
  645. // HTML report creation first
  646. $course_code = trim($cat[0]->get_course_code());
  647. $displayscore = ScoreDisplay :: instance();
  648. $customdisplays = $displayscore->get_custom_score_display_settings();
  649. $total = array();
  650. if (is_array($customdisplays) && count(($customdisplays))) {
  651. foreach ($customdisplays as $custom) {
  652. $total[$custom['display']] = 0;
  653. }
  654. $user_results = $flatviewtable->datagen->get_data_to_graph2();
  655. foreach ($user_results as $user_result) {
  656. $total[$user_result[count($user_result) - 1][1]]++;
  657. }
  658. }
  659. $parent_id = $cat[0]->get_parent_id();
  660. if (isset($cat[0]) && isset($parent_id)) {
  661. if ($parent_id == 0) {
  662. $grade_model_id = $cat[0]->get_grade_model_id();
  663. } else {
  664. $parent_cat = Category::load($parent_id);
  665. $grade_model_id = $parent_cat[0]->get_grade_model_id();
  666. }
  667. }
  668. $use_grade_model = true;
  669. if (empty($grade_model_id) || $grade_model_id == -1) {
  670. $use_grade_model = false;
  671. }
  672. if ($use_grade_model) {
  673. if ($parent_id == 0) {
  674. $title = api_strtoupper(get_lang('Average')) . '<br />' . get_lang('Detailed');
  675. } else {
  676. $title = api_strtoupper(get_lang('Average')) . '<br />' . $cat[0]->get_description() . ' - (' . $cat[0]->get_name() . ')';
  677. }
  678. } else {
  679. if ($parent_id == 0) {
  680. $title = api_strtoupper(get_lang('Average')) . '<br />' . get_lang('Detailed');
  681. } else {
  682. $title = api_strtoupper(get_lang('Average'));
  683. }
  684. }
  685. $columns = count($printable_data[0]);
  686. $has_data = is_array($printable_data[1]) && count($printable_data[1]) > 0;
  687. $table = new HTML_Table(array('class' => 'data_table'));
  688. $row = 0;
  689. $column = 0;
  690. $table->setHeaderContents($row, $column, get_lang('NumberAbbreviation'));
  691. $column++;
  692. foreach ($printable_data[0] as $printable_data_cell) {
  693. $printable_data_cell = strip_tags($printable_data_cell);
  694. $table->setHeaderContents($row, $column, $printable_data_cell);
  695. $column++;
  696. }
  697. $row++;
  698. if ($has_data) {
  699. $counter = 1;
  700. //var_dump($printable_data);exit;
  701. foreach ($printable_data[1] as &$printable_data_row) {
  702. $column = 0;
  703. $table->setCellContents($row, $column, $counter);
  704. $table->updateCellAttributes($row, $column, 'align="center"');
  705. $column++;
  706. $counter++;
  707. foreach ($printable_data_row as $key => &$printable_data_cell) {
  708. $attributes = array();
  709. $attributes['align'] = 'center';
  710. $attributes['style'] = null;
  711. if ($key === 'name') {
  712. $attributes['align'] = 'left';
  713. }
  714. if ($key === 'total') {
  715. $attributes['style'] = 'font-weight:bold';
  716. }
  717. //var_dump($key, $printable_data_cell, $attributes);
  718. $table->setCellContents($row, $column, $printable_data_cell);
  719. $table->updateCellAttributes($row, $column, $attributes);
  720. $column++;
  721. }
  722. $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
  723. $row++;
  724. }
  725. } else {
  726. $column = 0;
  727. $table->setCellContents($row, $column, get_lang('NoResults'));
  728. $table->updateCellAttributes($row, $column, 'colspan="' . $columns . '" align="center" class="row_odd"');
  729. }
  730. $params = array(
  731. 'filename' => get_lang('FlatView') . '_' . api_get_utc_datetime(),
  732. 'pdf_title' => $title,
  733. 'course_code' => $course_code,
  734. 'add_signatures' => true
  735. );
  736. $page_format = $params['orientation'] == 'landscape' ? 'A4-L' : 'A4';
  737. $pdf = new PDF($page_format, $params['orientation'], $params);
  738. $pdf->html_to_pdf_with_template($table->toHtml());
  739. exit;
  740. }
  741. function score_badges($list_values) {
  742. $counter = 1;
  743. $badges = array();
  744. foreach ($list_values as $value) {
  745. $class = 'info';
  746. if ($counter == 1) {
  747. $class = 'success';
  748. }
  749. $counter++;
  750. $badges[] = Display::badge($value, $class);
  751. }
  752. return Display::badge_group($badges);
  753. }