Advertisement
manchumahara

Moodle Course content block fix for moodle2.4 full code

Dec 31st, 2012
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.09 KB | None | 0 0
  1. <?php
  2. //fix for moodle 2.4 version https://moodle.org/plugins/view.php?plugin=block_course_contents
  3. // This file is part of Moodle - http://moodle.org/
  4. //
  5. // Moodle is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // Moodle is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17.  
  18. /**
  19.  * @package    block_course_contents
  20.  * @copyright  2009 David Mudrak <david@moodle.com>
  21.  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22.  */
  23.  
  24. defined('MOODLE_INTERNAL') || die();
  25.  
  26. require_once($CFG->dirroot.'/course/lib.php');
  27.  
  28. /**
  29.  * Course contents block generates a table of course contents based on the
  30.  * section descriptions
  31.  */
  32. class block_course_contents extends block_base {
  33.  
  34.     /**
  35.      * Initializes the block, called by the constructor
  36.      */
  37.     public function init() {
  38.         $this->title = get_string('pluginname', 'block_course_contents');
  39.     }
  40.  
  41.     /**
  42.      * Which page types this block may appear on
  43.      * @return array
  44.      */
  45.     public function applicable_formats() {
  46.         return (array('course-view-weeks' => true, 'course-view-topics' => true));
  47.     }
  48.  
  49.     /**
  50.      * Populate this block's content object
  51.      * @return stdClass block content info
  52.      */
  53.     public function get_content() {
  54.         global $CFG, $DB;
  55.  
  56.         $current = optional_param('section', null, PARAM_INT);
  57.         //var_dump($current);
  58.  
  59.         $highlight = 0;
  60.  
  61.         if ($this->content !== NULL) {
  62.             return $this->content;
  63.         }
  64.  
  65.         $this->content = new stdClass();
  66.         $this->content->footer = '';
  67.         $this->content->text   = '';
  68.  
  69.         if (empty($this->instance)) {
  70.             return $this->content;
  71.         }
  72.  
  73.         $course = $this->page->course;
  74.         $context = context_course::instance($course->id);
  75.        
  76.         //$cbcourse = course_get_format($course->id);
  77.         $cbcourse = course_get_format($course)->get_course();
  78.         $cbnumsections = $cbcourse->numsections;
  79.        
  80. //        echo '<pre>';
  81. //        print_r($cbcourse->numsections);
  82. //        echo '</pre>';
  83.  
  84.         if ($course->format == 'weeks') {
  85.             $highlight = ceil((time()-$course->startdate)/604800);
  86.             $linktext = get_string('jumptocurrentweek', 'block_course_contents');
  87.  
  88.         } else if ($course->format == 'scorm' or $course->format == 'social') {
  89.             // this formats do not have sections at all, no need for this block there
  90.             return $this->content;
  91.  
  92.         } else {
  93.             // anything else defaults to 'topics'
  94.             $highlight = $course->marker;
  95.             $linktext = get_string('jumptocurrenttopic', 'block_course_contents');
  96.         }
  97.  
  98.         // depending on whether there should be just one section displayed or all sections
  99.         // displayed, prepare the base URL to jump to
  100.         if ($course->coursedisplay == COURSE_DISPLAY_SINGLEPAGE) {
  101.             $link = '#section-';
  102.         } else if ($course->coursedisplay == COURSE_DISPLAY_MULTIPAGE) {
  103.             $link = $CFG->wwwroot.'/course/view.php?id='.$course->id.'&section=';
  104.         } else {
  105.             debugging('Unsupported course display mode', DEBUG_DEVELOPER);
  106.             $link = '#section-';
  107.         }
  108.  
  109.         $sql = "SELECT section, name, summary, summaryformat, visible
  110.                  FROM {course_sections}
  111.                 WHERE course = ? AND
  112.                       section < ?
  113.              ORDER BY section";
  114.        
  115. //        echo '<pre>';            
  116. //        print_r($course->numsections+1);
  117. //        echo '</pre>';            
  118.        
  119.         //if ($sections = $DB->get_records_sql($sql, array($course->id, $course->numsections+1))) {
  120.         if ($sections = $DB->get_records_sql($sql, array($course->id, $cbnumsections+1))) {    
  121.             $text = html_writer::start_tag('ul', array('class' => 'section-list'));
  122.            
  123. //            echo '<pre>';            
  124. //            print_r($sections);
  125. //            echo '</pre>';  
  126.            
  127.             foreach ($sections as $section) {
  128.                 $i = $section->section;
  129.                 //var_dump($i);
  130.                
  131.                 if (!isset($sections[$i]) or ($i == 0)) {
  132.                     continue;
  133.                 }
  134.                
  135.                 $isvisible = $sections[$i]->visible;
  136.                 if (!$isvisible and !has_capability('moodle/course:update', $context)) {
  137.                     continue;
  138.                 }
  139.                 if (!empty($section->name)) {
  140.                     $title = format_string($section->name, true, array('context' => $context));
  141.                 } else {
  142.                     $summary = format_text($section->summary, $section->summaryformat,
  143.                         array('para' => false, 'context' => $context));
  144.                     $title = format_string($this->extract_title($summary), true, array('context' => $context));
  145.                     if (empty($title)) {
  146.                         $title = get_generic_section_name($course->format, $section);
  147.                     }
  148.                 }
  149.                 $odd = $i % 2;
  150.                 if ($i == $highlight) {
  151.                     $text .= html_writer::start_tag('li', array('class' => 'section-item current r'.$odd));
  152.                 } else {
  153.                     $text .= html_writer::start_tag('li', array('class' => 'section-item r'.$odd));
  154.                 }
  155.                 $title = html_writer::tag('span', $i.' ', array('class' => 'section-number')).
  156.                          html_writer::tag('span', $title, array('class' => 'section-title'));
  157.                 if (is_null($current) or $i <> $current) {
  158.                     $text .= html_writer::link($link.$i, $title, array('class' => $isvisible ? '' : 'dimmed'));
  159.                 } else {
  160.                     $text .= $title;
  161.                 }
  162.                 $text .= html_writer::end_tag('li');
  163.             }
  164.             $text .= html_writer::end_tag('ul');
  165.             if ($highlight and isset($sections[$highlight])) {
  166.                 $isvisible = $sections[$highlight]->visible;
  167.                 if ($isvisible or has_capability('moodle/course:update', $context)) {
  168.                     $this->content->footer = html_writer::link($link.$highlight, $linktext,
  169.                             array('class' => $isvisible ? '' : 'dimmed'));
  170.                 }
  171.             }
  172.         }
  173.        
  174.         //var_dump($text);
  175.        
  176.         $this->content->text = $text;
  177.         return $this->content;
  178.     }
  179.  
  180.  
  181.     /**
  182.      * Given a section summary, exctract a text suitable as a section title
  183.      *
  184.      * @param string $summary Section summary as returned from database (no slashes)
  185.      * @return string Section title
  186.      */
  187.     private function extract_title($summary) {
  188.         global $CFG;
  189.         require_once(dirname(__FILE__).'/lib/simple_html_dom.php');
  190.  
  191.         $node = new simple_html_dom();
  192.         $node->load($summary);
  193.         return $this->node_plain_text($node);
  194.     }
  195.  
  196.  
  197.     /**
  198.      * Recursively find the first suitable plaintext from the HTML DOM.
  199.      *
  200.      * Internal private function called only from {@link extract_title()}
  201.      *
  202.      * @param simple_html_dom $node Current root node
  203.      * @return string
  204.      */
  205.     private function node_plain_text($node) {
  206.         if ($node->nodetype == HDOM_TYPE_TEXT) {
  207.             $t = trim($node->plaintext);
  208.             if (!empty($t)) {
  209.                 return $t;
  210.             }
  211.         }
  212.         $t = '';
  213.         foreach ($node->nodes as $n) {
  214.             $t = $this->node_plain_text($n);
  215.             if (!empty($t)) {
  216.                 break;
  217.             }
  218.         }
  219.         return $t;
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement