/** * Calculates the number of records in a listing as created by track_get_listing * * @param string $namesearch Search string for curriculum name * @param string $alpha Start initial of curriculum name filter * @param int $curriculumid Necessary associated curriculum * @param int $clusterid Necessary associated cluster * @param pm_context_set $contexts Contexts to provide permissions filtering, of null if none * @return int The number of records */ function track_count_records($namesearch = '', $alpha = '', $curriculumid = 0, $parentclusterid = 0, $contexts = null) { global $DB; //$LIKE = $this->_db->sql_compare(); $params = array(); $NAMESEARCH_LIKE = $DB->sql_like('name', ':search_namesearch', FALSE); $ALPHA_LIKE = $DB->sql_like('name', ':search_alpha', FALSE); $where = array('defaulttrack = 0'); if (!empty($namesearch)) { //$where[] = "name $LIKE '%$namesearch%'"; $where[] = $NAMESEARCH_LIKE; $params['search_namesearch'] = "%{$namesearch}%"; } if ($alpha) { //$where[] = "(name $LIKE '$alpha%')"; $where[] = $ALPHA_LIKE; $params['search_alpha'] = "{$alpha}%"; } if ($curriculumid) { //$where[] = "(curid = $curriculumid)"; $where[] = "(curid = :curriculumid)"; $params['curriculumid'] = $curriculumid; } if ($parentclusterid) { $where[] = "(id IN (SELECT trackid FROM {" . clustertrack::TABLE . "}\n WHERE clusterid = :parentclusterid))"; $params['parentclusterid'] = $parentclusterid; } if ($contexts !== null) { /* TODO: not working yet... $filter_object = $contexts->filter_for_context_level('id', 'track'); $where[] = $filter_object->get_sql(); */ $filter_object = $contexts->get_filter('id', 'track'); $filter_sql = $filter_object->get_sql(false, null, SQL_PARAMS_NAMED); if (isset($filter_sql['where'])) { $where[] = $filter_sql['where']; $params = array_merge($params, $filter_sql['where_parameters']); } } $where = implode(' AND ', $where); return $DB->count_records_select(track::TABLE, $where, $params); }
/** * Calculates the number of records in a listing as created by pmclass_get_listing * * @param string $namesearch Search string for course name * @param string $alpha Start initial of course name filter * @param int $id Corresponding courseid, or zero for any * @param boolean $onlyopen If true, only consider classes whose end date has not been passed * @param pm_context_set $contexts Contexts to provide permissions filtering, of null if none * @param int $clusterid Id of a cluster that the class must be assigned to via a track * @return int The number of records */ function pmclass_count_records($namesearch = '', $alpha = '', $id = 0, $onlyopen = false, $contexts = null, $clusterid = 0) { global $DB; $select = 'SELECT COUNT(cls.id) '; $tables = 'FROM {' . pmclass::TABLE . '} cls '; $join = 'LEFT JOIN {' . course::TABLE . '} crs ' . 'ON crs.id = cls.courseid '; //class associated to a particular cluster via a track if (!empty($clusterid)) { $join .= 'JOIN {' . trackassignment::TABLE . '} clstrk ON clstrk.classid = cls.id JOIN {' . clustertrack::TABLE . '} clsttrk ON clsttrk.trackid = clstrk.trackid AND clsttrk.clusterid = ' . $clusterid . ' '; } $where = array(); $params = array(); if (!empty($namesearch)) { $crslike = $DB->sql_like('crs.name', '?', FALSE); $clslike = $DB->sql_like('cls.idnumber', '?', FALSE); $where[] = "(({$crslike}) OR ({$clslike}))"; $params = array_merge($params, array("%{$namesearch}%", "%{$namesearch}%")); } if ($alpha) { $crslike = $DB->sql_like('cls.idnumber', '?', FALSE); $where[] = "({$crslike})"; $params[] = "{$alpha}%"; } if ($id) { $where[] = "(crs.id = {$id})"; } if ($onlyopen) { $curtime = time(); $where[] = "(cls.enddate > {$curtime} OR NOT cls.enddate)"; } if ($contexts !== null) { $filter_object = $contexts->get_filter('id', 'class'); $filter_sql = $filter_object->get_sql(false, 'cls'); if (isset($filter_sql['where'])) { $where[] = $filter_sql['where']; $params = array_merge($params, $filter_sql['where_parameters']); } } if (!empty($where)) { $where = 'WHERE ' . implode(' AND ', $where) . ' '; } else { $where = ''; } $sql = $select . $tables . $join . $where; return $DB->count_records_sql($sql, $params); }