Skip to content

Commit

Permalink
Add student_follow_page_add_LP_invisible_checkbox conf setting - refs…
Browse files Browse the repository at this point in the history
… BT#18671

Prepend a column in student LPs table to display a checkbox to select the LP category and its LPs. Requires DB changes:

INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, default_value, field_order, visible_to_self, visible_to_others, changeable, filter, created_at) VALUES
(20, 13, 'invisible', 'Invisible', '', 0, 1, 0, 0, 0, NOW());
  • Loading branch information
AngelFQC committed Apr 27, 2021
1 parent a67f006 commit 3351d26
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 8 deletions.
29 changes: 29 additions & 0 deletions main/inc/ajax/student_follow_page.ajax.php
Expand Up @@ -20,6 +20,12 @@
$httpRequest->query->getInt('lp_view')
);
break;
case 'views_invisible':
processViewsInvisible(
$httpRequest->request->get('chkb_view') ?: [],
$httpRequest->request->getBoolean('state')
);
break;
}

function displayForm(int $lpViewId)
Expand Down Expand Up @@ -105,3 +111,26 @@ function displayForm(int $lpViewId)
})
})</script>";
}

function processViewsInvisible(array $lpViewsIds, bool $state)
{
$lpViewsIds = array_map('intval', $lpViewsIds);
$lpViewsIds = array_filter($lpViewsIds);

if (empty($lpViewsIds)) {
return;
}

foreach ($lpViewsIds as $lpViewId) {
$extraFieldValue = new ExtraFieldValue('lp_view');
$extraFieldValue->save(
[
'variable' => StudentFollowPage::VARIABLE_INVISIBLE,
'item_id' => $lpViewId,
'comment' => json_encode(['user' => api_get_user_id(), 'datetime' => api_get_utc_datetime()]),
'value' => !$state,
]
);
}

}
69 changes: 61 additions & 8 deletions main/inc/lib/StudentFollowPage.php
Expand Up @@ -10,6 +10,7 @@
class StudentFollowPage
{
const VARIABLE_ACQUISITION = 'acquisition';
const VARIABLE_INVISIBLE = 'invisible';

public static function getLpSubscription(
array $lpInfo,
Expand Down Expand Up @@ -61,14 +62,7 @@ public static function getLpAcquisition(
int $courseId,
int $sessionId = 0
): string {
$tblLpView = Database::get_course_table(TABLE_LP_VIEW);

$sessionCondition = api_get_session_condition($sessionId);

$sql = "SELECT iid FROM $tblLpView
WHERE c_id = $courseId AND lp_id = {$lpInfo['iid']} AND user_id = $studentId $sessionCondition
ORDER BY view_count DESC";
$lpView = Database::fetch_assoc(Database::query($sql));
$lpView = learnpath::findLastView($lpInfo['iid'], $studentId, $courseId, $sessionId);

if (empty($lpView)) {
return '-';
Expand Down Expand Up @@ -120,4 +114,63 @@ function (array $option) use ($value) {

return '<div id="acquisition-'.$lpView['iid'].'">'.$return.'</div>';
}

public static function getLpVisibleScript()
{
$url = api_get_path(WEB_AJAX_PATH).'student_follow_page.ajax.php?'.http_build_query(['a' => 'views_invisible']);

return "<script>$(function () {
var chkbView = $('[name=\"chkb_view[]\"]');
function doRequest(element, state) {
element.prop('disabled', true);
var views = $.makeArray(element).map(function (input) { return input.value; });
return $.post('$url', { 'chkb_view[]': views, 'state': state }, function () { element.prop('disabled', false); });
}
$('[name=\"chkb_category[]\"]').on('change', function () {
var checked = this.checked;
var chkbs = $(this).parents('table').find('td :checkbox').each(function () { this.checked = checked; });
doRequest(chkbs, checked);
}).prop('checked', true);
chkbView.on('change', function () {
doRequest($(this), this.checked);
$(this).parents('table').find('th :checkbox').prop(
'checked',
$.makeArray($(this).parents('table').find('td :checkbox'))
.map(function (input) { return input.checked; })
.reduce(function (acc, cur) { return acc && cur; })
);
}).each(function () {
if (!this.checked) {
$(this).parents('table').find('th :checkbox').prop('checked', false);
}
});
});</script>";
}

public static function getLpVisibleField(array $lpInfo, int $studentId, int $courseId, int $sessionId = 0)
{
$lpView = learnpath::findLastView($lpInfo['iid'], $studentId, $courseId, $sessionId);

if (empty($lpView)) {
return '-';
}

$attrs = [];

$extraFieldValue = new ExtraFieldValue('lp_view');
$value = $extraFieldValue->get_values_by_handler_and_field_variable($lpView['iid'], self::VARIABLE_INVISIBLE);

if (empty($value) || empty($value['value'])) {
$attrs['checked'] = 'checked';
}

return Display::input('checkbox', 'chkb_view[]', $lpView['iid'], $attrs);
}
}
6 changes: 6 additions & 0 deletions main/install/configuration.dist.php
Expand Up @@ -393,6 +393,12 @@
*/
//$_configuration['student_follow_page_add_LP_acquisition_info'] = false;
// Prepend a column in student LPs table to display a checkbox to select the LP category and its LPs. Requires DB changes:
/*
INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, default_value, field_order, visible_to_self, visible_to_others, changeable, filter, created_at) VALUES
(20, 13, 'invisible', 'Invisible', '', 0, 1, 0, 0, 0, NOW());
*/
//$_configuration['student_follow_page_add_LP_invisible_checkbox'] = false;
// Hide session link of course_block on index/userportal
//$_configuration['remove_session_url']= false ;
// Allow foldable block for session list in session category on My courses tab
Expand Down
14 changes: 14 additions & 0 deletions main/lp/learnpath.class.php
Expand Up @@ -14310,4 +14310,18 @@ private function setAuthorLpItem(FormValidator $form)
$author->setOptions($options);
}
}

public static function findLastView(int $lpId, int $studentId, int $courseId, int $sessionId = 0)
{
$tblLpView = Database::get_course_table(TABLE_LP_VIEW);

$sessionCondition = api_get_session_condition($sessionId);

$sql = "SELECT iid FROM $tblLpView
WHERE c_id = $courseId AND lp_id = $lpId AND user_id = $studentId $sessionCondition
ORDER BY view_count DESC";
$result = Database::query($sql);

return Database::fetch_assoc($result);
}
}
1 change: 1 addition & 0 deletions main/lp/learnpathList.class.php
Expand Up @@ -203,6 +203,7 @@ public function __construct(
'lp_old_id' => $row->getId(),
'iid' => $row->getIid(),
'prerequisite' => $row->getPrerequisite(),
'category_id' => $row->getCategoryId(),
];
$names[$row->getName()] = $row->getIid();
}
Expand Down
26 changes: 26 additions & 0 deletions main/mySpace/myStudents.php
Expand Up @@ -1353,6 +1353,17 @@
}
}

if (true === api_get_configuration_value('student_follow_page_add_LP_invisible_checkbox')) {
echo StudentFollowPage::getLpVisibleScript();

$chkb = Display::input('checkbox', 'chkb_category[]', '');

$columnHeaders = array_merge(
['student_follow_page_add_LP_invisible_checkbox' => $chkb],
$columnHeaders
);
}

if (true === api_get_configuration_value('student_follow_page_add_LP_subscription_info')) {
$columnHeaders['student_follow_page_add_LP_subscription_info'] = get_lang('Unlock');
}
Expand All @@ -1372,6 +1383,8 @@
);
}

unset($columnHeadersToExport['student_follow_page_add_LP_invisible_checkbox']);

$hookLpTracking = HookMyStudentsLpTracking::create();
if ($hookLpTracking) {
$hookHeaders = $hookLpTracking->notifyTrackingHeader();
Expand Down Expand Up @@ -1544,6 +1557,19 @@

echo '<tr class="'.$css_class.'">';
$contentToExport = [];

if (in_array('student_follow_page_add_LP_invisible_checkbox', $columnHeadersKeys)) {
echo Display::tag(
'td',
StudentFollowPage::getLpVisibleField(
$learnpath,
$student_id,
$courseInfo['real_id'],
$sessionId
)
);
}

if (in_array('lp', $columnHeadersKeys)) {
$contentToExport[] = api_html_entity_decode(
stripslashes($lp_name),
Expand Down

0 comments on commit 3351d26

Please sign in to comment.