GradebookUtils.php 57 KB

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