GradebookUtils.php 56 KB

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