GradebookUtils.php 53 KB

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