GradebookUtils.php 57 KB

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