GradebookUtils.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class GradebookUtils
  5. */
  6. class GradebookUtils
  7. {
  8. /**
  9. * Adds a resource to the unique gradebook of a given course
  10. * @param int
  11. * @param string Course code
  12. * @param int Resource type (use constants defined in linkfactory.class.php)
  13. * @param int Resource ID in the corresponding tool
  14. * @param string Resource name to show in the gradebook
  15. * @param int Resource weight to set in the gradebook
  16. * @param int Resource max
  17. * @param string Resource description
  18. * @param int Visibility (0 hidden, 1 shown)
  19. * @param int Session ID (optional or 0 if not defined)
  20. * @param int
  21. * @param integer $resource_type
  22. * @return boolean True on success, false on failure
  23. */
  24. public static function add_resource_to_course_gradebook(
  25. $category_id,
  26. $course_code,
  27. $resource_type,
  28. $resource_id,
  29. $resource_name = '',
  30. $weight = 0,
  31. $max = 0,
  32. $resource_description = '',
  33. $visible = 0,
  34. $session_id = 0,
  35. $link_id = null
  36. ) {
  37. $link = LinkFactory :: create($resource_type);
  38. $link->set_user_id(api_get_user_id());
  39. $link->set_course_code($course_code);
  40. if (empty($category_id)) {
  41. return false;
  42. }
  43. $link->set_category_id($category_id);
  44. if ($link->needs_name_and_description()) {
  45. $link->set_name($resource_name);
  46. } else {
  47. $link->set_ref_id($resource_id);
  48. }
  49. $link->set_weight($weight);
  50. if ($link->needs_max()) {
  51. $link->set_max($max);
  52. }
  53. if ($link->needs_name_and_description()) {
  54. $link->set_description($resource_description);
  55. }
  56. $link->set_visible(empty($visible) ? 0 : 1);
  57. if (!empty($session_id)) {
  58. $link->set_session_id($session_id);
  59. }
  60. $link->add();
  61. return true;
  62. }
  63. /**
  64. * Update a resource weight
  65. * @param int Link/Resource ID
  66. * @param string
  67. * @param float
  68. * @return bool false on error, true on success
  69. */
  70. public static function update_resource_from_course_gradebook($link_id, $course_code, $weight)
  71. {
  72. $course_code = Database::escape_string($course_code);
  73. if (!empty($link_id)) {
  74. $link_id = intval($link_id);
  75. $sql = 'UPDATE ' . Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK) . '
  76. SET weight = ' . "'" . Database::escape_string((float) $weight) . "'" . '
  77. WHERE course_code = "' . $course_code . '" AND id = ' . $link_id;
  78. Database::query($sql);
  79. }
  80. return true;
  81. }
  82. /**
  83. * Remove a resource from the unique gradebook of a given course
  84. * @param int Link/Resource ID
  85. * @return bool false on error, true on success
  86. */
  87. public static function remove_resource_from_course_gradebook($link_id)
  88. {
  89. if (empty($link_id)) {
  90. return false;
  91. }
  92. // TODO find the corresponding category (the first one for this course, ordered by ID)
  93. $l = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  94. $sql = "DELETE FROM $l WHERE id = ".(int)$link_id;
  95. Database::query($sql);
  96. return true;
  97. }
  98. /**
  99. * Block students
  100. */
  101. public static function block_students()
  102. {
  103. if (!api_is_allowed_to_edit()) {
  104. api_not_allowed();
  105. }
  106. }
  107. /**
  108. * Builds an img tag for a gradebook item
  109. */
  110. public static function build_type_icon_tag($kind, $attributes = array())
  111. {
  112. return Display::return_icon(self::get_icon_file_name($kind), ' ', $attributes, ICON_SIZE_SMALL);
  113. }
  114. /**
  115. * Returns the icon filename for a gradebook item
  116. * @param string $type value returned by a gradebookitem's get_icon_name()
  117. */
  118. public static function get_icon_file_name($type)
  119. {
  120. switch ($type) {
  121. case 'cat':
  122. $icon = 'gradebook.png';
  123. break;
  124. case 'evalempty':
  125. $icon = 'empty_evaluation.png';
  126. break;
  127. case 'evalnotempty':
  128. $icon = 'no_empty_evaluation.png';
  129. break;
  130. case 'exercise':
  131. case LINK_EXERCISE:
  132. $icon = 'quiz.png';
  133. break;
  134. case 'learnpath':
  135. case LINK_LEARNPATH:
  136. $icon = 'learnpath.png';
  137. break;
  138. case 'studentpublication':
  139. case LINK_STUDENTPUBLICATION:
  140. $icon = 'works.gif';
  141. break;
  142. case 'link':
  143. $icon = 'link.gif';
  144. break;
  145. case 'forum':
  146. case LINK_FORUM_THREAD:
  147. $icon = 'forum.gif';
  148. break;
  149. case 'attendance':
  150. case LINK_ATTENDANCE:
  151. $icon = 'attendance.gif';
  152. break;
  153. case 'survey':
  154. case LINK_SURVEY:
  155. $icon = 'survey.gif';
  156. break;
  157. case 'dropbox':
  158. case LINK_DROPBOX:
  159. $icon = 'dropbox.gif';
  160. break;
  161. default:
  162. $icon = 'link.gif';
  163. break;
  164. }
  165. return $icon;
  166. }
  167. /**
  168. * Builds the course or platform admin icons to edit a category
  169. * @param Category $cat category
  170. * @param Category $selectcat id of selected category
  171. */
  172. public static function build_edit_icons_cat($cat, $selectcat)
  173. {
  174. $show_message = $cat->show_message_resource_delete($cat->get_course_code());
  175. $grade_model_id = $selectcat->get_grade_model_id();
  176. $selectcat = $selectcat->get_id();
  177. $modify_icons = null;
  178. if ($show_message === false) {
  179. $visibility_icon = ($cat->is_visible() == 0) ? 'invisible' : 'visible';
  180. $visibility_command = ($cat->is_visible() == 0) ? 'set_visible' : 'set_invisible';
  181. $modify_icons .= '<a class="view_children" data-cat-id="' . $cat->get_id() . '" href="javascript:void(0);">' .
  182. Display::return_icon('view_more_stats.gif', get_lang('Show'), '', ICON_SIZE_SMALL) . '</a>';
  183. if (!api_is_allowed_to_edit(null, true)) {
  184. $modify_icons .= Display::url(
  185. Display::return_icon(
  186. 'stats.png',
  187. get_lang('FlatView'),
  188. '',
  189. ICON_SIZE_SMALL
  190. ),
  191. 'personal_stats.php?' . http_build_query([
  192. 'selectcat' => $cat->get_id()
  193. ]) . '&' . api_get_cidreq(),
  194. [
  195. 'class' => 'ajax',
  196. 'data-title' => get_lang('FlatView')
  197. ]
  198. );
  199. }
  200. $courseParams = api_get_cidreq_params($cat->get_course_code(), $cat->get_session_id());
  201. if (api_is_allowed_to_edit(null, true)) {
  202. // Locking button
  203. if (api_get_setting('gradebook_locking_enabled') == 'true') {
  204. if ($cat->is_locked()) {
  205. if (api_is_platform_admin()) {
  206. $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">' .
  207. Display::return_icon('lock.png', get_lang('UnLockEvaluation'), '', ICON_SIZE_SMALL) . '</a>';
  208. } else {
  209. $modify_icons .= '&nbsp;<a href="#">' . Display::return_icon('lock_na.png', get_lang('GradebookLockedAlert'), '', ICON_SIZE_SMALL) . '</a>';
  210. }
  211. $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>';
  212. } else {
  213. $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">' .
  214. Display::return_icon('unlock.png', get_lang('LockEvaluation'), '', ICON_SIZE_SMALL) . '</a>';
  215. $modify_icons .= '&nbsp;<a href="#" >' . Display::return_icon('pdf_na.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() . '&'.$courseParams.'">' .
  223. Display::return_icon(
  224. 'edit.png',
  225. get_lang('Modify'),
  226. '',
  227. ICON_SIZE_SMALL
  228. ) . '</a>';
  229. }
  230. }
  231. $modify_icons .= '<a href="gradebook_edit_all.php?selectcat=' .$cat->get_id() . '&' . $courseParams.'">' .
  232. Display::return_icon(
  233. 'percentage.png',
  234. get_lang('EditAllWeights'),
  235. '',
  236. ICON_SIZE_SMALL
  237. ) . '</a>';
  238. $modify_icons .= '<a href="gradebook_flatview.php?selectcat=' .$cat->get_id() . '&' . $courseParams. '">' .
  239. Display::return_icon(
  240. 'stats.png',
  241. get_lang('FlatView'),
  242. '',
  243. ICON_SIZE_SMALL
  244. ) . '</a>';
  245. $modify_icons .= '&nbsp;<a href="' . api_get_self() .'?visiblecat=' . $cat->get_id() . '&' .$visibility_command . '=&selectcat=' . $selectcat .'&' . $courseParams. '">' .
  246. Display::return_icon(
  247. $visibility_icon . '.png',
  248. get_lang('Visible'),
  249. '',
  250. ICON_SIZE_SMALL
  251. ) . '</a>';
  252. if ($cat->is_locked() && !api_is_platform_admin()) {
  253. $modify_icons .= Display::return_icon('delete_na.png', get_lang('DeleteAll'), '', ICON_SIZE_SMALL);
  254. } else {
  255. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?deletecat=' . $cat->get_id() . '&selectcat=' . $selectcat . '&' . $courseParams. '" onclick="return confirmation();">' .
  256. Display::return_icon('delete.png', get_lang('DeleteAll'), '', ICON_SIZE_SMALL) . '</a>';
  257. }
  258. }
  259. return $modify_icons;
  260. }
  261. }
  262. /**
  263. * Builds the course or platform admin icons to edit an evaluation
  264. * @param Evaluation $eval evaluation object
  265. * @param int $selectcat id of selected category
  266. */
  267. public static function build_edit_icons_eval($eval, $selectcat)
  268. {
  269. $is_locked = $eval->is_locked();
  270. $eval->get_course_code();
  271. $cat = new Category();
  272. $message_eval = $cat->show_message_resource_delete($eval->get_course_code());
  273. $courseParams = api_get_cidreq_params($eval->get_course_code(), $eval->getSessionId());
  274. if ($message_eval === false && api_is_allowed_to_edit(null, true)) {
  275. $visibility_icon = ($eval->is_visible() == 0) ? 'invisible' : 'visible';
  276. $visibility_command = ($eval->is_visible() == 0) ? 'set_visible' : 'set_invisible';
  277. if ($is_locked && !api_is_platform_admin()) {
  278. $modify_icons = Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
  279. } else {
  280. $modify_icons = '<a href="gradebook_edit_eval.php?editeval=' . $eval->get_id() . '&' . $courseParams. '">' .
  281. Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL) . '</a>';
  282. }
  283. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?visibleeval=' . $eval->get_id() . '&' . $visibility_command . '=&selectcat=' . $selectcat . '&'.$courseParams. ' ">' .
  284. Display::return_icon($visibility_icon . '.png', get_lang('Visible'), '', ICON_SIZE_SMALL) . '</a>';
  285. if (api_is_allowed_to_edit(null, true)) {
  286. $modify_icons .= '&nbsp;<a href="gradebook_showlog_eval.php?visiblelog=' . $eval->get_id() . '&selectcat=' . $selectcat . ' &' . $courseParams. '">' .
  287. Display::return_icon('history.png', get_lang('GradebookQualifyLog'), '', ICON_SIZE_SMALL) . '</a>';
  288. }
  289. if ($is_locked && !api_is_platform_admin()) {
  290. $modify_icons .= '&nbsp;' . Display::return_icon('delete_na.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
  291. } else {
  292. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?deleteeval=' . $eval->get_id() . '&selectcat=' . $selectcat . ' &' . $courseParams. '" onclick="return confirmation();">' .
  293. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL) . '</a>';
  294. }
  295. return $modify_icons;
  296. }
  297. }
  298. /**
  299. * Builds the course or platform admin icons to edit a link
  300. * @param AbstractLink $link
  301. * @param int $selectcat id of selected category
  302. */
  303. public static function build_edit_icons_link($link, $selectcat)
  304. {
  305. $cat = new Category();
  306. $message_link = $cat->show_message_resource_delete($link->get_course_code());
  307. $is_locked = $link->is_locked();
  308. $modify_icons = null;
  309. if (!api_is_allowed_to_edit(null, true)) {
  310. return null;
  311. }
  312. $courseParams = api_get_cidreq_params($link->get_course_code(), $link->get_session_id());
  313. if ($message_link === false) {
  314. $visibility_icon = ($link->is_visible() == 0) ? 'invisible' : 'visible';
  315. $visibility_command = ($link->is_visible() == 0) ? 'set_visible' : 'set_invisible';
  316. if ($is_locked && !api_is_platform_admin()) {
  317. $modify_icons = Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
  318. } else {
  319. $modify_icons = '<a href="gradebook_edit_link.php?editlink=' . $link->get_id() . '&' . $courseParams.'">' .
  320. Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL) . '</a>';
  321. }
  322. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?visiblelink=' . $link->get_id() . '&' . $visibility_command . '=&selectcat=' . $selectcat . '&'.$courseParams. ' ">' .
  323. Display::return_icon($visibility_icon . '.png', get_lang('Visible'), '', ICON_SIZE_SMALL) . '</a>';
  324. $modify_icons .= '&nbsp;<a href="gradebook_showlog_link.php?visiblelink=' . $link->get_id() . '&selectcat=' . $selectcat . '&' . $courseParams. '">' .
  325. Display::return_icon('history.png', get_lang('GradebookQualifyLog'), '', ICON_SIZE_SMALL) . '</a>';
  326. //If a work is added in a gradebook you can only delete the link in the work tool
  327. if ($is_locked && !api_is_platform_admin()) {
  328. $modify_icons .= '&nbsp;' . Display::return_icon('delete_na.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
  329. } else {
  330. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?deletelink=' . $link->get_id() . '&selectcat=' . $selectcat . ' &' . $courseParams. '" onclick="return confirmation();">' .
  331. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL) . '</a>';
  332. }
  333. return $modify_icons;
  334. }
  335. }
  336. /**
  337. * Checks if a resource is in the unique gradebook of a given course
  338. * @param string $course_code Course code
  339. * @param int $resource_type Resource type (use constants defined in linkfactory.class.php)
  340. * @param int $resource_id Resource ID in the corresponding tool
  341. * @param int $session_id Session ID (optional - 0 if not defined)
  342. *
  343. * @return array false on error or array of resource
  344. */
  345. public static function isResourceInCourseGradebook($course_code, $resource_type, $resource_id, $session_id = 0)
  346. {
  347. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  348. $course_code = Database::escape_string($course_code);
  349. $sql = "SELECT * FROM $table l
  350. WHERE
  351. course_code = '$course_code' AND
  352. type = ".(int)$resource_type . " AND
  353. ref_id = " . (int)$resource_id;
  354. $res = Database::query($sql);
  355. if (Database::num_rows($res) < 1) {
  356. return false;
  357. }
  358. $row = Database::fetch_array($res, 'ASSOC');
  359. return $row;
  360. }
  361. /**
  362. * Remove a resource from the unique gradebook of a given course
  363. * @param int Link/Resource ID
  364. * @return bool false on error, true on success
  365. */
  366. public static function get_resource_from_course_gradebook($link_id)
  367. {
  368. if (empty($link_id)) {
  369. return false;
  370. }
  371. // TODO find the corresponding category (the first one for this course, ordered by ID)
  372. $l = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  373. $sql = "SELECT * FROM $l WHERE id = " . (int) $link_id;
  374. $res = Database::query($sql);
  375. $row = array();
  376. if (Database::num_rows($res) > 0) {
  377. $row = Database::fetch_array($res, 'ASSOC');
  378. }
  379. return $row;
  380. }
  381. /**
  382. * Return the course id
  383. * @param int
  384. * @return String
  385. */
  386. public static function get_course_id_by_link_id($id_link)
  387. {
  388. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  389. $tbl_grade_links = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  390. $sql = 'SELECT c.id FROM ' . $course_table . ' c
  391. INNER JOIN ' . $tbl_grade_links . ' l
  392. ON c.code = l.course_code
  393. WHERE l.id=' . intval($id_link) . ' OR l.category_id=' . intval($id_link);
  394. $res = Database::query($sql);
  395. $array = Database::fetch_array($res, 'ASSOC');
  396. return $array['id'];
  397. }
  398. /**
  399. * @param $type
  400. * @return string
  401. */
  402. public static function get_table_type_course($type)
  403. {
  404. global $table_evaluated;
  405. return Database::get_course_table($table_evaluated[$type][0]);
  406. }
  407. /**
  408. * @param Category $cat
  409. * @param $users
  410. * @param $alleval
  411. * @param $alllinks
  412. * @param $params
  413. * @param null $mainCourseCategory
  414. * @return array
  415. */
  416. public static function get_printable_data($cat, $users, $alleval, $alllinks, $params, $mainCourseCategory = null)
  417. {
  418. $datagen = new FlatViewDataGenerator(
  419. $users,
  420. $alleval,
  421. $alllinks,
  422. $params,
  423. $mainCourseCategory
  424. );
  425. $offset = isset($_GET['offset']) ? $_GET['offset'] : '0';
  426. $offset = intval($offset);
  427. // step 2: generate rows: students
  428. $datagen->category = $cat;
  429. $count = (($offset + 10) > $datagen->get_total_items_count()) ? ($datagen->get_total_items_count() - $offset) : GRADEBOOK_ITEM_LIMIT;
  430. $header_names = $datagen->get_header_names($offset, $count, true);
  431. $data_array = $datagen->get_data(
  432. FlatViewDataGenerator :: FVDG_SORT_LASTNAME,
  433. 0,
  434. null,
  435. $offset,
  436. $count,
  437. true,
  438. true
  439. );
  440. $result = array();
  441. foreach ($data_array as $data) {
  442. $result[] = array_slice($data, 1);
  443. }
  444. $return = array($header_names, $result);
  445. return $return;
  446. }
  447. /**
  448. * XML-parser: handle character data
  449. */
  450. public static function character_data($parser, $data)
  451. {
  452. global $current_value;
  453. $current_value = $data;
  454. }
  455. /**
  456. * XML-parser: handle end of element
  457. */
  458. public static function element_end($parser, $data)
  459. {
  460. global $user;
  461. global $users;
  462. global $current_value;
  463. switch ($data) {
  464. case 'Result':
  465. $users[] = $user;
  466. break;
  467. default:
  468. $user[$data] = $current_value;
  469. break;
  470. }
  471. }
  472. /**
  473. * XML-parser: handle start of element
  474. */
  475. public static function element_start($parser, $data)
  476. {
  477. global $user;
  478. global $current_tag;
  479. switch ($data) {
  480. case 'Result':
  481. $user = array();
  482. break;
  483. default:
  484. $current_tag = $data;
  485. }
  486. }
  487. public static function overwritescore($resid, $importscore, $eval_max)
  488. {
  489. $result = Result :: load($resid);
  490. if ($importscore > $eval_max) {
  491. header('Location: gradebook_view_result.php?selecteval=' . Security::remove_XSS($_GET['selecteval']) . '&overwritemax=');
  492. exit;
  493. }
  494. $result[0]->set_score($importscore);
  495. $result[0]->save();
  496. unset($result);
  497. }
  498. /**
  499. * Read the XML-file
  500. * @param string $file Path to the XML-file
  501. * @return array All user information read from the file
  502. */
  503. public static function parse_xml_data($file)
  504. {
  505. global $current_tag;
  506. global $current_value;
  507. global $user;
  508. global $users;
  509. $users = array();
  510. $parser = xml_parser_create();
  511. xml_set_element_handler($parser, 'element_start', 'element_end');
  512. xml_set_character_data_handler($parser, "character_data");
  513. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
  514. xml_parse($parser, file_get_contents($file));
  515. xml_parser_free($parser);
  516. return $users;
  517. }
  518. /**
  519. * register user info about certificate
  520. * @param int $cat_id The category id
  521. * @param int $user_id The user id
  522. * @param float $score_certificate The score obtained for certified
  523. * @param string $date_certificate The date when you obtained the certificate
  524. *
  525. * @return void
  526. */
  527. public static function registerUserInfoAboutCertificate(
  528. $cat_id,
  529. $user_id,
  530. $score_certificate,
  531. $date_certificate
  532. ) {
  533. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  534. $sql = 'SELECT COUNT(id) as count
  535. FROM ' . $table . ' gc
  536. WHERE gc.cat_id="' . intval($cat_id) . '" AND user_id="' . intval($user_id) . '" ';
  537. $rs_exist = Database::query($sql);
  538. $row = Database::fetch_array($rs_exist);
  539. if ($row['count'] == 0) {
  540. $params = [
  541. 'cat_id' => $cat_id,
  542. 'user_id' => $user_id,
  543. 'score_certificate' => $score_certificate,
  544. 'created_at' => $date_certificate
  545. ];
  546. Database::insert($table, $params);
  547. }
  548. }
  549. /**
  550. * Get date of user certificate
  551. * @param int $cat_id The category id
  552. * @param int $user_id The user id
  553. * @return Datetime The date when you obtained the certificate
  554. */
  555. public static function get_certificate_by_user_id($cat_id, $user_id)
  556. {
  557. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  558. $sql = 'SELECT * FROM ' . $table_certificate . '
  559. WHERE cat_id="' . intval($cat_id) . '" AND user_id="' . intval($user_id) . '"';
  560. $result = Database::query($sql);
  561. $row = Database::fetch_array($result, 'ASSOC');
  562. return $row;
  563. }
  564. /**
  565. * Get list of users certificates
  566. * @param int $cat_id The category id
  567. * @param array $userList Only users in this list
  568. * @return array
  569. */
  570. public static function get_list_users_certificates($cat_id = null, $userList = array())
  571. {
  572. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  573. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  574. $sql = 'SELECT DISTINCT u.user_id, u.lastname, u.firstname, u.username
  575. FROM ' . $table_user . ' u
  576. INNER JOIN ' . $table_certificate . ' gc
  577. ON u.user_id=gc.user_id ';
  578. if (!is_null($cat_id) && $cat_id > 0) {
  579. $sql.=' WHERE cat_id=' . intval($cat_id);
  580. }
  581. if (!empty($userList)) {
  582. $userList = array_map('intval', $userList);
  583. $userListCondition = implode("','", $userList);
  584. $sql .= " AND u.user_id IN ('$userListCondition')";
  585. }
  586. $sql.=' ORDER BY u.firstname';
  587. $rs = Database::query($sql);
  588. $list_users = array();
  589. while ($row = Database::fetch_array($rs)) {
  590. $list_users[] = $row;
  591. }
  592. return $list_users;
  593. }
  594. /**
  595. * Gets the certificate list by user id
  596. * @param int $user_id The user id
  597. * @param int $cat_id The category id
  598. * @return array
  599. */
  600. public static function get_list_gradebook_certificates_by_user_id($user_id, $cat_id = null)
  601. {
  602. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  603. $sql = 'SELECT gc.score_certificate, gc.created_at, gc.path_certificate, gc.cat_id, gc.user_id, gc.id
  604. FROM ' . $table_certificate . ' gc
  605. WHERE gc.user_id="' . intval($user_id) . '" ';
  606. if (!is_null($cat_id) && $cat_id > 0) {
  607. $sql.=' AND cat_id=' . intval($cat_id);
  608. }
  609. $rs = Database::query($sql);
  610. $list_certificate = array();
  611. while ($row = Database::fetch_array($rs)) {
  612. $list_certificate[] = $row;
  613. }
  614. return $list_certificate;
  615. }
  616. /**
  617. * @param int $user_id
  618. * @param string $course_code
  619. * @param int $sessionId
  620. * @param bool $is_preview
  621. * @param bool $hide_print_button
  622. *
  623. * @return array
  624. */
  625. public static function get_user_certificate_content($user_id, $course_code, $sessionId, $is_preview = false, $hide_print_button = false)
  626. {
  627. // Generate document HTML
  628. $content_html = DocumentManager::replace_user_info_into_html($user_id, $course_code, $sessionId, $is_preview);
  629. $new_content_html = isset($content_html['content']) ? $content_html['content'] : null;
  630. $variables = isset($content_html['variables']) ? $content_html['variables'] : null;
  631. $path_image = api_get_path(WEB_COURSE_PATH) . api_get_course_path($course_code) . '/document/images/gallery';
  632. $new_content_html = str_replace('../images/gallery', $path_image, $new_content_html);
  633. $path_image_in_default_course = api_get_path(WEB_CODE_PATH) . 'default_course_document';
  634. $new_content_html = str_replace('/main/default_course_document', $path_image_in_default_course, $new_content_html);
  635. $new_content_html = str_replace(SYS_CODE_PATH . 'img/', api_get_path(WEB_IMG_PATH), $new_content_html);
  636. $dom = new DOMDocument();
  637. $dom->loadHTML($new_content_html);
  638. //add print header
  639. if (!$hide_print_button) {
  640. $head = $dom->getElementsByTagName('head');
  641. $body = $dom->getElementsByTagName('body');
  642. $printStyle = $dom->createElement('style');
  643. $printStyle->setAttribute('media', 'print');
  644. $printStyle->setAttribute('type', 'text/css');
  645. $printStyle->textContent = '#print_div {visibility:hidden;}';
  646. $head->item(0)->appendChild($printStyle);
  647. $printIcon = $dom->createDocumentFragment();
  648. $printIcon->appendXML(Display::return_icon('printmgr.gif', get_lang('Print')));
  649. $printA = $dom->createElement('button');
  650. $printA->setAttribute('onclick', 'window.print();');
  651. $printA->setAttribute('id', 'print_div');
  652. $printA->setAttribute('style', 'float:right; padding:4px; border: 0 none;');
  653. $printA->appendChild($printIcon);
  654. $body->item(0)->insertBefore($printA, $body->item(0)->firstChild);
  655. }
  656. return array(
  657. 'content' => $dom->saveHTML(),
  658. 'variables' => $variables
  659. );
  660. }
  661. /**
  662. * @param null $course_code
  663. * @param int $gradebook_model_id
  664. * @return mixed
  665. */
  666. public static function create_default_course_gradebook($course_code = null, $gradebook_model_id = 0)
  667. {
  668. if (api_is_allowed_to_edit(true, true)) {
  669. if (!isset($course_code) || empty($course_code)) {
  670. $course_code = api_get_course_id();
  671. }
  672. $session_id = api_get_session_id();
  673. $t = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  674. $sql = "SELECT * FROM $t WHERE course_code = '" . Database::escape_string($course_code) . "' ";
  675. if (!empty($session_id)) {
  676. $sql .= " AND session_id = " . (int) $session_id;
  677. } else {
  678. $sql .= " AND (session_id IS NULL OR session_id = 0) ";
  679. }
  680. $sql .= " ORDER BY id";
  681. $res = Database::query($sql);
  682. if (Database::num_rows($res) < 1) {
  683. //there is no unique category for this course+session combination,
  684. $cat = new Category();
  685. if (!empty($session_id)) {
  686. $my_session_id = api_get_session_id();
  687. $s_name = api_get_session_name($my_session_id);
  688. $cat->set_name($course_code . ' - ' . get_lang('Session') . ' ' . $s_name);
  689. $cat->set_session_id($session_id);
  690. } else {
  691. $cat->set_name($course_code);
  692. }
  693. $cat->set_course_code($course_code);
  694. $cat->set_description(null);
  695. $cat->set_user_id(api_get_user_id());
  696. $cat->set_parent_id(0);
  697. $default_weight_setting = api_get_setting('gradebook_default_weight');
  698. $default_weight = isset($default_weight_setting) && !empty($default_weight_setting) ? $default_weight_setting : 100;
  699. $cat->set_weight($default_weight);
  700. $cat->set_grade_model_id($gradebook_model_id);
  701. $cat->set_certificate_min_score(75);
  702. $cat->set_visible(0);
  703. $cat->add();
  704. $category_id = $cat->get_id();
  705. unset($cat);
  706. } else {
  707. $row = Database::fetch_array($res);
  708. $category_id = $row['id'];
  709. }
  710. return $category_id;
  711. }
  712. return false;
  713. }
  714. /**
  715. * @param FormValidator $form
  716. */
  717. public static function load_gradebook_select_in_tool($form)
  718. {
  719. $course_code = api_get_course_id();
  720. $session_id = api_get_session_id();
  721. self::create_default_course_gradebook();
  722. // Cat list
  723. $all_categories = Category :: load(null, null, $course_code, null, null, $session_id, false);
  724. $select_gradebook = $form->addElement('select', 'category_id', get_lang('SelectGradebook'));
  725. if (!empty($all_categories)) {
  726. foreach ($all_categories as $my_cat) {
  727. if ($my_cat->get_course_code() == api_get_course_id()) {
  728. $grade_model_id = $my_cat->get_grade_model_id();
  729. if (empty($grade_model_id)) {
  730. if ($my_cat->get_parent_id() == 0) {
  731. //$default_weight = $my_cat->get_weight();
  732. $select_gradebook->addoption(get_lang('Default'), $my_cat->get_id());
  733. $cats_added[] = $my_cat->get_id();
  734. } else {
  735. $select_gradebook->addoption($my_cat->get_name(), $my_cat->get_id());
  736. $cats_added[] = $my_cat->get_id();
  737. }
  738. } else {
  739. $select_gradebook->addoption(get_lang('Select'), 0);
  740. }
  741. }
  742. }
  743. }
  744. }
  745. /**
  746. * @param FlatViewTable $flatviewtable
  747. * @param Category $cat
  748. * @param $users
  749. * @param $alleval
  750. * @param $alllinks
  751. * @param array $params
  752. * @param null $mainCourseCategory
  753. */
  754. public static function export_pdf_flatview(
  755. $flatviewtable,
  756. $cat,
  757. $users,
  758. $alleval,
  759. $alllinks,
  760. $params = array(),
  761. $mainCourseCategory = null
  762. ) {
  763. // Getting data
  764. $printable_data = self::get_printable_data($cat[0], $users, $alleval, $alllinks, $params, $mainCourseCategory);
  765. // HTML report creation first
  766. $course_code = trim($cat[0]->get_course_code());
  767. $displayscore = ScoreDisplay :: instance();
  768. $customdisplays = $displayscore->get_custom_score_display_settings();
  769. $total = array();
  770. if (is_array($customdisplays) && count(($customdisplays))) {
  771. foreach ($customdisplays as $custom) {
  772. $total[$custom['display']] = 0;
  773. }
  774. $user_results = $flatviewtable->datagen->get_data_to_graph2(false);
  775. foreach ($user_results as $user_result) {
  776. $total[$user_result[count($user_result) - 1][1]]++;
  777. }
  778. }
  779. $parent_id = $cat[0]->get_parent_id();
  780. if (isset($cat[0]) && isset($parent_id)) {
  781. if ($parent_id == 0) {
  782. $grade_model_id = $cat[0]->get_grade_model_id();
  783. } else {
  784. $parent_cat = Category::load($parent_id);
  785. $grade_model_id = $parent_cat[0]->get_grade_model_id();
  786. }
  787. }
  788. $use_grade_model = true;
  789. if (empty($grade_model_id) || $grade_model_id == -1) {
  790. $use_grade_model = false;
  791. }
  792. if ($use_grade_model) {
  793. if ($parent_id == 0) {
  794. $title = api_strtoupper(get_lang('Average')) . '<br />' . get_lang('Detailed');
  795. } else {
  796. $title = api_strtoupper(get_lang('Average')) . '<br />' . $cat[0]->get_description() . ' - (' . $cat[0]->get_name() . ')';
  797. }
  798. } else {
  799. if ($parent_id == 0) {
  800. $title = api_strtoupper(get_lang('Average')) . '<br />' . get_lang('Detailed');
  801. } else {
  802. $title = api_strtoupper(get_lang('Average'));
  803. }
  804. }
  805. $columns = count($printable_data[0]);
  806. $has_data = is_array($printable_data[1]) && count($printable_data[1]) > 0;
  807. $table = new HTML_Table(array('class' => 'data_table'));
  808. $row = 0;
  809. $column = 0;
  810. $table->setHeaderContents($row, $column, get_lang('NumberAbbreviation'));
  811. $column++;
  812. foreach ($printable_data[0] as $printable_data_cell) {
  813. if (!is_array($printable_data_cell)) {
  814. $printable_data_cell = strip_tags($printable_data_cell);
  815. }
  816. $table->setHeaderContents($row, $column, $printable_data_cell);
  817. $column++;
  818. }
  819. $row++;
  820. if ($has_data) {
  821. $counter = 1;
  822. foreach ($printable_data[1] as &$printable_data_row) {
  823. $column = 0;
  824. $table->setCellContents($row, $column, $counter);
  825. $table->updateCellAttributes($row, $column, 'align="center"');
  826. $column++;
  827. $counter++;
  828. foreach ($printable_data_row as $key => &$printable_data_cell) {
  829. $attributes = array();
  830. $attributes['align'] = 'center';
  831. $attributes['style'] = null;
  832. if ($key === 'name') {
  833. $attributes['align'] = 'left';
  834. }
  835. if ($key === 'total') {
  836. $attributes['style'] = 'font-weight:bold';
  837. }
  838. $table->setCellContents($row, $column, $printable_data_cell);
  839. $table->updateCellAttributes($row, $column, $attributes);
  840. $column++;
  841. }
  842. $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
  843. $row++;
  844. }
  845. } else {
  846. $column = 0;
  847. $table->setCellContents($row, $column, get_lang('NoResults'));
  848. $table->updateCellAttributes($row, $column, 'colspan="' . $columns . '" align="center" class="row_odd"');
  849. }
  850. $pdfParams = array(
  851. 'filename' => get_lang('FlatView') . '_' . api_get_utc_datetime(),
  852. 'pdf_title' => $title,
  853. 'course_code' => $course_code,
  854. 'add_signatures' => true
  855. );
  856. $page_format = $params['orientation'] == 'landscape' ? 'A4-L' : 'A4';
  857. ob_start();
  858. $pdf = new PDF($page_format, $page_format, $pdfParams);
  859. $pdf->html_to_pdf_with_template($flatviewtable->return_table());
  860. $content = ob_get_contents();
  861. ob_end_clean();
  862. echo $content;
  863. exit;
  864. }
  865. /**
  866. * @param string[] $list_values
  867. * @return string
  868. */
  869. public static function score_badges($list_values)
  870. {
  871. $counter = 1;
  872. $badges = array();
  873. foreach ($list_values as $value) {
  874. $class = 'warning';
  875. if ($counter == 1) {
  876. $class = 'success';
  877. }
  878. $counter++;
  879. $badges[] = Display::badge($value, $class);
  880. }
  881. return Display::badge_group($badges);
  882. }
  883. /**
  884. * returns users within a course given by param
  885. * @param string $courseCode
  886. */
  887. public static function get_users_in_course($courseCode)
  888. {
  889. $tbl_course_user = Database:: get_main_table(TABLE_MAIN_COURSE_USER);
  890. $tbl_session_course_user = Database:: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  891. $tbl_user = Database:: get_main_table(TABLE_MAIN_USER);
  892. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname ASC' : ' ORDER BY lastname, firstname ASC';
  893. $current_session = api_get_session_id();
  894. $courseCode = Database::escape_string($courseCode);
  895. $courseInfo = api_get_course_info($courseCode);
  896. $courseId = $courseInfo['real_id'];
  897. if (!empty($current_session)) {
  898. $sql = "SELECT user.user_id, user.username, lastname, firstname, official_code
  899. FROM $tbl_session_course_user as scru, $tbl_user as user
  900. WHERE
  901. scru.user_id = user.user_id AND
  902. scru.status=0 AND
  903. scru.c_id='$courseId' AND
  904. session_id ='$current_session'
  905. $order_clause
  906. ";
  907. } else {
  908. $sql = 'SELECT user.user_id, user.username, lastname, firstname, official_code
  909. FROM '.$tbl_course_user.' as course_rel_user, '.$tbl_user.' as user
  910. WHERE
  911. course_rel_user.user_id=user.user_id AND
  912. course_rel_user.status='.STUDENT.' AND
  913. course_rel_user.c_id = "'.$courseId.'" '.
  914. $order_clause;
  915. }
  916. $result = Database::query($sql);
  917. return self::get_user_array_from_sql_result($result);
  918. }
  919. /**
  920. * @param Doctrine\DBAL\Driver\Statement|null $result
  921. * @return array
  922. */
  923. public static function get_user_array_from_sql_result($result)
  924. {
  925. $a_students = array();
  926. while ($user = Database::fetch_array($result)) {
  927. if (!array_key_exists($user['user_id'], $a_students)) {
  928. $a_current_student = array ();
  929. $a_current_student[] = $user['user_id'];
  930. $a_current_student[] = $user['username'];
  931. $a_current_student[] = $user['lastname'];
  932. $a_current_student[] = $user['firstname'];
  933. $a_current_student[] = $user['official_code'];
  934. $a_students['STUD'.$user['user_id']] = $a_current_student;
  935. }
  936. }
  937. return $a_students;
  938. }
  939. /**
  940. * @param array $evals
  941. * @param array $links
  942. * @return array
  943. */
  944. public static function get_all_users($evals = array(), $links = array())
  945. {
  946. $coursecodes = array();
  947. // By default add all user in course
  948. $coursecodes[api_get_course_id()] = '1';
  949. $users = GradebookUtils::get_users_in_course(api_get_course_id());
  950. foreach ($evals as $eval) {
  951. $coursecode = $eval->get_course_code();
  952. // evaluation in course
  953. if (isset($coursecode) && !empty($coursecode)) {
  954. if (!array_key_exists($coursecode, $coursecodes)) {
  955. $coursecodes[$coursecode] = '1';
  956. $users = array_merge($users, GradebookUtils::get_users_in_course($coursecode));
  957. }
  958. } else {
  959. // course independent evaluation
  960. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  961. $tbl_res = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  962. $sql = 'SELECT user.user_id, lastname, firstname, user.official_code
  963. FROM '.$tbl_res.' as res, '.$tbl_user.' as user
  964. WHERE
  965. res.evaluation_id = '.intval($eval->get_id()).' AND
  966. res.user_id = user.user_id
  967. ';
  968. $sql .= ' ORDER BY lastname, firstname';
  969. if (api_is_western_name_order()) {
  970. $sql .= ' ORDER BY firstname, lastname';
  971. }
  972. $result = Database::query($sql);
  973. $users = array_merge($users, GradebookUtils::get_user_array_from_sql_result($result));
  974. }
  975. }
  976. foreach ($links as $link) {
  977. // links are always in a course
  978. $coursecode = $link->get_course_code();
  979. if (!array_key_exists($coursecode,$coursecodes)) {
  980. $coursecodes[$coursecode] = '1';
  981. $users = array_merge($users, GradebookUtils::get_users_in_course($coursecode));
  982. }
  983. }
  984. return $users;
  985. }
  986. /**
  987. * Search students matching a given last name and/or first name
  988. * @author Bert Steppé
  989. */
  990. public static function find_students($mask= '')
  991. {
  992. // students shouldn't be here // don't search if mask empty
  993. if (!api_is_allowed_to_edit() || empty ($mask)) {
  994. return null;
  995. }
  996. $mask = Database::escape_string($mask);
  997. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  998. $tbl_cru = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  999. $sql = 'SELECT DISTINCT user.user_id, user.lastname, user.firstname, user.email, user.official_code
  1000. FROM ' . $tbl_user . ' user';
  1001. if (!api_is_platform_admin()) {
  1002. $sql .= ', ' . $tbl_cru . ' cru';
  1003. }
  1004. $sql .= ' WHERE user.status = ' . STUDENT;
  1005. $sql .= ' AND (user.lastname LIKE '."'%" . $mask . "%'";
  1006. $sql .= ' OR user.firstname LIKE '."'%" . $mask . "%')";
  1007. if (!api_is_platform_admin()) {
  1008. $sql .= ' AND user.user_id = cru.user_id AND
  1009. cru.relation_type <> '.COURSE_RELATION_TYPE_RRHH.' AND
  1010. cru.c_id in (
  1011. SELECT c_id FROM '.$tbl_cru . '
  1012. WHERE
  1013. user_id = ' . api_get_user_id() . ' AND
  1014. status = ' . COURSEMANAGER . '
  1015. )
  1016. ';
  1017. }
  1018. $sql .= ' ORDER BY lastname, firstname';
  1019. if (api_is_western_name_order()) {
  1020. $sql .= ' ORDER BY firstname, lastname';
  1021. }
  1022. $result = Database::query($sql);
  1023. $users = Database::store_result($result);
  1024. return $users;
  1025. }
  1026. /**
  1027. * @param int $linkId
  1028. * @param float $weight
  1029. */
  1030. public static function updateLinkWeight($linkId, $name, $weight)
  1031. {
  1032. $linkId = intval($linkId);
  1033. $weight = floatval($weight);
  1034. $course_id = api_get_course_int_id();
  1035. AbstractLink::add_link_log($linkId, $name);
  1036. $table_link = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  1037. $em = Database::getManager();
  1038. $tbl_forum_thread = Database:: get_course_table(TABLE_FORUM_THREAD);
  1039. $tbl_attendance = Database:: get_course_table(TABLE_ATTENDANCE);
  1040. $sql = 'UPDATE '.$table_link.' SET weight = '."'".Database::escape_string($weight)."'".'
  1041. WHERE id = '.$linkId;
  1042. Database::query($sql);
  1043. // Update weight for attendance
  1044. $sql = 'SELECT ref_id FROM '.$table_link.'
  1045. WHERE id = '.$linkId.' AND type='.LINK_ATTENDANCE;
  1046. $rs_attendance = Database::query($sql);
  1047. if (Database::num_rows($rs_attendance) > 0) {
  1048. $row_attendance = Database::fetch_array($rs_attendance);
  1049. $sql = 'UPDATE '.$tbl_attendance.' SET attendance_weight ='.$weight.'
  1050. WHERE c_id = '.$course_id.' AND id = '.intval($row_attendance['ref_id']);
  1051. Database::query($sql);
  1052. }
  1053. // Update weight into forum thread
  1054. $sql = 'UPDATE '.$tbl_forum_thread.' SET thread_weight='.$weight.'
  1055. WHERE
  1056. c_id = '.$course_id.' AND
  1057. thread_id = (
  1058. SELECT ref_id FROM '.$table_link.'
  1059. WHERE id='.$linkId.' AND type='.LINK_FORUM_THREAD.'
  1060. )
  1061. ';
  1062. Database::query($sql);
  1063. //Update weight into student publication(work)
  1064. $em
  1065. ->createQuery('
  1066. UPDATE ChamiloCourseBundle:CStudentPublication w
  1067. SET w.weight = :final_weight
  1068. WHERE w.cId = :course
  1069. AND w.id = (
  1070. SELECT l.refId FROM ChamiloCoreBundle:GradebookLink l
  1071. WHERE l.id = :link AND l.type = :type
  1072. )
  1073. ')
  1074. ->execute([
  1075. 'final_weight' => $weight,
  1076. 'course' => $course_id,
  1077. 'link' => $linkId,
  1078. 'type' => LINK_STUDENTPUBLICATION
  1079. ]);
  1080. }
  1081. /**
  1082. * @param int $id
  1083. * @param float $weight
  1084. */
  1085. public static function updateEvaluationWeight($id, $weight)
  1086. {
  1087. $table_evaluation = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  1088. $id = intval($id);
  1089. $evaluation = new Evaluation();
  1090. $evaluation->add_evaluation_log($id);
  1091. $sql = 'UPDATE '.$table_evaluation.'
  1092. SET weight = '."'".Database::escape_string($weight)."'".'
  1093. WHERE id = '.$id;
  1094. Database::query($sql);
  1095. }
  1096. /**
  1097. *
  1098. * Get the achieved certificates for a user in courses
  1099. * @param int $userId The user id
  1100. * @param bool $includeNonPublicCertificates Whether include the non-plublic certificates
  1101. * @return array
  1102. */
  1103. public static function getUserCertificatesInCourses($userId, $includeNonPublicCertificates = true)
  1104. {
  1105. $userId = intval($userId);
  1106. $courseList = [];
  1107. $courses = CourseManager::get_courses_list_by_user_id($userId);
  1108. foreach ($courses as $course) {
  1109. if (!$includeNonPublicCertificates) {
  1110. $allowPublicCertificates = api_get_course_setting('allow_public_certificates', $course['code']);
  1111. if (empty($allowPublicCertificates)) {
  1112. continue;
  1113. }
  1114. }
  1115. $courseGradebookCategory = Category::load(null, null, $course['code']);
  1116. if (empty($courseGradebookCategory)) {
  1117. continue;
  1118. }
  1119. $courseGradebookId = $courseGradebookCategory[0]->get_id();
  1120. $certificateInfo = GradebookUtils::get_certificate_by_user_id($courseGradebookId, $userId);
  1121. if (empty($certificateInfo)) {
  1122. continue;
  1123. }
  1124. $courseInfo = api_get_course_info_by_id($course['real_id']);
  1125. $courseList[] = [
  1126. 'course' => $courseInfo['title'],
  1127. 'score' => $certificateInfo['score_certificate'],
  1128. 'date' => api_format_date($certificateInfo['created_at'], DATE_FORMAT_SHORT),
  1129. 'link' => api_get_path(WEB_PATH) . "certificates/index.php?id={$certificateInfo['id']}"
  1130. ];
  1131. }
  1132. return $courseList;
  1133. }
  1134. /**
  1135. * Get the achieved certificates for a user in course sessions
  1136. * @param int $userId The user id
  1137. * @param bool $includeNonPublicCertificates Whether include the non-plublic certificates
  1138. * @return array
  1139. */
  1140. public static function getUserCertificatesInSessions($userId, $includeNonPublicCertificates = true)
  1141. {
  1142. $userId = intval($userId);
  1143. $sessionList = [];
  1144. $sessions = SessionManager::get_sessions_by_user($userId, true, true);
  1145. foreach ($sessions as $session) {
  1146. if (empty($session['courses'])) {
  1147. continue;
  1148. }
  1149. $sessionCourses = SessionManager::get_course_list_by_session_id($session['session_id']);
  1150. foreach ($sessionCourses as $course) {
  1151. if (!$includeNonPublicCertificates) {
  1152. $allowPublicCertificates = api_get_course_setting('allow_public_certificates', $course['code']);
  1153. if (empty($allowPublicCertificates)) {
  1154. continue;
  1155. }
  1156. }
  1157. $courseGradebookCategory = Category::load(
  1158. null,
  1159. null,
  1160. $course['code'],
  1161. null,
  1162. null,
  1163. $session['session_id']
  1164. );
  1165. if (empty($courseGradebookCategory)) {
  1166. continue;
  1167. }
  1168. $courseGradebookId = $courseGradebookCategory[0]->get_id();
  1169. $certificateInfo = GradebookUtils::get_certificate_by_user_id(
  1170. $courseGradebookId,
  1171. $userId
  1172. );
  1173. if (empty($certificateInfo)) {
  1174. continue;
  1175. }
  1176. $sessionList[] = [
  1177. 'session' => $session['session_name'],
  1178. 'course' => $course['title'],
  1179. 'score' => $certificateInfo['score_certificate'],
  1180. 'date' => api_format_date($certificateInfo['created_at'], DATE_FORMAT_SHORT),
  1181. 'link' => api_get_path(WEB_PATH) . "certificates/index.php?id={$certificateInfo['id']}"
  1182. ];
  1183. }
  1184. }
  1185. return $sessionList;
  1186. }
  1187. /**
  1188. * @param int $userId
  1189. * @param array $cats
  1190. * @param bool $saveToFile
  1191. * @param bool $saveToHtmlFile
  1192. * @param array $studentList
  1193. * @param PDF $pdf
  1194. *
  1195. * @return string
  1196. */
  1197. public static function generateTable(
  1198. $userId,
  1199. $cats,
  1200. $saveToFile = false,
  1201. $saveToHtmlFile = false,
  1202. $studentList = array(),
  1203. $pdf = null
  1204. ) {
  1205. $courseInfo = api_get_course_info();
  1206. $userInfo = api_get_user_info($userId);
  1207. $cat = $cats[0];
  1208. $allcat = $cats[0]->get_subcategories(
  1209. $userId,
  1210. api_get_course_id(),
  1211. api_get_session_id()
  1212. );
  1213. $alleval = $cats[0]->get_evaluations($userId);
  1214. $alllink = $cats[0]->get_links($userId);
  1215. $gradebooktable = new GradebookTable(
  1216. $cat,
  1217. $allcat,
  1218. $alleval,
  1219. $alllink,
  1220. null, // params
  1221. true, // $exportToPdf
  1222. false, // showteacher
  1223. $userId,
  1224. $studentList
  1225. );
  1226. $gradebooktable->userId = $userId;
  1227. if (api_is_allowed_to_edit()) {
  1228. $gradebooktable->td_attributes = [
  1229. 4 => 'class=centered'
  1230. ];
  1231. } else {
  1232. $gradebooktable->td_attributes = [
  1233. 3 => 'class=centered',
  1234. 4 => 'class=centered',
  1235. 5 => 'class=centered',
  1236. 6 => 'class=centered',
  1237. 7 => 'class=centered'
  1238. ];
  1239. }
  1240. $table = $gradebooktable->return_table();
  1241. $graph = $gradebooktable->getGraph();
  1242. $sessionName = api_get_session_name(api_get_session_id());
  1243. $sessionName = !empty($sessionName) ? " - $sessionName" : '';
  1244. $params = array(
  1245. 'pdf_title' => sprintf(get_lang('GradeFromX'), $courseInfo['name']),
  1246. 'session_info' => '',
  1247. 'course_info' => '',
  1248. 'pdf_date' => '',
  1249. 'course_code' => api_get_course_id(),
  1250. 'add_signatures' => false,
  1251. 'student_info' => $userInfo,
  1252. 'show_grade_generated_date' => true,
  1253. 'show_real_course_teachers' => false,
  1254. 'show_teacher_as_myself' => false,
  1255. 'orientation' => 'P'
  1256. );
  1257. if (empty($pdf)) {
  1258. $pdf = new PDF('A4', $params['orientation'], $params);
  1259. }
  1260. $pdf->params['student_info'] = $userInfo;
  1261. $file = api_get_path(SYS_ARCHIVE_PATH).uniqid().'.html';
  1262. $content =
  1263. $table.
  1264. $graph.
  1265. '<br />'.get_lang('Feedback').'<br />
  1266. <textarea rows="5" cols="100" ></textarea>';
  1267. $address = api_get_setting('institution_address');
  1268. $phone = api_get_setting('administratorTelephone');
  1269. $address = str_replace('\n', '<br />', $address);
  1270. $pdf->custom_header = array('html' => "<h5 align='right'>$address <br />$phone</h5>");
  1271. $result = $pdf->html_to_pdf_with_template(
  1272. $content,
  1273. $saveToFile,
  1274. $saveToHtmlFile
  1275. );
  1276. if ($saveToHtmlFile) {
  1277. file_put_contents($file, $result);
  1278. return $file;
  1279. }
  1280. return $file;
  1281. }
  1282. }