Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Learnpath: Enable survey access in LP without invitation - refs #5713 #5766

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion public/main/inc/lib/api.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2438,7 +2438,12 @@ function api_check_password($password)
*/
function api_get_session_id()
{
return (int) Session::read('sid', 0);
$sessionId = (int) Session::read('sid', 0);
if (empty($sessionId) && !empty($_REQUEST['sid'])) {
$sessionId = (int) $_REQUEST['sid'];
}

return $sessionId;
}

/**
Expand Down
24 changes: 23 additions & 1 deletion public/main/lp/learnpath.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Chamilo\CourseBundle\Entity\CLpRelUser;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
use Chamilo\CourseBundle\Entity\CSurvey;
use Chamilo\CourseBundle\Repository\CLpRelUserRepository;
use Chamilo\CourseBundle\Component\CourseCopy\CourseArchiver;
use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
Expand Down Expand Up @@ -2767,7 +2768,7 @@ public function get_link($type = 'http', $item_id = 0, $provided_toc = false)
// then change the lp type to thread it as a normal Chamilo LP not a SCO.
if (in_array(
$lp_item_type,
['quiz', 'document', 'final_item', 'link', 'forum', 'thread', 'student_publication']
['quiz', 'document', 'final_item', 'link', 'forum', 'thread', 'student_publication', 'survey']
)
) {
$lp_type = CLp::LP_TYPE;
Expand Down Expand Up @@ -8015,6 +8016,27 @@ public static function rl_get_resource_link_for_learnpath(
}

return $main_dir_path.'work/work.php?'.api_get_cidreq().'&id='.$rowItem->getPath().'&'.$extraParams;
case TOOL_SURVEY:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CASE body must start on the line following the statement


$surveyId = (int) $id;
$repo = Container::getSurveyRepository();
if (!empty($surveyId)) {
/** @var CSurvey $survey */
$survey = $repo->find($surveyId);
$autoSurveyLink = SurveyUtil::generateFillSurveyLink(
$survey,
'auto',
api_get_course_entity($course_id),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable "course_id" is not in valid camel caps format

$session_id
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable "session_id" is not in valid camel caps format

);
$lpParams = [
'lp_id' => $learningPathId,
'lp_item_id' => $id_in_path,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable "id_in_path" is not in valid camel caps format

'origin' => 'learnpath',
];

return $autoSurveyLink.'&'.http_build_query($lpParams).'&'.$extraParams;
}
}

return $link;
Expand Down
2 changes: 1 addition & 1 deletion public/main/lp/lp_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@
'#content_id, #content_id_blank',
$itemType,
'function () {
var arr = ["link", "sco", "xapi", "quiz", "h5p", "forum"];
var arr = ["link", "sco", "xapi", "quiz", "h5p", "forum", "survey"];

return $.inArray(olms.lms_item_type, arr) !== -1;
}'
Expand Down
75 changes: 58 additions & 17 deletions public/main/survey/fillsurvey.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
$courseId = $courseInfo['real_id'];
$userInfo = api_get_user_info();
$sessionId = isset($_GET['sid']) ? (int) $_GET['sid'] : api_get_session_id();
$lpItemId = isset($_GET['lp_item_id']) ? (int) $_GET['lp_item_id'] : 0;
$allowSurveyInLp = true;

if (!empty($userInfo)) {
$interbreadcrumb[] = [
Expand Down Expand Up @@ -89,6 +91,16 @@
$surveyId = $survey->getIid();
$invitationCode = $_GET['invitationcode'] ?? null;

$lpItemCondition = '';
if ($allowSurveyInLp && !empty($lpItemId)) {
$lpItemCondition = " AND c_lp_item_id = $lpItemId";
}

$sessionCondition = '';
if (true === api_get_setting('survey.show_surveys_base_in_sessions')) {
$sessionCondition = api_get_session_condition($sessionId);
}

/*$surveyCode = isset($_GET['scode']) ? Database::escape_string($_GET['scode']) : '';
if ('' != $surveyCode) {
// Firstly we check if this survey is ready for anonymous use:
Expand Down Expand Up @@ -122,7 +134,9 @@
$userid,
$surveyCode,
$courseId,
$sessionId
$sessionId,
0,
$lpItemId
);
$lastInvitation = current($invitations);

Expand All @@ -142,7 +156,9 @@
FROM $table_survey_invitation
WHERE
c_id = $courseId AND
invitation_code = '".Database::escape_string($autoInvitationCode)."'";
invitation_code = '".Database::escape_string($autoInvitationCode)."'
$sessionCondition
$lpItemCondition";
$result = Database::query($sql);
$now = api_get_utc_datetime();
if (0 == Database::num_rows($result)) {
Expand All @@ -153,7 +169,7 @@
'invitation_code' => $autoInvitationCode,
'invitation_date' => $now,
'answered' => 0,
'c_lp_item_id' => 0,
'c_lp_item_id' => $lpItemId,
];
Database::insert($table_survey_invitation, $params);
}
Expand All @@ -167,7 +183,9 @@
$sql = "SELECT * FROM $table_survey_invitation
WHERE
c_id = $courseId AND
invitation_code = '".Database::escape_string($invitationCode)."'";
invitation_code = '".Database::escape_string($invitationCode)."'
$sessionCondition
$lpItemCondition";
$result = Database::query($sql);
if (Database::num_rows($result) < 1) {
api_not_allowed(true, get_lang('Wrong invitation code'));
Expand Down Expand Up @@ -256,7 +274,8 @@
SurveyUtil::remove_answer(
$survey_invitation['user_id'],
$surveyId,
$survey_question_id
$survey_question_id,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable "survey_question_id" is not in valid camel caps format

$lpItemId
);

foreach ($value as $answer_key => &$answer_value) {
Expand All @@ -273,7 +292,9 @@
$survey,
$question,
$option_id,
$option_value
$option_value,
'',
$lpItemId
);
}
} else {
Expand All @@ -298,7 +319,8 @@
SurveyUtil::remove_answer(
$survey_invitation['user_id'],
$surveyId,
$survey_question_id
$survey_question_id,
$lpItemId
);

SurveyUtil::saveAnswer(
Expand All @@ -307,7 +329,8 @@
$question,
$value,
$option_value,
$other
$other,
$lpItemId
);
}
}
Expand Down Expand Up @@ -358,15 +381,24 @@
SurveyUtil::remove_answer(
$survey_invitation['user_id'],
$survey_invitation['survey_id'],
$survey_question_id
$survey_question_id,
api_get_course_int_id(),
$lpItemId
);


$surveyId = (int) $survey_invitation['survey_id'];
$repo = Container::getSurveyRepository();
$survey = $repo->find($surveyId);

SurveyUtil::saveAnswer(
$survey_invitation['user_id'],
$survey_invitation['survey_id'],
api_get_user_entity($survey_invitation['user_id']),
$survey,
$questionList[$survey_question_id],
$value,
$option_value
$option_value,
'',
$lpItemId
);
}
}
Expand Down Expand Up @@ -398,12 +430,16 @@
}
}

$url = api_get_self().'?cid='.$courseId.'&sid='.$sessionId;
$url = api_get_self().'?'.api_get_cidreq();
$listQueryParams = explode('&', $_SERVER['QUERY_STRING']);
foreach ($listQueryParams as $param) {
$url .= '&'.Security::remove_XSS($param);
}

if (!empty($lpItemId)) {
$url .= '&lp_item_id='.$lpItemId;
}

// We use the same form as in auth/profile.php
$form = new FormValidator('profile', 'post', $url);
if (api_is_western_name_order()) {
Expand Down Expand Up @@ -611,10 +647,10 @@
if (isset($_POST['finish_survey'])) {
echo Display::return_message(get_lang('You have finished this survey.'), 'confirm');
echo Security::remove_XSS($survey->getSurveythanks());
SurveyManager::updateSurveyAnswered($survey, $survey_invitation['user_id']);
SurveyManager::updateSurveyAnswered($survey, $survey_invitation['user_id'], $lpItemId);
SurveyUtil::flagSurveyAsAnswered($survey->getCode(), $survey_invitation['c_id']);

if ($courseInfo && !api_is_anonymous()) {
if ($courseInfo && !api_is_anonymous() && 'learnpath' !== api_get_origin()) {
echo '<br /><br />';
echo Display::toolbarButton(
get_lang('Return to Course Homepage'),
Expand Down Expand Up @@ -1179,7 +1215,7 @@
$g_cr = isset($_GET['cidReq']) ? Security::remove_XSS($_GET['cidReq']) : '';
$p_l = isset($_POST['language']) ? Security::remove_XSS($_POST['language']) : '';
$add_parameters = isset($_GET['user_id']) ? '&user_id='.intval($_GET['user_id']) : '';
$url = api_get_self().'?cid='.$courseId.'&sid='.$sessionId.$add_parameters.
$url = api_get_self().'?'.api_get_cidreq().$add_parameters.
'&course='.$g_c.
'&invitationcode='.$g_ic.
'&show='.$show.
Expand All @@ -1189,6 +1225,11 @@
$lang = Security::remove_XSS($_GET['language']);
$url .= '&language='.$lang;
}

if (!empty($lpItemId)) {
$url .= '&lp_item_id='.$lpItemId;
}

$form = new FormValidator(
'question',
'post',
Expand Down Expand Up @@ -1256,7 +1297,7 @@
}
$form->addHtml('<div>'.Security::remove_XSS($question['survey_question']).'</div> ');

$userAnswerData = SurveyUtil::get_answers_of_question_by_user($question['survey_id'], $question['question_id']);
$userAnswerData = SurveyUtil::get_answers_of_question_by_user($question['survey_id'], $question['question_id'], $lpItemId);
$finalAnswer = null;

if (!empty($userAnswerData[$user_id])) {
Expand Down
79 changes: 54 additions & 25 deletions public/main/survey/survey.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ public static function empty_survey($surveyId, $courseId = 0)
/**
* Updates c_survey.answered: number of people who have taken the survey (=filled at least one question).
*/
public static function updateSurveyAnswered(CSurvey $survey, $user)
public static function updateSurveyAnswered(CSurvey $survey, $user, $lpItemId = 0): void
{
$em = Database::getManager();
$surveyId = $survey->getIid();
Expand All @@ -780,17 +780,27 @@ public static function updateSurveyAnswered(CSurvey $survey, $user)
$em->persist($survey);
$em->flush();

$lpItemCondition = '';
if (!empty($lpItemId)) {
$lpItemCondition = " AND c_lp_item_id = $lpItemId";
}
$sessionCondition = '';
if (true === api_get_configuration_value('show_surveys_base_in_sessions')) {
$sessionCondition = api_get_session_condition($sessionId);
}

$table = Database::get_course_table(TABLE_SURVEY_INVITATION);
// Storing that the user has finished the survey.
$sql = "UPDATE $table
SET
answered_at = '".api_get_utc_datetime()."',
answered = 1
WHERE
c_id = $courseId AND
session_id = $sessionId AND
user_id ='".Database::escape_string($user)."' AND
survey_id ='".$surveyId."'";
survey_id ='".$surveyId."'
$sessionCondition
$lpItemCondition";
Database::query($sql);
}

Expand Down Expand Up @@ -2154,39 +2164,39 @@ public static function checkTimeAvailability(?CSurvey $survey): void
}
}

/**
* @param int $userId
* @param string $surveyCode
* @param int $courseId
* @param int $sessionId
* @param int $groupId
*
* @return array|CSurveyInvitation[]
*/
public static function getUserInvitationsForSurveyInCourse(
$userId,
$surveyCode,
$courseId,
$sessionId = 0,
$groupId = 0
) {
int $userId,
string $surveyCode,
int $courseId,
int $sessionId = 0,
int $groupId = 0,
int $lpItemId = 0
): array {

$em = Database::getManager();
$invitationRepo = $em->getRepository(CSurveyInvitation::class);
$surveyRepo = $em->getRepository(CSurvey::class);
$survey = $surveyRepo->findBy(['code' => $surveyCode]);

$criteria = [
'user' => api_get_user_entity($userId),
'course' => api_get_course_entity($courseId),
'session' => api_get_session_entity($sessionId),
'group' => api_get_group_entity($groupId),
'survey' => $survey,
];

if (is_int($lpItemId) && $lpItemId > 0) {
$criteria['lpItemId'] = $lpItemId;
}

return $invitationRepo->findBy(
[
'user' => api_get_user_entity($userId),
'course' => api_get_course_entity($courseId),
'session' => api_get_session_entity($sessionId),
'group' => api_get_group_entity($groupId),
'survey' => $survey,
],
$criteria,
['invitationDate' => 'DESC']
);
}


/**
* @param array $userInfo
* @param int $answered (1 = answered 0 = not answered)
Expand Down Expand Up @@ -2352,4 +2362,23 @@ public static function sendToTutors(CSurvey $survey)

return false;
}

public static function getInvitationsAnswered(
$surveyCode,
$courseId,
$sessionId = 0
): array
{
$invitationRepo = Database::getManager()->getRepository(CSurveyInvitation::class);

return $invitationRepo->findBy(
[
'cId' => $courseId,
'sessionId' => $sessionId,
'answered' => true,
'surveyCode' => $surveyCode,
],
['invitationDate' => 'DESC']
);
}
}
Loading
Loading