GradebookUtils.php 51 KB

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