/**
 * Gets event information about modules monitored by an instance of a Progress Bar block
 *
 * @param stdClass $config  The block instance configuration values
 * @param array    $modules The modules used in the course
 * @param stdClass $course  The current course
 * @param int      $userid  The user's ID
 * @return mixed   returns array of visible events monitored,
 *                 empty array if none of the events are visible,
 *                 null if all events are configured to "no" monitoring and
 *                 0 if events are available but no config is set
 */
function block_progress_event_information($config, $modules, $course, $userid = 0)
{
    global $DB, $USER;
    $events = array();
    $numevents = 0;
    $numeventsconfigured = 0;
    if ($userid === 0) {
        $userid = $USER->id;
    }
    // Get section information for the course module layout.
    $sections = block_progress_course_sections($course);
    // Check each known module (described in lib.php).
    foreach ($modules as $module => $details) {
        $fields = 'id, name';
        if (array_key_exists('defaultTime', $details)) {
            $fields .= ', ' . $details['defaultTime'] . ' as due';
        }
        // Check if this type of module is used in the course, gather instance info.
        $records = $DB->get_records($module, array('course' => $course), '', $fields);
        foreach ($records as $record) {
            // Is the module being monitored?
            if (isset($config->{'monitor_' . $module . $record->id})) {
                $numeventsconfigured++;
            }
            if (progress_default_value($config->{'monitor_' . $module . $record->id}, 0) == 1) {
                $numevents++;
                // Check the time the module is due.
                if (isset($details['defaultTime']) && $record->due != 0 && progress_default_value($config->{'locked_' . $module . $record->id}, 0)) {
                    $expected = progress_default_value($record->due);
                } else {
                    $expected = $config->{'date_time_' . $module . $record->id};
                }
                // Gather together module information.
                $coursemodule = block_progress_get_coursemodule($module, $record->id, $course);
                $events[] = array('expected' => $expected, 'type' => $module, 'id' => $record->id, 'name' => format_string($record->name), 'cm' => $coursemodule, 'section' => $sections[$coursemodule->section]->section, 'position' => array_search($coursemodule->id, $sections[$coursemodule->section]->sequence));
            }
        }
    }
    if ($numeventsconfigured == 0) {
        return 0;
    }
    if ($numevents == 0) {
        return null;
    }
    // Sort by first value in each element, which is time due.
    if (isset($config->orderby) && $config->orderby == 'orderbycourse') {
        usort($events, 'block_progress_compare_events');
    } else {
        usort($events, 'block_progress_compare_times');
    }
    return $events;
}
 function get_content()
 {
     // Access to settings needed
     global $USER, $COURSE, $CFG;
     $eventArray = array();
     include $CFG->dirroot . '/blocks/progress/common.php';
     include_once $CFG->dirroot . '/blocks/progress/lib.php';
     include_once $CFG->libdir . '/ddllib.php';
     // 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 = '';
     // Collect up all the events to track progress
     $numEvents = 0;
     $visibleEvents = 0;
     foreach ($modules as $module => $details) {
         $table = new XMLDBTable($module);
         if (table_exists($table)) {
             $events = get_records($module, 'course', $this->instance->pageid, '', 'id, name' . (array_key_exists('defaultTime', $details) ? ', ' . $details['defaultTime'] . ' as due' : ''));
             if ($events) {
                 foreach ($events as $event) {
                     $monitored = progress_default_value($this->config->{'monitor_' . $module . $event->id});
                     if (isset($monitored) && $monitored == 'on') {
                         $numEvents++;
                         $courseModule = get_coursemodule_from_instance($module, $event->id, $COURSE->id);
                         // Check if the user has attempted the module
                         $query = $details['actions'][isset($this->config->{'action_' . $module . $event->id}) ? $this->config->{'action_' . $module . $event->id} : $details['actions']['defaultAction']];
                         $query = str_replace(array('#COURSEID#', '#USERID#', '#EVENTID#', '#CMID#'), array($this->instance->pageid, $USER->id, $event->id, $courseModule->id), $query);
                         $attempted = record_exists_sql($query) ? true : false;
                         // Check the time the module is due
                         $locked = progress_default_value($this->config->{'locked_' . $module . $event->id});
                         if (isset($details['defaultTime']) && $event->due != 0 && (!isset($locked) || $locked == 'on')) {
                             $expected = progress_default_value($event->due);
                         } else {
                             $day = $this->config->{'day_' . $module . $event->id};
                             $month = $this->config->{'month_' . $module . $event->id};
                             $year = $this->config->{'year_' . $module . $event->id};
                             $hour = $this->config->{'hour_' . $module . $event->id};
                             $minute = $this->config->{'minute_' . $module . $event->id};
                             $expected = mktime($hour, $minute, 0, $month, $day, $year);
                         }
                         // Check if the module is visible, and if so, keep a record for it
                         if ($courseModule->visible == 1) {
                             $visibleEvents++;
                             $eventArray[] = array('expected' => $expected, 'type' => $module, 'id' => $event->id, 'name' => $event->name, 'attempted' => $attempted, 'moduleID' => $courseModule->id, 'visible' => $courseModule->visible);
                         }
                     }
                 }
             }
         }
     }
     // Check if any events were found
     if ($numEvents == 0) {
         $this->content->text = get_string('no_events_message', 'block_progress');
     } else {
         if ($visibleEvents == 0) {
             $this->content->text = get_string('no_visible_events_message', 'block_progress');
         } else {
             // Set up variables
             sort($eventArray);
             // by first value in each element, which is time due
             $now = time();
             $nowPos = 0;
             // Find where to put now arrow
             while ($nowPos < $visibleEvents && $now > $eventArray[$nowPos]['expected']) {
                 $nowPos++;
             }
             // Output function to display activity/resource info
             $this->content->text = '
         <script>
             function progress_showInfo (mod, type, id, name, message, dateTime, instanceID, icon) {
                 document.getElementById("progressBarInfo"+instanceID).innerHTML="<a href=\\\'' . $CFG->wwwroot . '/mod/"+mod+"/view.php?id="+id+"\\\'><img src=\\\'' . $CFG->wwwroot . '/mod/"+mod+"/icon.gif\\\' /> "+name+"</a><br />"+type+" "+message+"&nbsp;<img align=\\\'absmiddle\\\' src=\\\'' . $CFG->wwwroot . '/blocks/progress/img/"+icon+".gif\\\' /><br />' . get_string('time_expected', 'block_progress') . ': "+dateTime+"<br />";
             }
         </script>';
             // Start table
             $this->content->text .= '<table class="progressBarProgressTable" cellpadding="0" cellspacing="0">';
             // Place now arrow
             if ($this->config->displayNow == '1') {
                 $this->content->text .= '<tr>';
                 if ($nowPos < $visibleEvents / 2) {
                     for ($i = 0; $i < $nowPos; $i++) {
                         $this->content->text .= '<td>&nbsp;</td>';
                     }
                     $this->content->text .= '<td colspan="' . ($visibleEvents - $nowPos) . '" style="text-align:left;" id="progressBarHeader"><img src="' . $CFG->wwwroot . '/blocks/progress/img/left.gif" />' . get_string('now_indicator', 'block_progress') . '</td>';
                 } else {
                     $this->content->text .= '<td colspan=' . $nowPos . ' style="text-align:right;" id="progressBarHeader">' . get_string('now_indicator', 'block_progress') . '<img src="' . $CFG->wwwroot . '/blocks/progress/img/right.gif" /></td>';
                     for ($i = $nowPos; $i < $visibleEvents; $i++) {
                         $this->content->text .= '<td>&nbsp;</td>';
                     }
                 }
                 $this->content->text .= '</tr>';
             }
             // Start progress bar
             $width = 100 / $visibleEvents;
             $this->content->text .= '<tr>';
             foreach ($eventArray as $event) {
                 $this->content->text .= '<td class="progressBarCell" width="' . $width . '%" onclick="document.location=\'' . $CFG->wwwroot . '/mod/' . $event['type'] . '/view.php?' . 'id=' . $event['moduleID'] . '\';"';
                 $this->content->text .= ' onmouseover="progress_showInfo(\'' . $event['type'] . '\',\'' . get_string($event['type'], 'block_progress') . '\',\'' . $event['moduleID'] . '\',\'' . addSlashes($event['name']) . '\',\'' . get_string($this->config->{'action_' . $event['type'] . $event['id']}, 'block_progress') . '\',\'' . userdate($event['expected'], get_string('date_format', 'block_progress'), $CFG->timezone) . '\',\'' . $this->instance->id . '\',\'' . ($event['attempted'] ? 'tick' : 'cross') . '\');"';
                 $this->content->text .= ' bgColor="';
                 if ($event['attempted']) {
                     $this->content->text .= (isset($CFG->blockProgressBarAttemptedColour) ? $CFG->blockProgressBarAttemptedColour : $defaultColours['attempted']) . '" /><img src="' . $CFG->wwwroot . '/blocks/progress/img/' . (isset($this->config->progressBarIcons) && $this->config->progressBarIcons == '1' ? 'tick.gif' : 'blank.gif') . '" />';
                 } else {
                     if ($event['expected'] < $now) {
                         $this->content->text .= (isset($CFG->blockProgressBarNotAttemptedColour) ? $CFG->blockProgressBarNotAttemptedColour : $defaultColours['notAttempted']) . '" /><img src="' . $CFG->wwwroot . '/blocks/progress/img/' . (isset($this->config->progressBarIcons) && $this->config->progressBarIcons == '1' ? 'cross.gif' : 'blank.gif') . '" />';
                     } else {
                         $this->content->text .= (isset($CFG->blockProgressBarFutureNotAttemptedColour) ? $CFG->blockProgressBarFutureNotAttemptedColour : $defaultColours['futureNotAttempted']) . '" /><img src="' . $CFG->wwwroot . '/blocks/progress/img/blank.gif" />';
                     }
                 }
                 $this->content->text .= '</a></td>';
             }
             $this->content->text .= '
             </tr>
         </table>
         <div class="progressEventInfo" id="progressBarInfo' . $this->instance->id . '">' . get_string('mouse_over_prompt', 'block_progress') . '</div>
         ';
         }
     }
     return $this->content;
 }
Example #3
0
 protected function specific_definition($mform)
 {
     global $CFG, $COURSE, $DB, $OUTPUT;
     include_once $CFG->dirroot . '/blocks/progress/lib.php';
     $turnallon = optional_param('turnallon', 0, PARAM_INT);
     $dbmanager = $DB->get_manager();
     // loads ddl manager and xmldb classes
     $count = 0;
     $usingweeklyformat = $COURSE->format == 'weeks' || $COURSE->format == 'weekscss' || $COURSE->format == 'weekcoll';
     // Start block specific section in config form
     $mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
     // Progress block instance title
     $mform->addElement('text', 'config_progressTitle', get_string('config_title', 'block_progress'));
     $mform->setDefault('config_progressTitle', '');
     $mform->setType('config_progressTitle', PARAM_MULTILANG);
     $mform->addHelpButton('config_progressTitle', 'why_set_the_title', 'block_progress');
     // Allow icons to be turned on/off on the block
     $mform->addElement('selectyesno', 'config_progressBarIcons', get_string('config_icons', 'block_progress') . '&nbsp;' . $OUTPUT->pix_icon('tick', '', 'block_progress') . '&nbsp;' . $OUTPUT->pix_icon('cross', '', 'block_progress'));
     $mform->setDefault('config_progressBarIcons', 0);
     $mform->addHelpButton('config_progressBarIcons', 'why_use_icons', 'block_progress');
     // Allow NOW to be turned on or off
     $mform->addElement('selectyesno', 'config_displayNow', get_string('config_now', 'block_progress') . '&nbsp;' . $OUTPUT->pix_icon('left', '', 'block_progress') . get_string('now_indicator', 'block_progress'));
     $mform->setDefault('config_displayNow', 1);
     $mform->addHelpButton('config_displayNow', 'why_display_now', 'block_progress');
     // Allow progress percentage to be turned on for students
     $mform->addElement('selectyesno', 'config_showpercentage', get_string('config_percentage', 'block_progress'));
     $mform->setDefault('config_showpercentage', 0);
     $mform->addHelpButton('config_showpercentage', 'why_show_precentage', 'block_progress');
     $mform->addElement('header', '', get_string('config_monitored', 'block_progress'));
     // Go through each type of activity/resource that can be monitored
     $modules = get_monitorable_modules();
     foreach ($modules as $module => $details) {
         // Get data about activities/resources of this type
         unset($instances);
         if ($dbmanager->table_exists($module)) {
             $sql = 'SELECT id, name';
             if ($module == 'assignment') {
                 $sql .= ', assignmenttype';
             }
             if (array_key_exists('defaultTime', $details)) {
                 $sql .= ', ' . $details['defaultTime'] . ' as due';
             }
             $sql .= ' FROM {' . $module . '} WHERE course=\'' . $COURSE->id . '\' ORDER BY name';
             $instances = $DB->get_records_sql($sql);
         }
         // If there are activities/resources of this type, show them
         if (!empty($instances)) {
             // Display each monitorable activity/resource as a row
             foreach ($instances as $i => $instance) {
                 $count++;
                 // Start of module border
                 $mform->addElement('html', '<div class="progressConfigBox">');
                 // Find type labels for assignment types
                 $asslabel = '';
                 if (isset($instance->assignmenttype)) {
                     $type = $instance->assignmenttype;
                     if (get_string_manager()->string_exists('type' . $type, 'mod_assignment')) {
                         $asslabel = get_string('type' . $type, 'assignment');
                     } else {
                         $asslabel = get_string('type' . $type, 'assignment_' . $type);
                     }
                     $asslabel = ' (' . $asslabel . ')';
                 }
                 // Icon, module type and name
                 $mform->addElement('html', $OUTPUT->pix_icon('icon', get_string('pluginname', $module), 'mod_' . $module) . '&nbsp;<strong>' . get_string($module, 'block_progress') . $asslabel . ': <em>' . format_string($instance->name) . '</em></strong>');
                 // Allow monitoring turned on or off
                 $mform->addElement('selectyesno', 'config_monitor_' . $module . $instance->id, get_string('config_header_monitored', 'block_progress'));
                 $mform->setDefault('config_monitor_' . $module . $instance->id, $turnallon);
                 $mform->addHelpButton('config_monitor_' . $module . $instance->id, 'what_does_monitored_mean', 'block_progress');
                 // Allow locking turned on or off
                 if (isset($details['defaultTime']) && $instance->due != 0) {
                     $mform->addElement('selectyesno', 'config_locked_' . $module . $instance->id, get_string('config_header_locked', 'block_progress'));
                     $mform->setDefault('config_locked_' . $module . $instance->id, 1);
                     $mform->disabledif('config_locked_' . $module . $instance->id, 'config_monitor_' . $module . $instance->id, 'eq', 0);
                     $mform->addHelpButton('config_locked_' . $module . $instance->id, 'what_locked_means', 'block_progress');
                 }
                 // Print the date selector
                 $mform->addElement('date_time_selector', 'config_date_time_' . $module . $instance->id, get_string('config_header_expected', 'block_progress'));
                 $mform->disabledif('config_date_time_' . $module . $instance->id, 'config_locked_' . $module . $instance->id, 'eq', 1);
                 $mform->disabledif('config_date_time_' . $module . $instance->id, 'config_monitor_' . $module . $instance->id, 'eq', 0);
                 // Assume a time/date for a activity/resource
                 $expected = null;
                 $datetimepropery = 'date_time_' . $module . $instance->id;
                 if (isset($this->block->config) && property_exists($this->block->config, $datetimepropery)) {
                     $expected = $this->block->config->{$datetimepropery};
                 }
                 if (empty($expected)) {
                     // If there is a date associated with the activity/resource, use that
                     if (isset($details['defaultTime']) && $instance->due != 0) {
                         $expected = progress_default_value($instance->due);
                     } else {
                         if ($usingweeklyformat) {
                             $cm = get_coursemodule_from_instance($module, $instance->id, $COURSE->id);
                             $params = array('id' => $cm->section);
                             $section = $DB->get_field('course_sections', 'section', $params);
                             $expected = $COURSE->startdate + ($section > 0 ? $section : 1) * 604800 - 300;
                         } else {
                             $currenttime = time();
                             $timearray = localtime($currenttime, true);
                             $endofweektimearray = localtime($currenttime + (7 - $timearray['tm_wday']) * 86400, true);
                             $expected = mktime(23, 55, 0, $endofweektimearray['tm_mon'] + 1, $endofweektimearray['tm_mday'], $endofweektimearray['tm_year'] + 1900);
                         }
                     }
                 }
                 $mform->setDefault('config_date_time_' . $module . $instance->id, $expected);
                 $mform->addHelpButton('config_date_time_' . $module . $instance->id, 'what_expected_by_means', 'block_progress');
                 // Print the action selector for the event
                 $actions = array();
                 foreach ($details['actions'] as $action => $sql) {
                     // Before allowing pass marks, see that Grade to pass value is set
                     if ($action == 'passed') {
                         $params = array('itemmodule' => $module, 'iteminstance' => $instance->id);
                         $gradetopass = $DB->get_record('grade_items', $params, 'gradepass');
                         if ($gradetopass && $gradetopass->gradepass > 0) {
                             $actions[$action] = get_string($action, 'block_progress');
                         }
                     } else {
                         $actions[$action] = get_string($action, 'block_progress');
                     }
                 }
                 if (isset($CFG->enablecompletion) && $CFG->enablecompletion == 1) {
                     $cm = get_coursemodule_from_instance($module, $instance->id, $COURSE->id);
                     if ($cm->completion != 0) {
                         $actions['activity_completion'] = get_string('activity_completion', 'block_progress');
                     }
                 }
                 $mform->addElement('select', 'config_action_' . $module . $instance->id, get_string('config_header_action', 'block_progress'), $actions);
                 $mform->setDefault('config_action_' . $module . $instance->id, $details['defaultAction']);
                 $mform->disabledif('config_action_' . $module . $instance->id, 'config_monitor_' . $module . $instance->id, 'eq', 0);
                 $mform->addHelpButton('config_action_' . $module . $instance->id, 'what_actions_can_be_monitored', 'block_progress');
                 $mform->addElement('html', '</div>');
             }
         }
     }
     // When there are no activities that can be monitored, prompt teacher to create some
     if ($count == 0) {
         $mform->addElement('html', get_string('no_events_config_message', 'block_progress'));
     }
 }
 protected function specific_definition($mform)
 {
     global $CFG, $COURSE, $DB, $OUTPUT, $SCRIPT;
     // The My home version is not configurable.
     if (block_progress_on_my_page()) {
         return;
     }
     $turnallon = optional_param('turnallon', 0, PARAM_INT);
     $dbmanager = $DB->get_manager();
     // Loads ddl manager and xmldb classes.
     $count = 0;
     $usingweeklyformat = $COURSE->format == 'weeks' || $COURSE->format == 'weekscss' || $COURSE->format == 'weekcoll';
     // Start block specific section in config form.
     $mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
     // Set Progress block instance title.
     $mform->addElement('text', 'config_progressTitle', get_string('config_title', 'block_progress'));
     $mform->setDefault('config_progressTitle', '');
     $mform->setType('config_progressTitle', PARAM_MULTILANG);
     $mform->addHelpButton('config_progressTitle', 'why_set_the_title', 'block_progress');
     // Allow icons to be turned on/off on the block.
     $mform->addElement('selectyesno', 'config_progressBarIcons', get_string('config_icons', 'block_progress') . '&nbsp;' . $OUTPUT->pix_icon('tick', '', 'block_progress') . '&nbsp;' . $OUTPUT->pix_icon('cross', '', 'block_progress'));
     $mform->setDefault('config_progressBarIcons', 0);
     $mform->addHelpButton('config_progressBarIcons', 'why_use_icons', 'block_progress');
     // Control order of items in Progress Bar.
     $orderingoptions = array('orderbytime' => get_string('config_orderby_due_time', 'block_progress'), 'orderbycourse' => get_string('config_orderby_course_order', 'block_progress'));
     $orderbylabel = get_string('config_orderby', 'block_progress');
     $mform->addElement('select', 'config_orderby', $orderbylabel, $orderingoptions);
     $mform->setDefault('config_orderby', 'orderbytime');
     $mform->addHelpButton('config_orderby', 'how_ordering_works', 'block_progress');
     // Allow NOW to be turned on or off.
     $mform->addElement('selectyesno', 'config_displayNow', get_string('config_now', 'block_progress') . '&nbsp;' . $OUTPUT->pix_icon('left', '', 'block_progress') . get_string('now_indicator', 'block_progress'));
     $mform->setDefault('config_displayNow', 1);
     $mform->addHelpButton('config_displayNow', 'why_display_now', 'block_progress');
     $mform->disabledif('config_displayNow', 'config_orderby', 'eq', 'orderbycourse');
     // Allow progress percentage to be turned on for students.
     $mform->addElement('selectyesno', 'config_showpercentage', get_string('config_percentage', 'block_progress'));
     $mform->setDefault('config_showpercentage', 0);
     $mform->addHelpButton('config_showpercentage', 'why_show_precentage', 'block_progress');
     // Allow the block to be visible to a single group.
     $groups = groups_get_all_groups($COURSE->id);
     if (!empty($groups)) {
         $groupsmenu = array();
         $groupsmenu[0] = get_string('allparticipants');
         foreach ($groups as $group) {
             $groupsmenu[$group->id] = format_string($group->name);
         }
         $grouplabel = get_string('config_group', 'block_progress');
         $mform->addElement('select', 'config_group', $grouplabel, $groupsmenu);
         $mform->setDefault('config_group', '0');
         $mform->addHelpButton('config_group', 'how_group_works', 'block_progress');
     }
     // Get course section information.
     $sections = block_progress_course_sections($COURSE->id);
     // Determine the time at the end of the week, less 5min.
     if (!$usingweeklyformat) {
         $currenttime = time();
         $timearray = localtime($currenttime, true);
         $endofweektimearray = localtime($currenttime + (7 - $timearray['tm_wday']) * 86400, true);
         $endofweektime = mktime(23, 55, 0, $endofweektimearray['tm_mon'] + 1, $endofweektimearray['tm_mday'], $endofweektimearray['tm_year'] + 1900);
     }
     // Go through each type of activity/resource that can be monitored to find instances in the course.
     $modules = block_progress_monitorable_modules();
     $unsortedmodules = array();
     foreach ($modules as $module => $details) {
         // Get data about instances of activities/resources of this type in this course.
         unset($instances);
         if ($dbmanager->table_exists($module)) {
             $sql = 'SELECT id, name';
             if ($module == 'assignment') {
                 $sql .= ', assignmenttype';
             }
             if (array_key_exists('defaultTime', $details)) {
                 $sql .= ', ' . $details['defaultTime'] . ' as due';
             }
             $sql .= ' FROM {' . $module . '} WHERE course=\'' . $COURSE->id . '\' ORDER BY name';
             $instances = $DB->get_records_sql($sql);
         }
         // If there are instances of activities/resources of this type, get more info about them.
         if (!empty($instances)) {
             foreach ($instances as $i => $instance) {
                 $count++;
                 $moduleinfo = new stdClass();
                 $moduleinfo->module = $module;
                 $moduleinfo->instanceid = $instance->id;
                 $moduleinfo->uniqueid = $module . $instance->id;
                 $moduleinfo->label = get_string($module, 'block_progress');
                 $moduleinfo->instancename = $instance->name;
                 $moduleinfo->lockpossible = isset($details['defaultTime']);
                 $moduleinfo->instancedue = $moduleinfo->lockpossible && $instance->due;
                 // Get position of activity/resource on course page.
                 $coursemodule = get_coursemodule_from_instance($module, $instance->id, $COURSE->id);
                 $moduleinfo->section = $coursemodule->section;
                 $moduleinfo->position = array_search($coursemodule->id, $sections[$coursemodule->section]->sequence);
                 $moduleinfo->coursemoduleid = $coursemodule->id;
                 $moduleinfo->completion = $coursemodule->completion;
                 $moduleinfo->completionexpected = $coursemodule->completionexpected;
                 // Find type labels for assignment types.
                 $asslabel = '';
                 if (isset($instance->assignmenttype)) {
                     $type = $instance->assignmenttype;
                     if (get_string_manager()->string_exists('type' . $type, 'mod_assignment')) {
                         $asslabel = get_string('type' . $type, 'assignment');
                     } else {
                         $asslabel = get_string('type' . $type, 'assignment_' . $type);
                     }
                     $moduleinfo->label .= ' (' . $asslabel . ')';
                 }
                 // Determine a time/date for a activity/resource.
                 $expected = null;
                 $datetimepropery = 'date_time_' . $module . $instance->id;
                 if (isset($this->block->config) && property_exists($this->block->config, $datetimepropery)) {
                     $expected = $this->block->config->{$datetimepropery};
                 }
                 // If there is a date associated with the activity/resource, use that.
                 $lockedproperty = 'locked_' . $module . $instance->id;
                 if (isset($details['defaultTime']) && $instance->due != 0 && (isset($this->block->config) && property_exists($this->block->config, $lockedproperty) && $this->block->config->{$lockedproperty} == 1 || empty($expected))) {
                     $expected = progress_default_value($instance->due);
                     if (isset($this->block->config) && property_exists($this->block->config, $datetimepropery)) {
                         $this->block->config->{$datetimepropery} = $expected;
                     }
                 }
                 if (empty($expected)) {
                     // If a expected date is set in the activity completion, use that.
                     if ($moduleinfo->completion != 0 && $moduleinfo->completionexpected != 0) {
                         $expected = $moduleinfo->completionexpected;
                     } else {
                         if ($usingweeklyformat) {
                             $expected = $COURSE->startdate + ($moduleinfo->section > 0 ? $moduleinfo->section : 1) * 604800 - 300;
                         } else {
                             $expected = $endofweektime;
                         }
                     }
                 }
                 $moduleinfo->expected = $expected;
                 // Get the list of possible actions for the event.
                 $actions = array();
                 foreach ($details['actions'] as $action => $sql) {
                     // Before allowing pass marks, see that Grade to pass value is set.
                     if ($action == 'passed' || $action == 'passedby') {
                         $params = array('courseid' => $COURSE->id, 'itemmodule' => $module, 'iteminstance' => $instance->id);
                         $gradetopass = $DB->get_record('grade_items', $params, 'id,gradepass', IGNORE_MULTIPLE);
                         if ($gradetopass && $gradetopass->gradepass > 0) {
                             $actions[$action] = get_string($action, 'block_progress');
                         }
                     } else {
                         $actions[$action] = get_string($action, 'block_progress');
                     }
                 }
                 if (!empty($CFG->enablecompletion)) {
                     if ($moduleinfo->completion != 0) {
                         $actions['activity_completion'] = get_string('activity_completion', 'block_progress');
                     }
                 }
                 $moduleinfo->actions = $actions;
                 // Add the module to the array.
                 $unsortedmodules[] = $moduleinfo;
             }
         }
     }
     // Sort the array by coursemodule.
     $modulesinform = array();
     foreach ($unsortedmodules as $key => $moduleinfo) {
         $modulesinform[$moduleinfo->coursemoduleid] = $moduleinfo;
     }
     // Output the form elements for each module.
     if ($count > 0) {
         foreach ($sections as $i => $section) {
             if (count($section->sequence) > 0) {
                 // Output the section header.
                 $sectionname = get_string('section') . ':&nbsp;' . get_section_name($COURSE, $section);
                 $mform->addElement('header', 'section' . $i, format_string($sectionname));
                 if (method_exists($mform, 'setExpanded')) {
                     $mform->setExpanded('section' . $i);
                 }
                 // Display each monitorable activity/resource as a row.
                 foreach ($section->sequence as $coursemoduleid) {
                     if (array_key_exists($coursemoduleid, $modulesinform)) {
                         $moduleinfo = $modulesinform[$coursemoduleid];
                         // Start box.
                         $attributes = array('class' => 'progressConfigBox');
                         $moduleboxstart = HTML_WRITER::start_tag('div', $attributes);
                         $mform->addElement('html', $moduleboxstart);
                         // Icon, module type and name.
                         $modulename = get_string('pluginname', $moduleinfo->module);
                         $icon = $OUTPUT->pix_icon('icon', $modulename, 'mod_' . $moduleinfo->module);
                         $text = '&nbsp;' . $moduleinfo->label . ':&nbsp;' . format_string($moduleinfo->instancename);
                         $attributes = array('class' => 'progressConfigModuleTitle');
                         $moduletitle = HTML_WRITER::tag('div', $icon . $text, $attributes);
                         $mform->addElement('html', $moduletitle);
                         // Allow monitoring turned on or off.
                         $mform->addElement('selectyesno', 'config_monitor_' . $moduleinfo->uniqueid, get_string('config_header_monitored', 'block_progress'));
                         $mform->setDefault('config_monitor_' . $moduleinfo->uniqueid, $turnallon);
                         $mform->addHelpButton('config_monitor_' . $moduleinfo->uniqueid, 'what_does_monitored_mean', 'block_progress');
                         // Allow locking turned on or off.
                         if ($moduleinfo->lockpossible && $moduleinfo->instancedue != 0) {
                             $mform->addElement('selectyesno', 'config_locked_' . $moduleinfo->uniqueid, get_string('config_header_locked', 'block_progress'));
                             $mform->setDefault('config_locked_' . $moduleinfo->uniqueid, 1);
                             $mform->disabledif('config_locked_' . $moduleinfo->uniqueid, 'config_monitor_' . $moduleinfo->uniqueid, 'eq', 0);
                             $mform->addHelpButton('config_locked_' . $moduleinfo->uniqueid, 'what_locked_means', 'block_progress');
                         }
                         // Print the date selector.
                         $mform->addElement('date_time_selector', 'config_date_time_' . $moduleinfo->uniqueid, get_string('config_header_expected', 'block_progress'));
                         $mform->disabledif('config_date_time_' . $moduleinfo->uniqueid, 'config_locked_' . $moduleinfo->uniqueid, 'eq', 1);
                         $mform->disabledif('config_date_time_' . $moduleinfo->uniqueid, 'config_monitor_' . $moduleinfo->uniqueid, 'eq', 0);
                         $mform->disabledif('config_date_time_' . $moduleinfo->uniqueid, 'config_orderby', 'eq', 'orderbycourse');
                         $mform->disabledif('config_locked_' . $moduleinfo->uniqueid, 'config_orderby', 'eq', 'orderbycourse');
                         $mform->setDefault('config_date_time_' . $moduleinfo->uniqueid, $moduleinfo->expected);
                         $mform->addHelpButton('config_date_time_' . $moduleinfo->uniqueid, 'what_expected_by_means', 'block_progress');
                         // Print the action selector for the event.
                         if (count($moduleinfo->actions) == 1) {
                             $moduleinfo->actions = array_keys($moduleinfo->actions);
                             $action = $moduleinfo->actions[0];
                             $mform->addElement('static', 'config_action_static_' . $moduleinfo->uniqueid, get_string('config_header_action', 'block_progress'), get_string($action, 'block_progress'));
                             $mform->addElement('hidden', 'config_action_' . $moduleinfo->uniqueid, $action);
                         } else {
                             $mform->addElement('select', 'config_action_' . $moduleinfo->uniqueid, get_string('config_header_action', 'block_progress'), $moduleinfo->actions);
                             if ((!$moduleinfo->lockpossible || $moduleinfo->instancedue == 0) && array_key_exists('activity_completion', $moduleinfo->actions)) {
                                 $defaultaction = 'activity_completion';
                             } else {
                                 $defaultaction = $details['defaultAction'];
                             }
                             $mform->setDefault('config_action_' . $moduleinfo->uniqueid, $defaultaction);
                             $mform->disabledif('config_action_' . $moduleinfo->uniqueid, 'config_monitor_' . $moduleinfo->uniqueid, 'eq', 0);
                         }
                         $mform->setType('config_action_' . $moduleinfo->uniqueid, PARAM_ALPHANUMEXT);
                         $mform->addHelpButton('config_action_' . $moduleinfo->uniqueid, 'what_actions_can_be_monitored', 'block_progress');
                         // End box.
                         $moduleboxend = HTML_WRITER::end_tag('div');
                         $mform->addElement('html', $moduleboxend);
                     }
                 }
             }
         }
     } else {
         $mform->addElement('html', get_string('no_events_config_message', 'block_progress'));
     }
 }
Example #5
0
/**
 * Gets event information about modules monitored by an instance of a Progress Bar block
 *
 * @param stdClass $config  The block instance configuration values
 * @param array    $modules The modules used in the course
 * @return mixed   returns array of visible events monitored,
 *                 empty array if none of the events are visible,
 *                 null if all events are configured to "no" monitoring and
 *                 0 if events are available but no cofig is set
 */
function event_information($config, $modules)
{
    global $COURSE, $DB;
    $dbmanager = $DB->get_manager();
    // used to check if tables exist
    $events = array();
    $numevents = 0;
    $numeventsconfigured = 0;
    // Check each known module (described in lib.php)
    foreach ($modules as $module => $details) {
        $fields = 'id, name';
        if (array_key_exists('defaultTime', $details)) {
            $fields .= ', ' . $details['defaultTime'] . ' as due';
        }
        // Check if this type of module is used in the course, gather instance info
        $records = $DB->get_records($module, array('course' => $COURSE->id), '', $fields);
        foreach ($records as $record) {
            // Is the module being monitored?
            if (isset($config->{'monitor_' . $module . $record->id})) {
                $numeventsconfigured++;
            }
            if (progress_default_value($config->{'monitor_' . $module . $record->id}, 0) == 1) {
                $numevents++;
                // Check the time the module is due
                if (isset($details['defaultTime']) && $record->due != 0 && progress_default_value($config->{'locked_' . $module . $record->id}, 0)) {
                    $expected = progress_default_value($record->due);
                } else {
                    $expected = $config->{'date_time_' . $module . $record->id};
                }
                // Get the course module info
                $coursemodule = get_coursemodule_from_instance($module, $record->id, $COURSE->id);
                // Check if the module is visible, and if so, keep a record for it
                if ($coursemodule->visible == 1) {
                    $events[] = array('expected' => $expected, 'type' => $module, 'id' => $record->id, 'name' => format_string($record->name), 'cmid' => $coursemodule->id);
                }
            }
        }
    }
    if ($numeventsconfigured == 0) {
        return 0;
    }
    if ($numevents == 0) {
        return null;
    }
    // Sort by first value in each element, which is time due
    sort($events);
    return $events;
}