GradebookUtils.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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. if ($cat->is_locked() && !api_is_platform_admin()) {
  252. $modify_icons .= Display::return_icon('delete_na.png', get_lang('DeleteAll'), '', ICON_SIZE_SMALL);
  253. } else {
  254. $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();">' .
  255. Display::return_icon('delete.png', get_lang('DeleteAll'), '', ICON_SIZE_SMALL) . '</a>';
  256. }
  257. }
  258. return $modify_icons;
  259. }
  260. }
  261. /**
  262. * Builds the course or platform admin icons to edit an evaluation
  263. * @param Evaluation $eval evaluation object
  264. * @param int $selectcat id of selected category
  265. */
  266. public static function build_edit_icons_eval($eval, $selectcat)
  267. {
  268. $status = CourseManager::get_user_in_course_status(api_get_user_id(), api_get_course_id());
  269. $is_locked = $eval->is_locked();
  270. $eval->get_course_code();
  271. $cat = new Category();
  272. $message_eval = $cat->show_message_resource_delete($eval->get_course_code());
  273. if ($message_eval === false && api_is_allowed_to_edit(null, true)) {
  274. $visibility_icon = ($eval->is_visible() == 0) ? 'invisible' : 'visible';
  275. $visibility_command = ($eval->is_visible() == 0) ? 'set_visible' : 'set_invisible';
  276. if ($is_locked && !api_is_platform_admin()) {
  277. $modify_icons = Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
  278. } else {
  279. $modify_icons = '<a href="gradebook_edit_eval.php?editeval=' . $eval->get_id() . '&cidReq=' . $eval->get_course_code() . '&id_session='.$eval->getSessionId(). '">' .
  280. Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL) . '</a>';
  281. }
  282. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?visibleeval=' . $eval->get_id() . '&' . $visibility_command . '=&selectcat=' . $selectcat . '&id_session='.$eval->getSessionId(). ' ">' .
  283. Display::return_icon($visibility_icon . '.png', get_lang('Visible'), '', ICON_SIZE_SMALL) . '</a>';
  284. if (api_is_allowed_to_edit(null, true)) {
  285. $modify_icons .= '&nbsp;<a href="gradebook_showlog_eval.php?visiblelog=' . $eval->get_id() . '&selectcat=' . $selectcat . ' &cidReq=' . $eval->get_course_code() . '&id_session='.$eval->getSessionId(). '">' .
  286. Display::return_icon('history.png', get_lang('GradebookQualifyLog'), '', ICON_SIZE_SMALL) . '</a>';
  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 . ' &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() . '&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() . '?visiblelink=' . $link->get_id() . '&' . $visibility_command . '=&selectcat=' . $selectcat . '&id_session='.$link->get_session_id(). ' ">' .
  320. Display::return_icon($visibility_icon . '.png', get_lang('Visible'), '', ICON_SIZE_SMALL) . '</a>';
  321. $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(). '">' .
  322. Display::return_icon('history.png', get_lang('GradebookQualifyLog'), '', ICON_SIZE_SMALL) . '</a>';
  323. //If a work is added in a gradebook you can only delete the link in the work tool
  324. if ($is_locked && !api_is_platform_admin()) {
  325. $modify_icons .= '&nbsp;' . Display::return_icon('delete_na.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
  326. } else {
  327. $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();">' .
  328. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL) . '</a>';
  329. }
  330. return $modify_icons;
  331. }
  332. }
  333. /**
  334. * Checks if a resource is in the unique gradebook of a given course
  335. * @param string $course_code Course code
  336. * @param int $resource_type Resource type (use constants defined in linkfactory.class.php)
  337. * @param int $resource_id Resource ID in the corresponding tool
  338. * @param int $session_id Session ID (optional - 0 if not defined)
  339. *
  340. * @return array false on error or array of resource
  341. */
  342. public static function is_resource_in_course_gradebook($course_code, $resource_type, $resource_id, $session_id = 0)
  343. {
  344. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  345. $course_code = Database::escape_string($course_code);
  346. $sql = "SELECT * FROM $table 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 = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  526. $sql = 'SELECT COUNT(*) as count
  527. FROM ' . $table . ' gc
  528. WHERE gc.cat_id="' . intval($cat_id) . '" AND user_id="' . intval($user_id) . '" ';
  529. $rs_exist = Database::query($sql);
  530. $row = Database::fetch_array($rs_exist);
  531. if ($row['count'] == 0) {
  532. $params = [
  533. 'cat_id' => $cat_id,
  534. 'user_id' => $user_id,
  535. 'score_certificate' => $score_certificate,
  536. 'created_at' => $date_certificate
  537. ];
  538. Database::insert($table, $params);
  539. }
  540. }
  541. /**
  542. * Get date of user certificate
  543. * @param int $cat_id The category id
  544. * @param int $user_id The user id
  545. * @return Datetime The date when you obtained the certificate
  546. */
  547. public static function get_certificate_by_user_id($cat_id, $user_id)
  548. {
  549. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  550. $sql = 'SELECT * FROM ' . $table_certificate . '
  551. WHERE cat_id="' . intval($cat_id) . '" AND user_id="' . intval($user_id) . '"';
  552. $result = Database::query($sql);
  553. $row = Database::fetch_array($result, 'ASSOC');
  554. return $row;
  555. }
  556. /**
  557. * Get list of users certificates
  558. * @param int $cat_id The category id
  559. * @param array $userList Only users in this list
  560. * @return array
  561. */
  562. public static function get_list_users_certificates($cat_id = null, $userList = array())
  563. {
  564. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  565. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  566. $sql = 'SELECT DISTINCT u.user_id, u.lastname, u.firstname, u.username
  567. FROM ' . $table_user . ' u
  568. INNER JOIN ' . $table_certificate . ' gc
  569. ON u.user_id=gc.user_id ';
  570. if (!is_null($cat_id) && $cat_id > 0) {
  571. $sql.=' WHERE cat_id=' . intval($cat_id);
  572. }
  573. if (!empty($userList)) {
  574. $userList = array_map('intval', $userList);
  575. $userListCondition = implode("','", $userList);
  576. $sql .= " AND u.user_id IN ('$userListCondition')";
  577. }
  578. $sql.=' ORDER BY u.firstname';
  579. $rs = Database::query($sql);
  580. $list_users = array();
  581. while ($row = Database::fetch_array($rs)) {
  582. $list_users[] = $row;
  583. }
  584. return $list_users;
  585. }
  586. /**
  587. * Gets the certificate list by user id
  588. * @param int $user_id The user id
  589. * @param int $cat_id The category id
  590. * @return array
  591. */
  592. public static function get_list_gradebook_certificates_by_user_id($user_id, $cat_id = null)
  593. {
  594. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  595. $sql = 'SELECT gc.score_certificate, gc.created_at, gc.path_certificate, gc.cat_id, gc.user_id, gc.id
  596. FROM ' . $table_certificate . ' gc
  597. WHERE gc.user_id="' . intval($user_id) . '" ';
  598. if (!is_null($cat_id) && $cat_id > 0) {
  599. $sql.=' AND cat_id=' . intval($cat_id);
  600. }
  601. $rs = Database::query($sql);
  602. $list_certificate = array();
  603. while ($row = Database::fetch_array($rs)) {
  604. $list_certificate[] = $row;
  605. }
  606. return $list_certificate;
  607. }
  608. /**
  609. * @param int $user_id
  610. * @param string $course_code
  611. * @param int $sessionId
  612. * @param bool $is_preview
  613. * @param bool $hide_print_button
  614. *
  615. * @return array
  616. */
  617. public static function get_user_certificate_content($user_id, $course_code, $sessionId, $is_preview = false, $hide_print_button = false)
  618. {
  619. // Generate document HTML
  620. $content_html = DocumentManager::replace_user_info_into_html($user_id, $course_code, $sessionId, $is_preview);
  621. $new_content_html = null;
  622. $variables = null;
  623. $contentHead = null;
  624. if (isset($content_html['content'])) {
  625. $new_content = explode('</head>', $content_html['content']);
  626. $new_content_html = $new_content[1];
  627. $contentHead = $new_content[0];
  628. }
  629. if (isset($content_html['variables'])) {
  630. $variables = $content_html['variables'];
  631. }
  632. $path_image = api_get_path(WEB_COURSE_PATH) . api_get_course_path($course_code) . '/document/images/gallery';
  633. $new_content_html = str_replace('../images/gallery', $path_image, $new_content_html);
  634. $path_image_in_default_course = api_get_path(WEB_CODE_PATH) . 'default_course_document';
  635. $new_content_html = str_replace('/main/default_course_document', $path_image_in_default_course, $new_content_html);
  636. $new_content_html = str_replace(SYS_CODE_PATH . 'img/', api_get_path(WEB_IMG_PATH), $new_content_html);
  637. $print = '';
  638. //add print header
  639. if ($hide_print_button == false) {
  640. $print .= '<style media="print" type="text/css">#print_div {visibility:hidden;}</style>';
  641. $print .= '<a href="javascript:window.print();" style="float:right; padding:4px;" id="print_div">';
  642. $print .= Display::return_icon('printmgr.gif', get_lang('Print'));
  643. $print .= '</a>';
  644. }
  645. // Add header
  646. $new_content_html = $contentHead. $print . '</head>' . $new_content_html;
  647. return array(
  648. 'content' => $new_content_html,
  649. 'variables' => $variables
  650. );
  651. }
  652. /**
  653. * @param null $course_code
  654. * @param int $gradebook_model_id
  655. * @return mixed
  656. */
  657. public static function create_default_course_gradebook($course_code = null, $gradebook_model_id = 0)
  658. {
  659. if (api_is_allowed_to_edit(true, true)) {
  660. if (!isset($course_code) || empty($course_code)) {
  661. $course_code = api_get_course_id();
  662. }
  663. $session_id = api_get_session_id();
  664. $t = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  665. $sql = "SELECT * FROM $t WHERE course_code = '" . Database::escape_string($course_code) . "' ";
  666. if (!empty($session_id)) {
  667. $sql .= " AND session_id = " . (int) $session_id;
  668. } else {
  669. $sql .= " AND (session_id IS NULL OR session_id = 0) ";
  670. }
  671. $sql .= " ORDER BY id";
  672. $res = Database::query($sql);
  673. if (Database::num_rows($res) < 1) {
  674. //there is no unique category for this course+session combination,
  675. $cat = new Category();
  676. if (!empty($session_id)) {
  677. $my_session_id = api_get_session_id();
  678. $s_name = api_get_session_name($my_session_id);
  679. $cat->set_name($course_code . ' - ' . get_lang('Session') . ' ' . $s_name);
  680. $cat->set_session_id($session_id);
  681. } else {
  682. $cat->set_name($course_code);
  683. }
  684. $cat->set_course_code($course_code);
  685. $cat->set_description(null);
  686. $cat->set_user_id(api_get_user_id());
  687. $cat->set_parent_id(0);
  688. $default_weight_setting = api_get_setting('gradebook_default_weight');
  689. $default_weight = isset($default_weight_setting) && !empty($default_weight_setting) ? $default_weight_setting : 100;
  690. $cat->set_weight($default_weight);
  691. $cat->set_grade_model_id($gradebook_model_id);
  692. $cat->set_certificate_min_score(75);
  693. $cat->set_visible(0);
  694. $cat->add();
  695. $category_id = $cat->get_id();
  696. unset($cat);
  697. } else {
  698. $row = Database::fetch_array($res);
  699. $category_id = $row['id'];
  700. }
  701. return $category_id;
  702. }
  703. return false;
  704. }
  705. /**
  706. * @param FormValidator $form
  707. */
  708. public static function load_gradebook_select_in_tool($form)
  709. {
  710. $course_code = api_get_course_id();
  711. $session_id = api_get_session_id();
  712. self::create_default_course_gradebook();
  713. // Cat list
  714. $all_categories = Category :: load(null, null, $course_code, null, null, $session_id, false);
  715. $select_gradebook = $form->addElement('select', 'category_id', get_lang('SelectGradebook'));
  716. if (!empty($all_categories)) {
  717. foreach ($all_categories as $my_cat) {
  718. if ($my_cat->get_course_code() == api_get_course_id()) {
  719. $grade_model_id = $my_cat->get_grade_model_id();
  720. if (empty($grade_model_id)) {
  721. if ($my_cat->get_parent_id() == 0) {
  722. //$default_weight = $my_cat->get_weight();
  723. $select_gradebook->addoption(get_lang('Default'), $my_cat->get_id());
  724. $cats_added[] = $my_cat->get_id();
  725. } else {
  726. $select_gradebook->addoption($my_cat->get_name(), $my_cat->get_id());
  727. $cats_added[] = $my_cat->get_id();
  728. }
  729. } else {
  730. $select_gradebook->addoption(get_lang('Select'), 0);
  731. }
  732. }
  733. }
  734. }
  735. }
  736. /**
  737. * @param FlatViewTable $flatviewtable
  738. * @param Category $cat
  739. * @param $users
  740. * @param $alleval
  741. * @param $alllinks
  742. * @param array $params
  743. * @param null $mainCourseCategory
  744. */
  745. public static function export_pdf_flatview(
  746. $flatviewtable,
  747. $cat,
  748. $users,
  749. $alleval,
  750. $alllinks,
  751. $params = array(),
  752. $mainCourseCategory = null
  753. ) {
  754. // Getting data
  755. $printable_data = self::get_printable_data($cat[0], $users, $alleval, $alllinks, $params, $mainCourseCategory);
  756. // HTML report creation first
  757. $course_code = trim($cat[0]->get_course_code());
  758. $displayscore = ScoreDisplay :: instance();
  759. $customdisplays = $displayscore->get_custom_score_display_settings();
  760. $total = array();
  761. if (is_array($customdisplays) && count(($customdisplays))) {
  762. foreach ($customdisplays as $custom) {
  763. $total[$custom['display']] = 0;
  764. }
  765. $user_results = $flatviewtable->datagen->get_data_to_graph2(false);
  766. foreach ($user_results as $user_result) {
  767. $total[$user_result[count($user_result) - 1][1]]++;
  768. }
  769. }
  770. $parent_id = $cat[0]->get_parent_id();
  771. if (isset($cat[0]) && isset($parent_id)) {
  772. if ($parent_id == 0) {
  773. $grade_model_id = $cat[0]->get_grade_model_id();
  774. } else {
  775. $parent_cat = Category::load($parent_id);
  776. $grade_model_id = $parent_cat[0]->get_grade_model_id();
  777. }
  778. }
  779. $use_grade_model = true;
  780. if (empty($grade_model_id) || $grade_model_id == -1) {
  781. $use_grade_model = false;
  782. }
  783. if ($use_grade_model) {
  784. if ($parent_id == 0) {
  785. $title = api_strtoupper(get_lang('Average')) . '<br />' . get_lang('Detailed');
  786. } else {
  787. $title = api_strtoupper(get_lang('Average')) . '<br />' . $cat[0]->get_description() . ' - (' . $cat[0]->get_name() . ')';
  788. }
  789. } else {
  790. if ($parent_id == 0) {
  791. $title = api_strtoupper(get_lang('Average')) . '<br />' . get_lang('Detailed');
  792. } else {
  793. $title = api_strtoupper(get_lang('Average'));
  794. }
  795. }
  796. $columns = count($printable_data[0]);
  797. $has_data = is_array($printable_data[1]) && count($printable_data[1]) > 0;
  798. $table = new HTML_Table(array('class' => 'data_table'));
  799. $row = 0;
  800. $column = 0;
  801. $table->setHeaderContents($row, $column, get_lang('NumberAbbreviation'));
  802. $column++;
  803. foreach ($printable_data[0] as $printable_data_cell) {
  804. if (!is_array($printable_data_cell)) {
  805. $printable_data_cell = strip_tags($printable_data_cell);
  806. }
  807. $table->setHeaderContents($row, $column, $printable_data_cell);
  808. $column++;
  809. }
  810. $row++;
  811. if ($has_data) {
  812. $counter = 1;
  813. foreach ($printable_data[1] as &$printable_data_row) {
  814. $column = 0;
  815. $table->setCellContents($row, $column, $counter);
  816. $table->updateCellAttributes($row, $column, 'align="center"');
  817. $column++;
  818. $counter++;
  819. foreach ($printable_data_row as $key => &$printable_data_cell) {
  820. $attributes = array();
  821. $attributes['align'] = 'center';
  822. $attributes['style'] = null;
  823. if ($key === 'name') {
  824. $attributes['align'] = 'left';
  825. }
  826. if ($key === 'total') {
  827. $attributes['style'] = 'font-weight:bold';
  828. }
  829. $table->setCellContents($row, $column, $printable_data_cell);
  830. $table->updateCellAttributes($row, $column, $attributes);
  831. $column++;
  832. }
  833. $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
  834. $row++;
  835. }
  836. } else {
  837. $column = 0;
  838. $table->setCellContents($row, $column, get_lang('NoResults'));
  839. $table->updateCellAttributes($row, $column, 'colspan="' . $columns . '" align="center" class="row_odd"');
  840. }
  841. $pdfParams = array(
  842. 'filename' => get_lang('FlatView') . '_' . api_get_utc_datetime(),
  843. 'pdf_title' => $title,
  844. 'course_code' => $course_code,
  845. 'add_signatures' => true
  846. );
  847. $page_format = $params['orientation'] == 'landscape' ? 'A4-L' : 'A4';
  848. ob_start();
  849. $pdf = new PDF($page_format, $page_format, $pdfParams);
  850. $pdf->html_to_pdf_with_template($flatviewtable->return_table());
  851. $content = ob_get_contents();
  852. ob_end_clean();
  853. echo $content;
  854. exit;
  855. }
  856. /**
  857. * @param array $list_values
  858. * @return string
  859. */
  860. public static function score_badges($list_values)
  861. {
  862. $counter = 1;
  863. $badges = array();
  864. foreach ($list_values as $value) {
  865. $class = 'warning';
  866. if ($counter == 1) {
  867. $class = 'success';
  868. }
  869. $counter++;
  870. $badges[] = Display::badge($value, $class);
  871. }
  872. return Display::badge_group($badges);
  873. }
  874. /**
  875. * returns users within a course given by param
  876. * @param string $courseCode
  877. */
  878. public static function get_users_in_course($courseCode)
  879. {
  880. $tbl_course_user = Database:: get_main_table(TABLE_MAIN_COURSE_USER);
  881. $tbl_session_course_user = Database:: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  882. $tbl_user = Database:: get_main_table(TABLE_MAIN_USER);
  883. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname ASC' : ' ORDER BY lastname, firstname ASC';
  884. $current_session = api_get_session_id();
  885. $courseCode = Database::escape_string($courseCode);
  886. $courseInfo = api_get_course_info($courseCode);
  887. $courseId = $courseInfo['real_id'];
  888. if (!empty($current_session)) {
  889. $sql = "SELECT user.user_id, user.username, lastname, firstname, official_code
  890. FROM $tbl_session_course_user as scru, $tbl_user as user
  891. WHERE
  892. scru.user_id = user.user_id AND
  893. scru.status=0 AND
  894. scru.c_id='$courseId' AND
  895. session_id ='$current_session'
  896. $order_clause
  897. ";
  898. } else {
  899. $sql = 'SELECT user.user_id, user.username, lastname, firstname, official_code
  900. FROM '.$tbl_course_user.' as course_rel_user, '.$tbl_user.' as user
  901. WHERE
  902. course_rel_user.user_id=user.user_id AND
  903. course_rel_user.status='.STUDENT.' AND
  904. course_rel_user.c_id = "'.$courseId.'" '.
  905. $order_clause;
  906. }
  907. $result = Database::query($sql);
  908. return self::get_user_array_from_sql_result($result);
  909. }
  910. /**
  911. * @param $result
  912. * @return array
  913. */
  914. public static function get_user_array_from_sql_result($result)
  915. {
  916. $a_students = array();
  917. while ($user = Database::fetch_array($result)) {
  918. if (!array_key_exists($user['user_id'], $a_students)) {
  919. $a_current_student = array ();
  920. $a_current_student[] = $user['user_id'];
  921. $a_current_student[] = $user['username'];
  922. $a_current_student[] = $user['lastname'];
  923. $a_current_student[] = $user['firstname'];
  924. $a_current_student[] = $user['official_code'];
  925. $a_students['STUD'.$user['user_id']] = $a_current_student;
  926. }
  927. }
  928. return $a_students;
  929. }
  930. /**
  931. * @param array $evals
  932. * @param array $links
  933. * @return array
  934. */
  935. public static function get_all_users($evals = array(), $links = array())
  936. {
  937. $coursecodes = array();
  938. $users = array();
  939. // By default add all user in course
  940. $coursecodes[api_get_course_id()] = '1';
  941. $users = GradebookUtils::get_users_in_course(api_get_course_id());
  942. foreach ($evals as $eval) {
  943. $coursecode = $eval->get_course_code();
  944. // evaluation in course
  945. if (isset($coursecode) && !empty($coursecode)) {
  946. if (!array_key_exists($coursecode, $coursecodes)) {
  947. $coursecodes[$coursecode] = '1';
  948. $users = array_merge($users, GradebookUtils::get_users_in_course($coursecode));
  949. }
  950. } else {
  951. // course independent evaluation
  952. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  953. $tbl_res = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  954. $sql = 'SELECT user.user_id, lastname, firstname, user.official_code
  955. FROM '.$tbl_res.' as res, '.$tbl_user.' as user
  956. WHERE
  957. res.evaluation_id = '.intval($eval->get_id()).' AND
  958. res.user_id = user.user_id
  959. ';
  960. $sql .= ' ORDER BY lastname, firstname';
  961. if (api_is_western_name_order()) {
  962. $sql .= ' ORDER BY firstname, lastname';
  963. }
  964. $result = Database::query($sql);
  965. $users = array_merge($users, GradebookUtils::get_user_array_from_sql_result($result));
  966. }
  967. }
  968. foreach ($links as $link) {
  969. // links are always in a course
  970. $coursecode = $link->get_course_code();
  971. if (!array_key_exists($coursecode,$coursecodes)) {
  972. $coursecodes[$coursecode] = '1';
  973. $users = array_merge($users, GradebookUtils::get_users_in_course($coursecode));
  974. }
  975. }
  976. return $users;
  977. }
  978. /**
  979. * Search students matching a given last name and/or first name
  980. * @author Bert Steppé
  981. */
  982. public static function find_students($mask= '')
  983. {
  984. // students shouldn't be here // don't search if mask empty
  985. if (!api_is_allowed_to_edit() || empty ($mask)) {
  986. return null;
  987. }
  988. $mask = Database::escape_string($mask);
  989. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  990. $tbl_cru = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  991. $sql = 'SELECT DISTINCT user.user_id, user.lastname, user.firstname, user.email, user.official_code
  992. FROM ' . $tbl_user . ' user';
  993. if (!api_is_platform_admin()) {
  994. $sql .= ', ' . $tbl_cru . ' cru';
  995. }
  996. $sql .= ' WHERE user.status = ' . STUDENT;
  997. $sql .= ' AND (user.lastname LIKE '."'%" . $mask . "%'";
  998. $sql .= ' OR user.firstname LIKE '."'%" . $mask . "%')";
  999. if (!api_is_platform_admin()) {
  1000. $sql .= ' AND user.user_id = cru.user_id AND
  1001. cru.relation_type <> '.COURSE_RELATION_TYPE_RRHH.' AND
  1002. cru.c_id in (
  1003. SELECT c_id FROM '.$tbl_cru . '
  1004. WHERE
  1005. user_id = ' . api_get_user_id() . ' AND
  1006. status = ' . COURSEMANAGER . '
  1007. )
  1008. ';
  1009. }
  1010. $sql .= ' ORDER BY lastname, firstname';
  1011. if (api_is_western_name_order()) {
  1012. $sql .= ' ORDER BY firstname, lastname';
  1013. }
  1014. $result = Database::query($sql);
  1015. $users = Database::store_result($result);
  1016. return $users;
  1017. }
  1018. /**
  1019. * @param int $linkId
  1020. * @param float $weight
  1021. */
  1022. public static function updateLinkWeight($linkId, $name, $weight)
  1023. {
  1024. $linkId = intval($linkId);
  1025. $weight = floatval($weight);
  1026. $course_id = api_get_course_int_id();
  1027. AbstractLink::add_link_log($linkId, $name);
  1028. $table_link = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  1029. $table_evaluation = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  1030. $tbl_forum_thread = Database:: get_course_table(TABLE_FORUM_THREAD);
  1031. $tbl_work = Database:: get_course_table(TABLE_STUDENT_PUBLICATION);
  1032. $tbl_attendance = Database:: get_course_table(TABLE_ATTENDANCE);
  1033. $sql = 'UPDATE '.$table_link.' SET weight = '."'".Database::escape_string($weight)."'".'
  1034. WHERE id = '.$linkId;
  1035. Database::query($sql);
  1036. // Update weight for attendance
  1037. $sql = 'SELECT ref_id FROM '.$table_link.'
  1038. WHERE id = '.$linkId.' AND type='.LINK_ATTENDANCE;
  1039. $rs_attendance = Database::query($sql);
  1040. if (Database::num_rows($rs_attendance) > 0) {
  1041. $row_attendance = Database::fetch_array($rs_attendance);
  1042. $sql = 'UPDATE '.$tbl_attendance.' SET attendance_weight ='.$weight.'
  1043. WHERE c_id = '.$course_id.' AND id = '.intval($row_attendance['ref_id']);
  1044. Database::query($sql);
  1045. }
  1046. // Update weight into forum thread
  1047. $sql = 'UPDATE '.$tbl_forum_thread.' SET thread_weight='.$weight.'
  1048. WHERE
  1049. c_id = '.$course_id.' AND
  1050. thread_id = (
  1051. SELECT ref_id FROM '.$table_link.'
  1052. WHERE id='.$linkId.' AND type='.LINK_FORUM_THREAD.'
  1053. )
  1054. ';
  1055. Database::query($sql);
  1056. //Update weight into student publication(work)
  1057. $sql = 'UPDATE '.$tbl_work.' SET weight='.$weight.'
  1058. WHERE
  1059. c_id = '.$course_id.' AND id = (
  1060. SELECT ref_id FROM '.$table_link.'
  1061. WHERE id='.$linkId.' AND type = '.LINK_STUDENTPUBLICATION.'
  1062. ) ';
  1063. Database::query($sql);
  1064. }
  1065. /**
  1066. * @param int $id
  1067. * @param float $weight
  1068. */
  1069. public static function updateEvaluationWeight($id, $weight)
  1070. {
  1071. $table_evaluation = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  1072. $id = intval($id);
  1073. $evaluation = new Evaluation();
  1074. $evaluation->add_evaluation_log($id);
  1075. $sql = 'UPDATE '.$table_evaluation.'
  1076. SET weight = '."'".Database::escape_string($weight)."'".'
  1077. WHERE id = '.$id;
  1078. Database::query($sql);
  1079. }
  1080. /**
  1081. *
  1082. * Get the achieved certificates for a user in courses
  1083. * @param int $userId The user id
  1084. * @param bool $includeNonPublicCertificates Whether include the non-plublic certificates
  1085. * @return array
  1086. */
  1087. public static function getUserCertificatesInCourses($userId, $includeNonPublicCertificates = true)
  1088. {
  1089. $userId = intval($userId);
  1090. $courseList = [];
  1091. $courses = CourseManager::get_courses_list_by_user_id($userId);
  1092. foreach ($courses as $course) {
  1093. if (!$includeNonPublicCertificates) {
  1094. $allowPublicCertificates = api_get_course_setting('allow_public_certificates', $course['code']);
  1095. if (empty($allowPublicCertificates)) {
  1096. continue;
  1097. }
  1098. }
  1099. $courseGradebookCategory = Category::load(null, null, $course['code']);
  1100. if (empty($courseGradebookCategory)) {
  1101. continue;
  1102. }
  1103. $courseGradebookId = $courseGradebookCategory[0]->get_id();
  1104. $certificateInfo = GradebookUtils::get_certificate_by_user_id($courseGradebookId, $userId);
  1105. if (empty($certificateInfo)) {
  1106. continue;
  1107. }
  1108. $courseInfo = api_get_course_info_by_id($course['real_id']);
  1109. $courseList[] = [
  1110. 'course' => $courseInfo['title'],
  1111. 'score' => $certificateInfo['score_certificate'],
  1112. 'date' => api_format_date($certificateInfo['created_at'], DATE_FORMAT_SHORT),
  1113. 'link' => api_get_path(WEB_PATH) . "certificates/index.php?id={$certificateInfo['id']}"
  1114. ];
  1115. }
  1116. return $courseList;
  1117. }
  1118. /**
  1119. * Get the achieved certificates for a user in course sessions
  1120. * @param int $userId The user id
  1121. * @param bool $includeNonPublicCertificates Whether include the non-plublic certificates
  1122. * @return array
  1123. */
  1124. public static function getUserCertificatesInSessions($userId, $includeNonPublicCertificates = true)
  1125. {
  1126. $userId = intval($userId);
  1127. $sessionList = [];
  1128. $sessions = SessionManager::get_sessions_by_user($userId, true, true);
  1129. foreach ($sessions as $session) {
  1130. if (empty($session['courses'])) {
  1131. continue;
  1132. }
  1133. $sessionCourses = SessionManager::get_course_list_by_session_id($session['session_id']);
  1134. foreach ($sessionCourses as $course) {
  1135. if (!$includeNonPublicCertificates) {
  1136. $allowPublicCertificates = api_get_course_setting('allow_public_certificates', $course['code']);
  1137. if (empty($allowPublicCertificates)) {
  1138. continue;
  1139. }
  1140. }
  1141. $courseGradebookCategory = Category::load(
  1142. null,
  1143. null,
  1144. $course['code'],
  1145. null,
  1146. null,
  1147. $session['session_id']
  1148. );
  1149. if (empty($courseGradebookCategory)) {
  1150. continue;
  1151. }
  1152. $courseGradebookId = $courseGradebookCategory[0]->get_id();
  1153. $certificateInfo = GradebookUtils::get_certificate_by_user_id(
  1154. $courseGradebookId,
  1155. $userId
  1156. );
  1157. if (empty($certificateInfo)) {
  1158. continue;
  1159. }
  1160. $sessionList[] = [
  1161. 'session' => $session['session_name'],
  1162. 'course' => $course['title'],
  1163. 'score' => $certificateInfo['score_certificate'],
  1164. 'date' => api_format_date($certificateInfo['created_at'], DATE_FORMAT_SHORT),
  1165. 'link' => api_get_path(WEB_PATH) . "certificates/index.php?id={$certificateInfo['id']}"
  1166. ];
  1167. }
  1168. }
  1169. return $sessionList;
  1170. }
  1171. /**
  1172. * @param int $userId
  1173. * @param array $cats
  1174. * @param bool $saveToFile
  1175. * @param bool $saveToHtmlFile
  1176. * @param array $studentList
  1177. * @param PDF $pdf
  1178. *
  1179. * @return string
  1180. */
  1181. public static function generateTable(
  1182. $userId,
  1183. $cats,
  1184. $saveToFile = false,
  1185. $saveToHtmlFile = false,
  1186. $studentList = array(),
  1187. $pdf = null
  1188. ) {
  1189. $courseInfo = api_get_course_info();
  1190. $userInfo = api_get_user_info($userId);
  1191. $cat = $cats[0];
  1192. $allcat = $cats[0]->get_subcategories(
  1193. $userId,
  1194. api_get_course_id(),
  1195. api_get_session_id()
  1196. );
  1197. $alleval = $cats[0]->get_evaluations($userId);
  1198. $alllink = $cats[0]->get_links($userId);
  1199. $gradebooktable = new GradebookTable(
  1200. $cat,
  1201. $allcat,
  1202. $alleval,
  1203. $alllink,
  1204. null, // params
  1205. true, // $exportToPdf
  1206. false, // showteacher
  1207. $userId,
  1208. $studentList
  1209. );
  1210. $gradebooktable->userId = $userId;
  1211. if (api_is_allowed_to_edit()) {
  1212. $gradebooktable->td_attributes = [
  1213. 4 => 'class=centered'
  1214. ];
  1215. } else {
  1216. $gradebooktable->td_attributes = [
  1217. 3 => 'class=centered',
  1218. 4 => 'class=centered',
  1219. 5 => 'class=centered',
  1220. 6 => 'class=centered',
  1221. 7 => 'class=centered'
  1222. ];
  1223. }
  1224. $table = $gradebooktable->return_table();
  1225. $graph = $gradebooktable->getGraph();
  1226. $sessionName = api_get_session_name(api_get_session_id());
  1227. $sessionName = !empty($sessionName) ? " - $sessionName" : '';
  1228. $params = array(
  1229. 'pdf_title' => sprintf(get_lang('GradeFromX'), $courseInfo['department_name']),
  1230. 'session_info' => '',
  1231. 'course_info' => '',
  1232. 'pdf_date' => '',
  1233. 'course_code' => api_get_course_id(),
  1234. 'add_signatures' => false,
  1235. 'student_info' => $userInfo,
  1236. 'show_grade_generated_date' => true,
  1237. 'show_real_course_teachers' => false,
  1238. 'show_teacher_as_myself' => false,
  1239. 'orientation' => 'P'
  1240. );
  1241. if (empty($pdf)) {
  1242. $pdf = new PDF('A4', $params['orientation'], $params);
  1243. }
  1244. $pdf->params['student_info'] = $userInfo;
  1245. $file = api_get_path(SYS_ARCHIVE_PATH).uniqid().'.html';
  1246. $content =
  1247. $table.
  1248. $graph.
  1249. '<br />'.get_lang('Feedback').'<br />
  1250. <textarea rows="5" cols="100" ></textarea>';
  1251. $address = api_get_setting('institution_address');
  1252. $phone = api_get_setting('administratorTelephone');
  1253. $address = str_replace('\n', '<br />', $address);
  1254. $pdf->custom_header = array('html' => "<h5 align='right'>$address <br />$phone</h5>");
  1255. $result = $pdf->html_to_pdf_with_template(
  1256. $content,
  1257. $saveToFile,
  1258. $saveToHtmlFile
  1259. );
  1260. if ($saveToHtmlFile) {
  1261. file_put_contents($file, $result);
  1262. return $file;
  1263. }
  1264. return $file;
  1265. }
  1266. }