Last active
April 16, 2019 17:05
-
-
Save HackinwaleDev/90fd17e4d86e7f3befef9e7abb251cc9 to your computer and use it in GitHub Desktop.
Some bugs here I don't know why
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Get performance trend [=>Not getting desired result yet.] | |
public function get_performance_trend(){ | |
global $DB; | |
//Query for all enrolled course first | |
$sql = "select c.id as id, c.shortname,c.category from {course} c | |
inner join {enrol} e on e.courseid = c.id | |
inner join {user_enrolments} u on e.id=u.enrolid | |
where u.userid=15"; //total is 6 | |
$courses = $DB->get_records_sql($sql); | |
$i = 0; | |
$data = array(); | |
$sql = 'select id, name from {course_categories} where parent=0'; | |
$primaryLevels = $DB->get_records_sql($sql); | |
foreach($primaryLevels as $category){ | |
$sumperformance = 0; | |
$avgperformance = 0; | |
$pcount = 0; | |
print('category '.$category->id."\n"); | |
//every course avg grade | |
foreach ($courses as $course){ | |
$flc = 0; | |
//check which category the course belong (Primary 1-6) | |
$flc = $this->checkFirstLevelCategory($course->category); | |
//print the course we are dealing with | |
print('Course with category '.$flc); | |
print_r(json_encode($course)); | |
if($category->id == $flc){ | |
print("\n".'Category test passed'."\n"); | |
//get user grade of this quiz | |
$sumgrade=0; | |
$quizcount=0; | |
$avggrade=0; | |
// print(' courseid '.$course->id); | |
$sql ="select id from {quiz} where course=".$course->id; | |
$coursequizs = $DB->get_records_sql($sql); | |
//there will be many quizs of the course. | |
foreach ($coursequizs as $coursequiz){ | |
//select avg(sumgrades)... won't work cuz sumgrades has NULL value | |
$sqlcoursequizgrade ="select sumgrades from {quiz_attempts} where quiz=".$coursequiz->id." and userid=15 order by timemodified desc limit 1"; | |
$grade = $DB->get_records_sql($sqlcoursequizgrade); | |
// print_r($grade); | |
foreach($grade as $grade){ | |
$sumgrade+=(float)$grade->sumgrades; | |
print_r($sumgrade); | |
$quizcount++; | |
}//foreach grade | |
if($quizcount!=0){ | |
$avggrade=(float)$sumgrade/$quizcount; | |
} | |
}//foreach quiz | |
// $data[] = array('course'=>$course->shortname, 'grade'=> (float)$avggrade); | |
$sumperformance += $avggrade; | |
// print('Sum performance '.$sumperformance); | |
$pcount++; | |
}//end if | |
}//foreach course | |
if($pcount != 0){ //validate to avoid infinity error | |
print('pcount '.$pcount); | |
$avgperformance = $sumperformance/$pcount; | |
} | |
//save into the array | |
$data[] = array('form'=>$category->name, 'performance'=>$avgperformance); | |
$i++; | |
}//foreach category | |
print('Loopcount '.$i); | |
if(empty($data[0])){ | |
$data[0] = array('form'=>"NoRecord", 'performance'=> (float)0); | |
} | |
$this->set_graphic_type('linechart'); | |
return $this->generate_chart(json_encode($data));//convert to json object | |
}//end get_performance_trend | |
//utility function for performance trend | |
public function checkFirstLevelCategory($cid){ | |
global $DB; | |
$sql = 'select parent from {course_categories} where id='.$cid; | |
$parent = $DB->get_record_sql($sql); | |
if($parent->parent != 0){ | |
$this->checkFirstLevelCategory(($parent->parent)); | |
}else{ | |
print("\n".'cFLC() returns '.$cid); | |
return $cid; | |
} | |
}//end getFirstLevelCategory |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because the method does not return the value on line 88.