Competencies Not Working For Classroom Course

Backend functionalities: user management, course management, reports, certificates, etc.
alfa24
Senior Boarder
Posts: 1321
Joined: Fri Nov 24, 2017 8:45 am
Version: forma.lms 1.4.2

Re: Competencies Not Working For Classroom Course

Post by alfa24 »

I'm not seeing steps to reproduce, so your posts are just a diary of your tests.
I'm Jasmines, the One. If you need, you can contact me.

scubatricky
FormaLms User
Posts: 69
Joined: Wed Oct 21, 2020 11:43 pm

Re: Competencies Not Working For Classroom Course

Post by scubatricky »

Thanks alfa24, suffice to say the code you posted previously updates the DB correctly, but somewhere else in the system is resetting the date automatically straight after the update. This can be watched with a trace on the DB field when running the code.

I'll continue debugging the code and find out what else is changing the field back to the Now() value.

The help is appreciated, I'll search an alternative solution.

alfa24
Senior Boarder
Posts: 1321
Joined: Fri Nov 24, 2017 8:45 am
Version: forma.lms 1.4.2

Re: Competencies Not Working For Classroom Course

Post by alfa24 »

Problem found.
call to CompetencesAdm->assignCompetenceUsers is not accepting date parameter (it will set always NOW).
The function is called by CompetencesAdm->assignCourseCompetencesToUser.

The solution requires some more analysis, but I'm on it.
I'm Jasmines, the One. If you need, you can contact me.

alfa24
Senior Boarder
Posts: 1321
Joined: Fri Nov 24, 2017 8:45 am
Version: forma.lms 1.4.2

Re: Competencies Not Working For Classroom Course

Post by alfa24 »

Give it a try:

in /appCore/models/CompetencesAdm.php

rewrite function assignCompetenceUsers as follows:

Code: Select all

public function assignCompetenceUsers($id_competence, $users, $track = false, $completion_date = false) {
		if ($id_competence <= 0) return false; //invalid competence
		if (count($users) <= 0) return true; //0 users operations always "successfull"

		//set insert values for query
		$values = array();
		foreach ($users as $id_user => $score) {
			if ($score > 0) {
				$values[] = "("
					.(int)$id_competence.", "
					.(int)$id_user.", "
					.(float)$score.", "
					.($completion_date?'" '.$completion_date.'"':" NOW()")
					.")";
			}
		}

		if (count($values) > 0) {
			$query = "INSERT INTO ".$this->_getCompetencesUsersTable()." "
				." (id_competence, id_user, score_got, last_assign_date) VALUES "
				.implode(",", $values);
			$res = $this->db->query($query);
		} else {
			//we were trying to assign some invalid score <= 0
			return false;
		}

		//track the operation
		if ($track) {
			$params = new stdClass();
			$params->operation = "manual_assign"; //the type of operation (manual, course etc.)
			$params->id_course = 0; //the id of the course which has assigned the score
			$params->assigned_by = Docebo::user()->getIdSt(); //user/administrator who has assigned the score to the user
			$params->date_assignment = date("Y-m-d H:i:s"); //the date of the operation
			$params->score_assigned = $score; //the score assigned
			$params->score_total = $score;
			$this->trackOperation($id_competence, array_keys($users), $params);
		}

		return $res ? true : false;
	}
and function assignCourseCompetencesToUser as follows:

Code: Select all

public function assignCourseCompetencesToUser($id_course, $id_user, $track = true) {
		if ($id_course <= 0) return false;
		if ($id_user <= 0) return false;

		$ccomps = $this->getCourseCompetences($id_course, true);
		//$ccomps = $this->getCompetencesInfo($_comps);
		$ucomps = $this->getUserCompetences($id_user);

                list($completion_date) = sql_fetch_row(sql_query("SELECT date_complete FROM %lms_courseuser WHERE idCourse = '".$id_course."' AND idUser = '".$id_user."';"));
		if($completion_date == NULL) return false;

		//addScoreToUsers
		//assignCompetenceUsers
		$res = true;
		foreach ($ccomps as $id_competence => $competence) {
			if (array_key_exists($id_competence, $ucomps)) { //check if the competence already exists for the user
				$res = $this->addScoreToUsers($id_competence, $id_user, $competence->score);
			} else {
				$user_score = array($id_user => $competence->score);
				$res = $this->assignCompetenceUsers($id_competence, $user_score, false, $completion_date);
			}
			
			//track the operation
			if ($track) {
				$params = new stdClass();
				$params->operation = "course_finish"; //the type of operation (manual, course etc.)
				$params->id_course = $id_course; //the id of the course which has assigned the score
				$params->assigned_by = 0; //user/administrator who has assigned the score to the user
				$params->date_assignment = date("Y-m-d H:i:s"); //the date of the operation
				$params->score_assigned = $competence->score; //the score assigned
				$params->score_total = $competence->score + (array_key_exists($id_competence, $ucomps)
						? $ucomps[$id_competence]->score_got + $competence->score
						: 0);
				$this->trackOperation($id_competence, $id_user, $params);
			}
		}

		return $res;
	}
I'm Jasmines, the One. If you need, you can contact me.

scubatricky
FormaLms User
Posts: 69
Joined: Wed Oct 21, 2020 11:43 pm

Re: Competencies Not Working For Classroom Course

Post by scubatricky »

:lol: alfa24, you may be my twin, I'm just looking through SubscriptionsAlms.php and it appears to call updateUserStatusInCourse, just after setDateFinished has been called.

updateUserStatusInCourse takes a variable $new_date_complete, which if empty, inserts Now() into the query...

The rabbit hole gets deeper :D

I will implement your code and see if there are other areas where Now() is the default value if a variable isn't passed.

Many many thanks for your input and help on this...I'm looking through code and debugging things as I go. I also downloaded the original code for Spaghetti LMS today... :lol:

scubatricky
FormaLms User
Posts: 69
Joined: Wed Oct 21, 2020 11:43 pm

Re: Competencies Not Working For Classroom Course

Post by scubatricky »

Just implemented the above code in the competencies files, no change to behaviour unfortunately. After dinner this evening I will trace through lib.subscribe.php and find the error.

alfa24
Senior Boarder
Posts: 1321
Joined: Fri Nov 24, 2017 8:45 am
Version: forma.lms 1.4.2

Re: Competencies Not Working For Classroom Course

Post by alfa24 »

If you need it to be working on classroom courses, you need the previous fix too: https://forum.formalms.org/topic.html?p=24255#p24255
I'm Jasmines, the One. If you need, you can contact me.

alfa24
Senior Boarder
Posts: 1321
Joined: Fri Nov 24, 2017 8:45 am
Version: forma.lms 1.4.2

Re: Competencies Not Working For Classroom Course

Post by alfa24 »

Moreover, you need to call the assignCourseCompetencesToUser function AFTER setDateFinished.
In /appLms/lib/lib.date.php change:

Code: Select all

$cmodel->assignCourseCompetencesToUser($id_course, $id_user);
$this->setDateFinished($id_date, $id_user);
to:

Code: Select all

$this->setDateFinished($id_date, $id_user);
$cmodel->assignCourseCompetencesToUser($id_course, $id_user);
I'm Jasmines, the One. If you need, you can contact me.

scubatricky
FormaLms User
Posts: 69
Joined: Wed Oct 21, 2020 11:43 pm

Re: Competencies Not Working For Classroom Course

Post by scubatricky »

Unfortunately no success. Marking a student as attended, and setting their course status to complete adds Now() to the learning_courseuser -> date_completed field.

I'm looking at lib.subscribe.php now for the bug.

alfa24
Senior Boarder
Posts: 1321
Joined: Fri Nov 24, 2017 8:45 am
Version: forma.lms 1.4.2

Re: Competencies Not Working For Classroom Course

Post by alfa24 »

You must be missing something. I've tried and it works.
I'm Jasmines, the One. If you need, you can contact me.

Post Reply