is_course_model = true; $this->table = Database::get_course_table('exercise_category'); } /** * Gets the number of values stored in the table (all fields together) * for this type of resource. * * @param int $courseId * * @return int Number of rows in the table */ public function getCourseCount($courseId) { $em = Database::getManager(); $query = $em->getRepository('ChamiloCourseBundle:CExerciseCategory')->createQueryBuilder('e'); $query->select('count(e.id)'); $query->where('e.cId = :cId'); $query->setParameter('cId', $courseId); return $query->getQuery()->getSingleScalarResult(); } /** * @param int $courseId * * @return array */ public function getCategories($courseId) { $em = Database::getManager(); $query = $em->getRepository('ChamiloCourseBundle:CExerciseCategory')->createQueryBuilder('e'); $query->where('e.cId = :cId'); $query->setParameter('cId', $courseId); $query->orderBy('e.position'); return $query->getQuery()->getResult(); } /** * @param int $courseId * * @return array */ public function getCategoriesForSelect($courseId) { $categories = $this->getCategories($courseId); $options = []; if (!empty($categories)) { /** @var CExerciseCategory $category */ foreach ($categories as $category) { $options[$category->getId()] = $category->getName(); } } return $options; } /** * @param int $id */ public function delete($id) { $em = Database::getManager(); $repo = Database::getManager()->getRepository('ChamiloCourseBundle:CExerciseCategory'); $category = $repo->find($id); if ($category) { $em->remove($category); $em->flush(); $courseId = api_get_course_int_id(); $table = Database::get_course_table(TABLE_QUIZ_TEST); $id = (int) $id; $sql = "UPDATE $table SET exercise_category_id = 0 WHERE c_id = $courseId AND exercise_category_id = $id"; Database::query($sql); } } /** * Save values in the *_field_values table. * * @param array $params Structured array with the values to save * @param bool $showQuery Whether to show the insert query (passed to the parent save() method) */ public function save($params, $showQuery = false) { $courseId = api_get_course_int_id(); $em = Database::getManager(); $category = new CExerciseCategory(); $category ->setName($params['name']) ->setCId(api_get_course_int_id()) ->setDescription($params['name']) ; /* // Update position $query = $em->getRepository('ChamiloCourseBundle:CExerciseCategory')->createQueryBuilder('e'); $query ->where('e.cId = :cId') ->setParameter('cId', $courseId) ->setMaxResults(1) ->orderBy('e.position', 'DESC'); $last = $query->getQuery()->getOneOrNullResult(); $position = 0; if (!empty($last)) { $position = $last->getPosition() + 1; } $category->setPosition($position); */ $em->persist($category); $em->flush(); return $category; } /** * @param $token * * @return string */ public function getJqgridActionLinks($token) { //With this function we can add actions to the jgrid (edit, delete, etc) $editIcon = Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL); $deleteIcon = Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL); $confirmMessage = addslashes( api_htmlentities(get_lang('Please confirm your choice'), ENT_QUOTES) ); $courseParams = api_get_cidreq(); $editButton = <<\ $editIcon\ JAVASCRIPT; $deleteButton = <<\ $deleteIcon\ JAVASCRIPT; return "function action_formatter(cellvalue, options, rowObject) { return '$editButton $deleteButton'; }"; } /** * @param string $url * @param string $action * * @return FormValidator */ public function return_form($url, $action) { $form = new FormValidator('category', 'post', $url); $id = isset($_GET['id']) ? (int) $_GET['id'] : null; $form->addElement('hidden', 'id', $id); // Setting the form elements $header = get_lang('Add'); $defaults = []; if ($action === 'edit') { $header = get_lang('Edit'); // Setting the defaults $defaults = $this->get($id, false); } $form->addElement('header', $header); $form->addText( 'name', get_lang('Name') ); $form->addHtmlEditor('description', get_lang('Description')); if ($action === 'edit') { $form->addButtonUpdate(get_lang('Edit')); } else { $form->addButtonCreate(get_lang('Add')); } /*if (!empty($defaults['created_at'])) { $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']); } if (!empty($defaults['updated_at'])) { $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']); }*/ $form->setDefaults($defaults); // Setting the rules $form->addRule('name', get_lang('Required field'), 'required'); return $form; } /** * @return string */ public function display() { // action links $content = ''; $content .= Display::grid_html('categories'); return $content; } }