diff --git a/main/newscorm/learnpath.class.php b/main/newscorm/learnpath.class.php index 208835a609..286f0eab03 100755 --- a/main/newscorm/learnpath.class.php +++ b/main/newscorm/learnpath.class.php @@ -1,6 +1,7 @@ created_on = $row['created_on']; $this->modified_on = $row['modified_on']; $this->ref = $row['ref']; + $this->categoryId = $row['category_id']; if ($row['publicated_on'] != '0000-00-00 00:00:00') { $this->publicated_on = $row['publicated_on']; @@ -708,7 +711,8 @@ class learnpath $origin = 'zip', $zipname = '', $publicated_on = '', - $expired_on = '' + $expired_on = '', + $categoryId = 0 ) { global $charset; $course_id = api_get_course_int_id(); @@ -717,6 +721,7 @@ class learnpath // Check lp_name doesn't exist, otherwise append something. $i = 0; $name = Database::escape_string($name); + $categoryId = intval($categoryId); // Session id. $session_id = api_get_session_id(); @@ -782,8 +787,8 @@ class learnpath $dsp = $row[0] + 1; } - $sql = "INSERT INTO $tbl_lp (c_id, lp_type,name,description,path,default_view_mod, default_encoding,display_order,content_maker,content_local,js_lib,session_id, created_on, publicated_on, expired_on) - VALUES ($course_id, $type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime()."' , '".$publicated_on."' , '".$expired_on."')"; + $sql = "INSERT INTO $tbl_lp (c_id, lp_type,name,description,path,default_view_mod, default_encoding,display_order,content_maker,content_local,js_lib,session_id, created_on, publicated_on, expired_on, category_id) + VALUES ($course_id, $type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime()."' , '".$publicated_on."' , '".$expired_on."', $categoryId)"; Database::query($sql); $id = Database :: insert_id(); if ($id > 0) { @@ -10022,6 +10027,179 @@ EOD; } } + /** + * @param array $params + */ + public static function createCategory($params) + { + $em = Database::getManager(); + $item = new CLpCategory(); + $item->setName($params['name']); + $item->setCId($params['c_id']); + $em->persist($item); + $em->flush(); + } + /** + * @param array $params + */ + public static function updateCategory($params) + { + $em = Database::getManager(); + $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']); + if ($item) { + $item->setName($params['name']); + $item->setCId($params['c_id']); + $em->persist($item); + $em->flush(); + } + } + + /** + * @param int $id + */ + public static function moveUpCategory($id) + { + $em = Database::getManager(); + $item = $em->find('ChamiloCourseBundle:CLpCategory', $id); + if ($item) { + $position = $item->getPosition() - 1; + $item->setPosition($position); + $em->persist($item); + $em->flush(); + } + } + + /** + * @param int $id + */ + public static function moveDownCategory($id) + { + $em = Database::getManager(); + $item = $em->find('ChamiloCourseBundle:CLpCategory', $id); + if ($item) { + $position = $item->getPosition() + 1; + $item->setPosition($position); + $em->persist($item); + $em->flush(); + } + } + + /** + * @param int $courseId + * @return int|mixed + */ + public static function getCountCategories($courseId) + { + if (empty($course_id)) { + return 0; + } + $em = Database::getManager(); + $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id'); + $query->setParameter('id', $courseId); + + return $query->getSingleScalarResult(); + } + + /** + * @param int $courseId + * + * @return mixed + */ + public static function getCategories($courseId) + { + $em = Database::getManager(); + //Default behaviour + /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy( + array('cId' => $course_id), + array('name' => 'ASC') + );*/ + + //Using doctrine extensions + $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->getBySortableGroupsQuery( + array('cId' => $courseId) + )->getResult(); + + return $items; + } + + /** + * @param int $id + * + * @return mixed + */ + public static function getCategory($id) + { + $em = Database::getManager(); + $item = $em->find('ChamiloCourseBundle:CLpCategory', $id); + + return $item; + } + + /** + * @param int $courseId + * @return array + */ + public static function getCategoryByCourse($courseId) + { + $em = Database::getManager(); + $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(array('cId' => $courseId)); + + return $items; + } + + /** + * @param int $id + * + * @return mixed + */ + public static function deleteCategory($id) + { + $em = Database::getManager(); + $item = $em->find('ChamiloCourseBundle:CLpCategory', $id); + if ($item) { + + $courseId = $item->getCId(); + $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId'); + $query->setParameter('id', $courseId); + $query->setParameter('catId', $item->getId()); + $lps = $query->getResult(); + + // Setting category = 0. + if ($lps) { + foreach ($lps as $lpItem) { + $lpItem->setCategoryId(0); + } + } + + // Removing category. + $em->remove($item); + $em->flush(); + } + } + + /** + * @param int $courseId + * @param bool $addSelectOption + * + * @return mixed + */ + static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false) + { + $items = self::getCategoryByCourse($courseId); + $cats = array(); + if ($addSelectOption) { + $cats = array(get_lang('SelectACategory')); + } + + if (!empty($items)) { + foreach ($items as $cat) { + $cats[$cat->getId()] = $cat->getName(); + } + } + + return $cats; + } + /** * Return the scorm item type object with spaces replaced with _ * The return result is use to build a css classname like scorm_type_$return @@ -10063,6 +10241,28 @@ EOD; return false; } + + /** + * @return int + */ + public function getCategoryId() + { + return $this->categoryId; + } + + public function setCategoryId($categoryId) + { + $this->categoryId = $categoryId; + + $courseId = api_get_course_int_id(); + $lp_table = Database :: get_course_table(TABLE_LP_MAIN); + $lp_id = $this->get_id(); + $sql = "UPDATE $lp_table SET category_id = '".intval($categoryId)."' + WHERE c_id = ".$courseId." AND id = '$lp_id'"; + Database::query($sql); + + return true; + } } if (!function_exists('trim_value')) { diff --git a/main/newscorm/learnpathList.class.php b/main/newscorm/learnpathList.class.php index f94a4695e9..83d2d96407 100755 --- a/main/newscorm/learnpathList.class.php +++ b/main/newscorm/learnpathList.class.php @@ -26,15 +26,23 @@ class LearnpathList * This method is the constructor for the learnpathList. It gets a list of available learning paths from * the database and creates the learnpath objects. This list depends on the user that is connected * (only displays) items if he has enough permissions to view them. - * @param integer $user_id - * @param string $course_code Optional course code (otherwise we use api_get_course_id()) - * @param int $session_id Optional session id (otherwise we use api_get_session_id()) - * @param string $order_by - * @param string $check_publication_dates + * @param integer $user_id + * @param string $course_code Optional course code (otherwise we use api_get_course_id()) + * @param int $session_id Optional session id (otherwise we use api_get_session_id()) + * @param string $order_by + * @param string $check_publication_dates + * @param int $categoryId + * * @return void */ - public function __construct($user_id, $course_code = '', $session_id = null, $order_by = null, $check_publication_dates = false) - { + public function __construct( + $user_id, + $course_code = '', + $session_id = null, + $order_by = null, + $check_publication_dates = false, + $categoryId = null + ) { $course_info = api_get_course_info($course_code); $lp_table = Database::get_course_table(TABLE_LP_MAIN); $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST); @@ -74,8 +82,21 @@ class LearnpathList "; } + $categoryFilter = ''; + + if (!is_null($categoryId) && is_numeric($categoryId)) { + $categoryId = intval($categoryId); + $categoryFilter = " AND category_id = $categoryId"; + } + $sql = "SELECT * FROM $lp_table - WHERE c_id = $course_id $time_conditions $condition_session $order"; + WHERE + c_id = $course_id + $time_conditions + $condition_session + $categoryFilter + $order + "; $res = Database::query($sql); $names = array(); diff --git a/main/newscorm/learnpath_functions.inc.php b/main/newscorm/learnpath_functions.inc.php index f5bb2c9f07..1f1fb283f5 100755 --- a/main/newscorm/learnpath_functions.inc.php +++ b/main/newscorm/learnpath_functions.inc.php @@ -24,7 +24,7 @@ * @todo remove code duplication */ -use \ChamiloSession as Session; +use ChamiloSession as Session; /** * This function deletes an item @@ -547,7 +547,8 @@ function display_learnpath_chapters($parent_item_id = 0, $tree = array (), $leve * @return void * @todo eliminate all global $lang declarations, use get_lang, improve structure. */ -function display_learnpath_items($categoryid) { +function display_learnpath_items($categoryid) +{ global $xml_output; global $lg_prerequisites, $lg_move_down, $lg_move_up, $lg_edit_learnpath_item, $lg_delete_learnpath_item, $learnpath_id, $lg_add_prereq, $lg_prereq_deleted_error, $lg_pre_short, $langThisItem; $course_id = api_get_course_int_id(); diff --git a/main/newscorm/lp_add.php b/main/newscorm/lp_add.php index bb3c9f15ce..c936964360 100755 --- a/main/newscorm/lp_add.php +++ b/main/newscorm/lp_add.php @@ -115,7 +115,13 @@ $form->addElement('hidden', 'action', 'add_lp'); $form->addButtonAdvancedSettings('advanced_params'); $form->addElement('html', '