Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/blocks/section_links/block_section_links.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 
00025 class block_section_links extends block_base {
00026 
00027     function init() {
00028         $this->title = get_string('pluginname', 'block_section_links');
00029     }
00030 
00031     function instance_config($instance) {
00032         global $DB;
00033 
00034         parent::instance_config($instance);
00035         $course = $this->page->course;
00036         if (isset($course->format)) {
00037             if ($course->format == 'topics') {
00038                 $this->title = get_string('topics', 'block_section_links');
00039             } else if ($course->format == 'weeks') {
00040                 $this->title = get_string('weeks', 'block_section_links');
00041             } else {
00042                 $this->title = get_string('pluginname', 'block_section_links');
00043             }
00044         }
00045     }
00046 
00047     function applicable_formats() {
00048         return (array('course-view-weeks' => true, 'course-view-topics' => true));
00049     }
00050 
00051     function get_content() {
00052         global $CFG, $USER, $DB;
00053 
00054         $highlight = 0;
00055         if(isset($this->config)){
00056             $config = $this->config;
00057         } else{
00058             // TODO: Move these config settings to proper ones using component name
00059             $config = get_config('blocks/section_links');
00060         }
00061 
00062         if ($this->content !== NULL) {
00063             return $this->content;
00064         }
00065 
00066         $this->content = new stdClass;
00067         $this->content->footer = '';
00068         $this->content->text   = '';
00069 
00070         if (empty($this->instance)) {
00071             return $this->content;
00072         }
00073 
00074         $course = $this->page->course;
00075         $context = get_context_instance(CONTEXT_COURSE, $course->id);
00076 
00077         if ($course->format == 'weeks' or $course->format == 'weekscss') {
00078             $highlight = ceil((time()-$course->startdate)/604800);
00079             $linktext = get_string('jumptocurrentweek', 'block_section_links');
00080             $sectionname = 'week';
00081         }
00082         else if ($course->format == 'topics') {
00083             $highlight = $course->marker;
00084             $linktext = get_string('jumptocurrenttopic', 'block_section_links');
00085             $sectionname = 'topic';
00086         }
00087         $inc = 1;
00088 
00089         if(!empty($config->numsections1) and ($course->numsections > $config->numsections1)) {
00090             $inc = $config->incby1;
00091         } else {
00092             if ($course->numsections > 22) {
00093                 $inc = 2;
00094             }
00095         }
00096 
00097         if(!empty($config->numsections2) and ($course->numsections > $config->numsections2)) {
00098             $inc = $config->incby2;
00099         } else {
00100             if ($course->numsections > 40) {
00101                 $inc = 5;
00102             }
00103         }
00104 
00105         if (isloggedin()) {
00106             $display = $DB->get_field('course_display', 'display', array('course'=>$this->page->course->id, 'userid'=>$USER->id));
00107         }
00108         if (!empty($display)) {
00109             $link = $CFG->wwwroot.'/course/view.php?id='.$this->page->course->id.'&amp;'.$sectionname.'=';
00110         } else {
00111             $link = '#section-';
00112         }
00113 
00114         $sql = "SELECT section, visible
00115                   FROM {course_sections}
00116                  WHERE course = ? AND
00117                        section < ".($course->numsections+1)."
00118               ORDER BY section";
00119 
00120         if ($sections = $DB->get_records_sql($sql, array($course->id))) {
00121             $text = '<ol class="inline-list">';
00122             for ($i = $inc; $i <= $course->numsections; $i += $inc) {
00123                 if (!isset($sections[$i])) {
00124                     continue;
00125                 }
00126                 $isvisible = $sections[$i]->visible;
00127                 if (!$isvisible and !has_capability('moodle/course:update', $context)) {
00128                     continue;
00129                 }
00130                 $style = ($isvisible) ? '' : ' class="dimmed"';
00131                 if ($i == $highlight) {
00132                     $text .= "<li><a href=\"$link$i\"$style><strong>$i</strong></a></li>\n";
00133                 } else {
00134                     $text .= "<li><a href=\"$link$i\"$style>$i</a></li>\n";
00135                 }
00136             }
00137             $text .= '</ol>';
00138             if ($highlight and isset($sections[$highlight])) {
00139                 $isvisible = $sections[$highlight]->visible;
00140                 if ($isvisible or has_capability('moodle/course:update', $context)) {
00141                     $style = ($isvisible) ? '' : ' class="dimmed"';
00142                     $text .= "\n<a href=\"$link$highlight\"$style>$linktext</a>";
00143                 }
00144             }
00145         }
00146 
00147         $this->content->text = $text;
00148         return $this->content;
00149     }
00154     function instance_allow_config() {
00155         return true;
00156     }
00157     function before_delete() {
00158         global $DB;
00159         // TODO: Move these config settings to proper ones using component name
00160         $DB->delete_records('config_plugins', array('plugin' => 'blocks/section_links'));
00161     }
00162 
00163     function has_config() {
00164         return true;
00165     }
00166 }
00167 
00168 
 All Data Structures Namespaces Files Functions Variables Enumerations