Exemple #1
0
 /**
  * Returns extra icon filters
  * @param string $extra sql
  * @return string
  */
 function get_sql_filter($extra = '', $exceptions = array(), $allow_interactive_filters = false, $allow_configured_filters = true, $secondary_filtering_key = '', $config_filter = false)
 {
     global $SESSION;
     $found = false;
     $per_filter_data = array();
     $sqls = array();
     $params = array();
     $user_preferences = php_report_filtering_get_user_preferences($this->reportname);
     foreach ($user_preferences as $key => $value) {
         $parts = explode('/', $key);
         //is preference php-report related?
         if (strpos($parts[0], 'php_report_') === 0) {
             $element_name = $parts[1];
             if (strpos($element_name, '_') !== FALSE) {
                 $parts = explode('_', $element_name);
                 $group_name = $parts[0];
             } else {
                 $group_name = $element_name;
             }
             if (!isset($per_filter_data[$group_name])) {
                 $per_filter_data[$group_name] = array();
             }
             $per_filter_data[$group_name][$element_name] = $value;
         }
     }
     //Using user preferences, create list of icons to send back
     $result = '';
     foreach ($this->_fields as $shortname => $field) {
         // Using exceptions array from report to not add any config type filters to filter
         if (in_array($shortname, (array) $exceptions)) {
             continue;
         }
         if (isset($per_filter_data[$shortname]) && $found !== true) {
             $formatted_data = $field->check_data((object) $per_filter_data[$shortname]);
             if ($formatted_data != false) {
                 if ($config_filter == true) {
                     $result = $formatted_data['value'];
                     $found = true;
                 } else {
                     $newsql = $field->get_sql_filter($formatted_data);
                     if ($newsql !== null && !empty($newsql[0])) {
                         $sqls[] = $newsql[0];
                         $params += $newsql[1];
                     }
                 }
             }
         }
     }
     //combine SQL conditions
     if (!empty($sqls)) {
         $sql_piece = implode(' AND ', $sqls);
         if ($result === '') {
             $result = $sql_piece;
         } else {
             $result .= ' AND ' . $sql_piece;
         }
     }
     return array($result, $params);
     // TBD
 }
 /**
  * Method that specifies the report's columns
  * (specifies various user-oriented fields)
  *
  * @return  table_report_column array  The list of report columns
  */
 function get_columns()
 {
     $result = array();
     $optionalcols = array('cur_name' => array('name' => 'curriculumname', 'id' => 'cur.name'), 'class_role' => array('name' => 'classrole', 'id' => '\'\' AS classrole'));
     //make sure the session is updated with URL parameters
     php_report_filtering_get_user_preferences($this->get_report_shortname());
     $filters = php_report_filtering_get_active_filter_values($this->get_report_shortname(), 'filter-detailcolumns');
     $cols = $filters[0]['value'];
     // Curriculum name
     if ($cols['cur_name']) {
         $curriculumname_heading = get_string('column_curriculumname', $this->languagefile);
         $curriculumname_column = new table_report_column('cur.name AS curriculumname', $curriculumname_heading, 'curriculumname');
         $result[] = $curriculumname_column;
         unset($optionalcols['cur_name']);
     }
     // Add curriculum custom fields
     $curriculum_custom_field_columns = $this->get_custom_field_columns($cols);
     $result = array_merge($result, $curriculum_custom_field_columns);
     // Course name
     $coursename_heading = get_string('column_coursename', $this->languagefile);
     $coursename_column = new table_report_column('crs.name AS coursename', $coursename_heading, 'coursename');
     $result[] = $coursename_column;
     // Class idnumber
     $classidnumber_heading = get_string('column_classidnumber', $this->languagefile);
     $classidnumber_column = new table_report_column('cls.idnumber AS classidnumber', $classidnumber_heading, 'classidnumber');
     $result[] = $classidnumber_column;
     // Environment name
     $environment_heading = get_string('column_environment', $this->languagefile);
     $environment_column = new table_report_column('env.name AS envname', $environment_heading, 'environment');
     $result[] = $environment_column;
     // Class startdate
     $classstartdate_heading = get_string('column_classstartdate', $this->languagefile);
     $classstartdate_column = new table_report_column('cls.startdate', $classstartdate_heading, 'classstartdate');
     $result[] = $classstartdate_column;
     // Completion elements
     $completionelements_heading = get_string('column_completionelements', $this->languagefile);
     $completionelements_column = new table_report_column('\'\' AS elementsdisplayed', $completionelements_heading, 'completionelements');
     $result[] = $completionelements_column;
     // Completion status
     $completion_heading = get_string('column_completion', $this->languagefile);
     $completion_column = new table_report_column('0 AS completionstatus', $completion_heading, 'completion');
     $result[] = $completion_column;
     // Completion date
     $completiondate_heading = get_string('column_completiondate', $this->languagefile);
     $completiondate_column = new table_report_column('completetime', $completiondate_heading, 'completiondate');
     $result[] = $completiondate_column;
     // Credits
     $credits_heading = get_string('column_credits', $this->languagefile);
     $credits_column = new table_report_column('0 AS creditsdisplayed', $credits_heading, 'credits');
     $result[] = $credits_column;
     foreach ($optionalcols as $key => $col) {
         if (!empty($cols[$key])) {
             $heading = get_string('column_' . $col['name'], $this->languagefile);
             $column = new table_report_column($col['id'], $heading, $col['name']);
             $result[] = $column;
         }
     }
     return $result;
 }
 /**
  * Specifies available report filters
  * (empty by default but can be implemented by child class)
  *
  * @param   boolean  $init_data  If true, signal the report to load the
  *                               actual content of the filter objects
  *
  * @return  array                The list of available filters
  */
 function get_filters($init_data = true)
 {
     global $CFG, $CURMAN, $SESSION;
     $filters = array();
     $users = array();
     if ($init_data) {
         $contexts = get_contexts_by_capability_for_user('user', $this->access_capability, $this->userid);
         $user_objects = usermanagement_get_users_recordset('name', 'ASC', 0, 0, '', $contexts);
         // If in interactive mode, user should have access to at least their own info
         if ($this->execution_mode == php_report::EXECUTION_MODE_INTERACTIVE) {
             $cm_user_id = cm_get_crlmuserid($this->userid);
             $user_object = new user($cm_user_id);
             $users[$user_object->id] = fullname($user_object) . ' (' . $user_object->idnumber . ')';
         }
         if (!empty($user_objects)) {
             // Create a list of users this user has permissions to view
             while ($user_object = rs_fetch_next_record($user_objects)) {
                 $users[$user_object->id] = $user_object->name . ' (' . $user_object->idnumber . ')';
             }
         }
     }
     $filters[] = new generalized_filter_entry('userid', 'crlmuser', 'id', get_string('filter_user', $this->lang_file), false, 'simpleselect', array('choices' => $users, 'numeric' => true, 'noany' => true, 'help' => array('individual_course_progress_user_help', get_string('displayname', 'rlreport_individual_course_progress'), 'block_php_report')));
     // Needs to pull from saved prefs
     $user_preferences = php_report_filtering_get_user_preferences('php_report_' . $this->get_report_shortname());
     $report_index = 'php_report_' . $this->get_report_shortname() . '/field' . $this->id;
     if (!isset($user_preferences[$report_index]) || ($default_fieldid_list = @unserialize(base64_decode($user_preferences[$report_index]))) === false) {
         $default_fieldid_list = array();
     }
     $field_list = array('fieldids' => $default_fieldid_list);
     // Add block id to field list array
     $field_list['block_instance'] = $this->id;
     $field_list['reportname'] = $this->get_report_shortname();
     // Need help text
     $field_list['help'] = array('individual_course_progress_field_help', get_string('displayname', 'rlreport_individual_course_progress'), 'block_php_report');
     $this->multiselect_filter = new generalized_filter_entry('field' . $this->id, 'field' . $this->id, 'id', get_string('selectcustomfields', $this->lang_file), false, 'custom_field_multiselect_values', $field_list);
     $filters[] = $this->multiselect_filter;
     return $filters;
 }
Exemple #4
0
    /**
     * Adds controls specific to this filter in the form.
     * @param object $mform a MoodleForm object to setup
     * @uses  $CFG
     * @uses  $OUTPUT
     * @uses  $PAGE
     * @uses  $USER
     */
    function setupForm(&$mform)
    {
        global $CFG, $OUTPUT, $PAGE, $USER;
        // Javascript for cluster dropdown onchange event
        $cluster_group_separator = '------------------------------';
        $js = '
<script type="text/javascript">
//<![CDATA[
    function dropdown_separator(selectelem) {
        /* alert("dropdown_separator(" + selectelem.selectedIndex + ")"); */
        if (selectelem.options[selectelem.selectedIndex].value < 0) {
            return 0;
        }
        return selectelem.selectedIndex;
    }
//]]>
</script>
';
        /**
         * CSS includes
         */
        $mform->addElement('html', '<style>@import url("' . $CFG->wwwroot . '/lib/yui/2.9.0/build/treeview/assets/skins/sam/treeview-skin.css");</style>' . $js);
        /**
         * Get set up necessary CSS classes
         */
        $manageclusters_css_class = block_elisadmin_get_item_css_class('manageclusters');
        $cluster_css_class = block_elisadmin_get_item_css_class('cluster_instance');
        //figure out which capability to check
        if ($this->execution_mode == php_report::EXECUTION_MODE_SCHEDULED) {
            $capability = 'local/elisreports:schedule';
        } else {
            $capability = 'local/elisreports:view';
        }
        $context_result = pm_context_set::for_user_with_capability('cluster', $capability, $USER->id);
        /**
         * TreeView-related work
         */
        //CM entities for placement at the top of the menu
        $cm_entity_pages = array();
        $cm_entity_pages[] = new menuitem('root');
        if ($clusters = cluster_get_listing('priority, name', 'ASC', 0, 0, '', '', array('parent' => 0))) {
            foreach ($clusters as $cluster) {
                $params = array('id' => $cluster->id, 'action' => 'viewreport', 'execution_mode' => $this->execution_mode);
                $cluster_count = cluster_count_records('', '', array('parent' => $cluster->id));
                $isLeaf = empty($cluster_count);
                $cm_entity_pages[] = test_cluster_tree_get_menu_item('cluster', $cluster, 'root', $manageclusters_css_class, $cluster->id, 0, $params, $isLeaf);
            }
        }
        $menuitemlisting = new menuitemlisting($cm_entity_pages);
        $tree = new checkbox_treerepresentation($menuitemlisting, $this->options['report_id']);
        /**
         * UI element setup
         */
        require_once $CFG->dirroot . '/local/eliscore/lib/filtering/equalityselect.php';
        $choices_array = array(0 => get_string('anyvalue', 'filters'));
        //set up cluster listing
        if ($records = $this->cluster_dropdown_get_listing($context_result)) {
            foreach ($records as $record) {
                if (empty($choices_array[$record->id])) {
                    if (count($choices_array) > 1) {
                        $choices_array[-$record->id] = $cluster_group_separator;
                    }
                    $ancestors = $record->depth - 1;
                    // shorten really long cluster names
                    $name = strlen($record->name) > 100 ? substr($record->name, 0, 100) . '...' : $record->name;
                    $choices_array[$record->id] = $ancestors ? str_repeat('- ', $ancestors) . $name : $name;
                    //merge in child clusters
                    $child_array = $this->find_child_clusters($records, $record->id, $ancestors);
                    $choices_array = $this->merge_array_keep_keys($choices_array, $child_array);
                }
            }
        }
        //get help text
        if (isset($this->options['help'])) {
            $this->_filterhelp = $this->options['help'];
        } else {
            $this->_filterhelp = null;
        }
        //add filterhelp and label to this filter
        //import required css for the fieldset
        $style = '<style>@import url("' . $CFG->wwwroot . '/local/elisprogram/styles.css");</style>';
        $helplink = '';
        $nested_fieldset = '';
        $title = '';
        if ($this->options['fieldset']) {
            $nested_fieldset = '<fieldset class="nested clearfix" id="' . $this->_uniqueid . "_label\">\n";
        } else {
            $title = $this->_label . $helplink . '&nbsp;';
        }
        $legend = '<legend class="ftoggler">' . $this->_label . "</legend>\n";
        $mform->addElement('html', $style . $nested_fieldset . $legend);
        $mform->addElement('static', $this->_uniqueid . '_help', '');
        // cluster select dropdown
        $selectparams = array('onchange' => 'this.selectedIndex = dropdown_separator(this);');
        $mform->addElement('select', $this->_uniqueid . '_dropdown', $title, $choices_array, $selectparams);
        //dropdown / cluster tree state storage
        $mform->addElement('hidden', $this->_uniqueid . '_usingdropdown');
        $mform->setType($this->_uniqueid . '_usingdropdown', PARAM_BOOL);
        // Must use addHelpButton() to NOT open help link on page, but in popup!
        $mform->addHelpButton($this->_uniqueid . '_dropdown', $this->_filterhelp[0], $this->_filterhelp[2]);
        // TBV
        //default to showing dropdown if nothing has been persisted
        $report_shortname = $this->options['report_shortname'];
        $preferences = php_report_filtering_get_user_preferences($report_shortname);
        if (!isset($preferences["php_report_{$report_shortname}/{$this->_uniqueid}_usingdropdown"])) {
            $mform->setDefault($this->_uniqueid . '_usingdropdown', 1);
        }
        $initclustertreeopts = array($CFG->httpswwwroot, $tree->instanceid, $this->_uniqueid, $tree->get_js_object(), $this->execution_mode, $this->options['report_id'], $this->options['dropdown_button_text'], $this->options['tree_button_text']);
        $PAGE->requires->yui_module('moodle-local_elisprogram-clustertree', 'M.local_elisprogram.init_clustertree', $initclustertreeopts, null, true);
        // cluster tree
        $clustertreehtml = '<div class="fitem"><div class="fitemtitle"></div>' . '<style>@import url("' . $CFG->wwwroot . '/lib/yui/2.9.0/build/treeview/assets/skins/sam/treeview.css");</style>' . '<div id="cluster_param_tree_' . $tree->instanceid . '_' . $this->_uniqueid . '" class="ygtv-checkbox felement"></div>' . '</div>';
        $mform->addElement('html', $clustertreehtml);
        //list of explicitly selected elements
        $mform->addElement('hidden', $this->_uniqueid . '_listing');
        $mform->setType($this->_uniqueid . '_listing', PARAM_TEXT);
        //list of selected and unexpanded elements
        $mform->addElement('hidden', $this->_uniqueid . '_unexpanded');
        $mform->setType($this->_uniqueid . '_unexpanded', PARAM_TEXT);
        //list of explicitly unselected elements
        $mform->addElement('hidden', $this->_uniqueid . '_clrunexpanded');
        $mform->setType($this->_uniqueid . '_clrunexpanded', PARAM_TEXT);
        $mform->addElement('button', $this->_uniqueid . '_toggle', '');
        // close hacked nested fieldset
        if ($this->options['fieldset']) {
            $mform->addElement('html', '</fieldset>');
        }
    }
 /**
  * Transforms a heading element displayed above the columns into a listing of such heading elements
  *
  * @param   string array           $grouping_current  Mapping of field names to current values in the grouping
  * @param   table_report_grouping  $grouping          Object containing all info about the current level of grouping
  *                                                    being handled
  * @param   stdClass               $datum             The most recent record encountered
  * @param   string    $export_format  The format being used to render the report
  * @uses    $DB
  * @return  string array                              Set of text entries to display
  */
 function transform_grouping_header_label($grouping_current, $grouping, $datum, $export_format)
 {
     global $DB;
     if ($grouping->field == 'curriculum.id') {
         /**
          * Curriculum grouping - display the curriculum name or a default
          * if none
          */
         //get the curriculum id from the current grouping info
         $curriculumid = $grouping_current['curriculum.id'];
         if (empty($curriculumid)) {
             //default label
             return array($this->add_grouping_header($grouping->label, get_string('non_curriculum_courses', 'rlreport_course_completion_by_cluster'), $export_format));
         } else {
             //actually have a curriculum, so display it
             $curriculum_name = $DB->get_field(curriculum::TABLE, 'name', array('id' => $curriculumid));
             $completed_description = '';
             if ($datum->completed) {
                 //flag the curriculum as complete and show the completion date
                 $a = $this->format_date($datum->curriculumcompletetime);
                 $completed_description = get_string('completed_yes', 'rlreport_course_completion_by_cluster', $a);
             } else {
                 //flag the curriculum as incomplete
                 $completed_description = get_string('completed_no', 'rlreport_course_completion_by_cluster');
             }
             return array($this->add_grouping_header($grouping->label, $curriculum_name . ' ' . $completed_description, $export_format));
         }
     } else {
         if ($grouping->field == 'cluster.id') {
             /**
              * Cluster grouping - display the hierarchy of clusters
              */
             //get the current (new) cluster id
             $clusterid = $grouping_current[$grouping->field];
             $cluster = new userset($clusterid);
             $result = array();
             //build the hierarchy bottom-up based on the supplied cluster
             $current_cluster_hierarchy = array();
             while ($cluster->id != 0) {
                 $current_cluster_hierarchy[] = $cluster->id;
                 $result[] = $this->add_grouping_header($grouping->label, $cluster->name, $export_format);
                 $cluster = new userset($cluster->parent);
             }
             //really need it top-down for comparison
             $current_cluster_hierarchy = array_reverse($current_cluster_hierarchy);
             //find the first position where the old and new hieararchies differ
             $final_pos = -1;
             for ($i = 0; $i < count($this->last_cluster_hierarchy) && $i < count($current_cluster_hierarchy); $i++) {
                 if ($this->last_cluster_hierarchy[$i] != $current_cluster_hierarchy[$i]) {
                     $final_pos = $i;
                     break;
                 }
             }
             //default to next level down
             if ($final_pos == -1) {
                 $final_pos = max(count($this->last_cluster_hierarchy), count($current_cluster_hierarchy)) - 1;
             }
             //store for next iteration
             $this->last_cluster_hierarchy = $current_cluster_hierarchy;
             $result = array_reverse($result);
             /**
              * Get the listing of cluster leaders
              */
             $preferences = php_report_filtering_get_user_preferences('course_completion_by_cluster');
             if (isset($preferences['php_report_course_completion_by_cluster/clusterrole'])) {
                 //query to retrieve users directly assigned the configured role in the current cluster
                 $sql = 'SELECT u.* FROM {user} u
                      JOIN {role_assignments} ra
                        ON u.id = ra.userid
                        AND ra.roleid = ?
                      JOIN {context} ctxt
                        ON ra.contextid = ctxt.id
                        AND ctxt.instanceid = ?
                        AND ctxt.contextlevel = ?';
                 $params = array($preferences['php_report_course_completion_by_cluster/clusterrole'], $datum->cluster, CONTEXT_ELIS_USERSET);
                 $display = '';
                 //append all the names together if there are multiple
                 if ($recordset = $DB->get_recordset_sql($sql, $params)) {
                     foreach ($recordset as $record) {
                         if ($display == '') {
                             $display = php_report::fullname($record);
                         } else {
                             $display .= ', ' . php_report::fullname($record);
                         }
                     }
                     $recordset->close();
                 }
                 if ($display == '') {
                     //no names found
                     $display = get_string('na', 'rlreport_course_completion_by_cluster');
                 }
                 //add a header entry
                 $cluster_leader_label = get_string('cluster_leaders', 'rlreport_course_completion_by_cluster') . ' ';
                 $result[] = $this->add_grouping_header($cluster_leader_label, $display, $export_format);
             }
             //return the labels in top-down order
             return $result;
         } else {
             return array($this->add_grouping_header($grouping->label, $grouping_current[$grouping->field], $export_format));
         }
     }
 }
 function update_field_list($action, $fieldid, $fieldname, $fieldidlist, $fieldnamelist, $scheduled)
 {
     global $SESSION;
     switch ($action) {
         case 'add':
             $fieldidlist[] = $fieldid;
             $fieldnamelist[] = $fieldname;
             break;
         case 'remove':
             // Get field index of array element - used for moveup and move down
             $fieldindex = array_search($fieldid, $fieldidlist);
             unset($fieldidlist[$fieldindex]);
             unset($fieldnamelist[$fieldindex]);
             break;
         case 'up':
             // Get field index of array element - used for moveup and move down
             $fieldindex = array_search($fieldid, $fieldidlist);
             $fieldidlist = $this->move_up($fieldidlist, $fieldindex);
             $fieldnamelist = $this->move_up($fieldnamelist, $fieldindex);
             break;
         case 'down':
             // Get field index of array element - used for moveup and move down
             $fieldindex = array_search($fieldid, $fieldidlist);
             $fieldidlist = $this->move_down($fieldidlist, $fieldindex);
             $fieldnamelist = $this->move_down($fieldnamelist, $fieldindex);
             break;
         case 'init':
             // Pull filter params if report is not being scheduled
             if (!$scheduled) {
                 $user_preferences = php_report_filtering_get_user_preferences($this->_block_instance);
                 $report_index = 'php_report_' . $this->_block_instance . '/field' . $this->_block_instance;
             }
             if ($scheduled) {
                 // accept current fieldidlist if in schedule mode
                 $fieldidlist = $fieldidlist;
                 $fieldnamelist = $fieldnamelist;
             } else {
                 if (!empty($report_index) && isset($user_preferences[$report_index])) {
                     $fieldidlist = unserialize(base64_decode($user_preferences[$report_index]));
                     $fieldnamelist = array();
                 } else {
                     $fieldidlist = array();
                     $fieldnamelist = array();
                 }
             }
             break;
     }
     // Reset this class's fieldidlist and fieldnamelist to recalculated lists
     $this->_fieldidlist = $fieldidlist;
     $this->_fieldnamelist = $fieldnamelist;
 }
 /**
  * Takes a record and transforms it into an appropriate format
  * This method is set up as a hook to be implented by actual report class
  *
  * @param   stdClass  $record         The current report record
  * @param   string    $export_format  The format being used to render the report
  * @uses $CFG
  * @return stdClass  The reformatted record
  */
 function transform_record($record, $export_format)
 {
     global $CFG;
     $showcurricula = $this->check_curricula();
     if (isset($record->id)) {
         //add a default curriculum name if appropriate
         if ($showcurricula && (empty($record->curid) || empty($record->name))) {
             if (!empty($record->preservecurname)) {
                 //actually corresponds to class enrolments
                 $record->name = get_string('noncurriculumcourses', $this->languagefile);
             } else {
                 //doesn't correspond to anything
                 $record->name = get_string('na', $this->languagefile);
             }
         }
         //link curriculum name to its "view" page if the the current record has a curriculum
         if ($export_format == table_report::$EXPORT_FORMAT_HTML && !empty($record->curid)) {
             $page = new curriculumpage(array('id' => $record->curid, 'action' => 'view'));
             if ($page->can_do()) {
                 $url = $page->url;
                 $record->name = '<span class="external_report_link">' . "<a href=\"{$url}\" target=\"_blank\">{$record->name}</a></span>";
             }
         }
         //base url
         $url = "{$CFG->wwwroot}/local/elisreports/render_report_page.php";
         //params being passed via url
         $url_params = array('report' => 'user_class_completion_details', 'filter-up-idnumber' => urlencode($record->useridnumber), 'filter-up-idnumber_op' => generalized_filter_text::$OPERATOR_IS_EQUAL_TO);
         $preferences = php_report_filtering_get_user_preferences($this->reportname);
         if (!empty($preferences)) {
             foreach ($preferences as $key => $val) {
                 // Determine if this is one of the completion range filter values
                 preg_match('/.+\\/(filter\\-completerange\\_[a-z]{3})/', $key, $matches);
                 if (isset($matches[1])) {
                     $url_params[$matches[1]] = $val;
                 }
             }
         }
         //used to track whether to add a ? or a &
         $first = true;
         foreach ($url_params as $key => $value) {
             //append the parameter to the url
             if ($first) {
                 $url .= '?';
             } else {
                 $url .= '&';
             }
             $url .= "{$key}={$value}";
             //signal the use of & on subsequent iterations
             $first = false;
         }
         //add parameters related to all these groups to the report
         $groupnames = array('filter-detailheaders', 'filter-detailcolumns');
         foreach ($groupnames as $groupname) {
             if ($first) {
                 $url .= '?';
             } else {
                 $url .= '&';
             }
             $url .= $this->get_param_url_string($groupname);
         }
         //extra attributes we are including in the anchor tag
         $tag_attributes = array('target' => '_blank');
         //build the additional attributes
         $attribute_string = '';
         foreach ($tag_attributes as $key => $value) {
             $attribute_string .= " {$key}={$value}";
         }
         // details label for the link.  First check to see if this is the first instance of the
         // student's record.  If so then show the details link.
         $record->id = '';
         if ($this->student_id_num != $record->useridnumber) {
             $link_text = get_string('details', $this->languagefile);
             $this->student_id_num = $record->useridnumber;
             $record->id = '<span class="external_report_link"><a href="' . $url . '"' . $attribute_string . '>' . $link_text . '</a></span>';
         }
     }
     //show link to user's profile based on capability to view the student management capability
     $fullname = php_report::fullname($record);
     if ($export_format == php_report::$EXPORT_FORMAT_HTML) {
         $userpage = new userpage(array('id' => $record->userid, 'action' => 'view'));
         if ($userpage->can_do()) {
             $record->lastname = '<span class="external_report_link"><a href="' . $userpage->url . '" target="_blank">' . $fullname . '</a></span>';
         } else {
             $record->lastname = $fullname;
         }
     } else {
         $record->lastname = $fullname;
     }
     //convert times to appropriate format
     if (!empty($record->timecompleted) && !empty($record->completed)) {
         $record->timecompleted = $this->format_date($record->timecompleted);
     } else {
         $record->timecompleted = get_string('na', $this->languagefile);
     }
     if (!empty($record->timeexpired) && !empty($record->completed)) {
         $record->timeexpired = $this->format_date($record->timeexpired);
     } else {
         $record->timeexpired = get_string('na', $this->languagefile);
     }
     //N/A for cretificate number if a valid one is not set
     if (empty($record->certificatecode) || empty($record->completed)) {
         $record->certificatecode = get_string('na', $this->languagefile);
     }
     //copy result of complex query into simple field
     if (!empty($record->numcredits)) {
         //use the provided value
         $record->displaynumcredits = $this->format_credits($record->numcredits);
     } else {
         //default to zero
         $record->displaynumcredits = $this->format_credits(0);
     }
     //handle custom field default values and display logic
     $this->transform_custom_field_data($record);
     return $record;
 }
 /**
  * Gets the chosen userid from the filter information
  *
  * @return int the user id
  */
 function get_chosen_userid()
 {
     $chosen_userid = '';
     $report_filters = php_report_filtering_get_user_preferences($this->get_report_shortname());
     if (!empty($report_filters) && is_array($report_filters)) {
         foreach ($report_filters as $filter => $val) {
             if ($filter === 'php_report_' . $this->get_report_shortname() . '/' . 'filterautoc') {
                 $chosen_userid = $val;
             }
         }
     }
     if (!empty($chosen_userid) && is_numeric($chosen_userid)) {
         return $chosen_userid;
     } else {
         return false;
     }
 }
 /**
  * Transforms a heading element displayed above the columns into a listing of such heading elements
  *
  * @param   string array           $grouping_current  Mapping of field names to current values in the grouping
  * @param   table_report_grouping  $grouping          Object containing all info about the current level of grouping
  *                                                    being handled
  * @param   stdClass               $datum             The most recent record encountered
  * @param   string    $export_format  The format being used to render the report
  *
  * @return  string array                              Set of text entries to display
  */
 function transform_grouping_header_label($grouping_current, $grouping, $datum, $export_format)
 {
     global $SESSION, $CURMAN;
     if ($grouping->field == 'curriculum.id') {
         /**
          * Curriculum grouping - display the curriculum name or a default
          * if none
          */
         //get the curriculum id from the current grouping info
         $curriculumid = $grouping_current['curriculum.id'];
         if (empty($curriculumid)) {
             //default label
             return array($this->add_grouping_header($grouping->label, get_string('non_curriculum_courses', 'rlreport_course_completion_by_cluster'), $export_format));
         } else {
             //actually have a curriculum, so display it
             $curriculum_name = get_field('crlm_curriculum', 'name', 'id', $curriculumid);
             $completed_description = '';
             if ($datum->completed) {
                 //flag the curriculum as complete and show the completion date
                 $a = $this->format_date($datum->curriculumcompletetime);
                 $completed_description = get_string('completed_yes', 'rlreport_course_completion_by_cluster', $a);
             } else {
                 //flag the curriculum as incomplete
                 $completed_description = get_string('completed_no', 'rlreport_course_completion_by_cluster');
             }
             return array($this->add_grouping_header($grouping->label, $curriculum_name . ' ' . $completed_description, $export_format));
         }
     } else {
         if ($grouping->field == 'cluster.id') {
             /**
              * Cluster grouping - display the hierarchy of clusters
              */
             //get the current (new) cluster id
             $clusterid = $grouping_current[$grouping->field];
             $cluster = new cluster($clusterid);
             $result = array();
             //build the hierarchy bottom-up based on the supplied cluster
             $current_cluster_hierarchy = array();
             while ($cluster->id != 0) {
                 $current_cluster_hierarchy[] = $cluster->id;
                 $result[] = $this->add_grouping_header($grouping->label, $cluster->name, $export_format);
                 $cluster = new cluster($cluster->parent);
             }
             //really need it top-down for comparison
             $current_cluster_hierarchy = array_reverse($current_cluster_hierarchy);
             //find the first position where the old and new hieararchies differ
             $final_pos = -1;
             for ($i = 0; $i < count($this->last_cluster_hierarchy) && $i < count($current_cluster_hierarchy); $i++) {
                 if ($this->last_cluster_hierarchy[$i] != $current_cluster_hierarchy[$i]) {
                     $final_pos = $i;
                     break;
                 }
             }
             //default to next level down
             if ($final_pos == -1) {
                 $final_pos = max(count($this->last_cluster_hierarchy), count($current_cluster_hierarchy)) - 1;
             }
             //store for next iteration
             $this->last_cluster_hierarchy = $current_cluster_hierarchy;
             $result = array_reverse($result);
             /**
              * Get the listing of cluster leaders
              */
             $preferences = php_report_filtering_get_user_preferences('course_completion_by_cluster');
             if (isset($preferences['php_report_course_completion_by_cluster/clusterrole'])) {
                 //context to check for role assignments
                 $cluster_context_level = context_level_base::get_custom_context_level('cluster', 'block_curr_admin');
                 //query to retrieve users directly assigned the configured role in the current cluster
                 $sql = "SELECT u.*\n                         FROM\n                         {$CURMAN->db->prefix_table('user')} u\n                         JOIN {$CURMAN->db->prefix_table('role_assignments')} ra\n                           ON u.id = ra.userid\n                           AND ra.roleid = {$preferences['php_report_course_completion_by_cluster/clusterrole']}\n                         JOIN {$CURMAN->db->prefix_table('context')} ctxt\n                           ON ra.contextid = ctxt.id\n                           AND ctxt.instanceid = {$datum->cluster}\n                           AND ctxt.contextlevel = {$cluster_context_level}";
                 $display = '';
                 //append all the names together if there are multiple
                 if ($recordset = get_recordset_sql($sql)) {
                     while ($record = rs_fetch_next_record($recordset)) {
                         if ($display == '') {
                             $display = fullname($record);
                         } else {
                             $display .= ', ' . fullname($record);
                         }
                     }
                 }
                 if ($display == '') {
                     //no names found
                     $display = get_string('na', 'rlreport_course_completion_by_cluster');
                 }
                 //add a header entry
                 $cluster_leader_label = get_string('cluster_leaders', 'rlreport_course_completion_by_cluster') . ' ';
                 $result[] = $this->add_grouping_header($cluster_leader_label, $display, $export_format);
             }
             //return the labels in top-down order
             return $result;
         } else {
             return array($this->add_grouping_header($grouping->label, $grouping_current[$grouping->field], $export_format));
         }
     }
 }
 /**
  * Specifies a pool of attributes to pull preferences from, organized by filter
  *
  * @return  array  Mapping of parameter shortname to a mapping of its UI element names to values
  */
 protected function get_preferences()
 {
     if (!empty($this->preferences_source_data)) {
         //using a pre-defined pool of preferences for API-related reasons,
         //so just convert them to the necessary format
         return php_report_filtering_get_per_filter_data($this, $this->preferences_source_data);
     }
     $per_filter_data = array();
     //obtain the current selection of preferences, based on stored
     //preferences and current active values
     $user_preferences = php_report_filtering_get_user_preferences($this->reportname);
     //go through and group accordingly
     foreach ($user_preferences as $key => $value) {
         $parts = explode('/', $key);
         //is preference php-report related?
         //prefix used to make sure we're in the right report
         $preference_prefix = 'php_report_' . $this->reportname;
         //is preference related to this report?
         //match exactly in case one report name prefixes another
         if ($parts[0] == $preference_prefix) {
             $element_name = $parts[1];
             //calculate the group name
             if (strpos($element_name, '_') !== FALSE) {
                 //multi-element group
                 $parts = explode('_', $element_name);
                 $group_name = $parts[0];
             } else {
                 //single-element group
                 $group_name = $element_name;
             }
             if (!isset($per_filter_data[$group_name])) {
                 //set up the array
                 $per_filter_data[$group_name] = array();
             }
             //append the data to the current group
             $per_filter_data[$group_name][$element_name] = $value;
         }
     }
     return $per_filter_data;
 }
 /**
  * Specifies available report filters
  * (empty by default but can be implemented by child class)
  *
  * @param   boolean  $init_data  If true, signal the report to load the
  *                               actual content of the filter objects
  *
  * @return  array                The list of available filters
  */
 function get_filters($init_data = true)
 {
     global $CFG, $CURMAN, $SESSION;
     $cms = array();
     $contexts = get_contexts_by_capability_for_user('course', $this->access_capability, $this->userid);
     $cms_objects = curriculum_get_listing('name', 'ASC', 0, 0, '', '', $contexts);
     if (!empty($cms_objects)) {
         foreach ($cms_objects as $curriculum) {
             $cms[$curriculum->id] = $curriculum->name;
         }
     }
     $curricula_options = array('choices' => $cms, 'numeric' => false);
     // Needs to pull from saved prefs
     $user_preferences = php_report_filtering_get_user_preferences($this->get_report_shortname());
     $report_index = 'php_report_' . $this->get_report_shortname() . '/field' . $this->id;
     if (!isset($user_preferences[$report_index]) || ($default_fieldid_list = @unserialize(base64_decode($user_preferences[$report_index]))) === false) {
         $default_fieldid_list = array();
     }
     $field_list = array('fieldids' => $default_fieldid_list);
     // Add block id to field list array
     $field_list['block_instance'] = $this->id;
     $field_list['reportname'] = $this->get_report_shortname();
     // Need help text
     $field_list['help'] = array('course_progress_summary_help', get_string('filter_custom_fields', 'rlreport_course_progress_summary'), 'block_php_report');
     $filter_entries = array();
     $this->curricula = new generalized_filter_entry('curr', 'curcrs', 'curriculumid', get_string('filter_curricula', 'rlreport_course_progress_summary'), false, 'selectany', $curricula_options);
     $filter_entries[] = $this->curricula;
     $filter_entries[] = new generalized_filter_entry('cluster', 'enrol', 'userid', get_string('filter_cluster', 'rlreport_course_progress_summary'), false, 'clusterselect', array('default' => null));
     $this->multiselect_filter = new generalized_filter_entry('field' . $this->id, 'field' . $this->id, 'id', get_string('filter_field', 'rlreport_course_progress_summary'), false, 'custom_field_multiselect_values', $field_list);
     $filter_entries[] = $this->multiselect_filter;
     $filter_entries[] = new generalized_filter_entry('enrol', 'enrol', 'enrolmenttime', get_string('filter_course_date', 'rlreport_course_progress_summary'), false, 'date');
     $filter_entries[] = new generalized_filter_entry('enrol', 'enrol', 'enrolmenttime', get_string('filter_course_date', 'rlreport_course_progress_summary'), false, 'date');
     return $filter_entries;
 }
Exemple #12
0
 /**
  * Adds controls specific to this filter in the form.
  * @param object $mform a MoodleForm object to setup
  */
 function setupForm(&$mform)
 {
     global $USER, $CFG;
     /**
      * CSS includes
      */
     $mform->addElement('html', '<style>@import url("' . $CFG->wwwroot . '/lib/yui/treeview/assets/skins/sam/treeview-skin.css");</style>');
     /**(use "git add" and/or "git commit -a")
      * JavaScript includes
      */
     //include the necessary javascript libraries for the YUI TreeView
     require_js(array('yui_yahoo', 'yui_dom', 'yui_event', 'yui_treeview'));
     //for converting tree representation
     require_js('yui_json');
     //for asynch request dynamic loading
     require_js('yui_connection');
     //include our custom code that handles the YUI Treeview menu
     require_js($CFG->wwwroot . '/curriculum/js/clustertree.js');
     /**
      * Get set up necessary CSS classes
      */
     $manageclusters_css_class = block_curr_admin_get_item_css_class('manageclusters');
     $cluster_css_class = block_curr_admin_get_item_css_class('cluster_instance');
     //figure out which capability to check
     if ($this->execution_mode == php_report::EXECUTION_MODE_SCHEDULED) {
         $capability = 'block/php_report:schedule';
     } else {
         $capability = 'block/php_report:view';
     }
     $context_result = cm_context_set::for_user_with_capability('cluster', $capability, $USER->id);
     $extrafilters = array('contexts' => $context_result, 'parent' => 0);
     $num_records = cluster_count_records('', '', $extrafilters);
     /**
      * TreeView-related work
      */
     //CM entities for placement at the top of the menu
     $cm_entity_pages = array();
     $cm_entity_pages[] = new menuitem('root');
     if ($clusters = cluster_get_listing('priority, name', 'ASC', 0, 0, '', '', array('parent' => 0))) {
         foreach ($clusters as $cluster) {
             $params = array('id' => $cluster->id, 'action' => 'viewreport', 'execution_mode' => $this->execution_mode);
             $cluster_count = cluster_count_records('', '', array('parent' => $cluster->id));
             $isLeaf = empty($cluster_count);
             $cm_entity_pages[] = test_cluster_tree_get_menu_item('cluster', $cluster, 'root', $manageclusters_css_class, $cluster->id, 0, $params, $isLeaf);
         }
     }
     $menuitemlisting = new menuitemlisting($cm_entity_pages);
     $tree = new checkbox_treerepresentation($menuitemlisting, $this->options['report_id']);
     $tree_html = $tree->convert_to_markup($this->_uniqueid, $this->execution_mode);
     $params = array($this->options['report_id'], $this->_uniqueid, $this->options['dropdown_button_text'], $this->options['tree_button_text']);
     $param_string = implode('", "', $params);
     /**
      * UI element setup
      */
     require_once $CFG->dirroot . '/curriculum/lib/filtering/equalityselect.php';
     $choices_array = array(0 => get_string('anyvalue', 'filters'));
     //set up cluster listing
     if ($records = cluster_get_listing('name', 'ASC', 0, 0, '', '', array('contexts' => $context_result))) {
         foreach ($records as $record) {
             if ($record->parent == 0) {
                 //merge in child clusters
                 $choices_array[$record->id] = $record->name;
                 $child_array = $this->find_child_clusters($records, $record->id);
                 $choices_array = $this->merge_array_keep_keys($choices_array, $child_array);
             }
         }
     }
     //get help text
     if (isset($this->options['help'])) {
         $this->_filterhelp = $this->options['help'];
     } else {
         $this->_filterhelp = null;
     }
     //add filterhelp and label to this filter
     //import required css for the fieldset
     $style = '<style>@import url("' . $CFG->wwwroot . '/curriculum/styles.css");</style>';
     //hack the nested fieldset into an html element
     $helptext = get_string('helpprefix2', $this->_filterhelp['1']) . ' (' . get_string('newwindow') . ')';
     $helpurl = '/help.php?module=' . $this->_filterhelp['2'] . '&amp;file=' . $this->_filterhelp['0'] . '.html&amp;forcelang=';
     $helplink = '<span class="helplink"><a title="' . $this->_filterhelp['1'] . '" href="' . $CFG->wwwroot . $helpurl . '"' . ' onclick="this.target=\'popup\'; return openpopup(\'' . $helpurl . '\', \'popup\', \'menubar=0,location=0,scrollbars,resizable,width=500,height=400\', 0);">' . ' <img class="iconhelp" alt="' . $helptext . '" src="' . $CFG->pixpath . '/help.gif"></a></span>';
     $nested_fieldset = false;
     $title = '';
     if ($this->options['fieldset']) {
         $nested_fieldset = '<fieldset class="nested clearfix" id="' . $this->_uniqueid . '_label' . '">' . "\n" . '<legend class="ftoggler">' . $this->_label . $helplink . '</legend>' . "\n";
     } else {
         $title = $this->_label . $helplink . '&nbsp;';
     }
     $mform->addElement('html', $style . $nested_fieldset);
     //cluster select dropdown
     $mform->addElement('select', $this->_uniqueid . '_dropdown', $title, $choices_array);
     //dropdown / cluster tree state storage
     $mform->addElement('hidden', $this->_uniqueid . '_usingdropdown');
     //default to showing dropdown if nothing has been persisted
     $report_shortname = $this->options['report_shortname'];
     $preferences = php_report_filtering_get_user_preferences($report_shortname);
     if (!isset($preferences["php_report_{$report_shortname}/{$this->_uniqueid}_usingdropdown"])) {
         $mform->setDefault($this->_uniqueid . '_usingdropdown', 1);
     }
     // dress it up like an mform element
     $tree_html = '<div class="fitem"><div class="fitemtitle"></div>' . $tree_html . '</div>';
     //cluster tree
     $mform->addElement('html', $tree_html);
     //list of explicitly selected elements
     $mform->addElement('hidden', $this->_uniqueid . '_listing');
     //list of selected and unexpanded elements
     $mform->addElement('hidden', $this->_uniqueid . '_unexpanded');
     //list of explicitly unselected elements
     $mform->addElement('hidden', $this->_uniqueid . '_clrunexpanded');
     /**
      * Work needed to initialize the state of necessary components
      */
     //parameters needed
     $params = array($this->options['report_id'], $this->_uniqueid, $this->options['dropdown_button_text'], $this->options['tree_button_text']);
     $param_string = implode('", "', $params);
     $mform->addElement('button', $this->_uniqueid . '_toggle', $this->options['dropdown_button_text'], array('onclick' => 'clustertree_toggle_tree("' . $param_string . '")'));
     //script to do the work
     $initialize_state_script = '<script type="text/javascript">
                                 clustertree_set_toggle_state("' . $param_string . '");
                                 </script>';
     $mform->addElement('html', $initialize_state_script);
     // close hacked nested fieldset
     if ($this->options['fieldset']) {
         $mform->addElement('html', '</fieldset>');
     }
 }