Skip to content

Instantly share code, notes, and snippets.

@HackinwaleDev
Last active April 16, 2019 17:05
Show Gist options
  • Save HackinwaleDev/90fd17e4d86e7f3befef9e7abb251cc9 to your computer and use it in GitHub Desktop.
Save HackinwaleDev/90fd17e4d86e7f3befef9e7abb251cc9 to your computer and use it in GitHub Desktop.
Some bugs here I don't know why
//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
@HackinwaleDev
Copy link
Author

I don't know why $flc is not storing the value returned from checkFirstLevelCategory()

@mudrd8mz
Copy link

To correctly implemented the recursion, the line 88 should probably read

return $this->checkFirstLevelCategory(($parent->parent));

@mudrd8mz
Copy link

mudrd8mz commented Apr 16, 2019

why $flc is not storing the value returned

Because the method does not return the value on line 88.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment