GradebookUtils.php 57 KB

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