Skip to content

Commit 89d96ae

Browse files
committed
LPs: Read lp_item extra field 'no_automatic_validation' see BT#18568
Chamilo by default changes the lp item to status = complete when the LP is a document. Setting the "no_automatic_validation" to true, Chamilo will not do that. The HTML file will be in charge of that update The document can update the status with LMSCommit('iframe'); Example in the HTML: <script> document.addEventListener("DOMContentLoaded", function(event) { if (window.parent && window.parent.API) { let api = window.parent.API; if (api) { api.LMSSetValue('cmi.core.score.raw', 100); api.LMSSetValue("cmi.core.lesson_status", "completed"); api.LMSCommit('iframe'); } } }); </script>
1 parent 210eacf commit 89d96ae

9 files changed

+158
-76
lines changed

main/lp/learnpath.class.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,6 +2574,7 @@ public function get_progress_bar_text($mode = '', $add = 0)
25742574
// path, then the rules are completely different: we assume only one
25752575
// item exists and the progress of the LP depends on the score
25762576
$scoreAsProgressSetting = api_get_configuration_value('lp_score_as_progress_enable');
2577+
25772578
if ($scoreAsProgressSetting === true) {
25782579
$scoreAsProgress = $this->getUseScoreAsProgress();
25792580
if ($scoreAsProgress) {
@@ -2605,6 +2606,7 @@ public function get_progress_bar_text($mode = '', $add = 0)
26052606
if ($completeItems > $total_items) {
26062607
$completeItems = $total_items;
26072608
}
2609+
26082610
if ($mode == '%') {
26092611
if ($total_items > 0) {
26102612
$percentage = ((float) $completeItems / (float) $total_items) * 100;
@@ -5596,10 +5598,14 @@ public function start_current_item($allow_new_attempt = false)
55965598
if ($debug) {
55975599
error_log('start_current_item will save item with prereq: '.$prereq_check);
55985600
}
5599-
$this->items[$this->current]->save(false, $prereq_check);
5601+
5602+
$saveStatus = learnpathItem::isLpItemAutoComplete($this->current);
5603+
if ($saveStatus) {
5604+
$this->items[$this->current]->save(false, $prereq_check);
5605+
}
56005606
}
56015607
// If sco, then it is supposed to have been updated by some other call.
5602-
if ($item_type == 'sco') {
5608+
if ($item_type === 'sco') {
56035609
$this->items[$this->current]->restart();
56045610
}
56055611
}

main/lp/learnpathItem.class.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4536,4 +4536,21 @@ public function getStatusFromOtherSessions($user_id, $prereqs_string, $refs_list
45364536
return $resultFromOtherSessions;
45374537
}
45384538
}
4539+
4540+
public static function isLpItemAutoComplete($lpItemId): bool
4541+
{
4542+
$extraFieldValue = new ExtraFieldValue('lp_item');
4543+
$saveAutomatic = $extraFieldValue->get_values_by_handler_and_field_variable(
4544+
$lpItemId,
4545+
'no_automatic_validation'
4546+
);
4547+
4548+
if (false !== $saveAutomatic && is_array($saveAutomatic) && isset($saveAutomatic['value'])) {
4549+
if (1 === (int) $saveAutomatic['value']) {
4550+
return false;
4551+
}
4552+
}
4553+
4554+
return true;
4555+
}
45394556
}

main/lp/lp_ajax_initialize.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function initialize_item($lp_id, $user_id, $view_id, $next_item)
4545
if ($debug) {
4646
error_log('In initialize_item() - new item is '.$next_item);
4747
}
48+
4849
$mylp->start_current_item(true);
4950

5051
if (is_object($mylp->items[$next_item])) {
@@ -111,7 +112,6 @@ function initialize_item($lp_id, $user_id, $view_id, $next_item)
111112
}
112113
}
113114
$myobjectives = json_encode($phpobjectives);
114-
115115
$return .=
116116
"olms.score=".$myscore.";".
117117
"olms.max=".$mymax.";".

main/lp/lp_ajax_save_item.php

Lines changed: 90 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ function save_item(
5858
$courseId = null,
5959
$lmsFinish = 0,
6060
$userNavigatesAway = 0,
61-
$statusSignalReceived = 0
61+
$statusSignalReceived = 0,
62+
$forceIframeSave = 0
6263
) {
6364
$debug = 0;
6465
$return = null;
@@ -107,6 +108,15 @@ function save_item(
107108

108109
// This functions sets the $this->db_item_view_id variable needed in get_status() see BT#5069
109110
$myLPI->set_lp_view($view_id);
111+
$my_type = $myLPI->get_type();
112+
113+
$saveStatus = true;
114+
if ('document' === $my_type) {
115+
$saveStatus = learnpathItem::isLpItemAutoComplete($myLPI->getIid());
116+
if ($forceIframeSave) {
117+
$saveStatus = true;
118+
}
119+
}
110120

111121
// Launch the prerequisites check and set error if needed
112122
if (true !== $prerequisitesCheck) {
@@ -171,24 +181,29 @@ function save_item(
171181
}
172182

173183
$statusIsSet = false;
174-
// Default behaviour.
175-
if (isset($status) && $status != '' && $status != 'undefined') {
176-
if ($debug > 1) {
177-
error_log('Calling set_status('.$status.')');
178-
}
184+
if ($saveStatus) {
185+
// Default behaviour.
186+
if (isset($status) && $status != '' && $status != 'undefined') {
187+
if ($debug > 1) {
188+
error_log('Calling set_status('.$status.')');
189+
}
179190

180-
$myLPI->set_status($status);
181-
$statusIsSet = true;
182-
if ($debug > 1) {
183-
error_log('Done calling set_status: checking from memory: '.$myLPI->get_status(false));
191+
$myLPI->set_status($status);
192+
$statusIsSet = true;
193+
if ($debug > 1) {
194+
error_log('Done calling set_status: checking from memory: '.$myLPI->get_status(false));
195+
}
196+
} else {
197+
if ($debug > 1) {
198+
error_log('Status not updated');
199+
}
184200
}
185201
} else {
186202
if ($debug > 1) {
187203
error_log('Status not updated');
188204
}
189205
}
190206

191-
$my_type = $myLPI->get_type();
192207
// Set status to completed for hotpotatoes if score > 80%.
193208
if ($my_type === 'hotpotatoes') {
194209
if ((empty($status) || $status == 'undefined' || $status == 'not attempted') && $max > 0) {
@@ -370,25 +385,30 @@ function save_item(
370385
// End of type=='sco'
371386
}
372387

373-
// If no previous condition changed the SCO status, proceed with a
374-
// generic behaviour
375-
if (!$statusIsSet && !$statusSignalReceived) {
376-
// Default behaviour
377-
if (isset($status) && $status != '' && $status != 'undefined') {
378-
if ($debug > 1) {
379-
error_log('Calling set_status('.$status.')');
380-
}
388+
// If no previous condition changed the SCO status, proceed with a generic behaviour
389+
if ($saveStatus) {
390+
if (!$statusIsSet && !$statusSignalReceived) {
391+
// Default behaviour
392+
if (isset($status) && $status != '' && $status != 'undefined') {
393+
if ($debug > 1) {
394+
error_log('Calling set_status('.$status.')');
395+
}
381396

382-
$myLPI->set_status($status);
397+
$myLPI->set_status($status);
383398

384-
if ($debug > 1) {
385-
error_log('Done calling set_status: checking from memory: '.$myLPI->get_status(false));
386-
}
387-
} else {
388-
if ($debug > 1) {
389-
error_log("Status not updated");
399+
if ($debug > 1) {
400+
error_log('Done calling set_status: checking from memory: '.$myLPI->get_status(false));
401+
}
402+
} else {
403+
if ($debug > 1) {
404+
error_log("Status not updated");
405+
}
390406
}
391407
}
408+
} else {
409+
if ($debug > 1) {
410+
error_log("Status not updated");
411+
}
392412
}
393413

394414
if (isset($time) && $time != '' && $time != 'undefined') {
@@ -440,7 +460,10 @@ function save_item(
440460
if ($core_exit != 'undefined') {
441461
$myLPI->set_core_exit($core_exit);
442462
}
443-
$myLP->save_item($item_id, false);
463+
464+
if ($saveStatus) {
465+
$myLP->save_item($item_id, false);
466+
}
444467
}
445468

446469
$myStatusInDB = $myLPI->get_status(true);
@@ -478,37 +501,39 @@ function save_item(
478501
error_log("progress: $myComplete / $myTotal");
479502
}
480503

481-
if ($myLPI->get_type() !== 'sco') {
482-
// If this object's JS status has not been updated by the SCORM API, update now.
483-
$return .= "olms.lesson_status='".$myStatus."';";
484-
}
485-
$return .= "update_toc('".$myStatus."','".$item_id."');";
486-
$update_list = $myLP->get_update_queue();
487-
488-
foreach ($update_list as $my_upd_id => $my_upd_status) {
489-
if ($my_upd_id != $item_id) {
490-
/* Only update the status from other items (i.e. parents and brothers),
491-
do not update current as we just did it already. */
492-
$return .= "update_toc('".$my_upd_status."','".$my_upd_id."');";
504+
if ($saveStatus) {
505+
if ($myLPI->get_type() !== 'sco') {
506+
// If this object's JS status has not been updated by the SCORM API, update now.
507+
$return .= "olms.lesson_status='".$myStatus."';";
493508
}
494-
}
495-
$progressBarSpecial = false;
496-
$scoreAsProgressSetting = api_get_configuration_value('lp_score_as_progress_enable');
497-
if ($scoreAsProgressSetting === true) {
498-
$scoreAsProgress = $myLP->getUseScoreAsProgress();
499-
if ($scoreAsProgress) {
500-
// Only update score if it was set by scorm.
501-
if (isset($score) && $score != -1) {
502-
$score = $myLPI->get_score();
503-
$maxScore = $myLPI->get_max();
504-
$return .= "update_progress_bar('$score', '$maxScore', '$myProgressMode');";
509+
$return .= "update_toc('".$myStatus."','".$item_id."');";
510+
$update_list = $myLP->get_update_queue();
511+
foreach ($update_list as $my_upd_id => $my_upd_status) {
512+
if ($my_upd_id != $item_id) {
513+
/* Only update the status from other items (i.e. parents and brothers),
514+
do not update current as we just did it already. */
515+
$return .= "update_toc('".$my_upd_status."','".$my_upd_id."');";
505516
}
506-
$progressBarSpecial = true;
507517
}
508-
}
509518

510-
if (!$progressBarSpecial) {
511-
$return .= "update_progress_bar('$myComplete', '$myTotal', '$myProgressMode');";
519+
$progressBarSpecial = false;
520+
$scoreAsProgressSetting = api_get_configuration_value('lp_score_as_progress_enable');
521+
if ($scoreAsProgressSetting === true) {
522+
$scoreAsProgress = $myLP->getUseScoreAsProgress();
523+
if ($scoreAsProgress) {
524+
// Only update score if it was set by scorm.
525+
if (isset($score) && $score != -1) {
526+
$score = $myLPI->get_score();
527+
$maxScore = $myLPI->get_max();
528+
$return .= "update_progress_bar('$score', '$maxScore', '$myProgressMode');";
529+
}
530+
$progressBarSpecial = true;
531+
}
532+
}
533+
534+
if (!$progressBarSpecial) {
535+
$return .= "update_progress_bar('$myComplete', '$myTotal', '$myProgressMode');";
536+
}
512537
}
513538

514539
if (!Session::read('login_as')) {
@@ -537,15 +562,17 @@ function save_item(
537562
$return .= 'update_stats();';
538563
}
539564

540-
// To be sure progress is updated.
541-
$myLP->save_last($score);
565+
if ($saveStatus) {
566+
// To be sure progress is updated.
567+
$myLP->save_last($score);
568+
HookLearningPathItemViewed::create()
569+
->setEventData(['item_view_id' => $myLPI->db_item_view_id])
570+
->notifyLearningPathItemViewed();
542571

543-
HookLearningPathItemViewed::create()
544-
->setEventData(['item_view_id' => $myLPI->db_item_view_id])
545-
->notifyLearningPathItemViewed();
572+
Session::write('lpobject', serialize($myLP));
573+
Session::write('oLP', $myLP);
574+
}
546575

547-
Session::write('lpobject', serialize($myLP));
548-
Session::write('oLP', $myLP);
549576
if ($debug > 0) {
550577
error_log("lp_view_session_id :".$myLP->lp_view_session_id);
551578
error_log('---------------- lp_ajax_save_item.php : save_item end ----- ');
@@ -593,5 +620,6 @@ function save_item(
593620
(!empty($_REQUEST['course_id']) ? $_REQUEST['course_id'] : ''),
594621
(empty($_REQUEST['finish']) ? 0 : 1),
595622
(empty($_REQUEST['userNavigatesAway']) ? 0 : 1),
596-
(empty($_REQUEST['statusSignalReceived']) ? 0 : 1)
623+
(empty($_REQUEST['statusSignalReceived']) ? 0 : 1),
624+
isset($_REQUEST['forceIframeSave']) ? (int) $_REQUEST['forceIframeSave'] : 0
597625
);

main/lp/lp_ajax_switch_item.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function switch_item_details($lp_id, $user_id, $view_id, $current_item, $next_it
4444
*/
4545
$mylp = learnpath::getLpFromSession(api_get_course_id(), $lp_id, $user_id);
4646
$new_item_id = 0;
47+
$saveStatus = learnpathItem::isLpItemAutoComplete($current_item);
48+
4749
switch ($next_item) {
4850
case 'next':
4951
$mylp->set_current_item($current_item);
@@ -93,8 +95,11 @@ function switch_item_details($lp_id, $user_id, $view_id, $current_item, $next_it
9395
}
9496

9597
$mylp->start_current_item(true);
96-
if ($mylp->force_commit) {
97-
$mylp->save_current();
98+
99+
if ($saveStatus) {
100+
if ($mylp->force_commit) {
101+
$mylp->save_current();
102+
}
98103
}
99104

100105
if (is_object($mylp->items[$new_item_id])) {
@@ -106,6 +111,7 @@ function switch_item_details($lp_id, $user_id, $view_id, $current_item, $next_it
106111
$mylpi = new learnpathItem($new_item_id, $user_id);
107112
$mylpi->set_lp_view($view_id);
108113
}
114+
109115
/*
110116
* now get what's needed by the SCORM API:
111117
* -score
@@ -263,7 +269,6 @@ function switch_item_details($lp_id, $user_id, $view_id, $current_item, $next_it
263269
"update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');".
264270
$updateMinTime
265271
;
266-
267272
$return .= 'updateGamificationValues(); ';
268273
$mylp->set_error_msg('');
269274
$mylp->prerequisites_match(); // Check the prerequisites are all complete.

main/lp/lp_ajax_switch_item_toc.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ function switch_item_toc($lpId, $userId, $viewId, $currentItem, $nextItem)
3434
error_log('In switch_item_toc('.$lpId.','.$userId.','.$viewId.','.$currentItem.','.$nextItem.')', 0);
3535
}
3636
$myLP = learnpath::getLpFromSession(api_get_course_id(), $lpId, $userId);
37+
$saveStatus = learnpathItem::isLpItemAutoComplete($currentItem);
38+
3739
$newItemId = 0;
3840
$oldItemId = 0;
3941
switch ($nextItem) {
@@ -79,7 +81,8 @@ function switch_item_toc($lpId, $userId, $viewId, $currentItem, $nextItem)
7981
break;
8082
}
8183
$myLP->start_current_item(true);
82-
if ($myLP->force_commit) {
84+
85+
if ($myLP->force_commit && $saveStatus) {
8386
$myLP->save_current();
8487
}
8588
if (is_object($myLP->items[$newItemId])) {

main/lp/lp_edit_item.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ function confirmation(name) {
151151

152152
$path_parts = pathinfo($path_file);
153153

154-
if (!empty($path_file) && isset($path_parts['extension']) && $path_parts['extension'] == 'html') {
154+
if (!empty($path_file) && isset($path_parts['extension']) && $path_parts['extension'] === 'html') {
155155
echo $learnPath->return_new_tree();
156156
// Show the template list
157157
echo '<div id="frmModel" class="scrollbar-inner lp-add-item"></div>';
@@ -167,6 +167,7 @@ function confirmation(name) {
167167
'authorlpitem',
168168
'price',
169169
];
170+
170171
if (api_is_platform_admin()) {
171172
// Only admins can edit this items
172173
$excludeExtraFields = [];
@@ -178,6 +179,9 @@ function confirmation(name) {
178179
echo $learnPath->display_item($_GET['id'], $msg);
179180
} else {
180181
$item = $learnPath->getItem($_GET['id']);
182+
if ('document' !== $item->get_type()) {
183+
$excludeExtraFields[] = 'no_automatic_validation';
184+
}
181185
echo $learnPath->display_edit_item($item->getIid(), $excludeExtraFields);
182186
$finalItem = Session::read('finalItem');
183187
if ($finalItem) {

main/lp/lp_view.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@
288288
break;
289289
}
290290

291+
// This change the status to complete in Chamilo LP.
291292
$lp->start_current_item(); // starts time counter manually if asset
292293
} else {
293294
$src = 'blank.php?error=prerequisites';

0 commit comments

Comments
 (0)