summaryrefslogtreecommitdiffstats
path: root/modules/custom/drop_jobs_recommendation/drop_jobs_recommendation.module
blob: ce427afeef6089449a0165ce38a7adfc405071d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
/**
 * @file
 * Drop Jobs Job Recommendation module.
 */
include_once('drop_jobs_recommendation.features.inc');

/**
 * Implements hook_permission().
 */
function drop_jobs_recommendation_permission() {
  return array(
    'administer drop_jobs_recommendation' => array(
      'title' => t('Administer Job Recommendations'),
    ),
    'delete own drop_jobs_recommendation' => array(
      'title' => t('Delete own Job Recommendations'),
    ),
    'view drop_jobs_recommendation' => array(
      'title' => t('View Job Recommendations'),
    ),
  );
}

/**
 * Implements hook_init().
 */
function drop_jobs_recommendation_init() {
  // Add admin section styles.
  if (drop_jobs_is_admin_page()) {
    drupal_add_css(drupal_get_path('module', 'drop_jobs_recommendation') . '/css/drop_jobs_recommendation_admin.css', array('group' => CSS_THEME, 'every_page' => TRUE));
  }
}

/**
 * Implements hook_entity_info().
 */
function drop_jobs_recommendation_entity_info() {
  $info = array();

  $info['drop_jobs_recommendation'] = array(
    'label' => t('Job Recommendation'),
    'entity class' => 'DropJobsRecommendation',
    'controller class' => 'DropJobsRecommendationController',
    'base table' => 'drop_jobs_recommendation',
    'fieldable' => FALSE,
    'entity keys' => array(
      'id' => 'rid',
    ),
    'bundles' => array(),
    'uri callback' => 'entity_class_uri',
    'label callback' => 'entity_class_label',
    'access callback' => 'drop_jobs_recommendation_access',
    'module' => 'drop_jobs_recommendation',
    'metadata controller class' => 'DropJobsRecommendationMetadataController',
    'views controller class' => 'DropJobsRecommendationViewsController',
  );

  // Support entity cache module.
  if (module_exists('entitycache')) {
    $info['recommendation']['entity cache'] = TRUE;
    // Entity cache obsoletes field cache.
    $info['recommendation']['field cache'] = FALSE;
  }

  return $info;
}

/**
 * Implements hook_menu().
 */
function drop_jobs_recommendation_menu() {
  $items = array();

  // Admin settings form.
  $items['admin/config/drop_jobs/recommendation'] = array(
    'title' => 'Recommendation',
    'description' => 'Configure Job Recommendations.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('drop_jobs_recommendation_settings'),
    'access arguments' => array('administer drop_jobs_recommendation'),
    'file' => 'drop_jobs_recommendation.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );

  // For the sake of convenience.
  $recommendation_uri = 'recommendation/%drop_jobs_recommendation';
  $recommendation_uri_argument_position = 1;

  // View recommendation.
  $items[$recommendation_uri] = array(
    'title callback' => 'drop_jobs_recommendation_page_title',
    'title arguments' => array($recommendation_uri_argument_position),
    'page callback' => 'drop_jobs_recommendation_view',
    'page arguments' => array($recommendation_uri_argument_position),
    'access callback' => 'entity_access',
    'access arguments' => array('view', 'drop_jobs_recommendation', $recommendation_uri_argument_position),
  );

  $items[$recommendation_uri . '/view'] = array(
    'title' => 'View',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );

  // Edit recommendation.
  $items[$recommendation_uri . '/edit'] = array(
    'title' => 'Edit',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('drop_jobs_recommendation_form', $recommendation_uri_argument_position),
    'access callback' => 'entity_access',
    'access arguments' => array('edit', 'drop_jobs_recommendation', $recommendation_uri_argument_position),
    'file' => 'drop_jobs_recommendation.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  );

  // Delete recommendation.
  $items[$recommendation_uri . '/delete'] = array(
    'title' => 'Delete recommendation',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('drop_jobs_recommendation_delete_form', $recommendation_uri_argument_position),
    'access callback' => 'entity_access',
    'access arguments' => array('edit', 'drop_jobs_recommendation', $recommendation_uri_argument_position),
    'file' => 'drop_jobs_recommendation.admin.inc',
  );

  // Devel integration.
  if (module_exists('devel')) {
    $devel_path = drupal_get_path('module', 'devel');
    $items[$recommendation_uri . '/devel'] = array(
      'title' => 'Devel',
      'page callback' => 'devel_load_object',
      'file' => 'devel.pages.inc',
      'file path' => $devel_path,
      'page arguments' => array('drop_jobs_recommendation', $recommendation_uri_argument_position),
      'access arguments' => array('access devel information'),
      'type' => MENU_LOCAL_TASK,
      'weight' => 100,
    );

    $items[$recommendation_uri . '/devel/load'] = array(
      'title' => 'Load',
      'type' => MENU_DEFAULT_LOCAL_TASK,
    );

    $items[$recommendation_uri . '/devel/render'] = array(
      'title' => 'Render',
      'page callback' => 'devel_render_object',
      'page arguments' => array('drop_jobs_recommendation', $recommendation_uri_argument_position),
      'access arguments' => array('access devel information'),
      'file' => 'devel.pages.inc',
      'file path' => $devel_path,
      'type' => MENU_LOCAL_TASK,
      'weight' => 100,
    );
  }

  return $items;
}

/**
 * Determines whether the given user has access to a recommendation operation.
 *
 * @param $op
 *   The operation being performed. One of 'view', 'update', 'create', 'delete'
 *   or just 'edit' (being the same as 'create' or 'update').
 * @param $recommendation
 *   Optionally a recommendation to check access for. If nothing is given,
 *    access for all recommendations is determined.
 * @param $account
 *   The user to check for. Leave it to NULL to check for the current user.
 * @return boolean
 *   Whether access is allowed or not.
 */
function drop_jobs_recommendation_access($op, $recommendation = NULL, $account = NULL) {
  if (!isset($account)) {
    $account = user_uid_optional_load();
  }

  if (user_access('administer drop_jobs_recommendation')) {
    return TRUE;
  }

  $node = node_load($recommendation->nid);
  $own_recommendation = ($account->uid == $recommendation->uid || $account->uid == $node->uid);

  switch ($op) {
    case 'delete':
      return user_access('delete own drop_jobs_recommendation') && $own_recommendation;
    case 'view':
      // Allow the user to view the recommendation of its for him or
      // from one of his jobs.
      return user_access('view drop_jobs_recommendation') && $own_recommendation;
    default:
      return FALSE;
  }
}

/**
 * Menu title callback.
 * Returns page title for recommendation-related menu items.
 *
 * @param DropJobsRecommendation
 *   The $recommendation we want to get a title for.
 * @return string
 *   The recommendation's label.
 *
 * @see drop_jobs_recommendation_menu()
 */
function drop_jobs_recommendation_page_title(DropJobsRecommendation $recommendation) {
  return $recommendation->label();
}

/**
 * Deletes a recommendation.
 *
 * @param DropJobsRecommendation
 *   The recommendation object.
 */
function drop_jobs_recommendation_delete(DropJobsRecommendation $recommendation) {
  $recommendation->delete();
}

/**
 * Delete multiple recommendations.
 *
 * @param array
 *   An array of recommendation IDs.
 */
function drop_jobs_recommendation_delete_multiple(array $rids) {
  // Bypass the traditional entity_get_controller() approach because we
  // need to cancel each recommendation before its deleted.
  foreach ($rids as $rid) {
    if ($recommendation = drop_jobs_recommendation_load($rid)) {
      drop_jobs_recommendation_delete($recommendation);
    }
  }
}

/**
 * Create a new recommendation object.
 *
 * @param $values
 *   An array of values to initialize the recommendation with.
 * @return DropJobsRecommendation
 *   The newly-created recommendation object.
 */
function drop_jobs_recommendation_create(array $values) {
  return new DropJobsRecommendation($values);
}

/**
 * Saves a recommendation.
 *
 * @param $recommendation
 *   The recommendation object.
 * @return boolean
 *   Whether the recommendation was saved successfully.
 */
function drop_jobs_recommendation_save(DropJobsRecommendation $recommendation) {
  return $recommendation->save();
}

/**
 * Fetch a recommendation object.
 *
 * @param $rid
 *   Integer specifying the recommendation id.
 * @param $reset
 *   A boolean indicating that the internal cache should be reset.
 * @return
 *   A fully-loaded $recommendation object or FALSE if it cannot be loaded.
 *
 * @see drop_jobs_recommendation_load_multiple()
 */
function drop_jobs_recommendation_load($rid, $reset = FALSE) {
  $recommendations = drop_jobs_recommendation_load_multiple(array($rid), array(), $reset);
  return reset($recommendations);
}

/**
 * Load multiple recommendations based on certain conditions.
 *
 * @param $rids
 *   An array of recommendation IDs.
 * @param $conditions
 *   An array of conditions to match against the {drop_jobs_recommendation} table.
 * @param $reset
 *   A boolean indicating that the internal cache should be reset.
 * @return
 *   An array of recommendation objects, indexed by ID.
 *
 * @see entity_load()
 * @see drop_jobs_recommendation_load()
 * @see drop_jobs_recommendation_load_by_user()
 */
function drop_jobs_recommendation_load_multiple($rids = array(), $conditions = array(), $reset = FALSE) {
  return entity_load('drop_jobs_recommendation', $rids, $conditions, $reset);
}

// @TODO load by user/job

/**
 * DropJobsRecommendationcription view callback.
 * Displays a recommendation entity.
 *
 * This is here as opposed to in drop_jobs_recommendation.pages.inc because if Devel is enabled
 * clicking on Devel->Render will trigger an error as the include is not
 * automatically included in that case.
 *
 * @param DropJobsRecommendation
 *   The $recommendation we want to view.
 * @return string
 *   The rendered recommendation entity.
 *
 * @see drop_jobs_recommendation_menu()
 */
function drop_jobs_recommendation_view(DropJobsRecommendation $recommendation) {
  drupal_set_title(entity_label('drop_jobs_recommendation', $recommendation));
  return entity_view('drop_jobs_recommendation', array(entity_id('drop_jobs_recommendation', $recommendation) => $recommendation), 'full');
}

/**
 * Implements hook_cron().
 */
function drop_jobs_recommendation_cron() {
  if (variable_get_value('drop_jobs_recommendation_candidate_enable') || variable_get_value('drop_jobs_recommendation_job_enable')) {
    // Find Job Recommendations for candidates. This actually works both ways
    // as if a job is recommended for a candidate we can safely assume that
    // that candidate would be recommended for that job.
    $candidates = drop_jobs_candidate_get_pids();

    if (!empty($candidates)) {
      $queue = DrupalQueue::get('drop_jobs_recommendation_queue');
      foreach ($candidates as $pid) {
        $candidate = profile2_load($pid);
        $queue->createItem($candidate);
      }
    }
  }
}

/**
 * Implements hook_cron_queue_info().
 */
function drop_jobs_recommendation_cron_queue_info() {
  return array(
    'drop_jobs_recommendation_queue' => array(
      'worker callback' => 'drop_jobs_recommendation_cron_run',
    // Default time setting should be ok.
    ),
  );
}

/**
 * Worker callback.
 *
 * Finds Job Recommendations for a given candidate.
 *
 * @param Profile
 *   The candidate $profile object.
 *
 * @see drop_jobs_recommendation_cron()
 * @see drop_jobs_recommendation_cron_queue_info()
 */
function drop_jobs_recommendation_cron_run(Profile $candidate) {
  // @TODO implement this.
//  $jobs = drop_jobs_recommendation_find($candidate);
//  include_once drupal_get_path('module', 'devel') . '/krumo/class.krumo.php';
//  krumo($jobs);die;
}

/**
 * Find Job Recommendations for a given candidate.
 *
 * @param Profile
 *   The candidate profile.
 * @return array
 *   An array of recommended jobs, keyed by nid.
 */
function drop_jobs_recommendation_find(Profile $candidate) {
  // @TODO implement this.
//  include_once drupal_get_path('module', 'devel') . '/krumo/class.krumo.php';
//  krumo($candidate);
//  die;
}

/**
 * Returns a map of job fields to candidate fields.
 *
 * @return array
 *   An array where the key is the job field and value is corresponding
 *   candidate field.
 */
function drop_jobs_recommendation_map() {
  $map = array();

  $map['field_job_field'] = 'field_candidate_exp_field';
  $map['field_job_study'] = 'field_candidate_edu_field';
  $map['field_job_type'] = 'field_candidate_pref_occupation';
  $map['field_job_languages'] = 'field_candidate_language';
  $map['field_job_experience'] = 'field_candidate_exp_time';
  $map['title'] = 'field_candidate_pref_title';

  // Allow the map to be altered.
  drupal_alter('drop_jobs_recommendation', $map);
  return $map;
}

/**
 * Returns the bias for each job recommendation field.
 *
 * @return array
 *   An array where the key is the field and the value is the bias.
 */
function drop_jobs_recommendation_bias() {
  $fields = array();

  foreach (drop_jobs_recommendation_map() as $job_field => $candidate_field) {
    $fields[$job_field] = variable_get("djrb_{$job_field}", 0);
  }

  return $fields;
}

/**
 * Implements hook_variable_info().
 */
function drop_jobs_recommendation_variable_info() {
  $variables = array();

  $variables['drop_jobs_recommendation_candidate_enable'] = array(
    'title' => t('Recommend jobs for candidates'),
    'default' => FALSE,
    'group' => 'drop_jobs_recommendation',
    'token' => FALSE,
  );

  $variables['drop_jobs_recommendation_candidate_subs'] = array(
    'title' => t('Recommend jobs only for candidates with active subscriptions'),
    'default' => FALSE,
    'group' => 'drop_jobs_recommendation',
    'token' => FALSE,
  );

  $variables['drop_jobs_recommendation_candidate_subs_plans'] = array(
    'title' => t('Recommend jobs only for candidates with active subscriptions of plans'),
    'default' => array(),
    'group' => 'drop_jobs_recommendation',
    'token' => FALSE,
  );

  $variables['drop_jobs_recommendation_job_enable'] = array(
    'title' => t('Recommend candidates for jobs'),
    'default' => FALSE,
    'group' => 'drop_jobs_recommendation',
    'token' => FALSE,
  );

  $variables['drop_jobs_recommendation_job_subs'] = array(
    'title' => t("Recommend candidates only for jobs who's employers have active subscriptions"),
    'default' => FALSE,
    'group' => 'drop_jobs_recommendation',
    'token' => FALSE,
  );

  $variables['drop_jobs_recommendation_job_subs_plans'] = array(
    'title' => t('Recommend candidates only for jobs with active subscriptions of plans'),
    'default' => array(),
    'group' => 'drop_jobs_recommendation',
    'token' => FALSE,
  );

  return $variables;
}

/**
 * Implements hook_help().
 */
function drop_jobs_recommendation_help($path, $arg) {
  switch ($path) {
    case 'dashboard/my-recommendations':
      return t('Here you can view, edit and delete your recommendations.');
    case 'admin/config/drop_jobs/recommendation':
      return t('Here you can configure settings for job recommendations. This module can both recommend jobs for candidates as well as candidates for jobs.');
    case 'admin/drop_jobs/recommendations':
      return t('Here you can view, edit and delete all job recommendations.');
      break;
  }
}