GradebookUtils.php 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class GradebookUtils.
  5. */
  6. class GradebookUtils
  7. {
  8. /**
  9. * Adds a resource to the unique gradebook of a given course.
  10. *
  11. * @param int
  12. * @param string Course code
  13. * @param int Resource type (use constants defined in linkfactory.class.php)
  14. * @param int Resource ID in the corresponding tool
  15. * @param string Resource name to show in the gradebook
  16. * @param int Resource weight to set in the gradebook
  17. * @param int Resource max
  18. * @param string Resource description
  19. * @param int Visibility (0 hidden, 1 shown)
  20. * @param int Session ID (optional or 0 if not defined)
  21. * @param int
  22. * @param int $resource_type
  23. *
  24. * @return bool True on success, false on failure
  25. */
  26. public static function add_resource_to_course_gradebook(
  27. $category_id,
  28. $course_code,
  29. $resource_type,
  30. $resource_id,
  31. $resource_name = '',
  32. $weight = 0,
  33. $max = 0,
  34. $resource_description = '',
  35. $visible = 0,
  36. $session_id = 0,
  37. $link_id = null
  38. ) {
  39. $link = LinkFactory::create($resource_type);
  40. $link->set_user_id(api_get_user_id());
  41. $link->set_course_code($course_code);
  42. if (empty($category_id)) {
  43. return false;
  44. }
  45. $link->set_category_id($category_id);
  46. if ($link->needs_name_and_description()) {
  47. $link->set_name($resource_name);
  48. } else {
  49. $link->set_ref_id($resource_id);
  50. }
  51. $link->set_weight($weight);
  52. if ($link->needs_max()) {
  53. $link->set_max($max);
  54. }
  55. if ($link->needs_name_and_description()) {
  56. $link->set_description($resource_description);
  57. }
  58. $link->set_visible(empty($visible) ? 0 : 1);
  59. if (!empty($session_id)) {
  60. $link->set_session_id($session_id);
  61. }
  62. $link->add();
  63. return true;
  64. }
  65. /**
  66. * Update a resource weight.
  67. *
  68. * @param int Link/Resource ID
  69. * @param string
  70. * @param float
  71. *
  72. * @return bool false on error, true on success
  73. */
  74. public static function updateResourceFromCourseGradebook(
  75. $link_id,
  76. $course_code,
  77. $weight
  78. ) {
  79. $link_id = (int) $link_id;
  80. if (!empty($link_id)) {
  81. $course_code = Database::escape_string($course_code);
  82. $sql = 'UPDATE '.Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK).'
  83. SET weight = '."'".api_float_val($weight)."'".'
  84. WHERE course_code = "'.$course_code.'" AND id = '.$link_id;
  85. Database::query($sql);
  86. }
  87. return true;
  88. }
  89. /**
  90. * Remove a resource from the unique gradebook of a given course.
  91. *
  92. * @param int Link/Resource ID
  93. *
  94. * @return bool false on error, true on success
  95. */
  96. public static function remove_resource_from_course_gradebook($link_id)
  97. {
  98. if (empty($link_id)) {
  99. return false;
  100. }
  101. // TODO find the corresponding category (the first one for this course, ordered by ID)
  102. $l = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  103. $sql = "DELETE FROM $l WHERE id = ".(int) $link_id;
  104. Database::query($sql);
  105. return true;
  106. }
  107. /**
  108. * Block students.
  109. */
  110. public static function block_students()
  111. {
  112. 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. $id_link = (int) $id_link;
  540. $sql = 'SELECT c.id FROM '.$course_table.' c
  541. INNER JOIN '.$tbl_grade_links.' l
  542. ON c.code = l.course_code
  543. WHERE l.id='.$id_link.' OR l.category_id='.$id_link;
  544. $res = Database::query($sql);
  545. $array = Database::fetch_array($res, 'ASSOC');
  546. return $array['id'];
  547. }
  548. /**
  549. * @param $type
  550. *
  551. * @return string
  552. */
  553. public static function get_table_type_course($type)
  554. {
  555. global $table_evaluated;
  556. return Database::get_course_table($table_evaluated[$type][0]);
  557. }
  558. /**
  559. * @param Category $cat
  560. * @param $users
  561. * @param $alleval
  562. * @param $alllinks
  563. * @param $params
  564. * @param null $mainCourseCategory
  565. *
  566. * @return array
  567. */
  568. public static function get_printable_data(
  569. $cat,
  570. $users,
  571. $alleval,
  572. $alllinks,
  573. $params,
  574. $mainCourseCategory = null
  575. ) {
  576. $datagen = new FlatViewDataGenerator(
  577. $users,
  578. $alleval,
  579. $alllinks,
  580. $params,
  581. $mainCourseCategory
  582. );
  583. $offset = isset($_GET['offset']) ? (int) $_GET['offset'] : 0;
  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 = ".$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 = (int) $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 = (int) $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 = (int) $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 = (int) $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. }