GradebookUtils.php 57 KB

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