GradebookUtils.php 57 KB

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