Skip to content

Commit

Permalink
add sessions support
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-cortizas-ponte committed Dec 5, 2021
1 parent 3af197e commit da86979
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 2 deletions.
5 changes: 3 additions & 2 deletions main/inc/lib/plugin.class.php
Expand Up @@ -1084,7 +1084,8 @@ protected function createLinkToCourseTool(
$name,
$courseId,
$iconName = null,
$link = null
$link = null,
$sessionId = 0
) {
if (!$this->addCourseTool) {
return null;
Expand Down Expand Up @@ -1121,7 +1122,7 @@ protected function createLinkToCourseTool(
->setAddedTool(false)
->setTarget('_self')
->setCategory('plugin')
->setSessionId(0);
->setSessionId($sessionId);

$em->persist($tool);
$em->flush();
Expand Down
32 changes: 32 additions & 0 deletions plugin/ims_lti/Entity/ImsLtiTool.php
Expand Up @@ -4,6 +4,7 @@
namespace Chamilo\PluginBundle\Entity\ImsLti;

use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CoreBundle\Entity\GradebookEvaluation;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
Expand Down Expand Up @@ -87,6 +88,13 @@ class ImsLtiTool
* @ORM\JoinColumn(name="c_id", referencedColumnName="id")
*/
private $course = null;
/**
* @var Session|null
*
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session")
* @ORM\JoinColumn(name="session_id", referencedColumnName="id")
*/
private $session = null;
/**
* @var GradebookEvaluation|null
*
Expand Down Expand Up @@ -435,6 +443,30 @@ public function setCourse(Course $course = null)
return $this;
}

/**
* Get session.
*
* @return Session|null
*/
public function getSession()
{
return $this->session;
}

/**
* Set session.
*
* @param Session|null $course
*
* @return ImsLtiTool
*/
public function setSession(Session $session = null)
{
$this->session = $session;

return $this;
}

/**
* Get gradebookEval.
*
Expand Down
30 changes: 30 additions & 0 deletions plugin/ims_lti/ImsLtiPlugin.php
Expand Up @@ -367,6 +367,36 @@ public function addCourseTool(Course $course, ImsLtiTool $ltiTool, $isVisible =
$em->flush();
}

/**
* Add the course session tool.
*
* @param Course $course
* @param Session $session
* @param ImsLtiTool $ltiTool
* @param bool $isVisible
*
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function addCourseSessionTool(Course $course, Session $session, ImsLtiTool $ltiTool, $isVisible = true)
{
$cTool = $this->createLinkToCourseTool(
$ltiTool->getName(),
$course->getId(),
null,
self::generateToolLink($ltiTool),
$session->getId()
);
$cTool
->setTarget(
$ltiTool->getDocumentTarget() === 'iframe' ? '_self' : '_blank'
)
->setVisibility($isVisible);

$em = Database::getManager();
$em->persist($cTool);
$em->flush();
}

/**
* @return string
*/
Expand Down
161 changes: 161 additions & 0 deletions plugin/ims_lti/multiply_session.php
@@ -0,0 +1,161 @@
<?php
/* For licensing terms, see /license.txt */

use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$cidReset = true;

require_once __DIR__.'/../../main/inc/global.inc.php';

api_protect_admin_script(false);

$plugin = ImsLtiPlugin::create();
$webPluginPath = api_get_path(WEB_PLUGIN_PATH).'ims_lti/';

$em = Database::getManager();

try {
if ($plugin->get('enabled') !== 'true') {
throw new Exception(get_lang('NotAllowed'));
}

$sessionId = isset($_REQUEST['session']);

if (empty($sessionId)) {
api_not_allowed();
}

$request = Request::createFromGlobals();
/** @var ImsLtiTool $tool */
$tool = $em->find('ChamiloPluginBundle:ImsLti\ImsLtiTool', $request->query->getInt('id'));

if (!$tool) {
throw new Exception($plugin->get_lang('NoTool'));
}

if ($tool->getParent()) {
throw new Exception($plugin->get_lang('NoAllowed'));
}

$content = '';

$courses = ImsLtiPlugin::getCoursesForParentTool($tool);

$slctCourses = [];

/** @var \Chamilo\CoreBundle\Entity\Course $course */
foreach ($courses as $course) {
$slctCourses[$course->getId()] = $course->getName();
}

$selectedCoursesIds = array_keys($slctCourses);

$form = new FormValidator('frm_multiply', 'post', api_get_self().'?id='.$tool->getId());
$form->addLabel($plugin->get_lang('Tool'), $tool->getName());
$form->addSelectAjax(
'courses',
get_lang('Courses'),
$slctCourses,
[
'url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?'.http_build_query(
[
'a' => 'search_course_by_session',
'session_id' => $sessionId
]
),
'multiple' => true
]
);
$form->addCheckBox('tool_visible', get_lang('SetVisible'), get_lang('ToolIsNowVisible'));
$form->addButtonExport(get_lang('Save'));

if ($form->validate()) {
$em = Database::getManager();
$formValues = $form->exportValues();
$formValues['courses'] = empty($formValues['courses']) ? [] : $formValues['courses'];
$formValues['tool_visible'] = !empty($formValues['tool_visible']);

$courseIdsToDelete = array_diff($selectedCoursesIds, $formValues['courses']);
$newSelectedCourseIds = array_diff($formValues['courses'], $selectedCoursesIds);

if ($courseIdsToDelete) {
$toolLinks = [];

/** @var ImsLtiTool $childInCourse */
foreach ($tool->getChildrenInCourses($courseIdsToDelete) as $childInCourse) {
$toolLinks[] = "ims_lti/start.php?id={$childInCourse->getId()}";

$em->remove($childInCourse);
}

$em->flush();

if (!empty($toolLinks)) {
$em
->createQuery(
"DELETE FROM ChamiloCourseBundle:CTool ct WHERE ct.category = :category AND ct.link IN (:links) AND ct.session_id = :sessionId"
)
->execute(['category' => 'plugin', 'links' => $toolLinks, 'sessionId' => $sessionId]);
}
}

if ($newSelectedCourseIds) {
foreach ($newSelectedCourseIds as $newSelectedCourseId) {
$newSelectedCourse = api_get_course_entity($newSelectedCourseId);
$session = api_get_session_entity($sessionId);

$newTool = clone $tool;
$newTool->setParent($tool);
$newTool->setCourse($newSelectedCourse);
$newTool->setSession($session);

$em->persist($newTool);
$em->flush();

if ($tool->isActiveDeepLinking()) {
continue;
}

$plugin->addCourseSessionTool(
$newSelectedCourse,
$session,
$newTool,
$formValues['tool_visible']
);
}
}

Display::addFlash(
Display::return_message(get_lang('ItemUpdated'))
);

header('Location: '.api_get_path(WEB_PLUGIN_PATH).'ims_lti/admin.php');
exit;
}

$form->setDefaults(
[
'courses' => $selectedCoursesIds,
'tool_visible' => true,
]
);
$form->protect();

$content = $form->returnForm();

$interbreadcrumb[] = ['url' => api_get_path(WEB_CODE_PATH).'admin/index.php', 'name' => get_lang('PlatformAdmin')];
$interbreadcrumb[] = ['url' => api_get_path(WEB_PLUGIN_PATH).'ims_lti/admin.php', 'name' => $plugin->get_title()];

$template = new Template($plugin->get_lang('AddInCourses'));
$template->assign('header', $plugin->get_lang('AddInCourses'));
$template->assign('content', $content);
$template->display_one_col_template();
} catch (Exception $exception) {
Display::addFlash(
Display::return_message($exception->getMessage(), 'error')
);

header('Location: '.api_get_path(WEB_PLUGIN_PATH).'ims_lti/admin.php');
}
90 changes: 90 additions & 0 deletions plugin/ims_lti/session.php
@@ -0,0 +1,90 @@
<?php
/* For licensing terms, see /license.txt */

use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$cidReset = true;

require_once __DIR__.'/../../main/inc/global.inc.php';

api_protect_admin_script(false);

$plugin = ImsLtiPlugin::create();
$webPluginPath = api_get_path(WEB_PLUGIN_PATH).'ims_lti/';

$em = Database::getManager();

try {
if ($plugin->get('enabled') !== 'true') {
throw new Exception(get_lang('NotAllowed'));
}

$request = Request::createFromGlobals();
/** @var ImsLtiTool $tool */
$tool = $em->find('ChamiloPluginBundle:ImsLti\ImsLtiTool', $request->query->getInt('id'));

if (!$tool) {
throw new Exception($plugin->get_lang('NoTool'));
}

if ($tool->getParent()) {
throw new Exception($plugin->get_lang('NoAllowed'));
}

$content = '';

$form = new FormValidator('frm_multiply', 'post', api_get_self().'?id='.$tool->getId());
$form->addLabel($plugin->get_lang('Tool'), $tool->getName());
$form->addSelectAjax(
'sessions',
get_lang('Sessions'),
$slctCourses,
[
'url' => api_get_path(WEB_AJAX_PATH).'session.ajax.php?'.http_build_query(
[
'a' => 'search_session'
]
),
'multiple' => false
]
);
$form->addButtonExport(get_lang('Next'));

if ($form->validate()) {
$em = Database::getManager();
$formValues = $form->exportValues();
$formValues['sessions'] = empty($formValues['sessions']) ? [] : $formValues['sessions'];

if (!$formValues['sessions']) {
Display::addFlash(
Display::return_message($plugin->get_lang('NeedToSelectASession'), 'error', false)
);
header('Location:'.api_get_self());
exit;
}

header('Location: '.api_get_path(WEB_PLUGIN_PATH).'ims_lti/multiply_session.php?'.$formValues['sessions'][0]);

exit;
}

$form->protect();

$content = $form->returnForm();

$interbreadcrumb[] = ['url' => api_get_path(WEB_CODE_PATH).'admin/index.php', 'name' => get_lang('PlatformAdmin')];
$interbreadcrumb[] = ['url' => api_get_path(WEB_PLUGIN_PATH).'ims_lti/admin.php', 'name' => $plugin->get_title()];

$template = new Template($plugin->get_lang('AddInCourses'));
$template->assign('header', $plugin->get_lang('AddInCourses'));
$template->assign('content', $content);
$template->display_one_col_template();
} catch (Exception $exception) {
Display::addFlash(
Display::return_message($exception->getMessage(), 'error')
);

header('Location: '.api_get_path(WEB_PLUGIN_PATH).'ims_lti/admin.php');
}

0 comments on commit da86979

Please sign in to comment.