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 \Entity\EntityCourse $course
* @param int $sessionId
@ -33,11 +35,9 @@ class ItemPropertyRepository extends EntityRepository
'group' => $group
);
return $this->findBy($criteria);
/*
$qb = $this->createQueryBuilder('i')
->select('i');
$wherePart = $qb->expr()->andx();
//Selecting courses for users
@ -53,9 +53,17 @@ class ItemPropertyRepository extends EntityRepository
$qb->where($wherePart);
$q = $qb->getQuery();
//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)
{
$criteria = array(
@ -69,7 +77,15 @@ class ItemPropertyRepository extends EntityRepository
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();
$groupsSubscribedToItem = $this->getGroupsSubscribedToItem($tool, $itemId, $course, $session);
@ -88,17 +104,7 @@ class ItemPropertyRepository extends EntityRepository
}
if ($toDelete) {
foreach ($toDelete as $itemToDelete) {
$item = $this->findOneBy(array(
'tool' => $tool,
'session' => $session,
'ref' => $itemId,
'toGroupId' => $itemToDelete
));
if ($item) {
$em->remove($item);
}
}
$this->unsubscribeGroupsToItem($tool, $course, $session, $itemId, $toDelete, true);
}
foreach ($newList as $groupId) {
@ -116,18 +122,83 @@ class ItemPropertyRepository extends EntityRepository
$item->setVisibility('1');
$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();
}
/**
* 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 \Entity\EntityCourse $course
* @param \Entity\EntitySession $session
* @param $itemId
* @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();
$user = $em->getRepository('Entity\EntityUser');
@ -148,17 +219,7 @@ class ItemPropertyRepository extends EntityRepository
}
if ($usersToDelete) {
foreach ($usersToDelete as $userToDelete) {
$item = $this->findOneBy(array(
'tool' => $tool,
'session' => $session,
'ref' => $itemId,
'toUserId' => $userToDelete
));
if ($item) {
$em->remove($item);
}
}
$this->unsubcribeUsersToItem($tool, $course, $session, $itemId, $usersToDelete);
}
foreach ($newUserList as $userId) {
@ -179,4 +240,32 @@ class ItemPropertyRepository extends EntityRepository
}
$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 ) {
//Checking only the user visibility
$userVisibility = api_get_item_visibility($course_info, 'learnpath', $row['id'], $session_id, $student_id, 'LearnpathSubscription');
if ($userVisibility == 1) {
@ -2396,8 +2398,8 @@ class learnpath
} else {
$is_visible = false;
}
$groupList = GroupManager::get_group_ids($course_info['real_id'], $student_id);
//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);
if (!empty($groupList)) {
foreach($groupList as $groupId) {
@ -2408,7 +2410,7 @@ class learnpath
break;
}
}
}
}*/
}
return $is_visible;
@ -6962,7 +6964,6 @@ class learnpath
public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
{
$course_id = api_get_course_int_id();
global $charset;
$uploadPath = DIR_HOTPOTATOES; //defined in main_api
$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 = '')
{
$course_id = api_get_course_int_id();
global $charset;
$tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
$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')
{
$course_id = api_get_course_int_id();
global $charset;
$tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
$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 = '')
{
$course_id = api_get_course_int_id();
global $charset;
$tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
$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 = '')
{
$course_id = api_get_course_int_id();
global $charset;
$tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
$tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
@ -8630,7 +8626,7 @@ class learnpath
$course_id = api_get_course_int_id();
$course_code = api_get_course_id();
global $charset, $_course;
global $_course;
$return = '<div class="actions">';
switch ($item_type) {
@ -8750,7 +8746,6 @@ class learnpath
ORDER BY display_order ASC";
$res_zero = Database::query($sql_zero);
global $charset;
$i = 0;
while ($row_zero = Database :: fetch_array($res_zero)) {
@ -8790,9 +8785,6 @@ class learnpath
public function display_move_item($item_id)
{
$course_id = api_get_course_int_id();
global $_course; //will disappear
global $charset;
$return = '';
if (is_numeric($item_id)) {

@ -1,5 +1,5 @@
<?php
/* For licensing terms, see /license.txt */
namespace ChamiloLMS\Controller;
use Silex\Application;
@ -8,10 +8,10 @@ use Symfony\Component\HttpFoundation\Response;
/**
* Class LearnpathController
* @package ChamiloLMS\Controller
* @author Julio Montoya <gugli100@gmail.com>
*/
class LearnpathController
{
/**
* Index
*
@ -25,11 +25,21 @@ class LearnpathController
public function indexAction(Application $app, $lpId)
{
$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();
$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));
//Setting breadcrumb @todo move this in the template lib
$breadcrumb = array(
array(
'url' => api_get_path(WEB_CODE_PATH).'newscorm/lp_controller.php?action=list',
@ -44,51 +54,64 @@ class LearnpathController
$app['breadcrumb'] = $breadcrumb;
//Find session
$sessionId = api_get_session_id();
$session = null;
if (!empty($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);
//Getting subscribe users to the course
$subscribedUsers = $app['orm.em']->getRepository('Entity\EntityCourse')->getSubscribedStudents($course);
$subscribedUsers = $subscribedUsers->getQuery();
$subscribedUsers = $subscribedUsers->execute();
//Getting all users
//Getting all users in a nice format
$choices = array();
foreach ($subscribedUsers as $user) {
$choices[$user->getUserId()] = $user->getCompleteNameWithClasses();
}
//Getting subscribed users to a LP
$subscribedUsersInLp = $app['orm.em']->getRepository('Entity\EntityCItemProperty')->getUsersSubscribedToItem(
'learnpath',
$lpId,
$course,
$session
);
//Getting users subscribed to the LP
$selectedChoices = array();
foreach ($subscribedUsersInLp as $itemProperty) {
$selectedChoices[] = $itemProperty->getToUserId();
}
$form = new \FormValidator('lp_edit', 'post', $url);
$form->addElement('header', get_lang('SubscribeUsersToLp'));
//Building the form for Users
$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('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
$groupList = \CourseManager::get_group_list_of_course(api_get_course_id(), api_get_session_id(), 1);
$groupChoices = array();
@ -147,39 +170,45 @@ class LearnpathController
->getForm();
*/
$defaults = array();
if (!empty($selectedChoices)) {
$defaults['users'] = $selectedChoices;
}
if (!empty($selectedGroupChoices)) {
$defaults['groups'] = $selectedGroupChoices;
}
$form->setDefaults($defaults);
if ($request->getMethod() == 'POST') {
//Subscribing users
$users = $request->get('users');
$app['orm.em']->getRepository('Entity\EntityCItemProperty')->SubscribedUsersToItem(
'learnpath',
$course,
$session,
$lpId,
$users
);
$userForm = $request->get('user_form');
if (!empty($userForm)) {
$app['orm.em']->getRepository('Entity\EntityCItemProperty')->subscribeUsersToItem(
'learnpath',
$course,
$session,
$lpId,
$users
);
}
//Subscribing groups
$groups = $request->get('groups');
$app['orm.em']->getRepository('Entity\EntityCItemProperty')->SubscribedGroupsToItem(
'learnpath',
$course,
$session,
$lpId,
$groups
);
$groupForm = $request->get('group_form');
if (!empty($groupForm)) {
$app['orm.em']->getRepository('Entity\EntityCItemProperty')->subscribeGroupsToItem(
'learnpath',
$course,
$session,
$lpId,
$groups
);
}
return $app->redirect($url);
} 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');
return new Response($response, 200, array());

Loading…
Cancel
Save