GradebookUtils.php 54 KB

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