GradebookUtils.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  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. $cat_id = (int) $cat_id;
  649. $user_id = (int) $user_id;
  650. $sql = "SELECT COUNT(id) as count
  651. FROM $table gc
  652. WHERE gc.cat_id = $cat_id AND user_id = $user_id ";
  653. $rs_exist = Database::query($sql);
  654. $row = Database::fetch_array($rs_exist);
  655. if ($row['count'] == 0) {
  656. $params = [
  657. 'cat_id' => $cat_id,
  658. 'user_id' => $user_id,
  659. 'score_certificate' => $score_certificate,
  660. 'created_at' => $date_certificate,
  661. ];
  662. Database::insert($table, $params);
  663. }
  664. }
  665. /**
  666. * Get date of user certificate.
  667. *
  668. * @param int $cat_id The category id
  669. * @param int $user_id The user id
  670. *
  671. * @return Datetime The date when you obtained the certificate
  672. */
  673. public static function get_certificate_by_user_id($cat_id, $user_id)
  674. {
  675. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  676. $cat_id = (int) $cat_id;
  677. $user_id = (int) $user_id;
  678. $sql = "SELECT * FROM $table
  679. WHERE cat_id = $cat_id AND user_id = $user_id ";
  680. $result = Database::query($sql);
  681. $row = Database::fetch_array($result, 'ASSOC');
  682. return $row;
  683. }
  684. /**
  685. * Get list of users certificates.
  686. *
  687. * @param int $cat_id The category id
  688. * @param array $userList Only users in this list
  689. *
  690. * @return array
  691. */
  692. public static function get_list_users_certificates($cat_id = null, $userList = [])
  693. {
  694. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  695. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  696. $sql = 'SELECT DISTINCT u.user_id, u.lastname, u.firstname, u.username
  697. FROM '.$table_user.' u
  698. INNER JOIN '.$table_certificate.' gc
  699. ON u.user_id=gc.user_id ';
  700. if (!is_null($cat_id) && $cat_id > 0) {
  701. $sql .= ' WHERE cat_id='.intval($cat_id);
  702. }
  703. if (!empty($userList)) {
  704. $userList = array_map('intval', $userList);
  705. $userListCondition = implode("','", $userList);
  706. $sql .= " AND u.user_id IN ('$userListCondition')";
  707. }
  708. $sql .= ' ORDER BY '.(api_sort_by_first_name() ? 'u.firstname' : 'u.lastname');
  709. $rs = Database::query($sql);
  710. $list_users = [];
  711. while ($row = Database::fetch_array($rs)) {
  712. $list_users[] = $row;
  713. }
  714. return $list_users;
  715. }
  716. /**
  717. * Gets the certificate list by user id.
  718. *
  719. * @param int $user_id The user id
  720. * @param int $cat_id The category id
  721. *
  722. * @return array
  723. */
  724. public static function get_list_gradebook_certificates_by_user_id(
  725. $user_id,
  726. $cat_id = null
  727. ) {
  728. $user_id = (int) $user_id;
  729. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  730. $sql = 'SELECT
  731. gc.score_certificate,
  732. gc.created_at,
  733. gc.path_certificate,
  734. gc.cat_id,
  735. gc.user_id,
  736. gc.id
  737. FROM '.$table_certificate.' gc
  738. WHERE gc.user_id = "'.$user_id.'" ';
  739. if (!is_null($cat_id) && $cat_id > 0) {
  740. $sql .= ' AND cat_id='.intval($cat_id);
  741. }
  742. $rs = Database::query($sql);
  743. $list = [];
  744. while ($row = Database::fetch_array($rs)) {
  745. $list[] = $row;
  746. }
  747. return $list;
  748. }
  749. /**
  750. * @param int $user_id
  751. * @param string $course_code
  752. * @param int $sessionId
  753. * @param bool $is_preview
  754. * @param bool $hide_print_button
  755. *
  756. * @return array
  757. */
  758. public static function get_user_certificate_content(
  759. $user_id,
  760. $course_code,
  761. $sessionId,
  762. $is_preview = false,
  763. $hide_print_button = false
  764. ) {
  765. // Generate document HTML
  766. $content_html = DocumentManager::replace_user_info_into_html(
  767. $user_id,
  768. $course_code,
  769. $sessionId,
  770. $is_preview
  771. );
  772. $new_content_html = isset($content_html['content']) ? $content_html['content'] : null;
  773. $variables = isset($content_html['variables']) ? $content_html['variables'] : null;
  774. $path_image = api_get_path(WEB_COURSE_PATH).api_get_course_path($course_code).'/document/images/gallery';
  775. $new_content_html = str_replace('../images/gallery', $path_image, $new_content_html);
  776. $path_image_in_default_course = api_get_path(WEB_CODE_PATH).'default_course_document';
  777. $new_content_html = str_replace('/main/default_course_document', $path_image_in_default_course, $new_content_html);
  778. $new_content_html = str_replace(SYS_CODE_PATH.'img/', api_get_path(WEB_IMG_PATH), $new_content_html);
  779. //add print header
  780. if (!$hide_print_button) {
  781. $print = '<style>#print_div {
  782. padding:4px;border: 0 none;position: absolute;top: 0px;right: 0px;
  783. }
  784. @media print {
  785. #print_div {
  786. display: none !important;
  787. }
  788. }
  789. </style>';
  790. $print .= Display::div(
  791. Display::url(
  792. Display::return_icon('printmgr.gif', get_lang('Print')),
  793. 'javascript:void()',
  794. ['onclick' => 'window.print();']
  795. ),
  796. ['id' => 'print_div']
  797. );
  798. $print .= '</html>';
  799. $new_content_html = str_replace('</html>', $print, $new_content_html);
  800. }
  801. return [
  802. 'content' => $new_content_html,
  803. 'variables' => $variables,
  804. ];
  805. }
  806. /**
  807. * @param null $course_code
  808. * @param int $gradebook_model_id
  809. *
  810. * @return mixed
  811. */
  812. public static function create_default_course_gradebook(
  813. $course_code = null,
  814. $gradebook_model_id = 0
  815. ) {
  816. if (api_is_allowed_to_edit(true, true)) {
  817. if (!isset($course_code) || empty($course_code)) {
  818. $course_code = api_get_course_id();
  819. }
  820. $session_id = api_get_session_id();
  821. $t = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  822. $sql = "SELECT * FROM $t
  823. WHERE course_code = '".Database::escape_string($course_code)."' ";
  824. if (!empty($session_id)) {
  825. $sql .= " AND session_id = ".$session_id;
  826. } else {
  827. $sql .= ' AND (session_id IS NULL OR session_id = 0) ';
  828. }
  829. $sql .= ' ORDER BY id ';
  830. $res = Database::query($sql);
  831. if (Database::num_rows($res) < 1) {
  832. //there is no unique category for this course+session combination,
  833. $cat = new Category();
  834. if (!empty($session_id)) {
  835. $my_session_id = api_get_session_id();
  836. $s_name = api_get_session_name($my_session_id);
  837. $cat->set_name($course_code.' - '.get_lang('Session').' '.$s_name);
  838. $cat->set_session_id($session_id);
  839. } else {
  840. $cat->set_name($course_code);
  841. }
  842. $cat->set_course_code($course_code);
  843. $cat->set_description(null);
  844. $cat->set_user_id(api_get_user_id());
  845. $cat->set_parent_id(0);
  846. $default_weight_setting = api_get_setting('gradebook_default_weight');
  847. $default_weight = isset($default_weight_setting) && !empty($default_weight_setting) ? $default_weight_setting : 100;
  848. $cat->set_weight($default_weight);
  849. $cat->set_grade_model_id($gradebook_model_id);
  850. $cat->set_certificate_min_score(75);
  851. $cat->set_visible(0);
  852. $cat->add();
  853. $category_id = $cat->get_id();
  854. unset($cat);
  855. } else {
  856. $row = Database::fetch_array($res);
  857. $category_id = $row['id'];
  858. }
  859. return $category_id;
  860. }
  861. return false;
  862. }
  863. /**
  864. * @param FormValidator $form
  865. */
  866. public static function load_gradebook_select_in_tool($form)
  867. {
  868. $course_code = api_get_course_id();
  869. $session_id = api_get_session_id();
  870. self::create_default_course_gradebook();
  871. // Cat list
  872. $all_categories = Category::load(
  873. null,
  874. null,
  875. $course_code,
  876. null,
  877. null,
  878. $session_id,
  879. false
  880. );
  881. $select_gradebook = $form->addElement(
  882. 'select',
  883. 'category_id',
  884. get_lang('SelectGradebook')
  885. );
  886. if (!empty($all_categories)) {
  887. foreach ($all_categories as $my_cat) {
  888. if ($my_cat->get_course_code() == api_get_course_id()) {
  889. $grade_model_id = $my_cat->get_grade_model_id();
  890. if (empty($grade_model_id)) {
  891. if ($my_cat->get_parent_id() == 0) {
  892. //$default_weight = $my_cat->get_weight();
  893. $select_gradebook->addoption(get_lang('Default'), $my_cat->get_id());
  894. $cats_added[] = $my_cat->get_id();
  895. } else {
  896. $select_gradebook->addoption($my_cat->get_name(), $my_cat->get_id());
  897. $cats_added[] = $my_cat->get_id();
  898. }
  899. } else {
  900. $select_gradebook->addoption(get_lang('Select'), 0);
  901. }
  902. }
  903. }
  904. }
  905. }
  906. /**
  907. * @param FlatViewTable $flatviewtable
  908. * @param Category $cat
  909. * @param $users
  910. * @param $alleval
  911. * @param $alllinks
  912. * @param array $params
  913. * @param null $mainCourseCategory
  914. */
  915. public static function export_pdf_flatview(
  916. $flatviewtable,
  917. $cat,
  918. $users,
  919. $alleval,
  920. $alllinks,
  921. $params = [],
  922. $mainCourseCategory = null
  923. ) {
  924. // Getting data
  925. $printable_data = self::get_printable_data(
  926. $cat[0],
  927. $users,
  928. $alleval,
  929. $alllinks,
  930. $params,
  931. $mainCourseCategory
  932. );
  933. // HTML report creation first
  934. $course_code = trim($cat[0]->get_course_code());
  935. $displayscore = ScoreDisplay::instance();
  936. $customDisplays = $displayscore->get_custom_score_display_settings();
  937. $total = [];
  938. if (is_array($customDisplays) && count(($customDisplays))) {
  939. foreach ($customDisplays as $custom) {
  940. $total[$custom['display']] = 0;
  941. }
  942. $user_results = $flatviewtable->datagen->get_data_to_graph2(false);
  943. foreach ($user_results as $user_result) {
  944. $item = $user_result[count($user_result) - 1];
  945. $customTag = isset($item[1]) ? strip_tags($item[1]) : '';
  946. $total[$customTag]++;
  947. }
  948. }
  949. $parent_id = $cat[0]->get_parent_id();
  950. if (isset($cat[0]) && isset($parent_id)) {
  951. if ($parent_id == 0) {
  952. $grade_model_id = $cat[0]->get_grade_model_id();
  953. } else {
  954. $parent_cat = Category::load($parent_id);
  955. $grade_model_id = $parent_cat[0]->get_grade_model_id();
  956. }
  957. }
  958. $use_grade_model = true;
  959. if (empty($grade_model_id) || $grade_model_id == -1) {
  960. $use_grade_model = false;
  961. }
  962. if ($use_grade_model) {
  963. if ($parent_id == 0) {
  964. $title = api_strtoupper(get_lang('Average')).'<br />'.get_lang('Detailed');
  965. } else {
  966. $title = api_strtoupper(get_lang('Average')).'<br />'.$cat[0]->get_description().' - ('.$cat[0]->get_name().')';
  967. }
  968. } else {
  969. if ($parent_id == 0) {
  970. $title = api_strtoupper(get_lang('Average')).'<br />'.get_lang('Detailed');
  971. } else {
  972. $title = api_strtoupper(get_lang('Average'));
  973. }
  974. }
  975. $columns = count($printable_data[0]);
  976. $has_data = is_array($printable_data[1]) && count($printable_data[1]) > 0;
  977. $table = new HTML_Table(['class' => 'data_table']);
  978. $row = 0;
  979. $column = 0;
  980. $table->setHeaderContents($row, $column, get_lang('NumberAbbreviation'));
  981. $column++;
  982. foreach ($printable_data[0] as $printable_data_cell) {
  983. if (!is_array($printable_data_cell)) {
  984. $printable_data_cell = strip_tags($printable_data_cell);
  985. }
  986. $table->setHeaderContents($row, $column, $printable_data_cell);
  987. $column++;
  988. }
  989. $row++;
  990. if ($has_data) {
  991. $counter = 1;
  992. foreach ($printable_data[1] as &$printable_data_row) {
  993. $column = 0;
  994. $table->setCellContents($row, $column, $counter);
  995. $table->updateCellAttributes($row, $column, 'align="center"');
  996. $column++;
  997. $counter++;
  998. foreach ($printable_data_row as $key => &$printable_data_cell) {
  999. $attributes = [];
  1000. $attributes['align'] = 'center';
  1001. $attributes['style'] = null;
  1002. if ($key === 'name') {
  1003. $attributes['align'] = 'left';
  1004. }
  1005. if ($key === 'total') {
  1006. $attributes['style'] = 'font-weight:bold';
  1007. }
  1008. $table->setCellContents($row, $column, $printable_data_cell);
  1009. $table->updateCellAttributes($row, $column, $attributes);
  1010. $column++;
  1011. }
  1012. $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
  1013. $row++;
  1014. }
  1015. } else {
  1016. $column = 0;
  1017. $table->setCellContents($row, $column, get_lang('NoResults'));
  1018. $table->updateCellAttributes($row, $column, 'colspan="'.$columns.'" align="center" class="row_odd"');
  1019. }
  1020. $pdfParams = [
  1021. 'filename' => get_lang('FlatView').'_'.api_get_local_time(),
  1022. 'pdf_title' => $title,
  1023. 'course_code' => $course_code,
  1024. 'add_signatures' => ['Drh', 'Teacher', 'Date'],
  1025. ];
  1026. $page_format = $params['orientation'] === 'landscape' ? 'A4-L' : 'A4';
  1027. ob_start();
  1028. $pdf = new PDF($page_format, $page_format, $pdfParams);
  1029. $pdf->html_to_pdf_with_template($flatviewtable->return_table(), false, false, true);
  1030. $content = ob_get_contents();
  1031. ob_end_clean();
  1032. echo $content;
  1033. exit;
  1034. }
  1035. /**
  1036. * @param string[] $list_values
  1037. *
  1038. * @return string
  1039. */
  1040. public static function score_badges($list_values)
  1041. {
  1042. $counter = 1;
  1043. $badges = [];
  1044. foreach ($list_values as $value) {
  1045. $class = 'warning';
  1046. if ($counter == 1) {
  1047. $class = 'success';
  1048. }
  1049. $counter++;
  1050. $badges[] = Display::badge($value, $class);
  1051. }
  1052. return Display::badge_group($badges);
  1053. }
  1054. /**
  1055. * returns users within a course given by param.
  1056. *
  1057. * @param string $courseCode
  1058. *
  1059. * @deprecated use CourseManager
  1060. *
  1061. * @return array
  1062. */
  1063. public static function get_users_in_course($courseCode)
  1064. {
  1065. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1066. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1067. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  1068. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname ASC' : ' ORDER BY lastname, firstname ASC';
  1069. $current_session = api_get_session_id();
  1070. $courseCode = Database::escape_string($courseCode);
  1071. $courseInfo = api_get_course_info($courseCode);
  1072. $courseId = $courseInfo['real_id'];
  1073. if (!empty($current_session)) {
  1074. $sql = "SELECT user.user_id, user.username, lastname, firstname, official_code
  1075. FROM $tbl_session_course_user as scru
  1076. INNER JOIN $tbl_user as user
  1077. ON (scru.user_id = user.user_id)
  1078. WHERE
  1079. scru.status = 0 AND
  1080. scru.c_id='$courseId' AND
  1081. session_id ='$current_session'
  1082. $order_clause
  1083. ";
  1084. } else {
  1085. $sql = 'SELECT user.user_id, user.username, lastname, firstname, official_code
  1086. FROM '.$tbl_course_user.' as course_rel_user
  1087. INNER JOIN '.$tbl_user.' as user
  1088. ON (course_rel_user.user_id = user.id)
  1089. WHERE
  1090. course_rel_user.status = '.STUDENT.' AND
  1091. course_rel_user.c_id = "'.$courseId.'" '.
  1092. $order_clause;
  1093. }
  1094. $result = Database::query($sql);
  1095. return self::get_user_array_from_sql_result($result);
  1096. }
  1097. /**
  1098. * @param Doctrine\DBAL\Driver\Statement|null $result
  1099. *
  1100. * @return array
  1101. */
  1102. public static function get_user_array_from_sql_result($result)
  1103. {
  1104. $a_students = [];
  1105. while ($user = Database::fetch_array($result)) {
  1106. if (!array_key_exists($user['user_id'], $a_students)) {
  1107. $a_current_student = [];
  1108. $a_current_student[] = $user['user_id'];
  1109. $a_current_student[] = $user['username'];
  1110. $a_current_student[] = $user['lastname'];
  1111. $a_current_student[] = $user['firstname'];
  1112. $a_current_student[] = $user['official_code'];
  1113. $a_students['STUD'.$user['user_id']] = $a_current_student;
  1114. }
  1115. }
  1116. return $a_students;
  1117. }
  1118. /**
  1119. * @param array $evals
  1120. * @param array $links
  1121. *
  1122. * @return array
  1123. */
  1124. public static function get_all_users($evals = [], $links = [])
  1125. {
  1126. $coursecodes = [];
  1127. // By default add all user in course
  1128. $coursecodes[api_get_course_id()] = '1';
  1129. $users = self::get_users_in_course(api_get_course_id());
  1130. foreach ($evals as $eval) {
  1131. $coursecode = $eval->get_course_code();
  1132. // evaluation in course
  1133. if (isset($coursecode) && !empty($coursecode)) {
  1134. if (!array_key_exists($coursecode, $coursecodes)) {
  1135. $coursecodes[$coursecode] = '1';
  1136. $users = array_merge($users, self::get_users_in_course($coursecode));
  1137. }
  1138. } else {
  1139. // course independent evaluation
  1140. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  1141. $tbl_res = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  1142. $sql = 'SELECT user.user_id, lastname, firstname, user.official_code
  1143. FROM '.$tbl_res.' as res, '.$tbl_user.' as user
  1144. WHERE
  1145. res.evaluation_id = '.intval($eval->get_id()).' AND
  1146. res.user_id = user.user_id
  1147. ';
  1148. $sql .= ' ORDER BY lastname, firstname';
  1149. if (api_is_western_name_order()) {
  1150. $sql .= ' ORDER BY firstname, lastname';
  1151. }
  1152. $result = Database::query($sql);
  1153. $users = array_merge(
  1154. $users,
  1155. self::get_user_array_from_sql_result($result)
  1156. );
  1157. }
  1158. }
  1159. foreach ($links as $link) {
  1160. // links are always in a course
  1161. $coursecode = $link->get_course_code();
  1162. if (!array_key_exists($coursecode, $coursecodes)) {
  1163. $coursecodes[$coursecode] = '1';
  1164. $users = array_merge(
  1165. $users,
  1166. self::get_users_in_course($coursecode)
  1167. );
  1168. }
  1169. }
  1170. return $users;
  1171. }
  1172. /**
  1173. * Search students matching a given last name and/or first name.
  1174. *
  1175. * @author Bert Steppé
  1176. */
  1177. public static function find_students($mask = '')
  1178. {
  1179. // students shouldn't be here // don't search if mask empty
  1180. if (!api_is_allowed_to_edit() || empty($mask)) {
  1181. return null;
  1182. }
  1183. $mask = Database::escape_string($mask);
  1184. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  1185. $tbl_cru = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1186. $sql = 'SELECT DISTINCT user.user_id, user.lastname, user.firstname, user.email, user.official_code
  1187. FROM '.$tbl_user.' user';
  1188. if (!api_is_platform_admin()) {
  1189. $sql .= ', '.$tbl_cru.' cru';
  1190. }
  1191. $sql .= ' WHERE user.status = '.STUDENT;
  1192. $sql .= ' AND (user.lastname LIKE '."'%".$mask."%'";
  1193. $sql .= ' OR user.firstname LIKE '."'%".$mask."%')";
  1194. if (!api_is_platform_admin()) {
  1195. $sql .= ' AND user.user_id = cru.user_id AND
  1196. cru.relation_type <> '.COURSE_RELATION_TYPE_RRHH.' AND
  1197. cru.c_id in (
  1198. SELECT c_id FROM '.$tbl_cru.'
  1199. WHERE
  1200. user_id = '.api_get_user_id().' AND
  1201. status = '.COURSEMANAGER.'
  1202. )
  1203. ';
  1204. }
  1205. $sql .= ' ORDER BY lastname, firstname';
  1206. if (api_is_western_name_order()) {
  1207. $sql .= ' ORDER BY firstname, lastname';
  1208. }
  1209. $result = Database::query($sql);
  1210. $users = Database::store_result($result);
  1211. return $users;
  1212. }
  1213. /**
  1214. * @param int $linkId
  1215. * @param float $weight
  1216. */
  1217. public static function updateLinkWeight($linkId, $name, $weight)
  1218. {
  1219. $linkId = (int) $linkId;
  1220. $weight = api_float_val($weight);
  1221. $course_id = api_get_course_int_id();
  1222. AbstractLink::add_link_log($linkId, $name);
  1223. $table_link = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  1224. $em = Database::getManager();
  1225. $tbl_forum_thread = Database::get_course_table(TABLE_FORUM_THREAD);
  1226. $tbl_attendance = Database::get_course_table(TABLE_ATTENDANCE);
  1227. $sql = 'UPDATE '.$table_link.'
  1228. SET weight = '."'".Database::escape_string($weight)."'".'
  1229. WHERE id = '.$linkId;
  1230. Database::query($sql);
  1231. // Update weight for attendance
  1232. $sql = 'SELECT ref_id FROM '.$table_link.'
  1233. WHERE id = '.$linkId.' AND type='.LINK_ATTENDANCE;
  1234. $rs_attendance = Database::query($sql);
  1235. if (Database::num_rows($rs_attendance) > 0) {
  1236. $row_attendance = Database::fetch_array($rs_attendance);
  1237. $sql = 'UPDATE '.$tbl_attendance.' SET
  1238. attendance_weight ='.api_float_val($weight).'
  1239. WHERE c_id = '.$course_id.' AND id = '.intval($row_attendance['ref_id']);
  1240. Database::query($sql);
  1241. }
  1242. // Update weight into forum thread
  1243. $sql = 'UPDATE '.$tbl_forum_thread.' SET
  1244. thread_weight = '.api_float_val($weight).'
  1245. WHERE
  1246. c_id = '.$course_id.' AND
  1247. thread_id = (
  1248. SELECT ref_id FROM '.$table_link.'
  1249. WHERE id='.$linkId.' AND type='.LINK_FORUM_THREAD.'
  1250. )
  1251. ';
  1252. Database::query($sql);
  1253. //Update weight into student publication(work)
  1254. $em
  1255. ->createQuery('
  1256. UPDATE ChamiloCourseBundle:CStudentPublication w
  1257. SET w.weight = :final_weight
  1258. WHERE w.cId = :course
  1259. AND w.id = (
  1260. SELECT l.refId FROM ChamiloCoreBundle:GradebookLink l
  1261. WHERE l.id = :link AND l.type = :type
  1262. )
  1263. ')
  1264. ->execute([
  1265. 'final_weight' => $weight,
  1266. 'course' => $course_id,
  1267. 'link' => $linkId,
  1268. 'type' => LINK_STUDENTPUBLICATION,
  1269. ]);
  1270. }
  1271. /**
  1272. * @param int $id
  1273. * @param float $weight
  1274. */
  1275. public static function updateEvaluationWeight($id, $weight)
  1276. {
  1277. $table_evaluation = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  1278. $id = (int) $id;
  1279. $evaluation = new Evaluation();
  1280. $evaluation->addEvaluationLog($id);
  1281. $sql = 'UPDATE '.$table_evaluation.'
  1282. SET weight = '."'".Database::escape_string($weight)."'".'
  1283. WHERE id = '.$id;
  1284. Database::query($sql);
  1285. }
  1286. /**
  1287. * Get the achieved certificates for a user in courses.
  1288. *
  1289. * @param int $userId The user id
  1290. * @param bool $includeNonPublicCertificates Whether include the non-plublic certificates
  1291. *
  1292. * @return array
  1293. */
  1294. public static function getUserCertificatesInCourses(
  1295. $userId,
  1296. $includeNonPublicCertificates = true
  1297. ) {
  1298. $userId = (int) $userId;
  1299. $courseList = [];
  1300. $courses = CourseManager::get_courses_list_by_user_id($userId);
  1301. foreach ($courses as $course) {
  1302. if (!$includeNonPublicCertificates) {
  1303. $allowPublicCertificates = api_get_course_setting('allow_public_certificates', $course['code']);
  1304. if (empty($allowPublicCertificates)) {
  1305. continue;
  1306. }
  1307. }
  1308. $category = Category::load(null, null, $course['code']);
  1309. if (empty($category)) {
  1310. continue;
  1311. }
  1312. if (!isset($category[0])) {
  1313. continue;
  1314. }
  1315. /** @var Category $category */
  1316. $category = $category[0];
  1317. if (empty($category->getGenerateCertificates())) {
  1318. continue;
  1319. }
  1320. $categoryId = $category->get_id();
  1321. $certificateInfo = self::get_certificate_by_user_id($categoryId, $userId);
  1322. if (empty($certificateInfo)) {
  1323. continue;
  1324. }
  1325. $courseInfo = api_get_course_info_by_id($course['real_id']);
  1326. if (empty($courseInfo)) {
  1327. continue;
  1328. }
  1329. $courseList[] = [
  1330. 'course' => $courseInfo['title'],
  1331. 'score' => $certificateInfo['score_certificate'],
  1332. 'date' => api_format_date($certificateInfo['created_at'], DATE_FORMAT_SHORT),
  1333. 'link' => api_get_path(WEB_PATH)."certificates/index.php?id={$certificateInfo['id']}",
  1334. ];
  1335. }
  1336. return $courseList;
  1337. }
  1338. /**
  1339. * Get the achieved certificates for a user in course sessions.
  1340. *
  1341. * @param int $userId The user id
  1342. * @param bool $includeNonPublicCertificates Whether include the non-public certificates
  1343. *
  1344. * @return array
  1345. */
  1346. public static function getUserCertificatesInSessions($userId, $includeNonPublicCertificates = true)
  1347. {
  1348. $userId = (int) $userId;
  1349. $sessionList = [];
  1350. $sessions = SessionManager::get_sessions_by_user($userId, true, true);
  1351. foreach ($sessions as $session) {
  1352. if (empty($session['courses'])) {
  1353. continue;
  1354. }
  1355. $sessionCourses = SessionManager::get_course_list_by_session_id($session['session_id']);
  1356. if (empty($sessionCourses)) {
  1357. continue;
  1358. }
  1359. foreach ($sessionCourses as $course) {
  1360. if (!$includeNonPublicCertificates) {
  1361. $allowPublicCertificates = api_get_course_setting('allow_public_certificates', $course['code']);
  1362. if (empty($allowPublicCertificates)) {
  1363. continue;
  1364. }
  1365. }
  1366. $category = Category::load(
  1367. null,
  1368. null,
  1369. $course['code'],
  1370. null,
  1371. null,
  1372. $session['session_id']
  1373. );
  1374. if (empty($category)) {
  1375. continue;
  1376. }
  1377. if (!isset($category[0])) {
  1378. continue;
  1379. }
  1380. /** @var Category $category */
  1381. $category = $category[0];
  1382. // Don't allow generate of certifications
  1383. if (empty($category->getGenerateCertificates())) {
  1384. continue;
  1385. }
  1386. $categoryId = $category->get_id();
  1387. $certificateInfo = self::get_certificate_by_user_id(
  1388. $categoryId,
  1389. $userId
  1390. );
  1391. if (empty($certificateInfo)) {
  1392. continue;
  1393. }
  1394. $sessionList[] = [
  1395. 'session' => $session['session_name'],
  1396. 'course' => $course['title'],
  1397. 'score' => $certificateInfo['score_certificate'],
  1398. 'date' => api_format_date($certificateInfo['created_at'], DATE_FORMAT_SHORT),
  1399. 'link' => api_get_path(WEB_PATH)."certificates/index.php?id={$certificateInfo['id']}",
  1400. ];
  1401. }
  1402. }
  1403. return $sessionList;
  1404. }
  1405. /**
  1406. * @param array $courseInfo
  1407. * @param int $userId
  1408. * @param array $cats
  1409. * @param bool $saveToFile
  1410. * @param bool $saveToHtmlFile
  1411. * @param array $studentList
  1412. * @param PDF $pdf
  1413. *
  1414. * @return string
  1415. */
  1416. public static function generateTable(
  1417. $courseInfo,
  1418. $userId,
  1419. $cats,
  1420. $saveToFile = false,
  1421. $saveToHtmlFile = false,
  1422. $studentList = [],
  1423. $pdf = null
  1424. ) {
  1425. $userInfo = api_get_user_info($userId);
  1426. $model = ExerciseLib::getCourseScoreModel();
  1427. $cat = $cats[0];
  1428. $allcat = $cats[0]->get_subcategories(
  1429. $userId,
  1430. api_get_course_id(),
  1431. api_get_session_id()
  1432. );
  1433. $alleval = $cats[0]->get_evaluations($userId);
  1434. $alllink = $cats[0]->get_links($userId);
  1435. $loadStats = [];
  1436. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  1437. $loadStats = [1, 2, 3];
  1438. } else {
  1439. if (api_get_configuration_value('gradebook_enable_best_score') !== false) {
  1440. $loadStats = [2];
  1441. }
  1442. }
  1443. $gradebooktable = new GradebookTable(
  1444. $cat,
  1445. $allcat,
  1446. $alleval,
  1447. $alllink,
  1448. null,
  1449. true,
  1450. false,
  1451. $userId,
  1452. $studentList,
  1453. $loadStats
  1454. );
  1455. $gradebooktable->userId = $userId;
  1456. if (api_is_allowed_to_edit(null, true)) {
  1457. } else {
  1458. if (empty($model)) {
  1459. $gradebooktable->td_attributes = [
  1460. 3 => 'class=centered',
  1461. 4 => 'class=centered',
  1462. 5 => 'class=centered',
  1463. 6 => 'class=centered',
  1464. 7 => 'class=centered',
  1465. ];
  1466. }
  1467. }
  1468. $table = $gradebooktable->return_table();
  1469. $graph = '';
  1470. if (empty($model)) {
  1471. $graph = $gradebooktable->getGraph();
  1472. }
  1473. $params = [
  1474. 'pdf_title' => sprintf(get_lang('GradeFromX'), $courseInfo['name']),
  1475. 'session_info' => '',
  1476. 'course_info' => '',
  1477. 'pdf_date' => '',
  1478. 'course_code' => api_get_course_id(),
  1479. 'student_info' => $userInfo,
  1480. 'show_grade_generated_date' => true,
  1481. 'show_real_course_teachers' => false,
  1482. 'show_teacher_as_myself' => false,
  1483. 'orientation' => 'P',
  1484. ];
  1485. if (empty($pdf)) {
  1486. $pdf = new PDF('A4', $params['orientation'], $params);
  1487. }
  1488. $pdf->params['student_info'] = $userInfo;
  1489. $file = api_get_path(SYS_ARCHIVE_PATH).uniqid().'.html';
  1490. $content =
  1491. $table.
  1492. $graph.
  1493. '<br />'.get_lang('Feedback').'<br />
  1494. <textarea class="form-control" rows="5" cols="100">&nbsp;</textarea>';
  1495. $result = $pdf->html_to_pdf_with_template(
  1496. $content,
  1497. $saveToFile,
  1498. $saveToHtmlFile,
  1499. true
  1500. );
  1501. if ($saveToHtmlFile) {
  1502. return $result;
  1503. file_put_contents($file, $result);
  1504. return $file;
  1505. }
  1506. return $file;
  1507. }
  1508. }