gradebook_functions.inc.php 34 KB

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