Exemplo n.º 1
0
 /**
  * Outputs the content of the widget
  *
  * @param array $args
  * @param array $instance
  */
 public function widget($args, $instance)
 {
     // outputs the content of the widget
     //Get args and output the title
     wp_register_style('moocview-widget-css', plugins_url('css/moocview-widget.css', __FILE__));
     wp_enqueue_style('moocview-widget-css');
     extract($args);
     echo $before_widget;
     $title = apply_filters('widget_title', $instance['title']);
     if ($title) {
         echo $before_title . $title . $after_title;
     }
     if ($instance['contenttype'] == '0') {
         $catlist = get_moodle_categories($this->moodle_ws_url, $this->moodle_site_token);
         foreach ($catlist as $category) {
             $langCourses = $category->coursecount == 1 ? __('course', 'moodle-courses-view') : __('courses', 'moodle-courses-view');
             echo "<div class='category'><a href='{$this->moodle_site}{$this->moocview_category_view_path}?{$this->moocview_category_id_par}={$category->id}'>{$category->name}</a> <br/>({$category->coursecount} {$langCourses})</div>";
         }
     } elseif ($instance['contenttype'] == '2') {
         $catcourses = get_moodle_categories_and_courses($this->moodle_ws_url, $this->moodle_site_token);
         foreach ($catcourses['courses'] as $cc) {
             $category = $cc['category'];
             echo "<div class='catcourses'>";
             $langCourses = $category->coursecount == 1 ? __('course', 'moodle-courses-view') : __('courses', 'moodle-courses-view');
             echo "<div class='cattitle'><a href='{$this->moodle_site}{$this->moocview_category_view_path}?{$this->moocview_category_id_par}={$category->id}'>{$category->name}</a> <br/>({$category->coursecount} {$langCourses})</div>";
             if ($category->coursecount > 0) {
                 echo "<ul class='courselist'>";
             }
             $i = 0;
             foreach ($cc['courses'] as $c) {
                 $i++;
                 if ($i <= $instance['maxcatcourses'] || $instance['maxcatcourses'] + 1 == $category->coursecount) {
                     echo "<li><a href='{$this->moodle_site}{$this->moocview_course_view_path}?{$this->moocview_course_id_par}={$c->id}'>{$c->fullname}</li>";
                 } else {
                     echo "<li class='morelink'><a href='{$instance['morelinkurl']}?{$instance['categoryparam']}={$category->id}'>" . __('More ...', 'moodle-courses-view') . "</li>";
                     break;
                 }
             }
             if ($category->coursecount > 0) {
                 echo "</ul>";
             }
             echo "</div>";
         }
     } else {
         $courses = get_moodle_courses($this->moodle_ws_url, $this->moodle_site_token);
         $i = 0;
         echo "<div class='catcourses'>";
         echo "<ul class='maincourselist'>";
         foreach ($courses as $c) {
             $i++;
             if ($i <= $instance['maxcatcourses']) {
                 echo "<li><a href='{$this->moodle_site}{$this->moocview_course_view_path}?{$this->moocview_course_id_par}={$c->id}'>{$c->fullname}</a></li>";
             } else {
                 echo "<li class='morelink'><a href='{$instance['morelinkurl']}'>" . __('More ...', 'moodle-courses-view') . "</li>";
                 break;
             }
         }
         echo "</ul></div>";
     }
 }
Exemplo n.º 2
0
/**
 * Get categories with their courses from moodle site. 
 * @param string $moodle_site the moodle site from where to get course categories
 * @param string $moodle_token the token of the moodle web service user 
 * @param integer $catid the moodle category id 
 * @return array an array ('courses', 'cids') containing in 'courses' for each category the corresponding courses and in 'cids' the list af all courses ids 
*/
function get_moodle_categories_and_courses($moodle_site, $moodle_token, $catid = null)
{
    $cats = get_moodle_categories($moodle_site, $moodle_token);
    $courses = get_moodle_courses($moodle_site, $moodle_token);
    $courseids = array();
    foreach ($cats as $ca) {
        if (is_null($catid) || $ca->id == $catid) {
            $catsarr[$ca->id]['category'] = $ca;
            $catsarr[$ca->id]['courses'] = array();
        }
    }
    foreach ($courses as $co) {
        if (key_exists('category', $catsarr[$co->categoryid])) {
            $catsarr[$co->categoryid]['courses'][] = $co;
            $courseids[] = $co->id;
        }
    }
    return array('courses' => $catsarr, 'cids' => $courseids);
}