Example #1
0
 /**
  * This function display the hub homepage
  * It is called early when loading any Moodle page.
  * @return integer return true if Moodle index.php home page must continue normal display
  */
 public function display_homepage()
 {
     global $PAGE, $SITE, $OUTPUT, $CFG, $USER;
     //check if the front page search should not be displayed
     //=> hand over the home page to Moodle index.php
     //Two cases possible:
     //1- the hub is private and the users are not logged in
     //2- the hub is set with no search form on the login page
     $hubprivacy = get_config('local_hub', 'privacy');
     $searchfornologin = get_config('local_hub', 'searchfornologin');
     if (($hubprivacy == HUBPRIVATE or $searchfornologin === '0') and !isloggedin()) {
         return true;
     }
     require_once $CFG->dirroot . "/local/hub/forms.php";
     $PAGE->set_url('/');
     $PAGE->set_pagetype('site-index');
     $PAGE->set_docs_path('');
     $PAGE->set_pagelayout('frontpage');
     $PAGE->set_title($SITE->fullname);
     $PAGE->set_heading($SITE->fullname);
     //little trick to require login in order to rate or comment
     $mustbelogged = optional_param('mustbelogged', false, PARAM_BOOL);
     if ($mustbelogged) {
         require_login();
     }
     //log redirection to a course page
     $redirectcourseid = optional_param('redirectcourseid', false, PARAM_INT);
     if (!empty($redirectcourseid)) {
         //do not check sesskey because can be call by RSS feed
         $course = $this->get_course($redirectcourseid);
         if (!empty($course->courseurl)) {
             $courseurl = new moodle_url($course->courseurl);
         } else {
             if (!empty($course->demourl)) {
                 $courseurl = new moodle_url($course->demourl);
             } else {
                 //we try to display a demo site but none has been set
                 echo $OUTPUT->header();
                 echo get_string('nodemo', 'local_hub');
                 echo $OUTPUT->footer();
                 die;
             }
         }
         $rss = optional_param('rss', false, PARAM_BOOL);
         $rss = empty($rss) ? '' : 'rss';
         add_to_log(SITEID, 'local_hub', 'course redirection ' . $rss, '', $redirectcourseid);
         redirect(new moodle_url($courseurl));
     }
     $search = optional_param('search', null, PARAM_TEXT);
     $renderer = $PAGE->get_renderer('local_hub');
     //forms
     //Warning: because we want to support GET and we want people to be able to give the url,
     // we need to bypass the sesskey form checking
     $_GET['sesskey'] = sesskey();
     $fromformdata['coverage'] = optional_param('coverage', 'all', PARAM_TEXT);
     $fromformdata['licence'] = optional_param('licence', 'all', PARAM_ALPHANUMEXT);
     $fromformdata['subject'] = optional_param('subject', 'all', PARAM_ALPHANUMEXT);
     $fromformdata['siteid'] = optional_param('siteid', 'all', PARAM_ALPHANUMEXT);
     $fromformdata['lastmodified'] = optional_param('lastmodified', HUB_LASTMODIFIED_WEEK, PARAM_ALPHANUMEXT);
     $fromformdata['audience'] = optional_param('audience', 'all', PARAM_ALPHANUMEXT);
     $currentlanguage = explode('_', current_language());
     $fromformdata['language'] = optional_param('language', $currentlanguage[0], PARAM_ALPHANUMEXT);
     $fromformdata['educationallevel'] = optional_param('educationallevel', 'all', PARAM_ALPHANUMEXT);
     $fromformdata['downloadable'] = optional_param('downloadable', 1, PARAM_ALPHANUM);
     $fromformdata['orderby'] = optional_param('orderby', 'newest', PARAM_ALPHA);
     $fromformdata['search'] = $search;
     $coursesearchform = new course_search_form('', $fromformdata, 'get');
     $fromform = $coursesearchform->get_data();
     $coursesearchform->set_data($fromformdata);
     $fromform = (object) $fromformdata;
     //Retrieve courses by web service
     $options = array();
     //special shortcut if a course id is given in param, we search straight forward this id
     if ($courseid = optional_param('courseid', 0, PARAM_INTEGER)) {
         $options['onlyvisible'] = true;
         $options['ids'] = array($courseid);
         $options['downloadable'] = true;
         $options['enrollable'] = true;
         $courses = $this->get_courses($options);
         $coursetotal = 1;
         //Add the name of the course to the page title
         //(useful because some sites as Facebook is going to read it to build a shared link name)
         foreach ($courses as $course) {
             $PAGE->set_title($course->fullname . ' - ' . $SITE->fullname);
         }
     } else {
         if (!empty($fromform) and optional_param('submitbutton', 0, PARAM_ALPHANUMEXT)) {
             $downloadable = optional_param('downloadable', false, PARAM_INTEGER);
             if (!empty($fromform->coverage)) {
                 $options['coverage'] = $fromform->coverage;
             }
             if ($fromform->licence != 'all') {
                 $options['licenceshortname'] = $fromform->licence;
             }
             if ($fromform->subject != 'all') {
                 $options['subject'] = $fromform->subject;
             }
             if ($fromform->audience != 'all') {
                 $options['audience'] = $fromform->audience;
             }
             if ($fromform->educationallevel != 'all') {
                 $options['educationallevel'] = $fromform->educationallevel;
             }
             if ($fromform->language != 'all') {
                 $options['language'] = $fromform->language;
             }
             //sort method
             switch ($fromform->orderby) {
                 case 'newest':
                     $options['orderby'] = 'timemodified DESC';
                     break;
                 case 'eldest':
                     $options['orderby'] = 'timemodified ASC';
                     break;
                 case 'publisher':
                     $options['orderby'] = 'publishername ASC';
                     break;
                 case 'fullname':
                     $options['orderby'] = 'fullname ASC';
                     break;
                 case 'ratingaverage':
                     $options['orderby'] = 'ratingaverage DESC';
                     break;
                 default:
                     break;
             }
             //get courses
             $options['search'] = $search;
             $options['onlyvisible'] = true;
             $options['downloadable'] = $downloadable;
             $options['enrollable'] = !$downloadable;
             $page = optional_param('page', 0, PARAM_INT);
             $courses = $this->get_courses($options, $page * HUB_COURSE_PER_PAGE, HUB_COURSE_PER_PAGE);
             $coursetotal = $this->get_courses($options, 0, 0, true);
             //reset the options
             $options['orderby'] = $fromform->orderby;
             unset($options['onlyvisible']);
         }
     }
     if (!empty($courses)) {
         //load javascript
         $courseids = array();
         //all result courses
         $courseimagenumbers = array();
         //number of screenshots of all courses (must be exact same order than $courseids)
         foreach ($courses as $course) {
             $courseids[] = $course->id;
             $courseimagenumbers[] = $course->screenshots;
         }
         $PAGE->requires->yui_module('moodle-block_community-imagegallery', 'M.blocks_community.init_imagegallery', array(array('imageids' => $courseids, 'imagenumbers' => $courseimagenumbers, 'huburl' => $CFG->wwwroot)));
         //get courses content
         foreach ($courses as $course) {
             $contents = $this->get_course_contents($course->id);
             if (!empty($contents)) {
                 foreach ($contents as $content) {
                     $course->contents[] = $content;
                 }
             }
         }
         //load ratings and comments
         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;
         //the aggregation method
         $ratingoptions->scaleid = 0 - get_config('local_hub', 'courseratingscaleid');
         //rating API is expecting "minus scaleid"
         $ratingoptions->userid = $USER->id;
         $ratingoptions->returnurl = $CFG->wwwroot . "/local/hub/index.php";
         $ratingoptions->component = 'local_hub';
         $ratingoptions->ratingarea = 'featured';
         $rm = new rating_manager();
         $courses = $rm->get_ratings($ratingoptions);
         //this function return $ratingoptions->items with information about the ratings
         foreach ($courses as $course) {
             $course->rating->settings->permissions->viewany = 1;
         }
         require_once $CFG->dirroot . '/comment/lib.php';
         foreach ($courses as $course) {
             $commentoptions = new stdClass();
             $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);
             $course->comment->set_view_permission(true);
         }
     }
     //create rss feed link
     $enablerssfeeds = get_config('local_hub', 'enablerssfeeds');
     if (!empty($enablerssfeeds)) {
         require $CFG->libdir . '/rsslib.php';
         $audience = key_exists('audience', $options) ? $options['audience'] : 'all';
         $educationallevel = key_exists('educationallevel', $options) ? $options['educationallevel'] : 'all';
         if (key_exists('downloadable', $options)) {
             $downloadable = empty($options['downloadable']) ? 0 : 1;
         } else {
             $downloadable = 'all';
         }
         $subject = key_exists('subject', $options) ? $options['subject'] : 'all';
         $licence = key_exists('licence', $options) ? $options['licence'] : 'all';
         $language = key_exists('language', $options) ? $options['language'] : 'all';
         $audience = key_exists('audience', $options) ? $options['audience'] : 'all';
         $orderby = key_exists('orderby', $options) ? $options['orderby'] : 'newest';
         $search = empty($search) ? 0 : urlencode($search);
         //retrieve guest user if user not logged in
         $userid = empty($USER->id) ? $CFG->siteguest : $USER->id;
         $ctx = context_course::instance(SITEID);
         //add the link tage to the header
         $rsslink = rss_get_url($ctx->id, $userid, 'local_hub', $downloadable . '/' . $audience . '/' . $educationallevel . '/' . $subject . '/' . $licence . '/' . $language . '/' . $search . '/');
         $PAGE->add_alternate_version('RSS', $rsslink, 'application/rss+xml');
         //create the rss icon
         $rssicon = rss_get_link($ctx->id, $userid, 'local_hub', $downloadable . '/' . $audience . '/' . $educationallevel . '/' . $subject . '/' . $licence . '/' . $language . '/' . $search . '/' . $orderby . '/', get_string('rsstooltip', 'local_hub'));
     }
     /// OUTPUT
     echo $OUTPUT->header();
     //notification message sent to publisher
     if (optional_param('messagesent', 0, PARAM_INTEGER)) {
         echo $OUTPUT->notification(get_string('messagesentsuccess', 'local_hub'), 'notifysuccess');
     }
     //search form
     $coursesearchform->display();
     //Course listing
     $options['submitbutton'] = 1;
     //need to set up the submitbutton to 1 for the paging bar (simulate search)
     //and paramlink
     //set language value back to 'all'
     if (!key_exists('language', $options)) {
         $options['language'] = 'all';
     }
     if (isset($courses) and empty($courseid)) {
         if (empty($coursetotal)) {
             $coursetotalhtml = get_string('nocourse', 'local_hub');
         } else {
             $coursetotalhtml = get_string('coursetotal', 'local_hub', $coursetotal);
         }
         echo html_writer::tag('div', $coursetotalhtml, array('class' => 'hubcoursetotal'));
     }
     if (!empty($courses)) {
         //paging bar
         if ($coursetotal > HUB_COURSE_PER_PAGE) {
             $baseurl = new moodle_url('', $options);
             $pagingbarhtml = $OUTPUT->paging_bar($coursetotal, $page, HUB_COURSE_PER_PAGE, $baseurl);
             $pagingbarhtml = html_writer::tag('div', $pagingbarhtml, array('class' => 'pagingbar'));
             echo $pagingbarhtml;
         }
         echo highlight($search, $renderer->course_list($courses));
         if (!empty($pagingbarhtml)) {
             echo $pagingbarhtml;
         }
     }
     //rss icon
     if (!empty($enablerssfeeds)) {
         echo html_writer::tag('div', $rssicon, array('class' => 'hubrsslink'));
     }
     //permalink
     if (!empty($courses)) {
         $permalinkparams = array();
         //special case: course list is a unique course for a given ID
         if (!empty($courseid)) {
             $permalinkparams['courseid'] = $courseid;
         } else {
             $permalinkparams = $options;
         }
         $permalink = html_writer::tag('div', html_writer::tag('a', get_string('permalink', 'local_hub'), array('href' => new moodle_url('', $permalinkparams))), array('class' => 'hubcoursepermalink'));
         echo $permalink;
     }
     echo $OUTPUT->footer();
 }
$courses = null;
/// FORM DATA
$fromformdata['coverage'] = optional_param('coverage', 'all', PARAM_TEXT);
$fromformdata['licence'] = optional_param('licence', 'all', PARAM_ALPHANUMEXT);
$fromformdata['subject'] = optional_param('subject', 'all', PARAM_ALPHANUMEXT);
$fromformdata['siteid'] = optional_param('siteid', 'all', PARAM_ALPHANUMEXT);
$fromformdata['lastmodified'] = optional_param('lastmodified', HUB_LASTMODIFIED_WEEK, PARAM_ALPHANUMEXT);
$fromformdata['audience'] = optional_param('audience', 'all', PARAM_ALPHANUMEXT);
$fromformdata['language'] = optional_param('language', 'all', PARAM_ALPHANUMEXT);
$fromformdata['educationallevel'] = optional_param('educationallevel', 'all', PARAM_ALPHANUMEXT);
$fromformdata['visibility'] = optional_param('visibility', COURSEVISIBILITY_NOTVISIBLE, PARAM_ALPHANUMEXT);
$fromformdata['downloadable'] = optional_param('downloadable', 'all', PARAM_ALPHANUM);
$fromformdata['orderby'] = optional_param('orderby', 'newest', PARAM_ALPHA);
$fromformdata['adminform'] = 1;
$fromformdata['search'] = $search;
$coursesearchform = new course_search_form('', $fromformdata);
$fromform = $coursesearchform->get_data();
$coursesearchform->set_data($fromformdata);
$fromform = (object) $fromformdata;
/// COURSE DATA
if (!empty($courseid)) {
    $options['ids'] = array($courseid);
    $options['downloadable'] = true;
    $options['enrollable'] = true;
    $courses = $hub->get_courses($options);
    unset($options['ids']);
    unset($options['downloadable']);
    unset($options['enrollable']);
    $coursetotal = 1;
} else {
    if (!empty($fromform->coverage)) {