ExerciseCategoryManager.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CExerciseCategory;
  4. /**
  5. * Class ExtraFieldValue
  6. * Declaration for the ExtraFieldValue class, managing the values in extra
  7. * fields for any data type.
  8. *
  9. * @package chamilo.library
  10. */
  11. class ExerciseCategoryManager extends Model
  12. {
  13. public $type = '';
  14. public $columns = [
  15. 'id',
  16. 'name',
  17. 'c_id',
  18. 'description',
  19. 'created_at',
  20. 'updated_at',
  21. ];
  22. /**
  23. * Formats the necessary elements for the given datatype.
  24. *
  25. * @param string $type The type of data to which this extra field
  26. * applies (user, course, session, ...)
  27. *
  28. * @assert (-1) === false
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. $this->is_course_model = true;
  34. $this->table = Database::get_course_table('exercise_category');
  35. }
  36. /**
  37. * Gets the number of values stored in the table (all fields together)
  38. * for this type of resource.
  39. *
  40. * @param int $courseId
  41. *
  42. * @return int Number of rows in the table
  43. */
  44. public function getCourseCount($courseId)
  45. {
  46. $em = Database::getManager();
  47. $query = $em->getRepository('ChamiloCourseBundle:CExerciseCategory')->createQueryBuilder('e');
  48. $query->select('count(e.id)');
  49. $query->where('e.cId = :cId');
  50. $query->setParameter('cId', $courseId);
  51. return $query->getQuery()->getSingleScalarResult();
  52. }
  53. /**
  54. * @param int $courseId
  55. *
  56. * @return array
  57. */
  58. public function getCategories($courseId)
  59. {
  60. $em = Database::getManager();
  61. $query = $em->getRepository('ChamiloCourseBundle:CExerciseCategory')->createQueryBuilder('e');
  62. $query->where('e.cId = :cId');
  63. $query->setParameter('cId', $courseId);
  64. $query->orderBy('e.position');
  65. return $query->getQuery()->getResult();
  66. }
  67. /**
  68. * @param int $courseId
  69. *
  70. * @return array
  71. */
  72. public function getCategoriesForSelect($courseId)
  73. {
  74. $categories = $this->getCategories($courseId);
  75. $options = [];
  76. if (!empty($categories)) {
  77. /** @var CExerciseCategory $category */
  78. foreach ($categories as $category) {
  79. $options[$category->getId()] = $category->getName();
  80. }
  81. }
  82. return $options;
  83. }
  84. /**
  85. * @param int $id
  86. */
  87. public function delete($id)
  88. {
  89. $em = Database::getManager();
  90. $repo = Database::getManager()->getRepository('ChamiloCourseBundle:CExerciseCategory');
  91. $category = $repo->find($id);
  92. if ($category) {
  93. $em->remove($category);
  94. $em->flush();
  95. $courseId = api_get_course_int_id();
  96. $table = Database::get_course_table(TABLE_QUIZ_TEST);
  97. $id = (int) $id;
  98. $sql = "UPDATE $table SET exercise_category_id = 0
  99. WHERE c_id = $courseId AND exercise_category_id = $id";
  100. Database::query($sql);
  101. }
  102. }
  103. /**
  104. * Save values in the *_field_values table.
  105. *
  106. * @param array $params Structured array with the values to save
  107. * @param bool $showQuery Whether to show the insert query (passed to the parent save() method)
  108. */
  109. public function save($params, $showQuery = false)
  110. {
  111. $courseId = api_get_course_int_id();
  112. $em = Database::getManager();
  113. $category = new CExerciseCategory();
  114. $category
  115. ->setName($params['name'])
  116. ->setCId(api_get_course_int_id())
  117. ->setDescription($params['name'])
  118. ;
  119. /*
  120. // Update position
  121. $query = $em->getRepository('ChamiloCourseBundle:CExerciseCategory')->createQueryBuilder('e');
  122. $query
  123. ->where('e.cId = :cId')
  124. ->setParameter('cId', $courseId)
  125. ->setMaxResults(1)
  126. ->orderBy('e.position', 'DESC');
  127. $last = $query->getQuery()->getOneOrNullResult();
  128. $position = 0;
  129. if (!empty($last)) {
  130. $position = $last->getPosition() + 1;
  131. }
  132. $category->setPosition($position);
  133. */
  134. $em->persist($category);
  135. $em->flush();
  136. return $category;
  137. }
  138. /**
  139. * @param $token
  140. *
  141. * @return string
  142. */
  143. public function getJqgridActionLinks($token)
  144. {
  145. //With this function we can add actions to the jgrid (edit, delete, etc)
  146. $editIcon = Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL);
  147. $deleteIcon = Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
  148. $confirmMessage = addslashes(
  149. api_htmlentities(get_lang('Please confirm your choice'), ENT_QUOTES)
  150. );
  151. $courseParams = api_get_cidreq();
  152. $editButton = <<<JAVASCRIPT
  153. <a href="?action=edit&{$courseParams}&id=' + options.rowId + '" class="btn btn-link btn-xs">\
  154. $editIcon\
  155. </a>
  156. JAVASCRIPT;
  157. $deleteButton = <<<JAVASCRIPT
  158. <a \
  159. onclick="if (!confirm(\'$confirmMessage\')) {return false;}" \
  160. href="?sec_token=$token&{$courseParams}&id=' + options.rowId + '&action=delete" \
  161. class="btn btn-link btn-xs">\
  162. $deleteIcon\
  163. </a>
  164. JAVASCRIPT;
  165. return "function action_formatter(cellvalue, options, rowObject) {
  166. return '$editButton $deleteButton';
  167. }";
  168. }
  169. /**
  170. * @param string $url
  171. * @param string $action
  172. *
  173. * @return FormValidator
  174. */
  175. public function return_form($url, $action)
  176. {
  177. $form = new FormValidator('category', 'post', $url);
  178. $id = isset($_GET['id']) ? (int) $_GET['id'] : null;
  179. $form->addElement('hidden', 'id', $id);
  180. // Setting the form elements
  181. $header = get_lang('Add');
  182. $defaults = [];
  183. if ($action === 'edit') {
  184. $header = get_lang('Edit');
  185. // Setting the defaults
  186. $defaults = $this->get($id, false);
  187. }
  188. $form->addElement('header', $header);
  189. $form->addText(
  190. 'name',
  191. get_lang('Name')
  192. );
  193. $form->addHtmlEditor('description', get_lang('Description'));
  194. if ($action === 'edit') {
  195. $form->addButtonUpdate(get_lang('Edit'));
  196. } else {
  197. $form->addButtonCreate(get_lang('Add'));
  198. }
  199. /*if (!empty($defaults['created_at'])) {
  200. $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
  201. }
  202. if (!empty($defaults['updated_at'])) {
  203. $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
  204. }*/
  205. $form->setDefaults($defaults);
  206. // Setting the rules
  207. $form->addRule('name', get_lang('Required field'), 'required');
  208. return $form;
  209. }
  210. /**
  211. * @return string
  212. */
  213. public function display()
  214. {
  215. // action links
  216. $content = '<div class="actions">';
  217. $content .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'">';
  218. $content .= Display::return_icon(
  219. 'back.png',
  220. get_lang('Back to').' '.get_lang('Administration'),
  221. '',
  222. ICON_SIZE_MEDIUM
  223. );
  224. $content .= '</a>';
  225. $content .= '<a href="'.api_get_self().'?action=add&'.api_get_cidreq().'">';
  226. $content .= Display::return_icon(
  227. 'add.png',
  228. get_lang('Add'),
  229. '',
  230. ICON_SIZE_MEDIUM
  231. );
  232. $content .= '</a>';
  233. $content .= '</div>';
  234. $content .= Display::grid_html('categories');
  235. return $content;
  236. }
  237. }