Subscribing users to a LP when relating groups with items see BT#5765

skala
Julio Montoya 12 years ago
parent 66b06f5ed4
commit 61f3fb4c81
  1. 147
      main/inc/Entity/Repository/ItemPropertyRepository.php
  2. 20
      main/newscorm/learnpath.class.php
  3. 101
      src/ChamiloLMS/Controller/LearnpathController.php

@ -14,7 +14,9 @@ class ItemPropertyRepository extends EntityRepository
{ {
/** /**
* *
* @param $tool * Get users subscribed to a item LP, Document, etc (item_property)
*
* @param $tool learnpath | document | etc
* @param $itemId * @param $itemId
* @param \Entity\EntityCourse $course * @param \Entity\EntityCourse $course
* @param int $sessionId * @param int $sessionId
@ -33,11 +35,9 @@ class ItemPropertyRepository extends EntityRepository
'group' => $group 'group' => $group
); );
return $this->findBy($criteria); return $this->findBy($criteria);
/*
$qb = $this->createQueryBuilder('i') $qb = $this->createQueryBuilder('i')
->select('i'); ->select('i');
$wherePart = $qb->expr()->andx(); $wherePart = $qb->expr()->andx();
//Selecting courses for users //Selecting courses for users
@ -53,9 +53,17 @@ class ItemPropertyRepository extends EntityRepository
$qb->where($wherePart); $qb->where($wherePart);
$q = $qb->getQuery(); $q = $qb->getQuery();
//var_dump($q->getSQL()); //var_dump($q->getSQL());
return $q->execute(); return $q->execute();*/
} }
/**
* Get Groups subscribed to a item: LP, Doc, etc
* @param $tool learnpath | document | etc
* @param $itemId
* @param \Entity\EntityCourse $course
* @param \Entity\EntitySession $session
* @return array
*/
public function getGroupsSubscribedToItem($tool, $itemId, \Entity\EntityCourse $course, \Entity\EntitySession $session = null) public function getGroupsSubscribedToItem($tool, $itemId, \Entity\EntityCourse $course, \Entity\EntitySession $session = null)
{ {
$criteria = array( $criteria = array(
@ -69,7 +77,15 @@ class ItemPropertyRepository extends EntityRepository
return $this->findBy($criteria); return $this->findBy($criteria);
} }
public function SubscribedGroupsToItem($tool, \Entity\EntityCourse $course, \Entity\EntitySession $session = null, $itemId, $newList = array()) /**
* Subscribe groups to a LP, doc (itemproperty)
* @param $tool learnpath | document | etc
* @param \Entity\EntityCourse $course
* @param \Entity\EntitySession $session
* @param $itemId
* @param array $newList
*/
public function subscribeGroupsToItem($tool, \Entity\EntityCourse $course, \Entity\EntitySession $session = null, $itemId, $newList = array())
{ {
$em = $this->getEntityManager(); $em = $this->getEntityManager();
$groupsSubscribedToItem = $this->getGroupsSubscribedToItem($tool, $itemId, $course, $session); $groupsSubscribedToItem = $this->getGroupsSubscribedToItem($tool, $itemId, $course, $session);
@ -88,17 +104,7 @@ class ItemPropertyRepository extends EntityRepository
} }
if ($toDelete) { if ($toDelete) {
foreach ($toDelete as $itemToDelete) { $this->unsubscribeGroupsToItem($tool, $course, $session, $itemId, $toDelete, true);
$item = $this->findOneBy(array(
'tool' => $tool,
'session' => $session,
'ref' => $itemId,
'toGroupId' => $itemToDelete
));
if ($item) {
$em->remove($item);
}
}
} }
foreach ($newList as $groupId) { foreach ($newList as $groupId) {
@ -116,18 +122,83 @@ class ItemPropertyRepository extends EntityRepository
$item->setVisibility('1'); $item->setVisibility('1');
$em->persist($item); //$em is an instance of EntityManager $em->persist($item); //$em is an instance of EntityManager
} }
//Adding users from this group to the item
$users = \GroupManager::get_members_and_tutors($groupId);
$newUserList = array();
if (!empty($users)) {
foreach($users as $user) {
$newUserList[] = $user['user_id'];
}
$this->subscribeUsersToItem(
'learnpath',
$course,
$session,
$itemId,
$newUserList
);
}
} }
$em->flush(); $em->flush();
} }
/** /**
* Unsubscribe groups to item
* @param $tool
* @param \Entity\EntityCourse $course
* @param \Entity\EntitySession $session
* @param $itemId
* @param $groups
*/
function unsubscribeGroupsToItem($tool, \Entity\EntityCourse $course, \Entity\EntitySession $session = null, $itemId, $groups, $unsubscribeUserToo = false)
{
if (!empty($groups)) {
$em = $this->getEntityManager();
foreach ($groups as $groupId) {
$item = $this->findOneBy(array(
'tool' => $tool,
'session' => $session,
'ref' => $itemId,
'toGroupId' => $groupId
));
if ($item) {
$em->remove($item);
}
if ($unsubscribeUserToo) {
//Adding users from this group to the item
$users = \GroupManager::get_members_and_tutors($groupId);
$newUserList = array();
if (!empty($users)) {
foreach($users as $user) {
$newUserList[] = $user['user_id'];
}
$this->unsubcribeUsersToItem(
'learnpath',
$course,
$session,
$itemId,
$newUserList
);
}
}
}
$em->flush();
}
}
/**
* Subscribe users to a LP, doc (itemproperty)
*
* @param $tool * @param $tool
* @param \Entity\EntityCourse $course * @param \Entity\EntityCourse $course
* @param \Entity\EntitySession $session * @param \Entity\EntitySession $session
* @param $itemId * @param $itemId
* @param array $newUserList * @param array $newUserList
*/ */
public function SubscribedUsersToItem($tool, \Entity\EntityCourse $course, \Entity\EntitySession $session = null, $itemId, $newUserList = array()) public function subscribeUsersToItem($tool, \Entity\EntityCourse $course, \Entity\EntitySession $session = null, $itemId, $newUserList = array())
{ {
$em = $this->getEntityManager(); $em = $this->getEntityManager();
$user = $em->getRepository('Entity\EntityUser'); $user = $em->getRepository('Entity\EntityUser');
@ -148,17 +219,7 @@ class ItemPropertyRepository extends EntityRepository
} }
if ($usersToDelete) { if ($usersToDelete) {
foreach ($usersToDelete as $userToDelete) { $this->unsubcribeUsersToItem($tool, $course, $session, $itemId, $usersToDelete);
$item = $this->findOneBy(array(
'tool' => $tool,
'session' => $session,
'ref' => $itemId,
'toUserId' => $userToDelete
));
if ($item) {
$em->remove($item);
}
}
} }
foreach ($newUserList as $userId) { foreach ($newUserList as $userId) {
@ -179,4 +240,32 @@ class ItemPropertyRepository extends EntityRepository
} }
$em->flush(); $em->flush();
} }
/**
* Unsubscribe users to item
* @param $tool
* @param \Entity\EntityCourse $course
* @param \Entity\EntitySession $session
* @param $itemId
* @param $usersToDelete
*/
public function unsubcribeUsersToItem($tool, \Entity\EntityCourse $course, \Entity\EntitySession $session = null, $itemId, $usersToDelete)
{
$em = $this->getEntityManager();
if (!empty($usersToDelete)) {
foreach ($usersToDelete as $userId) {
$item = $this->findOneBy(array(
'tool' => $tool,
'session' => $session,
'ref' => $itemId,
'toUserId' => $userId
));
if ($item) {
$em->remove($item);
}
}
$em->flush();
}
}
} }

@ -2388,7 +2388,9 @@ class learnpath
} }
} }
//Check if the subscription users/group to a LP is ON
if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1 ) { if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1 ) {
//Checking only the user visibility
$userVisibility = api_get_item_visibility($course_info, 'learnpath', $row['id'], $session_id, $student_id, 'LearnpathSubscription'); $userVisibility = api_get_item_visibility($course_info, 'learnpath', $row['id'], $session_id, $student_id, 'LearnpathSubscription');
if ($userVisibility == 1) { if ($userVisibility == 1) {
@ -2396,8 +2398,8 @@ class learnpath
} else { } else {
$is_visible = false; $is_visible = false;
} }
//the group visibility is not going to be checked because the user *must* be related to the LP
$groupList = GroupManager::get_group_ids($course_info['real_id'], $student_id); /*$groupList = GroupManager::get_group_ids($course_info['real_id'], $student_id);
if (!empty($groupList)) { if (!empty($groupList)) {
foreach($groupList as $groupId) { foreach($groupList as $groupId) {
@ -2408,7 +2410,7 @@ class learnpath
break; break;
} }
} }
} }*/
} }
return $is_visible; return $is_visible;
@ -6962,7 +6964,6 @@ class learnpath
public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '') public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
{ {
$course_id = api_get_course_int_id(); $course_id = api_get_course_int_id();
global $charset;
$uploadPath = DIR_HOTPOTATOES; //defined in main_api $uploadPath = DIR_HOTPOTATOES; //defined in main_api
$tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM); $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
@ -7160,7 +7161,6 @@ class learnpath
public function display_forum_form($action = 'add', $id = 0, $extra_info = '') public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
{ {
$course_id = api_get_course_int_id(); $course_id = api_get_course_int_id();
global $charset;
$tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM); $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
$tbl_forum = Database :: get_course_table(TABLE_FORUM); $tbl_forum = Database :: get_course_table(TABLE_FORUM);
@ -7827,7 +7827,6 @@ class learnpath
public function display_document_form($action = 'add', $id = 0, $extra_info = 'new') public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
{ {
$course_id = api_get_course_int_id(); $course_id = api_get_course_int_id();
global $charset;
$tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM); $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
$tbl_doc = Database :: get_course_table(TABLE_DOCUMENT); $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
@ -8199,7 +8198,6 @@ class learnpath
public function display_link_form($action = 'add', $id = 0, $extra_info = '') public function display_link_form($action = 'add', $id = 0, $extra_info = '')
{ {
$course_id = api_get_course_int_id(); $course_id = api_get_course_int_id();
global $charset;
$tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM); $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
$tbl_link = Database :: get_course_table(TABLE_LINK); $tbl_link = Database :: get_course_table(TABLE_LINK);
@ -8410,8 +8408,6 @@ class learnpath
public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '') public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
{ {
$course_id = api_get_course_int_id(); $course_id = api_get_course_int_id();
global $charset;
$tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM); $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
$tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION); $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
@ -8630,7 +8626,7 @@ class learnpath
$course_id = api_get_course_int_id(); $course_id = api_get_course_int_id();
$course_code = api_get_course_id(); $course_code = api_get_course_id();
global $charset, $_course; global $_course;
$return = '<div class="actions">'; $return = '<div class="actions">';
switch ($item_type) { switch ($item_type) {
@ -8750,7 +8746,6 @@ class learnpath
ORDER BY display_order ASC"; ORDER BY display_order ASC";
$res_zero = Database::query($sql_zero); $res_zero = Database::query($sql_zero);
global $charset;
$i = 0; $i = 0;
while ($row_zero = Database :: fetch_array($res_zero)) { while ($row_zero = Database :: fetch_array($res_zero)) {
@ -8790,9 +8785,6 @@ class learnpath
public function display_move_item($item_id) public function display_move_item($item_id)
{ {
$course_id = api_get_course_int_id(); $course_id = api_get_course_int_id();
global $_course; //will disappear
global $charset;
$return = ''; $return = '';
if (is_numeric($item_id)) { if (is_numeric($item_id)) {

@ -1,5 +1,5 @@
<?php <?php
/* For licensing terms, see /license.txt */
namespace ChamiloLMS\Controller; namespace ChamiloLMS\Controller;
use Silex\Application; use Silex\Application;
@ -8,10 +8,10 @@ use Symfony\Component\HttpFoundation\Response;
/** /**
* Class LearnpathController * Class LearnpathController
* @package ChamiloLMS\Controller * @package ChamiloLMS\Controller
* @author Julio Montoya <gugli100@gmail.com>
*/ */
class LearnpathController class LearnpathController
{ {
/** /**
* Index * Index
* *
@ -25,11 +25,21 @@ class LearnpathController
public function indexAction(Application $app, $lpId) public function indexAction(Application $app, $lpId)
{ {
$request = $app['request']; $request = $app['request'];
$courseId = api_get_course_int_id();
//@todo use the before filter to aborts this course calls
if (empty($courseId)) {
$app->abort(403, 'Course not available');
}
$courseCode = api_get_course_id(); $courseCode = api_get_course_id();
$lp = new \learnpath($courseCode, $lpId, api_get_user_id());
$lp = new \learnpath($courseCode, $lpId, api_get_user_id());
$url = $app['url_generator']->generate('subscribe_users', array('lpId' => $lpId)); $url = $app['url_generator']->generate('subscribe_users', array('lpId' => $lpId));
//Setting breadcrumb @todo move this in the template lib
$breadcrumb = array( $breadcrumb = array(
array( array(
'url' => api_get_path(WEB_CODE_PATH).'newscorm/lp_controller.php?action=list', 'url' => api_get_path(WEB_CODE_PATH).'newscorm/lp_controller.php?action=list',
@ -44,51 +54,64 @@ class LearnpathController
$app['breadcrumb'] = $breadcrumb; $app['breadcrumb'] = $breadcrumb;
//Find session
$sessionId = api_get_session_id(); $sessionId = api_get_session_id();
$session = null; $session = null;
if (!empty($sessionId)) { if (!empty($sessionId)) {
$session = $app['orm.em']->getRepository('Entity\EntitySession')->find($sessionId); $session = $app['orm.em']->getRepository('Entity\EntitySession')->find($sessionId);
} }
$courseId = api_get_course_int_id();
//@todo use the before filter do not put all aborts in controllers
if (empty($courseId)) {
$app->abort(403, 'Course not availablel');
}
//Find course
$course = $app['orm.em']->getRepository('Entity\EntityCourse')->find($courseId); $course = $app['orm.em']->getRepository('Entity\EntityCourse')->find($courseId);
//Getting subscribe users to the course
$subscribedUsers = $app['orm.em']->getRepository('Entity\EntityCourse')->getSubscribedStudents($course); $subscribedUsers = $app['orm.em']->getRepository('Entity\EntityCourse')->getSubscribedStudents($course);
$subscribedUsers = $subscribedUsers->getQuery(); $subscribedUsers = $subscribedUsers->getQuery();
$subscribedUsers = $subscribedUsers->execute(); $subscribedUsers = $subscribedUsers->execute();
//Getting all users //Getting all users in a nice format
$choices = array(); $choices = array();
foreach ($subscribedUsers as $user) { foreach ($subscribedUsers as $user) {
$choices[$user->getUserId()] = $user->getCompleteNameWithClasses(); $choices[$user->getUserId()] = $user->getCompleteNameWithClasses();
} }
//Getting subscribed users to a LP
$subscribedUsersInLp = $app['orm.em']->getRepository('Entity\EntityCItemProperty')->getUsersSubscribedToItem( $subscribedUsersInLp = $app['orm.em']->getRepository('Entity\EntityCItemProperty')->getUsersSubscribedToItem(
'learnpath', 'learnpath',
$lpId, $lpId,
$course, $course,
$session $session
); );
//Getting users subscribed to the LP
$selectedChoices = array(); $selectedChoices = array();
foreach ($subscribedUsersInLp as $itemProperty) { foreach ($subscribedUsersInLp as $itemProperty) {
$selectedChoices[] = $itemProperty->getToUserId(); $selectedChoices[] = $itemProperty->getToUserId();
} }
$form = new \FormValidator('lp_edit', 'post', $url); //Building the form for Users
$form->addElement('header', get_lang('SubscribeUsersToLp')); $formUsers = new \FormValidator('lp_edit', 'post', $url);
$formUsers->addElement('hidden', 'user_form', 1);
$formUsers->addElement('header', get_lang('SubscribeUsersToLp'));
$userMultiSelect = $form->addElement('advmultiselect', 'users', get_lang('Users'), $choices); $userMultiSelect = $formUsers->addElement('advmultiselect', 'users', get_lang('Users'), $choices);
$userMultiSelect->setButtonAttributes('add'); $userMultiSelect->setButtonAttributes('add');
$userMultiSelect->setButtonAttributes('remove'); $userMultiSelect->setButtonAttributes('remove');
$formUsers->addElement('style_submit_button', 'submit', get_lang('Save'), 'class="save"');
$defaults = array();
if (!empty($selectedChoices)) {
$defaults['users'] = $selectedChoices;
}
$formUsers->setDefaults($defaults);
//Building the form for Groups
$form = new \FormValidator('lp_edit', 'post', $url);
$form->addElement('header', get_lang('SubscribeGroupsToLp'));
$form->addElement('hidden', 'group_form', 1);
//Group list //Group list
$groupList = \CourseManager::get_group_list_of_course(api_get_course_id(), api_get_session_id(), 1); $groupList = \CourseManager::get_group_list_of_course(api_get_course_id(), api_get_session_id(), 1);
$groupChoices = array(); $groupChoices = array();
@ -147,39 +170,45 @@ class LearnpathController
->getForm(); ->getForm();
*/ */
$defaults = array(); $defaults = array();
if (!empty($selectedChoices)) {
$defaults['users'] = $selectedChoices;
}
if (!empty($selectedGroupChoices)) { if (!empty($selectedGroupChoices)) {
$defaults['groups'] = $selectedGroupChoices; $defaults['groups'] = $selectedGroupChoices;
} }
$form->setDefaults($defaults); $form->setDefaults($defaults);
if ($request->getMethod() == 'POST') { if ($request->getMethod() == 'POST') {
//Subscribing users
$users = $request->get('users'); $users = $request->get('users');
$app['orm.em']->getRepository('Entity\EntityCItemProperty')->SubscribedUsersToItem( $userForm = $request->get('user_form');
'learnpath', if (!empty($userForm)) {
$course, $app['orm.em']->getRepository('Entity\EntityCItemProperty')->subscribeUsersToItem(
$session, 'learnpath',
$lpId, $course,
$users $session,
); $lpId,
$users
);
}
//Subscribing groups
$groups = $request->get('groups'); $groups = $request->get('groups');
$app['orm.em']->getRepository('Entity\EntityCItemProperty')->SubscribedGroupsToItem( $groupForm = $request->get('group_form');
'learnpath',
$course, if (!empty($groupForm)) {
$session, $app['orm.em']->getRepository('Entity\EntityCItemProperty')->subscribeGroupsToItem(
$lpId, 'learnpath',
$groups $course,
); $session,
$lpId,
$groups
);
}
return $app->redirect($url); return $app->redirect($url);
} else { } else {
$app['template']->assign('form', $form->toHtml()); $app['template']->assign('formUsers', $formUsers->toHtml());
$app['template']->assign('formGroups', $form->toHtml());
} }
$response = $app['template']->render_template('learnpath/subscribe_users.tpl'); $response = $app['template']->render_template('learnpath/subscribe_users.tpl');
return new Response($response, 200, array()); return new Response($response, 200, array());

Loading…
Cancel
Save