Skip to content
Snippets Groups Projects
Commit 9f5e3a1a authored by xamgore's avatar xamgore
Browse files

Closes #27 !9: Teachers list

parents 73cb6e4d f415ce8d
Branches
Tags
No related merge requests found
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Student_Index extends Controller_Environment_Student
{
public function action_index() {
......@@ -12,10 +12,15 @@ class Controller_Student_Index extends Controller_Environment_Student
$marks[$dis->ID] = Model_Subject::getECTSMark($dis->Rate, $dis->MaxCurrentRate, $exam = NULL);
}
$teachers = $student->getTeachers();
foreach ($teachers as $disID => &$list) {
$list = Controller_Teacher_Index::getShortListOfTeachers($list);
}
$this->twig->set([
'Marks' => $marks,
'Disciplines' => $disciplines,
'Teachers' => $student->getTeachers(),
'Teachers' => $teachers,
'SemesterList' => Model_Semesters::loadAll(),
])->set_filename(static::STUDENT . 'index');
}
......@@ -24,4 +29,4 @@ class Controller_Student_Index extends Controller_Environment_Student
$this->twig->set_filename('settings');
}
}
......@@ -29,8 +29,10 @@ class Controller_Teacher_Index extends Controller_Environment_Teacher
$scientific = $dis->Subtype == Model_CourseWork::SCIENTIFIC;
// todo: Pavel will write an optimized query
if (!$scientific && !$teachers[$dis->ID])
$teachers[$dis->ID] = $dis->getTeachers();
if (!$scientific && !$teachers[$dis->ID]) {
$list = $dis->getTeachers()->groupByUniqueKey('ID');
$teachers[$dis->ID] = $this->getShortListOfTeachers($list, $dis->AuthorID, $this->user->TeacherID);
}
if (isset($dis->GroupNum) && $dis->GroupNum)
$groups[$dis->ID][] = $dis->GroupNum . ' гр.';
}
......@@ -42,8 +44,20 @@ class Controller_Teacher_Index extends Controller_Environment_Teacher
'SemesterList' => Model_Semesters::loadAll(),
])->set_filename('teacher/index');
}
public static function getShortListOfTeachers(&$teachers, $authorID = null, $vipID = null) {
$short = [];
// place vip & author at the beginning
foreach ($teachers as $id => &$teacher) {
if ($id == $authorID || $id == $vipID)
$short += [$id => $teacher];
}
return $short + Arr::shuffle_assoc($teachers);
}
public function action_settings() {
$this->twig->set_filename('settings');
}
}
\ No newline at end of file
}
......@@ -26,11 +26,13 @@
{% elseif Discipline.Subtype == 'scientific_coursework' %}
Науч. рук.
{% else %}
{% for teacher in Teachers[Discipline.ID] %}
{{ Text.abbreviateName(teacher) }}<br>
{% for teacher in Teachers[Discipline.ID] |slice(0, 4) |sortbyfield('LastName') %}
<div>{{ Text.abbreviateName(teacher) }}</div>
{% else %}
{% endfor %}
{% endif %}
</td>
......
......@@ -21,7 +21,7 @@
{% if Discipline.Subtype == 'scientific_coursework' %}
Сотрудники кафедры
{% else %}
{% for teacher in Teachers[Discipline.ID] | slice(0, 7) %}
{% for teacher in Teachers[Discipline.ID] |slice(0, 4) |sortbyfield('LastName') %}
<div>{{ Text.abbreviateName(teacher) }}</div>
{% else %}
......@@ -140,4 +140,4 @@
<h2 style="text-align: center;">В настоящий момент Вы не подписаны ни на одну из существующих дисциплин.</h2>
{% endfor %}
</div>
{% endblock %}
\ No newline at end of file
{% endblock %}
......@@ -171,6 +171,18 @@ abstract class Kohana_Database_Result implements Countable, Iterator, SeekableIt
return $results;
}
public function groupByUniqueKey($key) {
$results = [];
foreach ($this as $row) {
$results[$row[$key]] = $row;
}
$this->rewind();
return $results;
}
/**
* Return the named column from the current row.
*
......
......@@ -111,6 +111,7 @@ class Twig_Environment
$this->addExtension(new Twig_Extension_Core());
$this->addExtension(new Twig_Extension_Escaper($options['autoescape']));
$this->addExtension(new Twig_Extension_Optimizer($options['optimizations']));
$this->addExtension(new Twig_Extension_SortByField());
$this->extensionInitialized = false;
$this->staging = new Twig_Extension_Staging();
}
......
<?php
/**
* User: Victor Häggqvist
* Date: 3/4/15
* Time: 2:07 AM
*
* The base of the filter is borrowed from https://github.com/dannynimmo/craftcms-sortbyfield
*
* I have extended it to also sort array structures
*/
class Twig_Extension_SortByField extends \Twig_Extension {
public function getName() {
return 'sortbyfield';
}
public function getFilters() {
return array(
new \Twig_SimpleFilter('sortbyfield', array($this, 'sortByFieldFilter'))
);
}
/**
* The "sortByField" filter sorts an array of entries (objects or arrays) by the specified field's value
*
* Usage: {% for entry in master.entries|sortbyfield('ordering', 'desc') %}
*/
public function sortByFieldFilter($content, $sort_by = null, $direction = 'asc') {
if (!is_array($content)) {
throw new \InvalidArgumentException('Variable passed to the sortByField filter is not an array');
} elseif ($sort_by === null) {
throw new Exception('No sort by parameter passed to the sortByField filter');
} elseif (!self::isSortable($content[0], $sort_by)) {
throw new Exception('Entries passed to the sortByField filter do not have the field "' . $sort_by . '"');
} else {
// Unfortunately have to suppress warnings here due to __get function
// causing usort to think that the array has been modified:
// usort(): Array was modified by the user comparison function
@usort($content, function ($a, $b) use($sort_by, $direction) {
$flip = ($direction === 'desc') ? -1 : 1;
if (is_array($a))
$a_sort_value = $a[$sort_by];
else
$a_sort_value = $a->$sort_by;
if (is_array($b))
$b_sort_value = $b[$sort_by];
else
$b_sort_value = $b->$sort_by;
if($a_sort_value == $b_sort_value) {
return 0;
} else if($a_sort_value > $b_sort_value) {
return (1 * $flip);
} else {
return (-1 * $flip);
}
});
}
return $content;
}
/**
* Validate the passed $item to check if it can be sorted
* @param $item mixed Collection item to be sorted
* @param $field string
* @return bool If collection item can be sorted
*/
private static function isSortable($item, $field) {
if (is_array($item))
return array_key_exists($field, $item);
elseif (is_object($item))
return property_exists($item, $field);
else
return false;
}
}
......@@ -10,7 +10,22 @@
*/
class Kohana_Arr {
/**
public static function shuffle_assoc($list) {
if (!is_array($list))
return $list;
$keys = array_keys($list);
shuffle($keys);
$random = [];
foreach ($keys as &$key) {
$random[$key] = $list[$key];
}
return $random;
}
/**
* @var string default delimiter for path()
*/
public static $delimiter = '.';
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment