GradebookUtils.php 58 KB

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