gradebook_functions.inc.php 33 KB

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