Skip to content

Commit cf11e9b

Browse files
committed
improved the export helper, reduced large number of queries issued when
exporting, adjusted the format of export file so that it's easier to read
1 parent e517217 commit cf11e9b

File tree

6 files changed

+182
-147
lines changed

6 files changed

+182
-147
lines changed

app/controllers/components/export_helper.php

Lines changed: 102 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ function createCSV($params) {
4343
$eventId = $event['Event']['id'];
4444
$eventTemplateId = $event['Event']['template_id'];
4545
$eventTypeId = $event['Event']['event_template_type_id'];
46-
$groupEvents = $this->GroupEvent->findAll('event_id='.$eventId);
46+
$groupEvents = $this->GroupEvent->findAll('event_id='.$eventId.' AND group_id != 0');
4747

4848
//too much garbage... outsourced to createBody
4949
if (!empty($groupEvents)) {
50-
$csvContent .= $this->createBody($event, $groupEvents,$params,$eventTemplateId,$eventTypeId);
50+
$csvContent .= $this->createBody($event, $groupEvents, $params, $eventTemplateId, $eventTypeId);
5151
}
5252
}
5353

@@ -90,19 +90,21 @@ function createBody ($event, $groupEvents,$params,$eventTemplateId,$eventTypeId)
9090
$this->User = new User;
9191
$this->SimpleEvaluation = new SimpleEvaluation;
9292
$this->Rubric = new Rubric;
93-
$this->Mixeval = new Mixeval;
9493
$this->RubricsCriteria = new RubricsCriteria;
9594
$this->MixEvalsQuestionDesc = new MixevalsQuestionDesc;
9695
$this->EvaluationSimple = new EvaluationSimple;
9796
$this->EvaluationRubric = new EvaluationRubric;
9897
$this->EvaluationMixeval = new EvaluationMixeval;
98+
$this->EvaluationMixevalDetail = new EvaluationMixevalDetail;
99+
$this->User = new User;
99100
$mixedEvalNumeric = '';
100101

101102
$globEventId = $groupEvents[0]['GroupEvent']['event_id'];
102103

103104
$data = array();
104105
$legends = array();
105106
$i=0;
107+
106108
foreach ($groupEvents as $groupEvent) {
107109
//*******beef
108110
$groupId = $groupEvent['GroupEvent']['group_id'];
@@ -113,37 +115,41 @@ function createBody ($event, $groupEvents,$params,$eventTemplateId,$eventTypeId)
113115
//get group stati
114116
$data[$i]['group_status'] = $groupEvent['GroupEvent']['marked'];
115117

116-
$groupMembers = $this->GroupsMembers->getMembers($groupId);
117-
unset($groupMembers['member_count']);
118+
// $groupMembers = $this->GroupsMembers->getMembers($groupId, null);
119+
$groupMembers = $this->User->getGroupMembers($groupId);
118120
$j=0;
119121
$data[$i]['students'] = array();
120122

121123
global $globUsersArr;
122124
foreach($groupMembers as $groupMember) {
123-
$userId = $groupMember['GroupsMembers']['user_id'];
124-
$student = $this->User->findUserByid($userId);
125-
$globUsersArr[$student['User']['student_no']] = $userId;
125+
$userId = $groupMember['User']['id'];
126+
$globUsersArr[] = $userId;
126127
}
128+
127129
if (!empty($globUsersArr)) {
128-
$incompletedArr = $this->buildIncompletedArr();
130+
$incompletedArr = $this->buildIncompletedArr($groupEventId);
129131
} else {
130132
$incompletedArr = array();
131133
}
134+
132135
$count = 0;
133-
foreach($groupMembers as $groupMember) {
134-
if(in_array($groupMember['GroupsMembers']['user_id'], $incompletedArr)) {
135-
$count++;
136-
}
136+
foreach ($groupMembers as $key => $groupMember) {
137+
if (in_array($groupMember['User']['id'], $incompletedArr)) {
138+
$groupMembers[$key]['User']['incomplete'] = true;
139+
$count++;
140+
} else {
141+
$groupMembers[$key]['User']['incomplete'] = false;
142+
}
137143
}
138144

139145
foreach ($groupMembers as $groupMember) {
140146
//get student info: first_name, last_name, id, email
141-
$userId = $groupMember['GroupsMembers']['user_id'];
142-
$student = $this->User->findUserByid($userId);
143-
$data[$i]['students'][$j]['student_id'] = $student['User']['student_no'];
144-
$data[$i]['students'][$j]['first_name'] = $student['User']['first_name'];
145-
$data[$i]['students'][$j]['last_name'] = $student['User']['last_name'];
146-
$data[$i]['students'][$j]['email'] = $student['User']['email'];
147+
$userId = $groupMember['User']['id'];
148+
$data[$i]['students'][$j]['student_id'] = $groupMember['User']['student_no'];
149+
$data[$i]['students'][$j]['first_name'] = $groupMember['User']['first_name'];
150+
$data[$i]['students'][$j]['last_name'] = $groupMember['User']['last_name'];
151+
$data[$i]['students'][$j]['email'] = $groupMember['User']['email'];
152+
$data[$i]['students'][$j]['incomplete'] = $groupMember['User']['incomplete'];
147153

148154
switch ($eventTypeId) {
149155
case 1://simple
@@ -154,7 +160,7 @@ function createBody ($event, $groupEvents,$params,$eventTemplateId,$eventTypeId)
154160
}
155161
$data[$i]['students'][$j]['score'] = '';
156162
$score_tmp = $this->EvaluationSimple->getReceivedTotalScore($groupEventId,$userId);
157-
/**
163+
/**
158164
* 6 in the group, 4 incompleted
159165
* ----------------------------------------------
160166
* | | self eval on | self eval off |
@@ -196,68 +202,62 @@ function createBody ($event, $groupEvents,$params,$eventTemplateId,$eventTypeId)
196202
}
197203
break;
198204
case 4://mixeval
205+
// get the score
206+
$score_tmp = $this->EvaluationMixeval->getReceivedTotalScore($groupEventId, $userId);
207+
$data[$i]['students'][$j]['score'] = !isset($score_tmp[0]['received_total_score'])?'':$score_tmp[0]['received_total_score'];
199208

200-
$score_tmp = $this->EvaluationMixeval->getReceivedTotalScore($groupEventId,$userId);
201-
#Text data for specific questions is inside the userResults variable
202-
$userResults = $this->EvaluationMixeval->getResultsDetailByEvaluatee($groupEventId,$userId);
203-
$data[$i]['students'][$j]['score'] = !isset($score_tmp[0]['received_total_score'])?'':$score_tmp[0]['received_total_score'];
204-
$data[$i]['students'][$j]['comments'] = '';
209+
// Text data for specific questions is inside the userResults variable
210+
$userResults = $this->EvaluationMixevalDetail->getResultsDetailByEvaluatee($groupEventId, $userId);
211+
$data[$i]['students'][$j]['comments'] = '';
205212

206-
#Here we add some generic information to the mixedEvalNumeric variable. First we add the name of the person being evaluated in this particular mixed eval(evaluatee)
207-
#then we sample index 0 of the userResults array to just get the number of numeric questions. Then we can make column headers for each question (1,2,3...)
208-
$nameArray = $this->User->findUserByid($userId);
209-
$name=$nameArray['User']['first_name'] . ' ' . $nameArray['User']['last_name'];
210-
$mixedEvalNumeric .= "\n".$name . "," . $data[$i]['group_name'] . "\n";
211-
$tempEvaluatee = '';
212-
$counter = 0;
213-
214-
#go through userResults just to get the number of numeric questions.
215-
#YES we could combine this with the other loop but I'm running out of time and this is so much easier.
216-
foreach($userResults as $comment){
217-
if(!isset($comment['EvaluationMiexevalDetail']['question_comment'])){
218-
if(is_null($comment['EvaluationMixevalDetail']['question_comment'])){
213+
//Here we add some generic information to the mixedEvalNumeric variable. First we add the name of the person being evaluated in this particular mixed eval(evaluatee)
214+
//then we sample index 0 of the userResults array to just get the number of numeric questions. Then we can make column headers for each question (1,2,3...)
215+
//$nameArray = $this->User->findUserByid($userId);
216+
$name = $groupMember['User']['first_name'] . ' ' . $groupMember['User']['last_name'];
217+
$mixedEvalNumeric .= "\n".$name . "," . $data[$i]['group_name'] . "\n";
218+
$tempEvaluatee = '';
219+
$mixedEvalNumeric .= "Evaluator,";
220+
221+
//go through userResults just to get the number of numeric questions.
222+
$counter = 1;
223+
$lastQ = 0;
224+
foreach ($userResults as $comment) {
225+
if ($lastQ > $comment['EvaluationMixevalDetail']['question_number']) {
226+
break;
227+
}
228+
//Here we create the columns for our .csv file, to be imported into excel
229+
$mixedEvalNumeric .= "Q$counter".",";
219230
$counter++;
220-
} else {
221-
break;
222-
}
231+
$lastQ = $comment['EvaluationMixevalDetail']['question_number'];
223232
}
224233

225-
}
226-
#Here we create the columns for our .csv file, to be imported into excel
227-
$mixedEvalNumeric .= ",";
228-
for ($k = 1; $k < $counter+1; $k++) {
229-
$mixedEvalNumeric .= "Q$k".",";
230-
}
231-
$mixedEvalNumeric .= "\n";
232-
##########
233-
foreach ($userResults as $comment){
234-
$results = $this->EvaluationMixeval->find('id='.$comment['EvaluationMixevalDetail']['evaluation_mixeval_id']);
235-
236-
#Set some variables that hold the USER data for the evaluator and evaluatee, then set the names for convenience
237-
$evaluatorArray = $this->User->find('id='.$results['EvaluationMixeval']['evaluator']);
238-
$evaluatorName = $evaluatorArray['User']['first_name'] . ' ' . $evaluatorArray['User']['last_name'];
239-
$evaluateeArray = $this->User->find('id='.$results['EvaluationMixeval']['evaluatee']);
240-
$evaluateeName = $evaluateeArray['User']['first_name'] . ' ' . $evaluateeArray['User']['last_name'];
241-
242-
#The $userResults variable is a long list of entries for EVERY evaluatee. If the evaluator changes, it means we are on a new 'survey' created by a different person.
243-
#Then we want to add a new line to our numeric data variable.
244-
if($evaluatorArray['User']['username'] != $tempEvaluatee){
245-
$mixedEvalNumeric .= "\n";
246-
$mixedEvalNumeric .= $evaluatorName. ",";
247-
}
248-
#Set the temp variable
249-
$tempEvaluatee = $evaluatorArray['User']['username'];
234+
foreach ($userResults as $comment) {
235+
//Set some variables that hold the USER data for the evaluator and evaluatee, then set the names for convenience
236+
$evaluatorArray = $groupMembers[$comment['EvaluationMixeval']['evaluator']];
237+
$evaluatorName = $evaluatorArray['User']['first_name'] . ' ' . $evaluatorArray['User']['last_name'];
238+
$evaluateeName = $groupMember['User']['first_name'].' '.$groupMember['User']['last_name'];
250239

251-
#Here we go through the text responses for the mixed evaluation answers. If there is text content, we append it to the contents of the comments parameter for the evaluatee.
252-
$data[$i]['students'][$j]['comments'] .= isset($comment['EvaluationMixevalDetail']['question_comment'])&&!empty($comment['EvaluationMixevalDetail']['question_comment']) ? $evaluatorName.' on ' .$evaluateeName. ' Q'.$comment['EvaluationMixevalDetail']['question_number'] .' : '.$comment['EvaluationMixevalDetail']['question_comment']."\n".'':'';
253-
if(!isset($comment['EvaluationMixevalDetail']['question_comment'])){
254-
$mixedEvalNumeric .= trim($comment['EvaluationMixevalDetail']['grade']).",";
240+
//The $userResults variable is a long list of entries for EVERY evaluatee. If the evaluator changes, it means we are on a new 'survey' created by a different person.
241+
//Then we want to add a new line to our numeric data variable.
242+
if($comment['EvaluationMixeval']['evaluator'] != $tempEvaluatee) {
243+
$mixedEvalNumeric .= "\n";
244+
$mixedEvalNumeric .= $evaluatorName. ",";
245+
}
246+
//Set the temp variable
247+
$tempEvaluatee = $comment['EvaluationMixeval']['evaluator'];
248+
249+
//Here we go through the text responses for the mixed evaluation answers. If there is text content, we append it to the contents of the comments parameter for the evaluatee.
250+
//$data[$i]['students'][$j]['comments'] .= isset($comment['EvaluationMixevalDetail']['question_comment'])&&!empty($comment['EvaluationMixevalDetail']['question_comment']) ? $evaluatorName.' on ' .$evaluateeName. ' Q'.$comment['EvaluationMixevalDetail']['question_number'] .' : '.$comment['EvaluationMixevalDetail']['question_comment']."\n".'':'';
251+
if(!isset($comment['EvaluationMixevalDetail']['question_comment'])){
252+
$mixedEvalNumeric .= trim($comment['EvaluationMixevalDetail']['grade']).",";
253+
} else {
254+
$mixedEvalNumeric .= '"'.trim($comment['EvaluationMixevalDetail']['question_comment']).'"'.",";
255+
}
255256
}
256-
}
257-
$mixedEvalNumeric .= "\n";
258-
break;
257+
$mixedEvalNumeric .= "\n";
258+
break;
259259
default:
260-
break;
260+
break;
261261
}
262262
$j++;
263263
}
@@ -302,46 +302,48 @@ function formatBody($data, $params, $legends=null, $mixedEvalNumeric) {
302302
for ($i=1; $i <= $question_count; $i++) {
303303
$content .= $i.',';
304304
}
305-
$content .= !isset($params['form']['include_general_comments']) ? '':'Comments';
305+
//$content .= !isset($params['form']['include_general_comments']) ? '':'Comments';
306306
if ($hasContent) $content .= "\n\n";
307307

308308
foreach ($data as $group) {
309-
foreach ($group['students'] as $student) {
310-
if (!empty($params['form']['include_group_status'])) {
311-
$incompletedArr = $this->buildIncompletedArr();
312-
set_time_limit(1200);
313-
if(array_key_exists($student['student_id'], $incompletedArr) )
314-
$content .= 'X,';
315-
else
316-
$content .= 'OK,';
317-
}
318-
$content .= empty($params['form']['include_group_names']) ? '':$group['group_name'].",";$stuff=true;
319-
$content .= empty($params['form']['include_student_first']) ? '':"\"".$student['first_name']."\",";
320-
$content .= empty($params['form']['include_student_last']) ? '':"\"".$student['last_name']."\",";
321-
$content .= empty($params['form']['include_student_id']) ? '':$student['student_id'].",";
322-
$content .= empty($params['form']['include_student_email']) ? '':"\"".$student['email']."\",";
323-
$content .= empty($params['form']['include_criteria_marks']) ? '':$student['score'].",";
324-
$content .= (empty($params['form']['include_criteria_legend'])||!isset($student['sub_score']))? '':$student['sub_score'];
325-
$content .= empty($params['form']['include_general_comments']) ? '': "\"".str_replace('"', '""', $student['comments'])."\",";
309+
foreach ($group['students'] as $student) {
310+
if (!empty($params['form']['include_group_status'])) {
311+
if ($student['incomplete']) {
312+
$content .= 'X,';
313+
} else {
314+
$content .= 'OK,';
315+
}
316+
}
317+
$content .= empty($params['form']['include_group_names']) ? '':$group['group_name'].",";$stuff=true;
318+
$content .= empty($params['form']['include_student_first']) ? '':"\"".$student['first_name']."\",";
319+
$content .= empty($params['form']['include_student_last']) ? '':"\"".$student['last_name']."\",";
320+
$content .= empty($params['form']['include_student_id']) ? '':$student['student_id'].",";
321+
$content .= empty($params['form']['include_student_email']) ? '':"\"".$student['email']."\",";
322+
$content .= empty($params['form']['include_criteria_marks']) ? '':$student['score'].",";
323+
$content .= (empty($params['form']['include_criteria_legend'])||!isset($student['sub_score']))? '':$student['sub_score'];
324+
$content .= empty($params['form']['include_general_comments']) ? '': "\"".str_replace('"', '""', $student['comments'])."\",";
326325

326+
if ($hasContent) $content .= "\n";
327+
}
327328
if ($hasContent) $content .= "\n";
328-
}
329-
if ($hasContent) $content .= "\n";
330329
}
331330
####
332-
#Append the numeric mixed evaluation variable to the end of the .csv. This should be a list of tables for each evaluatee.
331+
#Append the numeric mixed evaluation variable to the end of the .csv. This should be a list of tables for each evaluatee.
333332
#This is not a good way of doing things, but it works.
334333
$content .= $mixedEvalNumeric;
335-
###
334+
336335
return $content;
337336
}
338337

339-
function buildIncompletedArr() {
340-
global $globEventId, $globUsersArr;
338+
function buildIncompletedArr($groupEventId) {
339+
global $globUsersArr;
341340
$this->EvalSubmission = new EvaluationSubmission();
341+
$submitters = $this->EvalSubmission->getSubmittersByGroupEventId($groupEventId);
342342
foreach ($globUsersArr as $globUserStuNum=>$globUserId) {
343-
if($this->EvalSubmission->getEvalSubmissionByEventIdSubmitter($globEventId, $globUserId) != null)
344-
unset($globUsersArr[$globUserStuNum]);
343+
// if($this->EvalSubmission->getEvalSubmissionByEventIdSubmitter($globEventId, $globUserId) != null)
344+
if (array_search($globUserId, $submitters) !== false) {
345+
unset($globUsersArr[$globUserStuNum]);
346+
}
345347
}
346348
return $globUsersArr;
347349
}

app/models/evaluation_mixeval.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,6 @@ function getResultsByEvaluator($grpEventId=null, $evaluator=null) {
2828
return $this->findAll('grp_event_id='.$grpEventId.' AND evaluator='.$evaluator, null, 'evaluatee ASC');
2929
}
3030

31-
// gets Mixeval evaluation result for a specific assignment and evaluator
32-
function getResultsDetailByEvaluatee($grpEventId=null, $evaluatee=null) {
33-
$condition = 'EvaluationMixeval.grp_event_id=' . $grpEventId .
34-
' AND EvaluationMixeval.evaluatee=' .$evaluatee;
35-
$fields = 'EvaluationMixevalDetail.*';
36-
$joinTable = array(' LEFT JOIN evaluation_mixeval_details AS EvaluationMixevalDetail ' .
37-
'ON EvaluationMixeval.id=EvaluationMixevalDetail.evaluation_mixeval_id');
38-
39-
return $this->findAll($condition, $fields, 'EvaluationMixeval.id ASC', null, null, null, $joinTable );
40-
}
41-
4231
// gets Mixeval evaluation result for a specific assignment and evaluator
4332
function getResultsDetailByEvaluator($grpEventId=null, $evaluator=null) {
4433
$condition = 'EvaluationMixeval.grp_event_id='.$grpEventId.' AND EvaluationMixeval.evaluator='.$evaluator;
@@ -52,7 +41,7 @@ function getResultsDetailByEvaluator($grpEventId=null, $evaluator=null) {
5241
function getReceivedTotalScore($grpEventId=null, $evaluatee=null) {
5342
return $this->find('grp_event_id=' . $grpEventId .
5443
' AND evaluatee=' . $evaluatee,
55-
'AVG(score) AS received_total_score');
44+
'AVG(score) AS received_total_score', null, 0);
5645
}
5746

5847
// get total evaluator each member recieved

0 commit comments

Comments
 (0)