Ejemplo n.º 1
0
 /**
  * Converts substrings matching the format of :property to regular expression wildcards
  * @param type $url
  * @return boolean
  */
 public function matches($url)
 {
     $pattern = $this->pattern;
     // get keys
     preg_match_all("#:([a-zA-Z0-9]+)#", $pattern, $keys);
     if (sizeof($keys) && sizeof($keys[0]) && sizeof($keys[1])) {
         $keys = $keys[1];
     } else {
         // no keys in the pattern, return a simple match
         return preg_match("#^{$pattern}\$#", $url);
     }
     // normalize route pattern
     $pattern = preg_replace("#(:[a-zA-Z0-9]+)#", "([a-zA-Z0-9-+_ ]+)", $pattern);
     // check values
     preg_match_all("#^{$pattern}\$#", $url, $values);
     if (sizeof($values) && sizeof($values[0]) && sizeof($values[1])) {
         // unset the matched url
         unset($values[0]);
         // values found, modify parameters and return
         $derived = array_combine($keys, ArrayMethods::flatten($values));
         $this->parameters = array_merge($this->parameters, $derived);
         return true;
     }
     return false;
 }
Ejemplo n.º 2
0
 public static function findFamily($user)
 {
     $families = \Family::all(["user_id = ?" => $user->id], ["member_id", "relation"]);
     $fs = array();
     foreach ($families as $f) {
         $u = \User::first(["id = ?" => $f->member_id], ["name"]);
         $fs[(int) $f->member_id] = ArrayMethods::toObject(["name" => $u->name, "user_id" => $f->member_id, "relation" => $f->relation]);
     }
     return $fs;
 }
Ejemplo n.º 3
0
 public function all($assignments, $courses)
 {
     $user = Registry::get("session")->get("user");
     $sub = Registry::get("MongoDB")->submission;
     $submissions = $sub->find(array("user_id" => (int) $user));
     $result = array();
     foreach ($assignments as $a) {
         $course = $courses[$a->course_id];
         $submit = $this->_submission($submissions, $a);
         $data = array("title" => $a->title, "description" => $a->description, "deadline" => $a->deadline, "id" => $a->id, "course" => $course->title, "submitted" => $submit["submission"], "filename" => $a->attachment ? $a->attachment : null, "marks" => $submit["grade"], "remarks" => $submit["remarks"], "status" => $submit["status"]);
         $data = ArrayMethods::toObject($data);
         $result[] = $data;
     }
     return $result;
 }
Ejemplo n.º 4
0
 /**
  * Get a configuration item.
  * If no item is requested, the entire configuration array will be returned.
  * @param  string  $key
  * @param  mixed   $default
  * @return array
  */
 public static function get($key, $default = null)
 {
     if (empty($key)) {
         throw new \Exception("\$key argument is not valid", 500);
     }
     list($file, $item) = static::_parse($key);
     if (file_exists(path('app') . 'configuration/' . $file . EXT)) {
         $items = (include path('app') . 'configuration/' . $file . EXT);
     } else {
         throw new \Exception("{$file} configuration file is missing", 500);
     }
     if (is_null($item)) {
         return $items;
     } else {
         return ArrayMethods::array_get($items, $item);
     }
 }
Ejemplo n.º 5
0
 /**
  * @before _secure, _school
  */
 public function index()
 {
     $this->setSEO(array("title" => "Admin | School | Dashboard"));
     $view = $this->getActionView();
     $counts = array();
     $counts["students"] = Scholar::count(array("organization_id = ?" => $this->organization->id));
     $counts["teachers"] = Educator::count(array("organization_id = ?" => $this->organization->id));
     $counts["classes"] = Grade::count(array("organization_id = ?" => $this->organization->id));
     $counts = ArrayMethods::toObject($counts);
     $session = Registry::get("session");
     $message = $session->get("redirectMessage");
     if ($message) {
         $view->set("message", $message);
         $session->erase("redirectMessage");
     }
     $view->set("counts", $counts);
 }
Ejemplo n.º 6
0
 public function cart()
 {
     $this->seo(array("title" => "Cart", "view" => $this->getLayoutView()));
     $view = $this->getActionView();
     $session = Registry::get("session");
     $cart_items = $session->get('User\\Cart:$cart', array());
     $result = $cart_items;
     if (count($cart_items) == 0 && $this->user) {
         $result = Cart::all(array("user_id = ?" => $this->user->id));
     } elseif (count($cart_items) > 0) {
         $result = array();
         foreach ($cart_items as $c) {
             $result[] = ArrayMethods::toObject($c);
         }
     }
     $view->set("items", $result);
 }
Ejemplo n.º 7
0
 /**
  * Adds New AD Categories by checking if that category already exists in 
  * the database to prevent duplicate
  */
 public static function addNew(&$categories, $org, $newCat = [])
 {
     $result = [];
     ArrayMethods::copy($categories, $result);
     $cat = RequestMethods::post("category") ?? $newCat;
     foreach ($cat as $c) {
         $found = self::first(['name' => strtolower($c), 'org_id' => $org->_id], ['_id', 'name']);
         // remove those which are found
         if ($found) {
             unset($categories[$found->getMongoID()]);
             continue;
         }
         $category = new self(['name' => $c, 'org_id' => $org->_id]);
         $category->save();
         $result[$category->_id] = $category;
     }
     return $result;
 }
Ejemplo n.º 8
0
 public function matches($url)
 {
     $pattern = $this->_pattern;
     preg_match_all("#:([a-zA-Z0-9]+)#", $pattern, $keys);
     if (count($keys) && count($keys[0]) && count($keys[1])) {
         $keys = $keys[1];
     } else {
         return preg_match("#^{$pattern}\$#", $url);
     }
     $pattern = preg_replace("#(:[a-zA-Z0-9]+)#", "([a-zA-Z0-9-_]+)", $pattern);
     preg_match_all("#^{$pattern}\$#", $url, $values);
     if (count($values) && count($values[0]) && count($values[1])) {
         unset($values[0]);
         $derived = array_combine($keys, ArrayMethods::flatten($values));
         $this->_parameters = array_merge($this->_parameters, $derived);
         return true;
     }
     return false;
 }
Ejemplo n.º 9
0
 /**
  * @before _secure, _school
  */
 public function enrollments($classroom_id, $grade_id)
 {
     $classroom = \Classroom::first(array("id = ?" => $classroom_id), array("id", "organization_id", "grade_id", "educator_id"));
     if (!$classroom || $classroom->organization_id != $this->organization->id || $classroom->grade_id != $grade_id) {
         self::redirect("/school");
     }
     $this->setSEO(array("title" => "School | View students in section"));
     $view = $this->getActionView();
     $enrollments = \Enrollment::all(array("classroom_id = ?" => $classroom_id));
     $students = array();
     foreach ($enrollments as $e) {
         $student = \Scholar::first(array("id = ?" => $e->scholar_id), array("user_id", "dob", "parent_id"));
         $parent = \StudentParent::first(array("id = ?" => $student->parent_id), array("name", "relation"));
         $usr = \User::first(array("id = ?" => $student->user_id));
         $students[] = array("name" => $usr->name, "parent_name" => $parent->name, "parent_relation" => $parent->relation, "dob" => $student->dob, "username" => $usr->username);
     }
     $students = ArrayMethods::toObject($students);
     $view->set("students", $students);
 }
Ejemplo n.º 10
0
 /**
  * Parse files from ini and include them
  * 
  * @param type $path
  * @return type
  * @throws Exception\Argument
  * @throws Exception\Syntax
  */
 public function parse($path)
 {
     if (empty($path)) {
         throw new Exception\Argument("\$path argument is not valid");
     }
     if (!isset($this->_parsed[$path])) {
         $config = array();
         ob_start();
         include "{$path}.ini";
         $string = ob_get_contents();
         ob_end_clean();
         $pairs = parse_ini_string($string);
         if ($pairs == false) {
             throw new Exception\Syntax("Could not parse Configuration file");
         }
         foreach ($pairs as $key => $value) {
             $config = $this->_pair($config, $key, $value);
         }
         $this->_parsed[$path] = ArrayMethods::toObject($config);
     }
     return $this->_parsed[$path];
 }
Ejemplo n.º 11
0
 /**
  * @before _secure, memberLayout
  */
 public function stats($keyword_id)
 {
     $keyword = \Keyword::first(array("id = ?" => $keyword_id, "serp = ?" => false), array("link", "user_id", "id"));
     $this->_authority($keyword);
     Shared\Service\Social::record($keyword);
     $end_date = RequestMethods::get("enddate", date("Y-m-d"));
     $start_date = RequestMethods::get("startdate", date("Y-m-d", strtotime($end_date . "-7 day")));
     $social_media = RequestMethods::get("media", "facebook");
     $this->seo(array("title" => "Serp | Stats", "view" => $this->getLayoutView()));
     $view = $this->getActionView();
     $socials = Registry::get("MongoDB")->socials;
     $start_time = strtotime($start_date);
     $end_time = strtotime($end_date);
     $obj = array();
     $records = $socials->find(array('created' => array('$gte' => new MongoDate($start_time), '$lte' => new MongoDate($end_time)), 'social_media' => (string) $social_media, 'keyword_id' => (int) $keyword->id));
     foreach ($records as $r) {
         $position = $r['count'];
         $media = array();
         $media['count_type'] = $r['count_type'];
         $media['social_media'] = $r['social_media'];
         $obj[] = array('y' => date('Y-m-d', $r['created']->sec), 'a' => $position);
     }
     $view->set("keyword", $keyword)->set("label", $media['count_type'])->set("social", array("type" => $media['count_type'], "media" => $media['social_media']))->set("data", ArrayMethods::toObject($obj));
 }
Ejemplo n.º 12
0
 public function enrollments($classroom, $opts = array())
 {
     $enrollments = \Enrollment::all(array("classroom_id = ?" => $classroom->id), array("user_id"));
     $students = array();
     foreach ($enrollments as $e) {
         $usr = \User::first(array("id = ?" => $e->user_id), array("name", "username"));
         if (!isset($opts['only_user'])) {
             $scholar = \Scholar::first(array("user_id = ?" => $e->user_id), array("roll_no"));
         }
         $extra = $this->_extraFields($e, $opts);
         if (isset($opts['conversation'])) {
             $extra = array('username' => $usr->username, 'class' => $classroom->grade, 'section' => $classroom->section, 'display' => $usr->name . " (Class: " . $classroom->grade . " - " . $classroom->section . ") Roll No: " . $scholar->roll_no);
         }
         if (!isset($opts['only_user'])) {
             $data = array("user_id" => $e->user_id, "name" => $usr->name, "roll_no" => $scholar->roll_no);
         } else {
             $data = array("user_id" => $e->user_id);
         }
         $data = array_merge($data, $extra);
         $data = ArrayMethods::toObject($data);
         $students[] = $data;
     }
     return $students;
 }
Ejemplo n.º 13
0
 /**
  * See stats of a keyword
  * @before _secure, memberLayout
  */
 public function stats($keyword_id)
 {
     $keyword = \Keyword::first(array("id = ?" => $keyword_id, "serp = ?" => true));
     $this->_authority($keyword);
     $rank = Registry::get("MongoDB")->rank;
     $r = $rank->findOne(['keyword_id' => (int) $keyword->id, 'created' => date('Y-m-d')]);
     if ($keyword->live && !$r) {
         try {
             Shared\Service\Serp::record(array($keyword));
         } catch (\Exception $e) {
         }
     }
     $end_date = RequestMethods::get("enddate", date("Y-m-d"));
     $start_date = RequestMethods::get("startdate", date("Y-m-d", strtotime($end_date . "-7 day")));
     $this->seo(array("title" => "Serp | Stats", "view" => $this->getLayoutView()));
     $view = $this->getActionView();
     $start_time = strtotime($start_date);
     $end_time = strtotime($end_date);
     $i = 0;
     $obj = array();
     while ($start_time < $end_time) {
         $start_time = strtotime($start_date . " +{$i} day");
         $date = date('Y-m-d', $start_time);
         $record = $rank->findOne(array('created' => $date, 'keyword_id' => (int) $keyword->id));
         if (isset($record)) {
             $position = $record['position'];
         } else {
             $position = 0;
         }
         $obj[] = array('y' => $date, 'a' => $position);
         ++$i;
     }
     $view->set("keyword", $keyword)->set("label", "Rank")->set("data", ArrayMethods::toObject($obj));
 }
Ejemplo n.º 14
0
 /**
  * @before _secure, _admin
  */
 public function dataAnalysis()
 {
     $this->seo(array("title" => "Data Analysis", "keywords" => "admin", "description" => "admin", "view" => $this->getLayoutView()));
     $view = $this->getActionView();
     if (RequestMethods::get("action") == "dataAnalysis") {
         $startdate = RequestMethods::get("startdate");
         $enddate = RequestMethods::get("enddate");
         $model = ucfirst(RequestMethods::get("model"));
         $diff = date_diff(date_create($startdate), date_create($enddate));
         for ($i = 0; $i < $diff->format("%a"); $i++) {
             $date = date('Y-m-d', strtotime($startdate . " +{$i} day"));
             $count = $model::count(array("created LIKE ?" => "%{$date}%"));
             $obj[] = array('y' => $date, 'a' => $count);
         }
         $view->set("data", \Framework\ArrayMethods::toObject($obj));
     }
 }
Ejemplo n.º 15
0
 /**
  * @before _secure, _vendor
  */
 public function create()
 {
     $this->seo(array("title" => "Create runner", "view" => $this->getLayoutView()));
     $view = $this->getActionView();
     $msg = "";
     $centres = Centre::all(array("organization_id = ?" => $this->organization->id), array("id", "location_id"));
     $upload = function ($key) {
         if (isset($_FILES["image"]["name"][$key])) {
             $file = $_FILES["image"];
             $path = APP_PATH . "/public/assets/uploads/images/";
             $extension = pathinfo($file["name"][$key], PATHINFO_EXTENSION);
             if (!preg_match("/^(jpe?g|gif|png|bmp)\$/", $extension)) {
                 return false;
             }
             $filename = uniqid() . ".{$extension}";
             if (move_uploaded_file($file["tmp_name"][$key], $path . $filename)) {
                 return $filename;
             } else {
                 return FALSE;
             }
         } else {
             return false;
         }
     };
     if (RequestMethods::post("phone")) {
         $phone = RequestMethods::post("phone");
         $name = RequestMethods::post("name");
         $centre_id = RequestMethods::post("centre_id");
         foreach ($phone as $key => $value) {
             if (!empty($value)) {
                 $exist = User::first(array("phone = ?" => $phone[$key]), array("id"));
                 $img = $upload($key);
                 if (!$img) {
                     $msg = 'Not a vaild image';
                 } else {
                     if ($exist) {
                         $msg = 'Phone number already exists';
                     }
                 }
                 if (!$exist && $img) {
                     $user = new User(array("name" => $name[$key], "email" => "", "phone" => $phone[$key], "password" => sha1(rand(100000, 9999999)), "gender" => $gender[$key], "birthday" => "", "live" => true));
                     $user->save();
                     foreach ($centre_id[$key] as $k => $v) {
                         $runner = new Member(array("user_id" => $user->id, "organization_id" => $this->organization->id, "centre_id" => $v, "designation" => "runner", "image" => $img, "live" => true));
                         $runner->save();
                     }
                     $msg = "Runner Created Successfully";
                 } else {
                     $msg .= ", Not all were added";
                 }
             }
         }
         $view->set("message", $msg);
     }
     $locations = array();
     foreach ($centres as $c) {
         $l = Location::first(array("id = ?" => $c->location_id), array("area_id"));
         $a = Area::first(array("id = ?" => $l->area_id), array("name", "id"));
         $data = array("id" => $c->id, "name" => $a->name);
         $data = ArrayMethods::toObject($data);
         $locations[$c->id] = $data;
     }
     $view->set("centres", $locations);
 }
Ejemplo n.º 16
0
 /**
  * Find all the courses to which the teacher is assigned
  * @before _secure, _teacher
  */
 public function courses()
 {
     $this->setSEO(array("title" => "Manage Your Courses | Teacher"));
     $view = $this->getActionView();
     $teaches = \Teach::all(array("user_id = ?" => $this->user->id, "live = ?" => true));
     $grades = \Grade::all(array("organization_id = ?" => $this->organization->id), array("id", "title"));
     $storedGrades = array();
     foreach ($grades as $g) {
         $storedGrades[$g->id] = $g;
     }
     $courses = TeacherService::$_courses;
     $classes = TeacherService::$_classes;
     $result = array();
     foreach ($teaches as $t) {
         $grade = $storedGrades[$t->grade_id];
         $class = $classes[$t->classroom_id];
         $course = $courses[$t->course_id];
         $asgmnt = \Assignment::count(array("course_id = ?" => $t->course_id));
         $data = array("grade" => $grade->title, "grade_id" => $g->id, "section" => $class->section, "course" => $course->title, "course_id" => $course->id, "classroom_id" => $class->id, "assignments" => $asgmnt);
         $data = ArrayMethods::toObject($data);
         $result[] = $data;
     }
     $session = Registry::get("session");
     if ($session->get('Notification\\Students:$sent')) {
         $view->set("success", "Notification sent to students");
         $session->erase('Notification\\Students:$sent');
     }
     $view->set("courses", $result);
 }
Ejemplo n.º 17
0
 /**
  * @before _secure, _student
  */
 public function courses()
 {
     $this->setSEO(array("title" => "Result | Student"));
     $view = $this->getActionView();
     $session = Registry::get("session");
     $courses = StudentService::$_courses;
     $result = array();
     $sub = Registry::get("MongoDB")->submission;
     foreach ($courses as $c) {
         $a = Assignment::count(array("course_id = ?" => $c->id));
         $s = $sub->count(array("course_id" => (int) $c->id, "user_id" => (int) $this->user->id));
         $data = array("_title" => $c->title, "_description" => $c->description, "_grade_id" => $c->grade_id, "_id" => $c->id, "_assignments" => $a, "_assignment_submitted" => $s);
         $data = ArrayMethods::toObject($data);
         $result[] = $data;
     }
     $view->set("courses", $result);
 }
Ejemplo n.º 18
0
 protected static function _getStats($records, &$stats, $date)
 {
     $keys = ['country', 'os', 'device', 'referer'];
     foreach ($records as $r) {
         $obj = Utils::toArray($r);
         $arr =& $stats[$date]['meta'];
         foreach ($keys as $k) {
             if (!isset($arr[$k])) {
                 $arr[$k] = [];
             }
             $index = $r['_id'][$k] ?? null;
             if (is_null($index)) {
                 continue;
             }
             if (strlen(trim($index)) === 0) {
                 $index = "Empty";
             }
             ArrayMethods::counter($arr[$k], $index, $obj['count']);
         }
     }
 }
Ejemplo n.º 19
0
 /**
  * @before _secure, memberLayout
  */
 public function logs($website_id)
 {
     $this->seo(array("title" => "Logs for your website", "view" => $this->getLayoutView()));
     $view = $this->getActionView();
     $this->getLayoutView()->set("logs", true);
     $mongo = Registry::get("MongoDB");
     $website = $mongo->website;
     $record = $website->findOne(array("website_id" => (int) $website_id));
     if (!$record || $record['user_id'] != $this->user->id) {
         $this->redirect("/404");
     }
     $this->_clearLogs($website_id);
     $logs = $mongo->logs;
     $where = array('website_id' => (int) $website_id);
     $page = RequestMethods::get("page", 1);
     $limit = RequestMethods::get("limit", 30);
     $count = $logs->count($where);
     $cursor = $logs->find($where, array('_id' => false));
     $cursor->skip($limit * ($page - 1));
     $cursor->limit($limit);
     $cursor->sort(array('created' => -1));
     $results = array();
     foreach ($cursor as $c) {
         $c = ArrayMethods::toObject($c);
         $results[] = $c;
     }
     $view->set("logs", $results)->set("page", $page)->set("limit", $limit)->set("actions", $this->actions)->set("ts", $this->triggers)->set("count", $count);
 }
Ejemplo n.º 20
0
 public static function overall($dateQuery = [], $user = null)
 {
     $q = [];
     $clicks = [];
     $conversions = [];
     $impressions = [];
     $payouts = [];
     $total_clicks = 0;
     $total_conversions = 0;
     $total_payouts = 0;
     $total_impressions = 0;
     if (is_array($user)) {
         $in = ArrayMethods::arrayKeys($user, '_id');
         $q["user_id"] = ['$in' => $in];
     } elseif ($user) {
         $q["user_id"] = $user->id;
     }
     if (count($dateQuery) > 0) {
         $q["created"] = ['$gte' => $dateQuery['start'], '$lte' => $dateQuery['end']];
     }
     $performances = self::all($q, ['revenue', 'clicks', 'created', 'impressions', 'conversions']);
     foreach ($performances as $p) {
         //calculating datewise
         $date = $p->created->format('Y-m-d');
         $total_clicks += $p->clicks;
         ArrayMethods::counter($clicks, $date, $p->clicks);
         $total_conversions += $p->conversions;
         ArrayMethods::counter($conversions, $date, $p->conversions);
         $total_impressions += $p->impressions;
         ArrayMethods::counter($impressions, $date, $p->impressions);
         $total_payouts += $p->revenue;
         ArrayMethods::counter($payouts, $date, $p->revenue);
     }
     ksort($clicks);
     ksort($impressions);
     ksort($payouts);
     $clicks = Utils::dateArray($clicks);
     $impressions = Utils::dateArray($impressions);
     $payouts = Utils::dateArray($payouts);
     return ["impressions" => $impressions, "total_impressions" => $total_impressions, "clicks" => $clicks, "total_clicks" => $total_clicks, "conversions" => $conversions, "total_conversions" => $total_conversions, "payouts" => $payouts, "total_payouts" => $total_payouts];
 }
Ejemplo n.º 21
0
 protected function _parse($comment)
 {
     $meta = array();
     $pattern = "(@[a-zA-Z]+\\s*[a-zA-Z0-9, ()_]*)";
     $matches = StringMethods::match($comment, $pattern);
     if ($matches != null) {
         foreach ($matches as $match) {
             $parts = ArrayMethods::clean(ArrayMethods::trim(StringMethods::split($match, "[\\s]", 2)));
             $meta[$parts[0]] = true;
             if (sizeof($parts) > 1) {
                 $meta[$parts[0]] = ArrayMethods::clean(ArrayMethods::trim(StringMethods::split($parts[1], ",")));
             }
         }
     }
     return $meta;
 }
Ejemplo n.º 22
0
 private function _findMarks($opts = array())
 {
     $setCourses = $opts["setCourses"];
     $exams = $opts["exams"];
     $results = array();
     $setResults = array();
     foreach ($exams as $e) {
         $result = ExamResult::first(array("exam_id = ?" => $e->id, "user_id = ?" => $opts["user_id"]));
         $setResults["{$result->id}"] = $result;
         $data = array("course" => $setCourses["{$e->course_id}"], "marks" => $result->marks, "course_id" => $e->course_id, "result_id" => $result->id);
         $data = ArrayMethods::toObject($data);
         $results[] = $data;
     }
     return array("results" => $results, "setResults" => $setResults);
 }
Ejemplo n.º 23
0
 private static function makeObject($k)
 {
     return ArrayMethods::toObject(["keyword_id" => (int) $k->id, "user_id" => (int) $k->user_id, "keyword" => $k->keyword, "link" => $k->link]);
 }
Ejemplo n.º 24
0
 /**
  * @before _secure, memberLayout
  */
 public function stats()
 {
     $this->seo(array("title" => "Ping | Stats", "view" => $this->getLayoutView()));
     $view = $this->getActionView();
     $url = RequestMethods::get("link");
     $ping = Registry::get("MongoDB")->ping;
     $search = array('user_id' => (int) $this->user->id, 'url' => $url);
     $record = $ping->findOne($search);
     if (!$record) {
         $this->redirect("/404");
     }
     $end_date = RequestMethods::get("enddate", date("Y-m-d"));
     $start_date = RequestMethods::get("startdate", date("Y-m-d", strtotime($end_date . "-7 day")));
     $start_time = strtotime($start_date);
     $end_time = strtotime($end_date);
     $ping_stats = Registry::get("MongoDB")->ping_stats;
     $records = $ping_stats->find(array('ping_id' => $record['_id'], 'created' => array('$gte' => new \MongoDate($start_time), '$lte' => new \MongoDate($end_time))));
     $obj = array();
     foreach ($records as $r) {
         $obj[] = array('y' => date('Y-m-d H:i:s', $r['created']->sec), 'a' => $r['latency']);
     }
     $view->set('ping', ArrayMethods::toObject($record))->set('label', 'Latency')->set('data', ArrayMethods::toObject($obj));
 }
Ejemplo n.º 25
0
 public function results($course)
 {
     $exams = \Exam::all(array("course_id = ?" => $course->id), array("year", "type", "id"));
     $result = array();
     foreach ($exams as $e) {
         $whole_class = \ExamResult::all(array("exam_id = ?" => $e->id), array("marks", "user_id"));
         $total = 0;
         $highest = -1;
         $count = 0;
         $user_marks = 0;
         foreach ($whole_class as $w_c) {
             $total += $w_c->marks;
             if ((int) $w_c->marks > $highest) {
                 $highest = (int) $w_c->marks;
             }
             if ($w_c->user_id == self::$_student->user_id) {
                 $user_marks = (int) $w_c->marks;
             }
             ++$count;
         }
         $data = array("type" => $e->type, "year" => $e->year, "exam_id" => $e->id, "marks" => $user_marks, "highest" => $highest, "average" => $total / $count);
         $data = ArrayMethods::toObject($data);
         $result[] = $data;
     }
     return $result;
 }
Ejemplo n.º 26
0
 /**
  * Loop through template sections and organize them into a hierarchy
  * and assign addition meta data to any tags
  * @param  array $array Array of template segments
  * @return array
  */
 protected function _tree($array)
 {
     $root = array('children' => array());
     $current =& $root;
     foreach ($array as $i => $node) {
         $result = $this->_tag($node);
         if ($result) {
             if (isset($result['tag'])) {
                 $tag = $result['tag'];
             } else {
                 $tag = "";
             }
             if (isset($result['arguments'])) {
                 $arguments = $result['arguments'];
             } else {
                 $arguments = "";
             }
             if ($tag) {
                 // If segment does not contain a closer
                 if (!$result['closer']) {
                     // clean up syntax if segment is isolated and
                     // preceded by an plain text segment
                     $last = ArrayMethods::last($current['children']);
                     if ($result['isolated'] && is_string($last)) {
                         array_pop($current['children']);
                     }
                     $current['children'][] = array('index' => $i, 'parent' => &$current, 'children' => array(), 'raw' => $result['source'], 'tag' => $tag, 'arguments' => $arguments, 'delimiter' => $result['delimiter'], 'number' => count($current['children']));
                     $current =& $current['children'][count($current['children']) - 1];
                 } else {
                     if (isset($current['tag']) && $result['tag'] == $current['tag']) {
                         $start = $current['index'] + 1;
                         $length = $i - $start;
                         $current['source'] = implode(array_slice($array, $start, $length));
                         $current =& $current['parent'];
                     }
                 }
             } else {
                 $current['children'][] = array('index' => $i, 'parent' => &$current, 'children' => array(), 'raw' => $result['source'], 'tag' => $tag, 'arguments' => $arguments, 'delimiter' => $result['delimiter'], 'number' => count($current['children']));
             }
         } else {
             $current['children'][] = $node;
         }
     }
     return $root;
 }
Ejemplo n.º 27
0
 public function first()
 {
     $limit = $this->_limit;
     $offset = $this->_offset;
     $this->limit(1);
     $all = $this->all();
     $first = ArrayMethods::first($all);
     if ($limit) {
         $this->_limit = $limit;
     }
     if ($offset) {
         $this->_offset = $offset;
     }
     return $first;
 }
Ejemplo n.º 28
0
 /**
  * @before _secure
  */
 public function profile()
 {
     $this->seo(array("title" => "Profile", "view" => $this->getLayoutView()));
     $view = $this->getActionView();
     $user = $this->user;
     $view->set("errors", array());
     if (RequestMethods::post("action") == "saveUser") {
         $user->name = RequestMethods::post("name");
         $user->phone = RequestMethods::post("phone");
         $password = RequestMethods::post("password");
         if (!empty($password)) {
             $user->password = sha1($password);
         }
         $user->save();
         $this->setUser($user);
         $view->set("success", "Info updated!!");
     }
     $locations = Location::all(array("user_id = ?" => $this->user->id), array("*"), "created", "desc");
     $family = Shared\Services\Patient::findFamily($this->user);
     $flocations = Shared\Services\Patient::familyLoc($family);
     if (RequestMethods::post("action") == "saveLocation") {
         $location_id = RequestMethods::post("location_id");
         $location = $locations[$location_id];
         $l = \Location::saveRecord(null, $location, array("validate" => true));
         if (is_array($l)) {
             $view->set("success", "Fix the following errors")->set("errors", $l);
         } else {
             $locations[$location_id] = $l;
             $view->set("success", "Updated Location");
         }
     }
     if (RequestMethods::post("addLocation")) {
         $user_id = (int) RequestMethods::post("user_id", $this->user->id);
         if ((int) $this->user->id == $user_id) {
             $u = $this->user;
         } else {
             $u = ArrayMethods::toObject(['id' => $family[$user_id]->user_id]);
         }
         $location = \Location::saveRecord($u, null, array("validate" => true));
         if (is_array($location)) {
             $view->set("success", "Fix the following errors")->set("errors", $location);
         } else {
             $view->set("success", "Added New Location");
             $arr = array($location->id => $location);
             $locations = array_merge($locations, $arr);
         }
     }
     $view->set("locations", $locations)->set("family", $family)->set("flocations", $flocations);
 }
Ejemplo n.º 29
0
 public static function livePerf($user, $s = null, $e = null)
 {
     $start = $end = date('Y-m-d');
     $type = $user->type ?? '';
     if ($s) {
         $start = $s;
     }
     if ($e) {
         $end = $e;
     }
     $perf = new \Performance();
     $match = ['is_bot' => false, 'created' => Db::dateQuery($start, $end)];
     switch ($user->type) {
         case 'publisher':
             $match['pid'] = Db::convertType($user->_id);
             break;
         case 'advertiser':
             $ads = \Ad::all(['user_id' => $user->_id], ['_id']);
             $keys = array_keys($ads);
             $match['adid'] = ['$in' => Db::convertType($keys)];
             break;
         default:
             return $perf;
     }
     $results = $commissions = [];
     $records = self::_livePerfQuery($match);
     foreach ($records as $r) {
         $obj = Utils::toArray($r);
         $adid = Utils::getMongoID($obj['_id']);
         $results[$adid] = $obj;
     }
     $comms = Db::query('Commission', ['ad_id' => ['$in' => array_keys($results)]], ['ad_id', 'rate', 'revenue', 'model', 'coverage']);
     $comms = \Click::classify($comms, 'ad_id');
     foreach ($comms as $adid => $value) {
         $value = array_map(function ($v) {
             $v["ad_id"] = Utils::getMongoID($v["ad_id"]);
             unset($v["_id"]);
             return (object) $v;
         }, Utils::toArray($value));
         $commissions[$adid] = \Commission::filter($value);
     }
     foreach ($results as $adid => $obj) {
         $comms = $commissions[$adid];
         foreach ($obj['countries'] as $value) {
             $country = $value['country'];
             $clicks = $value['count'];
             $commission = \Commission::campaignRate($adid, $comms, $country, ['type' => $type, "{$type}" => $user, 'start' => $start, 'end' => $end, 'commFetched' => true]);
             $updateData = [];
             $earning = \Ad::earning($commission, $clicks);
             AM::copy($earning, $updateData);
             $perf->update($updateData);
         }
     }
     return $perf;
 }
Ejemplo n.º 30
0
 /**
  * @before _secure, _admin
  */
 public function groupMembers($group_id)
 {
     $group = Group::first(array("id = ?" => $group_id));
     if (!$group || $group->name == "All") {
         $this->redirect("/admin");
     }
     $this->seo(array("title" => "Manage Group Members", "view" => $this->getLayoutView()));
     $view = $this->getActionView();
     $count = RequestMethods::get("count", 10);
     $group->users = json_decode($group->users);
     if ($count < count($group->users)) {
         $count = count($group->users) + 1;
     }
     $total = array();
     for ($i = 0; $i < $count; ++$i) {
         $total[] = $i;
     }
     if (RequestMethods::post("action") == "addMembers") {
         unset($_POST["action"]);
         $members = ArrayMethods::reArray($_POST);
         $members = json_encode($members);
         $group->users = $members;
         $group->save();
         $view->set("success", "Members were added for the group: {$group->name}");
     }
     $view->set("group", $group);
     $view->set("count", $total);
 }