public function get_content()
 {
     global $USER, $CFG, $DB, $COURSE;
     // If content is cached
     if ($this->content !== NULL) {
         return $this->content;
     }
     // Create empty content
     $this->content = new stdClass();
     // Can edit settings?
     $can_edit = has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $this->page->course->id));
     // Get course completion data
     $info = new completion_info($this->page->course);
     // Don't display if completion isn't enabled!
     if (!completion_info::is_enabled_for_site()) {
         if ($can_edit) {
             $this->content->text = get_string('completionnotenabledforsite', 'completion');
         }
         return $this->content;
     } else {
         if (!$info->is_enabled()) {
             if ($can_edit) {
                 $this->content->text = get_string('completionnotenabledforcourse', 'completion');
             }
             return $this->content;
         }
     }
     // Load criteria to display
     $completions = $info->get_completions($USER->id);
     // Check if this course has any criteria
     if (empty($completions)) {
         if ($can_edit) {
             $this->content->text = get_string('nocriteriaset', 'completion');
         }
         return $this->content;
     }
     // Check this user is enroled
     if (!$info->is_tracked_user($USER->id)) {
         // If not enrolled, but are can view the report:
         if (has_capability('report/completion:view', get_context_instance(CONTEXT_COURSE, $COURSE->id))) {
             $this->content->text = '<a href="' . $CFG->wwwroot . '/report/completion/index.php?course=' . $COURSE->id . '">' . get_string('viewcoursereport', 'completion') . '</a>';
             return $this->content;
         }
         // Otherwise, show error
         $this->content->text = get_string('notenroled', 'completion');
         return $this->content;
     }
     // Generate markup for criteria statuses
     $shtml = '';
     // For aggregating activity completion
     $activities = array();
     $activities_complete = 0;
     // For aggregating course prerequisites
     $prerequisites = array();
     $prerequisites_complete = 0;
     // Flag to set if current completion data is inconsistent with
     // what is stored in the database
     $pending_update = false;
     // Loop through course criteria
     foreach ($completions as $completion) {
         $criteria = $completion->get_criteria();
         $complete = $completion->is_complete();
         if (!$pending_update && $criteria->is_pending($completion)) {
             $pending_update = true;
         }
         // Activities are a special case, so cache them and leave them till last
         if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
             $activities[$criteria->moduleinstance] = $complete;
             if ($complete) {
                 $activities_complete++;
             }
             continue;
         }
         // Prerequisites are also a special case, so cache them and leave them till last
         if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_COURSE) {
             $prerequisites[$criteria->courseinstance] = $complete;
             if ($complete) {
                 $prerequisites_complete++;
             }
             continue;
         }
         $shtml .= '<tr><td>';
         $shtml .= $criteria->get_title();
         $shtml .= '</td><td style="text-align: right">';
         $shtml .= $completion->get_status();
         $shtml .= '</td></tr>';
     }
     // Aggregate activities
     if (!empty($activities)) {
         $shtml .= '<tr><td>';
         $shtml .= get_string('activitiescompleted', 'completion');
         $shtml .= '</td><td style="text-align: right">';
         $a = new stdClass();
         $a->first = $activities_complete;
         $a->second = count($activities);
         $shtml .= get_string('firstofsecond', 'block_completionstatus', $a);
         $shtml .= '</td></tr>';
     }
     // Aggregate prerequisites
     if (!empty($prerequisites)) {
         $phtml = '<tr><td>';
         $phtml .= get_string('prerequisitescompleted', 'completion');
         $phtml .= '</td><td style="text-align: right">';
         $a = new stdClass();
         $a->first = $prerequisites_complete;
         $a->second = count($prerequisites);
         $shtml .= get_string('firstofsecond', 'block_completionstatus', $a);
         $phtml .= '</td></tr>';
         $shtml = $phtml . $shtml;
     }
     // Display completion status
     $this->content->text = '<table width="100%" style="font-size: 90%;"><tbody>';
     $this->content->text .= '<tr><td colspan="2"><b>' . get_string('status') . ':</b> ';
     // Is course complete?
     $coursecomplete = $info->is_course_complete($USER->id);
     // Load course completion
     $params = array('userid' => $USER->id, 'course' => $COURSE->id);
     $ccompletion = new completion_completion($params);
     // Has this user completed any criteria?
     $criteriacomplete = $info->count_course_user_data($USER->id);
     if ($pending_update) {
         $this->content->text .= '<i>' . get_string('pending', 'completion') . '</i>';
     } else {
         if ($coursecomplete) {
             $this->content->text .= get_string('complete');
         } else {
             if (!$criteriacomplete && !$ccompletion->timestarted) {
                 $this->content->text .= '<i>' . get_string('notyetstarted', 'completion') . '</i>';
             } else {
                 $this->content->text .= '<i>' . get_string('inprogress', 'completion') . '</i>';
             }
         }
     }
     $this->content->text .= '</td></tr>';
     $this->content->text .= '<tr><td colspan="2">';
     // Get overall aggregation method
     $overall = $info->get_aggregation_method();
     if ($overall == COMPLETION_AGGREGATION_ALL) {
         $this->content->text .= get_string('criteriarequiredall', 'completion');
     } else {
         $this->content->text .= get_string('criteriarequiredany', 'completion');
     }
     $this->content->text .= ':</td></tr>';
     $this->content->text .= '<tr><td><b>' . get_string('requiredcriteria', 'completion') . '</b></td><td style="text-align: right"><b>' . get_string('status') . '</b></td></tr>';
     $this->content->text .= $shtml . '</tbody></table>';
     // Display link to detailed view
     $this->content->footer = '<br><a href="' . $CFG->wwwroot . '/blocks/completionstatus/details.php?course=' . $COURSE->id . '">' . get_string('moredetails', 'completion') . '</a>';
     return $this->content;
 }
Exemple #2
0
// Load course completion.
$params = array('userid' => $user->id, 'course' => $course->id);
$ccompletion = new completion_completion($params);
if ($coursecomplete) {
    echo get_string('complete');
} else {
    if (!$criteriacomplete && !$ccompletion->timestarted) {
        echo html_writer::tag('i', get_string('notyetstarted', 'completion'));
    } else {
        echo html_writer::tag('i', get_string('inprogress', 'completion'));
    }
}
echo html_writer::end_tag('td');
echo html_writer::end_tag('tr');
// Load criteria to display.
$completions = $info->get_completions($user->id);
// Check if this course has any criteria.
if (empty($completions)) {
    echo html_writer::start_tag('tr');
    echo html_writer::start_tag('td', array('colspan' => '2'));
    echo html_writer::start_tag('br');
    echo $OUTPUT->box(get_string('err_nocriteria', 'completion'), 'noticebox');
    echo html_writer::end_tag('td');
    echo html_writer::end_tag('tr');
    echo html_writer::end_tag('tbody');
    echo html_writer::end_tag('table');
} else {
    echo html_writer::start_tag('tr');
    echo html_writer::start_tag('td', array('colspan' => '2'));
    echo html_writer::tag('b', get_string('required'));
    // Get overall aggregation method.
Exemple #3
0
 /**
  * Get Course completion status
  *
  * @param int $courseid ID of the Course
  * @param int $userid ID of the User
  * @return array of course completion status and warnings
  * @since Moodle 2.9
  * @throws moodle_exception
  */
 public static function get_course_completion_status($courseid, $userid)
 {
     global $CFG, $USER;
     require_once $CFG->libdir . '/grouplib.php';
     $warnings = array();
     $arrayparams = array('courseid' => $courseid, 'userid' => $userid);
     $params = self::validate_parameters(self::get_course_completion_status_parameters(), $arrayparams);
     $course = get_course($params['courseid']);
     $user = core_user::get_user($params['userid'], 'id', MUST_EXIST);
     $context = context_course::instance($course->id);
     self::validate_context($context);
     // Can current user see user's course completion status?
     // This check verifies if completion is enabled because $course is mandatory.
     if (!completion_can_view_data($user->id, $course)) {
         throw new moodle_exception('cannotviewreport');
     }
     // The previous function doesn't check groups.
     if ($user->id != $USER->id) {
         if (!groups_user_groups_visible($course, $user->id)) {
             // We are not in the same group!
             throw new moodle_exception('accessdenied', 'admin');
         }
     }
     $info = new completion_info($course);
     // Check this user is enroled.
     if (!$info->is_tracked_user($user->id)) {
         if ($USER->id == $user->id) {
             throw new moodle_exception('notenroled', 'completion');
         } else {
             throw new moodle_exception('usernotenroled', 'completion');
         }
     }
     $completions = $info->get_completions($user->id);
     if (empty($completions)) {
         throw new moodle_exception('nocriteriaset', 'completion');
     }
     // Load course completion.
     $completionparams = array('userid' => $user->id, 'course' => $course->id);
     $ccompletion = new completion_completion($completionparams);
     $completionrows = array();
     // Loop through course criteria.
     foreach ($completions as $completion) {
         $criteria = $completion->get_criteria();
         $completionrow = array();
         $completionrow['type'] = $criteria->criteriatype;
         $completionrow['title'] = $criteria->get_title();
         $completionrow['status'] = $completion->get_status();
         $completionrow['complete'] = $completion->is_complete();
         $completionrow['timecompleted'] = $completion->timecompleted;
         $completionrow['details'] = $criteria->get_details($completion);
         $completionrows[] = $completionrow;
     }
     $result = array('completed' => $info->is_course_complete($user->id), 'aggregation' => $info->get_aggregation_method(), 'completions' => $completionrows);
     $results = array('completionstatus' => $result, 'warnings' => $warnings);
     return $results;
 }
 public function get_content()
 {
     global $USER;
     $rows = array();
     $srows = array();
     $prows = array();
     // If content is cached.
     if ($this->content !== null) {
         return $this->content;
     }
     $course = $this->page->course;
     $context = context_course::instance($course->id);
     // Create empty content.
     $this->content = new stdClass();
     $this->content->text = '';
     $this->content->footer = '';
     // Can edit settings?
     $can_edit = has_capability('moodle/course:update', $context);
     // Get course completion data.
     $info = new completion_info($course);
     // Don't display if completion isn't enabled!
     if (!completion_info::is_enabled_for_site()) {
         if ($can_edit) {
             $this->content->text .= get_string('completionnotenabledforsite', 'completion');
         }
         return $this->content;
     } else {
         if (!$info->is_enabled()) {
             if ($can_edit) {
                 $this->content->text .= get_string('completionnotenabledforcourse', 'completion');
             }
             return $this->content;
         }
     }
     // Load criteria to display.
     $completions = $info->get_completions($USER->id);
     // Check if this course has any criteria.
     if (empty($completions)) {
         if ($can_edit) {
             $this->content->text .= get_string('nocriteriaset', 'completion');
         }
         return $this->content;
     }
     // Check this user is enroled.
     if ($info->is_tracked_user($USER->id)) {
         // Generate markup for criteria statuses.
         $data = '';
         // For aggregating activity completion.
         $activities = array();
         $activities_complete = 0;
         // For aggregating course prerequisites.
         $prerequisites = array();
         $prerequisites_complete = 0;
         // Flag to set if current completion data is inconsistent with what is stored in the database.
         $pending_update = false;
         // Loop through course criteria.
         foreach ($completions as $completion) {
             $criteria = $completion->get_criteria();
             $complete = $completion->is_complete();
             if (!$pending_update && $criteria->is_pending($completion)) {
                 $pending_update = true;
             }
             // Activities are a special case, so cache them and leave them till last.
             if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
                 $activities[$criteria->moduleinstance] = $complete;
                 if ($complete) {
                     $activities_complete++;
                 }
                 continue;
             }
             // Prerequisites are also a special case, so cache them and leave them till last.
             if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_COURSE) {
                 $prerequisites[$criteria->courseinstance] = $complete;
                 if ($complete) {
                     $prerequisites_complete++;
                 }
                 continue;
             }
             $row = new html_table_row();
             $row->cells[0] = new html_table_cell($criteria->get_title());
             $row->cells[1] = new html_table_cell($completion->get_status());
             $row->cells[1]->style = 'text-align: right;';
             $srows[] = $row;
         }
         // Aggregate activities.
         if (!empty($activities)) {
             $a = new stdClass();
             $a->first = $activities_complete;
             $a->second = count($activities);
             $row = new html_table_row();
             $row->cells[0] = new html_table_cell(get_string('activitiescompleted', 'completion'));
             $row->cells[1] = new html_table_cell(get_string('firstofsecond', 'block_completionstatus', $a));
             $row->cells[1]->style = 'text-align: right;';
             $srows[] = $row;
         }
         // Aggregate prerequisites.
         if (!empty($prerequisites)) {
             $a = new stdClass();
             $a->first = $prerequisites_complete;
             $a->second = count($prerequisites);
             $row = new html_table_row();
             $row->cells[0] = new html_table_cell(get_string('dependenciescompleted', 'completion'));
             $row->cells[1] = new html_table_cell(get_string('firstofsecond', 'block_completionstatus', $a));
             $row->cells[1]->style = 'text-align: right;';
             $prows[] = $row;
             $srows = array_merge($prows, $srows);
         }
         // Display completion status.
         $table = new html_table();
         $table->width = '100%';
         $table->attributes = array('style' => 'font-size: 90%;', 'class' => '');
         $row = new html_table_row();
         $content = html_writer::tag('b', get_string('status') . ': ');
         // Is course complete?
         $coursecomplete = $info->is_course_complete($USER->id);
         // Load course completion.
         $params = array('userid' => $USER->id, 'course' => $course->id);
         $ccompletion = new completion_completion($params);
         // Has this user completed any criteria?
         $criteriacomplete = $info->count_course_user_data($USER->id);
         if ($pending_update) {
             $content .= html_writer::tag('i', get_string('pending', 'completion'));
         } else {
             if ($coursecomplete) {
                 $content .= get_string('complete');
             } else {
                 if (!$criteriacomplete && !$ccompletion->timestarted) {
                     $content .= html_writer::tag('i', get_string('notyetstarted', 'completion'));
                 } else {
                     $content .= html_writer::tag('i', get_string('inprogress', 'completion'));
                 }
             }
         }
         $row->cells[0] = new html_table_cell($content);
         $row->cells[0]->colspan = '2';
         $rows[] = $row;
         $row = new html_table_row();
         $content = "";
         // Get overall aggregation method.
         $overall = $info->get_aggregation_method();
         if ($overall == COMPLETION_AGGREGATION_ALL) {
             $content .= get_string('criteriarequiredall', 'completion');
         } else {
             $content .= get_string('criteriarequiredany', 'completion');
         }
         $content .= ':';
         $row->cells[0] = new html_table_cell($content);
         $row->cells[0]->colspan = '2';
         $rows[] = $row;
         $row = new html_table_row();
         $row->cells[0] = new html_table_cell(html_writer::tag('b', get_string('requiredcriteria', 'completion')));
         $row->cells[1] = new html_table_cell(html_writer::tag('b', get_string('status')));
         $row->cells[1]->style = 'text-align: right;';
         $rows[] = $row;
         // Array merge $rows and $data here.
         $rows = array_merge($rows, $srows);
         $table->data = $rows;
         $this->content->text .= html_writer::table($table);
         // Display link to detailed view.
         $details = new moodle_url('/blocks/completionstatus/details.php', array('course' => $course->id));
         $this->content->footer .= html_writer::link($details, get_string('moredetails', 'completion'));
     } else {
         // If user is not enrolled, show error.
         $this->content->text = get_string('nottracked', 'completion');
     }
     if (has_capability('report/completion:view', $context)) {
         $report = new moodle_url('/report/completion/index.php', array('course' => $course->id));
         if (empty($this->content->footer)) {
             $this->content->footer = '';
         }
         $this->content->footer .= html_writer::empty_tag('br');
         $this->content->footer .= html_writer::link($report, get_string('viewcoursereport', 'completion'));
     }
     return $this->content;
 }
 $completion = new completion_info($course);
 $trackeduser = $user ? $user : $USER->id;
 if (!$completion->is_enabled()) {
     throw new moodle_exception('completionnotenabled', 'completion');
 } else {
     if (!$completion->is_tracked_user($trackeduser)) {
         throw new moodle_exception('nottracked', 'completion');
     }
 }
 if ($user && $rolec) {
     require_sesskey();
     completion_criteria::factory(array('id' => $rolec, 'criteriatype' => COMPLETION_CRITERIA_TYPE_ROLE));
     //TODO: this is dumb, because it does not fetch the data?!?!
     $criteria = completion_criteria_role::fetch(array('id' => $rolec));
     if ($criteria and user_has_role_assignment($USER->id, $criteria->role, $context->id)) {
         $criteria_completions = $completion->get_completions($user, COMPLETION_CRITERIA_TYPE_ROLE);
         foreach ($criteria_completions as $criteria_completion) {
             if ($criteria_completion->criteriaid == $rolec) {
                 $criteria->complete($criteria_completion);
                 break;
             }
         }
     }
     // Return to previous page
     $referer = clean_param($_SERVER['HTTP_REFERER'], PARAM_LOCALURL);
     if (!empty($referer)) {
         redirect($referer);
     } else {
         redirect('view.php?id=' . $course->id);
     }
 } else {
    public function get_content()
    {
        global $USER, $CFG, $DB, $COURSE;
        if ($this->content !== null) {
            return $this->content;
        }
        $this->content = new stdClass();
        $courses1 = enrol_get_my_courses('', 'visible DESC,sortorder ASC');
        //defined in lib/enrollib.php file
        // Display completion status
        $this->content->text .= '<table cellpadding="0" cellspacing="0" width="178" border="0">';
        $this->content->text .= '<tr>';
        $this->content->text .= '<td width="85" align="right"><b>Total</b>';
        $this->content->text .= '</td><td align="left"><b>Over</b>';
        $this->content->text .= '</td><td align="left"><b>%</b>';
        $this->content->text .= '</td></tr></table>';
        $this->content->text .= '<table cellpadding="0" cellspacing="0" width="178" border="0">';
        foreach ($courses1 as $course1) {
            if ($course1->id == SITEID) {
                continue;
            }
            $userid = optional_param('user', 0, PARAM_INT);
            // Load course
            $course = $DB->get_record('course', array('id' => $course1->id));
            // Load user
            if ($userid) {
                $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
            } else {
                $user = $USER;
            }
            $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
            $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
            $can_view = false;
            // Can view own report
            if ($USER->id == $user->id) {
                $can_view = true;
            } else {
                if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)) {
                    $can_view = true;
                } else {
                    if (has_capability('coursereport/completion:view', $coursecontext)) {
                        $can_view = true;
                    } else {
                        if (has_capability('coursereport/completion:view', $personalcontext)) {
                            $can_view = true;
                        }
                    }
                }
            }
            if (!$can_view) {
                $this->content->text .= print_error('cannotviewreport');
            }
            // Load completion data
            $info = new completion_info($course);
            // Load criteria to display
            $completions = $info->get_completions($user->id);
            // Check this user is enroled
            if (!$info->is_tracked_user($user->id)) {
                if ($USER->id == $user->id) {
                    $this->content->text .= print_error('notenroled', 'completion', $returnurl);
                } else {
                    $this->content->text .= print_error('usernotenroled', 'completion', $returnurl);
                }
            }
            // Is course complete?
            $coursecomplete = $info->is_course_complete($user->id);
            // Has this user completed any criteria?
            $criteriacomplete = $info->count_course_user_data($user->id);
            if ($coursecomplete) {
                $status = get_string('complete');
            } else {
                if (!$criteriacomplete) {
                    $status = '<i>' . get_string('notyetstarted', 'completion') . '</i>';
                } else {
                    $status = '<i>' . get_string('inprogress', 'completion') . '</i>';
                }
            }
            //GET TOTAL NUMBER OF TASKS ASSIGNED TO CURRENT USER
            $conditions = array("course" => $course->id);
            $tot_rec = $DB->count_records('course_completion_criteria', $conditions);
            //GET TOTAL NUMBER OF TASKS ASSIGNED TO CURRENT USER
            $result = $DB->get_records_sql('SELECT mc.coursemoduleid,mc.userid,mc.completionstate,
				m.id,m.course FROM `mdl_course_modules_completion` mc 
				LEFT JOIN 
				mdl_course_modules m
				ON
				mc.coursemoduleid=m.id WHERE mc.userid = ? AND m.course = ?', array($user->id, $course->id));
            $tot_comp = count($result);
            $count1 = $tot_comp / $tot_rec;
            $count2 = $count1 * 100;
            $count = number_format($count2, 0);
            $linkhref = new moodle_url('/course/view.php', array('id' => $course->id));
            $this->content->text .= '<tr><td colspan="4"><b title="Course" style="color:#000066;">';
            $this->content->text .= '<a href="' . $linkhref . '">' . $course1->fullname . '</a>';
            $this->content->text .= '</b></td></tr>';
            $this->content->text .= '<tr><td align="left" width="70" title="Activity completion status">';
            //$this->content->text .=$course1->fullname;
            $this->content->text .= $status;
            $this->content->text .= '</td><td align="left" width="27" title="Total activities">' . $tot_rec;
            $this->content->text .= '</td><td width="27" align="left" title="Total Activities completed">' . $tot_comp;
            $this->content->text .= '</td><td width="26" align="right" title="Activities completed(%)">' . $count . '%';
            $this->content->text .= '</td></tr>';
        }
        $this->content->text .= '</table>';
        //$this->content->footer .= 'Footer here...';
        return $this->content;
    }
 /**
  * Get user information
  *
  * @param string $username EBS username, could be 8-digit int or string.
  * @return array An array describing targets (and metadata) for that user for all leapcore_* courses.
  */
 public static function get_targets_by_username($username)
 {
     global $CFG, $DB;
     $params = self::validate_parameters(self::get_targets_by_username_parameters(), array('username' => $username));
     if ($params['username'] == '') {
         header($_SERVER["SERVER_PROTOCOL"] . ' 422 Unprocessable Entity ($params[\'username\'] empty.)', true, 422);
         exit(1);
     }
     // Require for the course completion code.
     require_once $CFG->libdir . '/completionlib.php';
     // Could do with knowing what this user's {user}.id is.
     $sql = "SELECT id from {user} WHERE username LIKE ?;";
     if (!($user = $DB->get_record_sql($sql, array($params['username'] . '%')))) {
         header($_SERVER["SERVER_PROTOCOL"] . ' 422 Unprocessable Entity ($params[\'username\'] could not be matched against a valid user.)', true, 422);
         exit(1);
     }
     $cores = array('core' => 'leapcore_core', 'english' => 'leapcore_english', 'maths' => 'leapcore_maths', 'ppd' => 'leapcore_ppd', 'test' => 'leapcore_test', 'a2_artdes' => 'leapcore_a2_artdes', 'a2_artdesphoto' => 'leapcore_a2_artdesphoto', 'a2_artdestext' => 'leapcore_a2_artdestext', 'a2_biology' => 'leapcore_a2_biology', 'a2_busstud' => 'leapcore_a2_busstud', 'a2_chemistry' => 'leapcore_a2_chemistry', 'a2_englishlang' => 'leapcore_a2_englishlang', 'a2_englishlit' => 'leapcore_a2_englishlit', 'a2_envsci' => 'leapcore_a2_envsci', 'a2_envstud' => 'leapcore_a2_envstud', 'a2_filmstud' => 'leapcore_a2_filmstud', 'a2_geography' => 'leapcore_a2_geography', 'a2_govpoli' => 'leapcore_a2_govpoli', 'a2_history' => 'leapcore_a2_history', 'a2_humanbiology' => 'leapcore_a2_humanbiology', 'a2_law' => 'leapcore_a2_law', 'a2_maths' => 'leapcore_a2_maths', 'a2_mathsfurther' => 'leapcore_a2_mathsfurther', 'a2_media' => 'leapcore_a2_media', 'a2_philosophy' => 'leapcore_a2_philosophy', 'a2_physics' => 'leapcore_a2_physics', 'a2_psychology' => 'leapcore_a2_psychology', 'a2_sociology' => 'leapcore_a2_sociology', 'btecex_applsci' => 'leapcore_btecex_applsci', 'gcse_english' => 'leapcore_gcse_english', 'gcse_maths' => 'leapcore_gcse_maths');
     // Define the target's names.
     $targets = array('TAG', 'L3VA', 'MAG');
     $courses = array();
     foreach ($cores as $core => $coresql) {
         $courses[$core]['leapcore'] = $core;
         // Checking for user enrolled as student role, manual enrolments only.
         $sql = "SELECT DISTINCT c.id AS courseid, c.shortname AS shortname, c.fullname AS fullname, username\n                FROM mdl_user u\n                    JOIN mdl_user_enrolments ue ON ue.userid = u.id\n                    JOIN mdl_enrol e ON e.id = ue.enrolid\n                        -- AND e.enrol = 'manual'\n                    JOIN mdl_role_assignments ra ON ra.userid = u.id\n                    JOIN mdl_context ct ON ct.id = ra.contextid\n                        AND ct.contextlevel = 50\n                    JOIN mdl_course c ON c.id = ct.instanceid\n                        AND e.courseid = c.id\n                    JOIN mdl_role r ON r.id = ra.roleid\n                        AND r.shortname = 'student'\n                WHERE c.idnumber LIKE '%|" . $coresql . "|%'\n                    AND u.username LIKE '" . $params['username'] . "%'\n                    AND e.status = 0\n                    AND u.suspended = 0\n                    AND u.deleted = 0\n                    AND (\n                        ue.timeend = 0\n                        OR ue.timeend > NOW()\n                    )\n                    AND ue.status = 0;";
         // There is potential here for a user to have more than one 'leapcore_core' course, but it's pretty unlikely.
         // We probably need to handle this better (at the moment the below function expects 0 or 1 results and fails).
         if (!($result = $DB->get_record_sql($sql))) {
             unset($courses[$core]);
             continue;
         } else {
             $courses[$core]['course_shortname'] = $result->shortname;
             $courses[$core]['course_fullname'] = $result->fullname;
             $courses[$core]['course_id'] = $result->courseid;
         }
         // Walk through a fair few objects to get the course's time modified, final grade and named grade.
         $gi = new grade_item();
         // The course item is actually the right one to use, even if it is null.
         $gi_item = $gi::fetch(array('courseid' => $courses[$core]['course_id'], 'itemtype' => 'course'));
         // This may get changed further down the script as we want the most recently changed item's date.
         $courses[$core]['course_total_modified'] = $gi_item->timemodified;
         $gg = new grade_grade();
         $gg_grade = $gg::fetch(array('itemid' => $gi_item->id, 'userid' => $user->id));
         // If the scale is going to be a U (or Refer, or Fail etc) as the L3VA is 0, pass null.
         if ($gg_grade && $gg_grade->finalgrade > 0) {
             $courses[$core]['course_total'] = $gg_grade->finalgrade;
             $gs = new grade_scale();
             $gs_scale = $gs::fetch(array('id' => $gi_item->scaleid));
             if ($gi_item->display != 0) {
                 // Check first for a non-zero 'display' variable, and run with that if found.
                 $courses[$core]['course_total_display'] = grade_format_gradevalue($gg_grade->finalgrade, $gi_item, true, $gi_item->display);
             } else {
                 if ($gs_scale) {
                     // See if we have a scale and use that if found.
                     $courses[$core]['course_total_display'] = $gs_scale->get_nearest_item($gg_grade->finalgrade);
                 } else {
                     if (is_numeric($gg_grade->finalgrade)) {
                         $courses[$core]['course_total_display'] = round($courses[$core]['course_total'], 0, PHP_ROUND_HALF_UP);
                     } else {
                         $courses[$core]['course_total_display'] = $courses[$core]['course_total'];
                     }
                 }
             }
         } else {
             $courses[$core]['course_total'] = 0;
             $courses[$core]['course_total_display'] = null;
         }
         // For each target, same as above.
         foreach ($targets as $target) {
             $gi = new grade_item();
             $gi_item = $gi::fetch(array('courseid' => $courses[$core]['course_id'], 'itemtype' => 'manual', 'itemname' => $target));
             $gg = new grade_grade();
             $gg_grade = $gg::fetch(array('itemid' => $gi_item->id, 'userid' => $user->id));
             $courses[$core][strtolower($target)] = $gg_grade->finalgrade;
             // Get the named result (e.g. 'merit') only for targets which are not L3VA.
             if ($target != 'L3VA') {
                 $gs = new grade_scale();
                 $gs_scale = $gs::fetch(array('id' => $gi_item->scaleid));
                 // Updating the most recently modified date if it's newer.
                 if ($gi_item->timemodified > $courses[$core]['course_total_modified']) {
                     $courses[$core]['course_total_modified'] = $gi_item->timemodified;
                 }
                 // If the scale is going to be a U (or Refer, or Fail etc) as the L3VA is 0, pass null.
                 if ($gg_grade->finalgrade > 0) {
                     // If there's no scale, just pass the data across.
                     if ($gs_scale) {
                         $courses[$core][strtolower($target) . '_display'] = $gs_scale->get_nearest_item($gg_grade->finalgrade);
                     } else {
                         $courses[$core][strtolower($target) . '_display'] = $gg_grade->finalgrade;
                     }
                 } else {
                     $courses[$core][strtolower($target) . '_display'] = null;
                 }
             } else {
                 $courses[$core][strtolower($target) . '_display'] = $courses[$core][strtolower($target)];
             }
             // Rounding.
             if (is_numeric($courses[$core][strtolower($target) . '_display'])) {
                 $courses[$core][strtolower($target) . '_display'] = round($courses[$core][strtolower($target) . '_display'], 2, PHP_ROUND_HALF_UP);
             }
         }
         // Default both of these to null.
         $courses[$core]['course_completion_total'] = null;
         $courses[$core]['course_completion_completed'] = null;
         // We could do with a course object to use.
         $sql = "SELECT id from {course} WHERE id LIKE ?;";
         if (!($thiscourse = $DB->get_record_sql($sql, array($courses[$core]['course_id'])))) {
             exit(1);
         }
         if (completion_info::is_enabled_for_site()) {
             $info = new completion_info($thiscourse);
             $completions = $info->get_completions($user->id);
             // If there's no completions, none have been configured so do nothing.
             if (!empty($completions)) {
                 $courses[$core]['course_completion_total'] = count($completions);
                 $courses[$core]['course_completion_completed'] = $info->count_course_user_data($user->id);
                 // Loop through each timecompleted value, ignore if null, update if more recent.
                 foreach ($completions as $completion) {
                     if (!is_null($completion->timecompleted) && $completion->timecompleted > $courses[$core]['course_total_modified']) {
                         $courses[$core]['course_total_modified'] = $completion->timecompleted;
                     }
                 }
             }
         }
         // END completion info enabled for site check.
         // Stress reduction code.
         $courses[$core]['meaning_of_life'] = '42';
         $courses[$core]['smiley_face'] = ':)';
         // Incomplete course check.
         // TODO: make this better. We scan through all four 'leapcore_' tags (and all the new A2 ones) and get the results,
         // but sometimes there aren't any.  So for the tags with no associated courses, we remove them.
         if (!isset($courses[$core]['course_shortname'])) {
             unset($courses[$core]);
         }
     }
     // END foreach $courses.
     if (!empty($courses)) {
         return $courses;
     }
 }