Skip to content
Permalink
Browse files

Add "allow_gradebook_stats" config to improve gradebook speed

See BT#14357
Requires:
- DB changes see configuration.dist.php
- editing the entities: GradebookLink.php and GradebookEvaluation
  • Loading branch information...
jmontoyaa committed Mar 28, 2019
1 parent 48fcdbb commit efcd6d14dd2b515c3c56c912457e9a23e9f307ed
@@ -2767,6 +2767,7 @@ public function selectExpiredTime()
*/
public function cleanResults($cleanLpTests = false, $cleanResultBeforeDate = null)
{
$sessionId = api_get_session_id();
$table_track_e_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
$table_track_e_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
@@ -2796,7 +2797,7 @@ public function cleanResults($cleanLpTests = false, $cleanResultBeforeDate = nul
WHERE
c_id = ".api_get_course_int_id()." AND
exe_exo_id = ".$this->id." AND
session_id = ".api_get_session_id()." ".
session_id = ".$sessionId." ".
$sql_where;
$result = Database::query($sql);
@@ -2814,23 +2815,24 @@ public function cleanResults($cleanLpTests = false, $cleanResultBeforeDate = nul
}
}
$session_id = api_get_session_id();
// delete TRACK_E_EXERCISES table
$sql = "DELETE FROM $table_track_e_exercises
WHERE
c_id = ".api_get_course_int_id()." AND
exe_exo_id = ".$this->id." $sql_where AND
session_id = ".$session_id;
session_id = ".$sessionId;
Database::query($sql);
$this->generateStats($this->id, api_get_course_info(), $sessionId);
Event::addEvent(
LOG_EXERCISE_RESULT_DELETE,
LOG_EXERCISE_ID,
$this->id,
null,
null,
api_get_course_int_id(),
$session_id
$sessionId
);
return $i;
@@ -7962,6 +7964,145 @@ public function getQuestionForTeacher($start = 0, $lenght = 10)
return $questionList;
}
/**
* @param int $exerciseId
* @param array $courseInfo
* @param int $sessionId
*
* @return bool
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function generateStats($exerciseId, $courseInfo, $sessionId)
{
$allowStats = api_get_configuration_value('allow_gradebook_stats');
if (!$allowStats) {
return false;
}
if (empty($courseInfo)) {
return false;
}
$courseId = $courseInfo['real_id'];
$sessionId = (int) $sessionId;
$result = $this->read($exerciseId);
if (empty($result)) {
api_not_allowed(true);
}
$statusToFilter = empty($sessionId) ? STUDENT : 0;
$studentList = CourseManager::get_user_list_from_course_code(
api_get_course_id(),
$sessionId,
null,
null,
$statusToFilter
);
if (empty($studentList)) {
Display::addFlash(Display::return_message(get_lang('NoUsersInCourse')));
header('Location: '.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq());
exit;
}
$tblStats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
$studentIdList = [];
if (!empty($studentList)) {
$studentIdList = array_column($studentList, 'user_id');
}
if ($this->exercise_was_added_in_lp == false) {
$sql = "SELECT * FROM $tblStats
WHERE
exe_exo_id = $exerciseId AND
orig_lp_id = 0 AND
orig_lp_item_id = 0 AND
status <> 'incomplete' AND
session_id = $sessionId AND
c_id = $courseId
";
} else {
$lpId = null;
if (!empty($this->lpList)) {
// Taking only the first LP
$lpId = current($this->lpList);
$lpId = $lpId['lp_id'];
}
$sql = "SELECT *
FROM $tblStats
WHERE
exe_exo_id = $exerciseId AND
orig_lp_id = $lpId AND
status <> 'incomplete' AND
session_id = $sessionId AND
c_id = $courseId ";
}
$sql .= ' ORDER BY exe_id DESC';
$studentCount = 0;
$sum = 0;
$bestResult = 0;
$weight = 0;
$sumResult = 0;
$result = Database::query($sql);
while ($data = Database::fetch_array($result, 'ASSOC')) {
// Only take into account users in the current student list.
if (!empty($studentIdList)) {
if (!in_array($data['exe_user_id'], $studentIdList)) {
continue;
}
}
if (!isset($students[$data['exe_user_id']])) {
if ($data['exe_weighting'] != 0) {
$students[$data['exe_user_id']] = $data['exe_result'];
$studentCount++;
if ($data['exe_result'] > $bestResult) {
$bestResult = $data['exe_result'];
}
$sum += $data['exe_result'] / $data['exe_weighting'];
$sumResult += $data['exe_result'];
$weight = $data['exe_weighting'];
}
}
}
$count = count($studentList);
$average = $sumResult / $count;
$em = Database::getManager();
$links = AbstractLink::getGradebookLinksFromItem(
$this->selectId(),
LINK_EXERCISE,
api_get_course_id(),
api_get_session_id()
);
$repo = $em->getRepository('ChamiloCoreBundle:GradebookLink');
foreach ($links as $link) {
$linkId = $link['id'];
/** @var \Chamilo\CoreBundle\Entity\GradebookLink $exerciseLink */
$exerciseLink = $repo->find($linkId);
if ($exerciseLink) {
$exerciseLink
->setUserScoreList($students)
->setBestScore($bestResult)
->setAverageScore($average)
->setScoreWeight($this->get_max_score())
;
$em->persist($exerciseLink);
$em->flush();
}
}
}
/**
* Gets the question list ordered by the question_order setting (drag and drop).
*
@@ -19,7 +19,7 @@
$evaladd->set_user_id($_user['user_id']);
if (!empty($select_cat)) {
$evaladd->set_category_id($_GET['selectcat']);
$cat = Category :: load($_GET['selectcat']);
$cat = Category::load($_GET['selectcat']);
$evaladd->set_course_code($cat[0]->get_course_code());
} else {
$evaladd->set_category_id(0);
@@ -49,18 +49,16 @@
$eval->set_course_code(api_get_course_id());
$eval->set_category_id($values['hid_category_id']);
$parent_cat = Category :: load($values['hid_category_id']);
$parent_cat = Category::load($values['hid_category_id']);
$global_weight = $cat[0]->get_weight();
//$values['weight'] = $values['weight_mask']/$global_weight*$parent_cat[0]->get_weight();
$values['weight'] = $values['weight_mask'];
$eval->set_weight($values['weight']);
$eval->set_max($values['max']);
$visible = 1;
if (empty($values['visible'])) {
$visible = 0;
} else {
$visible = 1;
}
$eval->set_visible($visible);
$eval->add();
@@ -37,18 +37,25 @@
header('Location: gradebook_view_result.php?addresultnostudents=&selecteval='.$selectEval.'&'.api_get_cidreq());
exit;
}
$scores = ($values['score']);
foreach ($scores as $row) {
$scores = $values['score'];
$sumResult = 0;
$bestResult = 0;
$studentScoreList = [];
foreach ($scores as $userId => $row) {
$res = new Result();
$res->set_evaluation_id($values['evaluation_id']);
$res->set_user_id(key($scores));
//if no scores are given, don't set the score
if ((!empty($row)) || ($row == '0')) {
if (!empty($row) || $row == '0') {
$res->set_score($row);
}
$res->add();
next($scores);
}
Evaluation::generateStats($values['evaluation_id']);
Display::addFlash(Display::return_message(get_lang('ResultAdded'), 'confirmation', false));
header('Location: gradebook_view_result.php?addresult=&selecteval='.$selectEval.'&'.api_get_cidreq());
exit;

0 comments on commit efcd6d1

Please sign in to comment.
You can’t perform that action at this time.