All pastes #767969 Raw Edit

Unnamed

public php v1 · immutable
·published 2007-11-10 04:20 UTC
rendered paste body
<?php	class UsersController extends AppController {		var $name = 'Users';		var $uses = array('User','Host','LoginAttempt','SectionsUser');		var $helpers = array('Javascript', 'auth', 'Pagination');		var $components = array('auth', 'Pagination');		function login($username = null, $password = null) {			$error = '';						if (!empty($this->data)) {				$data = $this->data;			} else if (!empty($username) and !empty($password)) {				$data = array('User' => array('username' => urldecode($username), 'password' => urldecode($password)));			}						if (!empty($data)) {				if ($user_id = $this->auth->attemptLogin($data)) {					//$error = 'Logged in!';					$this->redirect($this->pglms->defaultPage($user_id));				} else {					$error = $this->auth->error;					if (!$error) $error = 'Internal error.';				}			} else if (!empty($this->uid)) {				//$error = 'Already logged in!';				// Shouldn't happen, but does anyway.				$this->redirect($this->pglms->defaultPage($this->uid));			}			$this->auth->newSalt();			$this->set('error', $error);			$this->set('allowcleartext', $this->auth->allowCleartext);			$this->set('onload', "this.document.pForm['data[User][username]'].focus()");			$this->pageTitle = 'User Login';		}				function token() {			// Takes various parameters, to generate a single-signon token url:			// 'u': Client-side user uid			// 'c': Section identifier			// 'p': School identifier			// 'f': User first name			// 'l': User last name			// 'e': User email address			// 'r': User role			// 'i': Course identifier			// Also looks at server-side variables to find the school id:			// 'PG_SSO-SystemID': eCollege identifier			// 'PG_SSO-MAC': Message Authentication Code			// 'PG_SSO-Timestamp': ISO8601-format timestamp						// Possible error codes returned:			// 100: Unknown error			// 101: Missing a required parameter			// 102: Unauthorized access (a header was missing or wrong)			// 103: Unable to create user			// 104: Invalid parameter value			// 105: Invalid timestamp			// 106: Unsupported language course			// 107: Unknown school			// 108: Invalid section						$params = $this->params['url'];			$messages = array();			$param_vars = array(				'u' => 'user_uid',      // user				'c' => 'section_uid',   // section				'p' => 'school_uid',    // school				//'s' => 'user_school',				'f' => 'first_name',    // first				'l' => 'last_name',     // last				'e' => 'email',				'r' => 'user_type',     // role				'i' => 'course_uid',    // course				'page' => 'next_page',			);						$header_vars = array(				'PG_SSO-SystemID' => 'system_school',				'PG_SSO-MAC' => 'message_hash',				'PG_SSO-Timestamp' => 'timestamp',			);						foreach ($param_vars as $key => $varname) {				$$varname = array_key_exists($key, $params)? $params[$key]: null;			}						foreach ($header_vars as $key => $varname) {				$ukey = 'HTTP_'.str_replace('-', '_', strtoupper($key));				if (empty($_SERVER[$ukey])) {					$messages[102] = 'Authentication headers not provided';					$$varname = 'Not provided';				} else {					$$varname = $_SERVER[$ukey];				}			}						if (!$messages) {				// Check the header variables				$this->User->School->useModel();				$system = $this->User->School->find(array('school_name' => $system_school), array('id', 'secret'));				if (empty($system['School']['id'])) {					$messages[102] = 'Unrecognized system identifier';				} else if (empty($system['School']['secret'])) {					// Make this the same as above, to prevent security breaches					//$messages[102] = 'Unknown shared secret.';					$messages[102] = 'Unrecognized system identifier';				} else if (strcmp($message_hash, $this->pglms->hmac($timestamp.$system['School']['secret'], $this->pglms->urn()))) {					$messages[102] = 'Incorrect message authentication code';				} else if (abs(time() - strtotime($timestamp)) > 300) {					// The request is more than five minutes old; assume that it has been spoofed.					// (However, it could be a simple case of a misconfigured server.)					// Alternatively, we could store all served requests, and refuse to serve them twice.					$messages[105] = 'Invalid timestamp';				} else if (empty($school_uid)) {					// Assume that we want the school for the system signon					$school_id = $system['School']['id'];				} else if (is_numeric($school_uid)) {					$children = $this->User->School->findChildrenIds($system['School']['id']);					if (in_array(intval($school_uid, $children))) {						$school_id = intval($school_uid);					} else {						$messages[107] = 'Invalid school id';					}				} else {					$fragments = explode('.', $school_uid);					if (count($fragments) > 1) foreach ($fragments as $portion) if ($portion) $school_uid = $portion;					$children = $this->User->School->findChildrenIds($system['School']['id']);					$school_id = $this->User->School->field('id', array('School.school_name' => $school_uid, 'School.id' => $children));					if (empty($school_id)) $messages[107] = 'Invalid school identifier';				}			}						if (!empty($school_id)) {				if ($first_name and $last_name and $user_type and $user_uid) {					$user_id = $this->User->autocreate($school_id, $user_type, $first_name, $last_name, $email, $user_uid)						or $messages[$this->User->errnum] = $this->User->error;				}								if ($section_uid and $course_uid) {					$uid_info = explode('.', $course_uid);					if (count($uid_info) > 1) {						$course_uid = $uid_info[0];						$segment = $uid_info[1];						$num_segments = (count($uid_info) > 2)? $uid_info[2]: 2;					} else {						$segment = $num_segments = 1;					}										$section_id = $this->User->Section->autocreate($school_id, $course_uid,						$segment, $num_segments, null, $section_uid, null, $system_school);					if (empty($section_id)) $messages[$this->User->Section->errnum] = $this->User->Section->error;				}								if (!empty($user_id) and !empty($section_id)) {					$joined = $this->User->autojoin($section_id, $user_id, $user_type)						or $messages[108] = 'Invalid section';					if ($joined and !$next_page) $next_page = '/sections/calendar/'.$section_id;				}			}						if (empty($joined)) {				if (empty($messages)) $messages[100] = 'Unknown Error';				$url = null;			} else {				$this->User->School->useModel();				$secret = $this->User->School->field('secret', array('id' => 1));				$token = time().'.'.$user_id;				$token .= '-'.sha1($token.'@'.$secret);				$url = $this->pglms->full_url('/users/signon/'.$token);				if (!empty($next_page)) $url .= '?page='.$next_page;			}						if (empty($url)) {				if (empty($school_id)) $school_id = empty($system['School']['id'])? 1: $system['School']['id'];								$subject = 'SSO Token Error';				$body = "The SSO token-creation process has failed.\n";				foreach ($messages as $errnum => $msg) {					$body .= "Error #$errnum: $msg\n";				}								$body .= "\nRequest headers:\n";				foreach ($header_vars as $key => $varname) {					$msg = $$varname;					$body .= "$key: $msg\n";				}								$body .= "\nRequest parameters:\n";				foreach ($param_vars as $key => $varname) {					$msg = is_null($$varname)? 'Not provided': $$varname;					$body .= "$varname ($key): $msg\n";				}								$urn = $this->pglms->urn();				$body .= "\nRequest URN:\n$urn\n$_SERVER[REQUEST_URI]\n";				$this->User->School->mail_techs($school_id, $subject, $body);			}						$this->set('messages', $messages);			$this->set('login_url', $url);		}				function signon($token) {			// Function for a single-signon login.			// The token should have been recently generated by our system.			$error = "Your system failed to log you in properly.				<br>If it doesn't work in a few minutes, talk to your administrator.";			$info = explode('-', $token);			if (count($info) == 2) {				list($whenwho, $sha) = $info;				$this->User->School->useModel();				$secret = $this->User->School->field('secret', array('id' => 1));				if ($sha === sha1($whenwho.'@'.$secret)) {					$info = explode('.', $whenwho);					if (count($info) == 2) {						$when = intval($info[0]);						$who = intval($info[1]);						$diff = time() - $when;						if (-30 <= $diff and $diff <= 45) {							$this->User->useModel();							$user = $this->User->read(null, $who);							if ($user) {								//pr($user); exit;								$user_id = $who;								$this->auth->login($user['User']);							}						} else if ($diff > 30) {							$error = 'The system took too long to authenticate.<br>Please go back and try again.';						}					}				}			}						if (empty($user_id)) {				$this->flash($error, '/users/login');			} else {				$this->redirect($this->pglms->defaultPage($user_id));			}		}		function logout() {			$success = $this->auth->attemptLogout();			$this->flash($success? 'Logout successful': 'Logout failed', '/');		}		function view($id) {			$this->redirect('/users/profile/'.$id);		}		function profile($id = null) {			// this function deprecates function view()			if (empty($id)) {				$id = $this->uid;			} else if ($this->pglms->verifyUserAdmin($id, true)) {				$this->redirect('/users/edit/'.$id);			}						$this->User->useModel(array('User'));			$user = $this->User->findById($id, null, null, 0);			$this->set("user", $user['User']);			$this->pageTitle = 'User Profile';		}		function admin() {			// Default landing page for school administrators			$role = $this->pglms->verifyAdmin();			$this->User->useModel('School');			$user = $this->User->findById($this->uid, array('School.description', 'School.school_name'));			$sch_name = empty($user['School']['description'])? $user['School']['school_name']: $user['School']['description'];			$this->pageTitle = $sch_name.' Administration';			$this->set('role', $role);		}		function home() {			$this->redirect($this->pglms->defaultPage($this->uid));		}		function grades($section_id, $student_id=null, $filter='all') {			if (($student_id == $this->uid) or !is_numeric($student_id)) {				$role = $this->pglms->verifySectionUser($section_id, true);				$student_id = $this->uid;			} else {				// Verify that the user is a teacher of this section				$role = $this->pglms->verifySectionUser($section_id, false);			}						$status_lists = array(				'all' => array(),        // Every assignment				'done' => array(),       // Completed, and either dateless or already due				'future' => array(),     // Not completed, but not due yet either				'missed' => array(),     // Not completed, and past due				'ungraded' => array(),   // Completed, but ungraded				'extra' => array(),      // Extra Credit				'early' => array(),      // Completed, but not due yet				'deleted' => array(),    // Deleted grades				'pointless' => array(),  // Not worth points				'locked' => array(),     // Grades that lock an assignment				'retakes' => array(),    // Grades marked as allowing a retake			);						if (is_numeric($filter)) {				// Request for a specific assignment, usually from the section gradebook.				// Todo: What if the assignment hasn't been completed yet?				$grades = $this->User->Grade->findAll(array(					'Grade.assignment_id' => $filter,					'Grade.section_id' => $section_id,					'Grade.user_id' => $student_id,					'Grade.display_sequence' => '> 0',					'Grade.completion_status' => '!= Started',				), 'Grade.id', null, null, 1, 0);				if (count($grades) == 1) {					// Move directly along to the specified grade.					$this->redirect('/grades/view/'.$grades[0]['Grade']['id']);				} else {					$filter = intval($filter);					$status_lists[$filter] = array();				}			}						$grades = array();			$this->User->useModel();			$student = $this->User->read(array('User.id', 'User.first_name', 'User.last_name'), $student_id);			$calendar = $this->User->Section->calendar($section_id, $student_id);						$this->User->Section->useModel('Students');			$section = $this->User->Section->findById($section_id);			$dateless = empty($section['Section']['start_date']);						if (!$dateless) {				$now = time();				$now_info = getdate($now);				$today = mktime(0,0,0, $now_info['mon'], $now_info['mday'], $now_info['year']);			}						$required = array();			$progress = array();			foreach ($calendar as $key => $asn) {				$daynum = $asn['SectionsAssgmt']['day_number'];				$aid = $asn['Assignment']['id'];				$status = array(					'Done' => 0,					'Retake' => 0,					'Locked' => 0,					'Started' => 0,				);								if (!empty($asn[0]['completion'])) {					foreach (explode(',', $asn[0]['completion']) as $stat) {						if (array_key_exists($stat, $status)) {							$status[$stat] += 1;						} else {							// Todo: Issue a warning, but only on dev platforms						}					}				}								if ($deleted = intval($asn[0]['deleted_grades'])) {					$status_lists['deleted'][$key] = $deleted;				}								extract($status);				//$worth = round(intval($asn['Assignment']['points']) * $asn['SectionsAssgmt']['weight'] / 100);				$worth = intval($asn['Assignment']['points']);				if ($calendar[$key]['worth'] = $worth) {					$status_lists['all'][$key] = 1;					if ($Locked) $status_lists['locked'][$key] = 1;					if ($Retake) $status_lists['retakes'][$key] = 1;					if ($asn['SectionsAssgmt']['extra_credit_flag']) $status_lists['extra'][$key] = 1;					$due = (!$dateless and !empty($asn['SectionsAssgmt']['date_due']) and strtotime($asn['SectionsAssgmt']['date_due']) < $today);					$status = $Done? (($dateless or $due)? 'done': 'early'): (($dateless or !$due)? 'future': 'missed');					$status_lists[$status][$key] = 1;										if ($Done or $Locked or $deleted) {						$this->User->Grade->useModel();						$grades = $this->User->Grade->findAll(array(							'Grade.user_id' => $student_id,							'Grade.section_id' => $section_id,							'Grade.assignment_id' => $aid,							'Grade.completion_status' => array('Done', 'Locked'),						));												$ungraded = 0;						foreach ($grades as $grade) {							if ($grade['Grade']['date_submitted'] and $grade['Grade']['display_sequence'] > 0 and !$grade['Grade']['date_graded']) {								$parsed['ungraded'][] = $grade;								$ungraded += 1;							}						}												if ($ungraded) $status_lists['ungraded'][$key] = $ungraded;						$calendar[$key]['grades'] = $grades;					}				} else {					$status_lists['pointless'][$key] = 1;				}								if ($asn[0]['num_items']) {					if (array_key_exists($daynum, $required)) {						$required[$daynum] += 1;						if ($Done) $progress[$daynum] += 1;					} else {						$required[$daynum] = 1;						$progress[$daynum] = ($Done? 1: 0);					}				}								if ($filter == $aid) {					$status_lists[$filter][$key] = 1;				}			}						// Cheat: Instead of calculating the grade, let the database do it.			$conditions = array('Taking.id' => intval($section_id), 'User.id' => intval($student_id));			$current_grade = $this->User->current_grades($conditions, $dateless);			$full_grade = $this->User->full_grades($conditions);						$this->set(array(				'calendar' => $calendar,				'required' => $required,				'progress' => $progress,				'student' => $student,				'section' => $section,				'filter' => $filter,				'lists' => $status_lists,				'role' => $role,								'points' => intval($full_grade[0]['Summary']['earned']),				'max_points' => intval($full_grade[0]['Summary']['possible']),				'ptd' => intval($current_grade[0]['Summary']['earned']),				'max_ptd' => intval($current_grade[0]['Summary']['possible']),			));		}				function edit($id) {			// Todo: Allow teachers to edit their own profiles			$role = $this->pglms->verifyUserAdmin($id, true) or $this->redirect('/users/profile/'.$id);						$this->User->useModel(array('User', 'Type', 'Taking', 'Teaching'));			$user = $this->User->read(null, $id);			$enrollment = array();			foreach ($user[$user['Type']['name'] == 'student'? 'Taking': 'Teaching'] as $section) {				$enrollment[] = $section['id'];			}						$this->User->Section->useModel(array('Section', 'School'));			$sections = $this->User->Section->findAll(array(				'Section.school_id' => $this->pglms->adminSchools(),				'or' => array('Section.end_date' => null, 'end_date' => '>= '.$this->User->date()),				'Section.display_sequence' => '> 0',			), array('Section.id', 'Section.section_name', 'Section.client_section_id', 'School.school_name'));						$this->pageTitle = ($this->uid == $id)? 'My Profile' : 'Edit ' . ucfirst($user['Type']['name']);			$this->set('sections', $sections);			$this->set('enrollment', $enrollment);			$this->set('user', $user['User']);			$this->set('type', $user['Type']);			$this->set('role', $role);		}				function save($student_id) {			if (!$this->pglms->verifyUserAdmin($student_id, true)) {				$this->flashback('Permission Denied.', '/users/profile/'.$student_id);			} else if (empty($this->data['User'])) {				$this->flashback('There was a problem loading the form data', '/users/edit/'.$student_id);			} else {				$this->User->id = $student_id;				if ($this->User->save($this->data['User'])) {					// Modify the sections					if (!empty($this->data['Section'])) {						$user = $this->User->read(null, $student_id);						$user_id = $user['User']['id'];						$user_type = $user['Type']['name'];						$user_name = $user['User']['first_name'] . ' ' . $user['User']['last_name'];												$messages = array('Your changes have been saved.');						$selected = $this->data['Section']['id'];												$keepers = array();						foreach ($user[$user['Type']['name'] == 'student'? 'Taking': 'Teaching'] as $section) {							$section_id = $section['id'];							if (in_array($section_id, $selected)) {								$keepers[$section_id] = true;							} else if (!$this->SectionsUser->del($section_id, $user_id, $user_type)) {								$section_name = empty($section['section_name'])? 'section #'.$section_id: $section['section_name'];								$messages[] = "$user_name could not be removed from $section_name.";							}						}												foreach ($selected as $section_id) {							if (empty($keepers[$section_id])) {								$section = $this->User->Section->read(array('Section.school_id', 'Section.section_name'), $section_id);								$section_name = empty($section['Section']['section_name'])? 'section #'.$section_id: $section['Section']['section_name'];																if ($this->pglms->verifyAdmin($section['Section']['school_id'], false)) {									$result = $this->SectionsUser->add($section_id, $user_id, $user_type);																		if (!$result) {										$messages[] = "$user_name could not be added to $section_name.";									}								} else {									$messages[] = "You do not have permission to add students to $section_name.";								}							}						}					}										$this->flashwayback(join("\n<BR>", $messages), '/users/edit/'.$student_id);				} else {					$this->flashback('There was a problem saving your changes.', '/users/edit/'.$student_id);				}			}		}		function add($user_type, $school_id = null) {			$this->pglms->verifyAdmin($school_id);						if (!empty($this->data['User'])) {				// give this user the proper role (user_type_id)				$role = $this->User->UserType->findByName($user_type, 'UserType.id', null, 0);				$this->data['User']['user_type_id'] = $role['UserType']['id'];				$this->data['User']['user_type'] = $user_type;				// give this user the proper school_id				$this->data['User']['school_id'] = $school_id? $school_id: $this->Session->read('school_id');				// define the creation date				$this->data['User']['date_created'] = date('Y-m-d H:i:s');				$this->User->id = null;				if ($this->User->save($this->data['User'])) {					if (!empty($this->data['Section'])) {						$result = $this->SectionsUser->add($this->data['Section']['id'], $this->User->id, $user_type);					} 										$this->flashwayback("The new $user_type has been added.", '/users/'.$user_type);				} else {					$message = "There was a problem saving the $user_type data.";					if (!empty($this->User->validationErrors)) {						$fields = array();						foreach ($this->User->validationErrors as $field => $invalid) {							if ($invalid) $fields[] = $field;						}												if ($fields) {							$field_names = $this->pglms->expand_list($fields);							$s = count($fields) == 1? '': 's';							$message .= "<br>Please check the $field_names field$s.";						}					}										$this->Session->setFlash($message);				}			}						$user = $this->User->read();			if (in_array($user_type, array('student', 'teacher'))) {				$this->User->Section->useModel();				$sections = $this->User->Section->findAll(array(					'Section.school_id' => $school_id? $school_id: $this->pglms->adminSchools(),					'or' => array(						'end_date' => null,						'Section.end_date' => '>= ' . $this->User->date(),					),					'Section.display_sequence' => '> 0',				));			} else {				$sections = null;			}						$this->set('school_id', $school_id);			$this->set('sections', $sections);			$this->set('user', $user['User']);			$this->set('user_type', $user_type);		}				function delete($user_id) {			if ($this->pglms->verifyUserAdmin($user_id, false)) {				$result = $this->User->delete($user_id);				$message = $result? 'User deleted.': 'Failed to delete user.';			} else {				$message = 'You lack permission to delete this user.';			}						$this->flashback($message, '/users/students');		}				function teaching($school_id = null, $teacher_id = null) {			if (empty($teacher_id)) $teacher_id = $this->uid;			$students = array();			$conditions = array(				'User.id' => intval($teacher_id),				'SectionsUser.dropped' => null,				'SectionsUser.role' => 'teacher',			);						if ($school_id) $conditions['Section.school_id'] = intval($school_id);			$teaching = $this->SectionsUser->findAll($conditions);			foreach ($teaching as $section) {				$taking = $this->SectionsUser->findAll(array(					'Section.id' => $section['Section']['id'],					'SectionsUser.dropped' => null,					'SectionsUser.role' => 'student',				));								foreach ($taking as $student) {					$key = $student['User']['last_name'].'/'.$student['User']['first_name'].'/'.$student['User']['id'];					$students[$key][] = $student;				}			}						ksort($students);			$this->pageTitle = 'Students in Your Sections';			$this->set('students', $students);			$this->set('sections', $teaching);		}				function teachers($sequence = '> 0') {			$schools = $this->pglms->adminSchools();			$criteria = $this->pglms->search_conditions(array(				'User.username',				'User.client_student_id',				'User.first_name',				'User.last_name',				'User.email',			), array(				'User.user_type' => 'teacher',				'User.school_id' => $schools,				'User.display_sequence' => $sequence,			));						list($order, $limit, $page) = $this->Pagination->init($criteria);			$order = array($order, 'User.last_name', 'User.first_name');			$this->User->useModel('Teaching');			$teachers = $this->User->findAll($criteria, null, $order, $limit, $page);						foreach ($teachers as $key => $teacher) {				$teachers[$key]['lag_time'] = $this->User->getLagTime($teacher['User']['id']);			}						$this->set('teachers', $teachers);		}		function students($sequence = '> 0') {			$criteria = $this->pglms->search_conditions(array(				'User.username',				'User.client_student_id',				'User.first_name',				'User.last_name',				'User.email',			), array(				'User.user_type' => 'student',				'User.school_id' => $this->pglms->adminSchools(),				'User.display_sequence' => $sequence,			));						list($order, $limit, $page) = $this->Pagination->init($criteria);			$order = array($order, 'User.last_name', 'User.first_name');			$this->User->useModel(array('User', 'Type', 'Taking'));			$students = $this->User->findAll($criteria, null, $order, $limit, $page);			$this->set('students', $students);		}				function unstarted($section_id = null) {			// CSV report of students who haven't logged in or done anything			$this->pglms->blankLayout();						if ($section_id) {				$sections = array($this->User->Section->read(null, $section_id));				$this->pglms->verifyAdmin($sections[0]['Section']['school_id']);				$unlogged = $this->SectionsUser->findAll(array(					'User.last_login' => null,					'SectionsUser.section_id' => $section_id,					'SectionsUser.dropped' => null,					'SectionsUser.role' => 'student',				));			} else {				$this->pglms->verifyAdmin();				$schools = $this->pglms->adminSchools();				$sections = $this->User->Section->findAll(array('Section.school_id' => $schools));				$unlogged = $this->User->findAll(array(					'User.last_login' => null,					'User.school_id' => $schools,				));			}						$this->set('sections', $sections);			$this->set('unlogged', $unlogged);		}				function dns($section_id = null) {			// "Did Not Start" report -- students who haven't logged in or done anything			if (!empty($this->data['Section']['id'])) $section_id = intval($this->data['Section']['id']);			if ($section_id) {				$this->pglms->verifySectionUser($section_id);				$this->User->Section->useModel(array('Section', 'Teachers'));				$section = $this->User->Section->read(null, $section_id);				$this->set('section', $section);				$this->SectionsUser->useModel(array('User'));				$this->SectionsUser->User->useModel(array('School'));				$unlogged = $this->SectionsUser->findAll(array(					'User.last_login' => null,					'SectionsUser.dropped' => null,					'SectionsUser.role' => 'student',					'SectionsUser.section_id' => $section_id,				), null, null, null, null, 2);			} else {				$this->pglms->verifyAdmin();				$schools = $this->pglms->adminSchools();				$this->User->useModel(array('User', 'School', 'Taking'));				$this->User->School->useModel(array());				$this->User->Taking->useModel(array('Teachers'));				$unlogged = $this->User->findAll(array(					'User.last_login' => null,					'User.school_id' => $schools,				), null, null, null, null, 2);			}						$this->set('unlogged', $unlogged);		}				function delinquency($section_id = null, $days = null) {			if (!empty($this->data['Days']['number'])) $days = intval($this->data['Days']['number']);			if (!empty($this->data['Section']['id'])) $section_id = intval($this->data['Section']['id']);						$conditions = array('Type.name' => 'student');			if (!empty($section_id) and is_numeric($section_id)) {				$this->pglms->verifySectionUser($section_id);				$conditions['Taking.id'] = $section_id;			} else if ($this->pglms->verifyAdmin(null, false)) {				$school_list = $this->pglms->adminSchools();				$conditions['and']['or'] = array(					'Taking.school_id' => $school_list,					'User.school_id' => $school_list,				);			} else {				$this->User->useModel(array('Teaching'));				$user = $this->User->read('User.id', $this->uid);				$sections = array();				foreach ($user['Teaching'] as $section) $sections[] = $section['id'];				if ($sections) {					$conditions['Taking.id'] = $sections;				} else {					$conditions['Taking.id'] = null;					$conditions['User.id'] = $this->uid;				}			}						if ($days and is_numeric($days)) {				$conditions['or'] = array(					'and' => array('User.last_login' => null),					'User.last_login' => date('< YmdHis', time() - (60*60*24*$days)),				);			} else {				$conditions['User.last_login'] = null;			}						$this->User->useModel(array('User', 'Type', 'School', 'Taking'));			$users = $this->User->findAll($conditions, array(				'User.first_name',				'User.last_name',				'User.client_student_id',				'User.last_login',				'Taking.section_name',				'Taking.client_section_id',				'School.school_name',			), array('User.last_name', 'User.first_name'));						$this->set('users', $users);			$this->set('days', is_numeric($days)? $days: 0);			$this->set('section_id', is_numeric($section_id)? $section_id: 'all');		}				function failing($section_id = null, $threshold = null, $flags = null) {			if (!empty($this->data['Section']['id'])) $section_id = intval($this->data['Section']['id']);			if (isset($this->data['Grades']['threshold'])) $threshold = $this->data['Grades']['threshold'];			if ($threshold == 'zero') $threshold = 0;			$flag_list = array('done' => false, 'future' => false, 'missing' => false, 'ungraded' => false, 'extra' => false, 'early' => false);			$flags_requested = empty($flags)? array(): explode('_', $flags);			foreach (array_keys($flag_list) as $flag) {				if (!empty($this->data['Grades'][$flag])) $flag_list[$flag] = true;				if (in_array($flag, $flags_requested)) $flag_list[$flag] = true;			}						$conditions = array();			if (!empty($section_id) and is_numeric($section_id)) {				$this->pglms->verifySectionUser($section_id);				$conditions['Taking.id'] = $section_id;			} else if ($this->pglms->verifyAdmin(null, false)) {				$school_list = $this->pglms->adminSchools();				$conditions['or'] = array(					'Taking.school_id' => $school_list,					'User.school_id' => $school_list,				);			} else {				$this->User->useModel(array('Teaching'));				$user = $this->User->read('User.id', $this->uid);				$sections = array();				foreach ($user['Teaching'] as $section) $sections[] = $section['id'];				if ($sections) {					$conditions['Taking.id'] = $sections;				} else {					$conditions['User.id'] = $this->uid;				}			}						$users = $this->User->getStudentGrades($conditions, $threshold, $flag_list);						$this->set('users', $users);			$this->set('flags', $flag_list);			$this->set('threshold', $threshold? $threshold: 'zero');			$this->set('section_id', is_numeric($section_id)? $section_id: 'all');		}	}?>