parent
d8539af9ea
commit
53e2b02659
@ -0,0 +1,170 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
use Chamilo\CourseBundle\Entity\CItemProperty; |
||||
use Chamilo\UserBundle\Entity\User; |
||||
|
||||
require_once '../inc/global.inc.php'; |
||||
|
||||
api_protect_course_script(); |
||||
|
||||
$is_allowed_to_edit = api_is_allowed_to_edit(false, true, false, false); |
||||
|
||||
if (!$is_allowed_to_edit) { |
||||
api_not_allowed(true); |
||||
} |
||||
|
||||
$lpId = isset($_GET['lp_id']) ? intval($_GET['lp_id']) : 0; |
||||
|
||||
if (empty($lpId)) { |
||||
api_not_allowed(true); |
||||
} |
||||
|
||||
|
||||
$oLP = new learnpath(api_get_course_id(), $lpId, api_get_user_id()); |
||||
|
||||
$interbreadcrumb[] = array('url' => 'lp_controller.php?action=list', 'name' => get_lang('LearningPaths')); |
||||
$interbreadcrumb[] = array('url' => api_get_self()."?action=build&lp_id=".$oLP->get_id(), 'name' => $oLP->get_name()); |
||||
|
||||
$courseId = api_get_course_int_id(); |
||||
$courseCode = api_get_course_id(); |
||||
|
||||
$url = api_get_self().'?'.api_get_cidreq().'&lp_id='.$lpId; |
||||
|
||||
$lp = new \learnpath($courseCode, $lpId, api_get_user_id()); |
||||
|
||||
$em = Database::getManager(); |
||||
|
||||
$session = null; |
||||
if (!empty($sessionId)) { |
||||
$session = $em->getRepository('ChamiloCoreBundle:Session')->find($sessionId); |
||||
} |
||||
|
||||
// Find course. |
||||
$course = $em->getRepository('ChamiloCoreBundle:Course')->find($courseId); |
||||
|
||||
// Getting subscribe users to the course. |
||||
$subscribedUsers = $em->getRepository('ChamiloCoreBundle:Course')->getSubscribedStudents($course); |
||||
$subscribedUsers = $subscribedUsers->getQuery(); |
||||
$subscribedUsers = $subscribedUsers->execute(); |
||||
|
||||
// Getting all users in a nice format. |
||||
$choices = array(); |
||||
/** @var User $user */ |
||||
foreach ($subscribedUsers as $user) { |
||||
$choices[$user->getUserId()] = $user->getCompleteNameWithClasses(); |
||||
} |
||||
|
||||
// Getting subscribed users to a LP. |
||||
$subscribedUsersInLp = $em->getRepository('ChamiloCourseBundle:CItemProperty')->getUsersSubscribedToItem( |
||||
'learnpath', |
||||
$lpId, |
||||
$course, |
||||
$session |
||||
); |
||||
$selectedChoices = array(); |
||||
foreach ($subscribedUsersInLp as $itemProperty) { |
||||
$selectedChoices[] = $itemProperty->getToUser()->getId(); |
||||
} |
||||
|
||||
//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 = $formUsers->addElement('advmultiselect', 'users', get_lang('Users'), $choices); |
||||
$formUsers->addButtonSave(get_lang('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_column($groupList, 'name', 'id'); |
||||
|
||||
// Subscribed groups to a LP |
||||
$subscribedGroupsInLp = $em->getRepository('ChamiloCourseBundle:CItemProperty')->getGroupsSubscribedToItem( |
||||
'learnpath', |
||||
$lpId, |
||||
$course, |
||||
$session |
||||
); |
||||
|
||||
$selectedGroupChoices = array(); |
||||
/** @var CItemProperty $itemProperty */ |
||||
foreach ($subscribedGroupsInLp as $itemProperty) { |
||||
$selectedGroupChoices[] = $itemProperty->getGroup()->getId(); |
||||
} |
||||
|
||||
$groupMultiSelect = $form->addElement('advmultiselect', 'groups', get_lang('Groups'), $groupChoices); |
||||
|
||||
// submit button |
||||
$form->addButtonSave(get_lang('Save')); |
||||
|
||||
$defaults = array(); |
||||
if (!empty($selectedGroupChoices)) { |
||||
$defaults['groups'] = $selectedGroupChoices; |
||||
} |
||||
$form->setDefaults($defaults); |
||||
|
||||
$tpl = new Template(); |
||||
|
||||
$currentUser = $em->getRepository('ChamiloUserBundle:User')->find(api_get_user_id()); |
||||
|
||||
if ($form->validate()) { |
||||
$values = $form->getSubmitValues(); |
||||
|
||||
// Subscribing users |
||||
$users = isset($values['users']) ? $values['users'] : []; |
||||
$userForm = isset($values['user_form']) ? $values['user_form'] : []; |
||||
|
||||
if (!empty($userForm)) { |
||||
$em->getRepository('ChamiloCourseBundle:CItemProperty')->subscribeUsersToItem( |
||||
$currentUser, |
||||
'learnpath', |
||||
$course, |
||||
$session, |
||||
$lpId, |
||||
$users |
||||
); |
||||
} |
||||
|
||||
// Subscribing groups |
||||
$groups = isset($values['groups']) ? $values['groups'] : []; |
||||
$groupForm = isset($values['group_form']) ? $values['group_form'] : []; |
||||
|
||||
if (!empty($groupForm)) { |
||||
$em->getRepository('ChamiloCourseBundle:CItemProperty')->subscribeGroupsToItem( |
||||
$currentUser, |
||||
'learnpath', |
||||
$course, |
||||
$session, |
||||
$lpId, |
||||
$groups |
||||
); |
||||
} |
||||
|
||||
header("Location: $url"); |
||||
exit; |
||||
} else { |
||||
$tpl->assign('form_users', $formUsers->toHtml()); |
||||
$tpl->assign('form_groups', $form->toHtml()); |
||||
} |
||||
|
||||
$layout = $tpl->get_template('learnpath/subscribe_users.tpl'); |
||||
$tpl->display($layout); |
||||
@ -0,0 +1,49 @@ |
||||
{% extends template ~ "/layout/layout_1_col.tpl" %} |
||||
{% block content %} |
||||
{# |
||||
<h2>{{ 'SubscribeUsersToLp' | trans }}</h2> |
||||
<script> |
||||
$().ready(function() { |
||||
$('#add').click(function() { |
||||
return !$('#{{ form.origin.vars.id }} option:selected').remove().appendTo('#{{ form.destination.vars.id }}'); |
||||
}); |
||||
|
||||
$('#remove').click(function() { |
||||
return !$('#{{ form.destination.vars.id }} option:selected').remove().appendTo('#{{ form.origin.vars.id }}'); |
||||
}); |
||||
|
||||
$('#send').click(function() { |
||||
$("#{{ form.destination.vars.id }}").each(function(){ |
||||
$("#{{ form.destination.vars.id }} option").attr("selected","selected"); |
||||
}); |
||||
}); |
||||
}); |
||||
</script> |
||||
<form method="POST" {{ form_enctype(form) }}> |
||||
{{ form_errors(form) }} |
||||
<div class="row"> |
||||
<div class="span4"> |
||||
{{ form_label(form.origin) }} |
||||
{{ form_widget(form.origin) }} |
||||
</div> |
||||
<div class="span1"> |
||||
<br /> |
||||
<button id="add" class="btn btn-block"><i class="icon-arrow-right icon-large"></i></button> |
||||
<button id="remove" class="btn btn-block"><i class="icon-arrow-left icon-large"></i></button> |
||||
</div> |
||||
<div class="span4"> |
||||
{{ form_label(form.destination) }} |
||||
{{ form_widget(form.destination) }} |
||||
</div> |
||||
</div> |
||||
<div class="row"> |
||||
<div class="span12"> |
||||
<button id="send" type="submit" class="btn btn-primary"><i class="icon-arrow-lef icon-large"></i>{{ 'Save' | trans }}</button> |
||||
</div> |
||||
</div> |
||||
{{ form_rest(form) }} |
||||
</form> |
||||
#} |
||||
{{ form_users }} |
||||
{{ form_groups }} |
||||
{% endblock %} |
||||
@ -0,0 +1,326 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Entity\Repository; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Course; |
||||
use Chamilo\CoreBundle\Entity\Group; |
||||
use Chamilo\CoreBundle\Entity\Session; |
||||
use Chamilo\CourseBundle\Entity\CItemProperty; |
||||
use Doctrine\ORM\EntityRepository; |
||||
|
||||
/** |
||||
* Class ItemPropertyRepository |
||||
* |
||||
*/ |
||||
class ItemPropertyRepository extends EntityRepository |
||||
{ |
||||
/** |
||||
* |
||||
* Get users subscribed to a item LP, Document, etc (item_property) |
||||
* |
||||
* @param $tool learnpath | document | etc |
||||
* @param $itemId |
||||
* @param Course $course |
||||
* @param int $sessionId |
||||
* @param int $groupId |
||||
* |
||||
* @return \Doctrine\ORM\QueryBuilder |
||||
*/ |
||||
public function getUsersSubscribedToItem( |
||||
$tool, |
||||
$itemId, |
||||
Course $course, |
||||
Session $session = null, |
||||
Group $group = null |
||||
) { |
||||
$criteria = array( |
||||
'tool' => $tool, |
||||
'lasteditType' => 'LearnpathSubscription', |
||||
'ref' => $itemId, |
||||
'course' => $course, |
||||
'session' => $session, |
||||
'group' => $group, |
||||
); |
||||
|
||||
return $this->findBy($criteria); |
||||
} |
||||
|
||||
/** |
||||
* Get Groups subscribed to a item: LP, Doc, etc |
||||
* @param $tool learnpath | document | etc |
||||
* @param $itemId |
||||
* @param Course $course |
||||
* @param Session $session |
||||
* @return array |
||||
*/ |
||||
public function getGroupsSubscribedToItem( |
||||
$tool, |
||||
$itemId, |
||||
Course $course, |
||||
Session $session = null |
||||
) { |
||||
$criteria = array( |
||||
'tool' => $tool, |
||||
'lasteditType' => 'LearnpathSubscription', |
||||
'ref' => $itemId, |
||||
'course' => $course, |
||||
'session' => $session, |
||||
'toUser' => null, |
||||
); |
||||
|
||||
return $this->findBy($criteria); |
||||
} |
||||
|
||||
/** |
||||
* Subscribe groups to a LP, doc (itemproperty) |
||||
* @param User $currentUser |
||||
* @param $tool learnpath | document | etc |
||||
* @param Course $course |
||||
* @param Session $session |
||||
* @param $itemId |
||||
* @param array $newList |
||||
*/ |
||||
public function subscribeGroupsToItem( |
||||
$currentUser, |
||||
$tool, |
||||
Course $course, |
||||
Session $session = null, |
||||
$itemId, |
||||
$newList = array() |
||||
) { |
||||
$em = $this->getEntityManager(); |
||||
$groupsSubscribedToItem = $this->getGroupsSubscribedToItem( |
||||
$tool, |
||||
$itemId, |
||||
$course, |
||||
$session |
||||
); |
||||
|
||||
$alreadyAdded = array(); |
||||
if ($groupsSubscribedToItem) { |
||||
/** @var CItemProperty $itemProperty */ |
||||
foreach ($groupsSubscribedToItem as $itemProperty) { |
||||
$alreadyAdded[] = $itemProperty->getGroup()->getId(); |
||||
} |
||||
} |
||||
|
||||
$toDelete = $alreadyAdded; |
||||
|
||||
if (!empty($newList)) { |
||||
$toDelete = array_diff($alreadyAdded, $newList); |
||||
} |
||||
|
||||
if ($toDelete) { |
||||
$this->unsubscribeGroupsToItem( |
||||
$tool, |
||||
$course, |
||||
$session, |
||||
$itemId, |
||||
$toDelete, |
||||
true |
||||
); |
||||
} |
||||
|
||||
foreach ($newList as $groupId) { |
||||
if (!in_array($groupId, $alreadyAdded)) { |
||||
$item = new CItemProperty($course); |
||||
$groupObj = $em->find('ChamiloCourseBundle:CGroupInfo', $groupId); |
||||
$item->setGroup($groupObj); |
||||
$item->setTool($tool); |
||||
$item->setRef($itemId); |
||||
$item->setInsertUser($currentUser); |
||||
|
||||
if (!empty($session)) { |
||||
$item->setSession($session); |
||||
} |
||||
$item->setLasteditType('LearnpathSubscription'); |
||||
$item->setVisibility('1'); |
||||
$em->persist($item); //$em is an instance of EntityManager |
||||
} |
||||
|
||||
//Adding users from this group to the item |
||||
/*$users = \GroupManager::getStudentsAndTutors($groupId); |
||||
$newUserList = array(); |
||||
if (!empty($users)) { |
||||
foreach ($users as $user) { |
||||
$newUserList[] = $user['user_id']; |
||||
} |
||||
$this->subscribeUsersToItem( |
||||
$currentUser, |
||||
'learnpath', |
||||
$course, |
||||
$session, |
||||
$itemId, |
||||
$newUserList |
||||
); |
||||
}*/ |
||||
} |
||||
|
||||
$em->flush(); |
||||
} |
||||
|
||||
/** |
||||
* Unsubscribe groups to item |
||||
* @param $tool |
||||
* @param Course $course |
||||
* @param Session $session |
||||
* @param $itemId |
||||
* @param $groups |
||||
*/ |
||||
public function unsubscribeGroupsToItem( |
||||
$tool, |
||||
Course $course, |
||||
Session $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, |
||||
'group' => $groupId, |
||||
)); |
||||
if ($item) { |
||||
$em->remove($item); |
||||
} |
||||
|
||||
if ($unsubscribeUserToo) { |
||||
|
||||
//Adding users from this group to the item |
||||
$users = \GroupManager::getStudentsAndTutors($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 User $currentUser |
||||
* @param $tool |
||||
* @param Course $course |
||||
* @param Session $session |
||||
* @param $itemId |
||||
* @param array $newUserList |
||||
*/ |
||||
public function subscribeUsersToItem( |
||||
$currentUser, |
||||
$tool, |
||||
Course $course, |
||||
Session $session = null, |
||||
$itemId, |
||||
$newUserList = array() |
||||
) { |
||||
$em = $this->getEntityManager(); |
||||
$user = $em->getRepository('ChamiloUserBundle:User'); |
||||
|
||||
$usersSubscribedToItem = $this->getUsersSubscribedToItem( |
||||
$tool, |
||||
$itemId, |
||||
$course, |
||||
$session |
||||
); |
||||
|
||||
$alreadyAddedUsers = array(); |
||||
if ($usersSubscribedToItem) { |
||||
/** @var CItemProperty $itemProperty */ |
||||
foreach ($usersSubscribedToItem as $itemProperty) { |
||||
$alreadyAddedUsers[] = $itemProperty->getToUser()->getId(); |
||||
} |
||||
} |
||||
|
||||
$usersToDelete = $alreadyAddedUsers; |
||||
|
||||
if (!empty($newUserList)) { |
||||
$usersToDelete = array_diff($alreadyAddedUsers, $newUserList); |
||||
} |
||||
|
||||
if ($usersToDelete) { |
||||
$this->unsubcribeUsersToItem( |
||||
$tool, |
||||
$course, |
||||
$session, |
||||
$itemId, |
||||
$usersToDelete |
||||
); |
||||
} |
||||
|
||||
foreach ($newUserList as $userId) { |
||||
if (!in_array($userId, $alreadyAddedUsers)) { |
||||
$userObj = $user->find($userId); |
||||
|
||||
$item = new CItemProperty($course); |
||||
$item |
||||
->setToUser($userObj) |
||||
->setTool($tool) |
||||
->setInsertUser($currentUser) |
||||
->setRef($itemId); |
||||
|
||||
if (!empty($session)) { |
||||
$item->setSession($session); |
||||
} |
||||
$item->setLasteditType('LearnpathSubscription'); |
||||
$item->setVisibility('1'); |
||||
$em->persist($item); //$em is an instance of EntityManager |
||||
} |
||||
} |
||||
|
||||
$em->flush(); |
||||
} |
||||
|
||||
/** |
||||
* Unsubscribe users to item |
||||
* |
||||
* @param $tool |
||||
* @param Course $course |
||||
* @param Session $session |
||||
* @param $itemId |
||||
* @param $usersToDelete |
||||
*/ |
||||
public function unsubcribeUsersToItem( |
||||
$tool, |
||||
Course $course, |
||||
Session $session = null, |
||||
$itemId, |
||||
$usersToDelete |
||||
) { |
||||
$em = $this->getEntityManager(); |
||||
|
||||
if (!empty($usersToDelete)) { |
||||
foreach ($usersToDelete as $userId) { |
||||
$item = $this->findOneBy( |
||||
array( |
||||
'tool' => $tool, |
||||
'session' => $session, |
||||
'ref' => $itemId, |
||||
'toUser' => $userId, |
||||
) |
||||
); |
||||
if ($item) { |
||||
$em->remove($item); |
||||
} |
||||
} |
||||
$em->flush(); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue