GradebookUtils.php 54 KB

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