} //complete course data with site name and course content $courseids = array(); $courseimagenumbers = array(); //number of screenshots of all courses (must be exact same order than $courseids) if (!empty($courses)) { foreach ($courses as $tmpcourse) { $courseids[] = $tmpcourse->id; } $courseidslist = implode(',', $courseids); $sites = $DB->get_records_sql('SELECT site.* FROM {hub_course_directory} course, {hub_site_directory} site WHERE ' . 'course.id IN (' . $courseidslist . ') AND site.id = course.siteid GROUP BY site.id'); foreach ($courses as &$course) { //get site name for each courses $course->site = $sites[$course->siteid]; //get course content for each course $contents = $hub->get_course_contents($course->id); if (!empty($contents)) { foreach ($contents as $content) { $course->contents[] = $content; } } //some information for the YUI imagegallery javascript $courseimagenumbers[] = $course->screenshots; } //add rating information to the course require_once $CFG->dirroot . '/rating/lib.php'; $ratingoptions = new stdclass(); $ratingoptions->context = context_course::instance(SITEID); //front page course $ratingoptions->items = $courses; $ratingoptions->aggregate = RATING_AGGREGATE_COUNT;
/** * Get courses * @return array courses */ public static function get_courses($search, $downloadable, $enrollable, $options = array()) { global $DB, $CFG, $USER; // Ensure the current user is allowed to run this function $context = context_system::instance(); self::validate_context($context); require_capability('local/hub:view', $context); $params = self::validate_parameters(self::get_courses_parameters(), array('search' => $search, 'downloadable' => $downloadable, 'enrollable' => $enrollable, 'options' => $options)); //retieve siteid $onlyvisible = true; $token = optional_param('wstoken', '', PARAM_ALPHANUM); $localhub = new local_hub(); $communication = $localhub->get_communication(WSSERVER, REGISTEREDSITE, null, $token); if (!empty($communication)) { $siteurl = $communication->remoteurl; if (!empty($siteurl)) { $site = $localhub->get_site_by_url($siteurl); if (!empty($site) and !empty($params['options']['allsitecourses'])) { $params['options']['siteid'] = $site->id; $onlyvisible = false; } } } $cleanedoptions = $params['options']; $cleanedoptions['onlyvisible'] = $onlyvisible; $cleanedoptions['search'] = $params['search']; $cleanedoptions['downloadable'] = $params['downloadable']; $cleanedoptions['enrollable'] = $params['enrollable']; //sort method if (!empty($params['options']['orderby'])) { switch ($params['options']['orderby']) { case 'newest': $cleanedoptions['orderby'] = 'timemodified DESC'; break; case 'eldest': $cleanedoptions['orderby'] = 'timemodified ASC'; break; case 'publisher': $cleanedoptions['orderby'] = 'publishername ASC'; break; case 'fullname': $cleanedoptions['orderby'] = 'fullname ASC'; break; case 'ratingaverage': $cleanedoptions['orderby'] = 'ratingaverage DESC'; break; default: unset($cleanedoptions['orderby']); break; } } //retrieve the range of courses to return $maxcourses = get_config('local_hub', 'maxwscourseresult'); if (empty($maxcourses)) { throw new moodle_exception('nocoursereturn', 'local_hub'); } $hub = new local_hub(); //the site is requesting all his own course if (!empty($params['options']['siteid'])) { $maxcourses = 0; $limitfrom = 0; } else { //the site is doing a normal courses request (not focussed on its own courses) //the hub server limit the return number of course $limitfrom = isset($params['options']['givememore']) ? $params['options']['givememore'] : 0; } $courses = $hub->get_courses($cleanedoptions, $limitfrom, $maxcourses); $coursetotal = $hub->get_courses($cleanedoptions, 0, 0, true); //load ratings and comments if (!empty($courses)) { require_once $CFG->dirroot . '/comment/lib.php'; } //create result $coursesresult = array(); foreach ($courses as $course) { $courseinfo = array(); $courseinfo['id'] = $course->id; $courseinfo['fullname'] = $course->fullname; $courseinfo['shortname'] = $course->shortname; $courseinfo['description'] = $course->description; $courseinfo['language'] = $course->language; $courseinfo['publishername'] = $course->publishername; //return publisher email, privacy and site course id // only if the request has been done by the site if (!empty($site) and $course->siteid == $site->id) { $courseinfo['publisheremail'] = $course->publisheremail; $courseinfo['privacy'] = $course->privacy; $courseinfo['sitecourseid'] = $course->sitecourseid; } $courseinfo['contributornames'] = $course->contributornames; $courseinfo['coverage'] = $course->coverage; $courseinfo['creatorname'] = $course->creatorname; $courseinfo['licenceshortname'] = $course->licenceshortname; $courseinfo['subject'] = $course->subject; $courseinfo['audience'] = $course->audience; $courseinfo['educationallevel'] = $course->educationallevel; $courseinfo['creatornotes'] = $course->creatornotes; $courseinfo['creatornotesformat'] = $course->creatornotesformat; $courseinfo['enrollable'] = $course->enrollable; $courseinfo['screenshots'] = $course->screenshots; $courseinfo['timemodified'] = $course->timemodified; if (!empty($course->courseurl)) { $courseinfo['courseurl'] = $course->courseurl; } else { if (!empty($course->demourl)) { //courseurl is mandatory, demo url can be blank $courseinfo['demourl'] = $course->demourl; } else { $courseurl = new moodle_url($CFG->wwwroot, array('sesskey' => sesskey(), 'redirectcourseid' => $course->id)); $courseinfo['demourl'] = $courseurl->out(false); } } //outcomes if (!empty($course->outcomes)) { foreach ($course->outcomes as $outcome) { $courseinfo['outcomes'][] = array('fullname' => $outcome); } } //get content $contents = $hub->get_course_contents($course->id); if (!empty($contents)) { foreach ($contents as $content) { $tmpcontent = array(); $tmpcontent['moduletype'] = $content->moduletype; $tmpcontent['modulename'] = $content->modulename; $tmpcontent['contentcount'] = $content->contentcount; $courseinfo['contents'][] = $tmpcontent; } } //set ratings if ($course->ratingcount) { //$courseinfo['rating']['aggregate'] = (float) $course->ratingaverage; //the ratings has been changed from scale 0 to 10 to a "Featured" award $courseinfo['rating']['aggregate'] = HUB_COURSE_RATING_SCALE; } $courseinfo['rating']['count'] = (int) $course->ratingcount; $courseinfo['rating']['scaleid'] = HUB_COURSE_RATING_SCALE; //get comments $commentoptions->context = context_course::instance(SITEID); $commentoptions->area = 'local_hub'; $commentoptions->itemid = $course->id; $commentoptions->showcount = true; $commentoptions->component = 'local_hub'; $course->comment = new comment($commentoptions); $comments = $course->comment->get_comments(); foreach ($comments as $comment) { $coursecomment = array(); $coursecomment['comment'] = clean_param($comment->content, PARAM_TEXT); $coursecomment['commentator'] = clean_param($comment->fullname, PARAM_TEXT); $coursecomment['date'] = $comment->timecreated; $courseinfo['comments'][] = $coursecomment; } //get backup size $returnthecourse = true; if (!$course->enrollable) { if ($hub->backup_exits($course->id)) { $courseinfo['backupsize'] = $hub->get_backup_size($course->id); } else { // We don't return the course when backup file is not found. $returnthecourse = false; } } if ($returnthecourse) { $coursesresult[] = $courseinfo; } } return array('courses' => $coursesresult, 'coursetotal' => $coursetotal); }