/**
  * Creates the blocks main content
  *
  * @return string
  */
 public function get_content()
 {
     global $USER, $COURSE, $CFG, $OUTPUT, $DB;
     // If content has already been generated, don't waste time generating it again.
     if ($this->content !== null) {
         return $this->content;
     }
     $this->content = new stdClass();
     $this->content->text = '';
     $this->content->footer = '';
     $blockinstancesonpage = array();
     // Guests do not have any progress. Don't show them the block.
     if (!isloggedin() or isguestuser()) {
         return $this->content;
     }
     // Draw the multi-bar content for the Dashboard and Front page.
     if (block_progress_on_site_page()) {
         $courses = enrol_get_my_courses();
         $coursenametoshow = get_config('block_progress', 'coursenametoshow') ?: 'shortname';
         $sql = "SELECT bi.id,\n                           bp.id AS blockpositionid,\n                           COALESCE(bp.region, bi.defaultregion) AS region,\n                           COALESCE(bp.weight, bi.defaultweight) AS weight,\n                           COALESCE(bp.visible, 1) AS visible,\n                           bi.configdata\n                      FROM {block_instances} bi\n                 LEFT JOIN {block_positions} bp ON bp.blockinstanceid = bi.id\n                                               AND " . $DB->sql_like('bp.pagetype', ':pagetype', false) . "\n                     WHERE bi.blockname = 'progress'\n                       AND bi.parentcontextid = :contextid\n                  ORDER BY region, weight, bi.id";
         foreach ($courses as $courseid => $course) {
             // Get specific block config and context.
             $modules = block_progress_modules_in_use($course->id);
             if ($course->visible && !empty($modules)) {
                 $context = block_progress_get_course_context($course->id);
                 $params = array('contextid' => $context->id, 'pagetype' => 'course-view-%');
                 $blockinstances = $DB->get_records_sql($sql, $params);
                 $blockinstancesonpage = array_merge($blockinstancesonpage, array_keys($blockinstances));
                 foreach ($blockinstances as $blockid => $blockinstance) {
                     $blockinstance->config = unserialize(base64_decode($blockinstance->configdata));
                     if (!empty($blockinstance->config)) {
                         $blockinstance->events = block_progress_event_information($blockinstance->config, $modules, $course->id);
                         $blockinstance->events = block_progress_filter_visibility($blockinstance->events, $USER->id, $context, $course);
                     }
                     if ($blockinstance->visible == 0 || empty($blockinstance->config) || $blockinstance->events == 0 || !empty($blockinstance->config->group) && !has_capability('moodle/site:accessallgroups', $context) && !groups_is_member($blockinstance->config->group, $USER->id)) {
                         unset($blockinstances[$blockid]);
                     }
                 }
                 // Output the Progress Bar.
                 if (!empty($blockinstances)) {
                     $courselink = new moodle_url('/course/view.php', array('id' => $course->id));
                     $linktext = HTML_WRITER::tag('h3', s($course->{$coursenametoshow}));
                     $this->content->text .= HTML_WRITER::link($courselink, $linktext);
                 }
                 foreach ($blockinstances as $blockid => $blockinstance) {
                     if ($blockinstance->config->progressTitle != '') {
                         $this->content->text .= HTML_WRITER::tag('p', s(format_string($blockinstance->config->progressTitle)));
                     }
                     $attempts = block_progress_attempts($modules, $blockinstance->config, $blockinstance->events, $USER->id, $course->id);
                     $this->content->text .= block_progress_bar($modules, $blockinstance->config, $blockinstance->events, $USER->id, $blockinstance->id, $attempts, $course->id);
                 }
             }
         }
         // Show a message explaining lack of bars, but only while editing is on.
         if ($this->page->user_is_editing() && $this->content->text == '') {
             $this->content->text = get_string('no_blocks', 'block_progress');
         }
     } else {
         // Check if user is in group for block.
         if (!empty($this->config->group) && !has_capability('moodle/site:accessallgroups', $this->context) && !groups_is_member($this->config->group, $USER->id)) {
             return $this->content;
         }
         // Check if any activities/resources have been created.
         $modules = block_progress_modules_in_use($COURSE->id);
         if (empty($modules)) {
             if (has_capability('moodle/block:edit', $this->context)) {
                 $this->content->text .= get_string('no_events_config_message', 'block_progress');
             }
             return $this->content;
         }
         // Check if activities/resources have been selected in config.
         $events = block_progress_event_information($this->config, $modules, $COURSE->id);
         $context = block_progress_get_course_context($COURSE->id);
         $events = block_progress_filter_visibility($events, $USER->id, $context);
         if ($events === null || $events === 0) {
             if (has_capability('moodle/block:edit', $this->context)) {
                 $this->content->text .= get_string('no_events_message', 'block_progress');
                 if ($USER->editing) {
                     $parameters = array('id' => $COURSE->id, 'sesskey' => sesskey(), 'bui_editid' => $this->instance->id);
                     $url = new moodle_url('/course/view.php', $parameters);
                     $label = get_string('selectitemstobeadded', 'block_progress');
                     $this->content->text .= $OUTPUT->single_button($url, $label);
                     if ($events === 0) {
                         $url->param('turnallon', '1');
                         $label = get_string('addallcurrentitems', 'block_progress');
                         $this->content->text .= $OUTPUT->single_button($url, $label);
                     }
                 }
             }
             return $this->content;
         } else {
             if (empty($events)) {
                 if (has_capability('moodle/block:edit', $this->context)) {
                     $this->content->text .= get_string('no_visible_events_message', 'block_progress');
                 }
                 return $this->content;
             }
         }
         // Display progress bar.
         $attempts = block_progress_attempts($modules, $this->config, $events, $USER->id, $COURSE->id);
         $this->content->text = block_progress_bar($modules, $this->config, $events, $USER->id, $this->instance->id, $attempts, $COURSE->id);
         $blockinstancesonpage = array($this->instance->id);
         // Allow teachers to access the overview page.
         if (has_capability('block/progress:overview', $this->context)) {
             $parameters = array('progressbarid' => $this->instance->id, 'courseid' => $COURSE->id);
             $url = new moodle_url('/blocks/progress/overview.php', $parameters);
             $label = get_string('overview', 'block_progress');
             $options = array('class' => 'overviewButton');
             $this->content->text .= $OUTPUT->single_button($url, $label, 'post', $options);
         }
     }
     // Organise access to JS.
     $jsmodule = array('name' => 'block_progress', 'fullpath' => '/blocks/progress/module.js', 'requires' => array(), 'strings' => array());
     $arguments = array($blockinstancesonpage, array($USER->id));
     $this->page->requires->js_init_call('M.block_progress.init', $arguments, false, $jsmodule);
     return $this->content;
 }
Example #2
0
define('USER_SMALL_CLASS', 20);
// Below this is considered small.
define('USER_LARGE_CLASS', 200);
// Above this is considered large.
define('DEFAULT_PAGE_SIZE', 20);
define('SHOW_ALL_PAGE_SIZE', 5000);
// Gather form data.
$id = required_param('progressbarid', PARAM_INT);
$courseid = required_param('courseid', PARAM_INT);
$page = optional_param('page', 0, PARAM_INT);
// Which page to show.
$perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);
// How many per page.
// Determine course and context.
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$context = block_progress_get_course_context($courseid);
// Get specific block config and context.
$progressblock = $DB->get_record('block_instances', array('id' => $id), '*', MUST_EXIST);
$progressconfig = unserialize(base64_decode($progressblock->configdata));
$progressblockcontext = block_progress_get_block_context($id);
// Set up page parameters.
$PAGE->set_course($course);
$PAGE->requires->css('/blocks/progress/styles.css');
$PAGE->set_url('/blocks/progress/overview.php', array('progressbarid' => $id, 'courseid' => $courseid, 'page' => $page, 'perpage' => $perpage));
$PAGE->set_context($context);
$title = get_string('overview', 'block_progress');
$PAGE->set_title($title);
$PAGE->set_heading($title);
$PAGE->navbar->add($title);
$PAGE->set_pagelayout('standard');
// Check user is logged in and capable of grading.